xref: /aoo41x/main/soltools/cpp/_tokens.c (revision cdf0e10c)
1*cdf0e10cSrcweir #include <stdio.h>
2*cdf0e10cSrcweir #include <stdlib.h>
3*cdf0e10cSrcweir #include <string.h>
4*cdf0e10cSrcweir #include <ctype.h>
5*cdf0e10cSrcweir #if (defined(_WIN32) || defined(_MSDOS) || defined(__IBMC__))
6*cdf0e10cSrcweir #include <io.h>
7*cdf0e10cSrcweir #else
8*cdf0e10cSrcweir #include <unistd.h>
9*cdf0e10cSrcweir #endif
10*cdf0e10cSrcweir #include "cpp.h"
11*cdf0e10cSrcweir 
12*cdf0e10cSrcweir 
13*cdf0e10cSrcweir static char wbuf[4 * OBS];
14*cdf0e10cSrcweir static char *wbp = wbuf;
15*cdf0e10cSrcweir static int EBCDIC_ExternTokenDetected = 0;
16*cdf0e10cSrcweir static int EBCDIC_StartTokenDetected = 0;
17*cdf0e10cSrcweir 
18*cdf0e10cSrcweir unsigned char toLatin1[256] =
19*cdf0e10cSrcweir {
20*cdf0e10cSrcweir     0x00, 0x01, 0x02, 0x03, 0x9c, 0x09, 0x86, 0x7f, 0x97, 0x8d,
21*cdf0e10cSrcweir     0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
22*cdf0e10cSrcweir     0x9d, 0x0a, 0x08, 0x87, 0x18, 0x19, 0x92, 0x8f, 0x1c, 0x1d,
23*cdf0e10cSrcweir     0x1e, 0x1f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x17, 0x1b,
24*cdf0e10cSrcweir     0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, 0x90, 0x91,
25*cdf0e10cSrcweir     0x16, 0x93, 0x94, 0x95, 0x96, 0x04, 0x98, 0x99, 0x9a, 0x9b,
26*cdf0e10cSrcweir     0x14, 0x15, 0x9e, 0x1a, 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1,
27*cdf0e10cSrcweir     0xe3, 0xe5, 0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c,
28*cdf0e10cSrcweir     0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, 0xec, 0xdf,
29*cdf0e10cSrcweir     0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, 0x2d, 0x2f, 0xc2, 0xc4,
30*cdf0e10cSrcweir     0xc0, 0xc1, 0xc3, 0xc5, 0xc7, 0xd1, 0xa6, 0x2c, 0x25, 0x5f,
31*cdf0e10cSrcweir     0x3e, 0x3f, 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf,
32*cdf0e10cSrcweir     0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22,
33*cdf0e10cSrcweir     0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
34*cdf0e10cSrcweir     0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, 0xb0, 0x6a, 0x6b, 0x6c,
35*cdf0e10cSrcweir     0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8,
36*cdf0e10cSrcweir     0xc6, 0xa4, 0xb5, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
37*cdf0e10cSrcweir     0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0x5b, 0xde, 0xae, 0xac, 0xa3,
38*cdf0e10cSrcweir     0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc, 0xbd, 0xbe, 0xdd, 0xa8,
39*cdf0e10cSrcweir     0xaf, 0x5d, 0xb4, 0xd7, 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45,
40*cdf0e10cSrcweir     0x46, 0x47, 0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5,
41*cdf0e10cSrcweir     0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52,
42*cdf0e10cSrcweir     0xb9, 0xfb, 0xfc, 0xf9, 0xfa, 0xff, 0x5c, 0xf7, 0x53, 0x54,
43*cdf0e10cSrcweir     0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2,
44*cdf0e10cSrcweir     0xd3, 0xd5, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
45*cdf0e10cSrcweir     0x38, 0x39, 0xb3, 0xdb, 0xdc, 0xd9, 0xda, 0x9f
46*cdf0e10cSrcweir };
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #define MASK    "\\x%x"
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir int
51*cdf0e10cSrcweir     memcpy_EBCDIC( char * pwbuf, uchar *p, int len )
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir     int currpos = 0;
54*cdf0e10cSrcweir     int processedchars = 0;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir     if( len == 0 )
57*cdf0e10cSrcweir         return 0;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     if( len == 1 )
60*cdf0e10cSrcweir     {
61*cdf0e10cSrcweir         *pwbuf = *p;
62*cdf0e10cSrcweir         return 1;
63*cdf0e10cSrcweir     }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     /* copy spaces until " or ' */
66*cdf0e10cSrcweir     while( (p[ processedchars ] != '\"') && (p[ processedchars ] != '\'') )
67*cdf0e10cSrcweir         pwbuf[ currpos++ ] = p[ processedchars++ ];
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     /* copy first " or ' */
70*cdf0e10cSrcweir     pwbuf[ currpos++ ] = p[ processedchars++ ];
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     /* convert all characters until " or ' */
73*cdf0e10cSrcweir     while( processedchars < (len - 1) )
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         if( p[ processedchars ] == '\\' )
76*cdf0e10cSrcweir         {
77*cdf0e10cSrcweir             switch( p[ ++processedchars ] )
78*cdf0e10cSrcweir             {
79*cdf0e10cSrcweir                 case 'n':
80*cdf0e10cSrcweir                     currpos += sprintf( &pwbuf[ currpos ], MASK, toLatin1['\n'] );
81*cdf0e10cSrcweir                     processedchars++;
82*cdf0e10cSrcweir                     break;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir                 case 't':
85*cdf0e10cSrcweir                     currpos += sprintf( &pwbuf[ currpos ], MASK, toLatin1['\t'] );
86*cdf0e10cSrcweir                     processedchars++;
87*cdf0e10cSrcweir                     break;
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir                 case 'v':
90*cdf0e10cSrcweir                     currpos += sprintf( &pwbuf[ currpos ], MASK, toLatin1['\v'] );
91*cdf0e10cSrcweir                     processedchars++;
92*cdf0e10cSrcweir                     break;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir                 case 'b':
95*cdf0e10cSrcweir                     currpos += sprintf( &pwbuf[ currpos ], MASK, toLatin1['\b'] );
96*cdf0e10cSrcweir                     processedchars++;
97*cdf0e10cSrcweir                     break;
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir                 case 'r':
100*cdf0e10cSrcweir                     currpos += sprintf( &pwbuf[ currpos ], MASK, toLatin1['\r'] );
101*cdf0e10cSrcweir                     processedchars++;
102*cdf0e10cSrcweir                     break;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir                 case 'f':
105*cdf0e10cSrcweir                     currpos += sprintf( &pwbuf[ currpos ], MASK, toLatin1['\f'] );
106*cdf0e10cSrcweir                     processedchars++;
107*cdf0e10cSrcweir                     break;
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir                 case 'a':
110*cdf0e10cSrcweir                     currpos += sprintf( &pwbuf[ currpos ], MASK, toLatin1['\a'] );
111*cdf0e10cSrcweir                     processedchars++;
112*cdf0e10cSrcweir                     break;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir                 case '\\':
115*cdf0e10cSrcweir                     currpos += sprintf( &pwbuf[ currpos ], MASK, toLatin1['\\'] );
116*cdf0e10cSrcweir                     processedchars++;
117*cdf0e10cSrcweir                     break;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir                 case '?':
120*cdf0e10cSrcweir                     currpos += sprintf( &pwbuf[ currpos ], MASK, toLatin1['\?'] );
121*cdf0e10cSrcweir                     processedchars++;
122*cdf0e10cSrcweir                     break;
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir                 case '\'':
125*cdf0e10cSrcweir                     currpos += sprintf( &pwbuf[ currpos ], MASK, toLatin1['\''] );
126*cdf0e10cSrcweir                     processedchars++;
127*cdf0e10cSrcweir                     break;
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir                 case '"':
130*cdf0e10cSrcweir                     currpos += sprintf( &pwbuf[ currpos ], MASK, toLatin1['\"'] );
131*cdf0e10cSrcweir                     processedchars++;
132*cdf0e10cSrcweir                     break;
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir                 /* octal coded character? -> copy */
135*cdf0e10cSrcweir                 case '0':
136*cdf0e10cSrcweir                 case '1':
137*cdf0e10cSrcweir                 case '2':
138*cdf0e10cSrcweir                 case '3':
139*cdf0e10cSrcweir                 case '4':
140*cdf0e10cSrcweir                 case '5':
141*cdf0e10cSrcweir                 case '6':
142*cdf0e10cSrcweir                 case '7':
143*cdf0e10cSrcweir                     {
144*cdf0e10cSrcweir                     int startpos = currpos;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir                     pwbuf[ currpos++ ] = '\\';
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir                     while( p[ processedchars ] >= '0' && p[ processedchars ] <= '7' && (currpos < startpos + 4) )
149*cdf0e10cSrcweir                           pwbuf[ currpos++ ] = (unsigned char)p[ processedchars++ ];
150*cdf0e10cSrcweir                     break;
151*cdf0e10cSrcweir                     }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir                 /* hex coded character? -> copy */
154*cdf0e10cSrcweir                 case 'x':
155*cdf0e10cSrcweir                 case 'X':
156*cdf0e10cSrcweir                     {
157*cdf0e10cSrcweir                     int startpos = currpos;
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir                     pwbuf[ currpos++ ] = '\\';
160*cdf0e10cSrcweir                     pwbuf[ currpos++ ] = 'x';
161*cdf0e10cSrcweir                     processedchars++;
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir                     while( isxdigit( p[ processedchars ] ) && (currpos < startpos + 4) )
164*cdf0e10cSrcweir                           pwbuf[ currpos++ ] = (unsigned char)p[ processedchars++ ];
165*cdf0e10cSrcweir                     break;
166*cdf0e10cSrcweir                     }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir             }
169*cdf0e10cSrcweir         }
170*cdf0e10cSrcweir         else
171*cdf0e10cSrcweir             currpos += sprintf( &pwbuf[ currpos ], MASK, toLatin1[p[ processedchars++ ]] );
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir     /* copy last " or ' */
176*cdf0e10cSrcweir     pwbuf[ currpos++ ] = p[ processedchars ];
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir     return currpos;
179*cdf0e10cSrcweir }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir void
182*cdf0e10cSrcweir     maketokenrow(int size, Tokenrow * trp)
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir     trp->max = size;
185*cdf0e10cSrcweir     if (size > 0)
186*cdf0e10cSrcweir         trp->bp = (Token *) domalloc(size * sizeof(Token));
187*cdf0e10cSrcweir     else
188*cdf0e10cSrcweir         trp->bp = NULL;
189*cdf0e10cSrcweir     trp->tp = trp->bp;
190*cdf0e10cSrcweir     trp->lp = trp->bp;
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir Token *
194*cdf0e10cSrcweir     growtokenrow(Tokenrow * trp)
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir     int ncur = trp->tp - trp->bp;
197*cdf0e10cSrcweir     int nlast = trp->lp - trp->bp;
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir     trp->max = 3 * trp->max / 2 + 1;
200*cdf0e10cSrcweir     trp->bp = (Token *) realloc(trp->bp, trp->max * sizeof(Token));
201*cdf0e10cSrcweir     trp->lp = &trp->bp[nlast];
202*cdf0e10cSrcweir     trp->tp = &trp->bp[ncur];
203*cdf0e10cSrcweir     return trp->lp;
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir /*
207*cdf0e10cSrcweir  * Compare a row of tokens, ignoring the content of WS; return !=0 if different
208*cdf0e10cSrcweir  */
209*cdf0e10cSrcweir int
210*cdf0e10cSrcweir     comparetokens(Tokenrow * tr1, Tokenrow * tr2)
211*cdf0e10cSrcweir {
212*cdf0e10cSrcweir     Token *tp1, *tp2;
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir     tp1 = tr1->tp;
215*cdf0e10cSrcweir     tp2 = tr2->tp;
216*cdf0e10cSrcweir     if (tr1->lp - tp1 != tr2->lp - tp2)
217*cdf0e10cSrcweir         return 1;
218*cdf0e10cSrcweir     for (; tp1 < tr1->lp; tp1++, tp2++)
219*cdf0e10cSrcweir     {
220*cdf0e10cSrcweir         if (tp1->type != tp2->type
221*cdf0e10cSrcweir             || (tp1->wslen == 0) != (tp2->wslen == 0)
222*cdf0e10cSrcweir             || tp1->len != tp2->len
223*cdf0e10cSrcweir             || strncmp((char *) tp1->t, (char *) tp2->t, tp1->len) != 0)
224*cdf0e10cSrcweir             return 1;
225*cdf0e10cSrcweir     }
226*cdf0e10cSrcweir     return 0;
227*cdf0e10cSrcweir }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir /*
230*cdf0e10cSrcweir  * replace ntok tokens starting at dtr->tp with the contents of str.
231*cdf0e10cSrcweir  * tp ends up pointing just beyond the replacement.
232*cdf0e10cSrcweir  * Canonical whitespace is assured on each side.
233*cdf0e10cSrcweir  */
234*cdf0e10cSrcweir void
235*cdf0e10cSrcweir     insertrow(Tokenrow * dtr, int ntok, Tokenrow * str)
236*cdf0e10cSrcweir {
237*cdf0e10cSrcweir     int nrtok = rowlen(str);
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir     dtr->tp += ntok;
240*cdf0e10cSrcweir     adjustrow(dtr, nrtok - ntok);
241*cdf0e10cSrcweir     dtr->tp -= ntok;
242*cdf0e10cSrcweir     movetokenrow(dtr, str);
243*cdf0e10cSrcweir     dtr->tp += nrtok;
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir /*
247*cdf0e10cSrcweir  * make sure there is WS before trp->tp, if tokens might merge in the output
248*cdf0e10cSrcweir  */
249*cdf0e10cSrcweir void
250*cdf0e10cSrcweir     makespace(Tokenrow * trp, Token * ntp)
251*cdf0e10cSrcweir {
252*cdf0e10cSrcweir     uchar *tt;
253*cdf0e10cSrcweir     Token *tp = trp->tp;
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir     if (tp >= trp->lp)
256*cdf0e10cSrcweir         return;
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir     if (ntp->wslen)
259*cdf0e10cSrcweir     {
260*cdf0e10cSrcweir         tt = newstring(tp->t, tp->len, ntp->wslen);
261*cdf0e10cSrcweir         strncpy((char *)tt, (char *)ntp->t - ntp->wslen, ntp->wslen);
262*cdf0e10cSrcweir         tp->t = tt + ntp->wslen;
263*cdf0e10cSrcweir         tp->wslen = ntp->wslen;
264*cdf0e10cSrcweir         tp->flag |= XPWS;
265*cdf0e10cSrcweir     }
266*cdf0e10cSrcweir }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir /*
269*cdf0e10cSrcweir  * Copy an entire tokenrow into another, at tp.
270*cdf0e10cSrcweir  * It is assumed that there is enough space.
271*cdf0e10cSrcweir  *  Not strictly conforming.
272*cdf0e10cSrcweir  */
273*cdf0e10cSrcweir void
274*cdf0e10cSrcweir     movetokenrow(Tokenrow * dtr, Tokenrow * str)
275*cdf0e10cSrcweir {
276*cdf0e10cSrcweir     int nby;
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir     /* nby = sizeof(Token) * (str->lp - str->bp); */
279*cdf0e10cSrcweir     nby = (char *) str->lp - (char *) str->bp;
280*cdf0e10cSrcweir     memmove(dtr->tp, str->bp, nby);
281*cdf0e10cSrcweir }
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir /*
284*cdf0e10cSrcweir  * Move the tokens in a row, starting at tr->tp, rightward by nt tokens;
285*cdf0e10cSrcweir  * nt may be negative (left move).
286*cdf0e10cSrcweir  * The row may need to be grown.
287*cdf0e10cSrcweir  * Non-strictly conforming because of the (char *), but easily fixed
288*cdf0e10cSrcweir  */
289*cdf0e10cSrcweir void
290*cdf0e10cSrcweir     adjustrow(Tokenrow * trp, int nt)
291*cdf0e10cSrcweir {
292*cdf0e10cSrcweir     int nby, size;
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir     if (nt == 0)
295*cdf0e10cSrcweir         return;
296*cdf0e10cSrcweir     size = (trp->lp - trp->bp) + nt;
297*cdf0e10cSrcweir     while (size > trp->max)
298*cdf0e10cSrcweir         growtokenrow(trp);
299*cdf0e10cSrcweir     /* nby = sizeof(Token) * (trp->lp - trp->tp); */
300*cdf0e10cSrcweir     nby = (char *) trp->lp - (char *) trp->tp;
301*cdf0e10cSrcweir     if (nby)
302*cdf0e10cSrcweir         memmove(trp->tp + nt, trp->tp, nby);
303*cdf0e10cSrcweir     trp->lp += nt;
304*cdf0e10cSrcweir }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir /*
307*cdf0e10cSrcweir  * Copy a row of tokens into the destination holder, allocating
308*cdf0e10cSrcweir  * the space for the contents.  Return the destination.
309*cdf0e10cSrcweir  */
310*cdf0e10cSrcweir Tokenrow *
311*cdf0e10cSrcweir     copytokenrow(Tokenrow * dtr, Tokenrow * str)
312*cdf0e10cSrcweir {
313*cdf0e10cSrcweir     int len = rowlen(str);
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir     maketokenrow(len, dtr);
316*cdf0e10cSrcweir     movetokenrow(dtr, str);
317*cdf0e10cSrcweir     dtr->lp += len;
318*cdf0e10cSrcweir     return dtr;
319*cdf0e10cSrcweir }
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir /*
322*cdf0e10cSrcweir  * Produce a copy of a row of tokens.  Start at trp->tp.
323*cdf0e10cSrcweir  * The value strings are copied as well.  The first token
324*cdf0e10cSrcweir  * has WS available.
325*cdf0e10cSrcweir  */
326*cdf0e10cSrcweir Tokenrow *
327*cdf0e10cSrcweir     normtokenrow(Tokenrow * trp)
328*cdf0e10cSrcweir {
329*cdf0e10cSrcweir     Token *tp;
330*cdf0e10cSrcweir     Tokenrow *ntrp = new(Tokenrow);
331*cdf0e10cSrcweir     int len;
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir     len = trp->lp - trp->tp;
334*cdf0e10cSrcweir     if (len <= 0)
335*cdf0e10cSrcweir         len = 1;
336*cdf0e10cSrcweir     maketokenrow(len, ntrp);
337*cdf0e10cSrcweir     for (tp = trp->tp; tp < trp->lp; tp++)
338*cdf0e10cSrcweir     {
339*cdf0e10cSrcweir         *ntrp->lp = *tp;
340*cdf0e10cSrcweir         if (tp->len)
341*cdf0e10cSrcweir         {
342*cdf0e10cSrcweir             ntrp->lp->t = newstring(tp->t, tp->len, 1);
343*cdf0e10cSrcweir             *ntrp->lp->t++ = ' ';
344*cdf0e10cSrcweir             if (tp->wslen)
345*cdf0e10cSrcweir                 ntrp->lp->wslen = 1;
346*cdf0e10cSrcweir         }
347*cdf0e10cSrcweir         ntrp->lp++;
348*cdf0e10cSrcweir     }
349*cdf0e10cSrcweir     if (ntrp->lp > ntrp->bp)
350*cdf0e10cSrcweir         ntrp->bp->wslen = 0;
351*cdf0e10cSrcweir     return ntrp;
352*cdf0e10cSrcweir }
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir /*
355*cdf0e10cSrcweir  * Debugging
356*cdf0e10cSrcweir  */
357*cdf0e10cSrcweir void
358*cdf0e10cSrcweir     peektokens(Tokenrow * trp, char *str)
359*cdf0e10cSrcweir {
360*cdf0e10cSrcweir     Token *tp;
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir     tp = trp->tp;
363*cdf0e10cSrcweir     flushout();
364*cdf0e10cSrcweir     if (str)
365*cdf0e10cSrcweir         fprintf(stderr, "%s ", str);
366*cdf0e10cSrcweir     if (tp < trp->bp || tp > trp->lp)
367*cdf0e10cSrcweir         fprintf(stderr, "(tp offset %ld) ", (long int) (tp - trp->bp));
368*cdf0e10cSrcweir     for (tp = trp->bp; tp < trp->lp && tp < trp->bp + 32; tp++)
369*cdf0e10cSrcweir     {
370*cdf0e10cSrcweir         if (tp->type != NL)
371*cdf0e10cSrcweir         {
372*cdf0e10cSrcweir             int c = tp->t[tp->len];
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir             tp->t[tp->len] = 0;
375*cdf0e10cSrcweir             fprintf(stderr, "%s", tp->t);
376*cdf0e10cSrcweir             tp->t[tp->len] = (uchar) c;
377*cdf0e10cSrcweir         }
378*cdf0e10cSrcweir         fprintf(stderr, tp == trp->tp ? "{%x*} " : "{%x} ", tp->type);
379*cdf0e10cSrcweir     }
380*cdf0e10cSrcweir     fprintf(stderr, "\n");
381*cdf0e10cSrcweir     fflush(stderr);
382*cdf0e10cSrcweir }
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir void
385*cdf0e10cSrcweir     puttokens(Tokenrow * trp)
386*cdf0e10cSrcweir {
387*cdf0e10cSrcweir     Token *tp;
388*cdf0e10cSrcweir     int len;
389*cdf0e10cSrcweir     uchar *p;
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir     if (Vflag)
392*cdf0e10cSrcweir         peektokens(trp, "");
393*cdf0e10cSrcweir     tp = trp->bp;
394*cdf0e10cSrcweir     for (; tp < trp->lp; tp++)
395*cdf0e10cSrcweir     {
396*cdf0e10cSrcweir         if (tp->type != NL)
397*cdf0e10cSrcweir         {
398*cdf0e10cSrcweir             len = tp->len + tp->wslen;
399*cdf0e10cSrcweir             p = tp->t - tp->wslen;
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir 			/* add parameter check to delete operator? */
402*cdf0e10cSrcweir 			if( Dflag )
403*cdf0e10cSrcweir 			{
404*cdf0e10cSrcweir 				if( (tp->type == NAME) && (strncmp( (char*)p, "delete", len ) == 0) )
405*cdf0e10cSrcweir 				{
406*cdf0e10cSrcweir 					Token* ntp = tp;
407*cdf0e10cSrcweir 					ntp++;
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir 					if( ntp->type == NAME )
410*cdf0e10cSrcweir 					{
411*cdf0e10cSrcweir 						uchar* np = ntp->t - ntp->wslen;
412*cdf0e10cSrcweir 			            int nlen = ntp->len + ntp->wslen;
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir 						memcpy(wbp, "if(", 3 );
415*cdf0e10cSrcweir  			            wbp += 4;
416*cdf0e10cSrcweir 						memcpy(wbp, np, nlen );
417*cdf0e10cSrcweir  			            wbp += nlen;
418*cdf0e10cSrcweir 						memcpy(wbp, ")", 1 );
419*cdf0e10cSrcweir  			            wbp++;
420*cdf0e10cSrcweir 
421*cdf0e10cSrcweir                         memcpy(wbp, p, len);
422*cdf0e10cSrcweir 					}
423*cdf0e10cSrcweir 				}
424*cdf0e10cSrcweir 			}
425*cdf0e10cSrcweir 
426*cdf0e10cSrcweir             /* EBCDIC to ANSI conversion requested? */
427*cdf0e10cSrcweir             if( Aflag )
428*cdf0e10cSrcweir             {
429*cdf0e10cSrcweir                 /* keyword __ToLatin1__ found? -> do conversion! */
430*cdf0e10cSrcweir                 if( EBCDIC_StartTokenDetected )
431*cdf0e10cSrcweir                 {
432*cdf0e10cSrcweir                     /* previous token was 'extern'? -> don't convert current token! */
433*cdf0e10cSrcweir                     if( EBCDIC_ExternTokenDetected )
434*cdf0e10cSrcweir                     {
435*cdf0e10cSrcweir                         EBCDIC_ExternTokenDetected = 0;
436*cdf0e10cSrcweir                         memcpy(wbp, p, len);
437*cdf0e10cSrcweir                     }
438*cdf0e10cSrcweir                     else
439*cdf0e10cSrcweir                     {
440*cdf0e10cSrcweir                         /* current token is keyword 'extern'? -> don't convert following token! */
441*cdf0e10cSrcweir                         if( (tp->wslen == 0) && (strncmp( (char*)p, "extern", len ) == 0) )
442*cdf0e10cSrcweir                         {
443*cdf0e10cSrcweir                             EBCDIC_ExternTokenDetected = 1;
444*cdf0e10cSrcweir                             memcpy(wbp, p, len);
445*cdf0e10cSrcweir                         }
446*cdf0e10cSrcweir                         else
447*cdf0e10cSrcweir                         {
448*cdf0e10cSrcweir                             /* token is string or char? -> process EBCDIC to ANSI conversion */
449*cdf0e10cSrcweir                             if ((tp->type == STRING) || (tp->type == CCON))
450*cdf0e10cSrcweir                                 len = memcpy_EBCDIC(wbp,  p, len);
451*cdf0e10cSrcweir                             else
452*cdf0e10cSrcweir                                 memcpy(wbp, p, len);
453*cdf0e10cSrcweir                         }
454*cdf0e10cSrcweir                     }
455*cdf0e10cSrcweir                 }
456*cdf0e10cSrcweir                 else
457*cdf0e10cSrcweir                     /* keyword __ToLatin1__ found? -> don't copy keyword and start conversion */
458*cdf0e10cSrcweir                     if( (tp->type == NAME) && (strncmp( (char*)p, "__ToLatin1__", len) == 0) )
459*cdf0e10cSrcweir                     {
460*cdf0e10cSrcweir                         EBCDIC_StartTokenDetected = 1;
461*cdf0e10cSrcweir                         len = 0;
462*cdf0e10cSrcweir                     }
463*cdf0e10cSrcweir                     else
464*cdf0e10cSrcweir                         memcpy(wbp, p, len);
465*cdf0e10cSrcweir             }
466*cdf0e10cSrcweir             else
467*cdf0e10cSrcweir                 memcpy(wbp, p, len);
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir             wbp += len;
470*cdf0e10cSrcweir         }
471*cdf0e10cSrcweir         else
472*cdf0e10cSrcweir             *wbp++ = '\n';
473*cdf0e10cSrcweir 
474*cdf0e10cSrcweir         if (wbp >= &wbuf[OBS])
475*cdf0e10cSrcweir         {
476*cdf0e10cSrcweir             if ( write(1, wbuf, OBS) != -1 ) {
477*cdf0e10cSrcweir             if (wbp > &wbuf[OBS])
478*cdf0e10cSrcweir                 memcpy(wbuf, wbuf + OBS, wbp - &wbuf[OBS]);
479*cdf0e10cSrcweir             wbp -= OBS;
480*cdf0e10cSrcweir 	    }
481*cdf0e10cSrcweir 		else exit(1);
482*cdf0e10cSrcweir         }
483*cdf0e10cSrcweir     }
484*cdf0e10cSrcweir     trp->tp = tp;
485*cdf0e10cSrcweir     if (cursource->fd == 0)
486*cdf0e10cSrcweir         flushout();
487*cdf0e10cSrcweir }
488*cdf0e10cSrcweir 
489*cdf0e10cSrcweir void
490*cdf0e10cSrcweir     flushout(void)
491*cdf0e10cSrcweir {
492*cdf0e10cSrcweir     if (wbp > wbuf)
493*cdf0e10cSrcweir     {
494*cdf0e10cSrcweir         if ( write(1, wbuf, wbp - wbuf) != -1)
495*cdf0e10cSrcweir         	wbp = wbuf;
496*cdf0e10cSrcweir 	else
497*cdf0e10cSrcweir 		exit(1);
498*cdf0e10cSrcweir     }
499*cdf0e10cSrcweir }
500*cdf0e10cSrcweir 
501*cdf0e10cSrcweir /*
502*cdf0e10cSrcweir  * turn a row into just a newline
503*cdf0e10cSrcweir  */
504*cdf0e10cSrcweir void
505*cdf0e10cSrcweir     setempty(Tokenrow * trp)
506*cdf0e10cSrcweir {
507*cdf0e10cSrcweir     trp->tp = trp->bp;
508*cdf0e10cSrcweir     trp->lp = trp->bp + 1;
509*cdf0e10cSrcweir     *trp->bp = nltoken;
510*cdf0e10cSrcweir }
511*cdf0e10cSrcweir 
512*cdf0e10cSrcweir /*
513*cdf0e10cSrcweir  * generate a number
514*cdf0e10cSrcweir  */
515*cdf0e10cSrcweir char *
516*cdf0e10cSrcweir     outnum(char *p, int n)
517*cdf0e10cSrcweir {
518*cdf0e10cSrcweir     if (n >= 10)
519*cdf0e10cSrcweir         p = outnum(p, n / 10);
520*cdf0e10cSrcweir     *p++ = (char) (n % 10 + '0');
521*cdf0e10cSrcweir     return p;
522*cdf0e10cSrcweir }
523*cdf0e10cSrcweir 
524*cdf0e10cSrcweir /*
525*cdf0e10cSrcweir  * allocate and initialize a new string from s, of length l, at offset o
526*cdf0e10cSrcweir  * Null terminated.
527*cdf0e10cSrcweir  */
528*cdf0e10cSrcweir uchar *
529*cdf0e10cSrcweir     newstring(uchar * s, int l, int o)
530*cdf0e10cSrcweir {
531*cdf0e10cSrcweir     uchar *ns = (uchar *) domalloc(l + o + 1);
532*cdf0e10cSrcweir 
533*cdf0e10cSrcweir     ns[l + o] = '\0';
534*cdf0e10cSrcweir     return (uchar *) strncpy((char *) ns + o, (char *) s, l) - o;
535*cdf0e10cSrcweir }
536