font: "ggrp" lines in font descriptions define character groups
[neatroff.git] / ren.c
blob04aea52e994be617317cb9a53bb3bf4a00a1e0c5
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 struct font *f = dev_font(fn);
91 if (font_getcs(f))
92 return font_getcs(f) * SC_EM / 36;
93 return DEVWID(sz, wid) + (font_getbd(f) ? font_getbd(f) - 1 : 0);
96 int f_divreg(void)
98 return cdiv ? cdiv->reg : -1;
101 int f_hpos(void)
103 return fmt_wid(cfmt) + wb_wid(cwb);
106 void tr_divbeg(char **args)
108 odiv_beg();
109 ren_div++;
112 void tr_divend(char **args)
114 if (ren_div <= 0)
115 errdie("neatroff: diversion stack empty\n");
116 odiv_end();
117 ren_div--;
120 static int trap_reg(int pos);
121 static int trap_pos(int pos);
122 static void trap_exec(int reg);
124 static void ren_page(int pg, int force)
126 if (!force && bp_final >= 2)
127 return;
128 n_nl = 0;
129 n_d = 0;
130 n_h = 0;
131 n_pg = pg;
132 bp_next = n_pg + 1;
133 bp_count++;
134 out("p%d\n", pg);
135 out("V%d\n", 0);
136 if (trap_pos(-1) == 0)
137 trap_exec(trap_reg(-1));
140 static int ren_first(void)
142 if (bp_first && !cdiv) {
143 bp_first = 0;
144 ren_page(bp_next, 1);
145 return 0;
147 return 1;
150 /* when nodiv, do not append .sp to diversions */
151 static void ren_sp(int n, int nodiv)
153 ren_first();
154 /* ignore .sp without arguments when reading diversions */
155 if (!n && ren_div && !n_u)
156 return;
157 n_d += n ? n : n_v;
158 if (n_d > n_h)
159 n_h = n_d;
160 if (cdiv && !nodiv) {
161 sbuf_putnl(&cdiv->sbuf);
162 sbuf_printf(&cdiv->sbuf, "%csp %du\n", c_cc, n ? n : n_v);
163 } else {
164 n_nl = n_d;
168 static int render_rec(int level);
169 static void trap_exec(int reg)
171 char cmd[16];
172 int partial = ren_partial && (!ren_un || ren_unbuf[0] != '\n');
173 if (str_get(reg)) {
174 sprintf(cmd, "%c%s %d\n", c_cc, TR_POPREN, ren_level);
175 in_push(cmd, NULL);
176 in_push(str_get(reg), NULL);
177 if (partial)
178 in_push("\n", NULL);
179 render_rec(++ren_level);
180 /* executed the trap while in the middle of an input line */
181 if (partial)
182 fmt_suppressnl(cfmt);
186 static int detect_traps(int beg, int end)
188 int pos = trap_pos(beg);
189 return pos >= 0 && (cdiv || pos < n_p) && pos <= end;
192 /* return 1 if executed a trap */
193 static int ren_traps(int beg, int end, int dosp)
195 int pos = trap_pos(beg);
196 if (detect_traps(beg, end)) {
197 if (dosp && pos > beg)
198 ren_sp(pos - beg, 0);
199 trap_exec(trap_reg(beg));
200 return 1;
202 return 0;
205 static int detect_pagelimit(int ne)
207 return !cdiv && n_nl + ne >= n_p;
210 /* start a new page if needed */
211 static int ren_pagelimit(int ne)
213 if (detect_pagelimit(ne)) {
214 ren_page(bp_next, 0);
215 return 1;
217 return 0;
220 /* return 1 if triggered a trap */
221 static int down(int n)
223 if (ren_traps(n_d, n_d + (n ? n : n_v), 1))
224 return 1;
225 ren_sp(n, 0);
226 return ren_pagelimit(0);
229 /* line adjustment */
230 static int ren_ljust(struct sbuf *spre, int w, int ad, int li, int ll)
232 int ljust = li;
233 int llen = ll - ljust;
234 n_n = w;
235 if ((ad & AD_B) == AD_C)
236 ljust += llen > w ? (llen - w) / 2 : 0;
237 if ((ad & AD_B) == AD_R)
238 ljust += llen - w;
239 if (ljust)
240 sbuf_printf(spre, "%ch'%du'", c_ec, ljust);
241 if (cdiv && cdiv->dl < w + ljust)
242 cdiv->dl = w + ljust;
243 return ljust;
246 /* append the line to the current diversion or send it to out.c */
247 static void ren_out(char *beg, char *mid, char *end)
249 if (cdiv) {
250 sbuf_append(&cdiv->sbuf, beg);
251 sbuf_append(&cdiv->sbuf, mid);
252 sbuf_append(&cdiv->sbuf, end);
253 } else {
254 out("H%d\n", n_o);
255 out("V%d\n", n_d);
256 out_line(beg);
257 out_line(mid);
258 out_line(end);
262 static void ren_transparent(char *s)
264 if (cdiv)
265 sbuf_printf(&cdiv->sbuf, "%s\n", s);
266 else
267 out("%s\n", s);
270 static int zwid(void)
272 struct glyph *g = dev_glyph("0", n_f);
273 return charwid(n_f, n_s, g ? g->wid : 0);
276 /* append the line number to the output line */
277 static void ren_lnum(struct sbuf *spre)
279 char num[16] = "";
280 char dig[16] = "";
281 struct wb wb;
282 int i = 0;
283 wb_init(&wb);
284 if (n_nn <= 0 && (n_ln % n_nM) == 0)
285 sprintf(num, "%d", n_ln);
286 wb_hmov(&wb, n_nI * zwid());
287 if (strlen(num) < 3)
288 wb_hmov(&wb, (3 - strlen(num)) * zwid());
289 while (num[i]) {
290 dig[0] = num[i++];
291 wb_put(&wb, dig);
293 wb_hmov(&wb, n_nS * zwid());
294 sbuf_append(spre, wb_buf(&wb));
295 wb_done(&wb);
296 if (n_nn > 0)
297 n_nn--;
298 else
299 n_ln++;
302 /* append margin character */
303 static void ren_mc(struct sbuf *sbuf, int w, int ljust)
305 struct wb wb;
306 wb_init(&wb);
307 if (w + ljust < n_l + n_mcn)
308 wb_hmov(&wb, n_l + n_mcn - w - ljust);
309 wb_putexpand(&wb, c_mc);
310 sbuf_append(sbuf, wb_buf(&wb));
311 wb_done(&wb);
314 /* process a line and print it with ren_out() */
315 static int ren_line(char *line, int w, int ad, int body,
316 int li, int ll, int els_neg, int els_pos)
318 struct sbuf sbeg, send;
319 int prev_d, lspc, ljust;
320 ren_first();
321 sbuf_init(&sbeg);
322 sbuf_init(&send);
323 lspc = MAX(1, n_L) * n_v; /* line space, ignoreing \x */
324 prev_d = n_d;
325 if (els_neg)
326 ren_sp(-els_neg, 1);
327 if (!n_ns || line[0] || els_neg || els_pos) {
328 ren_sp(0, 0);
329 if (line[0] && n_nm && body)
330 ren_lnum(&sbeg);
331 ljust = ren_ljust(&sbeg, w, ad, li, ll);
332 if (line[0] && body && n_mc)
333 ren_mc(&send, w, ljust);
334 ren_out(sbuf_buf(&sbeg), line, sbuf_buf(&send));
335 n_ns = 0;
337 sbuf_done(&sbeg);
338 sbuf_done(&send);
339 if (els_pos)
340 ren_sp(els_pos, 1);
341 n_a = els_pos;
342 if (detect_traps(prev_d, n_d) || detect_pagelimit(lspc - n_v)) {
343 if (!ren_pagelimit(lspc - n_v))
344 ren_traps(prev_d, n_d, 0);
345 return 1;
347 if (lspc - n_v && down(lspc - n_v))
348 return 1;
349 return 0;
352 /* read a line from fmt and send it to ren_line() */
353 static int ren_passline(struct fmt *fmt)
355 struct sbuf sbuf;
356 int ll, li, els_neg, els_pos, w, ret;
357 int ad = n_j;
358 ren_first();
359 if (!fmt_morewords(fmt))
360 return 0;
361 sbuf_init(&sbuf);
362 fmt_nextline(fmt, &sbuf, &w, &li, &ll, &els_neg, &els_pos);
363 if ((n_cp && !n_u) || n_na)
364 ad = AD_L;
365 if (n_ce)
366 ad = AD_C;
367 ret = ren_line(sbuf_buf(&sbuf), w, ad, 1, li, ll, els_neg, els_pos);
368 sbuf_done(&sbuf);
369 return ret;
372 /* output formatted lines in fmt */
373 static void ren_fmtpop(struct fmt *fmt)
375 while (fmt_morelines(fmt))
376 ren_passline(fmt);
379 /* format and output all lines in fmt */
380 static void ren_fmtpopall(struct fmt *fmt)
382 while (fmt_fill(fmt))
383 ren_fmtpop(fmt);
384 ren_fmtpop(fmt);
387 /* output current line; returns 1 if triggered a trap */
388 static int ren_br(void)
390 ren_fmtpopall(cfmt);
391 while (fmt_br(cfmt))
392 ren_fmtpop(cfmt);
393 return ren_passline(cfmt);
396 void tr_br(char **args)
398 ren_fmtpopall(cfmt); /* output the completed lines first */
399 if (args[0][0] == c_cc)
400 ren_br();
403 void tr_sp(char **args)
405 int traps = 0;
406 int n = args[1] ? eval(args[1], 'v') : n_v;
407 if (args[0][0] == c_cc)
408 traps = ren_br();
409 if (n && !n_ns && !traps)
410 down(n);
413 void tr_sv(char **args)
415 int n = eval(args[1], 'v');
416 n_sv = 0;
417 if (n_d + n < f_nexttrap())
418 down(n);
419 else
420 n_sv = n;
423 void tr_ns(char **args)
425 n_ns = 1;
428 void tr_rs(char **args)
430 n_ns = 0;
433 void tr_os(char **args)
435 if (n_sv)
436 down(n_sv);
437 n_sv = 0;
440 void tr_mk(char **args)
442 if (args[1])
443 num_set(map(args[1]), n_d);
444 else
445 n_mk = n_d;
448 void tr_rt(char **args)
450 int n = args[1] ? eval_re(args[1], n_d, 'v') : n_mk;
451 if (n >= 0 && n < n_d)
452 ren_sp(n - n_d, 0);
455 void tr_ne(char **args)
457 int n = args[1] ? eval(args[1], 'v') : n_v;
458 if (!ren_first())
459 return;
460 if (!ren_traps(n_d, n_d + n - 1, 1))
461 ren_pagelimit(n);
464 static void ren_ejectpage(int br)
466 bp_ejected = bp_count;
467 if (br)
468 ren_br();
469 while (bp_count == bp_ejected && !cdiv) {
470 if (detect_traps(n_d, n_p)) {
471 ren_traps(n_d, n_p, 1);
472 } else {
473 bp_ejected = 0;
474 ren_page(bp_next, 0);
479 void tr_bp(char **args)
481 if (!cdiv && (args[1] || !n_ns)) {
482 if (args[1])
483 bp_next = eval_re(args[1], n_pg, 0);
484 ren_ejectpage(args[0][0] == c_cc);
488 void tr_pn(char **args)
490 if (args[1])
491 bp_next = eval_re(args[1], n_pg, 0);
494 static void ren_ps(char *s)
496 int ps = !s || !*s || !strcmp("0", s) ? n_s0 : eval_re(s, n_s, 0);
497 n_s0 = n_s;
498 n_s = MAX(1, ps);
501 void tr_ps(char **args)
503 ren_ps(args[1]);
506 void tr_ll(char **args)
508 int ll = args[1] ? eval_re(args[1], n_l, 'm') : n_l0;
509 n_l0 = n_l;
510 n_l = MAX(0, ll);
513 void tr_in(char **args)
515 int in = args[1] ? eval_re(args[1], n_i, 'm') : n_i0;
516 if (args[0][0] == c_cc)
517 ren_br();
518 n_i0 = n_i;
519 n_i = MAX(0, in);
520 n_ti = -1;
523 void tr_ti(char **args)
525 if (args[0][0] == c_cc)
526 ren_br();
527 if (args[1])
528 n_ti = eval_re(args[1], n_i, 'm');
531 static void ren_ft(char *s)
533 int fn = !s || !*s || !strcmp("P", s) ? n_f0 : dev_pos(s);
534 if (fn < 0) {
535 errmsg("neatroff: failed to mount <%s>\n", s);
536 } else {
537 n_f0 = n_f;
538 n_f = fn;
542 void tr_ft(char **args)
544 ren_ft(args[1]);
547 void tr_fp(char **args)
549 if (!args[2])
550 return;
551 if (dev_mnt(atoi(args[1]), args[2], args[3] ? args[3] : args[2]) < 0)
552 errmsg("neatroff: failed to mount <%s>\n", args[2]);
555 void tr_nf(char **args)
557 if (args[0][0] == c_cc)
558 ren_br();
559 n_u = 0;
562 void tr_fi(char **args)
564 if (args[0][0] == c_cc)
565 ren_br();
566 n_u = 1;
569 void tr_ce(char **args)
571 if (args[0][0] == c_cc)
572 ren_br();
573 n_ce = args[1] ? atoi(args[1]) : 1;
576 void tr_fc(char **args)
578 char *fa = args[1];
579 char *fb = args[2];
580 if (fa && charread(&fa, c_fa) >= 0) {
581 if (!fb || charread(&fb, c_fb) < 0)
582 strcpy(c_fb, " ");
583 } else {
584 c_fa[0] = '\0';
585 c_fb[0] = '\0';
589 static void ren_cl(char *s)
591 int m = !s || !*s ? n_m0 : clr_get(s);
592 n_m0 = n_m;
593 n_m = m;
596 void tr_cl(char **args)
598 ren_cl(args[1]);
601 void tr_ab(char **args)
603 fprintf(stderr, "%s\n", args[1]);
604 ren_aborted = 1;
607 static void ren_cmd(struct wb *wb, int c, char *arg)
609 switch (c) {
610 case ' ':
611 wb_hmov(wb, N_SS(n_f, n_s));
612 break;
613 case 'b':
614 ren_bcmd(wb, arg);
615 break;
616 case 'c':
617 wb_setpart(wb);
618 break;
619 case 'D':
620 ren_dcmd(wb, arg);
621 break;
622 case 'd':
623 wb_vmov(wb, SC_EM / 2);
624 break;
625 case 'f':
626 ren_ft(arg);
627 break;
628 case 'h':
629 wb_hmov(wb, eval(arg, 'm'));
630 break;
631 case 'k':
632 num_set(map(arg), wb == cwb ? f_hpos() - n_lb : wb_wid(wb));
633 break;
634 case 'L':
635 ren_vlcmd(wb, arg);
636 break;
637 case 'l':
638 ren_hlcmd(wb, arg);
639 break;
640 case 'm':
641 ren_cl(arg);
642 break;
643 case 'o':
644 ren_ocmd(wb, arg);
645 break;
646 case 'p':
647 if (wb == cwb)
648 while (fmt_fillreq(cfmt))
649 ren_fmtpop(cfmt);
650 break;
651 case 'r':
652 wb_vmov(wb, -SC_EM);
653 break;
654 case 's':
655 ren_ps(arg);
656 break;
657 case 'u':
658 wb_vmov(wb, -SC_EM / 2);
659 break;
660 case 'v':
661 wb_vmov(wb, eval(arg, 'v'));
662 break;
663 case 'X':
664 wb_etc(wb, arg);
665 break;
666 case 'x':
667 wb_els(wb, eval(arg, 'v'));
668 break;
669 case 'Z':
670 ren_zcmd(wb, arg);
671 break;
672 case '0':
673 wb_hmov(wb, zwid());
674 break;
675 case '|':
676 wb_hmov(wb, SC_EM / 6);
677 break;
678 case '&':
679 wb_hmov(wb, 0);
680 break;
681 case '^':
682 wb_hmov(wb, SC_EM / 12);
683 break;
684 case '/':
685 wb_italiccorrection(wb);
686 break;
687 case ',':
688 wb_italiccorrectionleft(wb);
689 break;
690 case '{':
691 case '}':
692 break;
696 static void ren_field(struct wb *wb, int (*next)(void), void (*back)(int));
697 static void ren_tab(struct wb *wb, char *tc, int (*next)(void), void (*back)(int));
699 /* insert a character, escape sequence, field or etc into wb */
700 static void ren_put(struct wb *wb, char *c, int (*next)(void), void (*back)(int))
702 char arg[ILNLEN];
703 char *s;
704 int w, n;
705 if (c[0] == ' ' || c[0] == '\n') {
706 wb_put(wb, c);
707 return;
709 if (c[0] == '\t' || c[0] == '\x01') {
710 ren_tab(wb, c[0] == '\t' ? c_tc : c_lc, next, back);
711 return;
713 if (c_fa[0] && !strcmp(c_fa, c)) {
714 ren_field(wb, next, back);
715 return;
717 if (c[0] == c_ec) {
718 if (c[1] == 'z') {
719 w = wb_wid(wb);
720 ren_char(wb, next, back);
721 wb_hmov(wb, w - wb_wid(wb));
722 return;
724 if (c[1] == '!') {
725 if (ren_nl && next == ren_next) {
726 s = arg;
727 n = next();
728 while (n >= 0 && n != '\n') {
729 *s++ = n;
730 n = next();
732 *s = '\0';
733 ren_transparent(arg);
735 return;
737 if (strchr(" bCcDdefHhkLlmNoprSsuvXxZz0^|{}&/,", c[1])) {
738 arg[0] = '\0';
739 if (strchr(ESC_P, c[1]))
740 unquotednext(arg, c[1], next, back);
741 if (strchr(ESC_Q, c[1]))
742 quotednext(arg, next, back);
743 if (c[1] == 'e') {
744 snprintf(c, GNLEN, "%c%c", c_ec, c_ec);
745 } else if (c[1] == 'N') {
746 snprintf(c, GNLEN, "GID=%s", arg);
747 } else {
748 ren_cmd(wb, c[1], arg);
749 return;
753 if (ren_div) {
754 wb_putraw(wb, c);
755 return;
757 if (cdef_map(c, n_f)) /* .char characters */
758 wb_putexpand(wb, c);
759 else
760 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(wb_buf(&wb), 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 (!ren_un && !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;