1 /* rendering lines and managing traps */
8 #define cfmt env_fmt() /* 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 a newline */
30 static int ren_partial
; /* reading an input line in render_rec() */
31 static int ren_unbuf
[8]; /* ren_back() buffer */
33 static int ren_aborted
; /* .ab executed */
35 static int bp_first
= 1; /* prior to the first page */
36 static int bp_next
= 1; /* next page number */
37 static int bp_count
; /* number of pages so far */
38 static int bp_ejected
; /* current ejected page */
39 static int bp_final
; /* 1: executing em, 2: the final page, 3: the 2nd final page */
40 static int ren_level
; /* the depth of render_rec() calls */
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 fmt_wid(cfmt
) + 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 int ren_first(void)
145 if (bp_first
&& !cdiv
) {
147 ren_page(bp_next
, 1);
153 /* when nodiv, do not append .sp to diversions */
154 static void ren_sp(int n
, int nodiv
)
157 /* ignore .sp without arguments when reading diversions */
158 if (!n
&& ren_div
&& !n_u
)
163 if (cdiv
&& !nodiv
) {
164 sbuf_putnl(&cdiv
->sbuf
);
165 sbuf_printf(&cdiv
->sbuf
, "%csp %du\n", c_cc
, n
? n
: n_v
);
171 static int render_rec(int level
);
172 static void trap_exec(int reg
)
175 int partial
= ren_partial
;
177 sprintf(cmd
, "%c%s %d\n", c_cc
, TR_POPREN
, ren_level
);
178 in_pushnl(cmd
, NULL
);
179 in_pushnl(str_get(reg
), NULL
);
180 render_rec(++ren_level
);
181 /* executed the trap while in the middle of an input line */
183 fmt_suppressnl(cfmt
);
187 static int detect_traps(int beg
, int end
)
189 int pos
= trap_pos(beg
);
190 return pos
>= 0 && (cdiv
|| pos
< n_p
) && pos
<= end
;
193 /* return 1 if executed a trap */
194 static int ren_traps(int beg
, int end
, int dosp
)
196 int pos
= trap_pos(beg
);
197 if (detect_traps(beg
, end
)) {
198 if (dosp
&& pos
> beg
)
199 ren_sp(pos
- beg
, 0);
200 trap_exec(trap_reg(beg
));
206 static int detect_pagelimit(int ne
)
208 return !cdiv
&& n_nl
+ ne
>= n_p
;
211 /* start a new page if needed */
212 static int ren_pagelimit(int ne
)
214 if (detect_pagelimit(ne
)) {
215 ren_page(bp_next
, 0);
221 /* return 1 if triggered a trap */
222 static int down(int n
)
224 if (ren_traps(n_d
, n_d
+ (n
? n
: n_v
), 1))
227 return ren_pagelimit(0);
230 /* line adjustment */
231 static int ren_ljust(struct sbuf
*spre
, int w
, int ad
, int li
, int ll
)
234 int llen
= ll
- ljust
;
236 if ((ad
& AD_B
) == AD_C
)
237 ljust
+= llen
> w
? (llen
- w
) / 2 : 0;
238 if ((ad
& AD_B
) == AD_R
)
241 sbuf_printf(spre
, "%ch'%du'", c_ec
, ljust
);
242 if (cdiv
&& cdiv
->dl
< w
+ ljust
)
243 cdiv
->dl
= w
+ ljust
;
247 /* append the line to the current diversion or send it to out.c */
248 static void ren_out(char *beg
, char *mid
, char *end
)
251 sbuf_append(&cdiv
->sbuf
, beg
);
252 sbuf_append(&cdiv
->sbuf
, mid
);
253 sbuf_append(&cdiv
->sbuf
, end
);
263 static void ren_transparent(char *s
)
266 sbuf_printf(&cdiv
->sbuf
, "%s\n", s
);
271 static int zwid(void)
273 struct glyph
*g
= dev_glyph("0", n_f
);
274 return charwid(n_f
, n_s
, g
? g
->wid
: 0);
277 /* append the line number to the output line */
278 static void ren_lnum(struct sbuf
*spre
)
285 if (n_nn
<= 0 && (n_ln
% n_nM
) == 0)
286 sprintf(num
, "%d", n_ln
);
287 wb_hmov(&wb
, n_nI
* zwid());
289 wb_hmov(&wb
, (3 - strlen(num
)) * zwid());
294 wb_hmov(&wb
, n_nS
* zwid());
295 sbuf_append(spre
, sbuf_buf(&wb
.sbuf
));
303 /* append margin character */
304 static void ren_mc(struct sbuf
*sbuf
, int w
, int ljust
)
308 if (w
+ ljust
< n_l
+ n_mcn
)
309 wb_hmov(&wb
, n_l
+ n_mcn
- w
- ljust
);
310 wb_putexpand(&wb
, c_mc
);
311 sbuf_append(sbuf
, sbuf_buf(&wb
.sbuf
));
315 /* process a line and print it with ren_out() */
316 static int ren_line(char *line
, int w
, int ad
, int body
,
317 int li
, int ll
, int els_neg
, int els_pos
)
319 struct sbuf sbeg
, send
;
320 int prev_d
, lspc
, ljust
;
324 lspc
= MAX(1, n_L
) * n_v
; /* line space, ignoreing \x */
328 if (!n_ns
|| line
[0] || els_neg
|| els_pos
) {
330 if (line
[0] && n_nm
&& body
)
332 ljust
= ren_ljust(&sbeg
, w
, ad
, li
, ll
);
333 if (line
[0] && body
&& n_mc
)
334 ren_mc(&send
, w
, ljust
);
335 ren_out(sbuf_buf(&sbeg
), line
, sbuf_buf(&send
));
343 if (detect_traps(prev_d
, n_d
) || detect_pagelimit(lspc
- n_v
)) {
344 if (!ren_pagelimit(lspc
- n_v
))
345 ren_traps(prev_d
, n_d
, 0);
348 if (lspc
- n_v
&& down(lspc
- n_v
))
353 /* read a line from fmt and send it to ren_line() */
354 static int ren_passline(struct fmt
*fmt
)
357 int ll
, li
, els_neg
, els_pos
, w
, ret
;
360 if (!fmt_morewords(fmt
))
363 fmt_nextline(fmt
, &sbuf
, &w
, &li
, &ll
, &els_neg
, &els_pos
);
364 if ((n_cp
&& !n_u
) || n_na
)
368 ret
= ren_line(sbuf_buf(&sbuf
), w
, ad
, 1, li
, ll
, els_neg
, els_pos
);
373 /* output formatted lines in fmt */
374 static void ren_fmtpop(struct fmt
*fmt
)
376 while (fmt_morelines(fmt
))
380 /* format and output all lines in fmt */
381 static void ren_fmtpopall(struct fmt
*fmt
)
383 while (fmt_fill(fmt
))
388 /* output current line; returns 1 if triggered a trap */
389 static int ren_br(void)
394 return ren_passline(cfmt
);
397 void tr_br(char **args
)
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
, spacewid(n_f
, n_s
));
623 wb_vmov(wb
, SC_EM
/ 2);
629 wb_hmov(wb
, eval(arg
, 'm'));
632 num_set(map(arg
), RENWB(wb
) ? 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'));
673 wb_hmov(wb
, SC_EM
/ 6);
679 wb_hmov(wb
, SC_EM
/ 12);
682 wb_italiccorrection(wb
);
685 wb_italiccorrectionleft(wb
);
693 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int));
694 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int));
696 /* insert a character, escape sequence, field or etc into wb */
697 static void ren_put(struct wb
*wb
, char *c
, int (*next
)(void), void (*back
)(int))
702 if (c
[0] == ' ' || c
[0] == '\n') {
706 if (c
[0] == '\t' || c
[0] == '\x01') {
707 ren_tab(wb
, c
[0] == '\t' ? c_tc
: c_lc
, next
, back
);
710 if (c_fa
[0] && !strcmp(c_fa
, c
)) {
711 ren_field(wb
, next
, back
);
717 ren_char(wb
, next
, back
);
718 wb_hmov(wb
, w
- wb_wid(wb
));
722 if (ren_nl
&& next
== ren_next
) {
725 while (n
>= 0 && n
!= '\n') {
730 ren_transparent(arg
);
734 if (strchr(" bCcDdfHhkLlmNoprSsuvXxz0^|{}&/,", c
[1])) {
735 argnext(arg
, c
[1], next
, back
);
736 if (c
[1] == 'S' || c
[1] == 'H')
737 return; /* not implemented */
739 ren_cmd(wb
, c
[1], arg
);
742 snprintf(c
, GNLEN
, "GID=%s", 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
= RENWB(wb
) ? 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
= RENWB(wb
) ? 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 /* read characters from tr.c and pass the rendered lines to out.c */
935 static int render_rec(int level
)
937 struct wb
*wb
= &ren_wb
;
939 while (ren_level
>= level
) {
940 while (!tr_nextreq())
941 if (ren_level
< level
)
943 if (ren_level
< level
)
962 ren_partial
= c
!= '\n';
963 /* add wb (the current word) to cfmt */
964 if (c
== ' ' || c
== '\n') {
965 if (!wb_part(wb
)) { /* not after a \c */
966 while (fmt_word(cfmt
, wb
))
970 while (fmt_newline(cfmt
))
979 /* flush the line if necessary */
980 if (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(wb
))
987 n_ce
= MAX(0, n_ce
- 1);
988 if (c
!= ' ' && c
>= 0) {
990 ren_char(wb
, ren_next
, ren_back
);
998 /* render input words */
1001 struct wb
*wb
= &ren_wb
;
1004 while (!tr_nextreq())
1006 ren_first(); /* transition to the first page */
1009 if (fmt_morewords(cfmt
))
1010 ren_page(bp_next
, 1);
1018 #define tposval(i) (tpos[i] < 0 ? n_p + tpos[i] : tpos[i])
1020 static int tpos
[NTRAPS
]; /* trap positions */
1021 static int treg
[NTRAPS
]; /* trap registers */
1024 static int trap_first(int pos
)
1028 for (i
= 0; i
< ntraps
; i
++)
1029 if (treg
[i
] >= 0 && tposval(i
) > pos
)
1030 if (best
< 0 || tposval(i
) < tposval(best
))
1035 static int trap_byreg(int reg
)
1038 for (i
= 0; i
< ntraps
; i
++)
1044 static int trap_bypos(int reg
, int pos
)
1047 for (i
= 0; i
< ntraps
; i
++)
1048 if (treg
[i
] >= 0 && tposval(i
) == pos
)
1049 if (reg
== -1 || treg
[i
] == reg
)
1054 void tr_wh(char **args
)
1059 pos
= eval(args
[1], 'v');
1060 id
= trap_bypos(-1, pos
);
1068 id
= trap_byreg(-1);
1075 void tr_ch(char **args
)
1082 id
= trap_byreg(reg
);
1084 tpos
[id
] = args
[2] ? eval(args
[2], 'v') : -1;
1087 void tr_dt(char **args
)
1092 cdiv
->tpos
= eval(args
[1], 'v');
1093 cdiv
->treg
= map(args
[2]);
1099 void tr_em(char **args
)
1101 trap_em
= args
[1] ? map(args
[1]) : -1;
1104 static int trap_pos(int pos
)
1106 int ret
= trap_first(pos
);
1110 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->tpos
: -1;
1111 return ret
>= 0 ? tposval(ret
) : -1;
1114 static int trap_reg(int pos
)
1116 int ret
= trap_first(pos
);
1118 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->treg
: -1;
1119 return ret
>= 0 ? treg
[ret
] : -1;
1122 int f_nexttrap(void)
1124 int pos
= trap_pos(n_d
);
1126 return pos
>= 0 ? pos
: 0x7fffffff;
1127 return (pos
>= 0 && pos
< n_p
? pos
: n_p
) - n_d
;