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
: 0);
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))
688 if (c
[0] == ' ' || c
[0] == '\n') {
692 if (c
[0] == '\t' || c
[0] == '\x01') {
693 ren_tab(wb
, c
[0] == '\t' ? c_tc
: c_lc
, next
, back
);
696 if (c_fa
[0] && !strcmp(c_fa
, c
)) {
697 ren_field(wb
, next
, back
);
703 ren_char(wb
, next
, back
);
704 wb_hmov(wb
, w
- wb_wid(wb
));
708 if (ren_nl
&& next
== ren_next
) {
711 while (n
>= 0 && n
!= '\n') {
716 ren_transparent(arg
);
720 if (strchr(" bCcDdfHhkLlmNoprSsuvXxz0^|{}&/,", c
[1])) {
721 argnext(arg
, c
[1], next
, back
);
722 if (c
[1] == 'S' || c
[1] == 'H')
723 return; /* not implemented */
725 ren_cmd(wb
, c
[1], arg
);
728 snprintf(c
, GNLEN
, "GID=%s", arg
);
731 if (!ren_div
&& cdef_map(c
, n_f
)) { /* .char characters */
735 if (!n_lg
|| ren_div
|| wb_lig(wb
, c
)) {
736 if (n_kn
&& !ren_div
)
742 /* read one character and place it inside wb buffer */
743 int ren_char(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
746 if (charnext(c
, next
, back
) < 0)
748 ren_put(wb
, c
, next
, back
);
752 /* like ren_char(); return 1 if d1 was read and d2 if d2 was read */
753 static int ren_chardel(struct wb
*wb
, int (*next
)(void), void (*back
)(int),
757 if (charnext(c
, next
, back
) < 0)
759 if (d1
&& !strcmp(d1
, c
))
761 if (d2
&& !strcmp(d2
, c
))
763 ren_put(wb
, c
, next
, back
);
767 /* read the argument of \w and push its width */
768 int ren_wid(int (*next
)(void), void (*back
)(int))
774 charnext(delim
, next
, back
);
777 while (c
>= 0 && c
!= '\n') {
779 if (ren_chardel(&wb
, next
, back
, delim
, NULL
))
785 wb_wconf(&wb
, &n_ct
, &n_st
, &n_sb
, &n_llx
, &n_lly
, &n_urx
, &n_ury
);
790 /* return 1 if d1 was read and 2 if d2 was read */
791 static int ren_until(struct wb
*wb
, char *d1
, char *d2
,
792 int (*next
)(void), void (*back
)(int))
796 while (c
>= 0 && c
!= '\n') {
798 ret
= ren_chardel(wb
, next
, back
, d1
, d2
);
808 static void wb_cpy(struct wb
*dst
, struct wb
*src
, int left
)
810 wb_hmov(dst
, left
- wb_wid(dst
));
814 void ren_tl(int (*next
)(void), void (*back
)(int))
822 charnext(delim
, next
, back
);
823 /* the left-adjusted string */
824 ren_until(&wb2
, delim
, NULL
, next
, back
);
825 wb_cpy(&wb
, &wb2
, 0);
826 /* the centered string */
827 ren_until(&wb2
, delim
, NULL
, next
, back
);
828 wb_cpy(&wb
, &wb2
, (n_lt
- wb_wid(&wb2
)) / 2);
829 /* the right-adjusted string */
830 ren_until(&wb2
, delim
, NULL
, next
, back
);
831 wb_cpy(&wb
, &wb2
, n_lt
- wb_wid(&wb2
));
832 /* flushing the line */
836 ren_bradj(adj
, 0, AD_L
, 0);
842 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
844 struct wb wbs
[NFIELDS
];
847 int left
, right
, cur_left
;
849 while (n
< LEN(wbs
)) {
851 if (ren_until(&wbs
[n
++], c_fb
, c_fa
, next
, back
) != 1)
854 left
= RENWB(wb
) ? f_hpos() : wb_wid(wb
);
855 right
= tab_next(left
);
856 for (i
= 0; i
< n
; i
++)
857 wid
+= wb_wid(&wbs
[i
]);
858 pad
= (right
- left
- wid
) / (n
> 1 ? n
- 1 : 1);
859 rem
= (right
- left
- wid
) % (n
> 1 ? n
- 1 : 1);
860 for (i
= 0; i
< n
; i
++) {
864 cur_left
= right
- wb_wid(&wbs
[i
]);
866 cur_left
= wb_wid(wb
) + pad
+ (i
+ rem
>= n
);
867 wb_cpy(wb
, &wbs
[i
], cur_left
);
872 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int))
875 int pos
= RENWB(wb
) ? f_hpos() : wb_wid(wb
);
876 int ins
= tab_next(pos
); /* insertion position */
877 int typ
= tab_type(pos
); /* tab type */
880 if (typ
== 'R' || typ
== 'C') {
882 while (c
>= 0 && c
!= '\n' && c
!= '\t' && c
!= '\x01') {
884 ren_char(&t
, next
, back
);
890 ins
-= wb_wid(&t
) / 2;
893 if (!tc
[0] || ins
<= pos
)
894 wb_hmov(wb
, ins
- pos
);
896 ren_hline(wb
, ins
- pos
, tc
);
901 /* parse characters and troff requests of s and append them to wb */
902 int ren_parse(struct wb
*wb
, char *s
)
910 if (ren_char(wb
, sstr_next
, sstr_back
))
919 /* read characters from in.c and pass rendered lines to out.c */
922 struct wb
*wb
= &ren_wb
;
927 ren_first(); /* transition to the first page */
935 if (bp_final
== 0 && trap_em
>= 0) {
947 /* add wb (the current word) to cadj */
948 if (c
== ' ' || c
== '\n') {
949 adj_swid(cadj
, spacewid(n_f
, n_s
));
950 if (!wb_part(wb
)) { /* not after a \c */
952 /* wb contains only commands like \f */
953 if (!ren_nl
&& wb_empty(wb
))
961 /* flush the line if necessary */
962 if (c
== ' ' || c
== '\n') {
963 while ((ren_fillreq
&& !wb_part(wb
) && !n_ce
&& n_u
) ||
964 adj_full(cadj
, !n_ce
&& n_u
)) {
969 if (c
== '\n' || ren_nl
) /* end or start of input line */
971 if (c
== '\n' && n_it
&& --n_itn
== 0)
973 if (c
== '\n' && !wb_part(wb
))
974 n_ce
= MAX(0, n_ce
- 1);
977 ren_char(wb
, ren_next
, ren_back
);
983 if (!adj_empty(cadj
, 0))
984 ren_page(bp_next
, 1);
992 #define tposval(i) (tpos[i] < 0 ? n_p + tpos[i] : tpos[i])
994 static int tpos
[NTRAPS
]; /* trap positions */
995 static int treg
[NTRAPS
]; /* trap registers */
998 static int trap_first(int pos
)
1002 for (i
= 0; i
< ntraps
; i
++)
1003 if (treg
[i
] >= 0 && tposval(i
) > pos
)
1004 if (best
< 0 || tposval(i
) < tposval(best
))
1009 static int trap_byreg(int reg
)
1012 for (i
= 0; i
< ntraps
; i
++)
1018 static int trap_bypos(int reg
, int pos
)
1021 for (i
= 0; i
< ntraps
; i
++)
1022 if (treg
[i
] >= 0 && tposval(i
) == pos
)
1023 if (reg
== -1 || treg
[i
] == reg
)
1028 void tr_wh(char **args
)
1033 pos
= eval(args
[1], 'v');
1034 id
= trap_bypos(-1, pos
);
1042 id
= trap_byreg(-1);
1049 void tr_ch(char **args
)
1056 id
= trap_byreg(reg
);
1058 tpos
[id
] = args
[2] ? eval(args
[2], 'v') : -1;
1061 void tr_dt(char **args
)
1066 cdiv
->tpos
= eval(args
[1], 'v');
1067 cdiv
->treg
= map(args
[2]);
1073 void tr_em(char **args
)
1075 trap_em
= args
[1] ? map(args
[1]) : -1;
1078 static int trap_pos(int pos
)
1080 int ret
= trap_first(pos
);
1084 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->tpos
: -1;
1085 return ret
>= 0 ? tposval(ret
) : -1;
1088 static int trap_reg(int pos
)
1090 int ret
= trap_first(pos
);
1092 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->treg
: -1;
1093 return ret
>= 0 ? treg
[ret
] : -1;
1096 int f_nexttrap(void)
1098 int pos
= trap_pos(n_d
);
1100 return pos
>= 0 ? pos
: 0x7fffffff;
1101 return (pos
>= 0 && pos
< n_p
? pos
: n_p
) - n_d
;