#!/bin/sh # Stand-in C preprocessor for rpcgen(1) on an image that ships no compiler. # rpcgen forks `cpp -C -D ` for every pass; this # covers exactly the directives the rpcsvc .x files use (#ifdef / #endif, no # nesting, no #else, no #include, no macros) and refuses anything else so an # unhandled directive is a visible failure and never a silently wrong header. def= file= for a in "$@"; do case "$a" in -C) ;; -D*) def=${a#-D} ;; -*) ;; *) file=$a ;; esac done if [ -z "$file" ]; then file=/dev/stdin fi awk -v def="$def" ' /^[ \t]*#[ \t]*ifdef[ \t]/ { n = $0 sub(/^[ \t]*#[ \t]*ifdef[ \t]+/, "", n) sub(/[ \t].*$/, "", n) if (depth > 0) { print "rpcsvc-cpp: nested #ifdef unsupported" > "/dev/stderr"; exit 1 } depth = 1 skip = (n != def) next } /^[ \t]*#[ \t]*endif/ { if (depth == 0) { print "rpcsvc-cpp: unmatched #endif" > "/dev/stderr"; exit 1 } depth = 0 skip = 0 next } /^[ \t]*#/ { print "rpcsvc-cpp: unsupported directive: " $0 > "/dev/stderr" exit 1 } { if (!skip) print } END { if (depth != 0) { print "rpcsvc-cpp: unterminated #ifdef" > "/dev/stderr"; exit 1 } } ' "$file"