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
);
175 in_push(str_get(reg
), NULL
);
178 render_rec(++ren_level
);
179 /* executed the trap while in the middle of an input line */
181 fmt_suppressnl(cfmt
);
185 static int detect_traps(int beg
, int end
)
187 int pos
= trap_pos(beg
);
188 return pos
>= 0 && (cdiv
|| pos
< n_p
) && pos
<= end
;
191 /* return 1 if executed a trap */
192 static int ren_traps(int beg
, int end
, int dosp
)
194 int pos
= trap_pos(beg
);
195 if (detect_traps(beg
, end
)) {
196 if (dosp
&& pos
> beg
)
197 ren_sp(pos
- beg
, 0);
198 trap_exec(trap_reg(beg
));
204 static int detect_pagelimit(int ne
)
206 return !cdiv
&& n_nl
+ ne
>= n_p
;
209 /* start a new page if needed */
210 static int ren_pagelimit(int ne
)
212 if (detect_pagelimit(ne
)) {
213 ren_page(bp_next
, 0);
219 /* return 1 if triggered a trap */
220 static int down(int n
)
222 if (ren_traps(n_d
, n_d
+ (n
? n
: n_v
), 1))
225 return ren_pagelimit(0);
228 /* line adjustment */
229 static int ren_ljust(struct sbuf
*spre
, int w
, int ad
, int li
, int ll
)
232 int llen
= ll
- ljust
;
234 if ((ad
& AD_B
) == AD_C
)
235 ljust
+= llen
> w
? (llen
- w
) / 2 : 0;
236 if ((ad
& AD_B
) == AD_R
)
239 sbuf_printf(spre
, "%ch'%du'", c_ec
, ljust
);
240 if (cdiv
&& cdiv
->dl
< w
+ ljust
)
241 cdiv
->dl
= w
+ ljust
;
245 /* append the line to the current diversion or send it to out.c */
246 static void ren_out(char *beg
, char *mid
, char *end
)
249 sbuf_append(&cdiv
->sbuf
, beg
);
250 sbuf_append(&cdiv
->sbuf
, mid
);
251 sbuf_append(&cdiv
->sbuf
, end
);
261 static void ren_transparent(char *s
)
264 sbuf_printf(&cdiv
->sbuf
, "%s\n", s
);
269 static int zwid(void)
271 struct glyph
*g
= dev_glyph("0", n_f
);
272 return charwid(n_f
, n_s
, g
? g
->wid
: 0);
275 /* append the line number to the output line */
276 static void ren_lnum(struct sbuf
*spre
)
283 if (n_nn
<= 0 && (n_ln
% n_nM
) == 0)
284 sprintf(num
, "%d", n_ln
);
285 wb_hmov(&wb
, n_nI
* zwid());
287 wb_hmov(&wb
, (3 - strlen(num
)) * zwid());
292 wb_hmov(&wb
, n_nS
* zwid());
293 sbuf_append(spre
, sbuf_buf(&wb
.sbuf
));
301 /* append margin character */
302 static void ren_mc(struct sbuf
*sbuf
, int w
, int ljust
)
306 if (w
+ ljust
< n_l
+ n_mcn
)
307 wb_hmov(&wb
, n_l
+ n_mcn
- w
- ljust
);
308 wb_putexpand(&wb
, c_mc
);
309 sbuf_append(sbuf
, sbuf_buf(&wb
.sbuf
));
313 /* process a line and print it with ren_out() */
314 static int ren_line(char *line
, int w
, int ad
, int body
,
315 int li
, int ll
, int els_neg
, int els_pos
)
317 struct sbuf sbeg
, send
;
318 int prev_d
, lspc
, ljust
;
322 lspc
= MAX(1, n_L
) * n_v
; /* line space, ignoreing \x */
326 if (!n_ns
|| line
[0] || els_neg
|| els_pos
) {
328 if (line
[0] && n_nm
&& body
)
330 ljust
= ren_ljust(&sbeg
, w
, ad
, li
, ll
);
331 if (line
[0] && body
&& n_mc
)
332 ren_mc(&send
, w
, ljust
);
333 ren_out(sbuf_buf(&sbeg
), line
, sbuf_buf(&send
));
341 if (detect_traps(prev_d
, n_d
) || detect_pagelimit(lspc
- n_v
)) {
342 if (!ren_pagelimit(lspc
- n_v
))
343 ren_traps(prev_d
, n_d
, 0);
346 if (lspc
- n_v
&& down(lspc
- n_v
))
351 /* read a line from fmt and send it to ren_line() */
352 static int ren_passline(struct fmt
*fmt
)
355 int ll
, li
, els_neg
, els_pos
, w
, ret
;
358 if (!fmt_morewords(fmt
))
361 fmt_nextline(fmt
, &sbuf
, &w
, &li
, &ll
, &els_neg
, &els_pos
);
362 if ((n_cp
&& !n_u
) || n_na
)
366 ret
= ren_line(sbuf_buf(&sbuf
), w
, ad
, 1, li
, ll
, els_neg
, els_pos
);
371 /* output formatted lines in fmt */
372 static void ren_fmtpop(struct fmt
*fmt
)
374 while (fmt_morelines(fmt
))
378 /* format and output all lines in fmt */
379 static void ren_fmtpopall(struct fmt
*fmt
)
381 while (fmt_fill(fmt
))
386 /* output current line; returns 1 if triggered a trap */
387 static int ren_br(void)
392 return ren_passline(cfmt
);
395 void tr_br(char **args
)
397 ren_fmtpopall(cfmt
); /* output the completed lines first */
398 if (args
[0][0] == c_cc
)
402 void tr_sp(char **args
)
405 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
406 if (args
[0][0] == c_cc
)
408 if (n
&& !n_ns
&& !traps
)
412 void tr_sv(char **args
)
414 int n
= eval(args
[1], 'v');
416 if (n_d
+ n
< f_nexttrap())
422 void tr_ns(char **args
)
427 void tr_rs(char **args
)
432 void tr_os(char **args
)
439 void tr_mk(char **args
)
442 num_set(map(args
[1]), n_d
);
447 void tr_rt(char **args
)
449 int n
= args
[1] ? eval_re(args
[1], n_d
, 'v') : n_mk
;
450 if (n
>= 0 && n
< n_d
)
454 void tr_ne(char **args
)
456 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
459 if (!ren_traps(n_d
, n_d
+ n
- 1, 1))
463 static void ren_ejectpage(int br
)
465 bp_ejected
= bp_count
;
468 while (bp_count
== bp_ejected
&& !cdiv
) {
469 if (detect_traps(n_d
, n_p
)) {
470 ren_traps(n_d
, n_p
, 1);
473 ren_page(bp_next
, 0);
478 void tr_bp(char **args
)
480 if (!cdiv
&& (args
[1] || !n_ns
)) {
482 bp_next
= eval_re(args
[1], n_pg
, 0);
483 ren_ejectpage(args
[0][0] == c_cc
);
487 void tr_pn(char **args
)
490 bp_next
= eval_re(args
[1], n_pg
, 0);
493 static void ren_ps(char *s
)
495 int ps
= !s
|| !*s
|| !strcmp("0", s
) ? n_s0
: eval_re(s
, n_s
, 0);
500 void tr_ps(char **args
)
505 void tr_ll(char **args
)
507 int ll
= args
[1] ? eval_re(args
[1], n_l
, 'm') : n_l0
;
512 void tr_in(char **args
)
514 int in
= args
[1] ? eval_re(args
[1], n_i
, 'm') : n_i0
;
515 if (args
[0][0] == c_cc
)
522 void tr_ti(char **args
)
524 if (args
[0][0] == c_cc
)
527 n_ti
= eval_re(args
[1], n_i
, 'm');
530 static void ren_ft(char *s
)
532 int fn
= !s
|| !*s
|| !strcmp("P", s
) ? n_f0
: dev_pos(s
);
534 errmsg("neatroff: failed to mount <%s>\n", s
);
541 void tr_ft(char **args
)
546 void tr_fp(char **args
)
550 if (dev_mnt(atoi(args
[1]), args
[2], args
[3] ? args
[3] : args
[2]) < 0)
551 errmsg("neatroff: failed to mount <%s>\n", args
[2]);
554 void tr_nf(char **args
)
556 if (args
[0][0] == c_cc
)
561 void tr_fi(char **args
)
563 if (args
[0][0] == c_cc
)
568 void tr_ce(char **args
)
570 if (args
[0][0] == c_cc
)
572 n_ce
= args
[1] ? atoi(args
[1]) : 1;
575 void tr_fc(char **args
)
579 if (fa
&& charread(&fa
, c_fa
) >= 0) {
580 if (!fb
|| charread(&fb
, c_fb
) < 0)
588 static void ren_cl(char *s
)
590 int m
= !s
|| !*s
? n_m0
: clr_get(s
);
595 void tr_cl(char **args
)
600 void tr_ab(char **args
)
602 fprintf(stderr
, "%s\n", args
[1]);
606 static void ren_cmd(struct wb
*wb
, int c
, char *arg
)
610 wb_hmov(wb
, N_SS(n_f
, n_s
));
622 wb_vmov(wb
, SC_EM
/ 2);
628 wb_hmov(wb
, eval(arg
, 'm'));
631 num_set(map(arg
), wb
== cwb
? f_hpos() - n_lb
: wb_wid(wb
));
647 while (fmt_fillreq(cfmt
))
657 wb_vmov(wb
, -SC_EM
/ 2);
660 wb_vmov(wb
, eval(arg
, 'v'));
666 wb_els(wb
, eval(arg
, 'v'));
672 wb_hmov(wb
, SC_EM
/ 6);
678 wb_hmov(wb
, SC_EM
/ 12);
681 wb_italiccorrection(wb
);
684 wb_italiccorrectionleft(wb
);
692 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int));
693 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int));
695 /* insert a character, escape sequence, field or etc into wb */
696 static void ren_put(struct wb
*wb
, char *c
, int (*next
)(void), void (*back
)(int))
701 if (c
[0] == ' ' || c
[0] == '\n') {
705 if (c
[0] == '\t' || c
[0] == '\x01') {
706 ren_tab(wb
, c
[0] == '\t' ? c_tc
: c_lc
, next
, back
);
709 if (c_fa
[0] && !strcmp(c_fa
, c
)) {
710 ren_field(wb
, next
, back
);
716 ren_char(wb
, next
, back
);
717 wb_hmov(wb
, w
- wb_wid(wb
));
721 if (ren_nl
&& next
== ren_next
) {
724 while (n
>= 0 && n
!= '\n') {
729 ren_transparent(arg
);
733 if (strchr(" bCcDdefHhkLlmNoprSsuvXxz0^|{}&/,", c
[1])) {
734 argnext(arg
, c
[1], next
, back
);
736 snprintf(c
, GNLEN
, "%c%c", c_ec
, c_ec
);
737 } else if (c
[1] == 'N') {
738 snprintf(c
, GNLEN
, "GID=%s", arg
);
740 ren_cmd(wb
, c
[1], arg
);
745 if (!ren_div
&& cdef_map(c
, n_f
)) { /* .char characters */
749 if (!n_lg
|| ren_div
|| wb_lig(wb
, c
)) {
750 if (n_kn
&& !ren_div
)
756 /* read one character and place it inside wb buffer */
757 int ren_char(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
760 if (charnext(c
, next
, back
) < 0)
762 ren_put(wb
, c
, next
, back
);
766 /* like ren_char(); return 1 if d1 was read and d2 if d2 was read */
767 static int ren_chardel(struct wb
*wb
, int (*next
)(void), void (*back
)(int),
771 if (charnext(c
, next
, back
) < 0)
773 if (d1
&& !strcmp(d1
, c
))
775 if (d2
&& !strcmp(d2
, c
))
777 ren_put(wb
, c
, next
, back
);
781 /* read the argument of \w and push its width */
782 int ren_wid(int (*next
)(void), void (*back
)(int))
788 charnext(delim
, next
, back
);
791 while (c
>= 0 && c
!= '\n') {
793 if (ren_chardel(&wb
, next
, back
, delim
, NULL
))
799 wb_wconf(&wb
, &n_ct
, &n_st
, &n_sb
, &n_llx
, &n_lly
, &n_urx
, &n_ury
);
804 /* return 1 if d1 was read and 2 if d2 was read */
805 static int ren_until(struct wb
*wb
, char *d1
, char *d2
,
806 int (*next
)(void), void (*back
)(int))
810 while (c
>= 0 && c
!= '\n') {
812 ret
= ren_chardel(wb
, next
, back
, d1
, d2
);
822 static void wb_cpy(struct wb
*dst
, struct wb
*src
, int left
)
824 wb_hmov(dst
, left
- wb_wid(dst
));
828 void ren_tl(int (*next
)(void), void (*back
)(int))
834 charnext(delim
, next
, back
);
835 /* the left-adjusted string */
836 ren_until(&wb2
, delim
, NULL
, next
, back
);
837 wb_cpy(&wb
, &wb2
, 0);
838 /* the centered string */
839 ren_until(&wb2
, delim
, NULL
, next
, back
);
840 wb_cpy(&wb
, &wb2
, (n_lt
- wb_wid(&wb2
)) / 2);
841 /* the right-adjusted string */
842 ren_until(&wb2
, delim
, NULL
, next
, back
);
843 wb_cpy(&wb
, &wb2
, n_lt
- wb_wid(&wb2
));
844 /* flushing the line */
845 ren_line(sbuf_buf(&wb
.sbuf
), wb_wid(&wb
), AD_L
, 0,
846 0, n_lt
, wb
.els_neg
, wb
.els_pos
);
851 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
853 struct wb wbs
[NFIELDS
];
856 int left
, right
, cur_left
;
858 while (n
< LEN(wbs
)) {
860 if (ren_until(&wbs
[n
++], c_fb
, c_fa
, next
, back
) != 1)
863 left
= wb
== cwb
? f_hpos() : wb_wid(wb
);
864 right
= tab_next(left
);
865 for (i
= 0; i
< n
; i
++)
866 wid
+= wb_wid(&wbs
[i
]);
867 pad
= (right
- left
- wid
) / (n
> 1 ? n
- 1 : 1);
868 rem
= (right
- left
- wid
) % (n
> 1 ? n
- 1 : 1);
869 for (i
= 0; i
< n
; i
++) {
873 cur_left
= right
- wb_wid(&wbs
[i
]);
875 cur_left
= wb_wid(wb
) + pad
+ (i
+ rem
>= n
);
876 wb_cpy(wb
, &wbs
[i
], cur_left
);
881 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int))
884 int pos
= wb
== cwb
? f_hpos() : wb_wid(wb
);
885 int ins
= tab_next(pos
); /* insertion position */
886 int typ
= tab_type(pos
); /* tab type */
889 if (typ
== 'R' || typ
== 'C') {
891 while (c
>= 0 && c
!= '\n' && c
!= '\t' && c
!= '\x01') {
893 ren_char(&t
, next
, back
);
899 ins
-= wb_wid(&t
) / 2;
902 if (!tc
[0] || ins
<= pos
)
903 wb_hmov(wb
, ins
- pos
);
905 ren_hline(wb
, ins
- pos
, tc
);
910 /* parse characters and troff requests of s and append them to wb */
911 int ren_parse(struct wb
*wb
, char *s
)
919 if (ren_char(wb
, sstr_next
, sstr_back
))
928 /* cause nested render_rec() to exit */
929 void tr_popren(char **args
)
931 ren_level
= args
[1] ? atoi(args
[1]) : 0;
934 #define FMT_PAR() (n_u && !n_na && !n_ce && (n_j & AD_P) == AD_P)
936 /* read characters from tr.c and pass the rendered lines to out.c */
937 static int render_rec(int level
)
940 while (ren_level
>= level
) {
941 while (!tr_nextreq())
942 if (ren_level
< level
)
944 if (ren_level
< level
)
963 ren_partial
= c
!= '\n';
964 /* add cwb (the current word) to cfmt */
965 if (c
== ' ' || c
== '\n') {
966 if (!wb_part(cwb
)) { /* not after a \c */
967 while (fmt_word(cfmt
, cwb
))
971 while (fmt_newline(cfmt
))
979 /* flush the line if necessary */
980 if ((!FMT_PAR() && (c
== ' ' || c
== '\n')) || c
< 0)
982 if (c
== '\n' || ren_nl
) /* end or start of input line */
984 if (c
== '\n' && n_it
&& --n_itn
== 0)
986 if (c
== '\n' && !wb_part(cwb
))
987 n_ce
= MAX(0, n_ce
- 1);
988 if (c
!= ' ' && c
>= 0) {
990 ren_char(cwb
, ren_next
, ren_back
);
998 /* render input words */
1002 while (!tr_nextreq())
1004 ren_first(); /* transition to the first page */
1007 if (fmt_morewords(cfmt
))
1008 ren_page(bp_next
, 1);
1015 #define tposval(i) (tpos[i] < 0 ? n_p + tpos[i] : tpos[i])
1017 static int tpos
[NTRAPS
]; /* trap positions */
1018 static int treg
[NTRAPS
]; /* trap registers */
1021 static int trap_first(int pos
)
1025 for (i
= 0; i
< ntraps
; i
++)
1026 if (treg
[i
] >= 0 && tposval(i
) > pos
)
1027 if (best
< 0 || tposval(i
) < tposval(best
))
1032 static int trap_byreg(int reg
)
1035 for (i
= 0; i
< ntraps
; i
++)
1041 static int trap_bypos(int reg
, int pos
)
1044 for (i
= 0; i
< ntraps
; i
++)
1045 if (treg
[i
] >= 0 && tposval(i
) == pos
)
1046 if (reg
== -1 || treg
[i
] == reg
)
1051 void tr_wh(char **args
)
1056 pos
= eval(args
[1], 'v');
1057 id
= trap_bypos(-1, pos
);
1065 id
= trap_byreg(-1);
1072 void tr_ch(char **args
)
1079 id
= trap_byreg(reg
);
1081 tpos
[id
] = args
[2] ? eval(args
[2], 'v') : -1;
1084 void tr_dt(char **args
)
1089 cdiv
->tpos
= eval(args
[1], 'v');
1090 cdiv
->treg
= map(args
[2]);
1096 void tr_em(char **args
)
1098 trap_em
= args
[1] ? map(args
[1]) : -1;
1101 static int trap_pos(int pos
)
1103 int ret
= trap_first(pos
);
1107 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->tpos
: -1;
1108 return ret
>= 0 ? tposval(ret
) : -1;
1111 static int trap_reg(int pos
)
1113 int ret
= trap_first(pos
);
1115 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->treg
: -1;
1116 return ret
>= 0 ? treg
[ret
] : -1;
1119 int f_nexttrap(void)
1121 int pos
= trap_pos(n_d
);
1123 return pos
>= 0 ? pos
: 0x7fffffff;
1124 return (pos
>= 0 && pos
< n_p
? pos
: n_p
) - n_d
;