source: git/factory/bin/makeheader @ 6deedd

fieker-DuValspielwiese
Last change on this file since 6deedd was b1dfaf, checked in by Frank Seelisch <seelisch@…>, 14 years ago
patch from Kai (checked for problems under Windows OS: no problems) git-svn-id: file:///usr/local/Singular/svn/trunk@13210 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100755
File size: 3.5 KB
Line 
1#! /bin/sh
2# $Id$
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="makeheader.$$"
64final_outfile="$2"
65
66# get basename of $infile
67if echo "$infile" | grep '.*/' > /dev/null 2> /dev/null; then
68    include=`echo "$infile" | sed 's/\/[^\/]*$//'`
69    includes="$includes$include:"
70fi
71
72if test ! -r "$infile"; then
73    echo "makeheader: Cannot open template file $infile for reading" 1>&2
74    exit 1
75fi
76   
77# try to create $outfile if it does not exist
78if test -w "$outfile" || cp /dev/null "$outfile" > /dev/null 2> /dev/null; then
79    :
80else
81    echo "makeheader: Cannot open header file $outfile for writing" 1>&2
82    exit 1
83fi
84
85echo "/* $final_outfile automatically generated by makeheader from $infile */" > "$outfile"
86
87# scan through template
88while read line; do
89    if test "$line" = '/*MAKEHEADER*/'; then
90        readmode=1
91    elif test "$line" = '/*MAKEHEADER PUBLIC ONLY*/'; then
92        readmode=2
93    else
94        echo "$line"
95        continue
96    fi
97
98    # get include file from next line
99    read line
100#    if echo "$line" | grep -v '^#include <factory/.*>$' > /dev/null 2> /dev/null; then
101    if echo "$line" | grep -v '^#include ".*"$' > /dev/null 2> /dev/null; then
102        echo "makeheader: Invalid include statement $line" 1>&2
103        exit 1
104    fi
105    includefile=`echo "$line" | sed 's/^#include "//; s/"$//'`
106#    includefile=`echo "$line" | sed 's/^#include <factory//; 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"
146mv "$outfile" "$final_outfile"
Note: See TracBrowser for help on using the repository browser.