1#!/bin/sh
2
3# Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4#     1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5#     2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
6#     Massachusetts Institute of Technology
7#
8# This file is part of MIT/GNU Scheme.
9#
10# MIT/GNU Scheme is free software; you can redistribute it and/or
11# modify it under the terms of the GNU General Public License as
12# published by the Free Software Foundation; either version 2 of the
13# License, or (at your option) any later version.
14#
15# MIT/GNU Scheme is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18# General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with MIT/GNU Scheme; if not, write to the Free Software
22# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23# 02110-1301, USA.
24
25# Processing to simulate m4 accepting definition arguments.
26
27set -e
28
29if [ $# -le 1 ]; then
30  printf 'Usage: %s m4 <file/definition> ...\n' >&2
31  exit 1
32fi
33
34M4="${1}"
35shift
36
37TMP_FILE="m4.tmp"
38
39clean ()
40{
41  rm -f "${TMP_FILE}"
42}
43
44run_m4 ()
45{
46  ${M4} && clean
47}
48
49trap clean EXIT INT QUIT TERM
50rm -f "${TMP_FILE}"
51touch "${TMP_FILE}"
52
53if [ $# = 0 ]
54then
55  sed -e '/^#/D' | run_m4 | sed -e 's/@/$/g' -e 's/^$//'
56else
57  SEEN_INPUT=0
58  while [ $# != 0 ]; do
59    if [ "${1}" = "-P" ]; then
60      echo "define(${2})" >> "${TMP_FILE}"
61      shift
62    else
63      SEEN_INPUT=1
64      sed -e '/^#/D' < "${1}" >> "${TMP_FILE}"
65    fi
66    shift
67  done
68  if [ ${SEEN_INPUT} -eq 0 ]; then
69    sed -e '/^#/D' >> "${TMP_FILE}"
70  fi
71  run_m4 < "${TMP_FILE}" | sed -e 's/@/$/g' -e 's/^$//'
72fi
73
74# If m4 was successful, run_m4 has deleted the temporary file.  If
75# not, report the failure; exiting will have the effect of running
76# `clean', which will delete the temporary file.
77
78if [ -f "${TMP_FILE}" ]; then
79  exit 1
80fi
81