char: break escape sequence reading functions
[neatroff.git] / ren.c
blob053df973f8ecde15a3f623e07697cb074892f1e9
1 /* rendering lines and managing traps */
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "roff.h"
8 #define cfmt env_fmt() /* current formatter */
9 #define cwb env_wb() /* current word buffer */
11 /* diversions */
12 struct div {
13 struct sbuf sbuf; /* diversion output */
14 int reg; /* diversion register */
15 int tpos; /* diversion trap position */
16 int treg; /* diversion trap register */
17 int dl; /* diversion width */
18 int prev_d; /* previous \n(.d value */
19 int prev_h; /* previous \n(.h value */
20 int prev_mk; /* previous .mk internal register */
21 int prev_ns; /* previous .ns value */
23 static struct div divs[NPREV]; /* diversion stack */
24 static struct div *cdiv; /* current diversion */
25 static int ren_div; /* rendering a diversion */
26 static int trap_em = -1; /* end macro */
28 static int ren_nl; /* just after a newline */
29 static int ren_partial; /* reading an input line in render_rec() */
30 static int ren_unbuf[8]; /* ren_back() buffer */
31 static int ren_un;
32 static int ren_aborted; /* .ab executed */
34 static int bp_first = 1; /* prior to the first page */
35 static int bp_next = 1; /* next page number */
36 static int bp_count; /* number of pages so far */
37 static int bp_ejected; /* current ejected page */
38 static int bp_final; /* 1: executing em, 2: the final page, 3: the 2nd final page */
39 static int ren_level; /* the depth of render_rec() calls */
41 static char c_fa[GNLEN]; /* field delimiter */
42 static char c_fb[GNLEN]; /* field padding */
44 static int ren_next(void)
46 return ren_un > 0 ? ren_unbuf[--ren_un] : tr_next();
49 static void ren_back(int c)
51 ren_unbuf[ren_un++] = c;
54 void tr_di(char **args)
56 if (args[1]) {
57 cdiv = cdiv ? cdiv + 1 : divs;
58 memset(cdiv, 0, sizeof(*cdiv));
59 sbuf_init(&cdiv->sbuf);
60 cdiv->reg = map(args[1]);
61 cdiv->treg = -1;
62 if (args[0][2] == 'a' && str_get(cdiv->reg)) /* .da */
63 sbuf_append(&cdiv->sbuf, str_get(cdiv->reg));
64 sbuf_printf(&cdiv->sbuf, "%c%s\n", c_cc, TR_DIVBEG);
65 cdiv->prev_d = n_d;
66 cdiv->prev_h = n_h;
67 cdiv->prev_mk = n_mk;
68 cdiv->prev_ns = n_ns;
69 n_d = 0;
70 n_h = 0;
71 n_mk = 0;
72 n_ns = 0;
73 } else if (cdiv) {
74 sbuf_putnl(&cdiv->sbuf);
75 sbuf_printf(&cdiv->sbuf, "%c%s\n", c_cc, TR_DIVEND);
76 str_set(cdiv->reg, sbuf_buf(&cdiv->sbuf));
77 sbuf_done(&cdiv->sbuf);
78 n_dl = cdiv->dl;
79 n_dn = n_d;
80 n_d = cdiv->prev_d;
81 n_h = cdiv->prev_h;
82 n_mk = cdiv->prev_mk;
83 n_ns = cdiv->prev_ns;
84 cdiv = cdiv > divs ? cdiv - 1 : NULL;
88 int charwid(int fn, int sz, int wid)
90 if (dev_getcs(fn))
91 return dev_getcs(n_f) * SC_EM / 36;
92 return DEVWID(sz, wid) + (dev_getbd(fn) ? dev_getbd(fn) - 1 : 0);
95 int f_divreg(void)
97 return cdiv ? cdiv->reg : -1;
100 int f_hpos(void)
102 return fmt_wid(cfmt) + wb_wid(cwb);
105 void tr_divbeg(char **args)
107 odiv_beg();
108 ren_div++;
111 void tr_divend(char **args)
113 if (ren_div <= 0)
114 errdie("neatroff: diversion stack empty\n");
115 odiv_end();
116 ren_div--;
119 static int trap_reg(int pos);
120 static int trap_pos(int pos);
121 static void trap_exec(int reg);
123 static void ren_page(int pg, int force)
125 if (!force && bp_final >= 2)
126 return;
127 n_nl = 0;
128 n_d = 0;
129 n_h = 0;
130 n_pg = pg;
131 bp_next = n_pg + 1;
132 bp_count++;
133 out("p%d\n", pg);
134 out("V%d\n", 0);
135 if (trap_pos(-1) == 0)
136 trap_exec(trap_reg(-1));
139 static int ren_first(void)
141 if (bp_first && !cdiv) {
142 bp_first = 0;
143 ren_page(bp_next, 1);
144 return 0;
146 return 1;
149 /* when nodiv, do not append .sp to diversions */
150 static void ren_sp(int n, int nodiv)
152 ren_first();
153 /* ignore .sp without arguments when reading diversions */
154 if (!n && ren_div && !n_u)
155 return;
156 n_d += n ? n : n_v;
157 if (n_d > n_h)
158 n_h = n_d;
159 if (cdiv && !nodiv) {
160 sbuf_putnl(&cdiv->sbuf);
161 sbuf_printf(&cdiv->sbuf, "%csp %du\n", c_cc, n ? n : n_v);
162 } else {
163 n_nl = n_d;
167 static int render_rec(int level);
168 static void trap_exec(int reg)
170 char cmd[16];
171 int partial = ren_partial;
172 if (str_get(reg)) {
173 sprintf(cmd, "%c%s %d\n", c_cc, TR_POPREN, ren_level);
174 in_push(cmd, NULL);
175 in_push(str_get(reg), NULL);
176 if (partial)
177 in_push("\n", NULL);
178 render_rec(++ren_level);
179 /* executed the trap while in the middle of an input line */
180 if (partial)
181 fmt_suppressnl(cfmt);
185 static int detect_traps(int beg, int end)
187 int pos = trap_pos(beg);
188 return pos >= 0 && (cdiv || pos < n_p) && pos <= end;
191 /* return 1 if executed a trap */
192 static int ren_traps(int beg, int end, int dosp)
194 int pos = trap_pos(beg);
195 if (detect_traps(beg, end)) {
196 if (dosp && pos > beg)
197 ren_sp(pos - beg, 0);
198 trap_exec(trap_reg(beg));
199 return 1;
201 return 0;
204 static int detect_pagelimit(int ne)
206 return !cdiv && n_nl + ne >= n_p;
209 /* start a new page if needed */
210 static int ren_pagelimit(int ne)
212 if (detect_pagelimit(ne)) {
213 ren_page(bp_next, 0);
214 return 1;
216 return 0;
219 /* return 1 if triggered a trap */
220 static int down(int n)
222 if (ren_traps(n_d, n_d + (n ? n : n_v), 1))
223 return 1;
224 ren_sp(n, 0);
225 return ren_pagelimit(0);
228 /* line adjustment */
229 static int ren_ljust(struct sbuf *spre, int w, int ad, int li, int ll)
231 int ljust = li;
232 int llen = ll - ljust;
233 n_n = w;
234 if ((ad & AD_B) == AD_C)
235 ljust += llen > w ? (llen - w) / 2 : 0;
236 if ((ad & AD_B) == AD_R)
237 ljust += llen - w;
238 if (ljust)
239 sbuf_printf(spre, "%ch'%du'", c_ec, ljust);
240 if (cdiv && cdiv->dl < w + ljust)
241 cdiv->dl = w + ljust;
242 return ljust;
245 /* append the line to the current diversion or send it to out.c */
246 static void ren_out(char *beg, char *mid, char *end)
248 if (cdiv) {
249 sbuf_append(&cdiv->sbuf, beg);
250 sbuf_append(&cdiv->sbuf, mid);
251 sbuf_append(&cdiv->sbuf, end);
252 } else {
253 out("H%d\n", n_o);
254 out("V%d\n", n_d);
255 out_line(beg);
256 out_line(mid);
257 out_line(end);
261 static void ren_transparent(char *s)
263 if (cdiv)
264 sbuf_printf(&cdiv->sbuf, "%s\n", s);
265 else
266 out("%s\n", s);
269 static int zwid(void)
271 struct glyph *g = dev_glyph("0", n_f);
272 return charwid(n_f, n_s, g ? g->wid : 0);
275 /* append the line number to the output line */
276 static void ren_lnum(struct sbuf *spre)
278 char num[16] = "";
279 char dig[16] = "";
280 struct wb wb;
281 int i = 0;
282 wb_init(&wb);
283 if (n_nn <= 0 && (n_ln % n_nM) == 0)
284 sprintf(num, "%d", n_ln);
285 wb_hmov(&wb, n_nI * zwid());
286 if (strlen(num) < 3)
287 wb_hmov(&wb, (3 - strlen(num)) * zwid());
288 while (num[i]) {
289 dig[0] = num[i++];
290 wb_put(&wb, dig);
292 wb_hmov(&wb, n_nS * zwid());
293 sbuf_append(spre, sbuf_buf(&wb.sbuf));
294 wb_done(&wb);
295 if (n_nn > 0)
296 n_nn--;
297 else
298 n_ln++;
301 /* append margin character */
302 static void ren_mc(struct sbuf *sbuf, int w, int ljust)
304 struct wb wb;
305 wb_init(&wb);
306 if (w + ljust < n_l + n_mcn)
307 wb_hmov(&wb, n_l + n_mcn - w - ljust);
308 wb_putexpand(&wb, c_mc);
309 sbuf_append(sbuf, sbuf_buf(&wb.sbuf));
310 wb_done(&wb);
313 /* process a line and print it with ren_out() */
314 static int ren_line(char *line, int w, int ad, int body,
315 int li, int ll, int els_neg, int els_pos)
317 struct sbuf sbeg, send;
318 int prev_d, lspc, ljust;
319 ren_first();
320 sbuf_init(&sbeg);
321 sbuf_init(&send);
322 lspc = MAX(1, n_L) * n_v; /* line space, ignoreing \x */
323 prev_d = n_d;
324 if (els_neg)
325 ren_sp(-els_neg, 1);
326 if (!n_ns || line[0] || els_neg || els_pos) {
327 ren_sp(0, 0);
328 if (line[0] && n_nm && body)
329 ren_lnum(&sbeg);
330 ljust = ren_ljust(&sbeg, w, ad, li, ll);
331 if (line[0] && body && n_mc)
332 ren_mc(&send, w, ljust);
333 ren_out(sbuf_buf(&sbeg), line, sbuf_buf(&send));
334 n_ns = 0;
336 sbuf_done(&sbeg);
337 sbuf_done(&send);
338 if (els_pos)
339 ren_sp(els_pos, 1);
340 n_a = els_pos;
341 if (detect_traps(prev_d, n_d) || detect_pagelimit(lspc - n_v)) {
342 if (!ren_pagelimit(lspc - n_v))
343 ren_traps(prev_d, n_d, 0);
344 return 1;
346 if (lspc - n_v && down(lspc - n_v))
347 return 1;
348 return 0;
351 /* read a line from fmt and send it to ren_line() */
352 static int ren_passline(struct fmt *fmt)
354 struct sbuf sbuf;
355 int ll, li, els_neg, els_pos, w, ret;
356 int ad = n_j;
357 ren_first();
358 if (!fmt_morewords(fmt))
359 return 0;
360 sbuf_init(&sbuf);
361 fmt_nextline(fmt, &sbuf, &w, &li, &ll, &els_neg, &els_pos);
362 if ((n_cp && !n_u) || n_na)
363 ad = AD_L;
364 if (n_ce)
365 ad = AD_C;
366 ret = ren_line(sbuf_buf(&sbuf), w, ad, 1, li, ll, els_neg, els_pos);
367 sbuf_done(&sbuf);
368 return ret;
371 /* output formatted lines in fmt */
372 static void ren_fmtpop(struct fmt *fmt)
374 while (fmt_morelines(fmt))
375 ren_passline(fmt);
378 /* format and output all lines in fmt */
379 static void ren_fmtpopall(struct fmt *fmt)
381 while (fmt_fill(fmt))
382 ren_fmtpop(fmt);
383 ren_fmtpop(fmt);
386 /* output current line; returns 1 if triggered a trap */
387 static int ren_br(void)
389 ren_fmtpopall(cfmt);
390 while (fmt_br(cfmt))
391 ren_fmtpop(cfmt);
392 return ren_passline(cfmt);
395 void tr_br(char **args)
397 ren_fmtpopall(cfmt); /* output the completed lines first */
398 if (args[0][0] == c_cc)
399 ren_br();
402 void tr_sp(char **args)
404 int traps = 0;
405 int n = args[1] ? eval(args[1], 'v') : n_v;
406 if (args[0][0] == c_cc)
407 traps = ren_br();
408 if (n && !n_ns && !traps)
409 down(n);
412 void tr_sv(char **args)
414 int n = eval(args[1], 'v');
415 n_sv = 0;
416 if (n_d + n < f_nexttrap())
417 down(n);
418 else
419 n_sv = n;
422 void tr_ns(char **args)
424 n_ns = 1;
427 void tr_rs(char **args)
429 n_ns = 0;
432 void tr_os(char **args)
434 if (n_sv)
435 down(n_sv);
436 n_sv = 0;
439 void tr_mk(char **args)
441 if (args[1])
442 num_set(map(args[1]), n_d);
443 else
444 n_mk = n_d;
447 void tr_rt(char **args)
449 int n = args[1] ? eval_re(args[1], n_d, 'v') : n_mk;
450 if (n >= 0 && n < n_d)
451 ren_sp(n - n_d, 0);
454 void tr_ne(char **args)
456 int n = args[1] ? eval(args[1], 'v') : n_v;
457 if (!ren_first())
458 return;
459 if (!ren_traps(n_d, n_d + n - 1, 1))
460 ren_pagelimit(n);
463 static void ren_ejectpage(int br)
465 bp_ejected = bp_count;
466 if (br)
467 ren_br();
468 while (bp_count == bp_ejected && !cdiv) {
469 if (detect_traps(n_d, n_p)) {
470 ren_traps(n_d, n_p, 1);
471 } else {
472 bp_ejected = 0;
473 ren_page(bp_next, 0);
478 void tr_bp(char **args)
480 if (!cdiv && (args[1] || !n_ns)) {
481 if (args[1])
482 bp_next = eval_re(args[1], n_pg, 0);
483 ren_ejectpage(args[0][0] == c_cc);
487 void tr_pn(char **args)
489 if (args[1])
490 bp_next = eval_re(args[1], n_pg, 0);
493 static void ren_ps(char *s)
495 int ps = !s || !*s || !strcmp("0", s) ? n_s0 : eval_re(s, n_s, 0);
496 n_s0 = n_s;
497 n_s = MAX(1, ps);
500 void tr_ps(char **args)
502 ren_ps(args[1]);
505 void tr_ll(char **args)
507 int ll = args[1] ? eval_re(args[1], n_l, 'm') : n_l0;
508 n_l0 = n_l;
509 n_l = MAX(0, ll);
512 void tr_in(char **args)
514 int in = args[1] ? eval_re(args[1], n_i, 'm') : n_i0;
515 if (args[0][0] == c_cc)
516 ren_br();
517 n_i0 = n_i;
518 n_i = MAX(0, in);
519 n_ti = -1;
522 void tr_ti(char **args)
524 if (args[0][0] == c_cc)
525 ren_br();
526 if (args[1])
527 n_ti = eval_re(args[1], n_i, 'm');
530 static void ren_ft(char *s)
532 int fn = !s || !*s || !strcmp("P", s) ? n_f0 : dev_pos(s);
533 if (fn < 0) {
534 errmsg("neatroff: failed to mount <%s>\n", s);
535 } else {
536 n_f0 = n_f;
537 n_f = fn;
541 void tr_ft(char **args)
543 ren_ft(args[1]);
546 void tr_fp(char **args)
548 if (!args[2])
549 return;
550 if (dev_mnt(atoi(args[1]), args[2], args[3] ? args[3] : args[2]) < 0)
551 errmsg("neatroff: failed to mount <%s>\n", args[2]);
554 void tr_nf(char **args)
556 if (args[0][0] == c_cc)
557 ren_br();
558 n_u = 0;
561 void tr_fi(char **args)
563 if (args[0][0] == c_cc)
564 ren_br();
565 n_u = 1;
568 void tr_ce(char **args)
570 if (args[0][0] == c_cc)
571 ren_br();
572 n_ce = args[1] ? atoi(args[1]) : 1;
575 void tr_fc(char **args)
577 char *fa = args[1];
578 char *fb = args[2];
579 if (fa && charread(&fa, c_fa) >= 0) {
580 if (!fb || charread(&fb, c_fb) < 0)
581 strcpy(c_fb, " ");
582 } else {
583 c_fa[0] = '\0';
584 c_fb[0] = '\0';
588 static void ren_cl(char *s)
590 int m = !s || !*s ? n_m0 : clr_get(s);
591 n_m0 = n_m;
592 n_m = m;
595 void tr_cl(char **args)
597 ren_cl(args[1]);
600 void tr_ab(char **args)
602 fprintf(stderr, "%s\n", args[1]);
603 ren_aborted = 1;
606 static void ren_cmd(struct wb *wb, int c, char *arg)
608 switch (c) {
609 case ' ':
610 wb_hmov(wb, N_SS(n_f, n_s));
611 break;
612 case 'b':
613 ren_bcmd(wb, arg);
614 break;
615 case 'c':
616 wb_setpart(wb);
617 break;
618 case 'D':
619 ren_dcmd(wb, arg);
620 break;
621 case 'd':
622 wb_vmov(wb, SC_EM / 2);
623 break;
624 case 'f':
625 ren_ft(arg);
626 break;
627 case 'h':
628 wb_hmov(wb, eval(arg, 'm'));
629 break;
630 case 'k':
631 num_set(map(arg), wb == cwb ? f_hpos() - n_lb : wb_wid(wb));
632 break;
633 case 'L':
634 ren_vlcmd(wb, arg);
635 break;
636 case 'l':
637 ren_hlcmd(wb, arg);
638 break;
639 case 'm':
640 ren_cl(arg);
641 break;
642 case 'o':
643 ren_ocmd(wb, arg);
644 break;
645 case 'p':
646 if (wb == cwb)
647 while (fmt_fillreq(cfmt))
648 ren_fmtpop(cfmt);
649 break;
650 case 'r':
651 wb_vmov(wb, -SC_EM);
652 break;
653 case 's':
654 ren_ps(arg);
655 break;
656 case 'u':
657 wb_vmov(wb, -SC_EM / 2);
658 break;
659 case 'v':
660 wb_vmov(wb, eval(arg, 'v'));
661 break;
662 case 'X':
663 wb_etc(wb, arg);
664 break;
665 case 'x':
666 wb_els(wb, eval(arg, 'v'));
667 break;
668 case 'Z':
669 ren_zcmd(wb, arg);
670 break;
671 case '0':
672 wb_hmov(wb, zwid());
673 break;
674 case '|':
675 wb_hmov(wb, SC_EM / 6);
676 break;
677 case '&':
678 wb_hmov(wb, 0);
679 break;
680 case '^':
681 wb_hmov(wb, SC_EM / 12);
682 break;
683 case '/':
684 wb_italiccorrection(wb);
685 break;
686 case ',':
687 wb_italiccorrectionleft(wb);
688 break;
689 case '{':
690 case '}':
691 break;
695 static void ren_field(struct wb *wb, int (*next)(void), void (*back)(int));
696 static void ren_tab(struct wb *wb, char *tc, int (*next)(void), void (*back)(int));
698 /* insert a character, escape sequence, field or etc into wb */
699 static void ren_put(struct wb *wb, char *c, int (*next)(void), void (*back)(int))
701 char arg[ILNLEN];
702 char *s;
703 int w, n;
704 if (c[0] == ' ' || c[0] == '\n') {
705 wb_put(wb, c);
706 return;
708 if (c[0] == '\t' || c[0] == '\x01') {
709 ren_tab(wb, c[0] == '\t' ? c_tc : c_lc, next, back);
710 return;
712 if (c_fa[0] && !strcmp(c_fa, c)) {
713 ren_field(wb, next, back);
714 return;
716 if (c[0] == c_ec) {
717 if (c[1] == 'z') {
718 w = wb_wid(wb);
719 ren_char(wb, next, back);
720 wb_hmov(wb, w - wb_wid(wb));
721 return;
723 if (c[1] == '!') {
724 if (ren_nl && next == ren_next) {
725 s = arg;
726 n = next();
727 while (n >= 0 && n != '\n') {
728 *s++ = n;
729 n = next();
731 *s = '\0';
732 ren_transparent(arg);
734 return;
736 if (strchr(" bCcDdefHhkLlmNoprSsuvXxZz0^|{}&/,", c[1])) {
737 arg[0] = '\0';
738 if (strchr(ESC_P, c[1]))
739 unquotednext(arg, c[1], next, back);
740 if (strchr(ESC_Q, c[1]))
741 quotednext(arg, next, back);
742 if (c[1] == 'e') {
743 snprintf(c, GNLEN, "%c%c", c_ec, c_ec);
744 } else if (c[1] == 'N') {
745 snprintf(c, GNLEN, "GID=%s", arg);
746 } else {
747 ren_cmd(wb, c[1], arg);
748 return;
752 if (!ren_div && cdef_map(c, n_f)) { /* .char characters */
753 wb_putexpand(wb, c);
754 return;
756 if (!n_lg || ren_div || wb_lig(wb, c)) {
757 if (n_kn && !ren_div)
758 wb_kern(wb, c);
759 wb_put(wb, c);
763 /* read one character and place it inside wb buffer */
764 int ren_char(struct wb *wb, int (*next)(void), void (*back)(int))
766 char c[GNLEN * 4];
767 if (charnext(c, next, back) < 0)
768 return -1;
769 ren_put(wb, c, next, back);
770 return 0;
773 /* like ren_char(); return 1 if d1 was read and 2 if d2 was read */
774 static int ren_chardel(struct wb *wb, int (*next)(void), void (*back)(int),
775 char *d1, char *d2)
777 char c[GNLEN * 4];
778 if (charnext(c, next, back) < 0)
779 return -1;
780 if (d1 && !strcmp(d1, c))
781 return 1;
782 if (d2 && !strcmp(d2, c))
783 return 2;
784 ren_put(wb, c, next, back);
785 return 0;
788 /* read the argument of \w and push its width */
789 int ren_wid(int (*next)(void), void (*back)(int))
791 char delim[GNLEN];
792 int c, n;
793 struct wb wb;
794 wb_init(&wb);
795 charnext(delim, next, back);
796 odiv_beg();
797 c = next();
798 while (c >= 0 && c != '\n') {
799 back(c);
800 if (ren_chardel(&wb, next, back, delim, NULL))
801 break;
802 c = next();
804 odiv_end();
805 n = wb_wid(&wb);
806 wb_wconf(&wb, &n_ct, &n_st, &n_sb, &n_llx, &n_lly, &n_urx, &n_ury);
807 wb_done(&wb);
808 return n;
811 /* return 1 if d1 was read and 2 if d2 was read */
812 static int ren_until(struct wb *wb, char *d1, char *d2,
813 int (*next)(void), void (*back)(int))
815 int c, ret;
816 c = next();
817 while (c >= 0 && c != '\n') {
818 back(c);
819 ret = ren_chardel(wb, next, back, d1, d2);
820 if (ret)
821 return ret;
822 c = next();
824 if (c == '\n')
825 back(c);
826 return 0;
829 static void wb_cpy(struct wb *dst, struct wb *src, int left)
831 wb_hmov(dst, left - wb_wid(dst));
832 wb_cat(dst, src);
835 void ren_tl(int (*next)(void), void (*back)(int))
837 struct wb wb, wb2;
838 char delim[GNLEN];
839 wb_init(&wb);
840 wb_init(&wb2);
841 charnext(delim, next, back);
842 /* the left-adjusted string */
843 ren_until(&wb2, delim, NULL, next, back);
844 wb_cpy(&wb, &wb2, 0);
845 /* the centered string */
846 ren_until(&wb2, delim, NULL, next, back);
847 wb_cpy(&wb, &wb2, (n_lt - wb_wid(&wb2)) / 2);
848 /* the right-adjusted string */
849 ren_until(&wb2, delim, NULL, next, back);
850 wb_cpy(&wb, &wb2, n_lt - wb_wid(&wb2));
851 /* flushing the line */
852 ren_line(sbuf_buf(&wb.sbuf), wb_wid(&wb), AD_L, 0,
853 0, n_lt, wb.els_neg, wb.els_pos);
854 wb_done(&wb2);
855 wb_done(&wb);
858 static void ren_field(struct wb *wb, int (*next)(void), void (*back)(int))
860 struct wb wbs[NFIELDS];
861 int i, n = 0;
862 int wid = 0;
863 int left, right, cur_left;
864 int pad, rem;
865 while (n < LEN(wbs)) {
866 wb_init(&wbs[n]);
867 if (ren_until(&wbs[n++], c_fb, c_fa, next, back) != 1)
868 break;
870 left = wb == cwb ? f_hpos() : wb_wid(wb);
871 right = tab_next(left);
872 for (i = 0; i < n; i++)
873 wid += wb_wid(&wbs[i]);
874 pad = (right - left - wid) / (n > 1 ? n - 1 : 1);
875 rem = (right - left - wid) % (n > 1 ? n - 1 : 1);
876 for (i = 0; i < n; i++) {
877 if (i == 0)
878 cur_left = left;
879 else if (i == n - 1)
880 cur_left = right - wb_wid(&wbs[i]);
881 else
882 cur_left = wb_wid(wb) + pad + (i + rem >= n);
883 wb_cpy(wb, &wbs[i], cur_left);
884 wb_done(&wbs[i]);
888 static void ren_tab(struct wb *wb, char *tc, int (*next)(void), void (*back)(int))
890 struct wb t;
891 int pos = wb == cwb ? f_hpos() : wb_wid(wb);
892 int ins = tab_next(pos); /* insertion position */
893 int typ = tab_type(pos); /* tab type */
894 int c;
895 wb_init(&t);
896 if (typ == 'R' || typ == 'C') {
897 c = next();
898 while (c >= 0 && c != '\n' && c != '\t' && c != '\x01') {
899 back(c);
900 ren_char(&t, next, back);
901 c = next();
903 back(c);
905 if (typ == 'C')
906 ins -= wb_wid(&t) / 2;
907 if (typ == 'R')
908 ins -= wb_wid(&t);
909 if (!tc[0] || ins <= pos)
910 wb_hmov(wb, ins - pos);
911 else
912 ren_hline(wb, ins - pos, tc);
913 wb_cat(wb, &t);
914 wb_done(&t);
917 /* parse characters and troff requests of s and append them to wb */
918 int ren_parse(struct wb *wb, char *s)
920 int c;
921 odiv_beg();
922 sstr_push(s);
923 c = sstr_next();
924 while (c >= 0) {
925 sstr_back(c);
926 if (ren_char(wb, sstr_next, sstr_back))
927 break;
928 c = sstr_next();
930 sstr_pop();
931 odiv_end();
932 return 0;
935 /* cause nested render_rec() to exit */
936 void tr_popren(char **args)
938 ren_level = args[1] ? atoi(args[1]) : 0;
941 #define FMT_PAR() (n_u && !n_na && !n_ce && (n_j & AD_P) == AD_P)
943 /* read characters from tr.c and pass the rendered lines to out.c */
944 static int render_rec(int level)
946 int c;
947 while (ren_level >= level) {
948 while (!tr_nextreq())
949 if (ren_level < level)
950 break;
951 if (ren_level < level)
952 break;
953 if (ren_aborted)
954 return 1;
955 c = ren_next();
956 if (c < 0) {
957 if (bp_final >= 2)
958 break;
959 if (bp_final == 0) {
960 bp_final = 1;
961 ren_fmtpopall(cfmt);
962 if (trap_em >= 0)
963 trap_exec(trap_em);
964 } else {
965 bp_final = 2;
966 ren_ejectpage(1);
969 if (c >= 0)
970 ren_partial = c != '\n';
971 /* add cwb (the current word) to cfmt */
972 if (c == ' ' || c == '\n') {
973 if (!wb_part(cwb)) { /* not after a \c */
974 while (fmt_word(cfmt, cwb))
975 ren_fmtpop(cfmt);
976 wb_reset(cwb);
977 if (c == '\n')
978 while (fmt_newline(cfmt))
979 ren_fmtpop(cfmt);
980 if (!FMT_PAR())
981 ren_fmtpopall(cfmt);
982 if (c == ' ')
983 fmt_space(cfmt);
986 /* flush the line if necessary */
987 if ((!FMT_PAR() && (c == ' ' || c == '\n')) || c < 0)
988 ren_fmtpop(cfmt);
989 if (c == '\n' || ren_nl) /* end or start of input line */
990 n_lb = f_hpos();
991 if (c == '\n' && n_it && --n_itn == 0)
992 trap_exec(n_it);
993 if (c == '\n' && !wb_part(cwb))
994 n_ce = MAX(0, n_ce - 1);
995 if (c != ' ' && c >= 0) {
996 ren_back(c);
997 ren_char(cwb, ren_next, ren_back);
999 if (c >= 0)
1000 ren_nl = c == '\n';
1002 return 0;
1005 /* render input words */
1006 int render(void)
1008 n_nl = -1;
1009 while (!tr_nextreq())
1011 ren_first(); /* transition to the first page */
1012 render_rec(0);
1013 bp_final = 3;
1014 if (fmt_morewords(cfmt))
1015 ren_page(bp_next, 1);
1016 ren_br();
1017 return 0;
1020 /* trap handling */
1022 #define tposval(i) (tpos[i] < 0 ? n_p + tpos[i] : tpos[i])
1024 static int tpos[NTRAPS]; /* trap positions */
1025 static int treg[NTRAPS]; /* trap registers */
1026 static int ntraps;
1028 static int trap_first(int pos)
1030 int best = -1;
1031 int i;
1032 for (i = 0; i < ntraps; i++)
1033 if (treg[i] >= 0 && tposval(i) > pos)
1034 if (best < 0 || tposval(i) < tposval(best))
1035 best = i;
1036 return best;
1039 static int trap_byreg(int reg)
1041 int i;
1042 for (i = 0; i < ntraps; i++)
1043 if (treg[i] == reg)
1044 return i;
1045 return -1;
1048 static int trap_bypos(int reg, int pos)
1050 int i;
1051 for (i = 0; i < ntraps; i++)
1052 if (treg[i] >= 0 && tposval(i) == pos)
1053 if (reg == -1 || treg[i] == reg)
1054 return i;
1055 return -1;
1058 void tr_wh(char **args)
1060 int reg, pos, id;
1061 if (!args[1])
1062 return;
1063 pos = eval(args[1], 'v');
1064 id = trap_bypos(-1, pos);
1065 if (!args[2]) {
1066 if (id >= 0)
1067 treg[id] = -1;
1068 return;
1070 reg = map(args[2]);
1071 if (id < 0)
1072 id = trap_byreg(-1);
1073 if (id < 0)
1074 id = ntraps++;
1075 tpos[id] = pos;
1076 treg[id] = reg;
1079 void tr_ch(char **args)
1081 int reg;
1082 int id;
1083 if (!args[1])
1084 return;
1085 reg = map(args[1]);
1086 id = trap_byreg(reg);
1087 if (id >= 0)
1088 tpos[id] = args[2] ? eval(args[2], 'v') : -1;
1091 void tr_dt(char **args)
1093 if (!cdiv)
1094 return;
1095 if (args[2]) {
1096 cdiv->tpos = eval(args[1], 'v');
1097 cdiv->treg = map(args[2]);
1098 } else {
1099 cdiv->treg = -1;
1103 void tr_em(char **args)
1105 trap_em = args[1] ? map(args[1]) : -1;
1108 static int trap_pos(int pos)
1110 int ret = trap_first(pos);
1111 if (bp_final >= 3)
1112 return -1;
1113 if (cdiv)
1114 return cdiv->treg && cdiv->tpos > pos ? cdiv->tpos : -1;
1115 return ret >= 0 ? tposval(ret) : -1;
1118 static int trap_reg(int pos)
1120 int ret = trap_first(pos);
1121 if (cdiv)
1122 return cdiv->treg && cdiv->tpos > pos ? cdiv->treg : -1;
1123 return ret >= 0 ? treg[ret] : -1;
1126 int f_nexttrap(void)
1128 int pos = trap_pos(n_d);
1129 if (cdiv)
1130 return pos >= 0 ? pos : 0x7fffffff;
1131 return (pos >= 0 && pos < n_p ? pos : n_p) - n_d;