#! /usr/bin/awk -f # AUDIT - small auditing program in AWK # Copyright (C) 2007 Matous Jan Fialka # # This program is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation, either version 3 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # more details. # # You should have received a copy of the GNU General Public License along with # this program. If not, see . BEGIN { if (ARGC - 1) for (i = 1; i < ARGC; i++) source(ARGV[i]); else source("-"); exit 0; } function source( f,r,g,a,l,b,i,c,o,p,t,y,s,w) { apush(a, f); while ((getline l < f) > 0) { gsub(/#.*/, "", l); split(l, b); if (b[1]) { if (b[1] ~ /^:.*$/) { if (b[1] ~ /^:r(e(a(d)?)?)?$/) { if (acopy(b, c, 2) > 0) { source(ajoin(c), r, g); f = alast(a); } continue; } if (b[1] ~ /^:w(r(i(t(e)?)?)?)?$/) { if (acopy(b, c, 2) > 0) r["%w"] = ajoin(c); while (w = apopfirst(g)) if (r["%w"]) print w > r["%w"]; else print w; continue; } if (b[1] ~ /^:a(p(p(e(n(d)?)?)?)?)?$/) { if (acopy(b, c, 2) > 0) r["%w"] = ajoin(c); while (w = apopfirst(g)) if (r["%w"]) print w >> r["%w"]; else print w; continue; } if (b[1] ~ /^:c(l(o(s(e)?)?)?)?$/) break; if (b[1] ~ /^:p(r(i(n(t)?)?)?)?$/) { acopy(b, c, 2); s = ajoin(c); gsub(/\\a/, "\a", s); gsub(/\\b/, "\b", s); gsub(/\\f/, "\f", s); gsub(/\\n/, "\n", s); gsub(/\\r/, "\r", s); gsub(/\\t/, "\t", s); gsub(/\\v/, "\v", s); gsub(/\\e/, "\x1b", s); printf "%s\n", s; apush(g, s); continue; } if (b[1] ~ /^:f(l(u(s(h)?)?)?)?$/) { if (acopy(b, c, 2) > 0) delete r[ajoin(c)]; else delete r; continue; } if (b[1] ~ /^:q(u(i(t)?)?)?$/) exit 0; continue; } o = p = y = 0; t = 2; v = ""; if (b[1] ~ /^[+\-*\/\^,%]$/) { o = b[1]; if (b[2] ~ /^[0-9\.]+$/) { p = b[2]; t = 3; } else y = 1; } else if (b[1] ~ /^[0-9\.]+$/) { o = "+"; p = b[1]; } else if (b[1] ~ /^=$/) { o = b[1]; } else { o = "+"; t = y = 1; } if (o) { if (acopy(b, c, t) > 0) v = ajoin(c); if (y) if (v) p = r[v]; if (o == "," && p == 0) p = 2; if (o == "/" && p == 0) { s = "Divizion by zero!"; printf "%s\n", s; apush(g, s); break; } if (v) r[v] = p; if (o == "+") r["%s"] += p; if (o == "-") r["%s"] -= p; if (o == "*") r["%s"] *= p; if (o == "/") r["%s"] /= p; if (o == "^") r["%s"] ^= p; if (o == ",") r["%s"] = sqrti(r["%s"], p); if (o == "%") r["%s"] *= (p / 100); if (o == "=") { s = " ------- --"; printf "%s\n", s; apush(g, s); s = " " sprintf ("%10.2f", r["%s"]) "\t" v "\n"; printf "%s\n", s; apush(g, s); if (v) r[v] = r["%s"]; r["%s"] = 0; continue; } o = (o != "+" ? o : " "); s = o " " sprintf("%10.2f", p) "\t" v; printf "%s\n", s; apush(g, s); } } } close(f); } function alength(a ,i,r) { for (i in a) r++; return r; } function apush(a,v) { a[alength(a)+1] = v; return v; } function apop(a ,i,r) { i = alength(a); r = a[i]; delete a[i]; return r; } function apopfirst(a ,i,r,x) { r = a[1]; i = alength(a); delete a[1]; for (x = 1; x <= i; x++) a[x-1] = a[x]; delete a[i]; return r; } function alast(a ,v) { v = apop(a); apush(a, v); return v; } function acopy(a,b ,f,j,i,k) { f = f ? f : 1; j = j ? j : alength(a); delete b; for (i = f; i <= j; i++) b[++k] = a[i]; return k; } function ajoin(a ,s,i,j,r) { j = alength(a); s = s ? s : " "; for (i = 1; i <= j; i++) if (a[i]) { if (r) r = r s a[i]; else r = a[i]; } return r; } function sqrti(n ,i,j) { i = i ? i : 2; for (i = j; j <= i; j++) n = sqrt(n); return n; }