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/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 +++++++++-- 18 files changed, 146 insertions(+), 71 deletions(-) create mode 100644 awklib/eg/prog/guide.awk create mode 100644 awklib/eg/prog/testbits.awk (limited to 'awklib/eg/prog') 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