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 |
---|
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="makeheader.$$" |
---|
64 | final_outfile="$2" |
---|
65 | |
---|
66 | # get basename of $infile |
---|
67 | if echo "$infile" | grep '.*/' > /dev/null 2> /dev/null; then |
---|
68 | include=`echo "$infile" | sed 's/\/[^\/]*$//'` |
---|
69 | includes="$includes$include:" |
---|
70 | fi |
---|
71 | |
---|
72 | if test ! -r "$infile"; then |
---|
73 | echo "makeheader: Cannot open template file $infile for reading" 1>&2 |
---|
74 | exit 1 |
---|
75 | fi |
---|
76 | |
---|
77 | # try to create $outfile if it does not exist |
---|
78 | if test -w "$outfile" || cp /dev/null "$outfile" > /dev/null 2> /dev/null; then |
---|
79 | : |
---|
80 | else |
---|
81 | echo "makeheader: Cannot open header file $outfile for writing" 1>&2 |
---|
82 | exit 1 |
---|
83 | fi |
---|
84 | |
---|
85 | echo "/* $final_outfile automatically generated by makeheader from $infile */" > "$outfile" |
---|
86 | |
---|
87 | # scan through template |
---|
88 | while 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 | |
---|
145 | done < "$infile" >> "$outfile" |
---|
146 | mv "$outfile" "$final_outfile" |
---|