util  optab.awk at [4cecd72436]

File kpw/optab.awk artifact 7d6b5c7a46 part of check-in 4cecd72436


BEGIN {
	end = ""
	if (emit == "enum") {
		print "enum args {"
	} else if (emit == "argtbl") {
		print "struct tblrow argtbl[] = {"
	} else if (emit == "olong") {
		print "#define kpw_emit_long_option_switch \\"
		end = "\\"
	} else if (emit == "oshort") {
		print "#define kpw_emit_short_option_switch \\"
		end = "\\"
	} else if (emit == "usage") {
		print "const char kpw_usage[] ="
	}
	globalc = 0
}
function cify(str) {
	gsub(/[- ]/, "_", str);
	return str;
}
function say(line) {
	if (NF == 5) {
		print "\tkpw_only_" $5 "(" line ") " end
		globals[globalc] = $5
		++ globalc
	} else {
		print "\t" line " " end
	}
}

{ optstr = optstr $1 }
emit == "enum"   { say("arg_" cify($2) ",") }
emit == "argtbl" { say("{ arg_" cify($2)", \"" $2 "\" },") }
emit == "olong"  { say("case arg_" cify($2) ": " $3 "; break;") }
emit == "oshort" { say("case '" $1 "': " $3 "; break;") }
emit == "usage"  { say("\"\\t-"$1", --"$2": "$4"\\n\"") }

emit == "cond" {
	if (NF == 5 && !($5 in condlist)) {
		condlist[$5] = 1
		print "#ifdef " $5
		print "#	define kpw_only_" $5 "(x...) x"
		print "#else"
		print "#	define kpw_only_" $5 "(...)"
		print "#endif"
	}
}

END {
	if (emit == "olong" || emit == "oshort") {
		print ""
	} else if (emit == "usage") {
		print ";"
		print "const char kpw_optstr[] = \" [-" optstr "] [args]\\n\";"
	} else if (emit != "cond") {
		print "};"
	}
}