00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include "cpp.h"
00005
00006 static char wbuf[2*OBS];
00007 static char *wbp = wbuf;
00008
00009
00010
00011
00012
00013 static const char wstab[] = {
00014 0,
00015 0,
00016 0,
00017 0,
00018 0,
00019 0,
00020 1,
00021 0,
00022 0,
00023 0,
00024 0,
00025 0,
00026 0,
00027 0,
00028 0,
00029 0,
00030 0,
00031 0,
00032 0,
00033 0,
00034 1,
00035 1,
00036 1,
00037 1,
00038 0,
00039 0,
00040 0,
00041 0,
00042 0,
00043 0,
00044 0,
00045 0,
00046 0,
00047 0,
00048 0,
00049 0,
00050 0,
00051 0,
00052 0,
00053 0,
00054 1,
00055 0,
00056 1,
00057 1,
00058 1,
00059 0,
00060 0,
00061 0,
00062 0,
00063 0,
00064 0,
00065 0,
00066 0,
00067 0,
00068 0,
00069 0,
00070 0,
00071 0,
00072 0,
00073 0,
00074 };
00075
00076 void
00077 maketokenrow(int size, Tokenrow *trp)
00078 {
00079 trp->max = size;
00080 if (size>0)
00081 trp->bp = (Token *)domalloc(size*sizeof(Token));
00082 else
00083 trp->bp = NULL;
00084 trp->tp = trp->bp;
00085 trp->lp = trp->bp;
00086 }
00087
00088 Token *
00089 growtokenrow(Tokenrow *trp)
00090 {
00091 int ncur = trp->tp - trp->bp;
00092 int nlast = trp->lp - trp->bp;
00093
00094 trp->max = 3*trp->max/2 + 1;
00095 trp->bp = (Token *)realloc(trp->bp, trp->max*sizeof(Token));
00096 if (trp->bp == NULL)
00097 error(FATAL, "Out of memory from realloc");
00098 trp->lp = &trp->bp[nlast];
00099 trp->tp = &trp->bp[ncur];
00100 return trp->lp;
00101 }
00102
00103
00104
00105
00106 int
00107 comparetokens(Tokenrow *tr1, Tokenrow *tr2)
00108 {
00109 Token *tp1, *tp2;
00110
00111 tp1 = tr1->tp;
00112 tp2 = tr2->tp;
00113 if (tr1->lp-tp1 != tr2->lp-tp2)
00114 return 1;
00115 for (; tp1<tr1->lp ; tp1++, tp2++) {
00116 if (tp1->type != tp2->type
00117 || (tp1->wslen==0) != (tp2->wslen==0)
00118 || tp1->len != tp2->len
00119 || strncmp((char*)tp1->t, (char*)tp2->t, tp1->len)!=0)
00120 return 1;
00121 }
00122 return 0;
00123 }
00124
00125
00126
00127
00128
00129
00130 void
00131 insertrow(Tokenrow *dtr, int ntok, Tokenrow *str)
00132 {
00133 int nrtok = rowlen(str);
00134
00135 dtr->tp += ntok;
00136 adjustrow(dtr, nrtok-ntok);
00137 dtr->tp -= ntok;
00138 movetokenrow(dtr, str);
00139 makespace(dtr);
00140 dtr->tp += nrtok;
00141 makespace(dtr);
00142 }
00143
00144
00145
00146
00147 void
00148 makespace(Tokenrow *trp)
00149 {
00150 uchar *tt;
00151 Token *tp = trp->tp;
00152
00153 if (tp >= trp->lp)
00154 return;
00155 if (tp->wslen) {
00156 if (tp->flag&XPWS
00157 && (wstab[tp->type] || trp->tp>trp->bp && wstab[(tp-1)->type])) {
00158 tp->wslen = 0;
00159 return;
00160 }
00161 tp->t[-1] = ' ';
00162 return;
00163 }
00164 if (wstab[tp->type] || trp->tp>trp->bp && wstab[(tp-1)->type])
00165 return;
00166 tt = newstring(tp->t, tp->len, 1);
00167 *tt++ = ' ';
00168 tp->t = tt;
00169 tp->wslen = 1;
00170 tp->flag |= XPWS;
00171 }
00172
00173
00174
00175
00176
00177
00178 void
00179 movetokenrow(Tokenrow *dtr, Tokenrow *str)
00180 {
00181 int nby;
00182
00183
00184 nby = (char *)str->lp - (char *)str->bp;
00185 memmove(dtr->tp, str->bp, nby);
00186 }
00187
00188
00189
00190
00191
00192
00193
00194 void
00195 adjustrow(Tokenrow *trp, int nt)
00196 {
00197 int nby, size;
00198
00199 if (nt==0)
00200 return;
00201 size = (trp->lp - trp->bp) + nt;
00202 while (size > trp->max)
00203 growtokenrow(trp);
00204
00205 nby = (char *)trp->lp - (char *)trp->tp;
00206 if (nby)
00207 memmove(trp->tp+nt, trp->tp, nby);
00208 trp->lp += nt;
00209 }
00210
00211
00212
00213
00214
00215 Tokenrow *
00216 copytokenrow(Tokenrow *dtr, Tokenrow *str)
00217 {
00218 int len = rowlen(str);
00219
00220 maketokenrow(len, dtr);
00221 movetokenrow(dtr, str);
00222 dtr->lp += len;
00223 return dtr;
00224 }
00225
00226
00227
00228
00229
00230
00231 Tokenrow *
00232 normtokenrow(Tokenrow *trp)
00233 {
00234 Token *tp;
00235 Tokenrow *ntrp = new(Tokenrow);
00236 int len;
00237
00238 len = trp->lp - trp->tp;
00239 if (len<=0)
00240 len = 1;
00241 maketokenrow(len, ntrp);
00242 for (tp=trp->tp; tp < trp->lp; tp++) {
00243 *ntrp->lp = *tp;
00244 if (tp->len) {
00245 ntrp->lp->t = newstring(tp->t, tp->len, 1);
00246 *ntrp->lp->t++ = ' ';
00247 if (tp->wslen)
00248 ntrp->lp->wslen = 1;
00249 }
00250 ntrp->lp++;
00251 }
00252 if (ntrp->lp > ntrp->bp)
00253 ntrp->bp->wslen = 0;
00254 return ntrp;
00255 }
00256
00257
00258
00259
00260 void
00261 peektokens(Tokenrow *trp, char *str)
00262 {
00263 Token *tp;
00264
00265 tp = trp->tp;
00266 flushout();
00267 if (str)
00268 fprintf(stderr, "%s ", str);
00269 if (tp<trp->bp || tp>trp->lp)
00270 fprintf(stderr, "(tp offset %d) ", tp-trp->bp);
00271 for (tp=trp->bp; tp<trp->lp && tp<trp->bp+32; tp++) {
00272 if (tp->type!=NL) {
00273 int c = tp->t[tp->len];
00274 tp->t[tp->len] = 0;
00275 fprintf(stderr, "%s", tp->t);
00276 tp->t[tp->len] = c;
00277 }
00278 if (tp->type==NAME) {
00279 fprintf(stderr, tp==trp->tp?"{*":"{");
00280 prhideset(tp->hideset);
00281 fprintf(stderr, "} ");
00282 } else
00283 fprintf(stderr, tp==trp->tp?"{%x*} ":"{%x} ", tp->type);
00284 }
00285 fprintf(stderr, "\n");
00286 fflush(stderr);
00287 }
00288
00289 void
00290 puttokens(Tokenrow *trp)
00291 {
00292 Token *tp;
00293 int len;
00294 uchar *p;
00295
00296 if (verbose)
00297 peektokens(trp, "");
00298 tp = trp->bp;
00299 for (; tp<trp->lp; tp++) {
00300 len = tp->len+tp->wslen;
00301 p = tp->t-tp->wslen;
00302 while (tp<trp->lp-1 && p+len == (tp+1)->t - (tp+1)->wslen) {
00303 tp++;
00304 len += tp->wslen+tp->len;
00305 }
00306 if (len>OBS/2) {
00307 if (wbp > wbuf)
00308 write(1, wbuf, wbp-wbuf);
00309 write(1, (char *)p, len);
00310 wbp = wbuf;
00311 } else {
00312 memcpy(wbp, p, len);
00313 wbp += len;
00314 }
00315 if (wbp >= &wbuf[OBS]) {
00316 write(1, wbuf, OBS);
00317 if (wbp > &wbuf[OBS])
00318 memcpy(wbuf, wbuf+OBS, wbp - &wbuf[OBS]);
00319 wbp -= OBS;
00320 }
00321 }
00322 trp->tp = tp;
00323 if (cursource->fd==0)
00324 flushout();
00325 }
00326
00327 void
00328 flushout(void)
00329 {
00330 if (wbp>wbuf) {
00331 write(1, wbuf, wbp-wbuf);
00332 wbp = wbuf;
00333 }
00334 }
00335
00336
00337
00338
00339 void
00340 setempty(Tokenrow *trp)
00341 {
00342 trp->tp = trp->bp;
00343 trp->lp = trp->bp+1;
00344 *trp->bp = nltoken;
00345 }
00346
00347
00348
00349
00350 char *
00351 outnum(char *p, int n)
00352 {
00353 if (n>=10)
00354 p = outnum(p, n/10);
00355 *p++ = n%10 + '0';
00356 return p;
00357 }
00358
00359
00360
00361
00362
00363 uchar *
00364 newstring(uchar *s, int l, int o)
00365 {
00366 uchar *ns = (uchar *)domalloc(l+o+1);
00367
00368 ns[l+o] = '\0';
00369 return (uchar*)strncpy((char*)ns+o, (char*)s, l) - o;
00370 }