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 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_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 /* return 1 if triggered a trap */
216 static int down(int n
)
218 if (ren_traps(n_d
, n_d
+ (n
? n
: n_v
), 1))
221 return ren_pagelimit(0);
224 /* line adjustment */
225 static int ren_ljust(struct sbuf
*spre
, int w
, int ad
, int ll
, int li
, int lt
)
227 int ljust
= lt
>= 0 ? lt
: li
;
228 int llen
= ll
- ljust
;
231 ljust
+= llen
> w
? (llen
- w
) / 2 : 0;
235 sbuf_printf(spre
, "%ch'%du'", c_ec
, ljust
);
236 if (cdiv
&& cdiv
->dl
< w
+ ljust
)
237 cdiv
->dl
= w
+ ljust
;
241 /* append the line to the current diversion or send it to out.c */
242 static void ren_line(struct sbuf
*spre
, struct sbuf
*sbuf
)
245 if (!sbuf_empty(spre
))
246 sbuf_append(&cdiv
->sbuf
, sbuf_buf(spre
));
247 sbuf_append(&cdiv
->sbuf
, sbuf_buf(sbuf
));
251 if (!sbuf_empty(spre
))
252 out_line(sbuf_buf(spre
));
253 out_line(sbuf_buf(sbuf
));
257 static void ren_transparent(char *s
)
260 sbuf_printf(&cdiv
->sbuf
, "%s\n", s
);
265 static int zwid(void)
267 struct glyph
*g
= dev_glyph("0", n_f
);
268 return charwid(n_f
, n_s
, g
? g
->wid
: SC_DW
);
271 /* append the line number to the output line */
272 static void ren_lnum(struct sbuf
*spre
)
279 if (n_nn
<= 0 && (n_ln
% n_nM
) == 0)
280 sprintf(num
, "%d", n_ln
);
281 wb_hmov(&wb
, n_nI
* zwid());
283 wb_hmov(&wb
, (3 - strlen(num
)) * zwid());
288 wb_hmov(&wb
, n_nS
* zwid());
289 sbuf_append(spre
, sbuf_buf(&wb
.sbuf
));
297 /* append margin character */
298 static void ren_mc(struct sbuf
*sbuf
, int w
, int ljust
)
302 if (w
+ ljust
< n_l
+ n_mcn
)
303 wb_hmov(&wb
, n_l
+ n_mcn
- w
- ljust
);
305 sbuf_append(sbuf
, sbuf_buf(&wb
.sbuf
));
309 /* return 1 if triggered a trap */
310 static int ren_bradj(struct adj
*adj
, int fill
, int ad
, int body
)
313 struct sbuf sbuf
, spre
;
314 int ll
, li
, lt
, els_neg
, els_pos
;
315 int w
, hyph
, prev_d
, lspc
, ljust
;
317 if (!adj_empty(adj
, fill
)) {
321 lspc
= MAX(1, n_L
) * n_v
; /* line space, ignoreing \x */
322 if (n_hy
& HY_LAST
&& (detect_traps(n_d
, n_d
+ lspc
) ||
323 detect_pagelimit(lspc
)))
324 hyph
= 0; /* disable for last lines */
325 w
= adj_fill(adj
, ad
== AD_B
, fill
, hyph
, &sbuf
,
326 &ll
, &li
, <
, &els_neg
, &els_pos
);
330 if (!n_ns
|| !sbuf_empty(&sbuf
) || els_neg
|| els_pos
) {
332 if (!sbuf_empty(&sbuf
) && n_nm
&& body
)
334 ljust
= ren_ljust(&spre
, w
, ad
, ll
, li
, lt
);
335 if (!sbuf_empty(&sbuf
) && body
&& n_mc
)
336 ren_mc(&sbuf
, w
, ljust
);
337 ren_line(&spre
, &sbuf
);
345 if (detect_traps(prev_d
, n_d
) || detect_pagelimit(lspc
- n_v
)) {
346 sprintf(cmd
, "%c&", c_ec
);
347 if (!ren_cnl
) /* prevent unwanted newlines */
349 if (!ren_traps(prev_d
, n_d
, 0))
350 ren_pagelimit(lspc
- n_v
);
353 if (lspc
- n_v
&& down(lspc
- n_v
))
359 /* return 1 if triggered a trap */
360 static int ren_br(int force
)
363 if (!n_u
|| n_na
|| (n_j
== AD_B
&& force
))
367 return ren_bradj(cadj
, !force
&& !n_ce
&& n_u
, ad
, 1);
370 void tr_br(char **args
)
372 if (args
[0][0] == c_cc
)
376 void tr_sp(char **args
)
379 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
380 if (args
[0][0] == c_cc
)
382 if (n
&& !n_ns
&& !traps
)
386 void tr_sv(char **args
)
388 int n
= eval(args
[1], 'v');
390 if (n_d
+ n
< f_nexttrap())
396 void tr_ns(char **args
)
401 void tr_rs(char **args
)
406 void tr_os(char **args
)
413 void tr_mk(char **args
)
416 num_set(map(args
[1]), n_d
);
421 void tr_rt(char **args
)
423 int n
= args
[1] ? eval_re(args
[1], n_d
, 'v') : n_mk
;
424 if (n
>= 0 && n
< n_d
)
428 void tr_ne(char **args
)
430 int n
= args
[1] ? eval(args
[1], 'v') : n_v
;
431 if (!ren_traps(n_d
, n_d
+ n
- 1, 1))
435 static void push_eject(void)
438 bp_ejected
= bp_count
;
439 sprintf(buf
, "%c%s %d\n", c_cc
, TR_EJECT
, bp_ejected
);
440 in_pushnl(buf
, NULL
);
443 static void push_br(void)
445 char br
[8] = {c_cc
, 'b', 'r', '\n'};
449 static void ren_eject(int id
)
451 if (id
== bp_ejected
&& id
== bp_count
&& !cdiv
) {
452 if (detect_traps(n_d
, n_p
)) {
454 ren_traps(n_d
, n_p
, 1);
457 ren_page(bp_next
, 0);
462 void tr_eject(char **args
)
464 ren_eject(atoi(args
[1]));
467 void tr_bp(char **args
)
469 if (!cdiv
&& (args
[1] || !n_ns
)) {
470 if (bp_ejected
!= bp_count
)
472 if (args
[0][0] == c_cc
)
475 bp_next
= eval_re(args
[1], n_pg
, 0);
479 void tr_pn(char **args
)
482 bp_next
= eval_re(args
[1], n_pg
, 0);
485 static void ren_ps(char *s
)
487 int ps
= !s
|| !*s
|| !strcmp("0", s
) ? n_s0
: eval_re(s
, n_s
, 0);
492 void tr_ps(char **args
)
497 void tr_ll(char **args
)
499 int ll
= args
[1] ? eval_re(args
[1], n_l
, 'm') : n_l0
;
505 void tr_in(char **args
)
507 int in
= args
[1] ? eval_re(args
[1], n_i
, 'm') : n_i0
;
508 if (args
[0][0] == c_cc
)
516 void tr_ti(char **args
)
518 if (args
[0][0] == c_cc
)
521 adj_ti(cadj
, eval_re(args
[1], n_i
, 'm'));
524 static void ren_ft(char *s
)
526 int fn
= !s
|| !*s
|| !strcmp("P", s
) ? n_f0
: dev_pos(s
);
528 errmsg("neatroff: failed to mount <%s>\n", s
);
535 void tr_ft(char **args
)
540 void tr_fp(char **args
)
544 if (dev_mnt(atoi(args
[1]), args
[2], args
[3] ? args
[3] : args
[2]) < 0)
545 errmsg("neatroff: failed to mount <%s>\n", args
[2]);
548 void tr_nf(char **args
)
550 if (args
[0][0] == c_cc
)
555 void tr_fi(char **args
)
557 if (args
[0][0] == c_cc
)
562 void tr_ce(char **args
)
564 if (args
[0][0] == c_cc
)
566 n_ce
= args
[1] ? atoi(args
[1]) : 1;
569 void tr_fc(char **args
)
573 if (fa
&& charread(&fa
, c_fa
) >= 0) {
574 if (!fb
|| charread(&fb
, c_fb
) < 0)
582 static void ren_m(char *s
)
584 int m
= !s
|| !*s
? n_m0
: clr_get(s
);
589 void tr_ab(char **args
)
591 fprintf(stderr
, "%s\n", args
[1]);
595 static void ren_cmd(struct wb
*wb
, int c
, char *arg
)
599 wb_hmov(wb
, spacewid(n_f
, n_s
));
611 wb_vmov(wb
, SC_EM
/ 2);
617 wb_hmov(wb
, eval(arg
, 'm'));
620 num_set(map(arg
), RENWB(wb
) ? f_hpos() - n_lb
: wb_wid(wb
));
645 wb_vmov(wb
, -SC_EM
/ 2);
648 wb_vmov(wb
, eval(arg
, 'v'));
654 wb_els(wb
, eval(arg
, 'v'));
660 wb_hmov(wb
, SC_EM
/ 6);
666 wb_hmov(wb
, SC_EM
/ 12);
674 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int));
675 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int));
677 /* insert a character, escape sequence, field or etc into wb */
678 static void ren_put(struct wb
*wb
, char *c
, int (*next
)(void), void (*back
)(int))
684 if (c
[0] == ' ' || c
[0] == '\n') {
688 if (c
[0] == '\t' || c
[0] == '\x01') {
689 ren_tab(wb
, c
[0] == '\t' ? c_tc
: c_lc
, next
, back
);
692 if (c_fa
[0] && !strcmp(c_fa
, c
)) {
693 ren_field(wb
, next
, back
);
699 ren_char(wb
, next
, back
);
700 wb_hmov(wb
, w
- wb_wid(wb
));
704 if (ren_nl
&& next
== ren_next
) {
707 while (n
>= 0 && n
!= '\n') {
712 ren_transparent(arg
);
716 if (strchr(" bCcDdfHhkLlmNoprSsuvXxz0^|{}&", c
[1])) {
717 argnext(arg
, c
[1], next
, back
);
718 if (c
[1] == 'S' || c
[1] == 'H')
719 return; /* not implemented */
721 ren_cmd(wb
, c
[1], arg
);
724 g
= dev_glyph_byid(arg
, n_f
);
725 strcpy(c
, g
? g
->name
: "cnull");
728 if (!n_lg
|| wb_lig(wb
, c
)) {
735 /* read one character and place it inside wb buffer */
736 int ren_char(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
739 if (charnext(c
, next
, back
) < 0)
741 ren_put(wb
, c
, next
, back
);
745 /* like ren_char(); return 1 if d1 was read and d2 if d2 was read */
746 static int ren_chardel(struct wb
*wb
, int (*next
)(void), void (*back
)(int),
750 if (charnext(c
, next
, back
) < 0)
752 if (d1
&& !strcmp(d1
, c
))
754 if (d2
&& !strcmp(d2
, c
))
756 ren_put(wb
, c
, next
, back
);
760 /* read the argument of \w and push its width */
761 int ren_wid(int (*next
)(void), void (*back
)(int))
767 charnext(delim
, next
, back
);
770 while (c
>= 0 && c
!= '\n') {
772 if (ren_chardel(&wb
, next
, back
, delim
, NULL
))
778 wb_wconf(&wb
, &n_ct
, &n_st
, &n_sb
);
783 /* return 1 if d1 was read and 2 if d2 was read */
784 static int ren_until(struct wb
*wb
, char *d1
, char *d2
,
785 int (*next
)(void), void (*back
)(int))
789 while (c
>= 0 && c
!= '\n') {
791 ret
= ren_chardel(wb
, next
, back
, d1
, d2
);
801 static void wb_cpy(struct wb
*dst
, struct wb
*src
, int left
)
803 wb_hmov(dst
, left
- wb_wid(dst
));
807 void ren_tl(int (*next
)(void), void (*back
)(int))
815 charnext(delim
, next
, back
);
816 /* the left-adjusted string */
817 ren_until(&wb2
, delim
, NULL
, next
, back
);
818 wb_cpy(&wb
, &wb2
, 0);
819 /* the centered string */
820 ren_until(&wb2
, delim
, NULL
, next
, back
);
821 wb_cpy(&wb
, &wb2
, (n_lt
- wb_wid(&wb2
)) / 2);
822 /* the right-adjusted string */
823 ren_until(&wb2
, delim
, NULL
, next
, back
);
824 wb_cpy(&wb
, &wb2
, n_lt
- wb_wid(&wb2
));
825 /* flushing the line */
829 ren_bradj(adj
, 0, AD_L
, 0);
835 static void ren_field(struct wb
*wb
, int (*next
)(void), void (*back
)(int))
837 struct wb wbs
[NFIELDS
];
840 int left
, right
, cur_left
;
842 while (n
< LEN(wbs
)) {
844 if (ren_until(&wbs
[n
++], c_fb
, c_fa
, next
, back
) != 1)
847 left
= RENWB(wb
) ? f_hpos() : wb_wid(wb
);
848 right
= tab_next(left
);
849 for (i
= 0; i
< n
; i
++)
850 wid
+= wb_wid(&wbs
[i
]);
851 pad
= (right
- left
- wid
) / (n
> 1 ? n
- 1 : 1);
852 rem
= (right
- left
- wid
) % (n
> 1 ? n
- 1 : 1);
853 for (i
= 0; i
< n
; i
++) {
857 cur_left
= right
- wb_wid(&wbs
[i
]);
859 cur_left
= wb_wid(wb
) + pad
+ (i
+ rem
>= n
);
860 wb_cpy(wb
, &wbs
[i
], cur_left
);
865 static void ren_tab(struct wb
*wb
, char *tc
, int (*next
)(void), void (*back
)(int))
868 int pos
= RENWB(wb
) ? f_hpos() : wb_wid(wb
);
869 int ins
= tab_next(pos
); /* insertion position */
870 int typ
= tab_type(pos
); /* tab type */
873 if (typ
== 'R' || typ
== 'C') {
875 while (c
>= 0 && c
!= '\n' && c
!= '\t' && c
!= '\x01') {
877 ren_char(&t
, next
, back
);
883 ins
-= wb_wid(&t
) / 2;
886 if (!tc
[0] || ins
<= pos
)
887 wb_hmov(wb
, ins
- pos
);
889 ren_hline(wb
, ins
- pos
, tc
);
894 /* read characters from in.c and pass rendered lines to out.c */
897 struct wb
*wb
= &ren_wb
;
903 ren_first(); /* transition to the first page */
911 if (bp_final
== 0 && trap_em
>= 0) {
924 /* add wb (the current word) to cadj */
925 if (c
== ' ' || c
== '\n') {
926 adj_swid(cadj
, spacewid(n_f
, n_s
));
927 if (!wb_part(wb
)) { /* not after a \c */
929 fillreq
= ren_fillreq
;
937 while ((fillreq
&& !n_ce
&& n_u
) || adj_full(cadj
, !n_ce
&& n_u
)) {
941 if (c
== '\n' || ren_nl
) /* end or start of input line */
943 if (c
== '\n' && n_it
&& --n_itn
== 0)
945 if (c
== '\n' && !wb_part(wb
))
946 n_ce
= MAX(0, n_ce
- 1);
949 ren_char(wb
, ren_next
, ren_back
);
950 if (c
!= '\n' && wb_empty(wb
))
957 if (!adj_empty(cadj
, 0))
958 ren_page(bp_next
, 1);
966 #define tposval(i) (tpos[i] < 0 ? n_p + tpos[i] : tpos[i])
968 static int tpos
[NTRAPS
]; /* trap positions */
969 static int treg
[NTRAPS
]; /* trap registers */
972 static int trap_first(int pos
)
976 for (i
= 0; i
< ntraps
; i
++)
977 if (treg
[i
] >= 0 && tposval(i
) > pos
)
978 if (best
< 0 || tposval(i
) < tposval(best
))
983 static int trap_byreg(int reg
)
986 for (i
= 0; i
< ntraps
; i
++)
992 static int trap_bypos(int reg
, int pos
)
995 for (i
= 0; i
< ntraps
; i
++)
996 if (treg
[i
] >= 0 && tposval(i
) == pos
)
997 if (reg
== -1 || treg
[i
] == reg
)
1002 void tr_wh(char **args
)
1007 pos
= eval(args
[1], 'v');
1008 id
= trap_bypos(-1, pos
);
1016 id
= trap_byreg(-1);
1023 void tr_ch(char **args
)
1030 id
= trap_byreg(reg
);
1032 tpos
[id
] = args
[2] ? eval(args
[2], 'v') : -1;
1035 void tr_dt(char **args
)
1040 cdiv
->tpos
= eval(args
[1], 'v');
1041 cdiv
->treg
= map(args
[2]);
1047 void tr_em(char **args
)
1049 trap_em
= args
[1] ? map(args
[1]) : -1;
1052 static int trap_pos(int pos
)
1054 int ret
= trap_first(pos
);
1058 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->tpos
: -1;
1059 return ret
>= 0 ? tposval(ret
) : -1;
1062 static int trap_reg(int pos
)
1064 int ret
= trap_first(pos
);
1066 return cdiv
->treg
&& cdiv
->tpos
> pos
? cdiv
->treg
: -1;
1067 return ret
>= 0 ? treg
[ret
] : -1;
1070 int f_nexttrap(void)
1072 int pos
= trap_pos(n_d
);
1074 return pos
>= 0 ? pos
: 0x7fffffff;
1075 return (pos
>= 0 && pos
< n_p
? pos
: n_p
) - n_d
;