7 #define cadj env_adj() /* line buffer */
8 #define RENWB(wb) ((wb) == &ren_wb) /* is ren_wb */
12 struct sbuf sbuf
; /* diversion output */
13 int reg
; /* diversion register */
14 int tpos
; /* diversion trap position */
15 int treg
; /* diversion trap register */
16 int dl
; /* diversion width */
17 int prev_d
; /* previous \n(.d value */
18 int prev_h
; /* previous \n(.h value */
19 int prev_mk
; /* previous .mk internal register */
20 int prev_ns
; /* previous .ns value */
22 static struct div divs
[NPREV
]; /* diversion stack */
23 static struct div
*cdiv
; /* current diversion */
24 static int ren_div
; /* rendering a diversion */
25 static int trap_em
= -1; /* end macro */
27 static struct wb ren_wb
; /* the main ren.c word buffer */
28 static int ren_nl
; /* just after newline */
29 static int ren_cnl
; /* current char is a newline */
30 static int ren_unbuf
[8]; /* ren_back() buffer */
32 static int ren_fillreq
; /* \p request */
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 */
41 static int c_fa
; /* 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_base(int fn
, int sz
, int wid
)
90 /* the original troff rounds the widths up */
91 return (wid
* sz
+ dev_uwid
/ 2) / dev_uwid
;
94 int charwid(int fn
, int sz
, int wid
)
97 return dev_getcs(n_f
) * SC_EM
/ 36;
98 return charwid_base(fn
, sz
, wid
) +
99 (dev_getbd(fn
) ? dev_getbd(fn
) - 1 : 0);
102 int spacewid(int fn
, int sz
)
104 return charwid(fn
, sz
, (dev_font(fn
)->spacewid
* n_ss
+ 6) / 12);
109 return cdiv
? cdiv
->reg
: -1;
114 return adj_wid(cadj
) + wb_wid(&ren_wb
);
117 void tr_divbeg(char **args
)
123 void tr_divend(char **args
)
129 static int trap_reg(int pos
);
130 static int trap_pos(int pos
);
131 static void trap_exec(int reg
);
133 static void ren_page(int pg
, int force
)
135 if (!force
&& bp_final
>= 2)
145 if (trap_pos(-1) == 0)
146 trap_exec(trap_reg(-1));
149 static void ren_first(void)
151 if (bp_first
&& !cdiv
) {
153 ren_page(bp_next
, 1);
157 /* when nodiv, do not append .sp to diversions */
158 static void ren_sp(int n
, int nodiv
)
161 /* ignore .sp without arguments when reading diversions */
162 if (!n
&& ren_div
&& !n_u
)
167 if (cdiv
&& !nodiv
) {
168 sbuf_putnl(&cdiv
->sbuf
);
169 sbuf_printf(&cdiv
->sbuf
, "%csp %du\n", c_cc
, n
? n
: n_v
);
175 static void trap_exec(int reg
)
178 in_pushnl(str_get(reg
), NULL
);
181 static int detect_traps(int beg
, int end
)
183 int pos
= trap_pos(beg
);
184 return pos
>= 0 && (cdiv
|| pos
< n_p
) && pos
<= end
;
187 /* return 1 if executed a trap */
188 static int ren_traps(int beg
, int end
, int dosp
)
190 int pos
= trap_pos(beg
);
191 if (detect_traps(beg
, end
)) {
192 if (dosp
&& pos
> beg
)
193 ren_sp(pos
- beg
, 0);
194 trap_exec(trap_reg(beg
));
200 static int detect_pagelimit(int ne
)
202 return !cdiv
&& n_nl
+ ne
>= n_p
;
205 /* start a new page if needed */
206 static int ren_pagelimit(int ne
)
208 if (detect_pagelimit(ne
)) {
209 ren_page(bp_next
, 0);
215 static void down(int n
)
217 if (!ren_traps(n_d
, n_d
+ (n
? n
: n_v
), 1)) {
223 /* line adjustment */
224 static int ren_ljust(struct sbuf
*spre
, int w
, int ad
, int ll
, int li
, int lt
)
226 int ljust
= lt
>= 0 ? lt
: li
;
227 int llen
= ll
- ljust
;
230 ljust
+= llen
> w
? (llen
- w
) / 2 : 0;
234 sbuf_printf(spre
, "%ch'%du'", c_ec
, ljust
);
235 if (cdiv
&& cdiv
->dl
< w
)
240 /* append the line to the current diversion or send it to out.c */
241 static void ren_line(struct sbuf
*spre
, struct sbuf
*sbuf
)
244 if (!sbuf_empty(spre
))
245 sbuf_append(&cdiv
->sbuf
, sbuf_buf(spre
));
246 sbuf_append(&cdiv
->sbuf
, sbuf_buf(sbuf
));
250 if (!sbuf_empty(spre
))
251 out_line(sbuf_buf(spre
));
252 out_line(sbuf_buf(sbuf
));
256 static void ren_transparent(char *s
)
259 sbuf_printf(&cdiv
->sbuf
, "%s\n", s
);
264 static int zwid(void)
266 struct glyph
*g
= dev_glyph("0", n_f
);
267 return charwid(n_f
, n_s
, g
? g
->wid
: SC_DW
);
270 /* append the line number to the output line */
271 static void ren_lnum(struct sbuf
*spre
)
278 if (n_nn
<= 0 && (n_ln
% n_nM
) == 0)
279 sprintf(num
, "%d", n_ln
);
280 wb_hmov(&wb
, n_nI
* zwid());
282 wb_hmov(&wb
, (3 - strlen(num
)) * zwid());
287 wb_hmov(&wb
, n_nS
* zwid());
288 sbuf_append(spre
, sbuf_buf(&wb
.sbuf
));
296 /* append margin character */
297 static void ren_mc(struct sbuf
*sbuf
, int w
, int ljust
)
301 if (w
+ ljust
< n_l
+ n_mcn
)
302 wb_hmov(&wb
, n_l
+ n_mcn
- w
- ljust
);
304 sbuf_append(sbuf
, sbuf_buf(&wb
.sbuf
));
308 /* return 1 if triggered a trap */
309 static int ren_bradj(struct adj
*adj
, int fill
, int ad
, int body
)
312 struct sbuf sbuf
, spre
;
313 int ll
, li
, lt
, els_neg
, els_pos
;
314 int w
, prev_d
, lspc
, ljust
;
316 if (!adj_empty(adj
, fill
)) {
319 w
= adj_fill(adj
, ad
== AD_B
, fill
, n_hy
, &sbuf
,
320 &ll
, &li
, <
, &els_neg
, &els_pos
);
324 if (!n_ns
|| !sbuf_empty(&sbuf
) || els_neg
|| els_pos
) {
326 if (!sbuf_empty(&sbuf
) && n_nm
&& body
)
328 ljust
= ren_ljust(&spre
, w
, ad
, ll
, li
, lt
);
329 if (!sbuf_empty(&sbuf
) && body
&& n_mc
)
330 ren_mc(&sbuf
, w
, ljust
);
331 ren_line(&spre
, &sbuf
);
339 lspc
= MAX(1, n_L
) * n_v
- n_v
;
340 if (detect_traps(prev_d
, n_d
) || detect_pagelimit(lspc
)) {
341 sprintf(cmd
, "%c&", c_ec
);
342 if (!ren_cnl
) /* prevent unwanted newlines */
344 if (!ren_traps(prev_d
, n_d
, 0))
354 /* return 1 if triggered a trap */
355 static int ren_br(int force
)
358 if (!n_u
|| n_na
|| (n_j
== AD_B
&& force
))
362 return ren_bradj(cadj
, !force
&& !n_ce
&& n_u
, ad
, 1);
365 void tr_br(char **args
)
367 if (args
[0][0] == c_cc
)
371 void tr_sp(char **args
)
374 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
375 if (args
[0][0] == c_cc
)
377 if (n
&& !n_ns
&& !traps
)
381 void tr_sv(char **args
)
383 int n
= eval(args
[1], 'v');
385 if (n_d
+ n
< f_nexttrap())
391 void tr_ns(char **args
)
396 void tr_rs(char **args
)
401 void tr_os(char **args
)
408 void tr_mk(char **args
)
411 num_set(map(args
[1]), n_d
);
416 void tr_rt(char **args
)
418 int n
= args
[1] ? eval_re(args
[1], n_d
, 'v') : n_mk
;
419 if (n
>= 0 && n
< n_d
)
423 void tr_ne(char **args
)
425 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
426 if (!ren_traps(n_d
, n_d
+ n
- 1, 1))
430 static void push_eject(void)
433 bp_ejected
= bp_count
;
434 sprintf(buf
, "%c%s %d\n", c_cc
, TR_EJECT
, bp_ejected
);
435 in_pushnl(buf
, NULL
);
438 static void push_br(void)
440 char br
[8] = {c_cc
, 'b', 'r', '\n'};
444 static void ren_eject(int id
)
446 if (id
== bp_ejected
&& id
== bp_count
&& !cdiv
) {
447 if (detect_traps(n_d
, n_p
)) {
449 ren_traps(n_d
, n_p
, 1);
452 ren_page(bp_next
, 0);
457 void tr_eject(char **args
)
459 ren_eject(atoi(args
[1]));
462 void tr_bp(char **args
)
464 if (!cdiv
&& (args
[1] || !n_ns
)) {
465 if (bp_ejected
!= bp_count
)
467 if (args
[0][0] == c_cc
)
470 bp_next
= eval_re(args
[1], n_pg
, 0);
474 void tr_pn(char **args
)
477 bp_next
= eval_re(args
[1], n_pg
, 0);
480 static void ren_ps(char *s
)
482 int ps
= !s
|| !*s
|| !strcmp("0", s
) ? n_s0
: eval_re(s
, n_s
, 0);
487 void tr_ps(char **args
)
492 void tr_ll(char **args
)
494 int ll
= args
[1] ? eval_re(args
[1], n_l
, 'm') : n_l0
;
500 void tr_in(char **args
)
502 int in
= args
[1] ? eval_re(args
[1], n_i
, 'm') : n_i0
;
503 if (args
[0][0] == c_cc
)
511 void tr_ti(char **args
)
513 if (args
[0][0] == c_cc
)
516 adj_ti(cadj
, eval_re(args
[1], n_i
, 'm'));
519 static void ren_ft(char *s
)
521 int fn
= !s
|| !*s
|| !strcmp("P", s
) ? n_f0
: dev_pos(s
);
528 void tr_ft(char **args
)
533 void tr_fp(char **args
)
537 if (dev_mnt(atoi(args
[1]), args
[2], args
[3] ? args
[3] : args
[2]) < 0)
538 errmsg("troff: failed to mount %s\n", args
[2]);
541 void tr_nf(char **args
)
543 if (args
[0][0] == c_cc
)
548 void tr_fi(char **args
)
550 if (args
[0][0] == c_cc
)
555 void tr_ce(char **args
)
557 if (args
[0][0] == c_cc
)
559 n_ce
= args
[1] ? atoi(args
[1]) : 1;
562 void tr_fc(char **args
)
566 strcpy(c_fb
, args
[2] ? args
[2] : " ");
573 static void ren_m(char *s
)
575 int m
= !s
|| !*s
? n_m0
: clr_get(s
);
580 void tr_ab(char **args
)
582 fprintf(stderr
, "%s\n", args
[1]);
586 static void escarg_ren(char *d
, int cmd
, int (*next
)(void), void (*back
)(int))
590 if (strchr(ESC_P
, cmd
)) {
592 if (cmd
== 's' && (c
== '-' || c
== '+')) {
599 } else if (!n_cp
&& c
== '[') {
601 while (c
> 0 && c
!= '\n' && c
!= ']') {
607 if (cmd
== 's' && c
>= '1' && c
<= '3') {
616 if (strchr(ESC_Q
, cmd
)) {
617 schar_read(delim
, next
);
618 while (schar_jump(delim
, next
, back
)) {
619 if ((c
= next()) < 0)
627 static int nextchar(char *s
, int (*next
)(void))
635 for (i
= 1; i
< l
; i
++)
641 static void ren_cmd(struct wb
*wb
, int c
, char *arg
)
645 wb_hmov(wb
, spacewid(n_f
, n_s
));
657 wb_vmov(wb
, SC_EM
/ 2);
663 wb_hmov(wb
, eval(arg
, 'm'));
666 num_set(map(arg
), RENWB(wb
) ? f_hpos() - n_lb
: wb_wid(wb
));
691 wb_vmov(wb
, -SC_EM
/ 2);
694 wb_vmov(wb
, eval(arg
, 'v'));
700 wb_els(wb
, eval(arg
, 'v'));
706 wb_hmov(wb
, SC_EM
/ 6);
712 wb_hmov(wb
, SC_EM
/ 12);
720 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int));
721 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int));
723 /* read one character and place it inside wb buffer */
724 void ren_char(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
732 if (c
[0] == ' ' || c
[0] == '\n') {
736 if (c
[0] == '\t' || c
[0] == '\x01') {
737 ren_tab(wb
, c
[0] == '\t' ? c_tc
: c_lc
, next
, back
);
741 ren_field(wb
, next
, back
);
745 nextchar(c
+ 1, next
);
747 nextchar(c
+ 1, next
);
749 l
= nextchar(c
+ 2, next
);
750 l
+= nextchar(c
+ 2 + l
, next
);
752 } else if (!n_cp
&& c
[1] == '[') {
755 while (n
>= 0 && n
!= '\n' && n
!= ']' && l
< GNLEN
- 1) {
760 } else if (c
[1] == 'z') {
762 ren_char(wb
, next
, back
);
763 wb_hmov(wb
, w
- wb_wid(wb
));
765 } else if (c
[1] == '!') {
766 if (ren_nl
&& next
== ren_next
) {
769 while (n
>= 0 && n
!= '\n') {
774 ren_transparent(arg
);
777 } else if (strchr(" bCcDdfHhkLlmNoprSsuvXxz0^|{}&", c
[1])) {
778 escarg_ren(arg
, c
[1], next
, back
);
780 g
= dev_glyph_byid(arg
, n_f
);
782 strcpy(arg
, g
? g
->name
: "cnull");
784 if (c
[1] == 'S' || c
[1] == 'H')
785 return; /* not implemented */
787 ren_cmd(wb
, c
[1], arg
);
793 if (!n_lg
|| wb_lig(wb
, c
)) {
800 /* read the argument of \w and push its width */
801 int ren_wid(int (*next
)(void), void (*back
)(int))
807 schar_read(delim
, next
);
810 while (c
>= 0 && c
!= '\n') {
812 if (!schar_jump(delim
, next
, back
))
814 ren_char(&wb
, next
, back
);
819 wb_wconf(&wb
, &n_ct
, &n_st
, &n_sb
);
824 /* return 1 if the ending character (ec) was read */
825 static int ren_until(struct wb
*wb
, char *delim
, int ec
,
826 int (*next
)(void), void (*back
)(int))
830 while (c
>= 0 && c
!= '\n' && c
!= ec
) {
832 if (!schar_jump(delim
, next
, back
))
834 ren_char(wb
, next
, back
);
842 static void wb_cpy(struct wb
*dst
, struct wb
*src
, int left
)
844 wb_hmov(dst
, left
- wb_wid(dst
));
848 void ren_tl(int (*next
)(void), void (*back
)(int))
856 schar_read(delim
, next
);
857 /* the left-adjusted string */
858 ren_until(&wb2
, delim
, '\n', next
, back
);
859 wb_cpy(&wb
, &wb2
, 0);
860 /* the centered string */
861 ren_until(&wb2
, delim
, '\n', next
, back
);
862 wb_cpy(&wb
, &wb2
, (n_lt
- wb_wid(&wb2
)) / 2);
863 /* the right-adjusted string */
864 ren_until(&wb2
, delim
, '\n', next
, back
);
865 wb_cpy(&wb
, &wb2
, n_lt
- wb_wid(&wb2
));
866 /* flushing the line */
870 ren_bradj(adj
, 0, AD_L
, 0);
876 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
878 struct wb wbs
[NFIELDS
];
881 int left
, right
, cur_left
;
883 while (n
< LEN(wbs
)) {
885 if (ren_until(&wbs
[n
++], c_fb
, c_fa
, next
, back
))
888 left
= RENWB(wb
) ? f_hpos() : wb_wid(wb
);
889 right
= tab_next(left
);
890 for (i
= 0; i
< n
; i
++)
891 wid
+= wb_wid(&wbs
[i
]);
892 pad
= (right
- left
- wid
) / (n
> 1 ? n
- 1 : 1);
893 rem
= (right
- left
- wid
) % (n
> 1 ? n
- 1 : 1);
894 for (i
= 0; i
< n
; i
++) {
898 cur_left
= right
- wb_wid(&wbs
[i
]);
900 cur_left
= wb_wid(wb
) + pad
+ (i
+ rem
>= n
);
901 wb_cpy(wb
, &wbs
[i
], cur_left
);
906 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int))
909 int pos
= RENWB(wb
) ? f_hpos() : wb_wid(wb
);
910 int ins
= tab_next(pos
); /* insertion position */
911 int typ
= tab_type(pos
); /* tab type */
914 if (typ
== 'R' || typ
== 'C') {
916 while (c
>= 0 && c
!= '\n' && c
!= '\t' && c
!= '\x01') {
918 ren_char(&t
, next
, back
);
924 ins
-= wb_wid(&t
) / 2;
927 if (!tc
[0] || ins
<= pos
)
928 wb_hmov(wb
, ins
- pos
);
930 ren_hline(wb
, ins
- pos
, tc
);
935 /* read characters from in.c and pass rendered lines to out.c */
938 struct wb
*wb
= &ren_wb
;
944 ren_first(); /* transition to the first page */
952 if (bp_final
== 0 && trap_em
>= 0) {
965 /* add wb (the current word) to cadj */
966 if (c
== ' ' || c
== '\n') {
967 adj_swid(cadj
, spacewid(n_f
, n_s
));
968 if (!wb_part(wb
)) { /* not after a \c */
970 fillreq
= ren_fillreq
;
978 while ((fillreq
&& !n_ce
&& n_u
) || adj_full(cadj
, !n_ce
&& n_u
)) {
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);
990 ren_char(wb
, ren_next
, ren_back
);
991 if (c
!= '\n' && wb_empty(wb
))
998 if (!adj_empty(cadj
, 0))
999 ren_page(bp_next
, 1);
1007 #define tposval(i) (tpos[i] < 0 ? n_p + tpos[i] : tpos[i])
1009 static int tpos
[NTRAPS
]; /* trap positions */
1010 static int treg
[NTRAPS
]; /* trap registers */
1013 static int trap_first(int pos
)
1017 for (i
= 0; i
< ntraps
; i
++)
1018 if (treg
[i
] >= 0 && tposval(i
) > pos
)
1019 if (best
< 0 || tposval(i
) < tposval(best
))
1024 static int trap_byreg(int reg
)
1027 for (i
= 0; i
< ntraps
; i
++)
1033 static int trap_bypos(int reg
, int pos
)
1036 for (i
= 0; i
< ntraps
; i
++)
1037 if (treg
[i
] >= 0 && tposval(i
) == pos
)
1038 if (reg
== -1 || treg
[i
] == reg
)
1043 void tr_wh(char **args
)
1048 pos
= eval(args
[1], 'v');
1049 id
= trap_bypos(-1, pos
);
1057 id
= trap_byreg(-1);
1064 void tr_ch(char **args
)
1071 id
= trap_byreg(reg
);
1073 tpos
[id
] = args
[2] ? eval(args
[2], 'v') : -1;
1076 void tr_dt(char **args
)
1081 cdiv
->tpos
= eval(args
[1], 'v');
1082 cdiv
->treg
= map(args
[2]);
1088 void tr_em(char **args
)
1090 trap_em
= args
[1] ? map(args
[1]) : -1;
1093 static int trap_pos(int pos
)
1095 int ret
= trap_first(pos
);
1099 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->tpos
: -1;
1100 return ret
>= 0 ? tposval(ret
) : -1;
1103 static int trap_reg(int pos
)
1105 int ret
= trap_first(pos
);
1107 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->treg
: -1;
1108 return ret
>= 0 ? treg
[ret
] : -1;
1111 int f_nexttrap(void)
1113 int pos
= trap_pos(n_d
);
1115 return pos
>= 0 ? pos
: 0x7fffffff;
1116 return (pos
>= 0 && pos
< n_p
? pos
: n_p
) - n_d
;