map: use starting character lists for keys[]
[neatroff.git] / ren.c
blob2a6d6f899114d406feaac6e895356b25cd98db67
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "roff.h"
7 #define cadj env_adj() /* line buffer */
8 #define RENWB(wb) ((wb) == &ren_wb) /* is ren_wb */
10 /* diversions */
11 struct div {
12 struct sbuf sbuf; /* diversion output */
13 int reg; /* diversion register */
14 int tpos; /* diversion trap position */
15 int treg; /* diversion trap register */
16 int dl; /* diversion width */
17 int prev_d; /* previous \n(.d value */
18 int prev_h; /* previous \n(.h value */
19 int prev_mk; /* previous .mk internal register */
20 int prev_ns; /* previous .ns value */
22 static struct div divs[NPREV]; /* diversion stack */
23 static struct div *cdiv; /* current diversion */
24 static int ren_div; /* rendering a diversion */
25 static int trap_em = -1; /* end macro */
27 static struct wb ren_wb; /* the main ren.c word buffer */
28 static int ren_nl; /* just after newline */
29 static int ren_cnl; /* current char is a newline */
30 static int ren_unbuf[8]; /* ren_back() buffer */
31 static int ren_un;
32 static int ren_fillreq; /* \p request */
33 static int ren_aborted; /* .ab executed */
35 static int bp_first = 1; /* prior to the first page */
36 static int bp_next = 1; /* next page number */
37 static int bp_count; /* number of pages so far */
38 static int bp_ejected; /* current ejected page */
39 static int bp_final; /* 1: executing em, 2: the final page, 3: the 2nd final page */
41 static int c_fa; /* 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_base(int fn, int sz, int wid)
90 /* the original troff rounds the widths up */
91 return (wid * sz + dev_uwid / 2) / dev_uwid;
94 int charwid(int fn, int sz, int wid)
96 if (dev_getcs(fn))
97 return dev_getcs(n_f) * SC_EM / 36;
98 return charwid_base(fn, sz, wid) +
99 (dev_getbd(fn) ? dev_getbd(fn) - 1 : 0);
102 int spacewid(int fn, int sz)
104 return charwid(fn, sz, (dev_font(fn)->spacewid * n_ss + 6) / 12);
107 int f_divreg(void)
109 return cdiv ? cdiv->reg : -1;
112 int f_hpos(void)
114 return adj_wid(cadj) + wb_wid(&ren_wb);
117 void tr_divbeg(char **args)
119 odiv_beg();
120 ren_div++;
123 void tr_divend(char **args)
125 odiv_end();
126 ren_div--;
129 static int trap_reg(int pos);
130 static int trap_pos(int pos);
131 static void trap_exec(int reg);
133 static void ren_page(int pg, int force)
135 if (!force && bp_final >= 2)
136 return;
137 n_nl = 0;
138 n_d = 0;
139 n_h = 0;
140 n_pg = pg;
141 bp_next = n_pg + 1;
142 bp_count++;
143 out("p%d\n", pg);
144 out("V%d\n", 0);
145 if (trap_pos(-1) == 0)
146 trap_exec(trap_reg(-1));
149 static void ren_first(void)
151 if (bp_first && !cdiv) {
152 bp_first = 0;
153 ren_page(bp_next, 1);
157 /* when nodiv, do not append .sp to diversions */
158 static void ren_sp(int n, int nodiv)
160 ren_first();
161 /* ignore .sp without arguments when reading diversions */
162 if (!n && ren_div && !n_u)
163 return;
164 n_d += n ? n : n_v;
165 if (n_d > n_h)
166 n_h = n_d;
167 if (cdiv && !nodiv) {
168 sbuf_putnl(&cdiv->sbuf);
169 sbuf_printf(&cdiv->sbuf, "%csp %du\n", c_cc, n ? n : n_v);
170 } else {
171 n_nl = n_d;
175 static void trap_exec(int reg)
177 if (str_get(reg))
178 in_pushnl(str_get(reg), NULL);
181 static int detect_traps(int beg, int end)
183 int pos = trap_pos(beg);
184 return pos >= 0 && (cdiv || pos < n_p) && pos <= end;
187 /* return 1 if executed a trap */
188 static int ren_traps(int beg, int end, int dosp)
190 int pos = trap_pos(beg);
191 if (detect_traps(beg, end)) {
192 if (dosp && pos > beg)
193 ren_sp(pos - beg, 0);
194 trap_exec(trap_reg(beg));
195 return 1;
197 return 0;
200 static int detect_pagelimit(int ne)
202 return !cdiv && n_nl + ne >= n_p;
205 /* start a new page if needed */
206 static int ren_pagelimit(int ne)
208 if (detect_pagelimit(ne)) {
209 ren_page(bp_next, 0);
210 return 1;
212 return 0;
215 static void down(int n)
217 if (!ren_traps(n_d, n_d + (n ? n : n_v), 1)) {
218 ren_sp(n, 0);
219 ren_pagelimit(0);
223 /* line adjustment */
224 static int ren_ljust(struct sbuf *spre, int w, int ad, int ll, int li, int lt)
226 int ljust = lt >= 0 ? lt : li;
227 int llen = ll - ljust;
228 n_n = w;
229 if (ad == AD_C)
230 ljust += llen > w ? (llen - w) / 2 : 0;
231 if (ad == AD_R)
232 ljust += llen - w;
233 if (ljust)
234 sbuf_printf(spre, "%ch'%du'", c_ec, ljust);
235 if (cdiv && cdiv->dl < w)
236 cdiv->dl = w;
237 return ljust;
240 /* append the line to the current diversion or send it to out.c */
241 static void ren_line(struct sbuf *spre, struct sbuf *sbuf)
243 if (cdiv) {
244 if (!sbuf_empty(spre))
245 sbuf_append(&cdiv->sbuf, sbuf_buf(spre));
246 sbuf_append(&cdiv->sbuf, sbuf_buf(sbuf));
247 } else {
248 out("H%d\n", n_o);
249 out("V%d\n", n_d);
250 if (!sbuf_empty(spre))
251 out_line(sbuf_buf(spre));
252 out_line(sbuf_buf(sbuf));
256 static void ren_transparent(char *s)
258 if (cdiv)
259 sbuf_printf(&cdiv->sbuf, "%s\n", s);
260 else
261 out("%s\n", s);
264 static int zwid(void)
266 struct glyph *g = dev_glyph("0", n_f);
267 return charwid(n_f, n_s, g ? g->wid : SC_DW);
270 /* append the line number to the output line */
271 static void ren_lnum(struct sbuf *spre)
273 char num[16] = "";
274 char dig[16] = "";
275 struct wb wb;
276 int i = 0;
277 wb_init(&wb);
278 if (n_nn <= 0 && (n_ln % n_nM) == 0)
279 sprintf(num, "%d", n_ln);
280 wb_hmov(&wb, n_nI * zwid());
281 if (strlen(num) < 3)
282 wb_hmov(&wb, (3 - strlen(num)) * zwid());
283 while (num[i]) {
284 dig[0] = num[i++];
285 wb_put(&wb, dig);
287 wb_hmov(&wb, n_nS * zwid());
288 sbuf_append(spre, sbuf_buf(&wb.sbuf));
289 wb_done(&wb);
290 if (n_nn > 0)
291 n_nn--;
292 else
293 n_ln++;
296 /* append margin character */
297 static void ren_mc(struct sbuf *sbuf, int w, int ljust)
299 struct wb wb;
300 wb_init(&wb);
301 if (w + ljust < n_l + n_mcn)
302 wb_hmov(&wb, n_l + n_mcn - w - ljust);
303 wb_put(&wb, c_mc);
304 sbuf_append(sbuf, sbuf_buf(&wb.sbuf));
305 wb_done(&wb);
308 /* return 1 if triggered a trap */
309 static int ren_bradj(struct adj *adj, int fill, int ad, int body)
311 char cmd[16];
312 struct sbuf sbuf, spre;
313 int ll, li, lt, els_neg, els_pos;
314 int w, prev_d, lspc, ljust;
315 ren_first();
316 if (!adj_empty(adj, fill)) {
317 sbuf_init(&sbuf);
318 sbuf_init(&spre);
319 w = adj_fill(adj, ad == AD_B, fill, n_hy, &sbuf,
320 &ll, &li, &lt, &els_neg, &els_pos);
321 prev_d = n_d;
322 if (els_neg)
323 ren_sp(-els_neg, 1);
324 if (!n_ns || !sbuf_empty(&sbuf) || els_neg || els_pos) {
325 ren_sp(0, 0);
326 if (!sbuf_empty(&sbuf) && n_nm && body)
327 ren_lnum(&spre);
328 ljust = ren_ljust(&spre, w, ad, ll, li, lt);
329 if (!sbuf_empty(&sbuf) && body && n_mc)
330 ren_mc(&sbuf, w, ljust);
331 ren_line(&spre, &sbuf);
332 n_ns = 0;
334 sbuf_done(&spre);
335 sbuf_done(&sbuf);
336 if (els_pos)
337 ren_sp(els_pos, 1);
338 n_a = els_pos;
339 lspc = MAX(1, n_L) * n_v - n_v;
340 if (detect_traps(prev_d, n_d) || detect_pagelimit(lspc)) {
341 sprintf(cmd, "%c&", c_ec);
342 if (!ren_cnl) /* prevent unwanted newlines */
343 in_push(cmd, NULL);
344 if (!ren_traps(prev_d, n_d, 0))
345 ren_pagelimit(lspc);
346 return 1;
348 if (lspc)
349 down(lspc);
351 return 0;
354 /* return 1 if triggered a trap */
355 static int ren_br(int force)
357 int ad = n_j;
358 if (!n_u || n_na || (n_j == AD_B && force))
359 ad = AD_L;
360 if (n_ce)
361 ad = AD_C;
362 return ren_bradj(cadj, !force && !n_ce && n_u, ad, 1);
365 void tr_br(char **args)
367 if (args[0][0] == c_cc)
368 ren_br(1);
371 void tr_sp(char **args)
373 int traps = 0;
374 int n = args[1] ? eval(args[1], 'v') : n_v;
375 if (args[0][0] == c_cc)
376 traps = ren_br(1);
377 if (n && !n_ns && !traps)
378 down(n);
381 void tr_sv(char **args)
383 int n = eval(args[1], 'v');
384 n_sv = 0;
385 if (n_d + n < f_nexttrap())
386 down(n);
387 else
388 n_sv = n;
391 void tr_ns(char **args)
393 n_ns = 1;
396 void tr_rs(char **args)
398 n_ns = 0;
401 void tr_os(char **args)
403 if (n_sv)
404 down(n_sv);
405 n_sv = 0;
408 void tr_mk(char **args)
410 if (args[1])
411 num_set(map(args[1]), n_d);
412 else
413 n_mk = n_d;
416 void tr_rt(char **args)
418 int n = args[1] ? eval_re(args[1], n_d, 'v') : n_mk;
419 if (n >= 0 && n < n_d)
420 ren_sp(n - n_d, 0);
423 void tr_ne(char **args)
425 int n = args[1] ? eval(args[1], 'v') : n_v;
426 if (!ren_traps(n_d, n_d + n - 1, 1))
427 ren_pagelimit(n);
430 static void push_eject(void)
432 char buf[32];
433 bp_ejected = bp_count;
434 sprintf(buf, "%c%s %d\n", c_cc, TR_EJECT, bp_ejected);
435 in_pushnl(buf, NULL);
438 static void push_br(void)
440 char br[8] = {c_cc, 'b', 'r', '\n'};
441 in_pushnl(br, NULL);
444 static void ren_eject(int id)
446 if (id == bp_ejected && id == bp_count && !cdiv) {
447 if (detect_traps(n_d, n_p)) {
448 push_eject();
449 ren_traps(n_d, n_p, 1);
450 } else {
451 bp_ejected = 0;
452 ren_page(bp_next, 0);
457 void tr_eject(char **args)
459 ren_eject(atoi(args[1]));
462 void tr_bp(char **args)
464 if (!cdiv && (args[1] || !n_ns)) {
465 if (bp_ejected != bp_count)
466 push_eject();
467 if (args[0][0] == c_cc)
468 push_br();
469 if (args[1])
470 bp_next = eval_re(args[1], n_pg, 0);
474 void tr_pn(char **args)
476 if (args[1])
477 bp_next = eval_re(args[1], n_pg, 0);
480 static void ren_ps(char *s)
482 int ps = !s || !*s || !strcmp("0", s) ? n_s0 : eval_re(s, n_s, 0);
483 n_s0 = n_s;
484 n_s = MAX(1, ps);
487 void tr_ps(char **args)
489 ren_ps(args[1]);
492 void tr_ll(char **args)
494 int ll = args[1] ? eval_re(args[1], n_l, 'm') : n_l0;
495 n_l0 = n_l;
496 n_l = MAX(0, ll);
497 adj_ll(cadj, n_l);
500 void tr_in(char **args)
502 int in = args[1] ? eval_re(args[1], n_i, 'm') : n_i0;
503 if (args[0][0] == c_cc)
504 ren_br(1);
505 n_i0 = n_i;
506 n_i = MAX(0, in);
507 adj_in(cadj, n_i);
508 adj_ti(cadj, -1);
511 void tr_ti(char **args)
513 if (args[0][0] == c_cc)
514 ren_br(1);
515 if (args[1])
516 adj_ti(cadj, eval_re(args[1], n_i, 'm'));
519 static void ren_ft(char *s)
521 int fn = !s || !*s || !strcmp("P", s) ? n_f0 : dev_pos(s);
522 if (fn < 0) {
523 errmsg("neatroff: failed to mount <%s>\n", s);
524 } else {
525 n_f0 = n_f;
526 n_f = fn;
530 void tr_ft(char **args)
532 ren_ft(args[1]);
535 void tr_fp(char **args)
537 if (!args[2])
538 return;
539 if (dev_mnt(atoi(args[1]), args[2], args[3] ? args[3] : args[2]) < 0)
540 errmsg("neatroff: failed to mount <%s>\n", args[2]);
543 void tr_nf(char **args)
545 if (args[0][0] == c_cc)
546 ren_br(1);
547 n_u = 0;
550 void tr_fi(char **args)
552 if (args[0][0] == c_cc)
553 ren_br(1);
554 n_u = 1;
557 void tr_ce(char **args)
559 if (args[0][0] == c_cc)
560 ren_br(1);
561 n_ce = args[1] ? atoi(args[1]) : 1;
564 void tr_fc(char **args)
566 if (args[1]) {
567 c_fa = args[1][0];
568 strcpy(c_fb, args[2] ? args[2] : " ");
569 } else {
570 c_fa = -1;
571 c_fb[0] = '\0';
575 static void ren_m(char *s)
577 int m = !s || !*s ? n_m0 : clr_get(s);
578 n_m0 = n_m;
579 n_m = m;
582 void tr_ab(char **args)
584 fprintf(stderr, "%s\n", args[1]);
585 ren_aborted = 1;
588 static void escarg_ren(char *d, int cmd, int (*next)(void), void (*back)(int))
590 char delim[GNLEN];
591 int c;
592 if (strchr(ESC_P, cmd)) {
593 c = next();
594 if (cmd == 's' && (c == '-' || c == '+')) {
595 *d++ = c;
596 c = next();
598 if (c == '(') {
599 *d++ = next();
600 *d++ = next();
601 } else if (!n_cp && c == '[') {
602 c = next();
603 while (c > 0 && c != '\n' && c != ']') {
604 *d++ = c;
605 c = next();
607 } else {
608 *d++ = c;
609 if (cmd == 's' && c >= '1' && c <= '3') {
610 c = next();
611 if (isdigit(c))
612 *d++ = c;
613 else
614 back(c);
618 if (strchr(ESC_Q, cmd)) {
619 schar_read(delim, next);
620 while (schar_jump(delim, next, back)) {
621 if ((c = next()) < 0)
622 break;
623 *d++ = c;
626 *d = '\0';
629 static int nextchar(char *s, int (*next)(void))
631 int c = next();
632 int l = utf8len(c);
633 int i;
634 if (c < 0)
635 return 0;
636 s[0] = c;
637 for (i = 1; i < l; i++)
638 s[i] = next();
639 s[l] = '\0';
640 return l;
643 static void ren_cmd(struct wb *wb, int c, char *arg)
645 switch (c) {
646 case ' ':
647 wb_hmov(wb, spacewid(n_f, n_s));
648 break;
649 case 'b':
650 ren_bcmd(wb, arg);
651 break;
652 case 'c':
653 wb_setpart(wb);
654 break;
655 case 'D':
656 ren_dcmd(wb, arg);
657 break;
658 case 'd':
659 wb_vmov(wb, SC_EM / 2);
660 break;
661 case 'f':
662 ren_ft(arg);
663 break;
664 case 'h':
665 wb_hmov(wb, eval(arg, 'm'));
666 break;
667 case 'k':
668 num_set(map(arg), RENWB(wb) ? f_hpos() - n_lb : wb_wid(wb));
669 break;
670 case 'L':
671 ren_vlcmd(wb, arg);
672 break;
673 case 'l':
674 ren_hlcmd(wb, arg);
675 break;
676 case 'm':
677 ren_m(arg);
678 break;
679 case 'o':
680 ren_ocmd(wb, arg);
681 break;
682 case 'p':
683 if (RENWB(wb))
684 ren_fillreq = 1;
685 break;
686 case 'r':
687 wb_vmov(wb, -SC_EM);
688 break;
689 case 's':
690 ren_ps(arg);
691 break;
692 case 'u':
693 wb_vmov(wb, -SC_EM / 2);
694 break;
695 case 'v':
696 wb_vmov(wb, eval(arg, 'v'));
697 break;
698 case 'X':
699 wb_etc(wb, arg);
700 break;
701 case 'x':
702 wb_els(wb, eval(arg, 'v'));
703 break;
704 case '0':
705 wb_hmov(wb, zwid());
706 break;
707 case '|':
708 wb_hmov(wb, SC_EM / 6);
709 break;
710 case '&':
711 wb_hmov(wb, 0);
712 break;
713 case '^':
714 wb_hmov(wb, SC_EM / 12);
715 break;
716 case '{':
717 case '}':
718 break;
722 static void ren_field(struct wb *wb, int (*next)(void), void (*back)(int));
723 static void ren_tab(struct wb *wb, char *tc, int (*next)(void), void (*back)(int));
725 /* read one character and place it inside wb buffer */
726 void ren_char(struct wb *wb, int (*next)(void), void (*back)(int))
728 char c[GNLEN * 4];
729 char arg[ILNLEN];
730 struct glyph *g;
731 char *s;
732 int w, n, l;
733 nextchar(c, next);
734 if (c[0] == ' ' || c[0] == '\n') {
735 wb_put(wb, c);
736 return;
738 if (c[0] == '\t' || c[0] == '\x01') {
739 ren_tab(wb, c[0] == '\t' ? c_tc : c_lc, next, back);
740 return;
742 if (c[0] == c_fa) {
743 ren_field(wb, next, back);
744 return;
746 if (c[0] == c_ni)
747 nextchar(c + 1, next);
748 if (c[0] == c_ec) {
749 nextchar(c + 1, next);
750 if (c[1] == '(') {
751 l = nextchar(c + 2, next);
752 l += nextchar(c + 2 + l, next);
753 c[2 + l] = '\0';
754 } else if (!n_cp && c[1] == '[') {
755 l = 0;
756 n = next();
757 while (n >= 0 && n != '\n' && n != ']' && l < GNLEN - 1) {
758 c[l++] = n;
759 n = next();
761 c[l] = '\0';
762 } else if (c[1] == 'z') {
763 w = wb_wid(wb);
764 ren_char(wb, next, back);
765 wb_hmov(wb, w - wb_wid(wb));
766 return;
767 } else if (c[1] == '!') {
768 if (ren_nl && next == ren_next) {
769 s = arg;
770 n = next();
771 while (n >= 0 && n != '\n') {
772 *s++ = n;
773 n = next();
775 *s = '\0';
776 ren_transparent(arg);
778 return;
779 } else if (strchr(" bCcDdfHhkLlmNoprSsuvXxz0^|{}&", c[1])) {
780 escarg_ren(arg, c[1], next, back);
781 if (c[1] == 'N') {
782 g = dev_glyph_byid(arg, n_f);
783 c[1] = 'C';
784 strcpy(arg, g ? g->name : "cnull");
786 if (c[1] == 'S' || c[1] == 'H')
787 return; /* not implemented */
788 if (c[1] != 'C') {
789 ren_cmd(wb, c[1], arg);
790 return;
792 strcpy(c, arg);
795 if (!n_lg || wb_lig(wb, c)) {
796 if (n_kn)
797 wb_kern(wb, c);
798 wb_put(wb, c);
802 /* read the argument of \w and push its width */
803 int ren_wid(int (*next)(void), void (*back)(int))
805 char delim[GNLEN];
806 int c, n;
807 struct wb wb;
808 wb_init(&wb);
809 schar_read(delim, next);
810 odiv_beg();
811 c = next();
812 while (c >= 0 && c != '\n') {
813 back(c);
814 if (!schar_jump(delim, next, back))
815 break;
816 ren_char(&wb, next, back);
817 c = next();
819 odiv_end();
820 n = wb_wid(&wb);
821 wb_wconf(&wb, &n_ct, &n_st, &n_sb);
822 wb_done(&wb);
823 return n;
826 /* return 1 if the ending character (ec) was read */
827 static int ren_until(struct wb *wb, char *delim, int ec,
828 int (*next)(void), void (*back)(int))
830 int c;
831 c = next();
832 while (c >= 0 && c != '\n' && c != ec) {
833 back(c);
834 if (!schar_jump(delim, next, back))
835 break;
836 ren_char(wb, next, back);
837 c = next();
839 if (c == '\n')
840 back(c);
841 return c == ec;
844 static void wb_cpy(struct wb *dst, struct wb *src, int left)
846 wb_hmov(dst, left - wb_wid(dst));
847 wb_cat(dst, src);
850 void ren_tl(int (*next)(void), void (*back)(int))
852 struct adj *adj;
853 struct wb wb, wb2;
854 char delim[GNLEN];
855 adj = adj_alloc();
856 wb_init(&wb);
857 wb_init(&wb2);
858 schar_read(delim, next);
859 /* the left-adjusted string */
860 ren_until(&wb2, delim, '\n', next, back);
861 wb_cpy(&wb, &wb2, 0);
862 /* the centered string */
863 ren_until(&wb2, delim, '\n', next, back);
864 wb_cpy(&wb, &wb2, (n_lt - wb_wid(&wb2)) / 2);
865 /* the right-adjusted string */
866 ren_until(&wb2, delim, '\n', next, back);
867 wb_cpy(&wb, &wb2, n_lt - wb_wid(&wb2));
868 /* flushing the line */
869 adj_ll(adj, n_lt);
870 adj_wb(adj, &wb);
871 adj_nl(adj);
872 ren_bradj(adj, 0, AD_L, 0);
873 adj_free(adj);
874 wb_done(&wb2);
875 wb_done(&wb);
878 static void ren_field(struct wb *wb, int (*next)(void), void (*back)(int))
880 struct wb wbs[NFIELDS];
881 int i, n = 0;
882 int wid = 0;
883 int left, right, cur_left;
884 int pad, rem;
885 while (n < LEN(wbs)) {
886 wb_init(&wbs[n]);
887 if (ren_until(&wbs[n++], c_fb, c_fa, next, back))
888 break;
890 left = RENWB(wb) ? f_hpos() : wb_wid(wb);
891 right = tab_next(left);
892 for (i = 0; i < n; i++)
893 wid += wb_wid(&wbs[i]);
894 pad = (right - left - wid) / (n > 1 ? n - 1 : 1);
895 rem = (right - left - wid) % (n > 1 ? n - 1 : 1);
896 for (i = 0; i < n; i++) {
897 if (i == 0)
898 cur_left = left;
899 else if (i == n - 1)
900 cur_left = right - wb_wid(&wbs[i]);
901 else
902 cur_left = wb_wid(wb) + pad + (i + rem >= n);
903 wb_cpy(wb, &wbs[i], cur_left);
904 wb_done(&wbs[i]);
908 static void ren_tab(struct wb *wb, char *tc, int (*next)(void), void (*back)(int))
910 struct wb t;
911 int pos = RENWB(wb) ? f_hpos() : wb_wid(wb);
912 int ins = tab_next(pos); /* insertion position */
913 int typ = tab_type(pos); /* tab type */
914 int c;
915 wb_init(&t);
916 if (typ == 'R' || typ == 'C') {
917 c = next();
918 while (c >= 0 && c != '\n' && c != '\t' && c != '\x01') {
919 back(c);
920 ren_char(&t, next, back);
921 c = next();
923 back(c);
925 if (typ == 'C')
926 ins -= wb_wid(&t) / 2;
927 if (typ == 'R')
928 ins -= wb_wid(&t);
929 if (!tc[0] || ins <= pos)
930 wb_hmov(wb, ins - pos);
931 else
932 ren_hline(wb, ins - pos, tc);
933 wb_cat(wb, &t);
934 wb_done(&t);
937 /* read characters from in.c and pass rendered lines to out.c */
938 int render(void)
940 struct wb *wb = &ren_wb;
941 int fillreq;
942 int c;
943 n_nl = -1;
944 wb_init(wb);
945 tr_first();
946 ren_first(); /* transition to the first page */
947 c = ren_next();
948 while (1) {
949 if (ren_aborted)
950 return 1;
951 if (c < 0) {
952 if (bp_final >= 2)
953 break;
954 if (bp_final == 0 && trap_em >= 0) {
955 trap_exec(trap_em);
956 bp_final = 1;
957 } else {
958 bp_final = 2;
959 push_eject();
960 push_br();
962 c = ren_next();
963 continue;
965 ren_cnl = c == '\n';
966 fillreq = 0;
967 /* add wb (the current word) to cadj */
968 if (c == ' ' || c == '\n') {
969 adj_swid(cadj, spacewid(n_f, n_s));
970 if (!wb_part(wb)) { /* not after a \c */
971 adj_wb(cadj, wb);
972 fillreq = ren_fillreq;
973 ren_fillreq = 0;
974 if (c == '\n')
975 adj_nl(cadj);
976 else
977 adj_sp(cadj);
980 while ((fillreq && !n_ce && n_u) || adj_full(cadj, !n_ce && n_u)) {
981 ren_br(0);
982 fillreq = 0;
984 if (c == '\n' || ren_nl) /* end or start of input line */
985 n_lb = f_hpos();
986 if (c == '\n' && n_it && --n_itn == 0)
987 trap_exec(n_it);
988 if (c == '\n' && !wb_part(wb))
989 n_ce = MAX(0, n_ce - 1);
990 if (c != ' ') {
991 ren_back(c);
992 ren_char(wb, ren_next, ren_back);
993 if (c != '\n' && wb_empty(wb))
994 adj_nonl(cadj);
996 ren_nl = c == '\n';
997 c = ren_next();
999 bp_final = 3;
1000 if (!adj_empty(cadj, 0))
1001 ren_page(bp_next, 1);
1002 ren_br(1);
1003 wb_done(wb);
1004 return 0;
1007 /* trap handling */
1009 #define tposval(i) (tpos[i] < 0 ? n_p + tpos[i] : tpos[i])
1011 static int tpos[NTRAPS]; /* trap positions */
1012 static int treg[NTRAPS]; /* trap registers */
1013 static int ntraps;
1015 static int trap_first(int pos)
1017 int best = -1;
1018 int i;
1019 for (i = 0; i < ntraps; i++)
1020 if (treg[i] >= 0 && tposval(i) > pos)
1021 if (best < 0 || tposval(i) < tposval(best))
1022 best = i;
1023 return best;
1026 static int trap_byreg(int reg)
1028 int i;
1029 for (i = 0; i < ntraps; i++)
1030 if (treg[i] == reg)
1031 return i;
1032 return -1;
1035 static int trap_bypos(int reg, int pos)
1037 int i;
1038 for (i = 0; i < ntraps; i++)
1039 if (treg[i] >= 0 && tposval(i) == pos)
1040 if (reg == -1 || treg[i] == reg)
1041 return i;
1042 return -1;
1045 void tr_wh(char **args)
1047 int reg, pos, id;
1048 if (!args[1])
1049 return;
1050 pos = eval(args[1], 'v');
1051 id = trap_bypos(-1, pos);
1052 if (!args[2]) {
1053 if (id >= 0)
1054 treg[id] = -1;
1055 return;
1057 reg = map(args[2]);
1058 if (id < 0)
1059 id = trap_byreg(-1);
1060 if (id < 0)
1061 id = ntraps++;
1062 tpos[id] = pos;
1063 treg[id] = reg;
1066 void tr_ch(char **args)
1068 int reg;
1069 int id;
1070 if (!args[1])
1071 return;
1072 reg = map(args[1]);
1073 id = trap_byreg(reg);
1074 if (id >= 0)
1075 tpos[id] = args[2] ? eval(args[2], 'v') : -1;
1078 void tr_dt(char **args)
1080 if (!cdiv)
1081 return;
1082 if (args[2]) {
1083 cdiv->tpos = eval(args[1], 'v');
1084 cdiv->treg = map(args[2]);
1085 } else {
1086 cdiv->treg = -1;
1090 void tr_em(char **args)
1092 trap_em = args[1] ? map(args[1]) : -1;
1095 static int trap_pos(int pos)
1097 int ret = trap_first(pos);
1098 if (bp_final >= 3)
1099 return -1;
1100 if (cdiv)
1101 return cdiv->treg && cdiv->tpos > pos ? cdiv->tpos : -1;
1102 return ret >= 0 ? tposval(ret) : -1;
1105 static int trap_reg(int pos)
1107 int ret = trap_first(pos);
1108 if (cdiv)
1109 return cdiv->treg && cdiv->tpos > pos ? cdiv->treg : -1;
1110 return ret >= 0 ? treg[ret] : -1;
1113 int f_nexttrap(void)
1115 int pos = trap_pos(n_d);
1116 if (cdiv)
1117 return pos >= 0 ? pos : 0x7fffffff;
1118 return (pos >= 0 && pos < n_p ? pos : n_p) - n_d;