From 628837bf58f885225532b6c2de41b60ffa7c14e9 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Mon, 5 May 2014 17:46:19 +0300 Subject: Doc edits, through Chapter 9. --- awklib/eg/lib/ctime.awk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'awklib/eg') diff --git a/awklib/eg/lib/ctime.awk b/awklib/eg/lib/ctime.awk index f37856c6..ca750370 100644 --- a/awklib/eg/lib/ctime.awk +++ b/awklib/eg/lib/ctime.awk @@ -4,7 +4,7 @@ function ctime(ts, format) { - format = "%a %b %e %H:%M:%S %Z %Y" + format = PROCINFO["strftime"] if (ts == 0) ts = systime() # use current time as default return strftime(format, ts) -- cgit v1.2.3 From 8b086817a7907d54dbe813f0dd05626b86e56cd1 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Thu, 15 May 2014 22:28:21 +0300 Subject: Edits through Chapter 11, fix displays for docbook. --- awklib/eg/lib/getopt.awk | 2 +- awklib/eg/lib/gettime.awk | 2 +- awklib/eg/lib/grcat.c | 2 +- awklib/eg/lib/pwcat.c | 2 +- awklib/eg/prog/cut.awk | 2 +- awklib/eg/prog/egrep.awk | 4 +--- awklib/eg/prog/id.awk | 37 +++++++++++++++++-------------------- awklib/eg/prog/split.awk | 5 +++-- 8 files changed, 26 insertions(+), 30 deletions(-) (limited to 'awklib/eg') diff --git a/awklib/eg/lib/getopt.awk b/awklib/eg/lib/getopt.awk index 4283a7e1..db957ceb 100644 --- a/awklib/eg/lib/getopt.awk +++ b/awklib/eg/lib/getopt.awk @@ -70,7 +70,7 @@ BEGIN { # test program if (_getopt_test) { while ((_go_c = getopt(ARGC, ARGV, "ab:cd")) != -1) - printf("c = <%c>, optarg = <%s>\n", + printf("c = <%c>, Optarg = <%s>\n", _go_c, Optarg) printf("non-option arguments:\n") for (; Optind < ARGC; Optind++) diff --git a/awklib/eg/lib/gettime.awk b/awklib/eg/lib/gettime.awk index 4cb56330..3da9c8ab 100644 --- a/awklib/eg/lib/gettime.awk +++ b/awklib/eg/lib/gettime.awk @@ -31,7 +31,7 @@ function getlocaltime(time, ret, now, i) now = systime() # return date(1)-style output - ret = strftime("%a %b %e %H:%M:%S %Z %Y", now) + ret = strftime(PROCINFO["strftime"], now) # clear out target array delete time diff --git a/awklib/eg/lib/grcat.c b/awklib/eg/lib/grcat.c index ff2913a1..7d6b6a74 100644 --- a/awklib/eg/lib/grcat.c +++ b/awklib/eg/lib/grcat.c @@ -1,7 +1,7 @@ /* * grcat.c * - * Generate a printable version of the group database + * Generate a printable version of the group database. */ /* * Arnold Robbins, arnold@skeeve.com, May 1993 diff --git a/awklib/eg/lib/pwcat.c b/awklib/eg/lib/pwcat.c index 910e0329..934ef34e 100644 --- a/awklib/eg/lib/pwcat.c +++ b/awklib/eg/lib/pwcat.c @@ -1,7 +1,7 @@ /* * pwcat.c * - * Generate a printable version of the password database + * Generate a printable version of the password database. */ /* * Arnold Robbins, arnold@skeeve.com, May 1993 diff --git a/awklib/eg/prog/cut.awk b/awklib/eg/prog/cut.awk index 1399411e..09ba1f7c 100644 --- a/awklib/eg/prog/cut.awk +++ b/awklib/eg/prog/cut.awk @@ -43,7 +43,7 @@ BEGIN \ if (FS == " ") # defeat awk semantics FS = "[ ]" } else if (c == "s") - suppress++ + suppress = 1 else usage() } diff --git a/awklib/eg/prog/egrep.awk b/awklib/eg/prog/egrep.awk index 56d199c8..86b3cfda 100644 --- a/awklib/eg/prog/egrep.awk +++ b/awklib/eg/prog/egrep.awk @@ -90,9 +90,7 @@ function endfile(file) } END \ { - if (total == 0) - exit 1 - exit 0 + exit (total == 0) } function usage( e) { diff --git a/awklib/eg/prog/id.awk b/awklib/eg/prog/id.awk index 8b60a245..cf744447 100644 --- a/awklib/eg/prog/id.awk +++ b/awklib/eg/prog/id.awk @@ -5,6 +5,7 @@ # Arnold Robbins, arnold@skeeve.com, Public Domain # May 1993 # Revised February 1996 +# Revised May 2014 # output is: # uid=12(foo) euid=34(bar) gid=3(baz) \ @@ -19,34 +20,26 @@ BEGIN \ printf("uid=%d", uid) pw = getpwuid(uid) - if (pw != "") { - split(pw, a, ":") - printf("(%s)", a[1]) - } + if (pw != "") + pr_first_field(pw) if (euid != uid) { printf(" euid=%d", euid) pw = getpwuid(euid) - if (pw != "") { - split(pw, a, ":") - printf("(%s)", a[1]) - } + if (pw != "") + pr_first_field(pw) } printf(" gid=%d", gid) pw = getgrgid(gid) - if (pw != "") { - split(pw, a, ":") - printf("(%s)", a[1]) - } + if (pw != "") + pr_first_field(pw) if (egid != gid) { printf(" egid=%d", egid) pw = getgrgid(egid) - if (pw != "") { - split(pw, a, ":") - printf("(%s)", a[1]) - } + if (pw != "") + pr_first_field(pw) } for (i = 1; ("group" i) in PROCINFO; i++) { @@ -55,13 +48,17 @@ BEGIN \ group = PROCINFO["group" i] printf("%d", group) pw = getgrgid(group) - if (pw != "") { - split(pw, a, ":") - printf("(%s)", a[1]) - } + if (pw != "") + pr_first_field(pw) if (("group" (i+1)) in PROCINFO) printf(",") } print "" } + +function pr_first_field(str, a) +{ + split(str, a, ":") + printf("(%s)", a[1]) +} diff --git a/awklib/eg/prog/split.awk b/awklib/eg/prog/split.awk index c907530b..bcc73ae6 100644 --- a/awklib/eg/prog/split.awk +++ b/awklib/eg/prog/split.awk @@ -4,8 +4,9 @@ # # Arnold Robbins, arnold@skeeve.com, Public Domain # May 1993 +# Revised slightly, May 2014 -# usage: split [-num] [file] [outname] +# usage: split [-count] [file] [outname] BEGIN { outfile = "x" # default @@ -14,7 +15,7 @@ BEGIN { usage() i = 1 - if (ARGV[i] ~ /^-[[:digit:]]+$/) { + if (i in ARGV && ARGV[i] ~ /^-[[:digit:]]+$/) { count = -ARGV[i] ARGV[i] = "" i++ -- cgit v1.2.3 From daf5b0c87115b6793c5ac6568009916d4be3152b Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Tue, 3 Jun 2014 16:41:42 +0300 Subject: More doc improvements. --- awklib/eg/prog/alarm.awk | 2 +- awklib/eg/prog/cut.awk | 6 +++--- awklib/eg/prog/extract.awk | 8 ++++---- awklib/eg/prog/igawk.sh | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) (limited to 'awklib/eg') diff --git a/awklib/eg/prog/alarm.awk b/awklib/eg/prog/alarm.awk index 9bb1633c..63cf64a4 100644 --- a/awklib/eg/prog/alarm.awk +++ b/awklib/eg/prog/alarm.awk @@ -71,7 +71,7 @@ BEGIN \ # how long to sleep for naptime = target - current if (naptime <= 0) { - print "time is in the past!" > "/dev/stderr" + print "alarm: time is in the past!" > "/dev/stderr" exit 1 } # zzzzzz..... go away if interrupted diff --git a/awklib/eg/prog/cut.awk b/awklib/eg/prog/cut.awk index 09ba1f7c..04d9bc11 100644 --- a/awklib/eg/prog/cut.awk +++ b/awklib/eg/prog/cut.awk @@ -34,7 +34,7 @@ BEGIN \ OFS = "" } else if (c == "d") { if (length(Optarg) > 1) { - printf("Using first character of %s" \ + printf("cut: using first character of %s" \ " for delimiter\n", Optarg) > "/dev/stderr" Optarg = substr(Optarg, 1, 1) } @@ -75,7 +75,7 @@ function set_fieldlist( n, m, i, j, k, f, g) if (index(f[i], "-") != 0) { # a range m = split(f[i], g, "-") if (m != 2 || g[1] >= g[2]) { - printf("bad field list: %s\n", + printf("cut: bad field list: %s\n", f[i]) > "/dev/stderr" exit 1 } @@ -96,7 +96,7 @@ function set_charlist( field, i, j, f, g, n, m, t, if (index(f[i], "-") != 0) { # range m = split(f[i], g, "-") if (m != 2 || g[1] >= g[2]) { - printf("bad character list: %s\n", + printf("cut: bad character list: %s\n", f[i]) > "/dev/stderr" exit 1 } diff --git a/awklib/eg/prog/extract.awk b/awklib/eg/prog/extract.awk index dc105728..12e30b54 100644 --- a/awklib/eg/prog/extract.awk +++ b/awklib/eg/prog/extract.awk @@ -10,7 +10,7 @@ BEGIN { IGNORECASE = 1 } /^@c(omment)?[ \t]+system/ \ { if (NF < 3) { - e = (FILENAME ":" FNR) + e = ("extract: " FILENAME ":" FNR) e = (e ": badly formed `system' line") print e > "/dev/stderr" next @@ -19,7 +19,7 @@ BEGIN { IGNORECASE = 1 } $2 = "" stat = system($0) if (stat != 0) { - e = (FILENAME ":" FNR) + e = ("extract: " FILENAME ":" FNR) e = (e ": warning: system returned " stat) print e > "/dev/stderr" } @@ -27,7 +27,7 @@ BEGIN { IGNORECASE = 1 } /^@c(omment)?[ \t]+file/ \ { if (NF != 3) { - e = (FILENAME ":" FNR ": badly formed `file' line") + e = ("extract: " FILENAME ":" FNR ": badly formed `file' line") print e > "/dev/stderr" next } @@ -65,7 +65,7 @@ BEGIN { IGNORECASE = 1 } } function unexpected_eof() { - printf("%s:%d: unexpected EOF or error\n", + printf("extract: %s:%d: unexpected EOF or error\n", FILENAME, FNR) > "/dev/stderr" exit 1 } diff --git a/awklib/eg/prog/igawk.sh b/awklib/eg/prog/igawk.sh index 03d1c996..70edf606 100644 --- a/awklib/eg/prog/igawk.sh +++ b/awklib/eg/prog/igawk.sh @@ -115,7 +115,7 @@ 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 } -- cgit v1.2.3 From ec27289f558e73b40c3d90f599cf392a9d03e864 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sun, 15 Jun 2014 20:36:33 +0300 Subject: Finish up summaries. Improvements in mystrtonum(). --- awklib/eg/lib/strtonum.awk | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'awklib/eg') diff --git a/awklib/eg/lib/strtonum.awk b/awklib/eg/lib/strtonum.awk index a56ab50c..9342e789 100644 --- a/awklib/eg/lib/strtonum.awk +++ b/awklib/eg/lib/strtonum.awk @@ -3,8 +3,9 @@ # # Arnold Robbins, arnold@skeeve.com, Public Domain # February, 2004 +# Revised June, 2014 -function mystrtonum(str, ret, chars, n, i, k, c) +function mystrtonum(str, ret, n, i, k, c) { if (str ~ /^0[0-7]*$/) { # octal @@ -17,7 +18,7 @@ function mystrtonum(str, ret, chars, n, i, k, c) ret = ret * 8 + k } - } else if (str ~ /^0[xX][[:xdigit:]]+/) { + } else if (str ~ /^0[xX][[:xdigit:]]+$/) { # hexadecimal str = substr(str, 3) # lop off leading 0x n = length(str) @@ -25,10 +26,7 @@ function mystrtonum(str, ret, chars, n, i, k, c) for (i = 1; i <= n; i++) { c = substr(str, i, 1) c = tolower(c) - if ((k = index("0123456789", c)) > 0) - k-- # adjust for 1-basing in awk - else if ((k = index("abcdef", c)) > 0) - k += 9 + k = index("123456789abcdef", c) ret = ret * 16 + k } -- cgit v1.2.3 From 9e2907afe246b3930d9ae6043a2657c4492f4507 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Tue, 12 Aug 2014 21:15:26 +0300 Subject: Add div.awk to awklib. --- awklib/eg/lib/div.awk | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 awklib/eg/lib/div.awk (limited to 'awklib/eg') diff --git a/awklib/eg/lib/div.awk b/awklib/eg/lib/div.awk new file mode 100644 index 00000000..9d919288 --- /dev/null +++ b/awklib/eg/lib/div.awk @@ -0,0 +1,17 @@ +# div --- do integer division + +# +# Arnold Robbins, arnold@skeeve.com, Public Domain +# July, 2014 + +function div(numerator, denominator, result, i) +{ + split("", result) + + numerator = int(numerator) + denominator = int(denominator) + result["quotient"] = int(numerator / denominator) + result["remainder"] = int(numerator % denominator) + + return 0.0 +} -- cgit v1.2.3 From 3defec04e39c4ca6987a21f79686576d9823c653 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sat, 23 Aug 2014 22:40:59 +0300 Subject: More reviewer comments. --- awklib/eg/lib/groupawk.in | 3 +-- awklib/eg/prog/alarm.awk | 3 +-- awklib/eg/prog/cut.awk | 3 +-- awklib/eg/prog/egrep.awk | 3 +-- awklib/eg/prog/id.awk | 3 +-- awklib/eg/prog/labels.awk | 3 +-- awklib/eg/prog/tee.awk | 6 ++---- awklib/eg/prog/uniq.awk | 3 +-- 8 files changed, 9 insertions(+), 18 deletions(-) (limited to 'awklib/eg') diff --git a/awklib/eg/lib/groupawk.in b/awklib/eg/lib/groupawk.in index 0917b923..9382bce8 100644 --- a/awklib/eg/lib/groupawk.in +++ b/awklib/eg/lib/groupawk.in @@ -5,8 +5,7 @@ # Revised October 2000 # Revised December 2010 -BEGIN \ -{ +BEGIN { # Change to suit your system _gr_awklib = "/usr/local/libexec/awk/" } diff --git a/awklib/eg/prog/alarm.awk b/awklib/eg/prog/alarm.awk index 63cf64a4..59630ea8 100644 --- a/awklib/eg/prog/alarm.awk +++ b/awklib/eg/prog/alarm.awk @@ -8,8 +8,7 @@ # usage: alarm time [ "message" [ count [ delay ] ] ] -BEGIN \ -{ +BEGIN { # Initial argument sanity checking usage1 = "usage: alarm time ['message' [count [delay]]]" usage2 = sprintf("\t(%s) time ::= hh:mm", ARGV[1]) diff --git a/awklib/eg/prog/cut.awk b/awklib/eg/prog/cut.awk index 04d9bc11..56e35e71 100644 --- a/awklib/eg/prog/cut.awk +++ b/awklib/eg/prog/cut.awk @@ -20,8 +20,7 @@ function usage( e1, e2) print e2 > "/dev/stderr" exit 1 } -BEGIN \ -{ +BEGIN { FS = "\t" # default OFS = FS while ((c = getopt(ARGC, ARGV, "sf:c:d:")) != -1) { diff --git a/awklib/eg/prog/egrep.awk b/awklib/eg/prog/egrep.awk index 86b3cfda..094bdea5 100644 --- a/awklib/eg/prog/egrep.awk +++ b/awklib/eg/prog/egrep.awk @@ -88,8 +88,7 @@ function endfile(file) print } } -END \ -{ +END { exit (total == 0) } function usage( e) diff --git a/awklib/eg/prog/id.awk b/awklib/eg/prog/id.awk index cf744447..992fa57c 100644 --- a/awklib/eg/prog/id.awk +++ b/awklib/eg/prog/id.awk @@ -11,8 +11,7 @@ # uid=12(foo) euid=34(bar) gid=3(baz) \ # egid=5(blat) groups=9(nine),2(two),1(one) -BEGIN \ -{ +BEGIN { uid = PROCINFO["uid"] euid = PROCINFO["euid"] gid = PROCINFO["gid"] diff --git a/awklib/eg/prog/labels.awk b/awklib/eg/prog/labels.awk index abf53c3b..3195809b 100644 --- a/awklib/eg/prog/labels.awk +++ b/awklib/eg/prog/labels.awk @@ -48,7 +48,6 @@ function printpage( i, j) Count++ } -END \ -{ +END { printpage() } diff --git a/awklib/eg/prog/tee.awk b/awklib/eg/prog/tee.awk index 639b9f80..fd9985f1 100644 --- a/awklib/eg/prog/tee.awk +++ b/awklib/eg/prog/tee.awk @@ -7,8 +7,7 @@ # May 1993 # Revised December 1995 -BEGIN \ -{ +BEGIN { for (i = 1; i < ARGC; i++) copy[i] = ARGV[i] @@ -35,8 +34,7 @@ BEGIN \ print > copy[i] print } -END \ -{ +END { for (i in copy) close(copy[i]) } diff --git a/awklib/eg/prog/uniq.awk b/awklib/eg/prog/uniq.awk index 990387ac..effc8f6c 100644 --- a/awklib/eg/prog/uniq.awk +++ b/awklib/eg/prog/uniq.awk @@ -18,8 +18,7 @@ function usage( e) # -n skip n fields # +n skip n characters, skip fields first -BEGIN \ -{ +BEGIN { count = 1 outputfile = "/dev/stdout" opts = "udc0:1:2:3:4:5:6:7:8:9:" -- cgit v1.2.3 From 6c541fd0f75cd328dd80afec757ecccc833719af Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Fri, 29 Aug 2014 13:11:45 +0300 Subject: More doc updates. --- awklib/eg/lib/getopt.awk | 3 +-- awklib/eg/lib/strtonum.awk | 7 +++++-- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'awklib/eg') diff --git a/awklib/eg/lib/getopt.awk b/awklib/eg/lib/getopt.awk index db957ceb..6b1f4c50 100644 --- a/awklib/eg/lib/getopt.awk +++ b/awklib/eg/lib/getopt.awk @@ -38,8 +38,7 @@ function getopt(argc, argv, options, thisopt, i) i = index(options, thisopt) if (i == 0) { if (Opterr) - printf("%c -- invalid option\n", - thisopt) > "/dev/stderr" + printf("%c -- invalid option\n", thisopt) > "/dev/stderr" if (_opti >= length(argv[Optind])) { Optind++ _opti = 0 diff --git a/awklib/eg/lib/strtonum.awk b/awklib/eg/lib/strtonum.awk index 9342e789..5e20626b 100644 --- a/awklib/eg/lib/strtonum.awk +++ b/awklib/eg/lib/strtonum.awk @@ -13,8 +13,9 @@ function mystrtonum(str, ret, n, i, k, c) ret = 0 for (i = 1; i <= n; i++) { c = substr(str, i, 1) - if ((k = index("01234567", c)) > 0) - k-- # adjust for 1-basing in awk + # index() returns 0 if c not in string, + # includes c == "0" + k = index("1234567", c) ret = ret * 8 + k } @@ -26,6 +27,8 @@ function mystrtonum(str, ret, n, i, k, c) for (i = 1; i <= n; i++) { c = substr(str, i, 1) c = tolower(c) + # index() returns 0 if c not in string, + # includes c == "0" k = index("123456789abcdef", c) ret = ret * 16 + k -- cgit v1.2.3 From a0d7edfff1b489e50ae8751429ebf925948b746f Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Thu, 4 Sep 2014 08:49:02 +0300 Subject: Documentation fixes and improvements. --- awklib/eg/prog/uniq.awk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'awklib/eg') diff --git a/awklib/eg/prog/uniq.awk b/awklib/eg/prog/uniq.awk index effc8f6c..2a2cf63e 100644 --- a/awklib/eg/prog/uniq.awk +++ b/awklib/eg/prog/uniq.awk @@ -30,7 +30,7 @@ BEGIN { else if (c == "c") do_count++ else if (index("0123456789", c) != 0) { - # getopt requires args to options + # getopt() requires args to options # this messes us up for things like -5 if (Optarg ~ /^[[:digit:]]+$/) fcount = (c Optarg) + 0 -- cgit v1.2.3 From 8a1df492afae19d544fa5e5b636ed427b2d1c3f5 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sun, 14 Sep 2014 19:28:50 +0300 Subject: Doc edits. --- awklib/eg/lib/strtonum.awk | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'awklib/eg') diff --git a/awklib/eg/lib/strtonum.awk b/awklib/eg/lib/strtonum.awk index 5e20626b..f82c89c5 100644 --- a/awklib/eg/lib/strtonum.awk +++ b/awklib/eg/lib/strtonum.awk @@ -13,8 +13,8 @@ function mystrtonum(str, ret, n, i, k, c) ret = 0 for (i = 1; i <= n; i++) { c = substr(str, i, 1) - # index() returns 0 if c not in string, - # includes c == "0" + # index() returns 0 if c not in string, + # includes c == "0" k = index("1234567", c) ret = ret * 8 + k @@ -27,8 +27,8 @@ function mystrtonum(str, ret, n, i, k, c) for (i = 1; i <= n; i++) { c = substr(str, i, 1) c = tolower(c) - # index() returns 0 if c not in string, - # includes c == "0" + # index() returns 0 if c not in string, + # includes c == "0" k = index("123456789abcdef", c) ret = ret * 16 + k -- cgit v1.2.3