Install the trap for removing $cleanfile only when the value of
[bpt/guile.git] / libguile / guile-snarf.in
1 #!/bin/sh
2 # Extract the initialization actions from source files.
3 #
4 # Copyright (C) 1996, 97, 98, 99, 2000, 2001, 2002 Free Software Foundation, Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this software; see the file COPYING. If not, write to
18 # the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 # Boston, MA 02111-1307 USA
20
21 # Commentary:
22
23 # Usage: guile-snarf [-d | -D] [-o OUTFILE] INFILE [CPP-OPTIONS ...]
24 #
25 # Process INFILE using the C pre-processor and some other programs.
26 # Write output to a file named OUTFILE or to the standard output when no
27 # OUTFILE has been specified or when OUTFILE is "-". When writing
28 # to a file, ignore lines from the input matching the following grep(1)
29 # regular expression:
30 #
31 # ^#include ".*OUTFILE"
32 #
33 # If there are errors during processing, delete OUTFILE and exit with
34 # non-zero status.
35 #
36 # During snarfing, the pre-processor macro SCM_MAGIC_SNARFER is
37 # defined.
38 #
39 # Optional arg "-d" means grep INFILE for deprecated macros and
40 # issue a warning if any are found. Alternatively, "-D" means
41 # do the same thing but signal error and exit w/ non-zero status.
42 #
43 # If env var CPP is set, use its value instead of the C pre-processor
44 # determined at Guile configure-time: "@CPP@".
45
46 # Code:
47
48 ## config
49
50 deprecated_list="
51 SCM_CONST_LONG
52 SCM_VCELL
53 SCM_VCELL_INIT
54 SCM_GLOBAL_VCELL
55 SCM_GLOBAL_VCELL_INIT
56 "
57
58 ## funcs
59
60 modern_snarf () # writes stdout
61 {
62 ## Apparently, AIX's preprocessor is unhappy if you try to #include an
63 ## empty file.
64 echo "/* source: $infile */" ;
65 echo "/* cpp arguments: $@ */" ;
66 ${cpp} -DSCM_MAGIC_SNARF_INITS -DSCM_MAGIC_SNARFER "$@" > ${temp} && cpp_ok_p=true
67 grep "^ *\^ *\^" ${temp} | sed -e "s/^ *\^ *\^//" -e "s/\^\ *:\ *\^.*/;/"
68 }
69
70 grep_deprecated () # $1 is the filename
71 {
72 regexp="(^greetings!spooks!hows!life)"
73 for dep in `echo $deprecated_list` ; do
74 regexp="(^${dep}[^_A-Z])|${regexp}"
75 done
76 egrep -n ${regexp} $1 /dev/null > ${temp}
77 if [ -s ${temp} ] ; then
78 if $grep_dep_exit_p ; then hey=ERROR ; else hey=WARNING ; fi
79 echo $0: $hey: deprecated macros found:
80 sed -e 's/.clean.c:/:/g' ${temp}
81 $grep_dep_exit_p && exit 1
82 fi
83 }
84
85 ## main
86
87 # process command line
88 if [ x"$1" = x--help ] ; then
89 @AWK@ '/^#.Commentary:/,/^#.Code:/' $0 | grep -v Code: \
90 | sed -e 1,2d -e 's/^. *//g'
91 exit 0
92 fi
93 case x"$1" in x-d) grep_dep_p=true ; grep_dep_exit_p=false ; shift ;;
94 x-D) grep_dep_p=true ; grep_dep_exit_p=true ; shift ;;
95 *) grep_dep_p=false ;;
96 esac
97 if [ x"$1" = x-o ]
98 then outfile=$2 ; shift ; shift ; infile=$1 ; shift
99 else outfile="-"; infile=$1 ; shift
100 fi
101
102 [ x"$infile" = x ] && { echo $0: No input file ; exit 1 ; }
103 [ ! -f "$infile" ] && { echo $0: No such file: $infile ; exit 1 ; }
104
105 # set vars and handler -- handle CPP override
106 cpp_ok_p=false
107 temp="/tmp/snarf.$$"
108 if [ x"$CPP" = x ] ; then cpp="@CPP@" ; else cpp="$CPP" ; fi
109
110 trap "rm -f $temp" 0 1 2 15
111
112 if [ ! "$outfile" = "-" ]; then
113 self_blind_regexp='^#include ".*'`basename $outfile`'"'
114 clean_infile=$infile.clean.c # temp file in same dir as infile
115 # so that #include "foo" works
116 # (e.g., see libguile/eval.c).
117 # use .c to satisfy cpp heuristics.
118
119 # clean input file
120 trap "rm -f $cleanfile" 0 1 2 15
121 grep -v "$self_blind_regexp" $infile > $clean_infile
122 modern_snarf "$@" $clean_infile > $outfile
123 else
124 modern_snarf "$@" $infile
125 fi
126
127 # grep deprecated
128 $grep_dep_p && grep_deprecated $infile
129
130 # zonk outfile if errors occurred
131 if $cpp_ok_p ; then
132 exit 0
133 else
134 [ ! "$outfile" = "-" ] && rm -f $outfile
135 exit 1
136 fi
137
138 # guile-snarf ends here