21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

254 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>The stack is initialized with ARGV[1], which will be ‘/dev/stdin’. The main loop comesnext. Input lines are read in succession. Lines that do not start with ‘@include’ are printedverbatim. If the line does start with ‘@include’, the file name is in $2. pathto is called togenerate the full path. If it cannot, then we print an error message and continue.The next thing to check is if the file is included already. The processed array is indexedby the full file name of each included file and it tracks this information for us. If the file isseen again, a warning message is printed. Otherwise, the new file name is pushed onto thestack and processing continues.Finally, when getline encounters the end of the input file, the file is closed and thestack is popped. When stackptr is less than zero, the program is done:stackptr = 0input[stackptr] = ARGV[1] # ARGV[1] is first filefor (; stackptr >= 0; stackptr--) {while ((getline < input[stackptr]) > 0) {if (tolower($1) != "@include") {printcontinue}fpath = pathto($2)if (fpath == "") {printf("igawk:%s:%d: cannot find %s\n",input[stackptr], FNR, $2) > "/dev/stderr"continue}if (! (fpath in processed)) {processed[fpath] = input[stackptr]input[++stackptr] = fpath # push onto stack} elseprint $2, "included in", input[stackptr],"already included in",processed[fpath] > "/dev/stderr"}close(input[stackptr])}}’ # close quote ends ‘expand_prog’ variableprocessed_program=‘gawk -- "$expand_prog" /dev/stdin

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!