# sh-include 1.0.3 (1999-Sep-12-Sun) # Adam M. Costello # Source this file from a Bourne shell script to define the include # function. If this file is in PATH, you can just do ". sh-include". # This file should not be executable. # include file.h # # This runs file.h through cpp and assigns the #defined symbols (but not # the macros with parameters) to shell variables of the same names. # # include -later file.h # # Rather than assigning the variables, this outputs Bourne shell code to # assign the variables, which could then be sourced or eval'd. # # All variables internal to this script begin with "__include_" to avoid # conflicts with the caller, and they are unset before it returns. # Configure: __include_cpp="cpp -P" __include_awk=awk # End configuration. include() { __include_tmp=/tmp/include$$ if [ "$1" = -later ]; then shift __include_later=yes else __include_later=no fi __include_hfile=$1 # Extract only the preprocessor directives: $__include_awk '$1 ~ /^#/ { print; while ($0 ~ /\\$/) { if (!(getline)) break; print; } }' $__include_hfile > $__include_tmp.defs # Copy the directives and append code to assign the variables: cp $__include_tmp.defs $__include_tmp.h echo ": 2791024743" >> $__include_tmp.h $__include_awk '{ if ($1 == "#define") name = $2; else if ($1 == "#" && $2 == "define") name = $3; else next; if (name ~ /\(/) next; print "read \"" name "\" << \"2791024743\""; print name; print "2791024743"; }' $__include_tmp.defs >> $__include_tmp.h # Run through cpp to generate the real code, which we either output or # source: if [ $__include_later = yes ]; then $__include_cpp $__include_tmp.h | $__include_awk 'ok; /2791024743/ {ok = 1}' else $__include_cpp $__include_tmp.h > $__include_tmp.sh . $__include_tmp.sh rm $__include_tmp.sh fi # Clean up: rm $__include_tmp.defs $__include_tmp.h unset __include_cpp __include_awk unset __include_tmp __include_later __include_hfile }