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