From bc70de7b3302d5a81515b901cae376b8b51d2004 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Fri, 16 Jul 2010 13:09:56 +0300 Subject: Move to gawk-3.1.0. --- awklib/eg/data/guide-mellow.po | 8 ++ awklib/eg/data/guide.po | 8 ++ awklib/eg/lib/assert.awk | 2 + awklib/eg/lib/bits2str.awk | 16 +++ awklib/eg/lib/cliff_rand.awk | 14 ++ awklib/eg/lib/ftrans.awk | 4 +- awklib/eg/lib/getopt.awk | 20 ++- awklib/eg/lib/gettime.awk | 15 ++- awklib/eg/lib/grcat.c | 6 +- awklib/eg/lib/groupawk.in | 9 +- awklib/eg/lib/join.awk | 1 + awklib/eg/lib/libintl.awk | 9 ++ awklib/eg/lib/mktime.awk | 105 --------------- awklib/eg/lib/nextfile.awk | 1 + awklib/eg/lib/noassign.awk | 17 +++ awklib/eg/lib/ord.awk | 18 +-- awklib/eg/lib/passwdawk.in | 9 +- awklib/eg/lib/pwcat.c | 7 +- awklib/eg/lib/readable.awk | 16 +++ awklib/eg/lib/rewind.awk | 20 +++ awklib/eg/lib/round.awk | 4 +- awklib/eg/misc/findpat.awk | 10 ++ awklib/eg/misc/findpat.sh | 10 -- awklib/eg/network/PostAgent.sh | 9 ++ awklib/eg/network/coreserv.awk | 113 ++++++++++++++++ awklib/eg/network/eliza.awk | 270 +++++++++++++++++++++++++++++++++++++ awklib/eg/network/fingerclient.awk | 7 + awklib/eg/network/geturl.awk | 24 ++++ awklib/eg/network/hello-serv.awk | 14 ++ awklib/eg/network/maze.awk | 73 ++++++++++ awklib/eg/network/mobag.awk | 72 ++++++++++ awklib/eg/network/panic.awk | 18 +++ awklib/eg/network/protbase.awk | 11 ++ awklib/eg/network/protbase.request | 7 + awklib/eg/network/protbase.result | 9 ++ awklib/eg/network/remconf.awk | 54 ++++++++ awklib/eg/network/statist.awk | 85 ++++++++++++ awklib/eg/network/stoxdata.txt | 4 + awklib/eg/network/stoxpred.awk | 116 ++++++++++++++++ awklib/eg/network/testserv.awk | 12 ++ awklib/eg/network/urlchk.awk | 28 ++++ awklib/eg/network/webgrab.awk | 6 + awklib/eg/prog/alarm.awk | 10 +- awklib/eg/prog/awksed.awk | 2 +- awklib/eg/prog/cut.awk | 13 +- awklib/eg/prog/dupword.awk | 9 +- awklib/eg/prog/egrep.awk | 26 ++-- awklib/eg/prog/extract.awk | 12 +- awklib/eg/prog/guide.awk | 7 + awklib/eg/prog/histsort.awk | 3 +- awklib/eg/prog/id.awk | 42 +++--- awklib/eg/prog/igawk.sh | 19 +-- awklib/eg/prog/labels.awk | 9 +- awklib/eg/prog/split.awk | 8 +- awklib/eg/prog/tee.awk | 1 + awklib/eg/prog/testbits.awk | 27 ++++ awklib/eg/prog/translate.awk | 5 +- awklib/eg/prog/uniq.awk | 5 +- awklib/eg/prog/wc.awk | 8 +- awklib/eg/prog/wordfreq.awk | 11 +- 60 files changed, 1247 insertions(+), 231 deletions(-) create mode 100644 awklib/eg/data/guide-mellow.po create mode 100644 awklib/eg/data/guide.po create mode 100644 awklib/eg/lib/bits2str.awk create mode 100644 awklib/eg/lib/cliff_rand.awk create mode 100644 awklib/eg/lib/libintl.awk delete mode 100644 awklib/eg/lib/mktime.awk create mode 100644 awklib/eg/lib/noassign.awk create mode 100644 awklib/eg/lib/readable.awk create mode 100644 awklib/eg/lib/rewind.awk create mode 100644 awklib/eg/misc/findpat.awk delete mode 100644 awklib/eg/misc/findpat.sh create mode 100644 awklib/eg/network/PostAgent.sh create mode 100644 awklib/eg/network/coreserv.awk create mode 100644 awklib/eg/network/eliza.awk create mode 100644 awklib/eg/network/fingerclient.awk create mode 100644 awklib/eg/network/geturl.awk create mode 100644 awklib/eg/network/hello-serv.awk create mode 100644 awklib/eg/network/maze.awk create mode 100644 awklib/eg/network/mobag.awk create mode 100644 awklib/eg/network/panic.awk create mode 100644 awklib/eg/network/protbase.awk create mode 100644 awklib/eg/network/protbase.request create mode 100644 awklib/eg/network/protbase.result create mode 100644 awklib/eg/network/remconf.awk create mode 100644 awklib/eg/network/statist.awk create mode 100644 awklib/eg/network/stoxdata.txt create mode 100644 awklib/eg/network/stoxpred.awk create mode 100644 awklib/eg/network/testserv.awk create mode 100644 awklib/eg/network/urlchk.awk create mode 100644 awklib/eg/network/webgrab.awk create mode 100644 awklib/eg/prog/guide.awk create mode 100644 awklib/eg/prog/testbits.awk (limited to 'awklib/eg') diff --git a/awklib/eg/data/guide-mellow.po b/awklib/eg/data/guide-mellow.po new file mode 100644 index 00000000..98c388d8 --- /dev/null +++ b/awklib/eg/data/guide-mellow.po @@ -0,0 +1,8 @@ +#: guide.awk:4 +msgid "Don't Panic" +msgstr "Hey man, relax!" + +#: guide.awk:5 +msgid "The Answer Is" +msgstr "Like, the scoop is" + diff --git a/awklib/eg/data/guide.po b/awklib/eg/data/guide.po new file mode 100644 index 00000000..de21218a --- /dev/null +++ b/awklib/eg/data/guide.po @@ -0,0 +1,8 @@ +#: guide.awk:4 +msgid "Don't Panic" +msgstr "" + +#: guide.awk:5 +msgid "The Answer Is" +msgstr "" + diff --git a/awklib/eg/lib/assert.awk b/awklib/eg/lib/assert.awk index 50f42e7d..bbfc0669 100644 --- a/awklib/eg/lib/assert.awk +++ b/awklib/eg/lib/assert.awk @@ -1,4 +1,6 @@ # assert --- assert that a condition is true. Otherwise exit. + +# # Arnold Robbins, arnold@gnu.org, Public Domain # May, 1993 diff --git a/awklib/eg/lib/bits2str.awk b/awklib/eg/lib/bits2str.awk new file mode 100644 index 00000000..9725ee8f --- /dev/null +++ b/awklib/eg/lib/bits2str.awk @@ -0,0 +1,16 @@ +# bits2str --- turn a byte into readable 1's and 0's + +function bits2str(bits, data, mask) +{ + if (bits == 0) + return "0" + + mask = 1 + for (; bits != 0; bits = rshift(bits, 1)) + data = (and(bits, mask) ? "1" : "0") data + + while ((length(data) % 8) != 0) + data = "0" data + + return data +} diff --git a/awklib/eg/lib/cliff_rand.awk b/awklib/eg/lib/cliff_rand.awk new file mode 100644 index 00000000..345447ec --- /dev/null +++ b/awklib/eg/lib/cliff_rand.awk @@ -0,0 +1,14 @@ +# cliff_rand.awk --- generate Cliff random numbers +# +# Arnold Robbins, arnold@gnu.org, Public Domain +# December 2000 + +BEGIN { _cliff_seed = 0.1 } + +function cliff_rand() +{ + _cliff_seed = (100 * log(_cliff_seed)) % 1 + if (_cliff_seed < 0) + _cliff_seed = - _cliff_seed + return _cliff_seed +} diff --git a/awklib/eg/lib/ftrans.awk b/awklib/eg/lib/ftrans.awk index cec615be..b0743e28 100644 --- a/awklib/eg/lib/ftrans.awk +++ b/awklib/eg/lib/ftrans.awk @@ -2,8 +2,8 @@ # # user supplies beginfile() and endfile() functions # -# Arnold Robbins, arnold@gnu.org, November 1992 -# Public Domain +# Arnold Robbins, arnold@gnu.org, Public Domain +# November 1992 FNR == 1 { if (_filename_ != "") diff --git a/awklib/eg/lib/getopt.awk b/awklib/eg/lib/getopt.awk index dae20e5f..93753052 100644 --- a/awklib/eg/lib/getopt.awk +++ b/awklib/eg/lib/getopt.awk @@ -1,28 +1,26 @@ -# getopt --- do C library getopt(3) function in awk +# getopt.awk --- do C library getopt(3) function in awk # -# arnold@gnu.org -# Public domain +# Arnold Robbins, arnold@gnu.org, Public Domain # # Initial version: March, 1991 # Revised: May, 1993 # External variables: -# Optind -- index of ARGV for first non-option argument +# Optind -- index in ARGV of first non-option argument # Optarg -- string value of argument to current option -# Opterr -- if non-zero, print our own diagnostic +# Opterr -- if nonzero, print our own diagnostic # Optopt -- current option letter -# Returns +# Returns: # -1 at end of options # ? for unrecognized option # a character representing the current option -# Private Data -# _opti index in multi-flag option, e.g., -abc -function getopt(argc, argv, options, optl, thisopt, i) +# Private Data: +# _opti -- index in multi-flag option, e.g., -abc +function getopt(argc, argv, options, thisopt, i) { - optl = length(options) - if (optl == 0) # no options given + if (length(options) == 0) # no options given return -1 if (argv[Optind] == "--") { # all done diff --git a/awklib/eg/lib/gettime.awk b/awklib/eg/lib/gettime.awk index 82f09d72..c425123c 100644 --- a/awklib/eg/lib/gettime.awk +++ b/awklib/eg/lib/gettime.awk @@ -1,6 +1,8 @@ -# gettimeofday --- get the time of day in a usable format +# gettimeofday.awk --- get the time of day in a usable format +# # Arnold Robbins, arnold@gnu.org, Public Domain, May 1993 # + # Returns a string in the format of output of date(1) # Populates the array argument time with individual values: # time["second"] -- seconds (0 - 59) @@ -11,17 +13,17 @@ # time["month"] -- month of year (1 - 12) # time["monthname"] -- name of the month # time["shortmonth"] -- short name of the month -# time["year"] -- year within century (0 - 99) -# time["fullyear"] -- year with century (19xx or 20xx) +# time["year"] -- year modulo 100 (0 - 99) +# time["fullyear"] -- full year # time["weekday"] -- day of week (Sunday = 0) # time["altweekday"] -- day of week (Monday = 0) -# time["weeknum"] -- week number, Sunday first day -# time["altweeknum"] -- week number, Monday first day # time["dayname"] -- name of weekday # time["shortdayname"] -- short name of weekday # time["yearday"] -- day of year (0 - 365) # time["timezone"] -- abbreviation of timezone name # time["ampm"] -- AM or PM designation +# time["weeknum"] -- week number, Sunday first day +# time["altweeknum"] -- week number, Monday first day function gettimeofday(time, ret, now, i) { @@ -32,8 +34,7 @@ function gettimeofday(time, ret, now, i) ret = strftime("%a %b %d %H:%M:%S %Z %Y", now) # clear out target array - for (i in time) - delete time[i] + delete time # fill in values, force numeric values to be # numeric by adding 0 diff --git a/awklib/eg/lib/grcat.c b/awklib/eg/lib/grcat.c index 570b3230..d34ddd56 100644 --- a/awklib/eg/lib/grcat.c +++ b/awklib/eg/lib/grcat.c @@ -2,9 +2,9 @@ * grcat.c * * Generate a printable version of the group database - * - * Arnold Robbins, arnold@gnu.org - * May 1993 + */ +/* + * Arnold Robbins, arnold@gnu.org, May 1993 * Public Domain */ diff --git a/awklib/eg/lib/groupawk.in b/awklib/eg/lib/groupawk.in index 11a72a5a..9d8b4021 100644 --- a/awklib/eg/lib/groupawk.in +++ b/awklib/eg/lib/groupawk.in @@ -1,13 +1,17 @@ # group.awk --- functions for dealing with the group file +# # Arnold Robbins, arnold@gnu.org, Public Domain # May 1993 +# Revised October 2000 BEGIN \ { # Change to suit your system _gr_awklib = "/usr/local/libexec/awk/" } -function _gr_init( oldfs, oldrs, olddol0, grcat, n, a, i) + +function _gr_init( oldfs, oldrs, olddol0, grcat, + using_fw, n, a, i) { if (_gr_inited) return @@ -15,6 +19,7 @@ function _gr_init( oldfs, oldrs, olddol0, grcat, n, a, i) oldfs = FS oldrs = RS olddol0 = $0 + using_fw = (PROCINFO["FS"] == "FIELDWIDTHS") FS = ":" RS = "\n" @@ -43,6 +48,8 @@ function _gr_init( oldfs, oldrs, olddol0, grcat, n, a, i) _gr_count = 0 _gr_inited++ FS = oldfs + if (using_fw) + FIELDWIDTHS = FIELDWIDTHS RS = oldrs $0 = olddol0 } diff --git a/awklib/eg/lib/join.awk b/awklib/eg/lib/join.awk index f52f5e62..e17b4270 100644 --- a/awklib/eg/lib/join.awk +++ b/awklib/eg/lib/join.awk @@ -1,4 +1,5 @@ # join.awk --- join an array into a string +# # Arnold Robbins, arnold@gnu.org, Public Domain # May 1993 diff --git a/awklib/eg/lib/libintl.awk b/awklib/eg/lib/libintl.awk new file mode 100644 index 00000000..a9402c2a --- /dev/null +++ b/awklib/eg/lib/libintl.awk @@ -0,0 +1,9 @@ +function bindtextdomain(dir, domain) +{ + return dir +} + +function dcgettext(string, domain, category) +{ + return string +} diff --git a/awklib/eg/lib/mktime.awk b/awklib/eg/lib/mktime.awk deleted file mode 100644 index 57ff20e5..00000000 --- a/awklib/eg/lib/mktime.awk +++ /dev/null @@ -1,105 +0,0 @@ -# mktime.awk --- convert a canonical date representation -# into a timestamp -# Arnold Robbins, arnold@gnu.org, Public Domain -# May 1993 - -BEGIN \ -{ - # Initialize table of month lengths - _tm_months[0,1] = _tm_months[1,1] = 31 - _tm_months[0,2] = 28; _tm_months[1,2] = 29 - _tm_months[0,3] = _tm_months[1,3] = 31 - _tm_months[0,4] = _tm_months[1,4] = 30 - _tm_months[0,5] = _tm_months[1,5] = 31 - _tm_months[0,6] = _tm_months[1,6] = 30 - _tm_months[0,7] = _tm_months[1,7] = 31 - _tm_months[0,8] = _tm_months[1,8] = 31 - _tm_months[0,9] = _tm_months[1,9] = 30 - _tm_months[0,10] = _tm_months[1,10] = 31 - _tm_months[0,11] = _tm_months[1,11] = 30 - _tm_months[0,12] = _tm_months[1,12] = 31 -} -# decide if a year is a leap year -function _tm_isleap(year, ret) -{ - ret = (year % 4 == 0 && year % 100 != 0) || - (year % 400 == 0) - - return ret -} -# convert a date into seconds -function _tm_addup(a, total, yearsecs, daysecs, - hoursecs, i, j) -{ - hoursecs = 60 * 60 - daysecs = 24 * hoursecs - yearsecs = 365 * daysecs - - total = (a[1] - 1970) * yearsecs - - # extra day for leap years - for (i = 1970; i < a[1]; i++) - if (_tm_isleap(i)) - total += daysecs - - j = _tm_isleap(a[1]) - for (i = 1; i < a[2]; i++) - total += _tm_months[j, i] * daysecs - - total += (a[3] - 1) * daysecs - total += a[4] * hoursecs - total += a[5] * 60 - total += a[6] - - return total -} -# mktime --- convert a date into seconds, -# compensate for time zone - -function mktime(str, res1, res2, a, b, i, j, t, diff) -{ - i = split(str, a, " ") # don't rely on FS - - if (i != 6) - return -1 - - # force numeric - for (j in a) - a[j] += 0 - - # validate - if (a[1] < 1970 || - a[2] < 1 || a[2] > 12 || - a[3] < 1 || a[3] > 31 || - a[4] < 0 || a[4] > 23 || - a[5] < 0 || a[5] > 59 || - a[6] < 0 || a[6] > 60 ) - return -1 - - res1 = _tm_addup(a) - t = strftime("%Y %m %d %H %M %S", res1) - - if (_tm_debug) - printf("(%s) -> (%s)\n", str, t) > "/dev/stderr" - - split(t, b, " ") - res2 = _tm_addup(b) - - diff = res1 - res2 - - if (_tm_debug) - printf("diff = %d seconds\n", diff) > "/dev/stderr" - - res1 += diff - - return res1 -} -BEGIN { - if (_tm_test) { - printf "Enter date as yyyy mm dd hh mm ss: " - getline _tm_test_date - t = mktime(_tm_test_date) - r = strftime("%Y %m %d %H %M %S", t) - printf "Got back (%s)\n", r - } -} diff --git a/awklib/eg/lib/nextfile.awk b/awklib/eg/lib/nextfile.awk index 4bb07bc3..caedf0e4 100644 --- a/awklib/eg/lib/nextfile.awk +++ b/awklib/eg/lib/nextfile.awk @@ -1,5 +1,6 @@ # nextfile --- skip remaining records in current file # correctly handle successive occurrences of the same file +# # Arnold Robbins, arnold@gnu.org, Public Domain # May, 1993 diff --git a/awklib/eg/lib/noassign.awk b/awklib/eg/lib/noassign.awk new file mode 100644 index 00000000..d6d176e4 --- /dev/null +++ b/awklib/eg/lib/noassign.awk @@ -0,0 +1,17 @@ +# noassign.awk --- library file to avoid the need for a +# special option that disables command-line assignments +# +# Arnold Robbins, arnold@gnu.org, Public Domain +# October 1999 + +function disable_assigns(argc, argv, i) +{ + for (i = 1; i < argc; i++) + if (argv[i] ~ /^[A-Za-z_][A-Za-z_0-9]*=.*/) + argv[i] = ("./" argv[i]) +} + +BEGIN { + if (No_command_assign) + disable_assigns(ARGC, ARGV) +} diff --git a/awklib/eg/lib/ord.awk b/awklib/eg/lib/ord.awk index b19149eb..3eacbcc5 100644 --- a/awklib/eg/lib/ord.awk +++ b/awklib/eg/lib/ord.awk @@ -1,16 +1,15 @@ # ord.awk --- do ord and chr -# + # Global identifiers: # _ord_: numerical values indexed by characters # _ord_init: function to initialize _ord_ # -# Arnold Robbins -# arnold@gnu.org -# Public Domain +# Arnold Robbins, arnold@gnu.org, Public Domain # 16 January, 1992 # 20 July, 1992, revised BEGIN { _ord_init() } + function _ord_init( low, high, i, t) { low = sprintf("%c", 7) # BEL is ascii 7 @@ -37,18 +36,9 @@ function ord(str, c) c = substr(str, 1, 1) return _ord_[c] } + function chr(c) { # force c to be numeric by adding 0 return sprintf("%c", c + 0) } -#### test code #### -# BEGIN \ -# { -# for (;;) { -# printf("enter a character: ") -# if (getline var <= 0) -# break -# printf("ord(%s) = %d\n", var, ord(var)) -# } -# } diff --git a/awklib/eg/lib/passwdawk.in b/awklib/eg/lib/passwdawk.in index dd6ee7f5..5f6f9e26 100644 --- a/awklib/eg/lib/passwdawk.in +++ b/awklib/eg/lib/passwdawk.in @@ -1,21 +1,26 @@ # passwd.awk --- access password file information +# # Arnold Robbins, arnold@gnu.org, Public Domain # May 1993 +# Revised October 2000 BEGIN { # tailor this to suit your system _pw_awklib = "/usr/local/libexec/awk/" } -function _pw_init( oldfs, oldrs, olddol0, pwcat) +function _pw_init( oldfs, oldrs, olddol0, pwcat, using_fw) { if (_pw_inited) return + oldfs = FS oldrs = RS olddol0 = $0 + using_fw = (PROCINFO["FS"] == "FIELDWIDTHS") FS = ":" RS = "\n" + pwcat = _pw_awklib "pwcat" while ((pwcat | getline) > 0) { _pw_byname[$1] = $0 @@ -26,6 +31,8 @@ function _pw_init( oldfs, oldrs, olddol0, pwcat) _pw_count = 0 _pw_inited = 1 FS = oldfs + if (using_fw) + FIELDWIDTHS = FIELDWIDTHS RS = oldrs $0 = olddol0 } diff --git a/awklib/eg/lib/pwcat.c b/awklib/eg/lib/pwcat.c index 591b8523..b9a71340 100644 --- a/awklib/eg/lib/pwcat.c +++ b/awklib/eg/lib/pwcat.c @@ -2,10 +2,9 @@ * pwcat.c * * Generate a printable version of the password database - * - * Arnold Robbins - * arnold@gnu.org - * May 1993 + */ +/* + * Arnold Robbins, arnold@gnu.org, May 1993 * Public Domain */ diff --git a/awklib/eg/lib/readable.awk b/awklib/eg/lib/readable.awk new file mode 100644 index 00000000..51705aea --- /dev/null +++ b/awklib/eg/lib/readable.awk @@ -0,0 +1,16 @@ +# readable.awk --- library file to skip over unreadable files +# +# Arnold Robbins, arnold@gnu.org, Public Domain +# October 2000 + +BEGIN { + for (i = 1; i < ARGC; i++) { + if (ARGV[i] ~ /^[A-Za-z_][A-Za-z0-9_]*=.*/ \ + || ARGV[i] == "-") + continue # assignment or standard input + else if ((getline junk < ARGV[i]) < 0) # unreadable + delete ARGV[i] + else + close(ARGV[i]) + } +} diff --git a/awklib/eg/lib/rewind.awk b/awklib/eg/lib/rewind.awk new file mode 100644 index 00000000..33286611 --- /dev/null +++ b/awklib/eg/lib/rewind.awk @@ -0,0 +1,20 @@ +# rewind.awk --- rewind the current file and start over +# +# Arnold Robbins, arnold@gnu.org, Public Domain +# September 2000 + +function rewind( i) +{ + # shift remaining arguments up + for (i = ARGC; i > ARGIND; i--) + ARGV[i] = ARGV[i-1] + + # make sure gawk knows to keep going + ARGC++ + + # make current file next to get done + ARGV[ARGIND+1] = FILENAME + + # do it + nextfile +} diff --git a/awklib/eg/lib/round.awk b/awklib/eg/lib/round.awk index 67ce6469..bf16c2b6 100644 --- a/awklib/eg/lib/round.awk +++ b/awklib/eg/lib/round.awk @@ -1,7 +1,7 @@ # round --- do normal rounding # -# Arnold Robbins, arnold@gnu.org, August, 1996 -# Public Domain +# Arnold Robbins, arnold@gnu.org, Public Domain +# August, 1996 function round(x, ival, aval, fraction) { diff --git a/awklib/eg/misc/findpat.awk b/awklib/eg/misc/findpat.awk new file mode 100644 index 00000000..e9bef9ea --- /dev/null +++ b/awklib/eg/misc/findpat.awk @@ -0,0 +1,10 @@ +{ + if ($1 == "FIND") + regex = $2 + else { + where = match($0, regex) + if (where != 0) + print "Match of", regex, "found at", + where, "in", $0 + } +} diff --git a/awklib/eg/misc/findpat.sh b/awklib/eg/misc/findpat.sh deleted file mode 100644 index 39710324..00000000 --- a/awklib/eg/misc/findpat.sh +++ /dev/null @@ -1,10 +0,0 @@ -awk '{ - if ($1 == "FIND") - regex = $2 - else { - where = match($0, regex) - if (where != 0) - print "Match of", regex, "found at", \ - where, "in", $0 - } -}' diff --git a/awklib/eg/network/PostAgent.sh b/awklib/eg/network/PostAgent.sh new file mode 100644 index 00000000..ccf9a687 --- /dev/null +++ b/awklib/eg/network/PostAgent.sh @@ -0,0 +1,9 @@ +#!/bin/sh +MobAg=/tmp/MobileAgent.$$ +# direct script to mobile agent file +cat > $MobAg +# execute agent concurrently +gawk -f $MobAg $MobAg > /dev/null & +# HTTP header, terminator and body +gawk 'BEGIN { print "\r\nAgent started" }' +rm $MobAg # delete script file of agent diff --git a/awklib/eg/network/coreserv.awk b/awklib/eg/network/coreserv.awk new file mode 100644 index 00000000..e9e61710 --- /dev/null +++ b/awklib/eg/network/coreserv.awk @@ -0,0 +1,113 @@ +# CGI Library and core of a web server +# +# Juergen Kahrs, Juergen.Kahrs@vr-web.de +# with Arnold Robbins, arnold@gnu.org +# September 2000 + +# Global arrays +# GETARG --- arguments to CGI GET command +# MENU --- menu items (path names) +# PARAM --- parameters of form x=y + +# Optional variable MyHost contains host address +# Optional variable MyPort contains port number +# Needs TopHeader, TopDoc, TopFooter +# Sets MyPrefix, HttpService, Status, Reason + +BEGIN { + if (MyHost == "") { + "uname -n" | getline MyHost + close("uname -n") + } + if (MyPort == 0) MyPort = 8080 + HttpService = "/inet/tcp/" MyPort "/0/0" + MyPrefix = "http://" MyHost ":" MyPort + SetUpServer() + while ("awk" != "complex") { + # header lines are terminated this way + RS = ORS = "\r\n" + Status = 200 # this means OK + Reason = "OK" + Header = TopHeader + Document = TopDoc + Footer = TopFooter + if (GETARG["Method"] == "GET") { + HandleGET() + } else if (GETARG["Method"] == "HEAD") { + # not yet implemented + } else if (GETARG["Method"] != "") { + print "bad method", GETARG["Method"] + } + Prompt = Header Document Footer + print "HTTP/1.0", Status, Reason |& HttpService + print "Connection: Close" |& HttpService + print "Pragma: no-cache" |& HttpService + len = length(Prompt) + length(ORS) + print "Content-length:", len |& HttpService + print ORS Prompt |& HttpService + # ignore all the header lines + while ((HttpService |& getline) > 0) + continue + # stop talking to this client + close(HttpService) + # wait for new client request + HttpService |& getline + # do some logging + print systime(), strftime(), $0 + CGI_setup($1, $2, $3) + } +} + +function CGI_setup( method, uri, version, i) +{ + delete GETARG + delete MENU + delete PARAM + GETARG["Method"] = method + GETARG["URI"] = uri + GETARG["Version"] = version + + i = index(uri, "?") + if (i > 0) { # is there a "?" indicating a CGI request? + split(substr(uri, 1, i-1), MENU, "[/:]") + split(substr(uri, i+1), PARAM, "&") + for (i in PARAM) { + PARAM[i] = _CGI_decode(PARAM[i]) + j = index(PARAM[i], "=") + GETARG[substr(PARAM[i], 1, j-1)] = \ + substr(PARAM[i], j+1) + } + } else { # there is no "?", no need for splitting PARAMs + split(uri, MENU, "[/:]") + } + for (i in MENU) # decode characters in path + if (i > 4) # but not those in host name + MENU[i] = _CGI_decode(MENU[i]) +} +function _CGI_decode(str, hexdigs, i, pre, code1, code2, + val, result) +{ + hexdigs = "123456789abcdef" + + i = index(str, "%") + if (i == 0) # no work to do + return str + + do { + pre = substr(str, 1, i-1) # part before %xx + code1 = substr(str, i+1, 1) # first hex digit + code2 = substr(str, i+2, 1) # second hex digit + str = substr(str, i+3) # rest of string + + code1 = tolower(code1) + code2 = tolower(code2) + val = index(hexdigs, code1) * 16 \ + + index(hexdigs, code2) + + result = result pre sprintf("%c", val) + i = index(str, "%") + } while (i != 0) + if (length(str) > 0) + result = result str + return result +} diff --git a/awklib/eg/network/eliza.awk b/awklib/eg/network/eliza.awk new file mode 100644 index 00000000..2e022b41 --- /dev/null +++ b/awklib/eg/network/eliza.awk @@ -0,0 +1,270 @@ +function SetUpServer() { + SetUpEliza() + TopHeader = \ + "An HTTP-based System with GAWK\ + \ + " + TopDoc = "\ +

