ren: flush the partial line before computing .sp absolute positions
[neatroff.git] / ren.c
blob220b4cc07bf5a6a276d7edba53e611105f2af2a4
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 NOPAGE 0x40000000 /* undefined bp_next */
9 #define cfmt env_fmt() /* current formatter */
10 #define cwb env_wb() /* current word buffer */
12 /* diversions */
13 struct div {
14 struct sbuf sbuf; /* diversion output */
15 int reg; /* diversion register */
16 int tpos; /* diversion trap position */
17 int treg; /* diversion trap register */
18 int dl; /* diversion width */
19 int prev_d; /* previous \n(.d value */
20 int prev_h; /* previous \n(.h value */
21 int prev_mk; /* previous .mk internal register */
22 int prev_ns; /* previous .ns value */
24 static struct div divs[NPREV]; /* diversion stack */
25 static struct div *cdiv; /* current diversion */
26 static int ren_div; /* rendering a diversion */
27 static int ren_divvs; /* the amount of .v in diversions */
28 static int trap_em = -1; /* end macro */
30 static int ren_nl; /* just after a newline */
31 static int ren_partial; /* reading an input line in render_rec() */
32 static int ren_unbuf[8]; /* ren_back() buffer */
33 static int ren_un;
34 static int ren_aborted; /* .ab executed */
36 static int bp_first = 1; /* prior to the first page */
37 static int bp_next = NOPAGE; /* next page number */
38 static int bp_count; /* number of pages so far */
39 static int bp_ejected; /* current ejected page */
40 static int bp_final; /* 1: executing em, 2: the final page, 3: the 2nd final page */
41 static int ren_level; /* the depth of render_rec() calls */
43 static char c_fa[GNLEN]; /* field delimiter */
44 static char c_fb[GNLEN]; /* field padding */
46 static int ren_next(void)
48 return ren_un > 0 ? ren_unbuf[--ren_un] : tr_next();
51 static void ren_back(int c)
53 ren_unbuf[ren_un++] = c;
56 void tr_di(char **args)
58 if (args[1]) {
59 cdiv = cdiv ? cdiv + 1 : divs;
60 memset(cdiv, 0, sizeof(*cdiv));
61 sbuf_init(&cdiv->sbuf);
62 cdiv->reg = map(args[1]);
63 cdiv->treg = -1;
64 if (args[0][2] == 'a' && str_get(cdiv->reg)) /* .da */
65 sbuf_append(&cdiv->sbuf, str_get(cdiv->reg));
66 sbuf_printf(&cdiv->sbuf, "%c%s\n", c_cc, TR_DIVBEG);
67 cdiv->prev_d = n_d;
68 cdiv->prev_h = n_h;
69 cdiv->prev_mk = n_mk;
70 cdiv->prev_ns = n_ns;
71 n_d = 0;
72 n_h = 0;
73 n_mk = 0;
74 n_ns = 0;
75 } else if (cdiv) {
76 sbuf_printf(&cdiv->sbuf, "%c%s\n", c_cc, TR_DIVEND);
77 str_set(cdiv->reg, sbuf_buf(&cdiv->sbuf));
78 sbuf_done(&cdiv->sbuf);
79 n_dl = cdiv->dl;
80 n_dn = n_d;
81 n_d = cdiv->prev_d;
82 n_h = cdiv->prev_h;
83 n_mk = cdiv->prev_mk;
84 n_ns = cdiv->prev_ns;
85 cdiv = cdiv > divs ? cdiv - 1 : NULL;
89 int f_divreg(void)
91 return cdiv ? cdiv->reg : -1;
94 int f_hpos(void)
96 return fmt_wid(cfmt) + wb_wid(cwb);
99 void tr_divbeg(char **args)
101 odiv_beg();
102 ren_div++;
105 void tr_divend(char **args)
107 if (ren_div <= 0)
108 errdie("neatroff: diversion stack empty\n");
109 odiv_end();
110 ren_div--;
113 void tr_divvs(char **args)
115 ren_divvs = eval(args[1], 'u');
118 void tr_transparent(char **args)
120 if (cdiv)
121 sbuf_printf(&cdiv->sbuf, "%s\n", args[1]);
122 else
123 out("%s\n", args[1]);
126 static int trap_reg(int pos);
127 static int trap_pos(int pos);
128 static void trap_exec(int reg);
130 static void ren_page(int force)
132 if (!force && bp_final >= 2)
133 return;
134 n_nl = 0;
135 n_d = 0;
136 n_h = 0;
137 n_pg = bp_next != NOPAGE ? bp_next : n_pg + 1;
138 bp_next = NOPAGE;
139 bp_count++;
140 out("p%d\n", n_pg);
141 out("V%d\n", 0);
142 if (trap_pos(-1) == 0)
143 trap_exec(trap_reg(-1));
146 static int ren_first(void)
148 if (bp_first && !cdiv) {
149 bp_first = 0;
150 ren_page(1);
151 return 0;
153 return 1;
156 /* when nodiv, do not append .sp to diversions */
157 static void ren_sp(int n, int nodiv)
159 int linevs = !n; /* the vertical spacing before a line */
160 ren_first();
161 if (!n && ren_div && ren_divvs && !n_u)
162 n = ren_divvs; /* .v at the time of diversion */
163 ren_divvs = 0;
164 n_ns = 0;
165 n_d += n ? n : n_v;
166 if (n_d > n_h)
167 n_h = n_d;
168 if (cdiv && !nodiv) {
169 if (linevs)
170 sbuf_printf(&cdiv->sbuf, "%c%s %du\n", c_cc, TR_DIVVS, n_v);
171 else
172 sbuf_printf(&cdiv->sbuf, "%csp %du\n", c_cc, n ? n : n_v);
174 if (!cdiv)
175 n_nl = n_d;
178 static int render_rec(int level);
180 static void trap_exec(int reg)
182 char cmd[16];
183 int partial = ren_partial && (!ren_un || ren_unbuf[0] != '\n');
184 if (str_get(reg)) {
185 sprintf(cmd, "%c%s %d\n", c_cc, TR_POPREN, ren_level);
186 in_push(cmd, NULL);
187 in_push(str_get(reg), NULL);
188 if (partial)
189 in_push("\n", NULL);
190 render_rec(++ren_level);
191 /* executed the trap while in the middle of an input line */
192 if (partial)
193 fmt_suppressnl(cfmt);
197 static int detect_traps(int beg, int end)
199 int pos = trap_pos(beg);
200 return pos >= 0 && (cdiv || pos < n_p) && pos <= end;
203 /* return 1 if executed a trap */
204 static int ren_traps(int beg, int end, int dosp)
206 int pos = trap_pos(beg);
207 if (detect_traps(beg, end)) {
208 if (dosp && pos > beg)
209 ren_sp(pos - beg, 0);
210 trap_exec(trap_reg(beg));
211 return 1;
213 return 0;
216 static int detect_pagelimit(int ne)
218 return !cdiv && n_nl + ne >= n_p;
221 /* start a new page if needed */
222 static int ren_pagelimit(int ne)
224 if (detect_pagelimit(ne)) {
225 ren_page(0);
226 return 1;
228 return 0;
231 /* return 1 if triggered a trap */
232 static int down(int n)
234 if (ren_traps(n_d, n_d + (n ? n : n_v), 1))
235 return 1;
236 ren_sp(MAX(n, -n_d), 0);
237 return ren_pagelimit(0);
240 /* line adjustment */
241 static int ren_ljust(struct sbuf *spre, int w, int ad, int li, int ll)
243 int ljust = li;
244 int llen = ll - ljust;
245 n_n = w;
246 if ((ad & AD_B) == AD_C)
247 ljust += llen > w ? (llen - w) / 2 : 0;
248 if ((ad & AD_B) == AD_R)
249 ljust += llen - w;
250 if (ljust)
251 sbuf_printf(spre, "%ch'%du'", c_ec, ljust);
252 if (cdiv && cdiv->dl < w + ljust)
253 cdiv->dl = w + ljust;
254 return ljust;
257 /* append the line to the current diversion or send it to out.c */
258 static void ren_out(char *beg, char *mid, char *end)
260 if (cdiv) {
261 sbuf_append(&cdiv->sbuf, beg);
262 sbuf_append(&cdiv->sbuf, mid);
263 sbuf_append(&cdiv->sbuf, end);
264 sbuf_append(&cdiv->sbuf, "\n");
265 } else {
266 out("H%d\n", n_o);
267 out("V%d\n", n_d);
268 out_line(beg);
269 out_line(mid);
270 out_line(end);
274 static int zwid(void)
276 struct glyph *g = dev_glyph("0", n_f);
277 return g ? font_gwid(g->font, dev_font(n_f), n_s, g->wid) : 0;
280 /* append the line number to the output line */
281 static void ren_lnum(struct sbuf *spre)
283 char num[16] = "";
284 char dig[16] = "";
285 struct wb wb;
286 int i = 0;
287 wb_init(&wb);
288 if (n_nn <= 0 && (n_ln % n_nM) == 0)
289 sprintf(num, "%d", n_ln);
290 wb_hmov(&wb, n_nI * zwid());
291 if (strlen(num) < 3)
292 wb_hmov(&wb, (3 - strlen(num)) * zwid());
293 while (num[i]) {
294 dig[0] = num[i++];
295 wb_put(&wb, dig);
297 wb_hmov(&wb, n_nS * zwid());
298 sbuf_append(spre, wb_buf(&wb));
299 wb_done(&wb);
300 if (n_nn > 0)
301 n_nn--;
302 else
303 n_ln++;
306 /* append margin character */
307 static void ren_mc(struct sbuf *sbuf, int w, int ljust)
309 struct wb wb;
310 wb_init(&wb);
311 if (w + ljust < n_l + n_mcn)
312 wb_hmov(&wb, n_l + n_mcn - w - ljust);
313 wb_putexpand(&wb, c_mc);
314 sbuf_append(sbuf, wb_buf(&wb));
315 wb_done(&wb);
318 /* process a line and print it with ren_out() */
319 static int ren_line(char *line, int w, int ad, int body,
320 int li, int ll, int els_neg, int els_pos)
322 struct sbuf sbeg, send;
323 int prev_d, lspc, ljust;
324 ren_first();
325 sbuf_init(&sbeg);
326 sbuf_init(&send);
327 lspc = MAX(1, n_L) * n_v; /* line space, ignoreing \x */
328 prev_d = n_d;
329 if (!n_ns || line[0] || els_neg || els_pos) {
330 if (els_neg)
331 ren_sp(-els_neg, 1);
332 ren_sp(0, 0);
333 if (line[0] && n_nm && body)
334 ren_lnum(&sbeg);
335 ljust = ren_ljust(&sbeg, w, ad, li, ll);
336 if (line[0] && body && n_mc)
337 ren_mc(&send, w, ljust);
338 ren_out(sbuf_buf(&sbeg), line, sbuf_buf(&send));
339 n_ns = 0;
340 if (els_pos)
341 ren_sp(els_pos, 1);
343 sbuf_done(&sbeg);
344 sbuf_done(&send);
345 n_a = els_pos;
346 if (detect_traps(prev_d, n_d) || detect_pagelimit(lspc - n_v)) {
347 if (!ren_pagelimit(lspc - n_v))
348 ren_traps(prev_d, n_d, 0);
349 return 1;
351 if (lspc - n_v && down(lspc - n_v))
352 return 1;
353 return 0;
356 /* read a line from fmt and send it to ren_line() */
357 static int ren_passline(struct fmt *fmt)
359 char *buf;
360 int ll, li, els_neg, els_pos, w, ret;
361 int ad = n_j;
362 ren_first();
363 if (!fmt_morewords(fmt))
364 return 0;
365 buf = fmt_nextline(fmt, &w, &li, &ll, &els_neg, &els_pos);
366 if ((n_cp && !n_u) || n_na)
367 ad = AD_L;
368 if (n_ce)
369 ad = AD_C;
370 ret = ren_line(buf, w, ad, 1, li, ll, els_neg, els_pos);
371 free(buf);
372 return ret;
375 /* output formatted lines in fmt */
376 static int ren_fmtpop(struct fmt *fmt)
378 int ret = 0;
379 while (fmt_morelines(fmt))
380 ret = ren_passline(fmt);
381 return ret;
384 /* format and output all lines in fmt */
385 static void ren_fmtpopall(struct fmt *fmt)
387 while (fmt_fill(fmt, 0))
388 ren_fmtpop(fmt);
389 ren_fmtpop(fmt);
392 /* pass the given word buffer to the current line buffer (cfmt) */
393 static void ren_fmtword(struct wb *wb)
395 while (fmt_word(cfmt, wb))
396 ren_fmtpop(cfmt);
397 wb_reset(wb);
400 /* output current line; returns 1 if triggered a trap */
401 static int ren_br(void)
403 ren_first();
404 ren_fmtword(cwb);
405 while (fmt_fill(cfmt, 1))
406 ren_fmtpop(cfmt);
407 return ren_fmtpop(cfmt);
410 void tr_br(char **args)
412 if (args[0][0] == c_cc)
413 ren_br();
414 else
415 ren_fmtpopall(cfmt); /* output the completed lines */
418 void tr_sp(char **args)
420 int traps = 0;
421 int n;
422 if (args[0][0] == c_cc)
423 traps = ren_br();
424 n = args[1] ? eval(args[1], 'v') : n_v;
425 if (n && (!n_ns || ren_div) && !traps)
426 down(n);
429 void tr_sv(char **args)
431 int n = eval(args[1], 'v');
432 n_sv = 0;
433 if (n_d + n < f_nexttrap())
434 down(n);
435 else
436 n_sv = n;
439 void tr_ns(char **args)
441 n_ns = 1;
444 void tr_rs(char **args)
446 n_ns = 0;
449 void tr_os(char **args)
451 if (n_sv)
452 down(n_sv);
453 n_sv = 0;
456 void tr_mk(char **args)
458 if (args[1])
459 num_set(map(args[1]), n_d);
460 else
461 n_mk = n_d;
464 void tr_rt(char **args)
466 int n = args[1] ? eval_re(args[1], n_d, 'v') : n_mk;
467 if (n >= 0 && n < n_d)
468 ren_sp(n - n_d, 0);
471 void tr_ne(char **args)
473 int n = args[1] ? eval(args[1], 'v') : n_v;
474 if (!ren_first())
475 return;
476 if (!ren_traps(n_d, n_d + n - 1, 1))
477 ren_pagelimit(n);
480 static void ren_ejectpage(int br)
482 ren_first();
483 bp_ejected = bp_count;
484 if (br)
485 ren_br();
486 while (bp_count == bp_ejected && !cdiv) {
487 if (detect_traps(n_d, n_p)) {
488 ren_traps(n_d, n_p, 1);
489 } else {
490 bp_ejected = 0;
491 ren_page(0);
496 void tr_bp(char **args)
498 if (!cdiv && (args[1] || !n_ns)) {
499 if (args[1])
500 bp_next = eval_re(args[1], n_pg, 0);
501 ren_ejectpage(args[0][0] == c_cc);
505 void tr_pn(char **args)
507 if (args[1])
508 bp_next = eval_re(args[1], n_pg, 0);
511 static void ren_ps(char *s)
513 int ps = !s || !*s || !strcmp("0", s) ?
514 n_s0 * SC_PT : eval_re(s, n_s * SC_PT, 'p');
515 n_s0 = n_s;
516 n_s = MAX(1, (ps + SC_PT / 2) / SC_PT);
519 void tr_ps(char **args)
521 ren_ps(args[1]);
524 void tr_ll(char **args)
526 int ll = args[1] ? eval_re(args[1], n_l, 'm') : n_l0;
527 n_l0 = n_l;
528 n_l = MAX(0, ll);
531 void tr_in(char **args)
533 int in = args[1] ? eval_re(args[1], n_i, 'm') : n_i0;
534 if (args[0][0] == c_cc)
535 ren_br();
536 n_i0 = n_i;
537 n_i = MAX(0, in);
538 n_ti = -1;
541 void tr_ti(char **args)
543 if (args[0][0] == c_cc)
544 ren_br();
545 if (args[1])
546 n_ti = eval_re(args[1], n_i, 'm');
549 static void ren_ft(char *s)
551 int fn = !s || !*s || !strcmp("P", s) ? n_f0 : dev_pos(s);
552 if (fn < 0) {
553 errmsg("neatroff: failed to mount <%s>\n", s);
554 } else {
555 n_f0 = n_f;
556 n_f = fn;
560 void tr_ft(char **args)
562 ren_ft(args[1]);
565 void tr_fp(char **args)
567 if (!args[2])
568 return;
569 if (dev_mnt(atoi(args[1]), args[2], args[3] ? args[3] : args[2]) < 0)
570 errmsg("neatroff: failed to mount <%s>\n", args[2]);
573 void tr_nf(char **args)
575 if (args[0][0] == c_cc)
576 ren_br();
577 n_u = 0;
580 void tr_fi(char **args)
582 if (args[0][0] == c_cc)
583 ren_br();
584 n_u = 1;
587 void tr_ce(char **args)
589 if (args[0][0] == c_cc)
590 ren_br();
591 n_ce = args[1] ? atoi(args[1]) : 1;
594 void tr_fc(char **args)
596 char *fa = args[1];
597 char *fb = args[2];
598 if (fa && charread(&fa, c_fa) >= 0) {
599 if (!fb || charread(&fb, c_fb) < 0)
600 strcpy(c_fb, " ");
601 } else {
602 c_fa[0] = '\0';
603 c_fb[0] = '\0';
607 static void ren_cl(char *s)
609 int m = !s || !*s ? n_m0 : clr_get(s);
610 n_m0 = n_m;
611 n_m = m;
614 void tr_cl(char **args)
616 ren_cl(args[1]);
619 void tr_ab(char **args)
621 fprintf(stderr, "%s\n", args[1]);
622 ren_aborted = 1;
625 static void ren_cmd(struct wb *wb, int c, char *arg)
627 switch (c) {
628 case ' ':
629 wb_hmov(wb, font_swid(dev_font(n_f), n_s, n_ss));
630 break;
631 case 'b':
632 ren_bcmd(wb, arg);
633 break;
634 case 'c':
635 wb_setpart(wb);
636 break;
637 case 'D':
638 ren_dcmd(wb, arg);
639 break;
640 case 'd':
641 wb_vmov(wb, SC_EM / 2);
642 break;
643 case 'f':
644 ren_ft(arg);
645 break;
646 case 'h':
647 wb_hmov(wb, eval(arg, 'm'));
648 break;
649 case 'j':
650 wb_setcost(wb, eval(arg, 0));
651 break;
652 case 'k':
653 num_set(map(arg), wb == cwb ? f_hpos() - n_lb : wb_wid(wb));
654 break;
655 case 'L':
656 ren_vlcmd(wb, arg);
657 break;
658 case 'l':
659 ren_hlcmd(wb, arg);
660 break;
661 case 'm':
662 ren_cl(arg);
663 break;
664 case 'o':
665 ren_ocmd(wb, arg);
666 break;
667 case 'p':
668 if (wb == cwb)
669 while (fmt_fillreq(cfmt))
670 ren_fmtpop(cfmt);
671 break;
672 case 'r':
673 wb_vmov(wb, -SC_EM);
674 break;
675 case 's':
676 ren_ps(arg);
677 break;
678 case 'u':
679 wb_vmov(wb, -SC_EM / 2);
680 break;
681 case 'v':
682 wb_vmov(wb, eval(arg, 'v'));
683 break;
684 case 'X':
685 wb_etc(wb, arg);
686 break;
687 case 'x':
688 wb_els(wb, eval(arg, 'v'));
689 break;
690 case 'Z':
691 ren_zcmd(wb, arg);
692 break;
693 case '0':
694 wb_hmov(wb, zwid());
695 break;
696 case '|':
697 wb_hmov(wb, SC_EM / 6);
698 break;
699 case '&':
700 wb_hmov(wb, 0);
701 break;
702 case '^':
703 wb_hmov(wb, SC_EM / 12);
704 break;
705 case '/':
706 wb_italiccorrection(wb);
707 break;
708 case ',':
709 wb_italiccorrectionleft(wb);
710 break;
714 static void ren_field(struct wb *wb, int (*next)(void), void (*back)(int));
715 static void ren_tab(struct wb *wb, char *tc, int (*next)(void), void (*back)(int));
717 /* insert a character, escape sequence, field or etc into wb */
718 static void ren_put(struct wb *wb, char *c, int (*next)(void), void (*back)(int))
720 char arg[ILNLEN];
721 int w;
722 if (c[0] == ' ' || c[0] == '\n') {
723 wb_put(wb, c);
724 return;
726 if (c[0] == '\t' || c[0] == '\x01') {
727 ren_tab(wb, c[0] == '\t' ? c_tc : c_lc, next, back);
728 return;
730 if (c_fa[0] && !strcmp(c_fa, c)) {
731 ren_field(wb, next, back);
732 return;
734 if (c[0] == c_ec) {
735 if (c[1] == 'z') {
736 w = wb_wid(wb);
737 ren_char(wb, next, back);
738 wb_hmov(wb, w - wb_wid(wb));
739 return;
741 if (strchr(" bCcDdefHhjkLlmNoprSsuvXxZz0^|!{}&/,", c[1])) {
742 arg[0] = '\0';
743 if (strchr(ESC_P, c[1]))
744 unquotednext(arg, c[1], next, back);
745 if (strchr(ESC_Q, c[1]))
746 quotednext(arg, next, back);
747 if (c[1] == 'e') {
748 snprintf(c, GNLEN, "%c%c", c_ec, c_ec);
749 } else if (c[1] == 'N') {
750 snprintf(c, GNLEN, "GID=%s", arg);
751 } else {
752 ren_cmd(wb, c[1], arg);
753 return;
757 if (ren_div) {
758 wb_putraw(wb, c);
759 return;
761 if (cdef_map(c, n_f)) /* .char characters */
762 wb_putexpand(wb, c);
763 else
764 wb_put(wb, c);
767 /* read one character and place it inside wb buffer */
768 int ren_char(struct wb *wb, int (*next)(void), void (*back)(int))
770 char c[GNLEN * 4];
771 if (charnext(c, next, back) < 0)
772 return -1;
773 ren_put(wb, c, next, back);
774 return 0;
777 /* like ren_char(); return 1 if d1 was read and 2 if d2 was read */
778 static int ren_chardel(struct wb *wb, int (*next)(void), void (*back)(int),
779 char *d1, char *d2)
781 char c[GNLEN * 4];
782 if (charnext(c, next, back) < 0)
783 return -1;
784 if (d1 && !strcmp(d1, c))
785 return 1;
786 if (d2 && !strcmp(d2, c))
787 return 2;
788 ren_put(wb, c, next, back);
789 return 0;
792 /* read the argument of \w and push its width */
793 int ren_wid(int (*next)(void), void (*back)(int))
795 char delim[GNLEN];
796 int c, n;
797 struct wb wb;
798 wb_init(&wb);
799 charnext(delim, next, back);
800 odiv_beg();
801 c = next();
802 while (c >= 0 && c != '\n') {
803 back(c);
804 if (ren_chardel(&wb, next, back, delim, NULL))
805 break;
806 c = next();
808 odiv_end();
809 n = wb_wid(&wb);
810 wb_wconf(&wb, &n_ct, &n_st, &n_sb, &n_llx, &n_lly, &n_urx, &n_ury);
811 wb_done(&wb);
812 return n;
815 /* return 1 if d1 was read and 2 if d2 was read */
816 static int ren_until(struct wb *wb, int (*next)(void), void (*back)(int),
817 char *d1, char *d2)
819 int c, ret;
820 c = next();
821 while (c >= 0 && c != '\n') {
822 back(c);
823 ret = ren_chardel(wb, next, back, d1, d2);
824 if (ret)
825 return ret;
826 c = next();
828 if (c == '\n')
829 back(c);
830 return 0;
833 /* like ren_until(); map src to dst */
834 static int ren_untilmap(struct wb *wb, int (*next)(void), void (*back)(int),
835 char *end, char *src, char *dst)
837 int ret;
838 while ((ret = ren_until(wb, next, back, src, end)) == 1) {
839 sstr_push(dst);
840 ren_until(wb, sstr_next, sstr_back, end, NULL);
841 sstr_pop();
843 return 0;
846 static void wb_cpy(struct wb *dst, struct wb *src, int left)
848 wb_hmov(dst, left - wb_wid(dst));
849 wb_cat(dst, src);
852 void ren_tl(int (*next)(void), void (*back)(int))
854 struct wb wb, wb2;
855 char *pgnum;
856 char delim[GNLEN];
857 ren_first();
858 pgnum = num_str(map("%"));
859 wb_init(&wb);
860 wb_init(&wb2);
861 charnext(delim, next, back);
862 if (!strcmp("\n", delim))
863 back('\n');
864 /* the left-adjusted string */
865 ren_untilmap(&wb2, next, back, delim, c_pc, pgnum);
866 wb_cpy(&wb, &wb2, 0);
867 /* the centered string */
868 ren_untilmap(&wb2, next, back, delim, c_pc, pgnum);
869 wb_cpy(&wb, &wb2, (n_lt - wb_wid(&wb2)) / 2);
870 /* the right-adjusted string */
871 ren_untilmap(&wb2, next, back, delim, c_pc, pgnum);
872 wb_cpy(&wb, &wb2, n_lt - wb_wid(&wb2));
873 /* flushing the line */
874 ren_line(wb_buf(&wb), wb_wid(&wb), AD_L, 0,
875 0, n_lt, wb.els_neg, wb.els_pos);
876 wb_done(&wb2);
877 wb_done(&wb);
880 static void ren_field(struct wb *wb, int (*next)(void), void (*back)(int))
882 struct wb wbs[NFIELDS];
883 int i, n = 0;
884 int wid = 0;
885 int left, right, cur_left;
886 int pad, rem;
887 while (n < LEN(wbs)) {
888 wb_init(&wbs[n]);
889 if (ren_until(&wbs[n++], next, back, c_fb, c_fa) != 1)
890 break;
892 left = wb == cwb ? f_hpos() : wb_wid(wb);
893 right = tab_next(left);
894 for (i = 0; i < n; i++)
895 wid += wb_wid(&wbs[i]);
896 pad = (right - left - wid) / (n > 1 ? n - 1 : 1);
897 rem = (right - left - wid) % (n > 1 ? n - 1 : 1);
898 for (i = 0; i < n; i++) {
899 if (i == 0)
900 cur_left = left;
901 else if (i == n - 1)
902 cur_left = right - wb_wid(&wbs[i]);
903 else
904 cur_left = wb_wid(wb) + pad + (i + rem >= n);
905 wb_cpy(wb, &wbs[i], cur_left);
906 wb_done(&wbs[i]);
910 static void ren_tab(struct wb *wb, char *tc, int (*next)(void), void (*back)(int))
912 struct wb t;
913 int pos = wb == cwb ? f_hpos() : wb_wid(wb);
914 int ins = tab_next(pos); /* insertion position */
915 int typ = tab_type(pos); /* tab type */
916 int c;
917 wb_init(&t);
918 if (typ == 'R' || typ == 'C') {
919 c = next();
920 while (c >= 0 && c != '\n' && c != '\t' && c != '\x01') {
921 back(c);
922 ren_char(&t, next, back);
923 c = next();
925 back(c);
927 if (typ == 'C')
928 ins -= wb_wid(&t) / 2;
929 if (typ == 'R')
930 ins -= wb_wid(&t);
931 if (!tc[0] || ins <= pos)
932 wb_hmov(wb, ins - pos);
933 else
934 ren_hline(wb, ins - pos, tc);
935 wb_cat(wb, &t);
936 wb_done(&t);
939 /* parse characters and troff requests of s and append them to wb */
940 int ren_parse(struct wb *wb, char *s)
942 int c;
943 odiv_beg();
944 sstr_push(s);
945 c = sstr_next();
946 while (c >= 0) {
947 sstr_back(c);
948 if (ren_char(wb, sstr_next, sstr_back))
949 break;
950 c = sstr_next();
952 sstr_pop();
953 odiv_end();
954 return 0;
957 /* cause nested render_rec() to exit */
958 void tr_popren(char **args)
960 ren_level = args[1] ? atoi(args[1]) : 0;
963 #define FMT_PAR() (n_u && !n_na && !n_ce && (n_j & AD_P) == AD_P)
965 /* read characters from tr.c and pass the rendered lines to out.c */
966 static int render_rec(int level)
968 int c;
969 while (ren_level >= level) {
970 while (!ren_un && !tr_nextreq())
971 if (ren_level < level)
972 break;
973 if (ren_level < level)
974 break;
975 if (ren_aborted)
976 return 1;
977 c = ren_next();
978 if (c < 0) {
979 if (bp_final >= 2)
980 break;
981 if (bp_final == 0) {
982 bp_final = 1;
983 if (trap_em >= 0)
984 trap_exec(trap_em);
985 } else {
986 bp_final = 2;
987 ren_ejectpage(1);
990 if (c >= 0)
991 ren_partial = c != '\n';
992 /* add cwb (the current word) to cfmt */
993 if (c == ' ' || c == '\n') {
994 if (!wb_part(cwb)) { /* not after a \c */
995 ren_fmtword(cwb);
996 if (c == '\n')
997 while (fmt_newline(cfmt))
998 ren_fmtpop(cfmt);
999 if (!FMT_PAR())
1000 ren_fmtpopall(cfmt);
1001 if (c == ' ')
1002 fmt_space(cfmt);
1005 /* flush the line if necessary */
1006 if ((!FMT_PAR() && (c == ' ' || c == '\n')) || c < 0)
1007 ren_fmtpop(cfmt);
1008 if (c == '\n' || ren_nl) /* end or start of input line */
1009 n_lb = f_hpos();
1010 if (c == '\n' && n_it && --n_itn == 0)
1011 trap_exec(n_it);
1012 if (c == '\n' && !wb_part(cwb))
1013 n_ce = MAX(0, n_ce - 1);
1014 if (c != ' ' && c >= 0) {
1015 ren_back(c);
1016 ren_char(cwb, ren_next, ren_back);
1018 if (c >= 0)
1019 ren_nl = c == '\n';
1021 return 0;
1024 /* render input words */
1025 int render(void)
1027 n_nl = -1;
1028 while (!tr_nextreq())
1030 ren_first(); /* transition to the first page */
1031 render_rec(0);
1032 bp_final = 3;
1033 if (fmt_morewords(cfmt))
1034 ren_page(1);
1035 ren_br();
1036 return 0;
1039 /* trap handling */
1041 #define tposval(i) (tpos[i] < 0 ? n_p + tpos[i] : tpos[i])
1043 static int tpos[NTRAPS]; /* trap positions */
1044 static int treg[NTRAPS]; /* trap registers */
1045 static int ntraps;
1047 static int trap_first(int pos)
1049 int best = -1;
1050 int i;
1051 for (i = 0; i < ntraps; i++)
1052 if (treg[i] >= 0 && tposval(i) > pos)
1053 if (best < 0 || tposval(i) < tposval(best))
1054 best = i;
1055 return best;
1058 static int trap_byreg(int reg)
1060 int i;
1061 for (i = 0; i < ntraps; i++)
1062 if (treg[i] == reg)
1063 return i;
1064 return -1;
1067 static int trap_bypos(int reg, int pos)
1069 int i;
1070 for (i = 0; i < ntraps; i++)
1071 if (treg[i] >= 0 && tposval(i) == pos)
1072 if (reg == -1 || treg[i] == reg)
1073 return i;
1074 return -1;
1077 void tr_wh(char **args)
1079 int reg, pos, id;
1080 if (!args[1])
1081 return;
1082 pos = eval(args[1], 'v');
1083 id = trap_bypos(-1, pos);
1084 if (!args[2]) {
1085 if (id >= 0)
1086 treg[id] = -1;
1087 return;
1089 reg = map(args[2]);
1090 if (id < 0) /* find an unused position in treg[] */
1091 id = trap_byreg(-1);
1092 if (id < 0)
1093 id = ntraps++;
1094 tpos[id] = pos;
1095 treg[id] = reg;
1098 void tr_ch(char **args)
1100 int reg;
1101 int id;
1102 if (!args[1])
1103 return;
1104 reg = map(args[1]);
1105 id = trap_byreg(reg);
1106 if (id >= 0) {
1107 if (args[2])
1108 tpos[id] = eval(args[2], 'v');
1109 else
1110 treg[id] = -1;
1114 void tr_dt(char **args)
1116 if (!cdiv)
1117 return;
1118 if (args[2]) {
1119 cdiv->tpos = eval(args[1], 'v');
1120 cdiv->treg = map(args[2]);
1121 } else {
1122 cdiv->treg = -1;
1126 void tr_em(char **args)
1128 trap_em = args[1] ? map(args[1]) : -1;
1131 static int trap_pos(int pos)
1133 int ret = trap_first(pos);
1134 if (bp_final >= 3)
1135 return -1;
1136 if (cdiv)
1137 return cdiv->treg && cdiv->tpos > pos ? cdiv->tpos : -1;
1138 return ret >= 0 ? tposval(ret) : -1;
1141 static int trap_reg(int pos)
1143 int ret = trap_first(pos);
1144 if (cdiv)
1145 return cdiv->treg && cdiv->tpos > pos ? cdiv->treg : -1;
1146 return ret >= 0 ? treg[ret] : -1;
1149 int f_nexttrap(void)
1151 int pos = trap_pos(n_d);
1152 if (cdiv)
1153 return pos >= 0 ? pos : 0x7fffffff;
1154 return (pos >= 0 && pos < n_p ? pos : n_p) - n_d;