1 /* rendering lines and managing traps */
8 #define cfmt env_fmt() /* current formatter */
9 #define cwb env_wb() /* current word buffer */
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 int ren_nl
; /* just after a newline */
29 static int ren_partial
; /* reading an input line in render_rec() */
30 static int ren_unbuf
[8]; /* ren_back() buffer */
32 static int ren_aborted
; /* .ab executed */
34 static int bp_first
= 1; /* prior to the first page */
35 static int bp_next
= 1; /* next page number */
36 static int bp_count
; /* number of pages so far */
37 static int bp_ejected
; /* current ejected page */
38 static int bp_final
; /* 1: executing em, 2: the final page, 3: the 2nd final page */
39 static int ren_level
; /* the depth of render_rec() calls */
41 static char c_fa
[GNLEN
]; /* 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
)
57 cdiv
= cdiv
? cdiv
+ 1 : divs
;
58 memset(cdiv
, 0, sizeof(*cdiv
));
59 sbuf_init(&cdiv
->sbuf
);
60 cdiv
->reg
= map(args
[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
);
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
);
84 cdiv
= cdiv
> divs
? cdiv
- 1 : NULL
;
88 int charwid(int fn
, int sz
, int wid
)
91 return dev_getcs(n_f
) * SC_EM
/ 36;
92 return DEVWID(sz
, wid
) + (dev_getbd(fn
) ? dev_getbd(fn
) - 1 : 0);
97 return cdiv
? cdiv
->reg
: -1;
102 return fmt_wid(cfmt
) + wb_wid(cwb
);
105 void tr_divbeg(char **args
)
111 void tr_divend(char **args
)
114 errdie("neatroff: diversion stack empty\n");
119 static int trap_reg(int pos
);
120 static int trap_pos(int pos
);
121 static void trap_exec(int reg
);
123 static void ren_page(int pg
, int force
)
125 if (!force
&& bp_final
>= 2)
135 if (trap_pos(-1) == 0)
136 trap_exec(trap_reg(-1));
139 static int ren_first(void)
141 if (bp_first
&& !cdiv
) {
143 ren_page(bp_next
, 1);
149 /* when nodiv, do not append .sp to diversions */
150 static void ren_sp(int n
, int nodiv
)
153 /* ignore .sp without arguments when reading diversions */
154 if (!n
&& ren_div
&& !n_u
)
159 if (cdiv
&& !nodiv
) {
160 sbuf_putnl(&cdiv
->sbuf
);
161 sbuf_printf(&cdiv
->sbuf
, "%csp %du\n", c_cc
, n
? n
: n_v
);
167 static int render_rec(int level
);
168 static void trap_exec(int reg
)
171 int partial
= ren_partial
;
173 sprintf(cmd
, "%c%s %d\n", c_cc
, TR_POPREN
, ren_level
);
174 in_pushnl(cmd
, NULL
);
175 in_pushnl(str_get(reg
), NULL
);
176 render_rec(++ren_level
);
177 /* executed the trap while in the middle of an input line */
179 fmt_suppressnl(cfmt
);
183 static int detect_traps(int beg
, int end
)
185 int pos
= trap_pos(beg
);
186 return pos
>= 0 && (cdiv
|| pos
< n_p
) && pos
<= end
;
189 /* return 1 if executed a trap */
190 static int ren_traps(int beg
, int end
, int dosp
)
192 int pos
= trap_pos(beg
);
193 if (detect_traps(beg
, end
)) {
194 if (dosp
&& pos
> beg
)
195 ren_sp(pos
- beg
, 0);
196 trap_exec(trap_reg(beg
));
202 static int detect_pagelimit(int ne
)
204 return !cdiv
&& n_nl
+ ne
>= n_p
;
207 /* start a new page if needed */
208 static int ren_pagelimit(int ne
)
210 if (detect_pagelimit(ne
)) {
211 ren_page(bp_next
, 0);
217 /* return 1 if triggered a trap */
218 static int down(int n
)
220 if (ren_traps(n_d
, n_d
+ (n
? n
: n_v
), 1))
223 return ren_pagelimit(0);
226 /* line adjustment */
227 static int ren_ljust(struct sbuf
*spre
, int w
, int ad
, int li
, int ll
)
230 int llen
= ll
- ljust
;
232 if ((ad
& AD_B
) == AD_C
)
233 ljust
+= llen
> w
? (llen
- w
) / 2 : 0;
234 if ((ad
& AD_B
) == AD_R
)
237 sbuf_printf(spre
, "%ch'%du'", c_ec
, ljust
);
238 if (cdiv
&& cdiv
->dl
< w
+ ljust
)
239 cdiv
->dl
= w
+ ljust
;
243 /* append the line to the current diversion or send it to out.c */
244 static void ren_out(char *beg
, char *mid
, char *end
)
247 sbuf_append(&cdiv
->sbuf
, beg
);
248 sbuf_append(&cdiv
->sbuf
, mid
);
249 sbuf_append(&cdiv
->sbuf
, end
);
259 static void ren_transparent(char *s
)
262 sbuf_printf(&cdiv
->sbuf
, "%s\n", s
);
267 static int zwid(void)
269 struct glyph
*g
= dev_glyph("0", n_f
);
270 return charwid(n_f
, n_s
, g
? g
->wid
: 0);
273 /* append the line number to the output line */
274 static void ren_lnum(struct sbuf
*spre
)
281 if (n_nn
<= 0 && (n_ln
% n_nM
) == 0)
282 sprintf(num
, "%d", n_ln
);
283 wb_hmov(&wb
, n_nI
* zwid());
285 wb_hmov(&wb
, (3 - strlen(num
)) * zwid());
290 wb_hmov(&wb
, n_nS
* zwid());
291 sbuf_append(spre
, sbuf_buf(&wb
.sbuf
));
299 /* append margin character */
300 static void ren_mc(struct sbuf
*sbuf
, int w
, int ljust
)
304 if (w
+ ljust
< n_l
+ n_mcn
)
305 wb_hmov(&wb
, n_l
+ n_mcn
- w
- ljust
);
306 wb_putexpand(&wb
, c_mc
);
307 sbuf_append(sbuf
, sbuf_buf(&wb
.sbuf
));
311 /* process a line and print it with ren_out() */
312 static int ren_line(char *line
, int w
, int ad
, int body
,
313 int li
, int ll
, int els_neg
, int els_pos
)
315 struct sbuf sbeg
, send
;
316 int prev_d
, lspc
, ljust
;
320 lspc
= MAX(1, n_L
) * n_v
; /* line space, ignoreing \x */
324 if (!n_ns
|| line
[0] || els_neg
|| els_pos
) {
326 if (line
[0] && n_nm
&& body
)
328 ljust
= ren_ljust(&sbeg
, w
, ad
, li
, ll
);
329 if (line
[0] && body
&& n_mc
)
330 ren_mc(&send
, w
, ljust
);
331 ren_out(sbuf_buf(&sbeg
), line
, sbuf_buf(&send
));
339 if (detect_traps(prev_d
, n_d
) || detect_pagelimit(lspc
- n_v
)) {
340 if (!ren_pagelimit(lspc
- n_v
))
341 ren_traps(prev_d
, n_d
, 0);
344 if (lspc
- n_v
&& down(lspc
- n_v
))
349 /* read a line from fmt and send it to ren_line() */
350 static int ren_passline(struct fmt
*fmt
)
353 int ll
, li
, els_neg
, els_pos
, w
, ret
;
356 if (!fmt_morewords(fmt
))
359 fmt_nextline(fmt
, &sbuf
, &w
, &li
, &ll
, &els_neg
, &els_pos
);
360 if ((n_cp
&& !n_u
) || n_na
)
364 ret
= ren_line(sbuf_buf(&sbuf
), w
, ad
, 1, li
, ll
, els_neg
, els_pos
);
369 /* output formatted lines in fmt */
370 static void ren_fmtpop(struct fmt
*fmt
)
372 while (fmt_morelines(fmt
))
376 /* format and output all lines in fmt */
377 static void ren_fmtpopall(struct fmt
*fmt
)
379 while (fmt_fill(fmt
))
384 /* output current line; returns 1 if triggered a trap */
385 static int ren_br(void)
390 return ren_passline(cfmt
);
393 void tr_br(char **args
)
395 ren_fmtpopall(cfmt
); /* output the completed lines first */
396 if (args
[0][0] == c_cc
)
400 void tr_sp(char **args
)
403 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
404 if (args
[0][0] == c_cc
)
406 if (n
&& !n_ns
&& !traps
)
410 void tr_sv(char **args
)
412 int n
= eval(args
[1], 'v');
414 if (n_d
+ n
< f_nexttrap())
420 void tr_ns(char **args
)
425 void tr_rs(char **args
)
430 void tr_os(char **args
)
437 void tr_mk(char **args
)
440 num_set(map(args
[1]), n_d
);
445 void tr_rt(char **args
)
447 int n
= args
[1] ? eval_re(args
[1], n_d
, 'v') : n_mk
;
448 if (n
>= 0 && n
< n_d
)
452 void tr_ne(char **args
)
454 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
457 if (!ren_traps(n_d
, n_d
+ n
- 1, 1))
461 static void ren_ejectpage(int br
)
463 bp_ejected
= bp_count
;
466 while (bp_count
== bp_ejected
&& !cdiv
) {
467 if (detect_traps(n_d
, n_p
)) {
468 ren_traps(n_d
, n_p
, 1);
471 ren_page(bp_next
, 0);
476 void tr_bp(char **args
)
478 if (!cdiv
&& (args
[1] || !n_ns
)) {
480 bp_next
= eval_re(args
[1], n_pg
, 0);
481 ren_ejectpage(args
[0][0] == c_cc
);
485 void tr_pn(char **args
)
488 bp_next
= eval_re(args
[1], n_pg
, 0);
491 static void ren_ps(char *s
)
493 int ps
= !s
|| !*s
|| !strcmp("0", s
) ? n_s0
: eval_re(s
, n_s
, 0);
498 void tr_ps(char **args
)
503 void tr_ll(char **args
)
505 int ll
= args
[1] ? eval_re(args
[1], n_l
, 'm') : n_l0
;
510 void tr_in(char **args
)
512 int in
= args
[1] ? eval_re(args
[1], n_i
, 'm') : n_i0
;
513 if (args
[0][0] == c_cc
)
520 void tr_ti(char **args
)
522 if (args
[0][0] == c_cc
)
525 n_ti
= eval_re(args
[1], n_i
, 'm');
528 static void ren_ft(char *s
)
530 int fn
= !s
|| !*s
|| !strcmp("P", s
) ? n_f0
: dev_pos(s
);
532 errmsg("neatroff: failed to mount <%s>\n", s
);
539 void tr_ft(char **args
)
544 void tr_fp(char **args
)
548 if (dev_mnt(atoi(args
[1]), args
[2], args
[3] ? args
[3] : args
[2]) < 0)
549 errmsg("neatroff: failed to mount <%s>\n", args
[2]);
552 void tr_nf(char **args
)
554 if (args
[0][0] == c_cc
)
559 void tr_fi(char **args
)
561 if (args
[0][0] == c_cc
)
566 void tr_ce(char **args
)
568 if (args
[0][0] == c_cc
)
570 n_ce
= args
[1] ? atoi(args
[1]) : 1;
573 void tr_fc(char **args
)
577 if (fa
&& charread(&fa
, c_fa
) >= 0) {
578 if (!fb
|| charread(&fb
, c_fb
) < 0)
586 static void ren_cl(char *s
)
588 int m
= !s
|| !*s
? n_m0
: clr_get(s
);
593 void tr_cl(char **args
)
598 void tr_ab(char **args
)
600 fprintf(stderr
, "%s\n", args
[1]);
604 static void ren_cmd(struct wb
*wb
, int c
, char *arg
)
608 wb_hmov(wb
, N_SS(n_f
, n_s
));
620 wb_vmov(wb
, SC_EM
/ 2);
626 wb_hmov(wb
, eval(arg
, 'm'));
629 num_set(map(arg
), wb
== cwb
? f_hpos() - n_lb
: wb_wid(wb
));
645 while (fmt_fillreq(cfmt
))
655 wb_vmov(wb
, -SC_EM
/ 2);
658 wb_vmov(wb
, eval(arg
, 'v'));
664 wb_els(wb
, eval(arg
, 'v'));
670 wb_hmov(wb
, SC_EM
/ 6);
676 wb_hmov(wb
, SC_EM
/ 12);
679 wb_italiccorrection(wb
);
682 wb_italiccorrectionleft(wb
);
690 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int));
691 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int));
693 /* insert a character, escape sequence, field or etc into wb */
694 static void ren_put(struct wb
*wb
, char *c
, int (*next
)(void), void (*back
)(int))
699 if (c
[0] == ' ' || c
[0] == '\n') {
703 if (c
[0] == '\t' || c
[0] == '\x01') {
704 ren_tab(wb
, c
[0] == '\t' ? c_tc
: c_lc
, next
, back
);
707 if (c_fa
[0] && !strcmp(c_fa
, c
)) {
708 ren_field(wb
, next
, back
);
714 ren_char(wb
, next
, back
);
715 wb_hmov(wb
, w
- wb_wid(wb
));
719 if (ren_nl
&& next
== ren_next
) {
722 while (n
>= 0 && n
!= '\n') {
727 ren_transparent(arg
);
731 if (strchr(" bCcDdefHhkLlmNoprSsuvXxz0^|{}&/,", c
[1])) {
732 argnext(arg
, c
[1], next
, back
);
734 snprintf(c
, GNLEN
, "%c%c", c_ec
, c_ec
);
735 } else if (c
[1] == 'N') {
736 snprintf(c
, GNLEN
, "GID=%s", arg
);
738 ren_cmd(wb
, c
[1], arg
);
743 if (!ren_div
&& cdef_map(c
, n_f
)) { /* .char characters */
747 if (!n_lg
|| ren_div
|| wb_lig(wb
, c
)) {
748 if (n_kn
&& !ren_div
)
754 /* read one character and place it inside wb buffer */
755 int ren_char(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
758 if (charnext(c
, next
, back
) < 0)
760 ren_put(wb
, c
, next
, back
);
764 /* like ren_char(); return 1 if d1 was read and d2 if d2 was read */
765 static int ren_chardel(struct wb
*wb
, int (*next
)(void), void (*back
)(int),
769 if (charnext(c
, next
, back
) < 0)
771 if (d1
&& !strcmp(d1
, c
))
773 if (d2
&& !strcmp(d2
, c
))
775 ren_put(wb
, c
, next
, back
);
779 /* read the argument of \w and push its width */
780 int ren_wid(int (*next
)(void), void (*back
)(int))
786 charnext(delim
, next
, back
);
789 while (c
>= 0 && c
!= '\n') {
791 if (ren_chardel(&wb
, next
, back
, delim
, NULL
))
797 wb_wconf(&wb
, &n_ct
, &n_st
, &n_sb
, &n_llx
, &n_lly
, &n_urx
, &n_ury
);
802 /* return 1 if d1 was read and 2 if d2 was read */
803 static int ren_until(struct wb
*wb
, char *d1
, char *d2
,
804 int (*next
)(void), void (*back
)(int))
808 while (c
>= 0 && c
!= '\n') {
810 ret
= ren_chardel(wb
, next
, back
, d1
, d2
);
820 static void wb_cpy(struct wb
*dst
, struct wb
*src
, int left
)
822 wb_hmov(dst
, left
- wb_wid(dst
));
826 void ren_tl(int (*next
)(void), void (*back
)(int))
832 charnext(delim
, next
, back
);
833 /* the left-adjusted string */
834 ren_until(&wb2
, delim
, NULL
, next
, back
);
835 wb_cpy(&wb
, &wb2
, 0);
836 /* the centered string */
837 ren_until(&wb2
, delim
, NULL
, next
, back
);
838 wb_cpy(&wb
, &wb2
, (n_lt
- wb_wid(&wb2
)) / 2);
839 /* the right-adjusted string */
840 ren_until(&wb2
, delim
, NULL
, next
, back
);
841 wb_cpy(&wb
, &wb2
, n_lt
- wb_wid(&wb2
));
842 /* flushing the line */
843 ren_line(sbuf_buf(&wb
.sbuf
), wb_wid(&wb
), AD_L
, 0,
844 0, n_lt
, wb
.els_neg
, wb
.els_pos
);
849 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
851 struct wb wbs
[NFIELDS
];
854 int left
, right
, cur_left
;
856 while (n
< LEN(wbs
)) {
858 if (ren_until(&wbs
[n
++], c_fb
, c_fa
, next
, back
) != 1)
861 left
= wb
== cwb
? f_hpos() : wb_wid(wb
);
862 right
= tab_next(left
);
863 for (i
= 0; i
< n
; i
++)
864 wid
+= wb_wid(&wbs
[i
]);
865 pad
= (right
- left
- wid
) / (n
> 1 ? n
- 1 : 1);
866 rem
= (right
- left
- wid
) % (n
> 1 ? n
- 1 : 1);
867 for (i
= 0; i
< n
; i
++) {
871 cur_left
= right
- wb_wid(&wbs
[i
]);
873 cur_left
= wb_wid(wb
) + pad
+ (i
+ rem
>= n
);
874 wb_cpy(wb
, &wbs
[i
], cur_left
);
879 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int))
882 int pos
= wb
== cwb
? f_hpos() : wb_wid(wb
);
883 int ins
= tab_next(pos
); /* insertion position */
884 int typ
= tab_type(pos
); /* tab type */
887 if (typ
== 'R' || typ
== 'C') {
889 while (c
>= 0 && c
!= '\n' && c
!= '\t' && c
!= '\x01') {
891 ren_char(&t
, next
, back
);
897 ins
-= wb_wid(&t
) / 2;
900 if (!tc
[0] || ins
<= pos
)
901 wb_hmov(wb
, ins
- pos
);
903 ren_hline(wb
, ins
- pos
, tc
);
908 /* parse characters and troff requests of s and append them to wb */
909 int ren_parse(struct wb
*wb
, char *s
)
917 if (ren_char(wb
, sstr_next
, sstr_back
))
926 /* cause nested render_rec() to exit */
927 void tr_popren(char **args
)
929 ren_level
= args
[1] ? atoi(args
[1]) : 0;
932 /* read characters from tr.c and pass the rendered lines to out.c */
933 static int render_rec(int level
)
936 while (ren_level
>= level
) {
937 while (!tr_nextreq())
938 if (ren_level
< level
)
940 if (ren_level
< level
)
959 ren_partial
= c
!= '\n';
960 /* add cwb (the current word) to cfmt */
961 if (c
== ' ' || c
== '\n') {
962 if (!wb_part(cwb
)) { /* not after a \c */
963 while (fmt_word(cfmt
, cwb
))
967 while (fmt_newline(cfmt
))
976 /* flush the line if necessary */
977 if (c
== ' ' || c
== '\n' || c
< 0)
979 if (c
== '\n' || ren_nl
) /* end or start of input line */
981 if (c
== '\n' && n_it
&& --n_itn
== 0)
983 if (c
== '\n' && !wb_part(cwb
))
984 n_ce
= MAX(0, n_ce
- 1);
985 if (c
!= ' ' && c
>= 0) {
987 ren_char(cwb
, ren_next
, ren_back
);
995 /* render input words */
999 while (!tr_nextreq())
1001 ren_first(); /* transition to the first page */
1004 if (fmt_morewords(cfmt
))
1005 ren_page(bp_next
, 1);
1012 #define tposval(i) (tpos[i] < 0 ? n_p + tpos[i] : tpos[i])
1014 static int tpos
[NTRAPS
]; /* trap positions */
1015 static int treg
[NTRAPS
]; /* trap registers */
1018 static int trap_first(int pos
)
1022 for (i
= 0; i
< ntraps
; i
++)
1023 if (treg
[i
] >= 0 && tposval(i
) > pos
)
1024 if (best
< 0 || tposval(i
) < tposval(best
))
1029 static int trap_byreg(int reg
)
1032 for (i
= 0; i
< ntraps
; i
++)
1038 static int trap_bypos(int reg
, int pos
)
1041 for (i
= 0; i
< ntraps
; i
++)
1042 if (treg
[i
] >= 0 && tposval(i
) == pos
)
1043 if (reg
== -1 || treg
[i
] == reg
)
1048 void tr_wh(char **args
)
1053 pos
= eval(args
[1], 'v');
1054 id
= trap_bypos(-1, pos
);
1062 id
= trap_byreg(-1);
1069 void tr_ch(char **args
)
1076 id
= trap_byreg(reg
);
1078 tpos
[id
] = args
[2] ? eval(args
[2], 'v') : -1;
1081 void tr_dt(char **args
)
1086 cdiv
->tpos
= eval(args
[1], 'v');
1087 cdiv
->treg
= map(args
[2]);
1093 void tr_em(char **args
)
1095 trap_em
= args
[1] ? map(args
[1]) : -1;
1098 static int trap_pos(int pos
)
1100 int ret
= trap_first(pos
);
1104 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->tpos
: -1;
1105 return ret
>= 0 ? tposval(ret
) : -1;
1108 static int trap_reg(int pos
)
1110 int ret
= trap_first(pos
);
1112 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->treg
: -1;
1113 return ret
>= 0 ? treg
[ret
] : -1;
1116 int f_nexttrap(void)
1118 int pos
= trap_pos(n_d
);
1120 return pos
>= 0 ? pos
: 0x7fffffff;
1121 return (pos
>= 0 && pos
< n_p
? pos
: n_p
) - n_d
;