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
)
90 struct font
*f
= dev_font(fn
);
92 return font_getcs(f
) * SC_EM
/ 36;
93 return DEVWID(sz
, wid
) + (font_getbd(f
) ? font_getbd(f
) - 1 : 0);
98 return cdiv
? cdiv
->reg
: -1;
103 return fmt_wid(cfmt
) + wb_wid(cwb
);
106 void tr_divbeg(char **args
)
112 void tr_divend(char **args
)
115 errdie("neatroff: diversion stack empty\n");
120 static int trap_reg(int pos
);
121 static int trap_pos(int pos
);
122 static void trap_exec(int reg
);
124 static void ren_page(int pg
, int force
)
126 if (!force
&& bp_final
>= 2)
136 if (trap_pos(-1) == 0)
137 trap_exec(trap_reg(-1));
140 static int ren_first(void)
142 if (bp_first
&& !cdiv
) {
144 ren_page(bp_next
, 1);
150 /* when nodiv, do not append .sp to diversions */
151 static void ren_sp(int n
, int nodiv
)
154 /* ignore .sp without arguments when reading diversions */
155 if (!n
&& ren_div
&& !n_u
)
160 if (cdiv
&& !nodiv
) {
161 sbuf_putnl(&cdiv
->sbuf
);
162 sbuf_printf(&cdiv
->sbuf
, "%csp %du\n", c_cc
, n
? n
: n_v
);
168 static int render_rec(int level
);
169 static void trap_exec(int reg
)
172 int partial
= ren_partial
&& (!ren_un
|| ren_unbuf
[0] != '\n');
174 sprintf(cmd
, "%c%s %d\n", c_cc
, TR_POPREN
, ren_level
);
176 in_push(str_get(reg
), NULL
);
179 render_rec(++ren_level
);
180 /* executed the trap while in the middle of an input line */
182 fmt_suppressnl(cfmt
);
186 static int detect_traps(int beg
, int end
)
188 int pos
= trap_pos(beg
);
189 return pos
>= 0 && (cdiv
|| pos
< n_p
) && pos
<= end
;
192 /* return 1 if executed a trap */
193 static int ren_traps(int beg
, int end
, int dosp
)
195 int pos
= trap_pos(beg
);
196 if (detect_traps(beg
, end
)) {
197 if (dosp
&& pos
> beg
)
198 ren_sp(pos
- beg
, 0);
199 trap_exec(trap_reg(beg
));
205 static int detect_pagelimit(int ne
)
207 return !cdiv
&& n_nl
+ ne
>= n_p
;
210 /* start a new page if needed */
211 static int ren_pagelimit(int ne
)
213 if (detect_pagelimit(ne
)) {
214 ren_page(bp_next
, 0);
220 /* return 1 if triggered a trap */
221 static int down(int n
)
223 if (ren_traps(n_d
, n_d
+ (n
? n
: n_v
), 1))
226 return ren_pagelimit(0);
229 /* line adjustment */
230 static int ren_ljust(struct sbuf
*spre
, int w
, int ad
, int li
, int ll
)
233 int llen
= ll
- ljust
;
235 if ((ad
& AD_B
) == AD_C
)
236 ljust
+= llen
> w
? (llen
- w
) / 2 : 0;
237 if ((ad
& AD_B
) == AD_R
)
240 sbuf_printf(spre
, "%ch'%du'", c_ec
, ljust
);
241 if (cdiv
&& cdiv
->dl
< w
+ ljust
)
242 cdiv
->dl
= w
+ ljust
;
246 /* append the line to the current diversion or send it to out.c */
247 static void ren_out(char *beg
, char *mid
, char *end
)
250 sbuf_append(&cdiv
->sbuf
, beg
);
251 sbuf_append(&cdiv
->sbuf
, mid
);
252 sbuf_append(&cdiv
->sbuf
, end
);
262 static void ren_transparent(char *s
)
265 sbuf_printf(&cdiv
->sbuf
, "%s\n", s
);
270 static int zwid(void)
272 struct glyph
*g
= dev_glyph("0", n_f
);
273 return charwid(n_f
, n_s
, g
? g
->wid
: 0);
276 /* append the line number to the output line */
277 static void ren_lnum(struct sbuf
*spre
)
284 if (n_nn
<= 0 && (n_ln
% n_nM
) == 0)
285 sprintf(num
, "%d", n_ln
);
286 wb_hmov(&wb
, n_nI
* zwid());
288 wb_hmov(&wb
, (3 - strlen(num
)) * zwid());
293 wb_hmov(&wb
, n_nS
* zwid());
294 sbuf_append(spre
, wb_buf(&wb
));
302 /* append margin character */
303 static void ren_mc(struct sbuf
*sbuf
, int w
, int ljust
)
307 if (w
+ ljust
< n_l
+ n_mcn
)
308 wb_hmov(&wb
, n_l
+ n_mcn
- w
- ljust
);
309 wb_putexpand(&wb
, c_mc
);
310 sbuf_append(sbuf
, wb_buf(&wb
));
314 /* process a line and print it with ren_out() */
315 static int ren_line(char *line
, int w
, int ad
, int body
,
316 int li
, int ll
, int els_neg
, int els_pos
)
318 struct sbuf sbeg
, send
;
319 int prev_d
, lspc
, ljust
;
323 lspc
= MAX(1, n_L
) * n_v
; /* line space, ignoreing \x */
327 if (!n_ns
|| line
[0] || els_neg
|| els_pos
) {
329 if (line
[0] && n_nm
&& body
)
331 ljust
= ren_ljust(&sbeg
, w
, ad
, li
, ll
);
332 if (line
[0] && body
&& n_mc
)
333 ren_mc(&send
, w
, ljust
);
334 ren_out(sbuf_buf(&sbeg
), line
, sbuf_buf(&send
));
342 if (detect_traps(prev_d
, n_d
) || detect_pagelimit(lspc
- n_v
)) {
343 if (!ren_pagelimit(lspc
- n_v
))
344 ren_traps(prev_d
, n_d
, 0);
347 if (lspc
- n_v
&& down(lspc
- n_v
))
352 /* read a line from fmt and send it to ren_line() */
353 static int ren_passline(struct fmt
*fmt
)
356 int ll
, li
, els_neg
, els_pos
, w
, ret
;
359 if (!fmt_morewords(fmt
))
362 fmt_nextline(fmt
, &sbuf
, &w
, &li
, &ll
, &els_neg
, &els_pos
);
363 if ((n_cp
&& !n_u
) || n_na
)
367 ret
= ren_line(sbuf_buf(&sbuf
), w
, ad
, 1, li
, ll
, els_neg
, els_pos
);
372 /* output formatted lines in fmt */
373 static void ren_fmtpop(struct fmt
*fmt
)
375 while (fmt_morelines(fmt
))
379 /* format and output all lines in fmt */
380 static void ren_fmtpopall(struct fmt
*fmt
)
382 while (fmt_fill(fmt
))
387 /* output current line; returns 1 if triggered a trap */
388 static int ren_br(void)
393 return ren_passline(cfmt
);
396 void tr_br(char **args
)
398 ren_fmtpopall(cfmt
); /* output the completed lines first */
399 if (args
[0][0] == c_cc
)
403 void tr_sp(char **args
)
406 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
407 if (args
[0][0] == c_cc
)
409 if (n
&& !n_ns
&& !traps
)
413 void tr_sv(char **args
)
415 int n
= eval(args
[1], 'v');
417 if (n_d
+ n
< f_nexttrap())
423 void tr_ns(char **args
)
428 void tr_rs(char **args
)
433 void tr_os(char **args
)
440 void tr_mk(char **args
)
443 num_set(map(args
[1]), n_d
);
448 void tr_rt(char **args
)
450 int n
= args
[1] ? eval_re(args
[1], n_d
, 'v') : n_mk
;
451 if (n
>= 0 && n
< n_d
)
455 void tr_ne(char **args
)
457 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
460 if (!ren_traps(n_d
, n_d
+ n
- 1, 1))
464 static void ren_ejectpage(int br
)
466 bp_ejected
= bp_count
;
469 while (bp_count
== bp_ejected
&& !cdiv
) {
470 if (detect_traps(n_d
, n_p
)) {
471 ren_traps(n_d
, n_p
, 1);
474 ren_page(bp_next
, 0);
479 void tr_bp(char **args
)
481 if (!cdiv
&& (args
[1] || !n_ns
)) {
483 bp_next
= eval_re(args
[1], n_pg
, 0);
484 ren_ejectpage(args
[0][0] == c_cc
);
488 void tr_pn(char **args
)
491 bp_next
= eval_re(args
[1], n_pg
, 0);
494 static void ren_ps(char *s
)
496 int ps
= !s
|| !*s
|| !strcmp("0", s
) ? n_s0
: eval_re(s
, n_s
, 0);
501 void tr_ps(char **args
)
506 void tr_ll(char **args
)
508 int ll
= args
[1] ? eval_re(args
[1], n_l
, 'm') : n_l0
;
513 void tr_in(char **args
)
515 int in
= args
[1] ? eval_re(args
[1], n_i
, 'm') : n_i0
;
516 if (args
[0][0] == c_cc
)
523 void tr_ti(char **args
)
525 if (args
[0][0] == c_cc
)
528 n_ti
= eval_re(args
[1], n_i
, 'm');
531 static void ren_ft(char *s
)
533 int fn
= !s
|| !*s
|| !strcmp("P", s
) ? n_f0
: dev_pos(s
);
535 errmsg("neatroff: failed to mount <%s>\n", s
);
542 void tr_ft(char **args
)
547 void tr_fp(char **args
)
551 if (dev_mnt(atoi(args
[1]), args
[2], args
[3] ? args
[3] : args
[2]) < 0)
552 errmsg("neatroff: failed to mount <%s>\n", args
[2]);
555 void tr_nf(char **args
)
557 if (args
[0][0] == c_cc
)
562 void tr_fi(char **args
)
564 if (args
[0][0] == c_cc
)
569 void tr_ce(char **args
)
571 if (args
[0][0] == c_cc
)
573 n_ce
= args
[1] ? atoi(args
[1]) : 1;
576 void tr_fc(char **args
)
580 if (fa
&& charread(&fa
, c_fa
) >= 0) {
581 if (!fb
|| charread(&fb
, c_fb
) < 0)
589 static void ren_cl(char *s
)
591 int m
= !s
|| !*s
? n_m0
: clr_get(s
);
596 void tr_cl(char **args
)
601 void tr_ab(char **args
)
603 fprintf(stderr
, "%s\n", args
[1]);
607 static void ren_cmd(struct wb
*wb
, int c
, char *arg
)
611 wb_hmov(wb
, N_SS(n_f
, n_s
));
623 wb_vmov(wb
, SC_EM
/ 2);
629 wb_hmov(wb
, eval(arg
, 'm'));
632 num_set(map(arg
), wb
== cwb
? f_hpos() - n_lb
: wb_wid(wb
));
648 while (fmt_fillreq(cfmt
))
658 wb_vmov(wb
, -SC_EM
/ 2);
661 wb_vmov(wb
, eval(arg
, 'v'));
667 wb_els(wb
, eval(arg
, 'v'));
676 wb_hmov(wb
, SC_EM
/ 6);
682 wb_hmov(wb
, SC_EM
/ 12);
685 wb_italiccorrection(wb
);
688 wb_italiccorrectionleft(wb
);
696 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int));
697 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int));
699 /* insert a character, escape sequence, field or etc into wb */
700 static void ren_put(struct wb
*wb
, char *c
, int (*next
)(void), void (*back
)(int))
705 if (c
[0] == ' ' || c
[0] == '\n') {
709 if (c
[0] == '\t' || c
[0] == '\x01') {
710 ren_tab(wb
, c
[0] == '\t' ? c_tc
: c_lc
, next
, back
);
713 if (c_fa
[0] && !strcmp(c_fa
, c
)) {
714 ren_field(wb
, next
, back
);
720 ren_char(wb
, next
, back
);
721 wb_hmov(wb
, w
- wb_wid(wb
));
725 if (ren_nl
&& next
== ren_next
) {
728 while (n
>= 0 && n
!= '\n') {
733 ren_transparent(arg
);
737 if (strchr(" bCcDdefHhkLlmNoprSsuvXxZz0^|{}&/,", c
[1])) {
739 if (strchr(ESC_P
, c
[1]))
740 unquotednext(arg
, c
[1], next
, back
);
741 if (strchr(ESC_Q
, c
[1]))
742 quotednext(arg
, next
, back
);
744 snprintf(c
, GNLEN
, "%c%c", c_ec
, c_ec
);
745 } else if (c
[1] == 'N') {
746 snprintf(c
, GNLEN
, "GID=%s", arg
);
748 ren_cmd(wb
, c
[1], arg
);
757 if (cdef_map(c
, n_f
)) /* .char characters */
763 /* read one character and place it inside wb buffer */
764 int ren_char(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
767 if (charnext(c
, next
, back
) < 0)
769 ren_put(wb
, c
, next
, back
);
773 /* like ren_char(); return 1 if d1 was read and 2 if d2 was read */
774 static int ren_chardel(struct wb
*wb
, int (*next
)(void), void (*back
)(int),
778 if (charnext(c
, next
, back
) < 0)
780 if (d1
&& !strcmp(d1
, c
))
782 if (d2
&& !strcmp(d2
, c
))
784 ren_put(wb
, c
, next
, back
);
788 /* read the argument of \w and push its width */
789 int ren_wid(int (*next
)(void), void (*back
)(int))
795 charnext(delim
, next
, back
);
798 while (c
>= 0 && c
!= '\n') {
800 if (ren_chardel(&wb
, next
, back
, delim
, NULL
))
806 wb_wconf(&wb
, &n_ct
, &n_st
, &n_sb
, &n_llx
, &n_lly
, &n_urx
, &n_ury
);
811 /* return 1 if d1 was read and 2 if d2 was read */
812 static int ren_until(struct wb
*wb
, char *d1
, char *d2
,
813 int (*next
)(void), void (*back
)(int))
817 while (c
>= 0 && c
!= '\n') {
819 ret
= ren_chardel(wb
, next
, back
, d1
, d2
);
829 static void wb_cpy(struct wb
*dst
, struct wb
*src
, int left
)
831 wb_hmov(dst
, left
- wb_wid(dst
));
835 void ren_tl(int (*next
)(void), void (*back
)(int))
841 charnext(delim
, next
, back
);
842 /* the left-adjusted string */
843 ren_until(&wb2
, delim
, NULL
, next
, back
);
844 wb_cpy(&wb
, &wb2
, 0);
845 /* the centered string */
846 ren_until(&wb2
, delim
, NULL
, next
, back
);
847 wb_cpy(&wb
, &wb2
, (n_lt
- wb_wid(&wb2
)) / 2);
848 /* the right-adjusted string */
849 ren_until(&wb2
, delim
, NULL
, next
, back
);
850 wb_cpy(&wb
, &wb2
, n_lt
- wb_wid(&wb2
));
851 /* flushing the line */
852 ren_line(wb_buf(&wb
), wb_wid(&wb
), AD_L
, 0,
853 0, n_lt
, wb
.els_neg
, wb
.els_pos
);
858 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
860 struct wb wbs
[NFIELDS
];
863 int left
, right
, cur_left
;
865 while (n
< LEN(wbs
)) {
867 if (ren_until(&wbs
[n
++], c_fb
, c_fa
, next
, back
) != 1)
870 left
= wb
== cwb
? f_hpos() : wb_wid(wb
);
871 right
= tab_next(left
);
872 for (i
= 0; i
< n
; i
++)
873 wid
+= wb_wid(&wbs
[i
]);
874 pad
= (right
- left
- wid
) / (n
> 1 ? n
- 1 : 1);
875 rem
= (right
- left
- wid
) % (n
> 1 ? n
- 1 : 1);
876 for (i
= 0; i
< n
; i
++) {
880 cur_left
= right
- wb_wid(&wbs
[i
]);
882 cur_left
= wb_wid(wb
) + pad
+ (i
+ rem
>= n
);
883 wb_cpy(wb
, &wbs
[i
], cur_left
);
888 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int))
891 int pos
= wb
== cwb
? f_hpos() : wb_wid(wb
);
892 int ins
= tab_next(pos
); /* insertion position */
893 int typ
= tab_type(pos
); /* tab type */
896 if (typ
== 'R' || typ
== 'C') {
898 while (c
>= 0 && c
!= '\n' && c
!= '\t' && c
!= '\x01') {
900 ren_char(&t
, next
, back
);
906 ins
-= wb_wid(&t
) / 2;
909 if (!tc
[0] || ins
<= pos
)
910 wb_hmov(wb
, ins
- pos
);
912 ren_hline(wb
, ins
- pos
, tc
);
917 /* parse characters and troff requests of s and append them to wb */
918 int ren_parse(struct wb
*wb
, char *s
)
926 if (ren_char(wb
, sstr_next
, sstr_back
))
935 /* cause nested render_rec() to exit */
936 void tr_popren(char **args
)
938 ren_level
= args
[1] ? atoi(args
[1]) : 0;
941 #define FMT_PAR() (n_u && !n_na && !n_ce && (n_j & AD_P) == AD_P)
943 /* read characters from tr.c and pass the rendered lines to out.c */
944 static int render_rec(int level
)
947 while (ren_level
>= level
) {
948 while (!ren_un
&& !tr_nextreq())
949 if (ren_level
< level
)
951 if (ren_level
< level
)
970 ren_partial
= c
!= '\n';
971 /* add cwb (the current word) to cfmt */
972 if (c
== ' ' || c
== '\n') {
973 if (!wb_part(cwb
)) { /* not after a \c */
974 while (fmt_word(cfmt
, cwb
))
978 while (fmt_newline(cfmt
))
986 /* flush the line if necessary */
987 if ((!FMT_PAR() && (c
== ' ' || c
== '\n')) || c
< 0)
989 if (c
== '\n' || ren_nl
) /* end or start of input line */
991 if (c
== '\n' && n_it
&& --n_itn
== 0)
993 if (c
== '\n' && !wb_part(cwb
))
994 n_ce
= MAX(0, n_ce
- 1);
995 if (c
!= ' ' && c
>= 0) {
997 ren_char(cwb
, ren_next
, ren_back
);
1005 /* render input words */
1009 while (!tr_nextreq())
1011 ren_first(); /* transition to the first page */
1014 if (fmt_morewords(cfmt
))
1015 ren_page(bp_next
, 1);
1022 #define tposval(i) (tpos[i] < 0 ? n_p + tpos[i] : tpos[i])
1024 static int tpos
[NTRAPS
]; /* trap positions */
1025 static int treg
[NTRAPS
]; /* trap registers */
1028 static int trap_first(int pos
)
1032 for (i
= 0; i
< ntraps
; i
++)
1033 if (treg
[i
] >= 0 && tposval(i
) > pos
)
1034 if (best
< 0 || tposval(i
) < tposval(best
))
1039 static int trap_byreg(int reg
)
1042 for (i
= 0; i
< ntraps
; i
++)
1048 static int trap_bypos(int reg
, int pos
)
1051 for (i
= 0; i
< ntraps
; i
++)
1052 if (treg
[i
] >= 0 && tposval(i
) == pos
)
1053 if (reg
== -1 || treg
[i
] == reg
)
1058 void tr_wh(char **args
)
1063 pos
= eval(args
[1], 'v');
1064 id
= trap_bypos(-1, pos
);
1072 id
= trap_byreg(-1);
1079 void tr_ch(char **args
)
1086 id
= trap_byreg(reg
);
1088 tpos
[id
] = args
[2] ? eval(args
[2], 'v') : -1;
1091 void tr_dt(char **args
)
1096 cdiv
->tpos
= eval(args
[1], 'v');
1097 cdiv
->treg
= map(args
[2]);
1103 void tr_em(char **args
)
1105 trap_em
= args
[1] ? map(args
[1]) : -1;
1108 static int trap_pos(int pos
)
1110 int ret
= trap_first(pos
);
1114 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->tpos
: -1;
1115 return ret
>= 0 ? tposval(ret
) : -1;
1118 static int trap_reg(int pos
)
1120 int ret
= trap_first(pos
);
1122 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->treg
: -1;
1123 return ret
>= 0 ? treg
[ret
] : -1;
1126 int f_nexttrap(void)
1128 int pos
= trap_pos(n_d
);
1130 return pos
>= 0 ? pos
: 0x7fffffff;
1131 return (pos
>= 0 && pos
< n_p
? pos
: n_p
) - n_d
;