Remaining files for awk 2.0 that were left out of the previous checkin.

Maybe someday I'll become adept at using cvs...
This commit is contained in:
tribby 1998-04-07 17:06:53 +00:00
parent 0b6d503134
commit ddb82cb2e0
108 changed files with 3085 additions and 0 deletions

207
usr.bin/awk/proctab.c Normal file
View File

@ -0,0 +1,207 @@
/* $Id: proctab.c,v 1.1 1998/04/07 17:03:52 tribby Exp $ */
#include <stdio.h>
#include "awk.h"
#include "ytab.h"
static char *printname[92] = {
(char *) "FIRSTTOKEN", /* 257 */
(char *) "PROGRAM", /* 258 */
(char *) "PASTAT", /* 259 */
(char *) "PASTAT2", /* 260 */
(char *) "XBEGIN", /* 261 */
(char *) "XEND", /* 262 */
(char *) "NL", /* 263 */
(char *) "ARRAY", /* 264 */
(char *) "MATCH", /* 265 */
(char *) "NOTMATCH", /* 266 */
(char *) "MATCHOP", /* 267 */
(char *) "FINAL", /* 268 */
(char *) "DOT", /* 269 */
(char *) "ALL", /* 270 */
(char *) "CCL", /* 271 */
(char *) "NCCL", /* 272 */
(char *) "CHAR", /* 273 */
(char *) "OR", /* 274 */
(char *) "STAR", /* 275 */
(char *) "QUEST", /* 276 */
(char *) "PLUS", /* 277 */
(char *) "AND", /* 278 */
(char *) "BOR", /* 279 */
(char *) "APPEND", /* 280 */
(char *) "EQ", /* 281 */
(char *) "GE", /* 282 */
(char *) "GT", /* 283 */
(char *) "LE", /* 284 */
(char *) "LT", /* 285 */
(char *) "NE", /* 286 */
(char *) "IN", /* 287 */
(char *) "ARG", /* 288 */
(char *) "BLTIN", /* 289 */
(char *) "BREAK", /* 290 */
(char *) "CLOSE", /* 291 */
(char *) "CONTINUE", /* 292 */
(char *) "DELETE", /* 293 */
(char *) "DO", /* 294 */
(char *) "EXIT", /* 295 */
(char *) "FOR", /* 296 */
(char *) "FUNC", /* 297 */
(char *) "SUB", /* 298 */
(char *) "GSUB", /* 299 */
(char *) "IF", /* 300 */
(char *) "INDEX", /* 301 */
(char *) "LSUBSTR", /* 302 */
(char *) "MATCHFCN", /* 303 */
(char *) "NEXT", /* 304 */
(char *) "NEXTFILE", /* 305 */
(char *) "ADD", /* 306 */
(char *) "MINUS", /* 307 */
(char *) "MULT", /* 308 */
(char *) "DIVIDE", /* 309 */
(char *) "MOD", /* 310 */
(char *) "ASSIGN", /* 311 */
(char *) "ASGNOP", /* 312 */
(char *) "ADDEQ", /* 313 */
(char *) "SUBEQ", /* 314 */
(char *) "MULTEQ", /* 315 */
(char *) "DIVEQ", /* 316 */
(char *) "MODEQ", /* 317 */
(char *) "POWEQ", /* 318 */
(char *) "PRINT", /* 319 */
(char *) "PRINTF", /* 320 */
(char *) "SPRINTF", /* 321 */
(char *) "ELSE", /* 322 */
(char *) "INTEST", /* 323 */
(char *) "CONDEXPR", /* 324 */
(char *) "POSTINCR", /* 325 */
(char *) "PREINCR", /* 326 */
(char *) "POSTDECR", /* 327 */
(char *) "PREDECR", /* 328 */
(char *) "VAR", /* 329 */
(char *) "IVAR", /* 330 */
(char *) "VARNF", /* 331 */
(char *) "CALL", /* 332 */
(char *) "NUMBER", /* 333 */
(char *) "STRING", /* 334 */
(char *) "REGEXPR", /* 335 */
(char *) "GETLINE", /* 336 */
(char *) "RETURN", /* 337 */
(char *) "SPLIT", /* 338 */
(char *) "SUBSTR", /* 339 */
(char *) "WHILE", /* 340 */
(char *) "CAT", /* 341 */
(char *) "NOT", /* 342 */
(char *) "UMINUS", /* 343 */
(char *) "POWER", /* 344 */
(char *) "DECR", /* 345 */
(char *) "INCR", /* 346 */
(char *) "INDIRECT", /* 347 */
(char *) "LASTTOKEN", /* 348 */
};
Cell *(*proctab[92])(Node **, int) = {
nullproc, /* FIRSTTOKEN */
program, /* PROGRAM */
pastat, /* PASTAT */
dopa2, /* PASTAT2 */
nullproc, /* XBEGIN */
nullproc, /* XEND */
nullproc, /* NL */
array, /* ARRAY */
matchop, /* MATCH */
matchop, /* NOTMATCH */
nullproc, /* MATCHOP */
nullproc, /* FINAL */
nullproc, /* DOT */
nullproc, /* ALL */
nullproc, /* CCL */
nullproc, /* NCCL */
nullproc, /* CHAR */
nullproc, /* OR */
nullproc, /* STAR */
nullproc, /* QUEST */
nullproc, /* PLUS */
boolop, /* AND */
boolop, /* BOR */
nullproc, /* APPEND */
relop, /* EQ */
relop, /* GE */
relop, /* GT */
relop, /* LE */
relop, /* LT */
relop, /* NE */
instat, /* IN */
arg, /* ARG */
bltin, /* BLTIN */
jump, /* BREAK */
closefile, /* CLOSE */
jump, /* CONTINUE */
awkdelete, /* DELETE */
dostat, /* DO */
jump, /* EXIT */
forstat, /* FOR */
nullproc, /* FUNC */
sub, /* SUB */
gsub, /* GSUB */
ifstat, /* IF */
sindex, /* INDEX */
nullproc, /* LSUBSTR */
matchop, /* MATCHFCN */
jump, /* NEXT */
jump, /* NEXTFILE */
arith, /* ADD */
arith, /* MINUS */
arith, /* MULT */
arith, /* DIVIDE */
arith, /* MOD */
assign, /* ASSIGN */
nullproc, /* ASGNOP */
assign, /* ADDEQ */
assign, /* SUBEQ */
assign, /* MULTEQ */
assign, /* DIVEQ */
assign, /* MODEQ */
assign, /* POWEQ */
printstat, /* PRINT */
awkprintf, /* PRINTF */
awksprintf, /* SPRINTF */
nullproc, /* ELSE */
intest, /* INTEST */
condexpr, /* CONDEXPR */
incrdecr, /* POSTINCR */
incrdecr, /* PREINCR */
incrdecr, /* POSTDECR */
incrdecr, /* PREDECR */
nullproc, /* VAR */
nullproc, /* IVAR */
getnf, /* VARNF */
call, /* CALL */
nullproc, /* NUMBER */
nullproc, /* STRING */
nullproc, /* REGEXPR */
getline, /* GETLINE */
jump, /* RETURN */
split, /* SPLIT */
substr, /* SUBSTR */
whilestat, /* WHILE */
cat, /* CAT */
boolop, /* NOT */
arith, /* UMINUS */
arith, /* POWER */
nullproc, /* DECR */
nullproc, /* INCR */
indirect, /* INDIRECT */
nullproc, /* LASTTOKEN */
};
char *tokname(int n)
{
static char buf[100];
if (n < FIRSTTOKEN || n > LASTTOKEN) {
sprintf(buf, "token %d", n);
return buf;
}
return printname[n-FIRSTTOKEN];
}

