summaryrefslogtreecommitdiff
path: root/gnu/lib/libg++/genclass/genclass.sh
blob: 08d510754ad24191ed47da7c3a011c194d385dd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/bin/sh

#   Copyright (C) 1989 Free Software Foundation, Inc.
#   
#   genclass program enhanced by Wendell C. Baker 
#   (original by Doug Lea (dl@rocky.oswego.edu))

#This file is part of GNU libg++.

#GNU libg++ is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 1, or (at your option)
#any later version.

#GNU libg++ is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with GNU libg++; see the file COPYING.  If not, write to
#the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.


#
# genclass -list [proto ...]
# genclass -catalog [proto ...]
# genclass type1 {ref|val} proto [out_prefix]
# genclass -2 type1 {ref|val} type2 {ref, val} proto [out_prefix]
#
# Generate classes from prototypes
#
name=genclass ;
usage="
    $name -list [proto ...]
    $name -catalog [proto ...]
    $name type1 {ref|val} proto [out_prefix]
    $name -2 type1 {ref|val} type2 {ref|val} proto [out_prefix]" ;

case "$1" in
-usage)
    #
    # -usage
    #
    echo "usage: $usage" 1>&2 ;
    exit 0;
    ;;
-version)
    #
    # -version
    #
    # <VERSION> is substituted by the build process.
    # We currently use the libg++ version number (extracted from ../Makefile).
    echo "$name: version <VERSION>" ;
    exit 0;
    ;;
-requires)
    #
    # -requires
    #
    # The following line should contain any nonstandard programs
    # which must be in the users's path (i.e. not referenced by a
    # fullpath);it allows one to check a script for dependencies
    # without exhaustively testing its usages.
    # ... in this case genclass depends on nothing else.
    echo ;
    exit 0;
    ;;
esac ;

# pull it in from the environment
[ "$TRACE" = "" ] || set -xv 

# Search in standard g++ prototype directory and in the current directory
# NOTE: this variable is edited by the install process
PROTODIR=/projects/gnu-cygnus/gnu-cygnus-2/mips/lib/g++-include/gen

pwd=`pwd`

case "$1" in
-catalog*|-list*)
    #
    # genclass -catalog [proto ...]
    # genclass -list [proto ...]
    #
    option="$1" ;
    shift ;

    case $# in
    0)
        #
        # -catalog
        # -list
        #
        select=all ;
        select_pattern=p ;
        ;;
    *)
        #
        # -catalog proto ...
        # -list proto ...
        #
        select="$@" ;
        select_pattern= ;
        for i in $@ ; do
            select_pattern="\
$select_pattern
/.*$i\$/ p
" ;
        done ;

        ;;
    esac ;

    #
    # select_pattern is now a (possibly-vacuous) newline-
    # separated list of patterns of the form:
    #
    #     /.*Proto1$/ p
    #     /.*Proto2$/ p
    #     /.*Proto3$/ p
    #
    # or select_pattern is simply ``p'' to select everything

    # Hmmm... not all systems have a fmt program; should we
    # just go ahead and use ``nroff -Tcrt | cat -s'' here?
    fmt='nroff -Tcrt | cat -s'
    #fmt=fmt ;

    case "$option" in
    -catalog*)
        #
        # -catalog [proto ...]
        #
        echo "\
Catalog of ${name} class templates
directories searched:
    $PROTODIR
    $pwd
selecting: $select
classes available:" ;
        ;;
    -list*)
        #
        # -list [proto ...]
        #
        # no need to do anything (the list is coming out next)
        ;;
    esac ;

# The sed script does the following:
# - If it does not end in a .ccP or .hP then
#   it's not a template and we are not intereseted.
# - Get rid of pathname components [s;.*/;;]
# - Just take the template names
# - change quoting conventions and select off what we want to see
# -if it did not pass the patterns, kill it

    ls $pwd $PROTODIR | sed -e '
