summaryrefslogtreecommitdiff
path: root/libguile/guile-snarf.in
blob: 8973a52a8fd8594f7f5db85b89ddf2b9fdfa50ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/sh
# Extract the initialization actions from source files.
#
#  Copyright (C) 1996, 97, 98, 99, 2000, 2001, 2002 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA

# Commentary:

# Usage: guile-snarf [-d | -D] [-o OUTFILE] INFILE [CPP-OPTIONS ...]
#
# Process INFILE using the C pre-processor and some other programs.
# Write output to a file, named OUTFILE if specified, or STEM.x if
# INFILE looks like STEM.c and no OUTFILE is specified.  Ignore
# lines from the input matching grep(1) regular expression:
#
#       ^#include ".*OUTFILE"
#
# If there are errors during processing, delete OUTFILE and exit with
# non-zero status.
#
# Optional arg "-d" means grep INFILE for deprecated macros and
# issue a warning if any are found.  Alternatively, "-D" means
# do the same thing but signal error and exit w/ non-zero status.
#
# If env var CPP is set, use its value instead of the C pre-processor
# determined at Guile configure-time: "@CPP@".

# Code:

## config

deprecated_list="
 SCM_CONST_LONG
 SCM_VCELL
 SCM_VCELL_INIT
 SCM_GLOBAL_VCELL
 SCM_GLOBAL_VCELL_INIT
"

## funcs

modern_snarf ()                         # writes stdout
{
${cpp} -DSCM_MAGIC_SNARF_INITS "$@" > ${temp} && cpp_ok_p=true
grep "^ *\^ *\^" ${temp} | sed -e "s/^ *\^ *\^//" -e "s/\^\ *:\ *\^.*/;/"
}

grep_deprecated ()                      # $1 is the filename
{
regexp="(^greetings!spooks!hows!life)"
for dep in `echo $deprecated_list` ; do
    regexp="(^${dep}[^_A-Z])|${regexp}"
done
egrep -n ${regexp} $1 /dev/null > ${temp}
if [ -s ${temp} ] ; then
    if $grep_dep_exit_p ; then hey=ERROR ; else hey=WARNING ; fi
    echo $0: $hey: deprecated macros found:
    sed -e 's/.clean.c:/:/g' ${temp}
    $grep_dep_exit_p && exit 1
fi
}

## main

# process command line
if [ x"$1" = x--help ] ; then
    @AWK@ '/^#.Commentary:/,/^#.Code:/' $0 | grep -v Code: \
        | sed -e 1,2d -e 's/^. *//g'
    exit 0
fi
case x"$1" in x-d) grep_dep_p=true ; grep_dep_exit_p=false ; shift ;;
              x-D) grep_dep_p=true ; grep_dep_exit_p=true  ; shift ;;
              *)   grep_dep_p=false                                ;;
esac
if [ x"$1" = x-o ]
    then outfile=$2 ; shift ; shift ; infile=$1 ; shift
    else infile=$1 ; shift ; outfile=`basename $infile .c`.x
fi

[ x"$infile" = x ] && { echo $0: No input file ; exit 1 ; }
[ ! -f "$infile" ] && { echo $0: No such file: $infile ; exit 1 ; }

# set vars and handler -- handle CPP override
cpp_ok_p=false
temp="/tmp/snarf.$$"
if [ x"$CPP" = x ] ; then cpp="@CPP@" ; else cpp="$CPP" ; fi
self_blind_regexp='^#include ".*'`basename $outfile`'"'
clean_infile=$infile.clean.c            # temp file in same dir as infile
                                        # so that #include "foo" works
                                        # (e.g., see libguile/eval.c).
                                        # use .c to satisfy cpp heuristics.
trap "rm -f $temp $clean_infile" 0 1 2 15

# clean input file
grep -v "$self_blind_regexp" $infile > $clean_infile

# grep deprecated
$grep_dep_p && grep_deprecated $clean_infile

# do the snarfing -- output something extra for needy cpp programs (AIX)
{ echo "/* source: $infile */" ;
  echo "/* cpp-options: $@ */" ;
  modern_snarf "$@" $clean_infile ;
} > $outfile

# zonk outfile if errors occurred
if $cpp_ok_p ; then
    exit 0
else
    rm -f $outfile
    exit 1
fi

# guile-snarf ends here