6 static char wbuf[2*OBS];
7 static char *wbp = wbuf;
10 * 1 for tokens that don't need whitespace when they get inserted
13 static const char wstab[] = {
77 maketokenrow(int size, Tokenrow *trp)
81 trp->bp = (Token *)domalloc(size*sizeof(Token));
89 growtokenrow(Tokenrow *trp)
91 int ncur = trp->tp - trp->bp;
92 int nlast = trp->lp - trp->bp;
94 trp->max = 3*trp->max/2 + 1;
95 trp->bp = (Token *)realloc(trp->bp, trp->max*sizeof(Token));
97 error(FATAL, "Out of memory from realloc");
98 trp->lp = &trp->bp[nlast];
99 trp->tp = &trp->bp[ncur];
104 * Compare a row of tokens, ignoring the content of WS; return !=0 if different
107 comparetokens(Tokenrow *tr1, Tokenrow *tr2)
113 if (tr1->lp-tp1 != tr2->lp-tp2)
115 for (; tp1<tr1->lp ; tp1++, tp2++) {
116 if (tp1->type != tp2->type
117 || (tp1->wslen==0) != (tp2->wslen==0)
118 || tp1->len != tp2->len
119 || strncmp((char*)tp1->t, (char*)tp2->t, tp1->len)!=0)
126 * replace ntok tokens starting at dtr->tp with the contents of str.
127 * tp ends up pointing just beyond the replacement.
128 * Canonical whitespace is assured on each side.
131 insertrow(Tokenrow *dtr, int ntok, Tokenrow *str)
133 int nrtok = rowlen(str);
136 adjustrow(dtr, nrtok-ntok);
138 movetokenrow(dtr, str);
145 * make sure there is WS before trp->tp, if tokens might merge in the output
148 makespace(Tokenrow *trp)
157 && (wstab[tp->type] || (trp->tp>trp->bp && wstab[(tp-1)->type]))) {
164 if (wstab[tp->type] || (trp->tp>trp->bp && wstab[(tp-1)->type]))
166 tt = newstring(tp->t, tp->len, 1);
174 * Copy an entire tokenrow into another, at tp.
175 * It is assumed that there is enough space.
176 * Not strictly conforming.
179 movetokenrow(Tokenrow *dtr, Tokenrow *str)
183 /* nby = sizeof(Token) * (str->lp - str->bp); */
184 nby = (char *)str->lp - (char *)str->bp;
185 memmove(dtr->tp, str->bp, nby);
189 * Move the tokens in a row, starting at tr->tp, rightward by nt tokens;
190 * nt may be negative (left move).
191 * The row may need to be grown.
192 * Non-strictly conforming because of the (char *), but easily fixed
195 adjustrow(Tokenrow *trp, int nt)
201 size = (trp->lp - trp->bp) + nt;
202 while (size > trp->max)
204 /* nby = sizeof(Token) * (trp->lp - trp->tp); */
205 nby = (char *)trp->lp - (char *)trp->tp;
207 memmove(trp->tp+nt, trp->tp, nby);
212 * Copy a row of tokens into the destination holder, allocating
213 * the space for the contents. Return the destination.
216 copytokenrow(Tokenrow *dtr, Tokenrow *str)
218 int len = rowlen(str);
220 maketokenrow(len, dtr);
221 movetokenrow(dtr, str);
227 * Produce a copy of a row of tokens. Start at trp->tp.
228 * The value strings are copied as well. The first token
232 normtokenrow(Tokenrow *trp)
235 Tokenrow *ntrp = new(Tokenrow);
238 len = trp->lp - trp->tp;
241 maketokenrow(len, ntrp);
242 for (tp=trp->tp; tp < trp->lp; tp++) {
245 ntrp->lp->t = newstring(tp->t, tp->len, 1);
246 *ntrp->lp->t++ = ' ';
252 if (ntrp->lp > ntrp->bp)
261 peektokens(Tokenrow *trp, char *str)
268 fprintf(stderr, "%s ", str);
269 if (tp<trp->bp || tp>trp->lp)
270 fprintf(stderr, "(tp offset %ld) ", (long int) (tp - trp->bp));
271 for (tp=trp->bp; tp<trp->lp && tp<trp->bp+32; tp++) {
273 int c = tp->t[tp->len];
275 fprintf(stderr, "%s", tp->t);
278 if (tp->type==NAME) {
279 fprintf(stderr, tp==trp->tp?"{*":"{");
280 prhideset(tp->hideset);
281 fprintf(stderr, "} ");
283 fprintf(stderr, tp==trp->tp?"{%x*} ":"{%x} ", tp->type);
285 fprintf(stderr, "\n");
290 puttokens(Tokenrow *trp)
299 for (; tp<trp->lp; tp++) {
300 len = tp->len+tp->wslen;
302 while (tp<trp->lp-1 && p+len == (tp+1)->t - (tp+1)->wslen) {
304 len += tp->wslen+tp->len;
306 if (len>OBS/2) { /* handle giant token */
308 write(1, wbuf, wbp-wbuf);
309 write(1, (char *)p, len);
315 if (wbp >= &wbuf[OBS]) {
317 if (wbp > &wbuf[OBS])
318 memcpy(wbuf, wbuf+OBS, wbp - &wbuf[OBS]);
323 if (cursource->fd==0)
331 write(1, wbuf, wbp-wbuf);
337 * turn a row into just a newline
340 setempty(Tokenrow *trp)
351 outnum(char *p, int n)
360 * allocate and initialize a new string from s, of length l, at offset o
364 newstring(uchar *s, int l, int o)
366 uchar *ns = (uchar *)domalloc(l+o+1);
369 return (uchar*)strncpy((char*)ns+o, (char*)s, l) - o;