summaryrefslogtreecommitdiff
path: root/mcon/U/cpp_stuff.U
blob: 3a0715bb0dbfbc996bc401be2c9eda4f11785149 (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
?RCS: $Id$
?RCS:
?RCS: Copyright (c) 1991-1997, 2004-2006, Raphael Manfredi
?RCS:
?RCS: You may redistribute only under the terms of the Artistic License,
?RCS: as specified in the README file that comes with the distribution.
?RCS: You may reuse parts of this distribution only within the terms of
?RCS: that same Artistic License; a copy of which may be found at the root
?RCS: of the source tree for dist 4.0.
?RCS:
?RCS: $Log: cpp_stuff.U,v $
?RCS: Revision 3.0.1.2  1997/02/28  15:30:48  ram
?RCS: patch61: added cute quoting trick for wild stringify support
?RCS:
?RCS: Revision 3.0.1.1  1994/10/29  16:08:24  ram
?RCS: patch36: now uses cppstdin instead of plain cpp for consistency (ADO)
?RCS: patch36: remove temporary files when done
?RCS:
?RCS: Revision 3.0  1993/08/18  12:05:36  ram
?RCS: Baseline for dist 3.0 netwide release.
?RCS:
?MAKE:cpp_stuff cpp_quote: cat contains cppstdin cppflags cppminus rm Warn \
	package
?MAKE:	-pick add $@ %<
?S:cpp_stuff:
?S:	This variable contains an identification of the catenation mechanism
?S:	used by the C preprocessor.
?S:.
?S:cpp_quote:
?S:	This variable is set to either '"' or '' depending on whether the
?S:	pre-processor pre-dates ANSI or not.  It is used in the production of
?S:	the SQuoTe() and EQuoTe() macros, and was introduced to overcome a bug
?S:	in gcc 3.x whereby the pre-processor complained loudly about the
?S:	unterminated strings.
?S:.
?C:CAT2:
?C:	This macro catenates 2 tokens together.
?C:.
?C:CAT3:
?C:	This macro catenates 3 tokens together.
?C:.
?C:CAT4:
?C:	This macro catenates 4 tokens together.
?C:.
?C:CAT5:
?C:	This macro catenates 5 tokens together.
?C:.
?C:STRINGIFY:
?C:	This macro surrounds its token with double quotes.
?C:.
?C:SCAT2:
?C:	This macro catenates 2 tokens together and stringifies the result.
?C:.
?C:SCAT3:
?C:	This macro catenates 3 tokens together and stringifies the result.
?C:.
?C:SCAT4:
?C:	This macro catenates 4 tokens together and stringifies the result.
?C:.
?C:SCAT5:
?C:	This macro catenates 5 tokens together and stringifies the result.
?C:.
?H:?%<:#if $cpp_stuff == 1
?H:?CAT2:#define CAT2(a,b)a/**/b
?H:?CAT3:#define CAT3(a,b,c)a/**/b/**/c
?H:?CAT4:#define CAT4(a,b,c,d)a/**/b/**/c/**/d
?H:?CAT5:#define CAT5(a,b,c,d,e)a/**/b/**/c/**/d/**/e
?H:?STRINGIFY:#define STRINGIFY(a)"a"
?H:?%<:#define SQuoTe(a)${cpp_quote}a
?H:?%<:#define EQuoTe(a)a${cpp_quote}
?H:?SCAT2:#define SCAT2(a,b)EQuoTe(SQuoTe(a)b)
?H:?SCAT3:#define SCAT3(a,b,c)EQuoTe(SQuoTe(a)b/**/c)
?H:?SCAT4:#define SCAT4(a,b,c,d)EQuoTe(SQuoTe(a)b/**/c/**/d)
?H:?SCAT5:#define SCAT5(a,b,c,d,e)EQuoTe(SQuoTe(a)b/**/c/**/d/**/e)
?H:?%<:#endif
?H:?%<:#if $cpp_stuff == 42
?X: The additional level of indirection enables these macros to be
?X: used as arguments to other macros.  See K&R 2nd ed., page 231.
?H:?%<:#define CaTiFy(a,b) a ## b
?H:?%<:#define CAT2(a,b) CaTiFy(a,b)
?H:?CAT3:#define CAT3(a,b,c) CAT2(CaTiFy(a,b),c)
?H:?CAT4:#define CAT4(a,b,c,d) CAT2(CaTiFy(a,b), CaTiFy(c,d))
?H:?CAT5:#define CAT5(a,b,c,d,e) CAT2(CAT2(CaTiFy(a,b), CaTiFy(c,d)), e)
?H:?%<:#define StGiFy(a)# a
?H:?STRINGIFY:#define STRINGIFY(a)StGiFy(a)
?H:?SCAT2:#define SCAT2(a,b)StGiFy(a) StGiFy(b)
?H:?SCAT3:#define SCAT3(a,b,c)StGiFy(a) StGiFy(b) StGiFy(c)
?H:?SCAT4:#define SCAT4(a,b,c,d)StGiFy(a) StGiFy(b) StGiFy(c) StGiFy(d)
?H:?SCAT5:#define SCAT5(a,b,c,d,e)StGiFy(a) StGiFy(b) StGiFy(c) StGiFy(d) StGiFy(e)
?H:?%<:#endif
?H:?%<:#if $cpp_stuff != 1 && $cpp_stuff != 42
?H:?%<:#include "Bletch: How does this C preprocessor catenate tokens?"
?H:?%<:#endif
?H:.
?W:%<:CAT2 CAT3 CAT4 CAT5 STRINGIFY SCAT2 SCAT3 SCAT4 SCAT5
?F:!cpp_stuff.c
?LINT:known StGiFy EQuoTe SQuoTe CaTiFy
: how do we catenate cpp tokens here?
echo " "
echo "Checking to see how your cpp does stuff like catenate tokens..." >&4
cpp_quote=''
$cat >cpp_stuff.c <<'EOCP'
#define RCAT(a,b)a/**/b
#define ACAT(a,b)a ## b
RCAT(Rei,ser)
ACAT(Cir,cus)
EOCP
$cppstdin $cppflags $cppminus <cpp_stuff.c >cpp_stuff.out 2>&1
if $contains 'Circus' cpp_stuff.out >/dev/null 2>&1; then
	echo "Oh!  Smells like ANSI's been here."
	echo "We can catify or stringify, separately or together!"
	cpp_stuff=42
elif $contains 'Reiser' cpp_stuff.out >/dev/null 2>&1; then
	echo "Ah, yes!  The good old days!"
	cpp_stuff=1
	$cat >cpp_stuff.c <<'EOCP'
#define SQuoTe(a)"a
#define EQuoTe(a)a"
#define CAT2(a,b)EQuoTe(SQuoTe(a)b)
CAT2(Vic,tory)
EOCP
	$cppstdin $cppflags $cppminus <cpp_stuff.c >cpp_stuff.out 2>&1
	if $contains '"Victory"' cpp_stuff.out >/dev/null 2>&1; then
		echo "I'll resort to a cute trick to also be able to stringify."
		cpp_quote='"'
	else
		$cat <<EOM
However, in the good old days we don't know how to stringify and
catify at the same time...
@if SCAT2 || SCAT3 || SCAT4 || SCAT5
You might have to edit the values of the SCAT[2-5] macros in config.h...
@else
Hopefully, $package does not need this feature.
@end
EOM
	fi
else
	./warn 4>&4 <<EOM
I can't seem to be able to catenate tokens with your cpp.
@if CAT2 || CAT3 || CAT4 || CAT5 || SCAT2 || SCAT3 || SCAT4 || SCAT5
You're going to have to edit the values of the following
macros in config.h:

@if CAT2 || CAT3 || CAT4 || CAT5
	CAT[2-5]
@end
@if SCAT2 || SCAT3 || SCAT4 || SCAT5
	SCAT[2-5]
@end

in order to let me compile.
@end
EOM
	cpp_stuff="/* Help! How do we handle cpp_stuff? */*/"
fi
$rm -f cpp_stuff.*