#! /usr/bin/gawk -f # Last edited on 2000-09-03 16:32:48 by stolfi BEGIN { abort = -1; usage = ( ARGV[0] " FILENAME.. > OUTFILE" ); # Reads the named files, processing (possibli nested) "include" statements # of the form "@ filename". Inserts "#@ filename linenumber" directives # so that downstream programs can report errors meaningfully. split("", fileName); split("", nRead); maxLevels = 5; if (ARGC-1 <= 0) { fileName[1] = "/dev/stdin"; nRead[1] = 0; topFile = 1; } else { for(i=1; i 0) { while (get_next_line() > 0) { nRead[topFile]++; lastFile = topFile; if (match($0, /^[ ]*[@][ ]/)) { if (NF != 2) { data_error("bad \"@\" directive"); } fname = $2; if(topFile >= maxLevels) { data_error("too many nested includes"); } topFile++; fileName[topFile] = fname; } else { print; } } lastFile = topFile; close(fileName[topFile]); topFile--; } } function get_next_line( ) { if (lastFile != topFile) { printf "#@ %s %d\n", fileName[topFile], nRead[topFile]; lastFile = topFile; } return (getline < fileName[topFile]); } function data_error(msg) { printf "file %s, line %s: %s\n", fileName[topFile], nRead[topFile], msg \ > "/dev/stderr"; abort = 1; exit abort; }