tr: do not alter the line number in .chop
[neatroff.git] / ren.c
blobf72a8fe992d82257fbf3135bc08627ef8b640854
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 struct sbuf sbuf;
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 sbuf_init(&sbuf);
366 fmt_nextline(fmt, &sbuf, &w, &li, &ll, &els_neg, &els_pos);
367 if ((n_cp && !n_u) || n_na)
368 ad = AD_L;
369 if (n_ce)
370 ad = AD_C;
371 ret = ren_line(sbuf_buf(&sbuf), w, ad, 1, li, ll, els_neg, els_pos);
372 sbuf_done(&sbuf);
373 return ret;
376 /* output formatted lines in fmt */
377 static int ren_fmtpop(struct fmt *fmt)
379 int ret = 0;
380 while (fmt_morelines(fmt))
381 ret = ren_passline(fmt);
382 return ret;
385 /* format and output all lines in fmt */
386 static void ren_fmtpopall(struct fmt *fmt)
388 while (fmt_fill(fmt, 0))
389 ren_fmtpop(fmt);
390 ren_fmtpop(fmt);
393 /* pass the given word buffer to the current line buffer (cfmt) */
394 static void ren_fmtword(struct wb *wb)
396 while (fmt_word(cfmt, wb))
397 ren_fmtpop(cfmt);
398 wb_reset(wb);
401 /* output current line; returns 1 if triggered a trap */
402 static int ren_br(void)
404 ren_first();
405 ren_fmtword(cwb);
406 while (fmt_fill(cfmt, 1))
407 ren_fmtpop(cfmt);
408 return ren_fmtpop(cfmt);
411 void tr_br(char **args)
413 if (args[0][0] == c_cc)
414 ren_br();
415 else
416 ren_fmtpopall(cfmt); /* output the completed lines */
419 void tr_sp(char **args)
421 int traps = 0;
422 int n = args[1] ? eval(args[1], 'v') : n_v;
423 if (args[0][0] == c_cc)
424 traps = ren_br();
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 'k':
650 num_set(map(arg), wb == cwb ? f_hpos() - n_lb : wb_wid(wb));
651 break;
652 case 'L':
653 ren_vlcmd(wb, arg);
654 break;
655 case 'l':
656 ren_hlcmd(wb, arg);
657 break;
658 case 'm':
659 ren_cl(arg);
660 break;
661 case 'o':
662 ren_ocmd(wb, arg);
663 break;
664 case 'p':
665 if (wb == cwb)
666 while (fmt_fillreq(cfmt))
667 ren_fmtpop(cfmt);
668 break;
669 case 'r':
670 wb_vmov(wb, -SC_EM);
671 break;
672 case 's':
673 ren_ps(arg);
674 break;
675 case 'u':
676 wb_vmov(wb, -SC_EM / 2);
677 break;
678 case 'v':
679 wb_vmov(wb, eval(arg, 'v'));
680 break;
681 case 'X':
682 wb_etc(wb, arg);
683 break;
684 case 'x':
685 wb_els(wb, eval(arg, 'v'));
686 break;
687 case 'Z':
688 ren_zcmd(wb, arg);
689 break;
690 case '0':
691 wb_hmov(wb, zwid());
692 break;
693 case '|':
694 wb_hmov(wb, SC_EM / 6);
695 break;
696 case '&':
697 wb_hmov(wb, 0);
698 break;
699 case '^':
700 wb_hmov(wb, SC_EM / 12);
701 break;
702 case '/':
703 wb_italiccorrection(wb);
704 break;
705 case ',':
706 wb_italiccorrectionleft(wb);
707 break;
711 static void ren_field(struct wb *wb, int (*next)(void), void (*back)(int));
712 static void ren_tab(struct wb *wb, char *tc, int (*next)(void), void (*back)(int));
714 /* insert a character, escape sequence, field or etc into wb */
715 static void ren_put(struct wb *wb, char *c, int (*next)(void), void (*back)(int))
717 char arg[ILNLEN];
718 int w;
719 if (c[0] == ' ' || c[0] == '\n') {
720 wb_put(wb, c);
721 return;
723 if (c[0] == '\t' || c[0] == '\x01') {
724 ren_tab(wb, c[0] == '\t' ? c_tc : c_lc, next, back);
725 return;
727 if (c_fa[0] && !strcmp(c_fa, c)) {
728 ren_field(wb, next, back);
729 return;
731 if (c[0] == c_ec) {
732 if (c[1] == 'z') {
733 w = wb_wid(wb);
734 ren_char(wb, next, back);
735 wb_hmov(wb, w - wb_wid(wb));
736 return;
738 if (strchr(" bCcDdefHhkLlmNoprSsuvXxZz0^|!{}&/,", c[1])) {
739 arg[0] = '\0';
740 if (strchr(ESC_P, c[1]))
741 unquotednext(arg, c[1], next, back);
742 if (strchr(ESC_Q, c[1]))
743 quotednext(arg, next, back);
744 if (c[1] == 'e') {
745 snprintf(c, GNLEN, "%c%c", c_ec, c_ec);
746 } else if (c[1] == 'N') {
747 snprintf(c, GNLEN, "GID=%s", arg);
748 } else {
749 ren_cmd(wb, c[1], arg);
750 return;
754 if (ren_div) {
755 wb_putraw(wb, c);
756 return;
758 if (cdef_map(c, n_f)) /* .char characters */
759 wb_putexpand(wb, c);
760 else
761 wb_put(wb, c);
764 /* read one character and place it inside wb buffer */
765 int ren_char(struct wb *wb, int (*next)(void), void (*back)(int))
767 char c[GNLEN * 4];
768 if (charnext(c, next, back) < 0)
769 return -1;
770 ren_put(wb, c, next, back);
771 return 0;
774 /* like ren_char(); return 1 if d1 was read and 2 if d2 was read */
775 static int ren_chardel(struct wb *wb, int (*next)(void), void (*back)(int),
776 char *d1, char *d2)
778 char c[GNLEN * 4];
779 if (charnext(c, next, back) < 0)
780 return -1;
781 if (d1 && !strcmp(d1, c))
782 return 1;
783 if (d2 && !strcmp(d2, c))
784 return 2;
785 ren_put(wb, c, next, back);
786 return 0;
789 /* read the argument of \w and push its width */
790 int ren_wid(int (*next)(void), void (*back)(int))
792 char delim[GNLEN];
793 int c, n;
794 struct wb wb;
795 wb_init(&wb);
796 charnext(delim, next, back);
797 odiv_beg();
798 c = next();
799 while (c >= 0 && c != '\n') {
800 back(c);
801 if (ren_chardel(&wb, next, back, delim, NULL))
802 break;
803 c = next();
805 odiv_end();
806 n = wb_wid(&wb);
807 wb_wconf(&wb, &n_ct, &n_st, &n_sb, &n_llx, &n_lly, &n_urx, &n_ury);
808 wb_done(&wb);
809 return n;
812 /* return 1 if d1 was read and 2 if d2 was read */
813 static int ren_until(struct wb *wb, int (*next)(void), void (*back)(int),
814 char *d1, char *d2)
816 int c, ret;
817 c = next();
818 while (c >= 0 && c != '\n') {
819 back(c);
820 ret = ren_chardel(wb, next, back, d1, d2);
821 if (ret)
822 return ret;
823 c = next();
825 if (c == '\n')
826 back(c);
827 return 0;
830 /* like ren_until(); map src to dst */
831 static int ren_untilmap(struct wb *wb, int (*next)(void), void (*back)(int),
832 char *end, char *src, char *dst)
834 int ret;
835 while ((ret = ren_until(wb, next, back, src, end)) == 1) {
836 sstr_push(dst);
837 ren_until(wb, sstr_next, sstr_back, end, NULL);
838 sstr_pop();
840 return 0;
843 static void wb_cpy(struct wb *dst, struct wb *src, int left)
845 wb_hmov(dst, left - wb_wid(dst));
846 wb_cat(dst, src);
849 void ren_tl(int (*next)(void), void (*back)(int))
851 struct wb wb, wb2;
852 char *pgnum;
853 char delim[GNLEN];
854 ren_first();
855 pgnum = num_str(map("%"));
856 wb_init(&wb);
857 wb_init(&wb2);
858 charnext(delim, next, back);
859 if (!strcmp("\n", delim))
860 back('\n');
861 /* the left-adjusted string */
862 ren_untilmap(&wb2, next, back, delim, c_pc, pgnum);
863 wb_cpy(&wb, &wb2, 0);
864 /* the centered string */
865 ren_untilmap(&wb2, next, back, delim, c_pc, pgnum);
866 wb_cpy(&wb, &wb2, (n_lt - wb_wid(&wb2)) / 2);
867 /* the right-adjusted string */
868 ren_untilmap(&wb2, next, back, delim, c_pc, pgnum);
869 wb_cpy(&wb, &wb2, n_lt - wb_wid(&wb2));
870 /* flushing the line */
871 ren_line(wb_buf(&wb), wb_wid(&wb), AD_L, 0,
872 0, n_lt, wb.els_neg, wb.els_pos);
873 wb_done(&wb2);
874 wb_done(&wb);
877 static void ren_field(struct wb *wb, int (*next)(void), void (*back)(int))
879 struct wb wbs[NFIELDS];
880 int i, n = 0;
881 int wid = 0;
882 int left, right, cur_left;
883 int pad, rem;
884 while (n < LEN(wbs)) {
885 wb_init(&wbs[n]);
886 if (ren_until(&wbs[n++], next, back, c_fb, c_fa) != 1)
887 break;
889 left = wb == cwb ? f_hpos() : wb_wid(wb);
890 right = tab_next(left);
891 for (i = 0; i < n; i++)
892 wid += wb_wid(&wbs[i]);
893 pad = (right - left - wid) / (n > 1 ? n - 1 : 1);
894 rem = (right - left - wid) % (n > 1 ? n - 1 : 1);
895 for (i = 0; i < n; i++) {
896 if (i == 0)
897 cur_left = left;
898 else if (i == n - 1)
899 cur_left = right - wb_wid(&wbs[i]);
900 else
901 cur_left = wb_wid(wb) + pad + (i + rem >= n);
902 wb_cpy(wb, &wbs[i], cur_left);
903 wb_done(&wbs[i]);
907 static void ren_tab(struct wb *wb, char *tc, int (*next)(void), void (*back)(int))
909 struct wb t;
910 int pos = wb == cwb ? f_hpos() : wb_wid(wb);
911 int ins = tab_next(pos); /* insertion position */
912 int typ = tab_type(pos); /* tab type */
913 int c;
914 wb_init(&t);
915 if (typ == 'R' || typ == 'C') {
916 c = next();
917 while (c >= 0 && c != '\n' && c != '\t' && c != '\x01') {
918 back(c);
919 ren_char(&t, next, back);
920 c = next();
922 back(c);
924 if (typ == 'C')
925 ins -= wb_wid(&t) / 2;
926 if (typ == 'R')
927 ins -= wb_wid(&t);
928 if (!tc[0] || ins <= pos)
929 wb_hmov(wb, ins - pos);
930 else
931 ren_hline(wb, ins - pos, tc);
932 wb_cat(wb, &t);
933 wb_done(&t);
936 /* parse characters and troff requests of s and append them to wb */
937 int ren_parse(struct wb *wb, char *s)
939 int c;
940 odiv_beg();
941 sstr_push(s);
942 c = sstr_next();
943 while (c >= 0) {
944 sstr_back(c);
945 if (ren_char(wb, sstr_next, sstr_back))
946 break;
947 c = sstr_next();
949 sstr_pop();
950 odiv_end();
951 return 0;
954 /* cause nested render_rec() to exit */
955 void tr_popren(char **args)
957 ren_level = args[1] ? atoi(args[1]) : 0;
960 #define FMT_PAR() (n_u && !n_na && !n_ce && (n_j & AD_P) == AD_P)
962 /* read characters from tr.c and pass the rendered lines to out.c */
963 static int render_rec(int level)
965 int c;
966 while (ren_level >= level) {
967 while (!ren_un && !tr_nextreq())
968 if (ren_level < level)
969 break;
970 if (ren_level < level)
971 break;
972 if (ren_aborted)
973 return 1;
974 c = ren_next();
975 if (c < 0) {
976 if (bp_final >= 2)
977 break;
978 if (bp_final == 0) {
979 bp_final = 1;
980 if (trap_em >= 0)
981 trap_exec(trap_em);
982 } else {
983 bp_final = 2;
984 ren_ejectpage(1);
987 if (c >= 0)
988 ren_partial = c != '\n';
989 /* add cwb (the current word) to cfmt */
990 if (c == ' ' || c == '\n') {
991 if (!wb_part(cwb)) { /* not after a \c */
992 ren_fmtword(cwb);
993 if (c == '\n')
994 while (fmt_newline(cfmt))
995 ren_fmtpop(cfmt);
996 if (!FMT_PAR())
997 ren_fmtpopall(cfmt);
998 if (c == ' ')
999 fmt_space(cfmt);
1002 /* flush the line if necessary */
1003 if ((!FMT_PAR() && (c == ' ' || c == '\n')) || c < 0)
1004 ren_fmtpop(cfmt);
1005 if (c == '\n' || ren_nl) /* end or start of input line */
1006 n_lb = f_hpos();
1007 if (c == '\n' && n_it && --n_itn == 0)
1008 trap_exec(n_it);
1009 if (c == '\n' && !wb_part(cwb))
1010 n_ce = MAX(0, n_ce - 1);
1011 if (c != ' ' && c >= 0) {
1012 ren_back(c);
1013 ren_char(cwb, ren_next, ren_back);
1015 if (c >= 0)
1016 ren_nl = c == '\n';
1018 return 0;
1021 /* render input words */
1022 int render(void)
1024 n_nl = -1;
1025 while (!tr_nextreq())
1027 ren_first(); /* transition to the first page */
1028 render_rec(0);
1029 bp_final = 3;
1030 if (fmt_morewords(cfmt))
1031 ren_page(1);
1032 ren_br();
1033 return 0;
1036 /* trap handling */
1038 #define tposval(i) (tpos[i] < 0 ? n_p + tpos[i] : tpos[i])
1040 static int tpos[NTRAPS]; /* trap positions */
1041 static int treg[NTRAPS]; /* trap registers */
1042 static int ntraps;
1044 static int trap_first(int pos)
1046 int best = -1;
1047 int i;
1048 for (i = 0; i < ntraps; i++)
1049 if (treg[i] >= 0 && tposval(i) > pos)
1050 if (best < 0 || tposval(i) < tposval(best))
1051 best = i;
1052 return best;
1055 static int trap_byreg(int reg)
1057 int i;
1058 for (i = 0; i < ntraps; i++)
1059 if (treg[i] == reg)
1060 return i;
1061 return -1;
1064 static int trap_bypos(int reg, int pos)
1066 int i;
1067 for (i = 0; i < ntraps; i++)
1068 if (treg[i] >= 0 && tposval(i) == pos)
1069 if (reg == -1 || treg[i] == reg)
1070 return i;
1071 return -1;
1074 void tr_wh(char **args)
1076 int reg, pos, id;
1077 if (!args[1])
1078 return;
1079 pos = eval(args[1], 'v');
1080 id = trap_bypos(-1, pos);
1081 if (!args[2]) {
1082 if (id >= 0)
1083 treg[id] = -1;
1084 return;
1086 reg = map(args[2]);
1087 if (id < 0) /* find an unused position in treg[] */
1088 id = trap_byreg(-1);
1089 if (id < 0)
1090 id = ntraps++;
1091 tpos[id] = pos;
1092 treg[id] = reg;
1095 void tr_ch(char **args)
1097 int reg;
1098 int id;
1099 if (!args[1])
1100 return;
1101 reg = map(args[1]);
1102 id = trap_byreg(reg);
1103 if (id >= 0) {
1104 if (args[2])
1105 tpos[id] = eval(args[2], 'v');
1106 else
1107 treg[id] = -1;
1111 void tr_dt(char **args)
1113 if (!cdiv)
1114 return;
1115 if (args[2]) {
1116 cdiv->tpos = eval(args[1], 'v');
1117 cdiv->treg = map(args[2]);
1118 } else {
1119 cdiv->treg = -1;
1123 void tr_em(char **args)
1125 trap_em = args[1] ? map(args[1]) : -1;
1128 static int trap_pos(int pos)
1130 int ret = trap_first(pos);
1131 if (bp_final >= 3)
1132 return -1;
1133 if (cdiv)
1134 return cdiv->treg && cdiv->tpos > pos ? cdiv->tpos : -1;
1135 return ret >= 0 ? tposval(ret) : -1;
1138 static int trap_reg(int pos)
1140 int ret = trap_first(pos);
1141 if (cdiv)
1142 return cdiv->treg && cdiv->tpos > pos ? cdiv->treg : -1;
1143 return ret >= 0 ? treg[ret] : -1;
1146 int f_nexttrap(void)
1148 int pos = trap_pos(n_d);
1149 if (cdiv)
1150 return pos >= 0 ? pos : 0x7fffffff;
1151 return (pos >= 0 && pos < n_p ? pos : n_p) - n_d;