16
usr.bin/awk/tests/addr.1 Normal file
View File

@ -0,0 +1,16 @@
Adam Smith
1234 Wall St., Apt. 5C
New York, NY 10021
212 555-4321
David W. Copperfield
221 Dickens Lane
Monterey, CA 93940
408 555-0041
work phone 408 555-6532
Mary, birthday January 30
Canadian Consulate
555 Fifth Ave
New York, NY
212 586-2400

View File

@ -0,0 +1,19 @@
# Read values into an array
$1 == "REMOVE" { delete value[$2]; next }
$1 == "RESET" {delete value; next}
$1 == "SKIP" {nextfile}
$1 == "PRINT" {printarray(value); next}
{ value[$1] = $2 }
END { printarray(value) }
function printarray(A, key) {
print "Values in array";
for (key in A) print " ",key,A[key];
print " ";
}

View File

@ -0,0 +1,21 @@
one 1
two 2
three 3
four 4
five 5
six 6
seven 7
eight 8
nine 9
PRINT
REMOVE nine
REMOVE bad
PRINT
RESET
one I
two II
three III
four IIII
SKIP
badentry BAD
PRINT

View File

@ -0,0 +1,3 @@
four IV
five V
six VI

48
usr.bin/awk/tests/asm Normal file
View File

