Install the trap for removing $cleanfile only when the value of
[bpt/guile.git] / libguile / guile-snarf.in
CommitLineData
adb75a41 1#!/bin/sh
9bc6fb0a
TTN
2# Extract the initialization actions from source files.
3#
4# Copyright (C) 1996, 97, 98, 99, 2000, 2001, 2002 Free Software Foundation, Inc.
d6a35f3f 5#
d6a35f3f
MV
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.
9bc6fb0a 10#
d6a35f3f
MV
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.
9bc6fb0a 15#
d6a35f3f
MV
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
adb75a41 20
9bc6fb0a
TTN
21# Commentary:
22
77b26c93 23# Usage: guile-snarf [-d | -D] [-o OUTFILE] INFILE [CPP-OPTIONS ...]
9bc6fb0a
TTN
24#
25# Process INFILE using the C pre-processor and some other programs.
66c4b109
MV
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:
9bc6fb0a
TTN
30#
31# ^#include ".*OUTFILE"
32#
33# If there are errors during processing, delete OUTFILE and exit with
34# non-zero status.
35#
66c4b109
MV
36# During snarfing, the pre-processor macro SCM_MAGIC_SNARFER is
37# defined.
38#
77b26c93
TTN
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.
9bc6fb0a
TTN
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
77b26c93
TTN
48## config
49
50deprecated_list="
51 SCM_CONST_LONG
52 SCM_VCELL
53 SCM_VCELL_INIT
54 SCM_GLOBAL_VCELL
55 SCM_GLOBAL_VCELL_INIT
56"
57
9bc6fb0a
TTN
58## funcs
59
60modern_snarf () # writes stdout
61{
66c4b109
MV
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/\^\ *:\ *\^.*/;/"
9bc6fb0a
TTN
68}
69
77b26c93 70grep_deprecated () # $1 is the filename
9bc6fb0a 71{
77b26c93
TTN
72regexp="(^greetings!spooks!hows!life)"
73for dep in `echo $deprecated_list` ; do
74 regexp="(^${dep}[^_A-Z])|${regexp}"
75done
77b26c93
TTN
76egrep -n ${regexp} $1 /dev/null > ${temp}
77if [ -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
82fi
9bc6fb0a
TTN
83}
84
85## main
86
87# process command line
88if [ x"$1" = x--help ] ; then
89 @AWK@ '/^#.Commentary:/,/^#.Code:/' $0 | grep -v Code: \
90 | sed -e 1,2d -e 's/^. *//g'
91 exit 0
92fi
77b26c93
TTN
93case 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 ;;
96esac
9bc6fb0a
TTN
97if [ x"$1" = x-o ]
98 then outfile=$2 ; shift ; shift ; infile=$1 ; shift
66c4b109 99 else outfile="-"; infile=$1 ; shift
9bc6fb0a
TTN
100fi
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
106cpp_ok_p=false
7677589a 107temp="/tmp/snarf.$$"
9bc6fb0a 108if [ x"$CPP" = x ] ; then cpp="@CPP@" ; else cpp="$CPP" ; fi
66c4b109 109
3be0d96d 110trap "rm -f $temp" 0 1 2 15
66c4b109
MV
111
112if [ ! "$outfile" = "-" ]; then
113 self_blind_regexp='^#include ".*'`basename $outfile`'"'
114 clean_infile=$infile.clean.c # temp file in same dir as infile
9bc6fb0a
TTN
115 # so that #include "foo" works
116 # (e.g., see libguile/eval.c).
117 # use .c to satisfy cpp heuristics.
9bc6fb0a 118
66c4b109 119 # clean input file
3be0d96d 120 trap "rm -f $cleanfile" 0 1 2 15
66c4b109
MV
121 grep -v "$self_blind_regexp" $infile > $clean_infile
122 modern_snarf "$@" $clean_infile > $outfile
123else
124 modern_snarf "$@" $infile
125fi
77b26c93
TTN
126
127# grep deprecated
66c4b109 128$grep_dep_p && grep_deprecated $infile
7677589a 129
9bc6fb0a
TTN
130# zonk outfile if errors occurred
131if $cpp_ok_p ; then
132 exit 0
133else
66c4b109 134 [ ! "$outfile" = "-" ] && rm -f $outfile
9bc6fb0a
TTN
135 exit 1
136fi
63a646c5 137
9bc6fb0a 138# guile-snarf ends here