1 | #! /bin/sh |
---|
2 | # $Id: makeheader,v 1.1 2005-07-28 15:02:28 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 |
---|
11 | if test "$#" = 0 || test "$1" = "-?" || test "$1" = "-help"; then |
---|
12 | cat 1>&2 << \EOT |
---|
13 | usage: makeheader [-?] {-I<includedir>} <templatefile> <outfile> |
---|
14 | |
---|
15 | This is makeheader, a header file generation tool. |
---|
16 | With makeheader it is possible to generate a single header |
---|
17 | file out of several other header files. |
---|
18 | |
---|
19 | To include a header file into the generated header file |
---|
20 | (= <outfile>) use the two lines |
---|
21 | |
---|
22 | /*MAKEHEADER*/ |
---|
23 | #include "<includefile>" |
---|
24 | |
---|
25 | in <templatefile>. |
---|
26 | |
---|
27 | If you use the alternate form |
---|
28 | |
---|
29 | /*MAKEHEADER PUBLIC ONLY*/ |
---|
30 | #include "<includefile>" |
---|
31 | |
---|
32 | only sections marked with /*BEGINPUBLIC*/ and /*ENDPUBLIC*/ are |
---|
33 | pasted from <includefile> into <outfile>. |
---|
34 | |
---|
35 | <includefile> is looked up in all <includedir>s, then in the |
---|
36 | current directory, then in the base directory of <templatefile>. |
---|
37 | EOT |
---|
38 | exit 1 |
---|
39 | fi |
---|
40 | |
---|
41 | # get list of include directories |
---|
42 | includes="" |
---|
43 | while 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 |
---|
53 | done |
---|
54 | includes="$includes.:" |
---|
55 | |
---|
56 | # check for rest of arguments |
---|
57 | if test "$#" -lt 2; then |
---|
58 | echo "usage: makeheader [-?] {-I<includedir>} <templatefile> <outfile>" 1>&2 |
---|
59 | exit 1 |
---|
60 | fi |
---|
61 | |
---|
62 | infile="$1" |
---|
63 | outfile="$2" |
---|
64 | |
---|
65 | # get basename of $infile |
---|
66 | if echo "$infile" | grep '.*/' > /dev/null 2> /dev/null; then |
---|
67 | include=`echo "$infile" | sed 's/\/[^\/]*$//'` |
---|
68 | includes="$includes$include:" |
---|
69 | fi |
---|
70 | |
---|
71 | if test ! -r "$infile"; then |
---|
72 | echo "makeheader: Cannot open template file $infile for reading" 1>&2 |
---|
73 | exit 1 |
---|
74 | fi |
---|
75 | |
---|
76 | # try to create $outfile if it does not exist |
---|
77 | if test -w "$outfile" || cp /dev/null "$outfile" > /dev/null 2> /dev/null; then |
---|
78 | : |
---|
79 | else |
---|
80 | echo "makeheader: Cannot open header file $outfile for writing" 1>&2 |
---|
81 | exit 1 |
---|
82 | fi |
---|
83 | |
---|
84 | echo "/* $outfile automatically generated by makeheader from $infile */" > "$outfile" |
---|
85 | |
---|
86 | # scan through template |
---|
87 | while read line; do |
---|
88 | if test "$line" = '/*MAKEHEADER*/'; then |
---|
89 | readmode=1 |
---|
90 | elif test "$line" = '/*MAKEHEADER PUBLIC ONLY*/'; then |
---|
91 | readmode=2 |
---|
92 | else |
---|
93 | echo "$line" |
---|
94 | continue |
---|
95 | fi |
---|
96 | |
---|
97 | # get include file from next line |
---|
98 | read line |
---|
99 | if echo "$line" | grep -v '^#include ".*"$' > /dev/null 2> /dev/null; then |
---|
100 | echo "makeheader: Invalid include statement $line" 1>&2 |
---|
101 | exit 1 |
---|
102 | fi |
---|
103 | includefile=`echo "$line" | sed 's/^#include "//; s/"$//'` |
---|
104 | |
---|
105 | # search for includefile |
---|
106 | found=0 |
---|
107 | saveIFS="$IFS"; IFS=: |
---|
108 | for dir in $includes; do |
---|
109 | if test -r "$dir/$includefile"; then |
---|
110 | found=1 |
---|
111 | break |
---|
112 | fi |
---|
113 | done |
---|
114 | IFS="$saveIFS" |
---|
115 | |
---|
116 | if test $found = 0; then |
---|
117 | echo "makeheader: Include file $includefile not found" 1>&2 |
---|
118 | exit 1 |
---|
119 | else |
---|
120 | includefile="$dir/$includefile" |
---|
121 | fi |
---|
122 | |
---|
123 | # now paste includefile into outfile |
---|
124 | echo "/* stuff included from $includefile */" |
---|
125 | echo |
---|
126 | if test $readmode = 1; then |
---|
127 | # read whole includefile |
---|
128 | cat "$includefile" |
---|
129 | elif test $readmode = 2; then |
---|
130 | # read only public sections of includefile |
---|
131 | sed -n ' |
---|
132 | \@^/\*BEGINPUBLIC\*/$@!d |
---|
133 | \@^/\*BEGINPUBLIC\*/$@{ |
---|
134 | : loop |
---|
135 | n |
---|
136 | \@^/\*ENDPUBLIC\*/$@d |
---|
137 | p |
---|
138 | b loop |
---|
139 | }' "$includefile" |
---|
140 | fi |
---|
141 | |
---|
142 | done < "$infile" >> "$outfile" |
---|