#! /usr/bin/gawk -f # Last edited on 1998-10-10 02:37:25 by stolfi BEGIN { abort = -1; # Check arguments: if (table == "") { error("must define \"table\""); } if (key == "") { error("must define \"key\""); } psec = "?"; pqui = "?"; ppag = "?"; plan = "?"; phnd = "?"; # Read table "dic" mapping "fnum" to attribute: ndic = 0; while((getline lin < table) > 0) { ndic++; nf = split(lin, fld); if (nf != 2) { error(("bad table entry = \"" lin "\"")); } if (fld[1] in dic) { error(("repeated key = \"" lin "\"")); } dic[fld[1]] = fld[2]; } close (table); printf "read %d pairs\n", ndic >> "/dev/stderr"; # "Attributes" section indicator (-1=before first, 1=inside, 0=between) fnum = "???"; inAtt = 0; didPut = 1; } /^[#][#]/{ # Start of new page if (abort >= 0) { exit abort; } # Check for page without "Attributes" section: if (didPut == 0) { warning("`Attributes:' not found"); putatt(key, fnum, dic, source); didPut = 1; } # Reset the attribute section indicator: inAtt = -1; didPut = 0; # Extract the page f-number: if (match($0, /[<]f[0-9rv]*[>]/)) { fnum = substr($0, RSTART+1, RLENGTH-2); if (fnum in dic) { pnum = dic[fnum]; } else { pnum = "???"; } } else { warning("missing in ##-line"); } # Extract the VTX attributes: if (match($0, /[{][$].*[}]/)) { attrs = substr($0, RSTART+1, RLENGTH-2); gsub(/[=]/, " ", attrs); n = split(attrs, at); for (i=1;i<=n;i+=2) { vnam = at[i]; val = at[i+1]; if (vnam=="$I") { psec = val; } else if (vnam=="$Q") { pqui = val; } else if (vnam=="$P") { ppag = val; } else if (vnam=="$L") { plan = val; } else if (vnam=="$H") { phnd = val; } else { warning(("unknown $-variable \"" vnam "=" val "\"")); } } printf "begin page %s\n", fnum >> "/dev/stderr"; } else { warning("missing {$..} in ##-line"); } print; next; } /^[#][ ]Attributes: *$/ { # Start of attributes section if (abort >= 0) { exit abort; } inAtt=1; blankCt = 0; print; next; } /^[#][ ][A-Z]/ { # Start of some other section if (abort >= 0) { exit abort; } if ((inAtt == 1) && (didPut == 0)) { putatt(key, fnum, dic, source); didPut = 1; } inAtt=0; print; next; } /^[#][ ]*$/ { # Blank line if (abort >= 0) { exit abort; } if (inAtt == 1) { blankCt++; if ((blankCt == 2) && (didPut == 0)) { putatt(key, fnum, dic, source); didPut = 1; } } print; next; } /^[#]/ { if (abort >= 0) { exit abort; } print; next; } /^ *$/{ next; } // { error("missing #"); } END { if (abort >= 0) { exit abort; } if (didPut == 0) { warning("`Attributes:' not found"); putatt(key, fnum, dic, source); } } function putatt(key, fnum, dic, source) { if (fnum in dic) { printf "# %s: %s", key, dic[fnum]; if (source != "") { printf " (%s)", source; } printf "\n"; } else { warning(("fnum = \"" fnum "\" not in table")); } } function warning(msg) { printf "line %d: (warning) %s\n", NR, msg >> "/dev/stderr"; } function error(msg) { printf "line %d: %s\n", NR, msg >> "/dev/stderr"; abort = 1; exit abort; }