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 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 *)dorealloc(trp->bp, trp->max*sizeof(Token));
96 trp->lp = &trp->bp[nlast];
97 trp->tp = &trp->bp[ncur];
102 * Compare a row of tokens, ignoring the content of WS; return !=0 if different
105 comparetokens(Tokenrow *tr1, Tokenrow *tr2)
111 if (tr1->lp-tp1 != tr2->lp-tp2)
113 for (; tp1<tr1->lp ; tp1++, tp2++) {
114 if (tp1->type != tp2->type
115 || (tp1->wslen==0) != (tp2->wslen==0)
116 || tp1->len != tp2->len
117 || strncmp((char*)tp1->t, (char*)tp2->t, tp1->len)!=0)
124 * replace ntok tokens starting at dtr->tp with the contents of str.
125 * tp ends up pointing just beyond the replacement.
126 * Canonical whitespace is assured on each side.
129 insertrow(Tokenrow *dtr, int ntok, Tokenrow *str)
131 int nrtok = rowlen(str);
134 adjustrow(dtr, nrtok-ntok);
136 movetokenrow(dtr, str);
143 * make sure there is WS before trp->tp, if tokens might merge in the output
146 makespace(Tokenrow *trp)
155 && (wstab[tp->type] || trp->tp>trp->bp && wstab[(tp-1)->type])) {
162 if (wstab[tp->type] || trp->tp>trp->bp && wstab[(tp-1)->type])
164 tt = newstring(tp->t, tp->len, 1);
172 * Copy an entire tokenrow into another, at tp.
173 * It is assumed that there is enough space.
174 * Not strictly conforming.
177 movetokenrow(Tokenrow *dtr, Tokenrow *str)
181 /* nby = sizeof(Token) * (str->lp - str->bp); */
182 nby = (char *)str->lp - (char *)str->bp;
183 memmove(dtr->tp, str->bp, nby);
187 * Move the tokens in a row, starting at tr->tp, rightward by nt tokens;
188 * nt may be negative (left move).
189 * The row may need to be grown.
190 * Non-strictly conforming because of the (char *), but easily fixed
193 adjustrow(Tokenrow *trp, int nt)
199 size = (trp->lp - trp->bp) + nt;
200 while (size > trp->max)
202 /* nby = sizeof(Token) * (trp->lp - trp->tp); */
203 nby = (char *)trp->lp - (char *)trp->tp;
205 memmove(trp->tp+nt, trp->tp, nby);
210 * Copy a row of tokens into the destination holder, allocating
211 * the space for the contents. Return the destination.
214 copytokenrow(Tokenrow *dtr, Tokenrow *str)
216 int len = rowlen(str);
218 maketokenrow(len, dtr);
219 movetokenrow(dtr, str);
225 * Produce a copy of a row of tokens. Start at trp->tp.
226 * The value strings are copied as well. The first token
230 normtokenrow(Tokenrow *trp)
233 Tokenrow *ntrp = new(Tokenrow);
236 len = trp->lp - trp->tp;
239 maketokenrow(len, ntrp);
240 for (tp=trp->tp; tp < trp->lp; tp++) {
243 ntrp->lp->t = newstring(tp->t, tp->len, 1);
244 *ntrp->lp->t++ = ' ';
250 if (ntrp->lp > ntrp->bp)
259 peektokens(Tokenrow *trp, char *str)
267 fprintf(stderr, "%s ", str);
268 if (tp<trp->bp || tp>trp->lp)
269 fprintf(stderr, "(tp offset %d) ", tp-trp->bp);
270 for (tp=trp->bp; tp<trp->lp && tp<trp->bp+32; tp++) {
274 fprintf(stderr, "%s", tp->t, tp->len);
277 if (tp->type==NAME) {
278 fprintf(stderr, tp==trp->tp?"{*":"{");
279 prhideset(tp->hideset);
280 fprintf(stderr, "} ");
282 fprintf(stderr, tp==trp->tp?"{%x*} ":"{%x} ", tp->type);
284 fprintf(stderr, "\n");
289 puttokens(Tokenrow *trp)
298 for (; tp<trp->lp; tp++) {
299 len = tp->len+tp->wslen;
301 while (tp<trp->lp-1 && p+len == (tp+1)->t - (tp+1)->wslen) {
303 len += tp->wslen+tp->len;
306 if (len>OBS/2) { /* handle giant token */
308 write(1, wbuf, wbp-wbuf);
316 if (wbp >= &wbuf[OBS]) {
318 if (wbp > &wbuf[OBS])
319 memcpy(wbuf, wbuf+OBS, wbp - &wbuf[OBS]);
324 if (cursource->fd==0)
332 write(1, wbuf, wbp-wbuf);
338 * turn a row into just a newline
341 setempty(Tokenrow *trp)
352 outnum(char *p, int n)
361 * allocate and initialize a new string from s, of length l, at offset o
365 newstring(uchar *s, int l, int o)
367 uchar *ns = (uchar *)domalloc(l+o+1);
370 return (uchar*)strncpy((char*)ns+o, (char*)s, l) - o;