source: git/omalloc/makeheader @ d011a2

spielwiese
Last change on this file since d011a2 was fd1c20e, checked in by Hans Schönemann <hannes@…>, 18 years ago
*hannes: fix git-svn-id: file:///usr/local/Singular/svn/trunk@9087 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100755
File size: 3.5 KB
Line 
1#! /bin/sh
2# $Id: makeheader,v 1.2 2006-05-02 13:27:49 Singular Exp $
3
4#
5# makeheader.sh - generate a header file out of several header files.
6#
7# Type 'makeheader -?' for instructions on syntax/usage.
8#
9
10# print usage if necessary
11if test "$#" = 0 || test "$1" = "-?" || test "$1" = "-help"; then
12    cat 1>&2 << \EOT
13usage: makeheader [-?] {-I<includedir>} <templatefile> <outfile>
14
15This is makeheader, a header file generation tool.
16With makeheader it is possible to generate a single header
17file out of several other header files.
18
19To include a header file into the generated header file
20(= <outfile>) use the two lines
21
22/*MAKEHEADER*/
23#include "<includefile>"
24
25in <templatefile>.
26
27If you use the alternate form
28
29/*MAKEHEADER PUBLIC ONLY*/
30#include "<includefile>"
31
32only sections marked with /*BEGINPUBLIC*/ and /*ENDPUBLIC*/ are
33pasted from <includefile> into <outfile>.
34
35<includefile> is looked up in all <includedir>s, then in the
36current directory, then in the base directory of <templatefile>.
37EOT
38    exit 1
39fi
40
41# get list of include directories
42includes=""
43while echo "$1" | grep '^-I' > /dev/null 2> /dev/null; do
44    # get argument to option -I
45    if test "$1" = "-I"; then
46        shift
47        includes="$includes$1:"
48    else
49        include=`echo "$1" | sed 's/^-I//'`
50        includes="$includes$include:"
51    fi
52    shift
53done
54includes="$includes.:"
55
56# check for rest of arguments
57if test "$#" -lt 2; then
58    echo "usage: makeheader [-?] {-I<includedir>} <templatefile> <outfile>" 1>&2
59    exit 1
60fi
61
62infile="$1"
63outfile="$2"
64
65# get basename of $infile
66if echo "$infile" | grep '.*/' > /dev/null 2> /dev/null; then
67    include=`echo "$infile" | sed 's/\/[^\/]*$//'`
68    includes="$includes$include:"
69fi
70
71if test ! -r "$infile"; then
72    echo "makeheader: Cannot open template file $infile for reading" 1>&2
73    exit 1
74fi
75   
76# try to create $outfile if it does not exist
77if test -w "$outfile" || cp /dev/null "$outfile" > /dev/null 2> /dev/null; then
78    :
79else
80    echo "makeheader: Cannot open header file $outfile for writing" 1>&2
81    exit 1
82fi
83
84echo "/* $outfile automatically generated by makeheader from $infile */" > "$outfile"
85echo "#ifndef OMALLOC_H" >> "$outfile"
86echo "#define OMALLOC_H" >> "$outfile"
87echo "#endif" >> "$outfile"
88
89# scan through template
90while read line; do
91    if test "$line" = '/*MAKEHEADER*/'; then
92        readmode=1
93    elif test "$line" = '/*MAKEHEADER PUBLIC ONLY*/'; then
94        readmode=2
95    else
96        echo "$line"
97        continue
98    fi
99
100    # get include file from next line
101    read line
102    if echo "$line" | grep -v '^#include ".*"$' > /dev/null 2> /dev/null; then
103        echo "makeheader: Invalid include statement $line" 1>&2
104        exit 1
105    fi
106    includefile=`echo "$line" | sed 's/^#include "//; s/"$//'`
107
108    # search for includefile
109    found=0
110    saveIFS="$IFS"; IFS=:
111    for dir in $includes; do
112        if test -r "$dir/$includefile"; then
113            found=1
114            break
115        fi
116    done
117    IFS="$saveIFS"
118
119    if test $found = 0; then
120        echo "makeheader: Include file $includefile not found" 1>&2
121        exit 1
122    else
123        includefile="$dir/$includefile"
124    fi
125
126    # now paste includefile into outfile
127    echo "/* stuff included from $includefile */"
128    echo
129    if test $readmode = 1; then
130        # read whole includefile
131        cat "$includefile"
132    elif test $readmode = 2; then
133        # read only public sections of includefile
134        sed -n '
135          \@^/\*BEGINPUBLIC\*/$@!d
136          \@^/\*BEGINPUBLIC\*/$@{
137            : loop
138            n
139            \@^/\*ENDPUBLIC\*/$@d
140            p
141            b loop
142          }' "$includefile"
143    fi
144
145done < "$infile" >> "$outfile"
Note: See TracBrowser for help on using the repository browser.