#!/usr/local/plan9/bin/rc rfork e fn usage { echo 'usage: noifdefs [-DFOO]... [file]...' >[1=2] exit usage } defs=() ifs=' ' while(! ~ $#* 0 && ~ $1 -* && ! ~ $1 --){ switch($1){ case -D if(~ $#* 1) usage defs=($defs $2) shift shift case -D* x=`{echo $1 | sed s/-D//} defs=($defs $x) shift case * usage } } if(~ $1 --) shift { for(i in $defs) echo '#define '^$i echo '#show' cat $* } | awk ' BEGIN{ show = 0 skipping = 0 depth = 0 } { prag = "" } /^#[ ]+/ { prag = $2; arg = $3 } /^#[a-z]+/ { prag = substr($1, 2); arg = $2 } prag=="if" { isif[depth] = 1; sat[depth++] = skipping; } prag=="ifdef" { isif[depth] = 0; sat[depth++] = skipping; if(!defs[arg]) skipping = 1; next } prag=="ifndef" { isif[depth] = 0; sat[depth++] = skipping; if(defs[arg]) skipping = 1; next } prag=="else" && !isif[depth-1] { if(!sat[depth-1]) skipping = !skipping; next } prag=="endif" { skipping = sat[--depth]; if(!isif[depth]) next } prag=="define" { defs[arg] = 1 } prag=="undef" { delete defs[arg] } show==0 && /^#show/ { show = 1; next } !show || skipping { next } { print } '