@ -0,0 +1,48 @@
# asm - assembler and interpreter for simple computer
# usage: awk -f asm program-file data-files...
BEGIN {
srcfile = ARGV[1]
ARGV[1] = "" # remaining files are data
tempfile = "/tmp/asm.temp"
n = split("const get put ld st add sub jpos jz j halt", x)
for (i = 1; i <= n; i++) # create table of op codes
op[x[i]] = i-1
# ASSEMBLER PASS 1
FS = "[ \t]+"
while (getline <srcfile > 0) {
sub(/#.*/, "") # strip comments
symtab[$1] = nextmem # remember label location
if ($2 != "") { # save op, addr if present
print $2 "\t" $3 >tempfile
nextmem++
}
}
close(tempfile)
# ASSEMBLER PASS 2
nextmem = 0
while (getline <tempfile > 0) {
if ($2 !~ /^[0-9]*$/) # if symbolic addr,
$2 = symtab[$2] # replace by numeric value
mem[nextmem++] = 1000 * op[$1] + $2 # pack into word
}
# INTERPRETER
for (pc = 0; pc >= 0; ) {
addr = mem[pc] % 1000
code = int(mem[pc++] / 1000)
if (code == op["get"]) { getline acc }
else if (code == op["put"]) { print acc }
else if (code == op["st"]) { mem[addr] = acc }
else if (code == op["ld"]) { acc = mem[addr] }
else if (code == op["add"]) { acc += mem[addr] }
else if (code == op["sub"]) { acc -= mem[addr] }
else if (code == op["jpos"]) { if (acc > 0) pc = addr }
else if (code == op["jz"]) { if (acc == 0) pc = addr }
else if (code == op["j"]) { pc = addr }
else if (code == op["halt"]) { pc = -1 }
else { pc = -1 }
}
}

View File

@ -0,0 +1,37 @@
# asm - assembler and interpreter for simple computer
# usage: awk -f asm program-file data-files...
# this is a special version to produce a listing
BEGIN {
srcfile = ARGV[1]
ARGV[1] = "" # remaining files are data
tempfile = "/tmp/asm.temp"
n = split("const get put ld st add sub jpos jz j halt", x)
for (i = 1; i <= n; i++) # create table of op codes
op[x[i]] = i-1
# ASSEMBLER PASS 1
nextmem = 0 # new
FS = "[ \t]+"
while (getline <srcfile > 0) {
input[nextmem] = $0 # new: remember source line
sub(/#.*/, "") # strip comments
symtab[$1] = nextmem # remember label location
if ($2 != "") { # save op, addr if present
print $2 "\t" $3 >tempfile
nextmem++
}
}
close(tempfile)
# ASSEMBLER PASS 2
nextmem = 0
while (getline <tempfile > 0) {
if ($2 !~ /^[0-9]*$/) # if symbolic addr,
$2 = symtab[$2] # replace by numeric value
mem[nextmem++] = 1000 * op[$1] + $2 # pack into word
}
for (i = 0; i < nextmem; i++) # new: print memory
printf("%3d: %05d %s\n", i, mem[i], input[i]) # new
}

View File

@ -0,0 +1,171 @@
# awk.parser - recursive-descent translator for part of awk
# input: awk program (very restricted subset)
# output: C code to implement the awk program
BEGIN { program() }
function advance() { # lexical analyzer; returns next token
if (tok == "(eof)") return "(eof)"
while (length(line) == 0)
if (getline line == 0)
return tok = "(eof)"
sub(/^[ \t]+/, "", line) # remove white space
if (match(line, /^[A-Za-z_][A-Za-z_0-9]*/) || # identifier
match(line, /^-?([0-9]+\.?[0-9]*|\.[0-9]+)/) || # number
match(line, /^(<|<=|==|!=|>=|>)/) || # relational
match(line, /^./)) { # everything else
tok = substr(line, 1, RLENGTH)
line = substr(line, RLENGTH+1)
return tok
}
error("line " NR " incomprehensible at " line)
}
function gen(s) { # print s with nt leading tabs
printf("%s%s\n", substr("\t\t\t\t\t\t\t\t\t", 1, nt), s)
}
function eat(s) { # read next token if s == tok
if (tok != s) error("line " NF ": saw " tok ", expected " s)
advance()
}
function nl() { # absorb newlines and semicolons
while (tok == "\n" || tok == ";")
advance()
}
function error(s) { print "Error: " s | "cat 1>&2"; exit 1 }
function program() {
advance()
if (tok == "BEGIN") { eat("BEGIN"); statlist() }
pastats()
if (tok == "END") { eat("END"); statlist() }
if (tok != "(eof)") error("program continues after END")
}
function pastats() {
gen("while (getrec()) {"); nt++
while (tok != "END" && tok != "(eof)") pastat()
nt--; gen("}")
}
function pastat() { # pattern-action statement
if (tok == "{") # action only
statlist()
else { # pattern-action
gen("if (" pattern() ") {"); nt++
if (tok == "{") statlist()
else # default action is print $0
gen("print(field(0));")
nt--; gen("}")
}
}
function pattern() { return expr() }
function statlist() {
eat("{"); nl(); while (tok != "}") stat(); eat("}"); nl()
}
function stat() {
if (tok == "print") { eat("print"); gen("print(" exprlist() ");") }
else if (tok == "if") ifstat()
else if (tok == "while") whilestat()
else if (tok == "{") statlist()
else gen(simplestat() ";")
nl()
}
function ifstat() {
eat("if"); eat("("); gen("if (" expr() ") {"); eat(")"); nl(); nt++
stat()
if (tok == "else") { # optional else
eat("else")
nl(); nt--; gen("} else {"); nt++
stat()
}
nt--; gen("}")
}
function whilestat() {
eat("while"); eat("("); gen("while (" expr() ") {"); eat(")"); nl()
nt++; stat(); nt--; gen("}")
}
function simplestat( lhs) { # ident = expr | name(exprlist)
lhs = ident()
if (tok == "=") {
eat("=")
return "assign(" lhs ", " expr() ")"
} else return lhs
}
function exprlist( n, e) { # expr , expr , ...
e = expr() # has to be at least one
for (n = 1; tok == ","; n++) {
advance()
e = e ", " expr()
}
return e
}
function expr(e) { # rel | rel relop rel
e = rel()
while (tok ~ /<|<=|==|!=|>=|>/) {
op = tok
advance()
e = sprintf("eval(\"%s\", %s, %s)", op, e, rel())
}
return e
}
function rel(op, e) { # term | term [+-] term
e = term()
while (tok == "+" || tok == "-") {
op = tok
advance()
e = sprintf("eval(\"%s\", %s, %s)", op, e, term())
}
return e
}
function term(op, e) { # fact | fact [*/%] fact
e = fact()
while (tok == "*" || tok == "/" || tok == "%") {
op = tok
advance()
e = sprintf("eval(\"%s\", %s, %s)", op, e, fact())
}
return e
}
function fact( e) { # (expr) | $fact | ident | number
if (tok == "(") {
eat("("); e = expr(); eat(")")
return "(" e ")"
} else if (tok == "$") {
eat("$")
return "field(" fact() ")"
} else if (tok ~ /^[A-Za-z][A-Za-z0-9]*/) {
return ident()
} else if (tok ~ /^-?([0-9]+\.?[0-9]*|\.[0-9]+)/) {
e = tok
advance()
return "num((float)" e ")"
} else
error("unexpected " tok " at line " NR)
}
function ident( id, e) { # name | name[expr] | name(exprlist)
if (!match(tok, /^[A-Za-z_][A-Za-z_0-9]*/))
error("unexpected " tok " at line " NR)
id = tok
advance()
if (tok == "[") { # array
eat("["); e = expr(); eat("]")
return "array(" id ", " e ")"
} else if (tok == "(") { # function call
eat("(")
if (tok != ")") {
e = exprlist()
eat(")")
} else eat(")")
return id "(" e ")" # calls are statements
} else
return id # variable
}

6
usr.bin/awk/tests/bigpop Normal file
View File

@ -0,0 +1,6 @@
USSR 275
China 1032
USA 237
Brazil 134
India 746
Japan 120

View File

@ -0,0 +1,58 @@
# bridge - generate random bridge hands
BEGIN { split(permute(52,52), deck) # generate a random deck
sort(1,13); sort(14,26); sort(27,39); sort(40,52) # sort hands
prhands() # format and print the four hands
}
function permute(k, n, i, p, r) { # generate a random permutation
srand(1); # leave out "1" for more randomness
p = " " # of k integers between 1 and n
for (i = n-k+1; i <= n; i++)
if (p ~ " " (r = int(i*rand())+1) " " )
sub(" " r " ", " " r " " i " ", p) # put i after r in p
else p = " " r p # put r at beginning of p
return p
}
function sort(left,right, i,j,t) { # sort hand in deck[left..right]
for (i = left+1; i <= right; i++)
for (j = i; j > left && deck[j-1] < deck[j]; j--) {
t = deck[j-1]; deck[j-1] = deck[j]; deck[j] = t
}
}
function prhands() { # print the four hands
b = sprintf("%20s", " "); b40 = sprintf("%40s", " ")
card = 1 # global index into deck
suits(13); print b " NORTH"
print b spds; print b hrts; print b dnds; print b clbs
suits(26) # create the west hand from deck[14..26]
ws = spds substr(b40, 1, 40 - length(spds))
wh = hrts substr(b40, 1, 40 - length(hrts))
wd = dnds substr(b40, 1, 40 - length(dnds))
wc = clbs substr(b40, 1, 40 - length(clbs))
suits(39); print " WEST" sprintf("%36s", " ") "EAST"
print ws spds; print wh hrts; print wd dnds; print wc clbs
suits(52); print b " SOUTH"
print b spds; print b hrts; print b dnds; print b clbs
}
function suits(j) { # collect suits of hand in deck[j-12..j]
for (spds = "S:"; deck[card] > 39 && card <= j; card++)
spds = spds " " fvcard(deck[card])
for (hrts = "H:"; deck[card] > 26 && card <= j; card++)
hrts = hrts " " fvcard(deck[card])
for (dnds = "D:"; deck[card] > 13 && card <= j; card++)
dnds = dnds " " fvcard(deck[card])
for (clbs = "C:"; card <= j; card++)
clbs = clbs " " fvcard(deck[card])
}
function fvcard(i) { # compute face value of card i
if (i % 13 == 0) return "A"
else if (i % 13 == 12) return "K"
else if (i % 13 == 11) return "Q"
else if (i % 13 == 10) return "J"
else return (i % 13) + 1
}

3
usr.bin/awk/tests/bundle Normal file
View File

@ -0,0 +1,3 @@
# bundle - combine multiple files into one
{ print FILENAME, $0 }

View File

@ -0,0 +1,11 @@
USSR Moscow
Canada Ottawa
China Beijing
USA Washington
Brazil Brasilia
India New Delhi
Mexico Mexico City
France Paris
Japan Tokyo
Germany Bonn
England London

View File

@ -0,0 +1,11 @@
Brazil Brasilia
Canada Ottawa
China Beijing
England London
France Paris
Germany Bonn
India New Delhi
Japan Tokyo
Mexico Mexico City
USA Washington
USSR Moscow

View File

@ -0,0 +1 @@
{ printf("%-8s $%6.2f\n", $1, $2 * $3) }

View File

@ -0,0 +1,2 @@
{ product = $2 * $3;
print $1, $2, $3, product }

View File

@ -0,0 +1 @@
$2 >= 5

View File

@ -0,0 +1 @@
$2 * $3 > 50 { printf("$%.2f for %s\n", $2 * $3, $1) }

View File

@ -0,0 +1 @@
!($2 < 4 && $3 < 20)

View File

@ -0,0 +1 @@
{ print NF, $1, $NF }

View File

@ -0,0 +1,2 @@
BEGIN { print "NAME RATE HOURS"; print "" }
{ print }

View File

@ -0,0 +1,2 @@
$3 > 15 { emp = emp + 1 }
END { print emp, "employees worked more than 15 hours" }

View File

@ -0,0 +1,5 @@
{ pay = pay + $2 * $3 }
END { print NR, "employees"
print "total pay is", pay
print "average pay is", pay/NR
}

View File

@ -0,0 +1,2 @@
$2 > maxrate { maxrate = $2; maxemp = $1 }
END { print "highest hourly rate:", maxrate, "for", maxemp }

View File

@ -0,0 +1,2 @@
{ last = $0 }
END { print last }

View File

@ -0,0 +1,2 @@
{ names = names $1 " " }
END { print names }

View File

@ -0,0 +1,4 @@
{ nc = nc + length($0) + 1
nw = nw + NF
}
END { print NR, "lines,", nw, "words,", nc, "characters" }

View File

@ -0,0 +1,7 @@
$2 > 6 { n = n + 1; pay = pay + $2 * $3 }
END { if (n > 0)
print n, "employees, total pay is", pay,
"average pay is", pay/n
else
print "no employees are paid more than $6/hour"
}

View File

@ -0,0 +1,7 @@
# reverse - print input in reverse order by line
{ line[NR] = $0 } # remember each input line
END { for (i = NR; i > 0; i = i - 1)
print line[i]
}

View File

@ -0,0 +1,2 @@
{ field = $NF}
END { print field }

View File

@ -0,0 +1,2 @@
{ nf = nf + NF }
END { print nf }

View File

@ -0,0 +1,2 @@
$1 > max { max = $1; maxline = $0 }
END { print max, maxline }

View File

@ -0,0 +1,4 @@
{ sum = 0
for (i = 1; i <= NF; i = i + 1) sum = sum + $i
print sum
}

View File

@ -0,0 +1,2 @@
{ for (i = 1; i <= NF; i = i + 1) sum = sum + $i }
END { print sum }

View File

@ -0,0 +1,11 @@
# print countries with column headers and totals
BEGIN { FS = "\t" # make tab the field separator
printf("%10s %6s %5s %s\n\n",
"COUNTRY", "AREA", "POP", "CONTINENT")
}
{ printf("%10s %6d %5d %s\n", $1, $2, $3, $4)
area = area + $2
pop = pop + $3
}
END { printf("\n%10s %6d %5d\n", "TOTAL", area, pop) }

View File

@ -0,0 +1 @@
$4 ~ /^(Asia|Europe)$/

View File

@ -0,0 +1 @@
/Asia/ || /Europe/

View File

@ -0,0 +1 @@
/Asia|Europe/

View File

@ -0,0 +1 @@
/Europe/, /Africa/

View File

@ -0,0 +1 @@
FNR == 1, FNR == 5 { print FILENAME ": " $0 }

View File

@ -0,0 +1,4 @@
$4 == "Asia" { pop = pop + $3; n = n + 1 }
END { print "Total population of the", n,
"Asian countries is", pop, "million."
}

View File

@ -0,0 +1,4 @@
$3 > maxpop { maxpop = $3; country = $1 }
END { print "country with largest population:",
country, maxpop
}

View File

@ -0,0 +1 @@
{ print NR ":" $0 }

View File

@ -0,0 +1 @@
{ gsub(/USA/, "United States"); print }

View File

@ -0,0 +1 @@
{ $1 = substr($1, 1, 3); print $0 }

View File

@ -0,0 +1,2 @@
{ s = s substr($1, 1, 3) " " }
END { print s }

View File

@ -0,0 +1,4 @@
BEGIN { FS = OFS = "\t" }
$4 == "North America" { $4 = "NA" }
$4 == "South America" { $4 = "SA" }
{ print }

View File

@ -0,0 +1,15 @@
# seq - print sequences of integers
# input: arguments q, p q, or p q r; q >= p; r > 0
# output: integers 1 to q, p to q, or p to q in steps of r
BEGIN {
if (ARGC == 2)
for (i = 1; i <= ARGV[1]; i++)
print i
else if (ARGC == 3)
for (i = ARGV[1]; i <= ARGV[2]; i++)
print i
else if (ARGC == 4)
for (i = ARGV[1]; i <= ARGV[2]; i += ARGV[3])
print i
}

View File

@ -0,0 +1,6 @@
{ i = 1
while (i <= NF) {
print $i
i++
}
}

View File

@ -0,0 +1,3 @@
{ for (i = 1; i <= NF; i++)
print $i
}

View File

@ -0,0 +1,7 @@
/Asia/ { pop["Asia"] += $3 }
/Europe/ { pop["Europe"] += $3 }
END { print "Asian population is",
pop["Asia"], "million."
print "European population is",
pop["Europe"], "million."
}

View File

@ -0,0 +1,5 @@
BEGIN { FS = "\t" }
{ pop[$4] += $3 }
END { for (name in pop)
print name, pop[name]
}

View File

@ -0,0 +1,2 @@
BEGIN { OFS = ":"; ORS = "\n\n" }
{ print $1, $2 }

View File

@ -0,0 +1 @@
{ print($1, $3) > ($3 > 100 ? "/tmp/out2/bigpop" : "/tmp/out2/smallpop") }

View File

@ -0,0 +1,7 @@
# print continents and populations, sorted by population
BEGIN { FS = "\t" }
{ pop[$4] += $3 }
END { for (c in pop)
printf("%15s\t%6d\n", c, pop[c]) | "sort -t'\t' +1rn"
}

View File

@ -0,0 +1,9 @@
# include - replace #include "f" by contents of file f
/^#include/ {
gsub(/"/, "", $2)
while (getline x <$2 > 0)
print x
next
}
{ print }

View File

@ -0,0 +1,7 @@
# echo - print command-line arguments
BEGIN {
for (i = 1; i < ARGC; i++)
printf "%s ", ARGV[i]
printf "\n"
}

View File

@ -0,0 +1,2 @@
$1 == "#include" { gsub(/"/, "", $2); system("cat " $2); next }
{ print }

19
usr.bin/awk/tests/check1 Normal file
View File

@ -0,0 +1,19 @@
# check1 - print total deposits and checks
/^check/ { ck = 1; next }
/^deposit/ { dep = 1; next }
/^amount/ { amt = $2; next }
/^$/ { addup() }
END { addup()
printf("deposits $%.2f, checks $%.2f\n",
deposits, checks)
}
function addup() {
if (ck)
checks += amt
else if (dep)
deposits += amt
ck = dep = amt = 0
}

16
usr.bin/awk/tests/check3a Normal file
View File

@ -0,0 +1,16 @@
# check3 - print check information
# check3a modified to print in format that prchecks can use
BEGIN { RS = ""; FS = "\n" }
/(^|\n)check/ {
for (i = 1; i <= NF; i++) {
split($i, f, "\t")
val[f[1]] = f[2]
}
printf("%5d\t%s\t%s\n",
val["check"],
sprintf("%.2f", val["amount"]),
val["to"])
for (i in val)
delete val[i]
}

View File

@ -0,0 +1,58 @@
# prchecks - print formatted checks
# input: number \t amount \t payee
# output: eight lines of text for preprinted check forms
BEGIN {
FS = "\t"
dashes = sp45 = sprintf("%45s", " ")
gsub(/ /, "-", dashes) # to protect the payee
### Just use a fixed date: to avoid pipe and to ensure consistency
## "date" | getline date # get today's date
## split(date, d, " ")
## date = d[2] ". " d[3] ", " d[6]
date = "Mar. 7, 1998"
initnum() # set up tables for number conversion
}
NF != 3 {
printf("\nrec %d has %d fields:\n|%s|\n\nVOID\nVOID\n\n\n",
NR, NF, $0)
next
}
{ printf("\n") # nothing on line 1
printf("%s%s\n", sp45, $1) # number, indented 45 spaces
printf("%s%s\n", sp45, date) # date, indented 45 spaces
amt = sprintf("%.2f", $2) # formatted amount
printf("Pay to %45.45s $%s\n", $3 dashes, amt) # line 4
printf("the sum of %s\n", numtowords(amt)) # line 5
printf("\n\n\n") # lines 6, 7 and 8
}
function numtowords(n, cents, dols, s) { # n has 2 decimal places
cents = substr(n, length(n)-1, 2)
dols = substr(n, 1, length(n)-3)
if (dols == 0)
s = "zero dollars and " cents " cents exactly"
else
s = intowords(dols) " dollars and " cents " cents exactly"
sub(/^one dollars/, "one dollar", s)
gsub(/ +/, " ", s)
return s
}
function intowords(n) {
n = int(n)
if (n >= 1000000)
return("VOID")
if (n >= 1000)
return intowords(n/1000) " thousand " intowords(n%1000)
if (n >= 100)
return intowords(n/100) " hundred " intowords(n%100)
if (n >= 20)
return tens[int(n/10)] " " intowords(n%10)
return nums[n]
}
function initnum() {
split("one two three four five six seven eight nine " \
"ten eleven twelve thirteen fourteen fifteen " \
"sixteen seventeen eighteen nineteen", nums, " ")
split("ten twenty thirty forty fifty sixty " \
"seventy eighty ninety", tens, " ")
}

View File

@ -0,0 +1,30 @@
check 1021
to Champagne Unlimited
amount 123.10
date 1/1/87
deposit
amount 500.00
date 1/1/87
check 1022
date 1/2/87
amount 45.10
to Getwell Drug Store
tax medical
check 1023
amount 125.00
to International Travel
date 1/3/87
amount 50.00
to Carnegie Hall
date 1/3/87
check 1024
tax charitable contribution
to American Express
check 1025
amount 75.75
date 1/5/87

View File

@ -0,0 +1,33 @@
# awk program to parse the output of "lseg -d <files>" and calculate
# 1. The total size of code in each object file
# 2. The name of any procedure that uses over "maxseg" bytes (default=100)
BEGIN {
# If maxseg was not set on command line, use default
if (maxseg <= 0) maxseg = 100;
}
/ Code / {
# Is this the beginning of a new file's statistics?
if ($1 != filename) {
if (codesize > 0) {
sizes[filename] = codesize;
}
filename = $1;
codesize = 0;
}
codesize += $3;
# If there are > 4 fields, fourth one is the stack size
if ($4 > maxseg) print $5," in file ", $1," uses ",$4," stack bytes"
}
END {
# Print the report
print "Code file Code bytes"
for (fname in sizes) {
printf "%s", fname;
i = length(fname);
while (i++ < 20) printf " ";
printf " %6d\n", sizes[fname];
}
}

View File

@ -0,0 +1,177 @@
File Type Size Stack Name
-------------------- ------------------ -------- ------ ----------------
../main.o Code 3467 22 "main"
../main.o Code 669 14 "pgetc"
../main.o Code 38 "~GLOBALS"
../main.o Code 80 "~ARRAYS"
../b.o Code 1151 20 "makedfa"
../b.o Code 841 22 "mkdfa"
../b.o Code 1657 12 "makeinit"
../b.o Code 759 8 "penter"
../b.o Code 586 8 "freetr"
../b.o Code 377 16 "hexstr"
../b.o Code 506 16 "quoted"
../b.o Code 1151 24 "cclenter"
../b.o Code 89 2 "overflo"
../b.o Code 1400 14 "cfoll"
../b.o Code 1277 14 "first"
../b.o Code 333 12 "follow"
../b.o Code 87 8 "member"
../b.o Code 354 16 "match"
../b.o Code 1552 24 "pmatch"
../b.o Code 1525 24 "nematch"
../b.o Code 281 10 "reparse"
../b.o Code 43 6 "regexp"
../b.o Code 815 12 "primary"
../b.o Code 128 8 "concat"
../b.o Code 96 6 "alt"
../b.o Code 199 8 "unary"
../b.o Code 1270 20 "relex"
../b.o Code 3513 22 "cgoto"
../b.o Code 1123 8 "freefa"
../b.o Code 51 "~GLOBALS"
../b.o Code 80 "~ARRAYS"
../lib.o Code 537 6 "recinit"
../lib.o Code 392 58 "makefields"
../lib.o Code 182 12 "initgetrec"
../lib.o Code 1879 20 "getrec"
../lib.o Code 88 "nextfile"
../lib.o Code 1801 26 "readrec"
../lib.o Code 188 64 "getargv"
../lib.o Code 318 18 "setclvar"
../lib.o Code 3069 31 "fldbld"
../lib.o Code 240 12 "cleanfld"
../lib.o Code 97 6 "newfld"
../lib.o Code 180 10 "fieldadr"
../lib.o Code 214 8 "growfldtab"
../lib.o Code 1208 22 "refldbld"
../lib.o Code 1581 22 "recbld"
../lib.o Code 343 6 "yyerror"
../lib.o Code 73 2 "fpecatch"
../lib.o Code 112 4 "bracecheck"
../lib.o Code 363 6 "bcheck2"
../lib.o Code 838 6 "error"
../lib.o Code 3866 20 "eprint"
../lib.o Code 102 4 "bclass"
../lib.o Code 268 10 "errcheck"
../lib.o Code 303 12 "isclvar"
../lib.o Code 301 20 "isnumber"
../lib.o Code 46 "~GLOBALS"
../lib.o Code 450 "~ARRAYS"
../proctab.o Code 188 10 "tokname"
../proctab.o Code 1 "~GLOBALS"
../proctab.o Code 1380 "~ARRAYS"
../tran.o Code 1112 6 "syminit"
../tran.o Code 358 62 "arginit"
../tran.o Code 406 14 "envinit"
../tran.o Code 250 18 "makesymtab"
../tran.o Code 560 20 "freesymtab"
../tran.o Code 503 20 "freeelem"
../tran.o Code 830 16 "setsymtab"
../tran.o Code 144 12 "hash"
../tran.o Code 408 24 "rehash"
../tran.o Code 243 20 "lookup"
../tran.o Code 378 20 "setfval"
../tran.o Code 319 6 "funnyvar"
../tran.o Code 585 20 "setsval"
../tran.o Code 410 18 "getfval"
../tran.o Code 618 122 "getsval"
../tran.o Code 190 10 "tostring"
../tran.o Code 1031 24 "qstring"
../tran.o Code 105 "~GLOBALS"
../lex.o Code 37 10 "peek"
../lex.o Code 1379 24 "gettok"
../lex.o Code 3792 14 "yylex"
../lex.o Code 1919 128 "string"
../lex.o Code 203 16 "binsearch"
../lex.o Code 885 18 "word"
../lex.o Code 7 "startreg"
../lex.o Code 702 14 "regexpr"
../lex.o Code 303 10 "input"
../lex.o Code 320 6 "unput"
../lex.o Code 123 8 "unputstr"
../lex.o Code 43 "~GLOBALS"
../lex.o Code 991 "~ARRAYS"
../parse.o Code 209 14 "nodealloc"
../parse.o Code 44 10 "exptostat"
../parse.o Code 133 14 "node1"
../parse.o Code 200 14 "node2"
../parse.o Code 265 14 "node3"
../parse.o Code 332 14 "node4"
../parse.o Code 66 14 "stat1"
../parse.o Code 70 14 "stat2"
../parse.o Code 74 14 "stat3"
../parse.o Code 78 14 "stat4"
../parse.o Code 66 14 "op1"
../parse.o Code 70 14 "op2"
../parse.o Code 74 14 "op3"
../parse.o Code 78 14 "op4"
../parse.o Code 101 14 "celltonode"
../parse.o Code 51 6 "rectonode"
../parse.o Code 344 14 "makearr"
../parse.o Code 172 14 "pa2stat"
../parse.o Code 231 14 "linkum"
../parse.o Code 283 12 "defn"
../parse.o Code 209 14 "isarg"
../parse.o Code 3 "~GLOBALS"
../parse.o Code 100 "~ARRAYS"
../run.o Code 353 16 "adjbuf"
../run.o Code 31 2 "run"
../run.o Code 854 22 "execute"
../run.o Code 940 14 "program"
../run.o Code 2949 54 "call"
../run.o Code 238 14 "copycell"
../run.o Code 351 10 "arg"
../run.o Code 881 20 "jump"
../run.o Code 1323 32 "getline"
../run.o Code 91 10 "getnf"
../run.o Code 1085 42 "array"
../run.o Code 944 38 "adelete"
../run.o Code 1073 42 "intest"
../run.o Code 821 40 "matchop"
../run.o Code 587 22 "boolop"
../run.o Code 805 30 "relop"
../run.o Code 306 6 "tfree"
../run.o Code 387 16 "gettemp"
../run.o Code 327 20 "indirect"
../run.o Code 1171 34 "substr"
../run.o Code 550 50 "sindex"
../run.o Code 3191 44 "format"
../run.o Code 482 24 "awksprintf"
../run.o Code 911 30 "awkprintf"
../run.o Code 1335 48 "arith"
../run.o Code 285 22 "ipow"
../run.o Code 425 28 "incrdecr"
../run.o Code 1466 48 "assign"
../run.o Code 530 30 "cat"
../run.o Code 321 14 "pastat"
../run.o Code 558 16 "dopa2"
../run.o Code 3051 101 "split"
../run.o Code 283 14 "condexpr"
../run.o Code 354 14 "ifstat"
../run.o Code 392 14 "whilestat"
../run.o Code 412 14 "dostat"
../run.o Code 616 14 "forstat"
../run.o Code 704 36 "instat"
../run.o Code 1848 46 "bltin"
../run.o Code 670 22 "printstat"
../run.o Code 65 6 "nullproc"
../run.o Code 209 22 "redirect"
../run.o Code 1458 22 "openfile"
../run.o Code 206 12 "filename"
../run.o Code 1262 18 "closefile"
../run.o Code 686 10 "closeall"
../run.o Code 1890 48 "sub"
../run.o Code 2844 54 "gsub"
../run.o Code 706 14 "backsub"
../run.o Code 55 "~GLOBALS"
../run.o Code 689 "~ARRAYS"
../ytab.o Code 16 4 "yywrap"
../ytab.o Code 214 6 "setfname"
../ytab.o Code 134 8 "constnode"
../ytab.o Code 108 10 "strnode"
../ytab.o Code 140 12 "notnull"
../ytab.o Code 222 10 "checkdup"
../ytab.o Code 17185 32 "yyparse"
../ytab.o Code 45 "~GLOBALS"
../ytab.o Code 13274 "~ARRAYS"

View File

@ -0,0 +1,23 @@
605 506 156 3292 133 1054 608 6605 Comment
502 31296 1926 672 933 2167 1706 168
1868 186 737 197 1308 856 4637 71392
456 1038 6380 1089 1800 588 2206 232
1279 3008 678 163 1517 450 207 432
1265 4853 3288 2335 847 3817 224 4204
1541 6431 2635 1904 1295 4830 1658 2615
4593 1933 545 2269 4247 8784 1354 2341 Nice!
1429 12451 711 661 6757 9831 702 15964
4284 10646 2082 2254 2066 2386 216 2148
14795 264 3200 476 6383 791 739 929
145 1165 1985 385 336 3263 3900 268
14954 268 131 2863 784 20472 579 2340
3371 1092 163 882 6248 3124 14806 3695
1334 557 3984 8215 12075 6579 2648 167
15805 4533 33468 16518 9353 141 2936 3459
157 1715 2155 1508 8735 149 167 1343
1925 1807 65901 16015 36833 20737 35547 2166
225 574 14016 11902 181 181 4761 1619
181 186 186 196 272 261 1129 3164
178 3740 368 3335 1564 1506 78880 142
169 176 6619 176 181 408 9510 207
790 441 251 167 1274 3794 3708 4132

View File

@ -0,0 +1,12 @@
# print k distinct random integers between 1 and n
{ random($1, $2) }
function random(k, n, A, i, r) {
for (i = n-k+1; i <= n; i++)
((r = randint(i)) in A) ? A[i] : A[r]
for (i in A)
print i
}
function randint(n) { return int(n*rand())+1 }

View File

@ -0,0 +1,11 @@
USSR 8649 275 Asia
Canada 3852 25 North America
China 3705 1032 Asia
USA 3615 237 North America
Brazil 3286 134 South America
India 1267 746 Asia
Mexico 762 78 North America
France 211 55 Europe
Japan 144 120 Asia
Germany 96 61 Europe
England 94 56 Europe

View File

@ -0,0 +1,11 @@
Brazil 3286 134 South America
Canada 3852 25 North America
China 3705 1032 Asia
England 94 56 Europe
France 211 55 Europe
Germany 96 61 Europe
India 1267 746 Asia
Japan 144 120 Asia
Mexico 762 78 North America
USA 3615 237 North America
USSR 8649 275 Asia

View File

@ -0,0 +1,5 @@
013042 mary's birthday
032772 mark's birthday
052470 anniversary
061209 mother's birthday
110175 elizabeth's birthday

View File

@ -0,0 +1,3 @@
# date convert - convert mmddyy into yymmdd in $1
{ $1 = substr($1,5,2) substr($1,1,2) substr($1,3,2); print }

1206
usr.bin/awk/tests/dotests Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,6 @@
Beth 4.00 0
Dan 3.75 0
Kathy 4.00 10
Mark 5.00 20
Mary 5.50 22
Susie 4.25 18

View File

@ -0,0 +1,22 @@
# Calculate factorial of command-line argument
BEGIN {
# Get the command-line argument
if (ARGC < 2) {
print "ERROR: please provide number as command-line parameter"
exit 1
}
n = ARGV[1]; ARGV[1] = "";
# Print heading
print "Calculating",n,"factorial"
# Start the recursion
nf = factorial(n);
# All done; print the result
print "Result:",nf;
}
# Recursive function
function factorial(n) {
if (n <= 1) return 1;
else return n*factorial(n-1);
}

View File

@ -0,0 +1,19 @@
# CREATE THE TEST ENVIRONMENT FOR TR
# COMPILE AND LOAD FLAGS PASSED TO OCC
# -V: VERBOSE
# -I: ADD DIRECTORY TO HEADER PATH
CFLAGS += -V -I /USR/INCLUDE
LDFLAGS += -V
# DEFAULT TARGET, "ALL," GENERATES THE FILES
ALL: FILE2.BIN1 GENBIN
# FILES DEPEND UPON PROGRAM
FILE2.BIN1: GENBIN
./GENBIN
# REMOVE INTERMEDIATE FILES
CLEAN:
$(RM) -F *.O
$(RM) -F *.ROOT

View File

@ -0,0 +1,19 @@
# Create the test environment for tr
# Compile and load flags passed to occ
# -v: verbose
# -I: add directory to header path
CFLAGS += -v -I /usr/include
LDFLAGS += -v
# Default target, "all," generates the files
all: file2.bin1 genbin
# Files depend upon program
file2.bin1: genbin
./genbin
# Remove intermediate files
clean:
$(RM) -f *.o
$(RM) -f *.root

View File

@ -0,0 +1,19 @@
# C
# C
# -:
# -I:
CFLAGS += - -I //
LDFLAGS += -
# D , ","
: 2.1
# F
2.1:
./
# R
:
$(RM) - *.
$(RM) - *.

View File

@ -0,0 +1,19 @@
# Crt th tst nvrnmnt fr tr
# Cmpl nd ld flgs pssd t cc
# -v: vrbs
# -I: dd drctr t hdr pth
CFLAGS += -v -I /sr/ncld
LDFLAGS += -v
# Dflt trgt, "ll," gnrts th fls
ll: fl2.bn1 gnbn
# Fls dpnd pn prgrm
fl2.bn1: gnbn
./gnbn
# Rmv ntrmdt fls
cln:
$(RM) -f *.
$(RM) -f *.rt

View File

@ -0,0 +1,22 @@
# form.gen - generate form letters
# input: prototype file letter.text; data lines
# output: one form letter per data line
BEGIN {
oldfs = FS;
FS = ":"
while (getline <"letter.text" > 0) # read form letter
form[++n] = $0
FS = oldfs;
# get country name from command-line argument
pat = ARGV[1]; ARGV[1] = "-"
}
$1 ~ pat { for (i = 1; i <= n; i++) { # read data line for country
temp = form[i]
for (j = 1; j <= 3; j++)
gsub("#" j, $j, temp)
gsub("#4", $2/$3, temp)
print temp
}
}

10
usr.bin/awk/tests/form1 Normal file
View File

@ -0,0 +1,10 @@
# form1 - format countries data by continent, pop. den.
BEGIN { FS = ":"
printf("%-15s %-10s %10s %7s %12s\n",
"CONTINENT", "COUNTRY", "POPULATION",
"AREA", "POP. DEN.")
}
{ printf("%-15s %-10s %7d %10d %10.1f\n",
$1, $2, $3, $4, $5)
}

15
usr.bin/awk/tests/form2 Normal file
View File

@ -0,0 +1,15 @@
# form2 - format countries by continent, pop. den.
BEGIN { FS = ":"
printf("%-15s %-10s %10s %7s %12s\n",
"CONTINENT", "COUNTRY", "POPULATION",
"AREA", "POP. DEN.")
}
{ if ($1 != prev) {
print ""
prev = $1
} else
$1 = ""
printf("%-15s %-10s %7d %10d %10.1f\n",
$1, $2, $3, $4, $5)
}

View File

@ -0,0 +1,4 @@
# generate random integers
BEGIN { for (i = 1; i <= 200; i++)
print int(101*rand())
}

10
usr.bin/awk/tests/grammar Normal file
View File

@ -0,0 +1,10 @@
Sentence -> Nounphrase Verbphrase
Nounphrase -> the boy
Nounphrase -> the girl
Verbphrase -> Verb Modlist Adverb
Verb -> runs
Verb -> walks
Modlist ->
Modlist -> very Modlist
Adverb -> quickly
Adverb -> slowly

View File

@ -0,0 +1,19 @@
{ x[int($1/10)]++ }
END { max = MAXSTARS = 25
for (i = 0; i <= 10; i++)
if (x[i] > max)
max = x[i]
for (i = 0; i <= 10; i++)
y[i] = x[i]/max * MAXSTARS
for (i = 0; i < 10; i++)
printf(" %2d - %2d: %3d %s\n",
10*i, 10*i+9, x[i], rep(y[i],"*"))
printf("100: %3d %s\n", x[10], rep(y[10],"*"))
}
function rep(n,s, t) { # return string of n s's
while (n-- > 0)
t = t s
return t
}

View File

@ -0,0 +1,17 @@
# histogram
# input: numbers between 0 and 100
# output: histogram of deciles
{ x[int($1/10)]++ }
END { for (i = 0; i < 10; i++)
printf(" %2d - %2d: %3d %s\n",
10*i, 10*i+9, x[i], rep(x[i],"*"))
printf("100: %3d %s\n", x[10], rep(x[10],"*"))
}
function rep(n,s, t) { # return string of n s's
while (n-- > 0)
t = t s
return t
}

View File

@ -0,0 +1,4 @@
This is a testfile for the awk test ch2p62.awk
#include bigpop
#include smallpop

View File

@ -0,0 +1,7 @@
BEGIN { FS = "\t"; pat = ARGV[1]; ARGV[1] = "-" }
$1 ~ pat {
printf("%s:\n", $1)
printf("\t%d million people\n", $3)
printf("\t%.3f million sq. mi.\n", $2/1000)
printf("\t%.1f people per sq. mi.\n", 1000*$3/$2)
}

View File

@ -0,0 +1,46 @@
# join - join file1 file2 on first field
# input: two sorted files, tab-separated fields
# output: natural join of lines with common first field
BEGIN {
OFS = sep = "\t"
file2 = ARGV[2]
ARGV[2] = "" # read file1 implicitly, file2 explicitly
eofstat = 1 # end of file status for file2
if ((ng = getgroup()) <= 0)
exit # file2 is empty
}
{ while (prefix($0) > prefix(gp[1]))
if ((ng = getgroup()) <= 0)
exit # file2 exhausted
if (prefix($0) == prefix(gp[1])) # 1st attributes in file1
for (i = 1; i <= ng; i++) # and file2 match
print $0, suffix(gp[i]) # print joined line
}
function getgroup() { # put equal prefix group into gp[1..ng]
if (getone(file2, gp, 1) <= 0) # end of file
return 0
for (ng = 2; getone(file2, gp, ng) > 0; ng++)
if (prefix(gp[ng]) != prefix(gp[1])) {
unget(gp[ng]) # went too far
return ng-1
}
return ng-1
}
function getone(f, gp, n) { # get next line in gp[n]
if (eofstat <= 0) # eof or error has occurred
return 0
if (ungot) { # return lookahead line if it exists
gp[n] = ungotline
ungot = 0
return 1
}
return eofstat = (getline gp[n] <f)
}
function unget(s) { ungotline = s; ungot = 1 }
function prefix(s) { return substr(s, 1, index(s, sep) - 1) }
function suffix(s) { return substr(s, index(s, sep) + 1) }

View File

@ -0,0 +1,8 @@
Subject: Demographic Information About #1
From: AWK Demographics, Inc.
In response to your request for information about #1,
our latest research has revealed that its population is #2
million people and its area is #3 million square miles.
This gives #1 a population density of #4 people per
square mile.

View File

@ -0,0 +1,8 @@
BEGIN { FS = "\t" }
FILENAME == "capitals" {
cap[$1] = $2
}
FILENAME == "countries" && $4 == "Asia" {
print $1, $3, cap[$1]
}

View File

@ -0,0 +1,96 @@
605 506
156 3292
133 1054
608 6605
502 31296
1926 672
933 2167
1706 168
1868 186
737 197
1308 856
4637 71392
456 1038
6380 1089
1800 588
2206 232
1279 3008
678 163
1517 450
207 432
1265 4853
3288 2335
847 3817
224 4204
1541 6431
2635 1904
1295 4830
1658 2615
4593 1933
545 2269
4247 8784
1354 2341
1429 12451
711 661
6757 9831
702 15964
4284 10646
2082 2254
2066 2386
216 2148
14795 264
3200 476
6383 791
739 929
145 1165
1985 385
336 3263
3900 268
14954 268
131 2863
784 20472
579 2340
3371 1092
163 882
6248 3124
14806 3695
1334 557
3984 8215
12075 6579
2648 167
15805 4533
33468 16518
9353 141
2936 3459
157 1715
2155 1508
8735 149
167 1343
1925 1807
65901 16015
36833 20737
35547 2166
225 574
14016 11902
181 181
4761 1619
181 186
186 196
272 261
1129 3164
178 3740
368 3335
1564 1506
78880 142
169 176
6619 176
181 408
9510 207
790 441
251 167
1274 3794
3708 4132
2376 4659
654 8094
3399 168
1907 914864

View File

@ -0,0 +1,2 @@
BEGIN { RS = ""; ORS = "\n\n" }
/New York/

View File

@ -0,0 +1,13 @@
BEGIN { x = 0; y = 1 }
$1 > x { if (x == y+1) {
x = 1
y = x * 2
} else
print x, z[x]
}
NR > 1 { print $1 }
END { print NR }

10
usr.bin/awk/tests/percent Normal file
View File

@ -0,0 +1,10 @@
# percent
# input: a column of nonnegative numbers
# output: each number and its percentage of the total
{ x[NR] = $1; sum += $1 }
END { if (sum != 0)
for (i = 1; i <= NR; i++)
printf("%10.2f %5.1f\n", x[i], 100*x[i]/sum)
}

6
usr.bin/awk/tests/prep1 Normal file
View File

@ -0,0 +1,6 @@
# prep1 - prepare countries by continent and pop. den.
BEGIN { FS = "\t" }
{ printf("%s:%s:%d:%d:%.1f\n",
$4, $1, $3, $2, 1000*$3/$2) | "sort -t: +0 -1 +4rn"
}

7
usr.bin/awk/tests/prep2 Normal file
View File

@ -0,0 +1,7 @@
# prep2 - prepare countries by continent, inverse pop. den.
BEGIN { FS = "\t"}
{ den = 1000*$3/$2
printf("%-15s:%12.8f:%s:%d:%d:%.1f\n",
$4, 1/den, $1, $3, $2, den) | "sort"
}

View File

@ -0,0 +1,23 @@
# Print environment variables' values; names passed on command line
BEGIN {
if (ARGC < 2) {
print "Error: provide environment variable name on command line"
exit 1;
}
i = 1;
do {
rawname = ARGV[i];
upname = toupper(rawname);
loname = tolower(rawname);
printf "%s = '%s'", rawname,ENVIRON[rawname];
if (rawname != upname)
printf "; %s = '%s'", upname,ENVIRON[upname];
if (rawname != loname)
printf "; %s = '%s'", loname,ENVIRON[loname];
printf "\n"
i++;
} while (i < ARGC)
}

30
usr.bin/awk/tests/sentgen Normal file
View File

@ -0,0 +1,30 @@
# sentgen - random sentence generator
# input: grammar file; sequence of nonterminals
# output: a random sentence for each nonterminal
BEGIN { # read rules from grammar file
while (getline < "grammar" > 0)
if ($2 == "->") {
i = ++lhs[$1] # count lhs
rhscnt[$1, i] = NF-2 # how many in rhs
for (j = 3; j <= NF; j++) # record them
rhslist[$1, i, j-2] = $j
} else
print "illegal production: " $0
}
{ if ($1 in lhs) { # nonterminal to expand
gen($1)
printf("\n")
} else
print "unknown nonterminal: " $0
}
function gen(sym, i, j) {
if (sym in lhs) { # a nonterminal
i = int(lhs[sym] * rand()) + 1 # random production
for (j = 1; j <= rhscnt[sym, i]; j++) # expand rhs's
gen(rhslist[sym, i, j])
} else
printf("%s ", sym)
}

View File

@ -0,0 +1,7 @@
Sentence
Sentence
Nounphrase
Verb
Adverb
BadElement
Sentence

View File

@ -0,0 +1,38 @@
# sentgen2 - random sentence generator (nonrecursive)
# input: grammar file; sequence of nonterminals
# output: random sentences generated by the grammar
BEGIN { # read rules from grammar file
while (getline < "grammar" > 0)
if ($2 == "->") {
i = ++lhs[$1] # count lhs
rhscnt[$1, i] = NF-2 # how many in rhs
for (j = 3; j <= NF; j++) # record them
rhslist[$1, i, j-2] = $j
} else
print "illegal production: " $0
}
{ if ($1 in lhs) { # nonterminal to expand
push($1)
gen()
printf("\n")
} else
print "unknown nonterminal: " $0
}
function gen( i, j) {
while (stp >= 1) {
sym = pop()
if (sym in lhs) { # a nonterminal
i = int(lhs[sym] * rand()) + 1 # random production
for (j = rhscnt[sym, i]; j >= 1; j--) # expand rhs's
push(rhslist[sym, i, j])
} else
printf("%s ", sym)
}
}
function push(s) { stack[++stp] = s }
function pop() { return stack[stp--] }

View File

@ -0,0 +1,5 @@
Canada 25
Mexico 78
France 55
Germany 61
England 56

58
usr.bin/awk/tests/sortgen Normal file
View File

@ -0,0 +1,58 @@
# sortgen - generate sort command
# input: sequence of lines describing sorting options
# output: Unix sort command with appropriate arguments
BEGIN { key = 0 }
/no |not |n't / { print "error: can't do negatives:", $0; ok = 1 }
# rules for global options
{ ok = 0 }
/uniq|discard.*(iden|dupl)/ { uniq = " -u"; ok = 1 }
/separ.*tab|tab.*sep/ { sep = "t'\t'"; ok = 1 }
/separ/ { for (i = 1; i <= NF; i++)
if (length($i) == 1)
sep = "t'" $i "'"
ok = 1
}
/key/ { key++; dokey(); ok = 1 } # new key; must come in order
# rules for each key
/dict/ { dict[key] = "d"; ok = 1 }
/ignore.*(space|blank)/ { blank[key] = "b"; ok = 1 }
/fold|case/ { fold[key] = "f"; ok = 1 }
/num/ { num[key] = "n"; ok = 1 }
/rev|descend|decreas|down|oppos/ { rev[key] = "r"; ok = 1 }
/forward|ascend|increas|up|alpha/ { next } # this is sort's default
!ok { print "error: can't understand:", $0 }
END { # print flags for each key
cmd = "sort" uniq
flag = dict[0] blank[0] fold[0] rev[0] num[0] sep
if (flag) cmd = cmd " -" flag
for (i = 1; i <= key; i++)
if (pos[i] != "") {
flag = pos[i] dict[i] blank[i] fold[i] rev[i] num[i]
if (flag) cmd = cmd " +" flag
if (pos2[i]) cmd = cmd " -" pos2[i]
}
print cmd
}
function dokey( i) { # determine position of key
for (i = 1; i <= NF; i++)
if ($i ~ /^[0-9]+$/) {
pos[key] = $i - 1 # sort uses 0-origin
break
}
for (i++; i <= NF; i++)
if ($i ~ /^[0-9]+$/) {
pos2[key] = $i
break
}
if (pos[key] == "")
printf("error: invalid key specification: %s\n", $0)
if (pos2[key] == "")
pos2[key] = pos[key] + 1
}

Some files were not shown because too many files have changed in this diff Show More