# TAWK program to convert text files to postscript, with pagination. # This version can save the pages and print them out in reverse order # so they come out in the right order on the laserwriter local page[] local pages[] local pageno = 1 local lineno = 1 local rflag = 0 # If TRUE, save pages and print pages in reverse order. local hflag=1 # If TRUE print a header. local mflag=0 # If TRUE force manual feed. local nflag=0 # If TRUE number the lines. # Initial values of font and font pointsize. # Can be over-ridden on awk command line like so: # awk -vfont=Helvetica -vps=12 # We arrange things so there are 66 lines per page. # Page length = 11 inches x 72 points = 792 points # 66 x 10 = 660 points per page to make 66 lines. # So top and bottom margin are (792 - 66) / 2 = 66 pts global font="Courier" # Default font global ps= 9 # Default point size global vs= 10 # Default vertical spacing local left=72 # 1 inch (72 point) left margin global tab= 4 # size of tab local top=66 # top margin in points. local bot=66 # bottom margin in points. BEGIN { today = ctime() # This tells what version of Postscript we assume; its optional. print "%!PSAdobe-1.0" # This is postscript a comment. print "%%Creator: TAWK program by Thompson Automation, Inc" print "%%CreationDate:",ctime() for (i = 1; i < ARGC; i++) { if (ARGV[i] ~ /^-t/) { tab = substr(ARGV[i],3)+0; ARGV[i] = ""; continue; } if (ARGV[i] == "-n") { nflag = 1; ARGV[i] = ""; continue; } if (ARGV[i] == "-h") { hflag = 0; ARGV[i] = ""; continue; } if (ARGV[i] == "-m") { mflag = 1; ARGV[i] = ""; continue; } if (ARGV[i] == "-r") { rflag = ! rflag; ARGV[i] = ""; continue; } if (substr(ARGV[i],1,1) == "-") { print ARGV[0] ": Unrecognized flag:",ARGV[i] > stderr exit(2) } break; } } function startpage() { # save/restore command pairs mark the boundaries of the scope # for function definitions. page[lineno++] = "save" # This defines a function to print using font and size. page[lineno++] = "/font1 /" font " findfont " ps " scalefont def" page[lineno++] = "/shf1 {font1 setfont show} def" # Initialize x and y coordinates on page. # Character coordinate is at lower left of character, excluding # any extension below the line x = left y = 11 * 72 - top - ps inpage = 1 # Print the header if (hflag) { outline("==== File " FILENAME " Page " ++pagenumber " " today " ====",0) outline("",0) } } function endpage() { page[lineno++] = "restore" # End of scope for function definitions. if (mflag) { # Request manual feed page[lineno++] = "statusdict begin" page[lineno++] = "/manualfeed true def" page[lineno++] = "end" } page[lineno++] = "showpage" # Print the page. inpage = 0 lineno = 1 if (rflag) { # Save all pages until end pages[pageno++] = page page = 0 } else { for (i in page) print page[i] delete page printf("\4") # Control-D is the end of page mark for the Laserwriter. } } function outline(line,donumber) # line is the line to output # donumber is a flag that is TRUE to number the output line { page[lineno++] = x " " y " moveto" # Fix up the tabs. while ((ind = index(line,"\t"))) { # nspaces = 1 + tab - (ind % tab) nspaces = tab - ((ind-1) % tab) line = substr(line,1,ind-1) substr(" ",1,nspaces) substr(line,ind+1) } gsub(/\t/," ",line) # Change tab to spaces. gsub(/\\/,"\\\\",line) # Insert a backslash before a backslash gsub(/\)/,"\\)",line) # Insert a backslash before right paren gsub(/\(/,"\\(",line) # Insert a backslash before left paren if (donumber) { page[lineno++] = "(" sprintf("%4d: ",linenumber) line ") shf1" } else { page[lineno++] = "(" line ") shf1" } y = y - vs if (y < bot) { endpage() } } FNR == 1 && NR != 1 { if (inpage) { endpage() } pagenumber = 0 } inpage == 0 { startpage() } { linenumber++ outline($0,nflag) } END { local i, j if (inpage) { endpage() } if (rflag) { # Print saved pages in reversed order. for (i = pageno-1; i >= 1; i--) { for (j in pages[i]) { print pages[i][j] } printf("\04") } } }