1 /* rendering lines and managing traps */
8 #define cadj env_adj() /* line buffer */
9 #define RENWB(wb) ((wb) == &ren_wb) /* is ren_wb */
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 struct wb ren_wb
; /* the main ren.c word buffer */
29 static int ren_nl
; /* just after newline */
30 static int ren_cnl
; /* current char is a newline */
31 static int ren_unbuf
[8]; /* ren_back() buffer */
33 static int ren_fillreq
; /* \p request */
34 static int ren_aborted
; /* .ab executed */
36 static int bp_first
= 1; /* prior to the first page */
37 static int bp_next
= 1; /* 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 */
42 static char c_fa
[GNLEN
]; /* field delimiter */
43 static char c_fb
[GNLEN
]; /* field padding */
45 static int ren_next(void)
47 return ren_un
> 0 ? ren_unbuf
[--ren_un
] : tr_next();
50 static void ren_back(int c
)
52 ren_unbuf
[ren_un
++] = c
;
55 void tr_di(char **args
)
58 cdiv
= cdiv
? cdiv
+ 1 : divs
;
59 memset(cdiv
, 0, sizeof(*cdiv
));
60 sbuf_init(&cdiv
->sbuf
);
61 cdiv
->reg
= map(args
[1]);
63 if (args
[0][2] == 'a' && str_get(cdiv
->reg
)) /* .da */
64 sbuf_append(&cdiv
->sbuf
, str_get(cdiv
->reg
));
65 sbuf_printf(&cdiv
->sbuf
, "%c%s\n", c_cc
, TR_DIVBEG
);
75 sbuf_putnl(&cdiv
->sbuf
);
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
);
85 cdiv
= cdiv
> divs
? cdiv
- 1 : NULL
;
89 int charwid(int fn
, int sz
, int wid
)
92 return dev_getcs(n_f
) * SC_EM
/ 36;
93 return DEVWID(sz
, wid
) + (dev_getbd(fn
) ? dev_getbd(fn
) - 1 : 0);
96 int spacewid(int fn
, int sz
)
98 return charwid(fn
, sz
, (dev_font(fn
)->spacewid
* n_ss
+ 6) / 12);
103 return cdiv
? cdiv
->reg
: -1;
108 return adj_wid(cadj
) + wb_wid(&ren_wb
);
111 void tr_divbeg(char **args
)
117 void tr_divend(char **args
)
123 static int trap_reg(int pos
);
124 static int trap_pos(int pos
);
125 static void trap_exec(int reg
);
127 static void ren_page(int pg
, int force
)
129 if (!force
&& bp_final
>= 2)
139 if (trap_pos(-1) == 0)
140 trap_exec(trap_reg(-1));
143 static void ren_first(void)
145 if (bp_first
&& !cdiv
) {
147 ren_page(bp_next
, 1);
151 /* when nodiv, do not append .sp to diversions */
152 static void ren_sp(int n
, int nodiv
)
155 /* ignore .sp without arguments when reading diversions */
156 if (!n
&& ren_div
&& !n_u
)
161 if (cdiv
&& !nodiv
) {
162 sbuf_putnl(&cdiv
->sbuf
);
163 sbuf_printf(&cdiv
->sbuf
, "%csp %du\n", c_cc
, n
? n
: n_v
);
169 static void trap_exec(int reg
)
172 in_pushnl(str_get(reg
), NULL
);
175 static int detect_traps(int beg
, int end
)
177 int pos
= trap_pos(beg
);
178 return pos
>= 0 && (cdiv
|| pos
< n_p
) && pos
<= end
;
181 /* return 1 if executed a trap */
182 static int ren_traps(int beg
, int end
, int dosp
)
184 int pos
= trap_pos(beg
);
185 if (detect_traps(beg
, end
)) {
186 if (dosp
&& pos
> beg
)
187 ren_sp(pos
- beg
, 0);
188 trap_exec(trap_reg(beg
));
194 static int detect_pagelimit(int ne
)
196 return !cdiv
&& n_nl
+ ne
>= n_p
;
199 /* start a new page if needed */
200 static int ren_pagelimit(int ne
)
202 if (detect_pagelimit(ne
)) {
203 ren_page(bp_next
, 0);
209 /* return 1 if triggered a trap */
210 static int down(int n
)
212 if (ren_traps(n_d
, n_d
+ (n
? n
: n_v
), 1))
215 return ren_pagelimit(0);
218 /* line adjustment */
219 static int ren_ljust(struct sbuf
*spre
, int w
, int ad
, int ll
, int li
, int lt
)
221 int ljust
= lt
>= 0 ? lt
: li
;
222 int llen
= ll
- ljust
;
225 ljust
+= llen
> w
? (llen
- w
) / 2 : 0;
229 sbuf_printf(spre
, "%ch'%du'", c_ec
, ljust
);
230 if (cdiv
&& cdiv
->dl
< w
+ ljust
)
231 cdiv
->dl
= w
+ ljust
;
235 /* append the line to the current diversion or send it to out.c */
236 static void ren_line(struct sbuf
*spre
, struct sbuf
*sbuf
)
239 if (!sbuf_empty(spre
))
240 sbuf_append(&cdiv
->sbuf
, sbuf_buf(spre
));
241 sbuf_append(&cdiv
->sbuf
, sbuf_buf(sbuf
));
245 if (!sbuf_empty(spre
))
246 out_line(sbuf_buf(spre
));
247 out_line(sbuf_buf(sbuf
));
251 static void ren_transparent(char *s
)
254 sbuf_printf(&cdiv
->sbuf
, "%s\n", s
);
259 static int zwid(void)
261 struct glyph
*g
= dev_glyph("0", n_f
);
262 return charwid(n_f
, n_s
, g
? g
->wid
: SC_DW
);
265 /* append the line number to the output line */
266 static void ren_lnum(struct sbuf
*spre
)
273 if (n_nn
<= 0 && (n_ln
% n_nM
) == 0)
274 sprintf(num
, "%d", n_ln
);
275 wb_hmov(&wb
, n_nI
* zwid());
277 wb_hmov(&wb
, (3 - strlen(num
)) * zwid());
282 wb_hmov(&wb
, n_nS
* zwid());
283 sbuf_append(spre
, sbuf_buf(&wb
.sbuf
));
291 /* append margin character */
292 static void ren_mc(struct sbuf
*sbuf
, int w
, int ljust
)
296 if (w
+ ljust
< n_l
+ n_mcn
)
297 wb_hmov(&wb
, n_l
+ n_mcn
- w
- ljust
);
298 wb_putexpand(&wb
, c_mc
);
299 sbuf_append(sbuf
, sbuf_buf(&wb
.sbuf
));
303 /* output current line; returns 1 if triggered a trap */
304 static int ren_bradj(struct adj
*adj
, int fill
, int ad
, int body
)
307 struct sbuf sbuf
, spre
;
308 int ll
, li
, lt
, els_neg
, els_pos
;
309 int w
, hyph
, prev_d
, lspc
, ljust
;
311 if (!adj_empty(adj
, fill
)) {
315 lspc
= MAX(1, n_L
) * n_v
; /* line space, ignoreing \x */
316 if (n_hy
& HY_LAST
&& (detect_traps(n_d
, n_d
+ lspc
) ||
317 detect_pagelimit(lspc
)))
318 hyph
= 0; /* disable for last lines */
319 w
= adj_fill(adj
, ad
== AD_B
, fill
, hyph
, &sbuf
,
320 &ll
, &li
, <
, &els_neg
, &els_pos
);
324 if (!n_ns
|| !sbuf_empty(&sbuf
) || els_neg
|| els_pos
) {
326 if (!sbuf_empty(&sbuf
) && n_nm
&& body
)
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
);
339 if (detect_traps(prev_d
, n_d
) || detect_pagelimit(lspc
- n_v
)) {
340 sprintf(cmd
, "%c&", c_ec
);
341 if (!ren_cnl
) /* prevent unwanted newlines */
343 if (!ren_traps(prev_d
, n_d
, 0))
344 ren_pagelimit(lspc
- n_v
);
347 if (lspc
- n_v
&& down(lspc
- n_v
))
353 /* output current line; returns 1 if triggered a trap */
354 static int ren_br(int force
)
357 if (!n_u
|| n_na
|| (n_j
== AD_B
&& force
))
361 return ren_bradj(cadj
, !force
&& !n_ce
&& n_u
, ad
, 1);
364 void tr_br(char **args
)
366 if (args
[0][0] == c_cc
)
370 void tr_sp(char **args
)
373 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
374 if (args
[0][0] == c_cc
)
376 if (n
&& !n_ns
&& !traps
)
380 void tr_sv(char **args
)
382 int n
= eval(args
[1], 'v');
384 if (n_d
+ n
< f_nexttrap())
390 void tr_ns(char **args
)
395 void tr_rs(char **args
)
400 void tr_os(char **args
)
407 void tr_mk(char **args
)
410 num_set(map(args
[1]), n_d
);
415 void tr_rt(char **args
)
417 int n
= args
[1] ? eval_re(args
[1], n_d
, 'v') : n_mk
;
418 if (n
>= 0 && n
< n_d
)
422 void tr_ne(char **args
)
424 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
425 if (!ren_traps(n_d
, n_d
+ n
- 1, 1))
429 static void push_eject(void)
432 bp_ejected
= bp_count
;
433 sprintf(buf
, "%c%s %d\n", c_cc
, TR_EJECT
, bp_ejected
);
434 in_pushnl(buf
, NULL
);
437 static void push_br(void)
439 char br
[8] = {c_cc
, 'b', 'r', '\n'};
443 static void ren_eject(int id
)
445 if (id
== bp_ejected
&& id
== bp_count
&& !cdiv
) {
446 if (detect_traps(n_d
, n_p
)) {
448 ren_traps(n_d
, n_p
, 1);
451 ren_page(bp_next
, 0);
456 void tr_eject(char **args
)
458 ren_eject(atoi(args
[1]));
461 void tr_bp(char **args
)
463 if (!cdiv
&& (args
[1] || !n_ns
)) {
464 if (bp_ejected
!= bp_count
)
466 if (args
[0][0] == c_cc
)
469 bp_next
= eval_re(args
[1], n_pg
, 0);
473 void tr_pn(char **args
)
476 bp_next
= eval_re(args
[1], n_pg
, 0);
479 static void ren_ps(char *s
)
481 int ps
= !s
|| !*s
|| !strcmp("0", s
) ? n_s0
: eval_re(s
, n_s
, 0);
486 void tr_ps(char **args
)
491 void tr_ll(char **args
)
493 int ll
= args
[1] ? eval_re(args
[1], n_l
, 'm') : n_l0
;
499 void tr_in(char **args
)
501 int in
= args
[1] ? eval_re(args
[1], n_i
, 'm') : n_i0
;
502 if (args
[0][0] == c_cc
)
510 void tr_ti(char **args
)
512 if (args
[0][0] == c_cc
)
515 adj_ti(cadj
, eval_re(args
[1], n_i
, 'm'));
518 static void ren_ft(char *s
)
520 int fn
= !s
|| !*s
|| !strcmp("P", s
) ? n_f0
: dev_pos(s
);
522 errmsg("neatroff: failed to mount <%s>\n", s
);
529 void tr_ft(char **args
)
534 void tr_fp(char **args
)
538 if (dev_mnt(atoi(args
[1]), args
[2], args
[3] ? args
[3] : args
[2]) < 0)
539 errmsg("neatroff: failed to mount <%s>\n", args
[2]);
542 void tr_nf(char **args
)
544 if (args
[0][0] == c_cc
)
549 void tr_fi(char **args
)
551 if (args
[0][0] == c_cc
)
556 void tr_ce(char **args
)
558 if (args
[0][0] == c_cc
)
560 n_ce
= args
[1] ? atoi(args
[1]) : 1;
563 void tr_fc(char **args
)
567 if (fa
&& charread(&fa
, c_fa
) >= 0) {
568 if (!fb
|| charread(&fb
, c_fb
) < 0)
576 static void ren_cl(char *s
)
578 int m
= !s
|| !*s
? n_m0
: clr_get(s
);
583 void tr_cl(char **args
)
588 void tr_ab(char **args
)
590 fprintf(stderr
, "%s\n", args
[1]);
594 static void ren_cmd(struct wb
*wb
, int c
, char *arg
)
598 wb_hmov(wb
, spacewid(n_f
, n_s
));
610 wb_vmov(wb
, SC_EM
/ 2);
616 wb_hmov(wb
, eval(arg
, 'm'));
619 num_set(map(arg
), RENWB(wb
) ? f_hpos() - n_lb
: wb_wid(wb
));
644 wb_vmov(wb
, -SC_EM
/ 2);
647 wb_vmov(wb
, eval(arg
, 'v'));
653 wb_els(wb
, eval(arg
, 'v'));
659 wb_hmov(wb
, SC_EM
/ 6);
665 wb_hmov(wb
, SC_EM
/ 12);
668 wb_italiccorrection(wb
);
671 wb_italiccorrectionleft(wb
);
679 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int));
680 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int));
682 /* insert a character, escape sequence, field or etc into wb */
683 static void ren_put(struct wb
*wb
, char *c
, int (*next
)(void), void (*back
)(int))
689 if (c
[0] == ' ' || c
[0] == '\n') {
693 if (c
[0] == '\t' || c
[0] == '\x01') {
694 ren_tab(wb
, c
[0] == '\t' ? c_tc
: c_lc
, next
, back
);
697 if (c_fa
[0] && !strcmp(c_fa
, c
)) {
698 ren_field(wb
, next
, back
);
704 ren_char(wb
, next
, back
);
705 wb_hmov(wb
, w
- wb_wid(wb
));
709 if (ren_nl
&& next
== ren_next
) {
712 while (n
>= 0 && n
!= '\n') {
717 ren_transparent(arg
);
721 if (strchr(" bCcDdfHhkLlmNoprSsuvXxz0^|{}&/,", c
[1])) {
722 argnext(arg
, c
[1], next
, back
);
723 if (c
[1] == 'S' || c
[1] == 'H')
724 return; /* not implemented */
726 ren_cmd(wb
, c
[1], arg
);
729 g
= dev_glyph_byid(arg
, n_f
);
730 strcpy(c
, g
? g
->name
: "cnull");
733 if (!ren_div
&& cdef_map(c
, n_f
)) { /* .char characters */
737 if (!n_lg
|| ren_div
|| wb_lig(wb
, c
)) {
738 if (n_kn
&& !ren_div
)
744 /* read one character and place it inside wb buffer */
745 int ren_char(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
748 if (charnext(c
, next
, back
) < 0)
750 ren_put(wb
, c
, next
, back
);
754 /* like ren_char(); return 1 if d1 was read and d2 if d2 was read */
755 static int ren_chardel(struct wb
*wb
, int (*next
)(void), void (*back
)(int),
759 if (charnext(c
, next
, back
) < 0)
761 if (d1
&& !strcmp(d1
, c
))
763 if (d2
&& !strcmp(d2
, c
))
765 ren_put(wb
, c
, next
, back
);
769 /* read the argument of \w and push its width */
770 int ren_wid(int (*next
)(void), void (*back
)(int))
776 charnext(delim
, next
, back
);
779 while (c
>= 0 && c
!= '\n') {
781 if (ren_chardel(&wb
, next
, back
, delim
, NULL
))
787 wb_wconf(&wb
, &n_ct
, &n_st
, &n_sb
);
792 /* return 1 if d1 was read and 2 if d2 was read */
793 static int ren_until(struct wb
*wb
, char *d1
, char *d2
,
794 int (*next
)(void), void (*back
)(int))
798 while (c
>= 0 && c
!= '\n') {
800 ret
= ren_chardel(wb
, next
, back
, d1
, d2
);
810 static void wb_cpy(struct wb
*dst
, struct wb
*src
, int left
)
812 wb_hmov(dst
, left
- wb_wid(dst
));
816 void ren_tl(int (*next
)(void), void (*back
)(int))
824 charnext(delim
, next
, back
);
825 /* the left-adjusted string */
826 ren_until(&wb2
, delim
, NULL
, next
, back
);
827 wb_cpy(&wb
, &wb2
, 0);
828 /* the centered string */
829 ren_until(&wb2
, delim
, NULL
, next
, back
);
830 wb_cpy(&wb
, &wb2
, (n_lt
- wb_wid(&wb2
)) / 2);
831 /* the right-adjusted string */
832 ren_until(&wb2
, delim
, NULL
, next
, back
);
833 wb_cpy(&wb
, &wb2
, n_lt
- wb_wid(&wb2
));
834 /* flushing the line */
838 ren_bradj(adj
, 0, AD_L
, 0);
844 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
846 struct wb wbs
[NFIELDS
];
849 int left
, right
, cur_left
;
851 while (n
< LEN(wbs
)) {
853 if (ren_until(&wbs
[n
++], c_fb
, c_fa
, next
, back
) != 1)
856 left
= RENWB(wb
) ? f_hpos() : wb_wid(wb
);
857 right
= tab_next(left
);
858 for (i
= 0; i
< n
; i
++)
859 wid
+= wb_wid(&wbs
[i
]);
860 pad
= (right
- left
- wid
) / (n
> 1 ? n
- 1 : 1);
861 rem
= (right
- left
- wid
) % (n
> 1 ? n
- 1 : 1);
862 for (i
= 0; i
< n
; i
++) {
866 cur_left
= right
- wb_wid(&wbs
[i
]);
868 cur_left
= wb_wid(wb
) + pad
+ (i
+ rem
>= n
);
869 wb_cpy(wb
, &wbs
[i
], cur_left
);
874 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int))
877 int pos
= RENWB(wb
) ? f_hpos() : wb_wid(wb
);
878 int ins
= tab_next(pos
); /* insertion position */
879 int typ
= tab_type(pos
); /* tab type */
882 if (typ
== 'R' || typ
== 'C') {
884 while (c
>= 0 && c
!= '\n' && c
!= '\t' && c
!= '\x01') {
886 ren_char(&t
, next
, back
);
892 ins
-= wb_wid(&t
) / 2;
895 if (!tc
[0] || ins
<= pos
)
896 wb_hmov(wb
, ins
- pos
);
898 ren_hline(wb
, ins
- pos
, tc
);
903 /* parse characters and troff requests of s and append them to wb */
904 int ren_parse(struct wb
*wb
, char *s
)
912 if (ren_char(wb
, sstr_next
, sstr_back
))
921 /* read characters from in.c and pass rendered lines to out.c */
924 struct wb
*wb
= &ren_wb
;
929 ren_first(); /* transition to the first page */
937 if (bp_final
== 0 && trap_em
>= 0) {
949 /* add wb (the current word) to cadj */
950 if (c
== ' ' || c
== '\n') {
951 adj_swid(cadj
, spacewid(n_f
, n_s
));
952 if (!wb_part(wb
)) { /* not after a \c */
960 /* flush the line if necessary */
961 if (c
== ' ' || c
== '\n') {
962 while ((ren_fillreq
&& !wb_part(wb
) && !n_ce
&& n_u
) ||
963 adj_full(cadj
, !n_ce
&& n_u
)) {
968 if (c
== '\n' || ren_nl
) /* end or start of input line */
970 if (c
== '\n' && n_it
&& --n_itn
== 0)
972 if (c
== '\n' && !wb_part(wb
))
973 n_ce
= MAX(0, n_ce
- 1);
976 ren_char(wb
, ren_next
, ren_back
);
977 if (c
!= '\n' && wb_empty(wb
))
984 if (!adj_empty(cadj
, 0))
985 ren_page(bp_next
, 1);
993 #define tposval(i) (tpos[i] < 0 ? n_p + tpos[i] : tpos[i])
995 static int tpos
[NTRAPS
]; /* trap positions */
996 static int treg
[NTRAPS
]; /* trap registers */
999 static int trap_first(int pos
)
1003 for (i
= 0; i
< ntraps
; i
++)
1004 if (treg
[i
] >= 0 && tposval(i
) > pos
)
1005 if (best
< 0 || tposval(i
) < tposval(best
))
1010 static int trap_byreg(int reg
)
1013 for (i
= 0; i
< ntraps
; i
++)
1019 static int trap_bypos(int reg
, int pos
)
1022 for (i
= 0; i
< ntraps
; i
++)
1023 if (treg
[i
] >= 0 && tposval(i
) == pos
)
1024 if (reg
== -1 || treg
[i
] == reg
)
1029 void tr_wh(char **args
)
1034 pos
= eval(args
[1], 'v');
1035 id
= trap_bypos(-1, pos
);
1043 id
= trap_byreg(-1);
1050 void tr_ch(char **args
)
1057 id
= trap_byreg(reg
);
1059 tpos
[id
] = args
[2] ? eval(args
[2], 'v') : -1;
1062 void tr_dt(char **args
)
1067 cdiv
->tpos
= eval(args
[1], 'v');
1068 cdiv
->treg
= map(args
[2]);
1074 void tr_em(char **args
)
1076 trap_em
= args
[1] ? map(args
[1]) : -1;
1079 static int trap_pos(int pos
)
1081 int ret
= trap_first(pos
);
1085 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->tpos
: -1;
1086 return ret
>= 0 ? tposval(ret
) : -1;
1089 static int trap_reg(int pos
)
1091 int ret
= trap_first(pos
);
1093 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->treg
: -1;
1094 return ret
>= 0 ? treg
[ret
] : -1;
1097 int f_nexttrap(void)
1099 int pos
= trap_pos(n_d
);
1101 return pos
>= 0 ? pos
: 0x7fffffff;
1102 return (pos
>= 0 && pos
< n_p
? pos
: n_p
) - n_d
;