/\.ccP$/ !{
   /\.hP$/ !{
     d
   }
}
s;.*/;;
s/\.ccP$//
s/\.hP$//
' -e "$select_pattern
d
" | sort -u | case "$option" in
    -catalog*)
        # The library catalog information preceded the list
        # format the list, and tab in in a bit to make it readable.
        # Re-evaluate $fmt because it might contain a shell command
        eval $fmt | sed -e 's/.*/    &/' ;
        ;;
    -list*)
        # nothing special, just let the sorted list dribble out
        # we must use cat to receive (and reproduce) the incoming list
        cat ;
        ;;
    esac ;
    exit 0;
    ;;
-2)
    #
    # genclass -2 type1 {ref|val} type2 {ref|val} proto [out_prefix]
    #
    N=2 ;

    case $# in
    6) # genclass -2 type1 {ref|val} type2 {ref|val} proto
       ;;
    7) # genclass -2 type1 {ref|val} type2 {ref|val} proto out_prefix
       ;;
    *)
	echo "usage: $usage" 1>&2 ;
	exit 1;
    esac ;
    shift ;
    ;;
*)
    #
    # genclass type1 {ref|val} proto [out_prefix]
    #
    N=1 ;

    case $# in
    3) # genclass type1 {ref|val} proto
       ;;
    4) # genclass type1 {ref|val} proto out_prefix
       ;;
    *)
	echo "usage: $usage" 1>&2 ;
	exit 1;
    esac ;
    ;;
esac

#
# Args are now (the point being the leading ``-2'' is gone)
#
#     type1 {ref|val} proto [out_prefix]
#     type1 {ref|val} type2 {ref|val} proto [out_prefix]
#

#
# Quote all of the $1 $2 etc references to guard against
# dynamic syntax errors due to vacuous arguments (i.e. '')
# as sometimes occurs when genclass is used from a Makefile
#

T1="$1";
T1NAME="$T1." ;
T1SEDNAME="$T1" ;

case "$2" in
ref)
     T1ACC="\&" ;
     ;;
val)
     T1ACC=" " ;
     ;;
*)
    echo "${name}: Must specify type1 access as ref or val" 1>&2 ;
    echo "usage: $usage" 1>&2 ;
    exit 1;
    ;;
esac

# N is either 1 or 2

case $N in
1)
    #
    # type1 {ref|val} proto [out_prefix]
    #
    class="$3" ;

    T2="" ;
    T2ACC="" ;
    ;;
2)
    #
    # type1 {ref|val} type2 {ref|val} proto [out_prefix]
    #
    class="$5" ;

    T2="$3";
    T2NAME="$T2." ;
    T2SEDNAME="$T2" ;

    case "$4" in
    ref)
        T2ACC="\&" ;
        ;;
    val)
        T2ACC=" " ;
        ;;
     *)
        echo "${name}: Must specify type2 access: ref or val" 1>&2 ;
	echo "usage: $usage" 1>&2 ;
	exit 1;;
    esac;
    ;;
esac

defaultprefix="$T1NAME$T2NAME" ;

case $# in
3)  # type1 {ref|val} proto
    replaceprefix="N" ;
    prefix="$defaultprefix" ;
    ;;
5)  # type1 {ref|val} type2 {ref|val} proto
    replaceprefix="N" ;
    prefix="$defaultprefix" ;
    ;;
4)  # type1 {ref|val} proto out_prefix
    prefix="$4" ;
    replaceprefix="Y" ;
    ;;
6)  # type1 {ref|val} type2 {ref|val} proto out_prefix
    prefix="$6" ;
    replaceprefix="Y" ;
    ;;
*)
    echo "${name}: too many arguments" 1>&2 ;
    echo "usage: $usage" 1>&2 ;  
    exit 1;
    ;;
esac ;

src_h=$class.hP
src_cc=$class.ccP
out_h=$prefix$class.h;
out_cc=$prefix$class.cc ;

#
# Note #1: The .h and .cc parts are done separately
#     in case only a .h exists for the prototype
#
# Note #2: Bind the .h and .cc parts to the fullpath
#     directories at the same time to ensure consistency.
#

if [ -f $pwd/$src_h ] ; then
    fullsrc_h=$pwd/$src_h ;
    fullsrc_cc=$pwd/$src_cc ;
elif [ -f $PROTODIR/$src_h ] ; then
    fullsrc_h=$PROTODIR/$src_h ;
    fullsrc_cc=$PROTODIR/$src_cc ;
