#!/bin/sh # monolithic.sh getstatics(){ # find all static decls, remove function parens # give each variable its own line, remove superflous chars like '*' # and isolate the variable name itself grep '^static' "$1" | sed 's/(.*//' | tr ',' '\n' | tr -d '*;' | sed 's/static//; s/=.*//; s/^ \+//; s/ \+$//; s/^\(const \)\?\(struct \)\?[^ ]\+ \+//' } main='/tmp/sbase_mono' trap "rm -f $main" EXIT grep '^#inc' *.c | sed 's/^[^:]\+://' | sort | uniq for f in *.c do pre=`echo $f | awk -F. '{print $1}'` # i.e. hello.c -> hello statics=`getstatics $f` sedexpr='' for s in $statics do sedexpr="$sedexpr s/\b$s\b\([^']\)/${pre}_$s\1/g;" done # replace any static variable with hello_$varname also exclude # replacements inside single quotes, for example 't' isn't replaced # with 'ls_t'. Bit of a bodge but short of writing a parser I couldn't # think of anything else sed "$sedexpr s/^main(/main_${pre}(/; /^#inc/d" "$f" # generate the monolithic main() "switch" cat <> $main if(!strcmp("$pre", basename)) return main_$pre(argc, argv); ! done cat - $main <