%%s 7/7/304 %%d D 1.2 15-Jun-83 11:43:03 sventek 2 1 %%c Removed trailing periods from quoted strings, as they are no longer needed. %%s 0/0/0 %%d D 1.1 27-Nov-82 14:54:56 sventek 1 0 %%c This is the initial source and documentation for the printf tool. %%T %%I 1 %%D 2 #-h- printf.r 5518 asc 27-nov-82 14:54:06 sventek (joseph sventek) %%E 2 %%I 2 #-h- printf.r 5514 asc 15-jun-83 11:42:41 sventek (joseph sventek) %%E 2 #-h- defns 71 asc 24-nov-82 22:10:52 sventek (joseph sventek) define(MAXFIELDS,25) define(ARGFLAG,-1) # or define(ARGFLAG,255) #-h- main 1996 asc 24-nov-82 22:33:35 sventek (joseph sventek) # field - rearrange fields in a file DRIVER(field) character buf(MAXLINE), fmt(MAXLINE), out(MAXLINE), tabc integer from(MAXFIELDS), to(MAXFIELDS), width(MAXFIELDS), start(MAXFIELDS) integer i, j, k, n, w, files, tflag integer getarg, getlin filedes fd filedes open string usestr "usage: printf [-t[c] | fieldlist] outputformat [file] ..." call query(usestr) tabc = '@t' tflag = YES if (getarg(1, buf, MAXLINE) == EOF) call error(usestr) if (buf(1) == '-' & (buf(2) == 't' | buf(2) == 'T')) { # tab fields are specified if (buf(3) != EOS) tabc = buf(3) i = 2 } else if (IS_DIGIT(buf(1))) { # fields are specified call getfld(buf, from, to) tflag = NO i = 2 } else i = 1 if (getarg(i, buf, MAXLINE) == EOF) call error(usestr) #error, no output format specified call getfmt(buf, fmt, width) files = NO for (i=i+1; ; i=i+1) { if (getarg(i, buf, MAXLINE) == EOF) #done? { if (files == YES) #yes, done break fd = STDIN #not done, read from STDIN } else if (buf(1) == '-' & buf(2) == EOS) fd = STDIN else fd = open(buf, READ) files = YES if (fd == ERR) call cant(buf) while (getlin(buf, fd) != EOF) { if (tflag == YES) call dotabs(buf, tabc, out, start) else call doflds(buf, from, to, out, start) k = 1 for (j = 1; fmt(j) != EOS; j = j + 1) if (fmt(j) != ARGFLAG) call putch(fmt(j), STDOUT) else { j = j + 1 w = fmt(j) n = start(k) k = k + 1 call putstr(out(n), width(w), STDOUT) } call putch('@n', STDOUT) } if (fd != STDIN) call close(fd) } DRETURN end %%D 2 #-h- getfld 921 asc 24-nov-82 22:47:10 sventek (joseph sventek) %%E 2 %%I 2 #-h- getfld 919 asc 15-jun-83 11:42:18 sventek (joseph sventek) %%E 2 subroutine getfld(buf, from, to) character buf(MAXLINE) integer from(MAXFIELDS), to(MAXFIELDS) integer i, n integer ctoi n = 0 for (i = 1; buf(i) != EOS; ) { n = n + 1 if (n > MAXFIELDS) %%D 2 call error("Too many fields for internal storage.") %%E 2 %%I 2 call error("Too many fields for internal storage") %%E 2 from(n) = ctoi(buf, i) to(n) = from(n) call skipbl(buf, i) if (buf(i) == '-') # n-m { i = i + 1 to(n) = ctoi(buf, i) } else if (buf(i) == '+') #n+m { i = i + 1 to(n) = from(n) + ctoi(buf, i) - 1 } call skipbl(buf, i) if (from(n) < 1 | from(n) > to(n) | (buf(i) != ',' & buf(i) != EOS)) { call putlnl(buf, ERROUT) for ( ; i > 1; i = i - 1) call putch(' ', ERROUT) call putch('^', ERROUT) call putch('@n', ERROUT) %%D 2 call error("Field specification error.") %%E 2 %%I 2 call error("Field specification error") %%E 2 } if (buf(i) == ',') i = i + 1 } for (i = n + 1; i <= MAXFIELDS; i = i + 1) { from(i) = HUGE to(i) = HUGE } return end %%D 2 #-h- getfmt 1087 asc 24-nov-82 22:29:49 sventek (joseph sventek) %%E 2 %%I 2 #-h- getfmt 1085 asc 15-jun-83 11:42:19 sventek (joseph sventek) %%E 2 # getfmt - convert output format in buf to internal form in fmt and width subroutine getfmt(buf, fmt, width) character buf(MAXLINE), fmt(MAXLINE) integer width(MAXFIELDS) character esc integer i, j, k, junk integer addset, ctoi j = 1 k = 0 for (i = 1; buf(i) != EOS; i = i + 1) if (buf(i) == '%') if (buf(i+1) == '%') # %% --> % { i = i + 1 junk = addset('%', fmt, j, MAXLINE) } else { k = k + 1 if (k > MAXFIELDS) %%D 2 call error("Too many fields for internal storage.") %%E 2 %%I 2 call error("Too many fields for internal storage") %%E 2 junk = addset(ARGFLAG, fmt, j, MAXLINE) junk = addset(k, fmt, j, MAXLINE) i = i + 1 width(k) = ctoi(buf, i) if (buf(i) != 's' & buf(i) != 'S') { call putlnl(buf, ERROUT) for ( ; i > 1; i = i - 1) call putch(' ', ERROUT) call putch('^', ERROUT) call putch('@n', ERROUT) %%D 2 call error("Incorrectly formatted string.") %%E 2 %%I 2 call error("Incorrectly formatted string") %%E 2 } } else junk = addset(esc(buf, i), fmt, j, MAXLINE) fmt(j) = EOS for (k = k + 1; k <= MAXFIELDS; k = k + 1) width(k) = 0 return end #-h- doflds 432 asc 24-nov-82 22:43:01 sventek (joseph sventek) subroutine doflds(buf, from, to, out, start) character buf(MAXLINE), out(MAXLINE) integer from(MAXFIELDS), to(MAXFIELDS), start(MAXFIELDS) integer i, j, k, n integer length j = 1 n = length(buf) for (i = 1; i <= MAXFIELDS; i = i + 1) { start(i) = j for (k = from(i); k <= to(i); k = k + 1) if (k >= n) break else { out(j) = buf(k) j = j + 1 } out(j) = EOS j = j + 1 } return end #-h- dotabs 549 asc 27-nov-82 14:50:11 sventek (joseph sventek) # dotabs - break buf into fields defined by tab character c subroutine dotabs(buf, c, out, start) character buf(MAXLINE), out(MAXLINE), c integer start(MAXFIELDS) integer i, j j = 1 start(1) = 1 for (i = 1; buf(i) != '@n'; i = i + 1) if (buf(i) != c) out(i) = buf(i) else { out(i) = EOS j = j + 1 if (j > MAXFIELDS) break start(j) = i + 1 } out(i) = EOS for (j = j + 1; j <= MAXFIELDS; j = j + 1) start(j) = i return end #-h- printf.fmt 2776 asc 27-nov-82 14:54:08 sventek (joseph sventek) .so ~bin/manhdr .hd Printf (1) 27-Nov-82 justify fields of data in fixed-width fields .sy printf [-t[c] | fieldlist] outputformat [file] ... .ds printf is used to manipulate data kept in formatted fields. It selects data from certain fields of the input files and copies it into fixed-width fields, with justification, in the standard output. The 'fieldlist' parameter is used to describe the interesting columns on the input file. Fields are specified by naming the columns in which they occur (e.g. 5-10) or the columns in which they start and an indication of their length (e.g. 3+2, meaning a field which starts in column 3 and spans 2 columns). When specifying more than one field, separate the specs with commas (e.g. 5-10,16,72+8) Fields may overlap, and need not be in ascending numerical order (e.g. 1-25,10,3 is OK). If input fields do not fall in certain columns, but rather are separated by some character (such as a blank or a comma), describe the fields by using the '-tc' flag, replacing 'c' with the appropriate separator (a tab character is the default). Once fields have been described with either the '-tc' flag or a fieldlist, they can be arranged on output by the 'outputformat' argument. This argument is actually a picture of what the output line should look like. Fields from the input are referred to as "%[-][n]s", with the following meanings for the optional characters: .in +3 .ti -2 n The next input field is to be output in a field `n' characters wide, right justified in the field. .ti -2 - The input field is to be left justified in the specified field. .in -3 If a percent character is to be output, it can be specified either as %% or as @%. A percent character followed by anything other than % or [-][n]s is a syntax error in the outputformat argument. For example, an outputformat of: .ce "%-10s is equivalent to %s" would produce an output line such as: .ce field1 is equivalent to field2 If no input files are specified, or if the filename '-' is found, field will read from the standard input. If re-ordering of a set of fields before output is necessary, the `field' tool can be used prior to `printf': field "$3@t$2@t$1" | printf "%-15s | %-15s | %s" >outfile .di Field specification error. .in +5 The fieldlist specification was in error, probably because it contained letters or some other illegal characters .sp .ti -5 Incorrectly formatted string. .br The outputformat specification contains an illegal % contruct. .sp .ti -5 Too many fields for internal storage. .br The fieldlist specification or the outputformat specification provides for more fields than internal storage can handle. The program can be recompiled with a larger value for the symbol MAXFIELDS. .in -5 .sa sedit(1), field(1) .au Joe Sventek %%E 1