Please choose one of the following actions:

\ +
" + TopFooter = "" +} +function HandleGET() { + # A real HTTP server would treat some parts of the URI as a file name. + # We take parts of the URI as menu choices and go on accordingly. + if(MENU[2] == "AboutServer") { + Document = "This is not a CGI script.\ + This is an httpd, an HTML file, and a CGI script all \ + in one GAWK script. It needs no separate www-server, \ + no installation, and no root privileges.\ +

To run it, do this:

\

\ Details of HTTP come from:

JK 14.9.1997

" + } else if (MENU[2] == "AboutELIZA") { + Document = "This is an implementation of the famous ELIZA\ + program by Joseph Weizenbaum. It is written in GAWK and\ +/bin/sh: expad: command not found + } else if (MENU[2] == "StartELIZA") { + gsub(/\+/, " ", GETARG["YouSay"]) + # Here we also have to substitute coded special characters + Document = "
" \ + "

" ElizaSays(GETARG["YouSay"]) "

\ +

\ +

" + } +} +function ElizaSays(YouSay) { + if (YouSay == "") { + cost = 0 + answer = "HI, IM ELIZA, TELL ME YOUR PROBLEM" + } else { + q = toupper(YouSay) + gsub("'", "", q) + if(q == qold) { + answer = "PLEASE DONT REPEAT YOURSELF !" + } else { + if (index(q, "SHUT UP") > 0) { + answer = "WELL, PLEASE PAY YOUR BILL. ITS EXACTLY ... $"\ + int(100*rand()+30+cost/100) + } else { + qold = q + w = "-" # no keyword recognized yet + for (i in k) { # search for keywords + if (index(q, i) > 0) { + w = i + break + } + } + if (w == "-") { # no keyword, take old subject + w = wold + subj = subjold + } else { # find subject + subj = substr(q, index(q, w) + length(w)+1) + wold = w + subjold = subj # remember keyword and subject + } + for (i in conj) + gsub(i, conj[i], q) # conjugation + # from all answers to this keyword, select one randomly + answer = r[indices[int(split(k[w], indices) * rand()) + 1]] + # insert subject into answer + gsub("_", subj, answer) + } + } + } + cost += length(answer) # for later payment : 1 cent per character + return answer +} +function SetUpEliza() { + srand() + wold = "-" + subjold = " " + + # table for conjugation + conj[" ARE " ] = " AM " + conj["WERE " ] = "WAS " + conj[" YOU " ] = " I " + conj["YOUR " ] = "MY " + conj[" IVE " ] =\ + conj[" I HAVE " ] = " YOU HAVE " + conj[" YOUVE " ] =\ + conj[" YOU HAVE "] = " I HAVE " + conj[" IM " ] =\ + conj[" I AM " ] = " YOU ARE " + conj[" YOURE " ] =\ + conj[" YOU ARE " ] = " I AM " + + # table of all answers + r[1] = "DONT YOU BELIEVE THAT I CAN _" + r[2] = "PERHAPS YOU WOULD LIKE TO BE ABLE TO _ ?" + r[3] = "YOU WANT ME TO BE ABLE TO _ ?" + r[4] = "PERHAPS YOU DONT WANT TO _ " + r[5] = "DO YOU WANT TO BE ABLE TO _ ?" + r[6] = "WHAT MAKES YOU THINK I AM _ ?" + r[7] = "DOES IT PLEASE YOU TO BELIEVE I AM _ ?" + r[8] = "PERHAPS YOU WOULD LIKE TO BE _ ?" + r[9] = "DO YOU SOMETIMES WISH YOU WERE _ ?" + r[10] = "DONT YOU REALLY _ ?" + r[11] = "WHY DONT YOU _ ?" + r[12] = "DO YOU WISH TO BE ABLE TO _ ?" + r[13] = "DOES THAT TROUBLE YOU ?" + r[14] = "TELL ME MORE ABOUT SUCH FEELINGS" + r[15] = "DO YOU OFTEN FEEL _ ?" + r[16] = "DO YOU ENJOY FEELING _ ?" + r[17] = "DO YOU REALLY BELIEVE I DONT _ ?" + r[18] = "PERHAPS IN GOOD TIME I WILL _ " + r[19] = "DO YOU WANT ME TO _ ?" + r[20] = "DO YOU THINK YOU SHOULD BE ABLE TO _ ?" + r[21] = "WHY CANT YOU _ ?" + r[22] = "WHY ARE YOU INTERESTED IN WHETHER OR NOT I AM _ ?" + r[23] = "WOULD YOU PREFER IF I WERE NOT _ ?" + r[24] = "PERHAPS IN YOUR FANTASIES I AM _ " + r[25] = "HOW DO YOU KNOW YOU CANT _ ?" + r[26] = "HAVE YOU TRIED ?" + r[27] = "PERHAPS YOU CAN NOW _ " + r[28] = "DID YOU COME TO ME BECAUSE YOU ARE _ ?" + r[29] = "HOW LONG HAVE YOU BEEN _ ?" + r[30] = "DO YOU BELIEVE ITS NORMAL TO BE _ ?" + r[31] = "DO YOU ENJOY BEING _ ?" + r[32] = "WE WERE DISCUSSING YOU -- NOT ME" + r[33] = "Oh, I _" + r[34] = "YOU'RE NOT REALLY TALKING ABOUT ME, ARE YOU ?" + r[35] = "WHAT WOULD IT MEAN TO YOU, IF YOU GOT _ ?" + r[36] = "WHY DO YOU WANT _ ?" + r[37] = "SUPPOSE YOU SOON GOT _" + r[38] = "WHAT IF YOU NEVER GOT _ ?" + r[39] = "I SOMETIMES ALSO WANT _" + r[40] = "WHY DO YOU ASK ?" + r[41] = "DOES THAT QUESTION INTEREST YOU ?" + r[42] = "WHAT ANSWER WOULD PLEASE YOU THE MOST ?" + r[43] = "WHAT DO YOU THINK ?" + r[44] = "ARE SUCH QUESTIONS IN YOUR MIND OFTEN ?" + r[45] = "WHAT IS IT THAT YOU REALLY WANT TO KNOW ?" + r[46] = "HAVE YOU ASKED ANYONE ELSE ?" + r[47] = "HAVE YOU ASKED SUCH QUESTIONS BEFORE ?" + r[48] = "WHAT ELSE COMES TO MIND WHEN YOU ASK THAT ?" + r[49] = "NAMES DON'T INTEREST ME" + r[50] = "I DONT CARE ABOUT NAMES -- PLEASE GO ON" + r[51] = "IS THAT THE REAL REASON ?" + r[52] = "DONT ANY OTHER REASONS COME TO MIND ?" + r[53] = "DOES THAT REASON EXPLAIN ANYTHING ELSE ?" + r[54] = "WHAT OTHER REASONS MIGHT THERE BE ?" + r[55] = "PLEASE DON'T APOLOGIZE !" + r[56] = "APOLOGIES ARE NOT NECESSARY" + r[57] = "WHAT FEELINGS DO YOU HAVE WHEN YOU APOLOGIZE ?" + r[58] = "DON'T BE SO DEFENSIVE" + r[59] = "WHAT DOES THAT DREAM SUGGEST TO YOU ?" + r[60] = "DO YOU DREAM OFTEN ?" + r[61] = "WHAT PERSONS APPEAR IN YOUR DREAMS ?" + r[62] = "ARE YOU DISTURBED BY YOUR DREAMS ?" + r[63] = "HOW DO YOU DO ... PLEASE STATE YOUR PROBLEM" + r[64] = "YOU DON'T SEEM QUITE CERTAIN" + r[65] = "WHY THE UNCERTAIN TONE ?" + r[66] = "CAN'T YOU BE MORE POSITIVE ?" + r[67] = "YOU AREN'T SURE ?" + r[68] = "DON'T YOU KNOW ?" + r[69] = "WHY NO _ ?" + r[70] = "DON'T SAY NO, IT'S ALWAYS SO NEGATIVE" + r[71] = "WHY NOT ?" + r[72] = "ARE YOU SURE ?" + r[73] = "WHY NO ?" + r[74] = "WHY ARE YOU CONCERNED ABOUT MY _ ?" + r[75] = "WHAT ABOUT YOUR OWN _ ?" + r[76] = "CAN'T YOU THINK ABOUT A SPECIFIC EXAMPLE ?" + r[77] = "WHEN ?" + r[78] = "WHAT ARE YOU THINKING OF ?" + r[79] = "REALLY, ALWAYS ?" + r[80] = "DO YOU REALLY THINK SO ?" + r[81] = "BUT YOU ARE NOT SURE YOU _ " + r[82] = "DO YOU DOUBT YOU _ ?" + r[83] = "IN WHAT WAY ?" + r[84] = "WHAT RESEMBLANCE DO YOU SEE ?" + r[85] = "WHAT DOES THE SIMILARITY SUGGEST TO YOU ?" + r[86] = "WHAT OTHER CONNECTION DO YOU SEE ?" + r[87] = "COULD THERE REALLY BE SOME CONNECTIONS ?" + r[88] = "HOW ?" + r[89] = "YOU SEEM QUITE POSITIVE" + r[90] = "ARE YOU SURE ?" + r[91] = "I SEE" + r[92] = "I UNDERSTAND" + r[93] = "WHY DO YOU BRING UP THE TOPIC OF FRIENDS ?" + r[94] = "DO YOUR FRIENDS WORRY YOU ?" + r[95] = "DO YOUR FRIENDS PICK ON YOU ?" + r[96] = "ARE YOU SURE YOU HAVE ANY FRIENDS ?" + r[97] = "DO YOU IMPOSE ON YOUR FRIENDS ?" + r[98] = "PERHAPS YOUR LOVE FOR FRIENDS WORRIES YOU" + r[99] = "DO COMPUTERS WORRY YOU ?" + r[100] = "ARE YOU TALKING ABOUT ME IN PARTICULAR ?" + r[101] = "ARE YOU FRIGHTENED BY MACHINES ?" + r[102] = "WHY DO YOU MENTION COMPUTERS ?" + r[103] = "WHAT DO YOU THINK MACHINES HAVE TO DO WITH YOUR PROBLEMS ?" + r[104] = "DON'T YOU THINK COMPUTERS CAN HELP PEOPLE ?" + r[105] = "WHAT IS IT ABOUT MACHINES THAT WORRIES YOU ?" + r[106] = "SAY, DO YOU HAVE ANY PSYCHOLOGICAL PROBLEMS ?" + r[107] = "WHAT DOES THAT SUGGEST TO YOU ?" + r[108] = "I SEE" + r[109] = "IM NOT SURE I UNDERSTAND YOU FULLY" + r[110] = "COME COME ELUCIDATE YOUR THOUGHTS" + r[111] = "CAN YOU ELABORATE ON THAT ?" + r[112] = "THAT IS QUITE INTERESTING" + r[113] = "WHY DO YOU HAVE PROBLEMS WITH MONEY ?" + r[114] = "DO YOU THINK MONEY IS EVERYTHING ?" + r[115] = "ARE YOU SURE THAT MONEY IS THE PROBLEM ?" + r[116] = "I THINK WE WANT TO TALK ABOUT YOU, NOT ABOUT ME" + r[117] = "WHAT'S ABOUT ME ?" + r[118] = "WHY DO YOU ALWAYS BRING UP MY NAME ?" + # table for looking up answers that + # fit to a certain keyword + k["CAN YOU"] = "1 2 3" + k["CAN I"] = "4 5" + k["YOU ARE"] =\ + k["YOURE"] = "6 7 8 9" + k["I DONT"] = "10 11 12 13" + k["I FEEL"] = "14 15 16" + k["WHY DONT YOU"] = "17 18 19" + k["WHY CANT I"] = "20 21" + k["ARE YOU"] = "22 23 24" + k["I CANT"] = "25 26 27" + k["I AM"] =\ + k["IM "] = "28 29 30 31" + k["YOU "] = "32 33 34" + k["I WANT"] = "35 36 37 38 39" + k["WHAT"] =\ + k["HOW"] =\ + k["WHO"] =\ + k["WHERE"] =\ + k["WHEN"] =\ + k["WHY"] = "40 41 42 43 44 45 46 47 48" + k["NAME"] = "49 50" + k["CAUSE"] = "51 52 53 54" + k["SORRY"] = "55 56 57 58" + k["DREAM"] = "59 60 61 62" + k["HELLO"] =\ + k["HI "] = "63" + k["MAYBE"] = "64 65 66 67 68" + k[" NO "] = "69 70 71 72 73" + k["YOUR"] = "74 75" + k["ALWAYS"] = "76 77 78 79" + k["THINK"] = "80 81 82" + k["LIKE"] = "83 84 85 86 87 88 89" + k["YES"] = "90 91 92" + k["FRIEND"] = "93 94 95 96 97 98" + k["COMPUTER"] = "99 100 101 102 103 104 105" + k["-"] = "106 107 108 109 110 111 112" + k["MONEY"] = "113 114 115" + k["ELIZA"] = "116 117 118" +} diff --git a/awklib/eg/network/fingerclient.awk b/awklib/eg/network/fingerclient.awk new file mode 100644 index 00000000..bcc2c94c --- /dev/null +++ b/awklib/eg/network/fingerclient.awk @@ -0,0 +1,7 @@ +BEGIN { + NetService = "/inet/tcp/0/localhost/finger" + print "var{name}" |& NetService + while ((NetService |& getline) > 0) + print $0 + close(NetService) +} diff --git a/awklib/eg/network/geturl.awk b/awklib/eg/network/geturl.awk new file mode 100644 index 00000000..53853e50 --- /dev/null +++ b/awklib/eg/network/geturl.awk @@ -0,0 +1,24 @@ +BEGIN { + if (ARGC != 2) { + print "GETURL - retrieve Web page via HTTP 1.0" + print "IN:\n the URL as a command-line parameter" + print "PARAM(S):\n -v Proxy=MyProxy" + print "OUT:\n the page content on stdout" + print " the page header on stderr" + print "JK 16.05.1997" + print "ADR 13.08.2000" + exit + } + URL = ARGV[1]; ARGV[1] = "" + if (Proxy == "") Proxy = "127.0.0.1" + if (ProxyPort == 0) ProxyPort = 80 + if (Method == "") Method = "GET" + HttpService = "/inet/tcp/0/" Proxy "/" ProxyPort + ORS = RS = "\r\n\r\n" + print Method " " URL " HTTP/1.0" |& HttpService + HttpService |& getline Header + print Header > "/dev/stderr" + while ((HttpService |& getline) > 0) + printf "%s", $0 + close(HttpService) +} diff --git a/awklib/eg/network/hello-serv.awk b/awklib/eg/network/hello-serv.awk new file mode 100644 index 00000000..003ee088 --- /dev/null +++ b/awklib/eg/network/hello-serv.awk @@ -0,0 +1,14 @@ +BEGIN { + RS = ORS = "\r\n" + HttpService = "/inet/tcp/8080/0/0" + Hello = "" \ + "A Famous Greeting" \ + "

Hello, world

" + Len = length(Hello) + length(ORS) + print "HTTP/1.0 200 OK" |& HttpService + print "Content-Length: " Len ORS |& HttpService + print Hello |& HttpService + while ((HttpService |& getline) > 0) + continue; + close(HttpService) +} diff --git a/awklib/eg/network/maze.awk b/awklib/eg/network/maze.awk new file mode 100644 index 00000000..94888f8c --- /dev/null +++ b/awklib/eg/network/maze.awk @@ -0,0 +1,73 @@ +function SetUpServer() { + TopHeader = "Walk through a maze" + TopDoc = "\ +

Please choose one of the following actions:

\ + " + TopFooter = "" + srand() +} +function HandleGET() { + if (MENU[2] == "AboutServer") { + Document = "If your browser has a VRML 2 plugin,\ + this server shows you a simple VRML scene." + } else if (MENU[2] == "VRMLtest") { + XSIZE = YSIZE = 11 # initially, everything is wall + for (y = 0; y < YSIZE; y++) + for (x = 0; x < XSIZE; x++) + Maze[x, y] = "#" + delete Maze[0, 1] # entry is not wall + delete Maze[XSIZE-1, YSIZE-2] # exit is not wall + MakeMaze(1, 1) + Document = "\ +#VRML V2.0 utf8\n\ +Group {\n\ + children [\n\ + PointLight {\n\ + ambientIntensity 0.2\n\ + color 0.7 0.7 0.7\n\ + location 0.0 8.0 10.0\n\ + }\n\ + DEF B1 Background {\n\ + skyColor [0 0 0, 1.0 1.0 1.0 ]\n\ + skyAngle 1.6\n\ + groundColor [1 1 1, 0.8 0.8 0.8, 0.2 0.2 0.2 ]\n\ + groundAngle [ 1.2 1.57 ]\n\ + }\n\ + DEF Wall Shape {\n\ + geometry Box {size 1 1 1}\n\ + appearance Appearance { material Material { diffuseColor 0 0 1 } }\n\ + }\n\ + DEF Entry Viewpoint {\n\ + position 0.5 1.0 5.0\n\ + orientation 0.0 0.0 -1.0 0.52\n\ + }\n" + for (i in Maze) { + split(i, t, SUBSEP) + Document = Document " Transform { translation " + Document = Document t[1] " 0 -" t[2] " children USE Wall }\n" + } + Document = Document " ] # end of group for world\n}" + Reason = "OK" ORS "Content-type: model/vrml" + Header = Footer = "" + } +} +function MakeMaze(x, y) { + delete Maze[x, y] # here we are, we have no wall here + p = 0 # count unvisited fields in all directions + if (x-2 SUBSEP y in Maze) d[p++] = "-x" + if (x SUBSEP y-2 in Maze) d[p++] = "-y" + if (x+2 SUBSEP y in Maze) d[p++] = "+x" + if (x SUBSEP y+2 in Maze) d[p++] = "+y" + if (p>0) { # if there are univisited fields, go there + p = int(p*rand()) # choose one unvisited field at random + if (d[p] == "-x") { delete Maze[x - 1, y]; MakeMaze(x - 2, y) + } else if (d[p] == "-y") { delete Maze[x, y - 1]; MakeMaze(x, y - 2) + } else if (d[p] == "+x") { delete Maze[x + 1, y]; MakeMaze(x + 2, y) + } else if (d[p] == "+y") { delete Maze[x, y + 1]; MakeMaze(x, y + 2) + } # we are back from recursion + MakeMaze(x, y); # try again while there are unvisited fields + } +} diff --git a/awklib/eg/network/mobag.awk b/awklib/eg/network/mobag.awk new file mode 100644 index 00000000..a8c55008 --- /dev/null +++ b/awklib/eg/network/mobag.awk @@ -0,0 +1,72 @@ +BEGIN { + if (ARGC != 2) { + print "MOBAG - a simple mobile agent" + print "CALL:\n gawk -f mobag.awk mobag.awk" + print "IN:\n the name of this script as a command-line parameter" + print "PARAM:\n -v MyOrigin=myhost.com" + print "OUT:\n the result on stdout" + print "JK 29.03.1998 01.04.1998" + exit + } + if (MyOrigin == "") { + "uname -n" | getline MyOrigin + close("uname -n") + } +} +#ReadMySelf +/^function / { FUNC = $2 } +/^END/ || /^#ReadMySelf/ { FUNC = $1 } +FUNC != "" { MOBFUN[FUNC] = MOBFUN[FUNC] RS $0 } +(FUNC != "") && (/^}/ || /^#EndOfMySelf/) \ + { FUNC = "" } +#EndOfMySelf +function migrate(Destination, MobCode, Label) { + MOBVAR["Label"] = Label + MOBVAR["Destination"] = Destination + RS = ORS = "\r\n" + HttpService = "/inet/tcp/0/" Destination + for (i in MOBFUN) + MobCode = (MobCode "\n" MOBFUN[i]) + MobCode = MobCode "\n\nBEGIN {" + for (i in MOBVAR) + MobCode = (MobCode "\n MOBVAR[\"" i "\"] = \"" MOBVAR[i] "\"") + MobCode = MobCode "\n}\n" + print "POST /cgi-bin/PostAgent.sh HTTP/1.0" |& HttpService + print "Content-length:", length(MobCode) ORS |& HttpService + printf "%s", MobCode |& HttpService + while ((HttpService |& getline) > 0) + print $0 + close(HttpService) +} +END { + if (ARGC != 2) exit # stop when called with wrong parameters + if (MyOrigin != "") # is this the originating host? + MyInit() # if so, initialize the application + else # we are on a host with migrated data + MyJob() # so we do our job +} +function MyInit() { + MOBVAR["MyOrigin"] = MyOrigin + MOBVAR["Machines"] = "localhost/80 max/80 moritz/80 castor/80" + split(MOBVAR["Machines"], Machines) # which host is the first? + migrate(Machines[1], "", "") # go to the first host + while (("/inet/tcp/8080/0/0" |& getline) > 0) # wait for result + print $0 # print result + close("/inet/tcp/8080/0/0") +} +function MyJob() { + # forget this host + sub(MOBVAR["Destination"], "", MOBVAR["Machines"]) + MOBVAR["Result"]=MOBVAR["Result"] SUBSEP SUBSEP MOBVAR["Destination"] ":" + while (("who" | getline) > 0) # who is logged in? + MOBVAR["Result"] = MOBVAR["Result"] SUBSEP $0 + close("who") + if (index(MOBVAR["Machines"], "/") > 0) { # any more machines to visit? + split(MOBVAR["Machines"], Machines) # which host is next? + migrate(Machines[1], "", "") # go there + } else { # no more machines + gsub(SUBSEP, "\n", MOBVAR["Result"]) # send result to origin + print MOBVAR["Result"] |& "/inet/tcp/0/" MOBVAR["MyOrigin"] "/8080" + close("/inet/tcp/0/" MOBVAR["MyOrigin"] "/8080") + } +} diff --git a/awklib/eg/network/panic.awk b/awklib/eg/network/panic.awk new file mode 100644 index 00000000..6db8c46d --- /dev/null +++ b/awklib/eg/network/panic.awk @@ -0,0 +1,18 @@ +BEGIN { + RS = ORS = "\r\n" + if (MyPort == 0) MyPort = 8080 + HttpService = "/inet/tcp/" MyPort "/0/0" + Hello = "Out Of Service" \ + "

" \ + "This site is temporarily out of service." \ + "

" + Len = length(Hello) + length(ORS) + while ("awk" != "complex") { + print "HTTP/1.0 200 OK" |& HttpService + print "Content-Length: " Len ORS |& HttpService + print Hello |& HttpService + while ((HttpService |& getline) > 0) + continue; + close(HttpService) + } +} diff --git a/awklib/eg/network/protbase.awk b/awklib/eg/network/protbase.awk new file mode 100644 index 00000000..16199e45 --- /dev/null +++ b/awklib/eg/network/protbase.awk @@ -0,0 +1,11 @@ +{ request = request "\n" $0 } + +END { + BLASTService = "/inet/tcp/0/www.ncbi.nlm.nih.gov/80" + printf "POST /cgi-bin/BLAST/nph-blast_report HTTP/1.0\n" |& BLASTService + printf "Content-Length: " length(request) "\n\n" |& BLASTService + printf request |& BLASTService + while ((BLASTService |& getline) > 0) + print $0 + close(BLASTService) +} diff --git a/awklib/eg/network/protbase.request b/awklib/eg/network/protbase.request new file mode 100644 index 00000000..4c5c3d2c --- /dev/null +++ b/awklib/eg/network/protbase.request @@ -0,0 +1,7 @@ +PROGRAM blastn +DATALIB month +EXPECT 0.75 +BEGIN +>GAWK310 the gawking gene GNU AWK +tgcttggctgaggagccataggacgagagcttcctggtgaagtgtgtttcttgaaatcat +caccaccatggacagcaaa diff --git a/awklib/eg/network/protbase.result b/awklib/eg/network/protbase.result new file mode 100644 index 00000000..a087af43 --- /dev/null +++ b/awklib/eg/network/protbase.result @@ -0,0 +1,9 @@ +Sequences producing significant alignments: (bits) Value + +gb|AC021182.14|AC021182 Homo sapiens chromosome 7 clone RP11-733... 38 0.20 +gb|AC021056.12|AC021056 Homo sapiens chromosome 3 clone RP11-115... 38 0.20 +emb|AL160278.10|AL160278 Homo sapiens chromosome 9 clone RP11-57... 38 0.20 +emb|AL391139.11|AL391139 Homo sapiens chromosome X clone RP11-35... 38 0.20 +emb|AL365192.6|AL365192 Homo sapiens chromosome 6 clone RP3-421H... 38 0.20 +emb|AL138812.9|AL138812 Homo sapiens chromosome 11 clone RP1-276... 38 0.20 +gb|AC073881.3|AC073881 Homo sapiens chromosome 15 clone CTD-2169... 38 0.20 diff --git a/awklib/eg/network/remconf.awk b/awklib/eg/network/remconf.awk new file mode 100644 index 00000000..f4f21e1a --- /dev/null +++ b/awklib/eg/network/remconf.awk @@ -0,0 +1,54 @@ +function SetUpServer() { + TopHeader = "Remote Configuration" + TopDoc = "\ +

Please choose one of the following actions:

\ + " + TopFooter = "" + if (ConfigFile == "") ConfigFile = "config.asc" +} +function HandleGET() { + if(MENU[2] == "AboutServer") { + Document = "This is a GUI for remote configuration of an\ + embedded system. It is is implemented as one GAWK script." + } else if (MENU[2] == "ReadConfig") { + RS = "\n" + while ((getline < ConfigFile) > 0) + config[$1] = $2; + close(ConfigFile) + RS = "\r\n" + Document = "Configuration has been read." + } else if (MENU[2] == "CheckConfig") { + Document = "" + for (i in config) + Document = Document "" \ + "" + Document = Document "
" i "" config[i] "
" + } else if (MENU[2] == "ChangeConfig") { + if ("Param" in GETARG) { # any parameter to set? + if (GETARG["Param"] in config) { # is parameter valid? + config[GETARG["Param"]] = GETARG["Value"] + Document = (GETARG["Param"] " = " GETARG["Value"] ".") + } else { + Document = "Parameter " GETARG["Param"] " is invalid." + } + } else { + Document = "

Change one parameter

\ + \ + \ + \ + \ +
ParameterValue
" + } + } else if (MENU[2] == "SaveConfig") { + for (i in config) + printf("%s %s\n", i, config[i]) > ConfigFile + close(ConfigFile) + Document = "Configuration has been saved." + } +} diff --git a/awklib/eg/network/statist.awk b/awklib/eg/network/statist.awk new file mode 100644 index 00000000..8a583940 --- /dev/null +++ b/awklib/eg/network/statist.awk @@ -0,0 +1,85 @@ +function SetUpServer() { + TopHeader = "Statistics with GAWK" + TopDoc = "\ +

Please choose one of the following actions:

\ + " + TopFooter = "" + GnuPlot = "gnuplot 2>&1" + m1=m2=0; v1=v2=1; n1=n2=10 +} +function HandleGET() { + if(MENU[2] == "AboutServer") { + Document = "This is a GUI for a statistical computation.\ + It compares means and variances of two distributions.\ + It is implemented as one GAWK script and uses GNUPLOT." + } else if (MENU[2] == "EnterParameters") { + Document = "" + if ("m1" in GETARG) { # are there parameters to compare? + Document = Document "" + m1 = GETARG["m1"]; v1 = GETARG["v1"]; n1 = GETARG["n1"] + m2 = GETARG["m2"]; v2 = GETARG["v2"]; n2 = GETARG["n2"] + t = (m1-m2)/sqrt(v1/n1+v2/n2) + df = (v1/n1+v2/n2)*(v1/n1+v2/n2)/((v1/n1)*(v1/n1)/(n1-1) \ + + (v2/n2)*(v2/n2) /(n2-1)) + if (v1>v2) { + f = v1/v2 + df1 = n1 - 1 + df2 = n2 - 1 + } else { + f = v2/v1 + df1 = n2 - 1 + df2 = n1 - 1 + } + print "pt=ibeta(" df/2 ",0.5," df/(df+t*t) ")" |& GnuPlot + print "pF=2.0*ibeta(" df2/2 "," df1/2 "," \ + df2/(df2+df1*f) ")" |& GnuPlot + print "print pt, pF" |& GnuPlot + RS="\n"; GnuPlot |& getline; RS="\r\n" # $1 is pt, $2 is pF + print "invsqrt2pi=1.0/sqrt(2.0*pi)" |& GnuPlot + print "nd(x)=invsqrt2pi/sd*exp(-0.5*((x-mu)/sd)**2)" |& GnuPlot + print "set term png small color" |& GnuPlot + #print "set term postscript color" |& GnuPlot + #print "set term gif medium size 320,240" |& GnuPlot + print "set yrange[-0.3:]" |& GnuPlot + print "set label 'p(m1=m2) =" $1 "' at 0,-0.1 left" |& GnuPlot + print "set label 'p(v1=v2) =" $2 "' at 0,-0.2 left" |& GnuPlot + print "plot mu=" m1 ",sd=" sqrt(v1) ", nd(x) title 'sample 1',\ + mu=" m2 ",sd=" sqrt(v2) ", nd(x) title 'sample 2'" |& GnuPlot + print "quit" |& GnuPlot + GnuPlot |& getline Image + while ((GnuPlot |& getline) > 0) + Image = Image RS $0 + close(GnuPlot) + } + Document = Document "\ +

Do these samples have the same Gaussian distribution?

\ +
\ + \ + + \ + + \ + + \ + \ + + \ + + \ + + \ + \ +
1. Mean 1. Variance1. Count
2. Mean 2. Variance2. Count

" + } else if (MENU[2] ~ "Image") { + Reason = "OK" ORS "Content-type: image/png" + #Reason = "OK" ORS "Content-type: application/x-postscript" + #Reason = "OK" ORS "Content-type: image/gif" + Header = Footer = "" + Document = Image + } +} diff --git a/awklib/eg/network/stoxdata.txt b/awklib/eg/network/stoxdata.txt new file mode 100644 index 00000000..1b6d015e --- /dev/null +++ b/awklib/eg/network/stoxdata.txt @@ -0,0 +1,4 @@ +Date,Open,High,Low,Close,Volume +9-Oct-00,22.75,22.75,21.375,22.375,7888500 +6-Oct-00,23.8125,24.9375,21.5625,22,10701100 +5-Oct-00,24.4375,24.625,23.125,23.50,5810300 diff --git a/awklib/eg/network/stoxpred.awk b/awklib/eg/network/stoxpred.awk new file mode 100644 index 00000000..5cdc2a8d --- /dev/null +++ b/awklib/eg/network/stoxpred.awk @@ -0,0 +1,116 @@ +function ReadQuotes() { + # Retrieve historical data for each ticker symbol + FS = "," + for (stock = 1; stock <= StockCount; stock++) { + URL = "http://chart.yahoo.com/table.csv?s=" name[stock] \ + "&a=" month "&b=" day "&c=" year-1 \ + "&d=" month "&e=" day "&f=" year \ + "g=d&q=q&y=0&z=" name[stock] "&x=.csv" + printf("GET " URL " HTTP/1.0\r\n\r\n") |& YahooData + while ((YahooData |& getline) > 0) { + if (NF == 6 && $1 ~ /Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/) { + if (stock == 1) + days[++daycount] = $1; + quote[$1, stock] = $5 + } + } + close(YahooData) + } + FS = " " +} +function CleanUp() { + # clean up time series; eliminate incomplete data sets + for (d = 1; d <= daycount; d++) { + for (stock = 1; stock <= StockCount; stock++) + if (! ((days[d], stock) in quote)) + stock = StockCount + 10 + if (stock > StockCount + 1) + continue + datacount++ + for (stock = 1; stock <= StockCount; stock++) + data[datacount, stock] = int(0.5 + quote[days[d], stock]) + } + delete quote + delete days +} +function Prediction() { + # Predict each ticker symbol by prolonging yesterday's trend + for (stock = 1; stock <= StockCount; stock++) { + if (data[1, stock] > data[2, stock]) { + predict[stock] = "up" + } else if (data[1, stock] < data[2, stock]) { + predict[stock] = "down" + } else { + predict[stock] = "neutral" + } + if ((data[1, stock] > data[2, stock]) && (data[2, stock] > data[3, stock])) + hot[stock] = 1 + if ((data[1, stock] < data[2, stock]) && (data[2, stock] < data[3, stock])) + avoid[stock] = 1 + } + # Do a plausibility check: how many predictions proved correct? + for (s = 1; s <= StockCount; s++) { + for (d = 1; d <= datacount-2; d++) { + if (data[d+1, s] > data[d+2, s]) { + UpCount++ + } else if (data[d+1, s] < data[d+2, s]) { + DownCount++ + } else { + NeutralCount++ + } + if (((data[d, s] > data[d+1, s]) && (data[d+1, s] > data[d+2, s])) || + ((data[d, s] < data[d+1, s]) && (data[d+1, s] < data[d+2, s])) || + ((data[d, s] == data[d+1, s]) && (data[d+1, s] == data[d+2, s]))) + CorrectCount++ + } + } +} +function Report() { + # Generate report + report = "\nThis is your daily " + report = report "stock market report for "strftime("%A, %B %d, %Y")".\n" + report = report "Here are the predictions for today:\n\n" + for (stock = 1; stock <= StockCount; stock++) + report = report "\t" name[stock] "\t" predict[stock] "\n" + for (stock in hot) { + if (HotCount++ == 0) + report = report "\nThe most promising shares for today are these:\n\n" + report = report "\t" name[stock] "\t\thttp://biz.yahoo.com/n/" \ + tolower(substr(name[stock], 1, 1)) "/" tolower(name[stock]) ".html\n" + } + for (stock in avoid) { + if (AvoidCount++ == 0) + report = report "\nThe stock shares to avoid today are these:\n\n" + report = report "\t" name[stock] "\t\thttp://biz.yahoo.com/n/" \ + tolower(substr(name[stock], 1, 1)) "/" tolower(name[stock]) ".html\n" + } + report = report "\nThis sums up to " HotCount+0 " winners and " AvoidCount+0 + report = report " losers. When using this kind\nof prediction scheme for" + report = report " the 12 months which lie behind us,\nwe get " UpCount + report = report " 'ups' and " DownCount " 'downs' and " NeutralCount + report = report " 'neutrals'. Of all\nthese " UpCount+DownCount+NeutralCount + report = report " predictions " CorrectCount " proved correct next day.\n" + report = report "A success rate of "\ + int(100*CorrectCount/(UpCount+DownCount+NeutralCount)) "%.\n" + report = report "Random choice would have produced a 33% success rate.\n" + report = report "Disclaimer: Like every other prediction of the stock\n" + report = report "market, this report is, of course, complete nonsense.\n" + report = report "If you are stupid enough to believe these predictions\n" + report = report "you should visit a doctor who can treat your ailment." +} +function SendMail() { + # send report to customers + customer["uncle.scrooge@ducktown.gov"] = "Uncle Scrooge" + customer["more@utopia.org" ] = "Sir Thomas More" + customer["spinoza@denhaag.nl" ] = "Baruch de Spinoza" + customer["marx@highgate.uk" ] = "Karl Marx" + customer["keynes@the.long.run" ] = "John Maynard Keynes" + customer["bierce@devil.hell.org" ] = "Ambrose Bierce" + customer["laplace@paris.fr" ] = "Pierre Simon de Laplace" + for (c in customer) { + MailPipe = "mail -s 'Daily Stock Prediction Newsletter'" c + print "Good morning " customer[c] "," | MailPipe + print report "\n.\n" | MailPipe + close(MailPipe) + } +} diff --git a/awklib/eg/network/testserv.awk b/awklib/eg/network/testserv.awk new file mode 100644 index 00000000..812bfe6a --- /dev/null +++ b/awklib/eg/network/testserv.awk @@ -0,0 +1,12 @@ +BEGIN { + CGI_setup("GET", + "http://www.gnu.org/cgi-bin/foo?p1=stuff&p2=stuff%26junk" \ + "&percent=a %25 sign", + "1.0") + for (i in MENU) + printf "MENU[\"%s\"] = %s\n", i, MENU[i] + for (i in PARAM) + printf "PARAM[\"%s\"] = %s\n", i, PARAM[i] + for (i in GETARG) + printf "GETARG[\"%s\"] = %s\n", i, GETARG[i] +} diff --git a/awklib/eg/network/urlchk.awk b/awklib/eg/network/urlchk.awk new file mode 100644 index 00000000..6ddedfa8 --- /dev/null +++ b/awklib/eg/network/urlchk.awk @@ -0,0 +1,28 @@ +BEGIN { + if (ARGC != 2) { + print "URLCHK - check if URLs have changed" + print "IN:\n the file with URLs as a command-line parameter" + print " file contains URL, old length, new length" + print "PARAMS:\n -v Proxy=MyProxy -v ProxyPort=8080" + print "OUT:\n same as file with URLs" + print "JK 02.03.1998" + exit + } + URLfile = ARGV[1]; ARGV[1] = "" + if (Proxy != "") Proxy = " -v Proxy=" Proxy + if (ProxyPort != "") ProxyPort = " -v ProxyPort=" ProxyPort + while ((getline < URLfile) > 0) + Length[$1] = $3 + 0 + close(URLfile) # now, URLfile is read in and can be updated + GetHeader = "gawk " Proxy ProxyPort " -v Method=\"HEAD\" -f geturl.awk " + for (i in Length) { + GetThisHeader = GetHeader i " 2>&1" + while ((GetThisHeader | getline) > 0) + if (toupper($0) ~ /CONTENT-LENGTH/) NewLength = $2 + 0 + close(GetThisHeader) + print i, Length[i], NewLength > URLfile + if (Length[i] != NewLength) # report only changed URLs + print i, Length[i], NewLength + } + close(URLfile) +} diff --git a/awklib/eg/network/webgrab.awk b/awklib/eg/network/webgrab.awk new file mode 100644 index 00000000..41738805 --- /dev/null +++ b/awklib/eg/network/webgrab.awk @@ -0,0 +1,6 @@ +BEGIN { RS = "http://[#%&\\+\\-\\./0-9\\:;\\?A-Z_a-z\\~]*" } +RT != "" { + command = ("gawk -v Proxy=MyProxy -f geturl.awk " RT \ + " > doc" NR ".html") + print command +} diff --git a/awklib/eg/prog/alarm.awk b/awklib/eg/prog/alarm.awk index e158e451..26252fa1 100644 --- a/awklib/eg/prog/alarm.awk +++ b/awklib/eg/prog/alarm.awk @@ -1,4 +1,7 @@ -# alarm --- set an alarm +# alarm.awk --- set an alarm +# +# Requires gettimeofday library function +# # Arnold Robbins, arnold@gnu.org, Public Domain # May 1993 @@ -11,7 +14,8 @@ BEGIN \ usage2 = sprintf("\t(%s) time ::= hh:mm", ARGV[1]) if (ARGC < 2) { - print usage > "/dev/stderr" + print usage1 > "/dev/stderr" + print usage2 > "/dev/stderr" exit 1 } else if (ARGC == 5) { delay = ARGV[4] + 0 @@ -37,7 +41,7 @@ BEGIN \ message = sprintf("\aIt is now %s!\a", ARGV[1]) else if (index(message, "\a") == 0) message = "\a" message "\a" - # split up dest time + # split up alarm time split(ARGV[1], atime, ":") hour = atime[1] + 0 # force numeric minute = atime[2] + 0 # force numeric diff --git a/awklib/eg/prog/awksed.awk b/awklib/eg/prog/awksed.awk index 1795b24b..4b188c64 100644 --- a/awklib/eg/prog/awksed.awk +++ b/awklib/eg/prog/awksed.awk @@ -1,6 +1,6 @@ # awksed.awk --- do s/foo/bar/g using just print # Thanks to Michael Brennan for the idea - +# # Arnold Robbins, arnold@gnu.org, Public Domain # August 1995 diff --git a/awklib/eg/prog/cut.awk b/awklib/eg/prog/cut.awk index 6e2dd064..d9c6c9b9 100644 --- a/awklib/eg/prog/cut.awk +++ b/awklib/eg/prog/cut.awk @@ -1,13 +1,16 @@ # cut.awk --- implement cut in awk +# # Arnold Robbins, arnold@gnu.org, Public Domain # May 1993 # Options: -# -f list Cut fields -# -d c Field delimiter character -# -c list Cut characters +# -f list Cut fields +# -d c Field delimiter character +# -c list Cut characters +# +# -s Suppress lines without the delimiter # -# -s Suppress lines without the delimiter character +# Requires getopt and join library functions function usage( e1, e2) { @@ -122,7 +125,7 @@ function set_charlist( field, i, j, f, g, t, nfields = j - 1 } { - if (by_fields && suppress && $0 !~ FS) + if (by_fields && suppress && index($0, FS) != 0) next for (i = 1; i <= nfields; i++) { diff --git a/awklib/eg/prog/dupword.awk b/awklib/eg/prog/dupword.awk index 9a518a16..a2cc7d2b 100644 --- a/awklib/eg/prog/dupword.awk +++ b/awklib/eg/prog/dupword.awk @@ -1,10 +1,15 @@ -# dupword --- find duplicate words in text +# dupword.awk --- find duplicate words in text +# # Arnold Robbins, arnold@gnu.org, Public Domain # December 1991 +# Revised October 2000 { $0 = tolower($0) - gsub(/[^A-Za-z0-9 \t]/, ""); + gsub(/[^[:alnum:][:blank:]]/, " "); + $0 = $0 # re-split + if (NF == 0) + next if ($1 == prev) printf("%s:%d: duplicate %s\n", FILENAME, FNR, $1) diff --git a/awklib/eg/prog/egrep.awk b/awklib/eg/prog/egrep.awk index 06762a1f..73f175ca 100644 --- a/awklib/eg/prog/egrep.awk +++ b/awklib/eg/prog/egrep.awk @@ -1,4 +1,5 @@ # egrep.awk --- simulate egrep in awk +# # Arnold Robbins, arnold@gnu.org, Public Domain # May 1993 @@ -9,6 +10,8 @@ # -i ignore case # -l print filenames only # -e argument is pattern +# +# Requires getopt and file transition library functions BEGIN { while ((c = getopt(ARGC, ARGV, "ce:svil")) != -1) { @@ -69,18 +72,20 @@ function endfile(file) if (! matches) next - if (no_print && ! count_only) - nextfile + if (! count_only) { + if (no_print) + nextfile - if (filenames_only && ! count_only) { - print FILENAME - nextfile - } + if (filenames_only) { + print FILENAME + nextfile + } - if (do_filenames && ! count_only) - print FILENAME ":" $0 - else if (! count_only) - print + if (do_filenames) + print FILENAME ":" $0 + else + print + } } END \ { @@ -91,6 +96,7 @@ END \ function usage( e) { e = "Usage: egrep [-csvil] [-e pat] [files ...]" + e = e "\n\tegrep [-csvil] pat [files ...]" print e > "/dev/stderr" exit 1 } diff --git a/awklib/eg/prog/extract.awk b/awklib/eg/prog/extract.awk index 65f3f2d2..5cb191ae 100644 --- a/awklib/eg/prog/extract.awk +++ b/awklib/eg/prog/extract.awk @@ -1,6 +1,9 @@ # extract.awk --- extract files and run programs # from texinfo files -# Arnold Robbins, arnold@gnu.org, Public Domain, May 1993 +# +# Arnold Robbins, arnold@gnu.org, Public Domain +# May 1993 +# Revised September 2000 BEGIN { IGNORECASE = 1 } @@ -41,6 +44,8 @@ BEGIN { IGNORECASE = 1 } break else if (line ~ /^@(end[ \t]+)?group/) continue + else if (line ~ /^@c(omment+)?[ \t]+/) + continue if (index(line, "@") == 0) { print line > curfile continue @@ -58,9 +63,8 @@ BEGIN { IGNORECASE = 1 } print join(a, 1, n, SUBSEP) > curfile } } -function unexpected_eof() -{ - printf("%s:%d: unexpected EOF or error\n", \ +function unexpected_eof() { + printf("%s:%d: unexpected EOF or error\n", FILENAME, FNR) > "/dev/stderr" exit 1 } diff --git a/awklib/eg/prog/guide.awk b/awklib/eg/prog/guide.awk new file mode 100644 index 00000000..a2dea1b7 --- /dev/null +++ b/awklib/eg/prog/guide.awk @@ -0,0 +1,7 @@ +BEGIN { + TEXTDOMAIN = "guide" + bindtextdomain(".") # for testing + print _"Don't Panic" + print _"The Answer Is", 42 + print "Pardon me, Zaphod who?" +} diff --git a/awklib/eg/prog/histsort.awk b/awklib/eg/prog/histsort.awk index 48186d0b..c0a9165a 100644 --- a/awklib/eg/prog/histsort.awk +++ b/awklib/eg/prog/histsort.awk @@ -1,8 +1,9 @@ # histsort.awk --- compact a shell history file +# Thanks to Byron Rakitzis for the general idea +# # Arnold Robbins, arnold@gnu.org, Public Domain # May 1993 -# Thanks to Byron Rakitzis for the general idea { if (data[$0]++ == 0) lines[++count] = $0 diff --git a/awklib/eg/prog/id.awk b/awklib/eg/prog/id.awk index a983c572..af78f760 100644 --- a/awklib/eg/prog/id.awk +++ b/awklib/eg/prog/id.awk @@ -1,6 +1,10 @@ # id.awk --- implement id in awk +# +# Requires user and group library functions +# # Arnold Robbins, arnold@gnu.org, Public Domain # May 1993 +# Revised February 1996 # output is: # uid=12(foo) euid=34(bar) gid=3(baz) \ @@ -8,17 +12,10 @@ BEGIN \ { - if ((getline < "/dev/user") < 0) { - err = "id: no /dev/user support - cannot run" - print err > "/dev/stderr" - exit 1 - } - close("/dev/user") - - uid = $1 - euid = $2 - gid = $3 - egid = $4 + uid = PROCINFO["uid"] + euid = PROCINFO["euid"] + gid = PROCINFO["gid"] + egid = PROCINFO["egid"] printf("uid=%d", uid) pw = getpwuid(uid) @@ -52,18 +49,19 @@ BEGIN \ } } - if (NF > 4) { - printf(" groups="); - for (i = 5; i <= NF; i++) { - printf("%d", $i) - pw = getgrgid($i) - if (pw != "") { - split(pw, a, ":") - printf("(%s)", a[1]) - } - if (i < NF) - printf(",") + for (i = 1; ("group" i) in PROCINFO; i++) { + if (i == 1) + printf(" groups=") + group = PROCINFO["group" i] + printf("%d", group) + pw = getgrgid(group) + if (pw != "") { + split(pw, a, ":") + printf("(%s)", a[1]) } + if (("group" (i+1)) in PROCINFO) + printf(",") } + print "" } diff --git a/awklib/eg/prog/igawk.sh b/awklib/eg/prog/igawk.sh index 6fb70c38..7144ce50 100644 --- a/awklib/eg/prog/igawk.sh +++ b/awklib/eg/prog/igawk.sh @@ -1,6 +1,6 @@ #! /bin/sh - # igawk --- like gawk but do @include processing +# # Arnold Robbins, arnold@gnu.org, Public Domain # July 1993 @@ -37,15 +37,15 @@ do f=`echo "$1" | sed 's/-.file=//'` echo @include "$f" >> /tmp/ig.s.$$ ;; - -?file) # get arg, $2 + -?file) # get arg, $2 echo @include "$2" >> /tmp/ig.s.$$ shift;; - -?source=*) # -Wsource or --source + -?source=*) # -Wsource or --source t=`echo "$1" | sed 's/-.source=//'` echo "$t" >> /tmp/ig.s.$$ ;; - -?source) # get arg, $2 + -?source) # get arg, $2 echo "$2" >> /tmp/ig.s.$$ shift;; @@ -54,7 +54,7 @@ do gawk --version exit 0 ;; - -[W-]*) opts="$opts '$1'" ;; + -[W-]*) opts="$opts '$1'" ;; *) break;; esac @@ -76,6 +76,7 @@ fi # at this point, /tmp/ig.s.$$ has the program gawk -- ' # process @include directives + function pathto(file, i, t, junk) { if (index(file, "/") != 0) @@ -109,16 +110,16 @@ BEGIN { } fpath = pathto($2) if (fpath == "") { - printf("igawk:%s:%d: cannot find %s\n", \ + printf("igawk:%s:%d: cannot find %s\n", input[stackptr], FNR, $2) > "/dev/stderr" continue } if (! (fpath in processed)) { processed[fpath] = input[stackptr] - input[++stackptr] = fpath + input[++stackptr] = fpath # push onto stack } else - print $2, "included in", input[stackptr], \ - "already included in", \ + print $2, "included in", input[stackptr], + "already included in", processed[fpath] > "/dev/stderr" } close(input[stackptr]) diff --git a/awklib/eg/prog/labels.awk b/awklib/eg/prog/labels.awk index 3c69751a..fa9c4dab 100644 --- a/awklib/eg/prog/labels.awk +++ b/awklib/eg/prog/labels.awk @@ -1,10 +1,11 @@ -# labels.awk +# labels.awk --- print mailing labels +# # Arnold Robbins, arnold@gnu.org, Public Domain # June 1992 -# Program to print labels. Each label is 5 lines of data -# that may have blank lines. The label sheets have 2 -# blank lines at the top and 2 at the bottom. +# Each label is 5 lines of data that may have blank lines. +# The label sheets have 2 blank lines at the top and 2 at +# the bottom. BEGIN { RS = "" ; MAXLINES = 100 } diff --git a/awklib/eg/prog/split.awk b/awklib/eg/prog/split.awk index 863ba4e4..2906a853 100644 --- a/awklib/eg/prog/split.awk +++ b/awklib/eg/prog/split.awk @@ -1,4 +1,7 @@ # split.awk --- do split in awk +# +# Requires ord and chr library functions +# # Arnold Robbins, arnold@gnu.org, Public Domain # May 1993 @@ -32,13 +35,14 @@ BEGIN { close(out) if (s2 == "z") { if (s1 == "z") { - printf("split: %s is too large to split\n", \ + printf("split: %s is too large to split\n", FILENAME) > "/dev/stderr" exit 1 } s1 = chr(ord(s1) + 1) s2 = "a" - } else + } + else s2 = chr(ord(s2) + 1) out = (outfile s1 s2) tcount = 1 diff --git a/awklib/eg/prog/tee.awk b/awklib/eg/prog/tee.awk index 4c12c56d..eafc4b9a 100644 --- a/awklib/eg/prog/tee.awk +++ b/awklib/eg/prog/tee.awk @@ -1,4 +1,5 @@ # tee.awk --- tee in awk +# # Arnold Robbins, arnold@gnu.org, Public Domain # May 1993 # Revised December 1995 diff --git a/awklib/eg/prog/testbits.awk b/awklib/eg/prog/testbits.awk new file mode 100644 index 00000000..143cd916 --- /dev/null +++ b/awklib/eg/prog/testbits.awk @@ -0,0 +1,27 @@ +# bits2str --- turn a byte into readable 1's and 0's + +function bits2str(bits, data, mask) +{ + if (bits == 0) + return "0" + + mask = 1 + for (; bits != 0; bits = rshift(bits, 1)) + data = (and(bits, mask) ? "1" : "0") data + + while ((length(data) % 8) != 0) + data = "0" data + + return data +} +BEGIN { + printf "123 = %s\n", bits2str(123) + printf "0123 = %s\n", bits2str(0123) + printf "0x99 = %s\n", bits2str(0x99) + comp = compl(0x99) + printf "compl(0x99) = %#x = %s\n", comp, bits2str(comp) + shift = lshift(0x99, 2) + printf "lshift(0x99, 2) = %#x = %s\n", shift, bits2str(shift) + shift = rshift(0x99, 2) + printf "rshift(0x99, 2) = %#x = %s\n", shift, bits2str(shift) +} diff --git a/awklib/eg/prog/translate.awk b/awklib/eg/prog/translate.awk index 97c4ada6..803700c4 100644 --- a/awklib/eg/prog/translate.awk +++ b/awklib/eg/prog/translate.awk @@ -1,8 +1,9 @@ -# translate --- do tr like stuff +# translate.awk --- do tr-like stuff +# # Arnold Robbins, arnold@gnu.org, Public Domain # August 1989 -# bugs: does not handle things like: tr A-Z a-z, it has +# Bugs: does not handle things like: tr A-Z a-z, it has # to be spelled out. However, if `to' is shorter than `from', # the last character in `to' is used for the rest of `from'. diff --git a/awklib/eg/prog/uniq.awk b/awklib/eg/prog/uniq.awk index d97eecca..cfb50c79 100644 --- a/awklib/eg/prog/uniq.awk +++ b/awklib/eg/prog/uniq.awk @@ -1,4 +1,7 @@ # uniq.awk --- do uniq in awk +# +# Requires getopt and join library functions +# # Arnold Robbins, arnold@gnu.org, Public Domain # May 1993 @@ -81,7 +84,7 @@ NR == 1 { last = $0 next } - + { equal = are_equal() diff --git a/awklib/eg/prog/wc.awk b/awklib/eg/prog/wc.awk index 56aab429..f46616b9 100644 --- a/awklib/eg/prog/wc.awk +++ b/awklib/eg/prog/wc.awk @@ -1,4 +1,5 @@ # wc.awk --- count lines, words, characters +# # Arnold Robbins, arnold@gnu.org, Public Domain # May 1993 @@ -8,6 +9,8 @@ # -c only count characters # # Default is to count lines, words, characters +# +# Requires getopt and file transition library functions BEGIN { # let getopt print a message about @@ -29,11 +32,11 @@ BEGIN { print_total = (ARGC - i > 2) } -function beginfile(file) { +function beginfile(file) +{ chars = lines = words = 0 fname = FILENAME } - function endfile(file) { tchars += chars @@ -53,7 +56,6 @@ function endfile(file) lines++ words += NF } - END { if (print_total) { if (do_lines) diff --git a/awklib/eg/prog/wordfreq.awk b/awklib/eg/prog/wordfreq.awk index b67fed47..62db5cfa 100644 --- a/awklib/eg/prog/wordfreq.awk +++ b/awklib/eg/prog/wordfreq.awk @@ -1,10 +1,17 @@ -# Print list of word frequencies +# wordfreq.awk --- print list of word frequencies + { $0 = tolower($0) # remove case distinctions - gsub(/[^a-z0-9_ \t]/, "", $0) # remove punctuation + # remove punctuation + gsub(/[^[:alnum:]_[:blank:]]/, "", $0) for (i = 1; i <= NF; i++) freq[$i]++ } + +END { + for (word in freq) + printf "%s\t%d\n", word, freq[word] +} END { sort = "sort +1 -nr" for (word in freq) -- cgit v1.2.3