# This AWK program performs substantially the same function as the # dedicated ctags program available under UNIX, except # it does it on AWK source files. # Invoke this program like this: # awk -f awktags.awk *.awk > tags # This function adds a tag to the tag array. # Name is the name of the tag. # Line is the line on which the tag appears. # Truncate line to 60 characters; that should be enough to be unique. function addtag(name,line) { # Change all occurences of "/" to "\/" and "\" to "\\" gsub(/\\/,"\\\\",line) gsub(/\//,"\\/",line) tag[name] = FILENAME "\t/^" substr(line,1,60) "/" } # Kill comments # This does not distinguish between # appearing in a string # or regular expression, but for our purposes we don't care. { sub(/#.*/,"") } # A function definition begins in column one and is followed # by a parenthesized list not followed by a semi-colon. # If you use a different convention for function definitions # you will have to change this. /function[ \t]+[A-Za-z0-9_]+\(/ { decl = substr($0,1,index($0,"(") - 1) if (decl ~ /\"/) next # Skip strings n = split(decl,va,"[ \t*]+") rtnname = va[n] if (rtnname == "if") next if (rtnname == "do") next if (rtnname == "while") next if (rtnname == "switch") next addtag(rtnname,$0) next # Now change all occurences of "/" to "\/" and "\" to "\\" # n = split($0,va,"\\") # line = va[1] # for (i = 2; i <= n; i++) { line = line "\\\\" va[i] } # n = split(line,va,"/") # line = va[1] # for (i = 2; i <= n; i++) { line = line "\\/" va[i] } # print ( rtnname "\t" FILENAME "\t/^" line "/" ) # next } # Ignore all other lines { next } END { # Print out the tags in alphabetic order. for (i in tag) { print i "\t" tag[i] | "sort" } }