#! /usr/bin/gawk -f # Last edited on 2012-02-13 05:02:28 by stolfilocal BEGIN { abort = -1; usage = ( ARGV[0] " \\\n" \ " < main.src > main.wds" \ ); # TO DO: !!! Output locator lines to print errors with correct filename and line split("", filestack); split("", filecount); top = -1; if (ARGC <= 1) { insert_file("-"); } else { for (i = ARGC; i > 0; i--) { if (ARGV[i] != "") { insert_file(ARGV[i]); } } } while (get_next_line()) { if (match(lin, /^[@]include[ ]*[{][^ {}]+[}][ ]*$/)) { # Output "@include" as comment entry: printf "# %s\n", lin; # Extract the file name: fname = lin; gsub(/^[ ]*@include[ ]*[{]/, "", fname); gsub(/[}][ ]*$/, "", fname); if (fname == "") { data_error(("empty file name")); } # Start reading from that file: insert_file(fname); } else { # output line normally: print lin; } } exit 0; } function get_next_line( res,fname) { while (top >= 0) { fname = filestack[top]; if (filecount[top] == 0) { printf "reading %s...\n", fname > "/dev/stderr"; } res = (getline lin < fname); if (res > 0) { filecount[top]++; return 1; } else { close(fname); printf "read %7d lines from %s\n", \ filecount[top], fname > "/dev/stderr"; top--; } } return 0; } function insert_file(fname) { top++; filestack[top] = fname; filecount[top] = 0; } function arg_error(msg) { printf "%s\n", msg > "/dev/stderr"; printf "usage: %s\n", usage > "/dev/stderr"; abort = 1; exit 1; } function data_error(msg) { printf "%s:%d: %s\n", filestack[top], filecount[top], msg > "/dev/stderr"; abort = 1; exit 1; }