% % Anyone have any good leads on such an awk (or even perl) script?
%
% I have one somewhere. I got it as an example with either mawk or C-awk
% (that was an `old' awk by some fellow from BC, and I think it
% later became the basis for gawk).
% As I recall, it prints lines of code, lines of pre-processor directives, and
% lines of comments, but I haven't run it in 8 years or so, so I don't
% really recall. I'll see if I can dig it up and post it this week-end.
Or even today. I had a bit of a booze-up, but I've returned home much earlier
than I ever thought possible. I must be getting old. Anyway, here are a few
awk scripts, first c_count:
# -8<----8<--- c_count.awk begins
# Date: 09-15-88 12:33
# From: Dan Kozak
# count lines in a C program, not counting comments,
# blank lines or form feeds. Does separate count of
# preprocessor directives if a preprocessor directive
# is commented out, it does not count it.
{
if (file == "") {
file = FILENAME
}
if (file != FILENAME) {
printf("Number of lines in %s is: %d\n",file,nl+ppd)
printf("Number of preprocessor directives is: %d\n",ppd)
printf("Number of lines excluding preprocessor directives is: %d\n\n",nl)
file = FILENAME
tnl += nl
tppd += ppd
nl = 0
ppd = 0
}
if ($0 == "") { ; }
else if ($1 ~ /^\/\*/ && $NF ~ /\*\/$/) { ; }
else if ($0 ~ /\/\*/ && $0 !~ /\*\//) { in_comment = 1 }
else if ($0 !~ /\/\*/ && $0 ~ /\*\//) { in_comment = 0 }
else if (in_comment) { ; }
else if ($1 ~ /^#/) { ppd++ }
else { nl++ }
Quote:
}
END { printf("Number of lines in %s is: %d\n",file,nl+ppd)
printf("Number of preprocessor directives is: %d\n",ppd)
printf("Number of lines excluding preprocessor directives is: %d\n\n",nl)
file = FILENAME
tnl += nl
tppd += ppd
printf("Total number of lines is: %d\n",tnl+tppd)
printf("Number of preprocessor directives is: %d\n",tppd)
printf("Number of lines excluding preprocessor directives is: %d\n",tnl)
}
# ->8---->8--- c_count.awk ends
and here's Jon Bentleys' m1 macro processor:
# -8<----8<--- m1.awk begins
# From Jon Bentley's article in Computer Language June '90 (v7n6)
# LISTING 4
function error(s) {
print "m1: " s >"CON" #
exit 1
Quote:
}
function dofile(fname, savefile, savebuffer, newstring) {
if (fname in activefiles)
error("recursively reading file: " fname)
activefiles[fname] = 1
savefile = file
file = fname
savebuffer = buffer
buffer = ""
while (readline() != EOF) {
print $0
}
dodef(1) #
}
dodef(0) #
}
if (NF != 2) error("bad inlcude line")
dofile(dosubs($2))
}
if (NF != 2) error("bad if line")
if (!($2 in symtab) || symtab[$2] == 0) gobble()
}
if (NF != 2) error("bad unless line")
if (($2 in symtab) && symtab[$2] != 0) gobble()
}
}
}
else {
newstring = dosubs($0)
print newstring
else
buffer = newstring "\n" buffer
}
}
close(fname)
delete activefiles[fname]
file = savefile
buffer = savebuffer
Quote:
}
function readline( i, status) {
status = ""
if (buffer != "") {
i = index(buffer, "\n")
$0 = substr(buffer, 1, i - 1)
buffer = substr(buffer, i + 1)
}
else {
if (file == "-") { #
i = getline #
} #
else { #
i = getline < file #
} #
status = i <= 0 ? "EOF" : "" #
}
return status
Quote:
}
function gobble( ifdepth) {
ifdepth = 1
while (readline()) {
}
Quote:
}
function dosubs(s, l, r, i, m) {
l = "" # Left of current pos: ready for output
r = s # Right of current: unexamined at this time
l = l substr(r, 1, i - 1)
if (i == 0) {
break
}
m = substr(r, 1, i - 1)
r = substr(r, i + 1)
if (m in symtab) {
r = symtab[m] r
}
else {
}
}
return l r
Quote:
}
function dodef(def, str, name) {
name = $2
sub(/^[ \t]*[^ \t]+[ \t]+[^ \t]+[ \t]+/, "") # $0=$P($0,FS,3,NF)
str = $0
while (str ~ /\\$/) {
if (readline() == EOF)
error("EOF inside definition")
sub(/^[ \t]+/, "")
sub(/[ \t]*\\$/, "\n" $0, str)
}
if (def || !(name in symtab)) #
symtab[name] = str
Quote:
}
BEGIN {
EOF = "EOF"
if (ARGC == 1) dofile("-") #
else if (ARGC == 2) dofile(ARGV[1])
else {
print "Usage: m1 [file]" >"CON" #
exit
}
Quote:
}
# ->8---->8--- m1.awk ends
and some sample data for m1:
This area was profoundly influenced by the groundbreaking work of Professor
# ->8---->8--- m1.dat ends
this situation many times yourself.
Sincerly,
# ->8---->8--- sayno.mac ends
# ->8---->8--- sayno.def ends
and finally a cross referencing program
# -8<----8<--- xref.awk begins
# generate cross reference of identifiers in a program
# Original program courtesy Bruce Feist of Arlington VA
{
# remove non alphanumeric characters
# convert to upper case
$0 = toupper($0)
# add reference
for (i = 1; i <= NF; i++)
{
if ($i !~ /^[0-9]+$/ && done[$i] != NR) # check if number or done
{
done[$i] = NR # mark as done
xref[$i] = xref[$i] " " NR # add reference
}
}
Quote:
}
END {
for (i in xref)
print i ": ", xref[i]
Quote:
}
# ->8---->8--- xref.awk ends
--
Patrick TJ McPhee
East York Canada