1 /* rendering lines and managing traps */
8 #define NOPAGE 0x40000000 /* undefined bp_next */
9 #define cfmt env_fmt() /* current formatter */
10 #define cwb env_wb() /* current word buffer */
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 */
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
)
59 cdiv
= cdiv
? cdiv
+ 1 : divs
;
60 memset(cdiv
, 0, sizeof(*cdiv
));
61 sbuf_init(&cdiv
->sbuf
);
62 cdiv
->reg
= map(args
[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
);
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
;
91 return cdiv
? cdiv
->reg
: -1;
96 return fmt_wid(cfmt
) + wb_wid(cwb
);
99 void tr_divbeg(char **args
)
105 void tr_divend(char **args
)
108 errdie("neatroff: diversion stack empty\n");
113 void tr_divvs(char **args
)
115 ren_divvs
= eval(args
[1], 'u');
118 void tr_transparent(char **args
)
121 sbuf_printf(&cdiv
->sbuf
, "%s\n", args
[1]);
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)
137 n_pg
= bp_next
!= NOPAGE
? bp_next
: n_pg
+ 1;
142 if (trap_pos(-1) == 0)
143 trap_exec(trap_reg(-1));
146 static int ren_first(void)
148 if (bp_first
&& !cdiv
) {
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 */
161 if (!n
&& ren_div
&& ren_divvs
&& !n_u
)
162 n
= ren_divvs
; /* .v at the time of diversion */
168 if (cdiv
&& !nodiv
) {
170 sbuf_printf(&cdiv
->sbuf
, "%c%s %du\n", c_cc
, TR_DIVVS
, n_v
);
172 sbuf_printf(&cdiv
->sbuf
, "%csp %du\n", c_cc
, n
? n
: n_v
);
178 static int render_rec(int level
);
180 static void trap_exec(int reg
)
183 int partial
= ren_partial
&& (!ren_un
|| ren_unbuf
[0] != '\n');
185 sprintf(cmd
, "%c%s %d\n", c_cc
, TR_POPREN
, ren_level
);
187 in_push(str_get(reg
), NULL
);
190 render_rec(++ren_level
);
191 /* executed the trap while in the middle of an input line */
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
));
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
)) {
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))
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
)
244 int llen
= ll
- ljust
;
246 if ((ad
& AD_B
) == AD_C
)
247 ljust
+= llen
> w
? (llen
- w
) / 2 : 0;
248 if ((ad
& AD_B
) == AD_R
)
251 sbuf_printf(spre
, "%ch'%du'", c_ec
, ljust
);
252 if (cdiv
&& cdiv
->dl
< w
+ ljust
)
253 cdiv
->dl
= w
+ 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
)
261 sbuf_append(&cdiv
->sbuf
, beg
);
262 sbuf_append(&cdiv
->sbuf
, mid
);
263 sbuf_append(&cdiv
->sbuf
, end
);
264 sbuf_append(&cdiv
->sbuf
, "\n");
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
)
288 if (n_nn
<= 0 && (n_ln
% n_nM
) == 0)
289 sprintf(num
, "%d", n_ln
);
290 wb_hmov(&wb
, n_nI
* zwid());
292 wb_hmov(&wb
, (3 - strlen(num
)) * zwid());
297 wb_hmov(&wb
, n_nS
* zwid());
298 sbuf_append(spre
, wb_buf(&wb
));
306 /* append margin character */
307 static void ren_mc(struct sbuf
*sbuf
, int w
, int ljust
)
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
));
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
;
327 lspc
= MAX(1, n_L
) * n_v
; /* line space, ignoreing \x */
329 if (!n_ns
|| line
[0] || els_neg
|| els_pos
) {
333 if (line
[0] && n_nm
&& body
)
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
));
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);
351 if (lspc
- n_v
&& down(lspc
- n_v
))
356 /* read a line from fmt and send it to ren_line() */
357 static int ren_passline(struct fmt
*fmt
)
360 int ll
, li
, els_neg
, els_pos
, w
, ret
;
363 if (!fmt_morewords(fmt
))
366 fmt_nextline(fmt
, &sbuf
, &w
, &li
, &ll
, &els_neg
, &els_pos
);
367 if ((n_cp
&& !n_u
) || n_na
)
371 ret
= ren_line(sbuf_buf(&sbuf
), w
, ad
, 1, li
, ll
, els_neg
, els_pos
);
376 /* output formatted lines in fmt */
377 static int ren_fmtpop(struct fmt
*fmt
)
380 while (fmt_morelines(fmt
))
381 ret
= ren_passline(fmt
);
385 /* format and output all lines in fmt */
386 static void ren_fmtpopall(struct fmt
*fmt
)
388 while (fmt_fill(fmt
, 0))
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
))
401 /* output current line; returns 1 if triggered a trap */
402 static int ren_br(void)
406 while (fmt_fill(cfmt
, 1))
408 return ren_fmtpop(cfmt
);
411 void tr_br(char **args
)
413 if (args
[0][0] == c_cc
)
416 ren_fmtpopall(cfmt
); /* output the completed lines */
419 void tr_sp(char **args
)
422 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
423 if (args
[0][0] == c_cc
)
425 if (n
&& (!n_ns
|| ren_div
) && !traps
)
429 void tr_sv(char **args
)
431 int n
= eval(args
[1], 'v');
433 if (n_d
+ n
< f_nexttrap())
439 void tr_ns(char **args
)
444 void tr_rs(char **args
)
449 void tr_os(char **args
)
456 void tr_mk(char **args
)
459 num_set(map(args
[1]), 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
)
471 void tr_ne(char **args
)
473 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
476 if (!ren_traps(n_d
, n_d
+ n
- 1, 1))
480 static void ren_ejectpage(int br
)
483 bp_ejected
= bp_count
;
486 while (bp_count
== bp_ejected
&& !cdiv
) {
487 if (detect_traps(n_d
, n_p
)) {
488 ren_traps(n_d
, n_p
, 1);
496 void tr_bp(char **args
)
498 if (!cdiv
&& (args
[1] || !n_ns
)) {
500 bp_next
= eval_re(args
[1], n_pg
, 0);
501 ren_ejectpage(args
[0][0] == c_cc
);
505 void tr_pn(char **args
)
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');
516 n_s
= MAX(1, (ps
+ SC_PT
/ 2) / SC_PT
);
519 void tr_ps(char **args
)
524 void tr_ll(char **args
)
526 int ll
= args
[1] ? eval_re(args
[1], n_l
, 'm') : n_l0
;
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
)
541 void tr_ti(char **args
)
543 if (args
[0][0] == c_cc
)
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
);
553 errmsg("neatroff: failed to mount <%s>\n", s
);
560 void tr_ft(char **args
)
565 void tr_fp(char **args
)
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
)
580 void tr_fi(char **args
)
582 if (args
[0][0] == c_cc
)
587 void tr_ce(char **args
)
589 if (args
[0][0] == c_cc
)
591 n_ce
= args
[1] ? atoi(args
[1]) : 1;
594 void tr_fc(char **args
)
598 if (fa
&& charread(&fa
, c_fa
) >= 0) {
599 if (!fb
|| charread(&fb
, c_fb
) < 0)
607 static void ren_cl(char *s
)
609 int m
= !s
|| !*s
? n_m0
: clr_get(s
);
614 void tr_cl(char **args
)
619 void tr_ab(char **args
)
621 fprintf(stderr
, "%s\n", args
[1]);
625 static void ren_cmd(struct wb
*wb
, int c
, char *arg
)
629 wb_hmov(wb
, font_swid(dev_font(n_f
), n_s
, n_ss
));
641 wb_vmov(wb
, SC_EM
/ 2);
647 wb_hmov(wb
, eval(arg
, 'm'));
650 num_set(map(arg
), wb
== cwb
? f_hpos() - n_lb
: wb_wid(wb
));
666 while (fmt_fillreq(cfmt
))
676 wb_vmov(wb
, -SC_EM
/ 2);
679 wb_vmov(wb
, eval(arg
, 'v'));
685 wb_els(wb
, eval(arg
, 'v'));
694 wb_hmov(wb
, SC_EM
/ 6);
700 wb_hmov(wb
, SC_EM
/ 12);
703 wb_italiccorrection(wb
);
706 wb_italiccorrectionleft(wb
);
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))
719 if (c
[0] == ' ' || c
[0] == '\n') {
723 if (c
[0] == '\t' || c
[0] == '\x01') {
724 ren_tab(wb
, c
[0] == '\t' ? c_tc
: c_lc
, next
, back
);
727 if (c_fa
[0] && !strcmp(c_fa
, c
)) {
728 ren_field(wb
, next
, back
);
734 ren_char(wb
, next
, back
);
735 wb_hmov(wb
, w
- wb_wid(wb
));
738 if (strchr(" bCcDdefHhkLlmNoprSsuvXxZz0^|!{}&/,", c
[1])) {
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
);
745 snprintf(c
, GNLEN
, "%c%c", c_ec
, c_ec
);
746 } else if (c
[1] == 'N') {
747 snprintf(c
, GNLEN
, "GID=%s", arg
);
749 ren_cmd(wb
, c
[1], arg
);
758 if (cdef_map(c
, n_f
)) /* .char characters */
764 /* read one character and place it inside wb buffer */
765 int ren_char(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
768 if (charnext(c
, next
, back
) < 0)
770 ren_put(wb
, c
, next
, back
);
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),
779 if (charnext(c
, next
, back
) < 0)
781 if (d1
&& !strcmp(d1
, c
))
783 if (d2
&& !strcmp(d2
, c
))
785 ren_put(wb
, c
, next
, back
);
789 /* read the argument of \w and push its width */
790 int ren_wid(int (*next
)(void), void (*back
)(int))
796 charnext(delim
, next
, back
);
799 while (c
>= 0 && c
!= '\n') {
801 if (ren_chardel(&wb
, next
, back
, delim
, NULL
))
807 wb_wconf(&wb
, &n_ct
, &n_st
, &n_sb
, &n_llx
, &n_lly
, &n_urx
, &n_ury
);
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),
818 while (c
>= 0 && c
!= '\n') {
820 ret
= ren_chardel(wb
, next
, back
, d1
, d2
);
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
)
835 while ((ret
= ren_until(wb
, next
, back
, src
, end
)) == 1) {
837 ren_until(wb
, sstr_next
, sstr_back
, end
, NULL
);
843 static void wb_cpy(struct wb
*dst
, struct wb
*src
, int left
)
845 wb_hmov(dst
, left
- wb_wid(dst
));
849 void ren_tl(int (*next
)(void), void (*back
)(int))
855 pgnum
= num_str(map("%"));
858 charnext(delim
, next
, back
);
859 if (!strcmp("\n", delim
))
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
);
877 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
879 struct wb wbs
[NFIELDS
];
882 int left
, right
, cur_left
;
884 while (n
< LEN(wbs
)) {
886 if (ren_until(&wbs
[n
++], next
, back
, c_fb
, c_fa
) != 1)
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
++) {
899 cur_left
= right
- wb_wid(&wbs
[i
]);
901 cur_left
= wb_wid(wb
) + pad
+ (i
+ rem
>= n
);
902 wb_cpy(wb
, &wbs
[i
], cur_left
);
907 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int))
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 */
915 if (typ
== 'R' || typ
== 'C') {
917 while (c
>= 0 && c
!= '\n' && c
!= '\t' && c
!= '\x01') {
919 ren_char(&t
, next
, back
);
925 ins
-= wb_wid(&t
) / 2;
928 if (!tc
[0] || ins
<= pos
)
929 wb_hmov(wb
, ins
- pos
);
931 ren_hline(wb
, ins
- pos
, tc
);
936 /* parse characters and troff requests of s and append them to wb */
937 int ren_parse(struct wb
*wb
, char *s
)
945 if (ren_char(wb
, sstr_next
, sstr_back
))
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
)
966 while (ren_level
>= level
) {
967 while (!ren_un
&& !tr_nextreq())
968 if (ren_level
< level
)
970 if (ren_level
< level
)
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 */
994 while (fmt_newline(cfmt
))
1002 /* flush the line if necessary */
1003 if ((!FMT_PAR() && (c
== ' ' || c
== '\n')) || c
< 0)
1005 if (c
== '\n' || ren_nl
) /* end or start of input line */
1007 if (c
== '\n' && n_it
&& --n_itn
== 0)
1009 if (c
== '\n' && !wb_part(cwb
))
1010 n_ce
= MAX(0, n_ce
- 1);
1011 if (c
!= ' ' && c
>= 0) {
1013 ren_char(cwb
, ren_next
, ren_back
);
1021 /* render input words */
1025 while (!tr_nextreq())
1027 ren_first(); /* transition to the first page */
1030 if (fmt_morewords(cfmt
))
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 */
1044 static int trap_first(int pos
)
1048 for (i
= 0; i
< ntraps
; i
++)
1049 if (treg
[i
] >= 0 && tposval(i
) > pos
)
1050 if (best
< 0 || tposval(i
) < tposval(best
))
1055 static int trap_byreg(int reg
)
1058 for (i
= 0; i
< ntraps
; i
++)
1064 static int trap_bypos(int reg
, int pos
)
1067 for (i
= 0; i
< ntraps
; i
++)
1068 if (treg
[i
] >= 0 && tposval(i
) == pos
)
1069 if (reg
== -1 || treg
[i
] == reg
)
1074 void tr_wh(char **args
)
1079 pos
= eval(args
[1], 'v');
1080 id
= trap_bypos(-1, pos
);
1087 if (id
< 0) /* find an unused position in treg[] */
1088 id
= trap_byreg(-1);
1095 void tr_ch(char **args
)
1102 id
= trap_byreg(reg
);
1105 tpos
[id
] = eval(args
[2], 'v');
1111 void tr_dt(char **args
)
1116 cdiv
->tpos
= eval(args
[1], 'v');
1117 cdiv
->treg
= map(args
[2]);
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
);
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
);
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
);
1150 return pos
>= 0 ? pos
: 0x7fffffff;
1151 return (pos
>= 0 && pos
< n_p
? pos
: n_p
) - n_d
;