Fix infinite loop in expander
[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, 2004, 2006, 2008,
5 # 2009, 2014 Free Software Foundation, Inc.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as
9 # published by the Free Software Foundation; either version 3, or (at
10 # your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this software; see the file COPYING.LESSER. If
19 # not, write to the Free Software Foundation, Inc., 51 Franklin
20 # Street, Fifth Floor, Boston, MA 02110-1301 USA
21
22 # Commentary:
23
24 # Usage: guile-snarf [-o OUTFILE] [CPP-ARGS ...]
25
26 # Initialization actions are extracted to OUTFILE or to standard
27 # output when no OUTFILE has been specified or when OUTFILE is "-".
28 # The C preprocessor is called with CPP-ARGS (which usually include a
29 # input file) and the output is filtered for the actions.
30 #
31 # If there are errors during processing, OUTFILE is deleted and the
32 # program exits with non-zero status.
33 #
34 # During snarfing, the pre-processor macro SCM_MAGIC_SNARFER is
35 # defined. You can use this to avoid including snarfer output files
36 # that don't yet exist by writing code like this:
37 #
38 # #ifndef SCM_MAGIC_SNARFER
39 # #include "foo.x"
40 # #endif
41 #
42 # If the environment variable CPP is set, use its value instead of the
43 # C pre-processor determined at Guile configure-time: "@CPP@".
44
45 # Code:
46
47 ## funcs
48
49 modern_snarf () # writes stdout
50 {
51 ## Apparently, AIX's preprocessor is unhappy if you try to #include an
52 ## empty file.
53 echo "/* cpp arguments: $@ */" ;
54 ${cpp} -DSCM_MAGIC_SNARF_INITS -DSCM_MAGIC_SNARFER "$@" > ${temp} && cpp_ok_p=true
55 sed -ne 's/ *\^ *\^ */\
56 /
57 s/.*\n//
58 t x
59 d
60 : x
61 s/ *\^ *: *\^ */;\
62 /
63 t y
64 N
65 s/\n\(#.*\)/ /
66 s/\n/ /
67 t x
68 : y
69 P
70 D' ${temp}
71 }
72
73 ## main
74
75 # process command line
76 if [ x"$1" = x--help ] ; then
77 @AWK@ '/^#.Commentary:/,/^#.Code:/' $0 | grep -v Code: \
78 | sed -e 1,2d -e 's/^. *//g'
79 exit 0
80 fi
81 if [ x"$1" = x-o ]
82 then outfile="$2" ; shift ; shift ;
83 else outfile="-" ;
84 fi
85
86 # set vars and handler -- handle CPP override
87 cpp_ok_p=false
88
89 if [ x"$TMPDIR" = x ]; then TMPDIR="/tmp" ; else : ; fi
90 tempdir="$TMPDIR/guile-snarf.$$"
91 (umask 077 && mkdir $tempdir) || exit 1
92 temp="$tempdir/tmp"
93
94 if [ x"$CPP" = x ] ; then cpp="@CPP@" ; else cpp="$CPP" ; fi
95
96 trap "rm -rf $tempdir" 0 1 2 15
97
98 if [ ! "$outfile" = "-" ] ; then
99 modern_snarf "$@" > $outfile
100 else
101 modern_snarf "$@"
102 fi
103
104 # zonk outfile if errors occurred
105 if $cpp_ok_p ; then
106 exit 0
107 else
108 [ ! "$outfile" = "-" ] && rm -f $outfile
109 exit 1
110 fi
111
112 # guile-snarf ends here