#!/bin/gawk -f # # Copyright (C) 2009 Karel Zak # Distributed under GPL2+. # BEGIN { MATCH[0] = ""; MATCHHI[0] = "" read_setting(ENVIRON["HOME"] "/.comakerc") makecmd = "make" for (i = 1; i < ARGC; i++) makecmd = makecmd " " ARGV[i] makecmd = makecmd " 2>&1" while ((makecmd |& getline xline) > 0) { do_counters(xline) do_highlighting(xline) } close(makecmd) if (length(ct_results) > 0) { print MODE[HI["toolname"]] "-------" MODE["reset"] for (name in ct_results) { print MODE[HI["toolname"]] "comake: " \ MODE[HI["counter"]] name " " \ ct_results[name] MODE["reset"] } } exit 0 } function dbg(mesg) { if (SET["debug"] == "on") print ">>>comake>>> " mesg } function stripoff(prefix, str) { return gensub(/"$[[:space:]]*/, "", 1, gensub(prefix, "", 1, str)) } function read_setting(filename) { dbg("reading: " filename) m = length(MATCH) - 1 h = length(MATCHHI) - 1 while ((getline ln < filename) > 0) { if (ln ~ /^Set/) { if (split(ln, items, "[[:space:]]+") == 3) SET[items[2]] = items[3] } if (ln ~ /^MatchHi/) { MATCHHI[h++] = stripoff("^MatchHi[[:space:]]+\"", ln) } else if (ln ~ /^Match/) { MATCH[m++] = stripoff("^Match[[:space:]]+\"", ln) } else if (ln ~ /^Hi/) { if (split(ln, items, "[[:space:]]+") == 3) HI[items[2]] = items[3] } else if (ln ~ /^Mode/) { match(ln, "^Mode[[:space:]]+([^[:space:]]+)[[:space:]]+(.*)", subs) name = substr(ln, subs[1, "start"], subs[1, "length"]) cmd = substr(ln, subs[2, "start"], subs[2, "length"]) cmd |& getline MODE[name] close(cmd) } else if (ln ~ /^Counter/) { match(ln, "^Counter[[:space:]]+\"(.*)\"[[:space:]]+\"(.*)\"", subs) name = substr(ln, subs[1, "start"], subs[1, "length"]) reg = substr(ln, subs[2, "start"], subs[2, "length"]) COUNTER[name] = reg } } close(filename) } function colorize(str, mode, off, len) { dbg("colorize: off=" off " len=" len) res = "" if (off > 1) res = substr(str, 1, off - 1) res = res mode substr(str, off, len) MODE["reset"] if (length(str) >= off + len) res = res substr(str, off + len) return res } function do_counters(line) { for (name in COUNTER) { regx = COUNTER[name] if (line !~ regx) continue if (name in ct_results) ct_results[name]++ else ct_results[name] = 1 } } function do_highlighting(line) { delete frags nfrags = 0 for (idx in MATCH) { regx = MATCH[idx] if (line !~ regx) continue dbg("gr[" idx "]: " regx) split(MATCHHI[idx], hilist, ":") match(line, regx, subs) dbg("number of sub-expressions: " length(subs)/3 - 1) hin = 1 last_off = 0 last_len = 0 for (m = 1; m <= length(subs)/3 - 1; m++) { off = subs[m, "start"] len = subs[m, "length"] dbg("subs[" m "]:" \ " off=" off \ " len=" len \ " : " subs[m]) if (off >= last_off && off + len <= last_off + last_len) { dbg("sun-subexpresion: IGNORE") continue } last_off = off last_len = len hiname = hilist[hin++] if (hiname == "NONE" || length(hiname) == 0) continue modename = HI[hiname] if (length(modename) == 0) continue if (! modename in MODE) continue frags[nfrags,"mode"] = MODE[modename] frags[nfrags,"off"] = off frags[nfrags,"len"] = len dbg("add frag[" nfrags "]: mode=" modename) nfrags++ } break } for (frag = nfrags - 1; frag >= 0; frag--) line = colorize(line, frags[frag,"mode"], frags[frag,"off"], frags[frag,"len"]) print line }