#!/bin/sh # # $Id: ifdef,v 1.5 2006/05/15 18:52:45 vogelke Exp $ # # NAME: # ifdef # # SYNOPSIS: # ifdef [-hv] files # # DESCRIPTION: # This writes something I can include in a program to tell me what # macros were used when compiling. Handy for things like the TCP # wrapper programs, so I can tell if I used -DPARANOID by just # doing "strings /usr/etc/tcpd". # # Assumes you have nawk plus a version of cpp that reads from stdin. # # OPTIONS: # "-h" prints help and exits. # "-v" prints the version and exits. # # AUTHOR: # Karl Vogel # Sumaria Systems, Inc. PATH=/bin:/usr/sbin:/usr/local/bin:/usr/ucb export PATH tmp=/tmp/ifdef.tmp.$$ trap "rm -f $tmp; exit 0" 0 1 2 3 15 tag=`basename $0` # tag for the syslog entries. # ============================= FUNCTIONS ================================== die () { echo "$tag: ERROR -- $*" >& 2 exit 1 } usage () { lines=`egrep -n '^# (NAME|AUTHOR)' $0 | cut -f1 -d:` ( case "$#" in 0) ;; *) echo "usage error: $*"; echo ;; esac case "$lines" in "") ;; *) set `echo $lines | sed -e 's/ /,/'` sed -n ${1}p $0 | sed -e 's/^#//g' | egrep -v AUTHOR: ;; esac ) >& 2 exit 1 } version () { local_sedscr='s/RCSfile: // s/.Date: // s/,v . .Revision: / v/ s/\$//g' local_revno='$RCSfile: ifdef,v $ $Revision: 1.5 $' local_revdate='$Date: 2006/05/15 18:52:45 $' echo "$local_revno $local_revdate" | sed -e "$local_sedscr" } # ============================= MAIN PROGRAM =============================== # # Handle command line arguments. # set -- `getopt hv $*` case "$?" in 0) ;; *) usage "unrecognized option" ;; esac for i in $* do case $i in -h) usage ;; -v) version; exit 0;; --) shift; break;; esac done # # Strip non-preprocessor stuff from each input file. # We want to find lines containing "defined" or "if[n]def", remove # || or && conditions, and ignore crap like "#ifdef lint". # cpp=/usr/ccs/lib/cpp rmjunk=' s/^defined(// s/)$// /^lint$/d /^LIBC_SCCS$/d ' for file in $* do sed -e 's/^#/ #/' $file | $cpp -P | sed -e 's/^ #/#/' | egrep 'ifdef|ifndef|defined\(' | tr -d '!|&' | fmt -1 | grep -v '^#' | sed -e "$rmjunk" >> $tmp done # # Read macro strings from stdin, write lines that look like this to stdout: # # #ifdef something # static char *something_defined = "something is defined"; # #endif # # Break the static char line if it gets too long. # date=`date` sort -u $tmp | nawk 'BEGIN { printf "/* generated by '$tag' on '"$date"' */\n\n" } { var = $1 gsub ("_", "", var) part1 = sprintf ("static char *%s_defined = ", var) part2 = sprintf ("\"%s is defined\";", $1) if (length (part1) + length (part2) > 77) { part1 = sprintf ("static char *%s_defined =\n\t", var) } printf "#ifdef %s\n", $1 printf "%s%s\n", part1, part2 printf "#endif\n\n" }' exit 0