else
    echo "${name}: there is no prototype for class $class - file $src_h" 1>&2 ;
    $0 -list ;
    exit 1;
fi

CASES="$N$replaceprefix" ;
# CASES is one of { 2Y 2N 1Y 1N }

#
# WATCHOUT - we have no way of checking whether or not
# the proper case type is being used with the prototype.
#
# For example, we have no way of ensuring that any of
# Map variants are specified with the -2 argument set
# Further, we have no way of ensuring that -2 is not
# used with the prototypes which require only one.
#
# The second problem is not serious because it still
# results in correctly-generated C++ code; the first
# problem is serious because it results in C++ code that
# still has ``<C>'' and ``<C&>'' syntax inside it.  Such
# code of course will not compile.
#
# SO THE BEST WE CAN DO - is check for the presence of
# <C> and <C&> AFTER the thing has been generated.
#

case $CASES in
2Y) # Two output substitutions, change the prefix
    sed < $fullsrc_h > $out_h -e "
s/<T>/$T1/g
s/<T&>/$T1$T1ACC/g
s/<C>/$T2/g
s/<C&>/$T2$T2ACC/g
s/$T1SEDNAME\.$T2SEDNAME\./$prefix/g
s/$T1SEDNAME\./$prefix/g
s/$T2SEDNAME\./$prefix/g
" ;
    ;;
2N) # Two output substitutions, use the default prefix
    sed < $fullsrc_h > $out_h -e "
s/<T>/$T1/g
s/<T&>/$T1$T1ACC/g
s/<C>/$T2/g
s/<C&>/$T2$T2ACC/g
" ;
    ;;
1Y) # One output substitution, change the prefix
    sed < $fullsrc_h > $out_h -e "
s/<T>/$T1/g
s/<T&>/$T1$T1ACC/g
s/$T1SEDNAME\./$prefix/g
" ;
    ;;
1N) # One output substitution, use the default prefix
    sed < $fullsrc_h > $out_h -e "
s/<T>/$T1/g
s/<T&>/$T1$T1ACC/g
" ;
    ;;
esac

if egrep '<C&?>' $out_h > /dev/null ; then
    echo "${name}: the $class class requires the -2 syntax for the 2nd type" 1>&2 ;
    echo "usage: $usage" 1>&2 ;
    # the user does not get to see the mistakes (he might try to compile it)
    rm $out_h ;
    exit 1;
fi ;

if [ ! -f $fullsrc_cc ] ; then
    echo "${name}: warning, class has a .h but no .cc file" 1>&2 ;
    exit 0;
fi

case $CASES in
2Y) # Two output substitutions, change the prefix
    sed < $fullsrc_cc > $out_cc -e "
s/<T>/$T1/g
s/<T&>/$T1$T1ACC/g
s/<C>/$T2/g
s/<C&>/$T2$T2ACC/g
s/$T1SEDNAME\.$T2SEDNAME\./$prefix/g
s/$T1SEDNAME\./$prefix/g
s/$T2SEDNAME\./$prefix/g
"
    ;;
2N) # Two output substitutions, use the default prefix
    sed < $fullsrc_cc > $out_cc -e "
s/<T>/$T1/g
s/<T&>/$T1$T1ACC/g
s/<C>/$T2/g
s/<C&>/$T2$T2ACC/g
"
    ;;
1Y) # One output substitution, change the prefix
    sed < $fullsrc_cc > $out_cc -e "
s/<T>/$T1/g
s/<T&>/$T1$T1ACC/g
s/$T1SEDNAME\./$prefix/g
"
    ;;
1N) # One output substitution, use the default prefix
    sed < $fullsrc_cc > $out_cc -e "
s/<T>/$T1/g
s/<T&>/$T1$T1ACC/g
"
    ;;
esac

if egrep '<C&?>' $out_h $out_cc > /dev/null ; then
    echo "${name}: the $class class requires the -2 syntax for the 2nd type" 1>&2 ;
    echo "usage: $usage" 1>&2 ;
    # the user does not get to see the mistakes (he might try to compile it)
    rm $out_h $out_cc ;
    exit 1;
fi ;

exit 0;