1 /* built-in troff requests */
8 static int tr_nl
= 1; /* just read a newline */
9 static int tr_bm
= -1; /* blank line macro */
10 char c_pc
[GNLEN
] = "%"; /* page number character */
11 int c_ec
= '\\'; /* escape character */
12 int c_cc
= '.'; /* control character */
13 int c_c2
= '\''; /* no-break control character */
15 /* skip everything until the end of line */
16 static void jmp_eol(void)
21 } while (c
>= 0 && c
!= '\n');
24 static void tr_vs(char **args
)
26 int vs
= args
[1] ? eval_re(args
[1], n_v
, 'p') : n_v0
;
31 static void tr_ls(char **args
)
33 int ls
= args
[1] ? eval_re(args
[1], n_L
, 0) : n_L0
;
38 static void tr_pl(char **args
)
40 int n
= eval_re(args
[1] ? args
[1] : "11i", n_p
, 'v');
44 static void tr_nr(char **args
)
50 num_set(id
, eval_re(args
[2], num_get(id
), 'u'));
51 num_setinc(id
, args
[3] ? eval(args
[3], 'u') : 0);
54 static void tr_rr(char **args
)
57 for (i
= 1; i
<= NARGS
; i
++)
59 num_del(map(args
[i
]));
62 static void tr_af(char **args
)
65 num_setfmt(map(args
[1]), args
[2]);
68 static void tr_ds(char **args
)
70 str_set(map(args
[1]), args
[2] ? args
[2] : "");
73 static void tr_as(char **args
)
78 s1
= str_get(reg
) ? str_get(reg
) : "";
79 s2
= args
[2] ? args
[2] : "";
80 s
= xmalloc(strlen(s1
) + strlen(s2
) + 1);
87 static void tr_rm(char **args
)
90 for (i
= 1; i
<= NARGS
; i
++)
95 static void tr_rn(char **args
)
99 str_rn(map(args
[1]), map(args
[2]));
102 static void tr_po(char **args
)
104 int po
= args
[1] ? eval_re(args
[1], n_o
, 'm') : n_o0
;
109 /* read a string argument of a macro */
110 static char *read_string(void)
117 while ((c
= cp_next()) == ' ')
119 empty
= c
<= 0 || c
== '\n';
122 while (c
> 0 && c
!= '\n') {
134 return sbuf_out(&sbuf
);
137 /* read a space separated macro argument; if two, read at most two characters */
138 static char *read_name(int two
)
144 while (c
== ' ' || c
== '\t' || c
== c_ni
)
146 while (c
> 0 && c
!= ' ' && c
!= '\t' && c
!= '\n' && (!two
|| i
< 2)) {
155 return sbuf_out(&sbuf
);
158 static void macrobody(struct sbuf
*sbuf
, char *end
)
165 while ((c
= cp_next()) >= 0) {
170 if ((c
= cp_next()) != c_cc
) {
174 req
= read_name(n_cp
);
175 if (!strcmp(end
, req
)) {
181 sbuf_add(sbuf
, c_cc
);
182 sbuf_append(sbuf
, req
);
192 static void tr_de(char **args
)
200 if (args
[0][1] == 'a' && args
[0][2] == 'm' && str_get(id
))
201 sbuf_append(&sbuf
, str_get(id
));
202 macrobody(&sbuf
, args
[2] ? args
[2] : ".");
203 str_set(id
, sbuf_buf(&sbuf
));
207 static void tr_ig(char **args
)
209 macrobody(NULL
, args
[1] ? args
[1] : ".");
212 /* read into sbuf until stop; if stop is NULL, stop at whitespace */
213 static int read_until(struct sbuf
*sbuf
, char *stop
,
214 int (*next
)(void), void (*back
)(int))
216 char cs
[GNLEN
], cs2
[GNLEN
];
218 while ((c
= next()) >= 0) {
224 if (!stop
&& (c
== ' ' || c
== '\t'))
226 charnext(cs
, next
, back
);
227 if (stop
&& !strcmp(stop
, cs
))
229 charnext_str(cs2
, cs
);
230 sbuf_append(sbuf
, cs2
);
235 /* evaluate .if strcmp (i.e. 'str'str') */
236 static int if_strcmp(int (*next
)(void), void (*back
)(int))
241 charnext(delim
, next
, back
);
244 read_until(&s1
, delim
, next
, back
);
245 read_until(&s2
, delim
, next
, back
);
247 ret
= !strcmp(sbuf_buf(&s1
), sbuf_buf(&s2
));
253 /* evaluate .if condition letters */
254 static int if_cond(int (*next
)(void), void (*back
)(int))
269 /* evaluate .if condition */
270 static int if_eval(int (*next
)(void), void (*back
)(int))
275 read_until(&sbuf
, NULL
, next
, back
);
276 ret
= eval(sbuf_buf(&sbuf
), '\0') > 0;
281 static int eval_if(int (*next
)(void), void (*back
)(int))
288 } while (c
== ' ' || c
== '\t');
294 if (strchr("oetn", c
)) {
295 ret
= if_cond(next
, back
);
296 } else if (!isdigit(c
) && !strchr("-+*/%<=>&:.|()", c
)) {
297 ret
= if_strcmp(next
, back
);
299 ret
= if_eval(next
, back
);
304 static int ie_cond
[NIES
]; /* .ie condition stack */
307 static void tr_if(char **args
)
309 int c
= eval_if(cp_next
, cp_back
);
310 if (args
[0][1] == 'i' && args
[0][2] == 'e') /* .ie command */
312 ie_cond
[ie_depth
++] = c
;
316 static void tr_el(char **args
)
318 cp_blk(ie_depth
> 0 ? ie_cond
[--ie_depth
] : 1);
321 static void tr_na(char **args
)
326 static int adjmode(int c
, int def
)
342 static void tr_ad(char **args
)
348 if (isdigit((unsigned char) s
[0]))
351 n_j
= s
[0] == 'p' ? AD_P
| adjmode(s
[1], AD_B
) : adjmode(s
[0], n_j
);
354 static void tr_tm(char **args
)
356 fprintf(stderr
, "%s\n", args
[1] ? args
[1] : "");
359 static void tr_so(char **args
)
365 static void tr_nx(char **args
)
370 static void tr_ex(char **args
)
375 static void tr_sy(char **args
)
380 static void tr_lt(char **args
)
382 int lt
= args
[1] ? eval_re(args
[1], n_lt
, 'm') : n_t0
;
387 static void tr_pc(char **args
)
390 if (!s
|| charread(&s
, c_pc
) < 0)
394 static void tr_tl(char **args
)
399 } while (c
>= 0 && (c
== ' ' || c
== '\t'));
401 ren_tl(cp_next
, cp_back
);
404 } while (c
>= 0 && c
!= '\n');
407 static void tr_ec(char **args
)
409 c_ec
= args
[1] ? args
[1][0] : '\\';
412 static void tr_cc(char **args
)
414 c_cc
= args
[1] ? args
[1][0] : '.';
417 static void tr_c2(char **args
)
419 c_c2
= args
[1] ? args
[1][0] : '\'';
422 static void tr_eo(char **args
)
427 static void tr_hc(char **args
)
430 if (!s
|| charread(&s
, c_hc
) < 0)
434 /* sentence ending and their transparent characters */
435 static char eos_sent
[NCHARS
][GNLEN
] = { ".", "?", "!", };
436 static int eos_sentcnt
= 3;
437 static char eos_tran
[NCHARS
][GNLEN
] = { "'", "\"", ")", "]", "*", };
438 static int eos_trancnt
= 5;
440 static void tr_eos(char **args
)
446 while (s
&& charread(&s
, eos_sent
[eos_sentcnt
]) >= 0)
447 if (eos_sentcnt
< NCHARS
- 1)
452 while (s
&& charread(&s
, eos_tran
[eos_trancnt
]) >= 0)
453 if (eos_trancnt
< NCHARS
- 1)
458 int c_eossent(char *s
)
461 for (i
= 0; i
< eos_sentcnt
; i
++)
462 if (!strcmp(eos_sent
[i
], s
))
467 int c_eostran(char *s
)
470 for (i
= 0; i
< eos_trancnt
; i
++)
471 if (!strcmp(eos_tran
[i
], s
))
476 /* hyphenation dashes and hyphenation inhibiting character */
477 static char hy_dash
[NCHARS
][GNLEN
] = { "\\:", "-", "em", "en", "\\-", "--", "hy", };
478 static int hy_dashcnt
= 7;
479 static char hy_stop
[NCHARS
][GNLEN
] = { "\\%", };
480 static int hy_stopcnt
= 1;
482 static void tr_nh(char **args
)
487 static void tr_hy(char **args
)
489 n_hy
= args
[1] ? eval_re(args
[1], n_hy
, '\0') : 1;
492 static void tr_hlm(char **args
)
494 n_hlm
= args
[1] ? eval_re(args
[1], n_hlm
, '\0') : 0;
497 static void tr_hycost(char **args
)
499 n_hycost
= args
[1] ? eval_re(args
[1], n_hycost
, '\0') : 0;
500 n_hycost2
= args
[2] ? eval_re(args
[2], n_hycost2
, '\0') : 0;
501 n_hycost3
= args
[3] ? eval_re(args
[3], n_hycost3
, '\0') : 0;
504 static void tr_hydash(char **args
)
509 while (s
&& charread(&s
, hy_dash
[hy_dashcnt
]) >= 0)
510 if (hy_dashcnt
< NCHARS
- 1)
515 static void tr_hystop(char **args
)
520 while (s
&& charread(&s
, hy_stop
[hy_stopcnt
]) >= 0)
521 if (hy_stopcnt
< NCHARS
- 1)
526 int c_hydash(char *s
)
529 for (i
= 0; i
< hy_dashcnt
; i
++)
530 if (!strcmp(hy_dash
[i
], s
))
535 int c_hystop(char *s
)
538 for (i
= 0; i
< hy_stopcnt
; i
++)
539 if (!strcmp(hy_stop
[i
], s
))
544 int c_hymark(char *s
)
546 return !strcmp(c_bp
, s
) || !strcmp(c_hc
, s
);
549 static void tr_pmll(char **args
)
551 n_pmll
= args
[1] ? eval_re(args
[1], n_pmll
, '\0') : 0;
552 n_pmllcost
= args
[2] ? eval_re(args
[2], n_pmllcost
, '\0') : 100;
555 static void tr_lg(char **args
)
558 n_lg
= eval(args
[1], '\0');
561 static void tr_kn(char **args
)
564 n_kn
= eval(args
[1], '\0');
567 static void tr_cp(char **args
)
570 n_cp
= atoi(args
[1]);
573 static void tr_ss(char **args
)
576 n_ss
= eval_re(args
[1], n_ss
, 0);
577 n_sss
= args
[2] ? eval_re(args
[2], n_sss
, 0) : n_ss
;
581 static void tr_ssh(char **args
)
583 n_ssh
= args
[1] ? eval_re(args
[1], n_ssh
, 0) : 0;
586 static void tr_cs(char **args
)
588 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
590 font_setcs(fn
, args
[2] ? eval(args
[2], 0) : 0,
591 args
[3] ? eval(args
[3], 0) : 0);
594 static void tr_fzoom(char **args
)
596 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
598 font_setzoom(fn
, args
[2] ? eval(args
[2], 0) : 0);
601 static void tr_tkf(char **args
)
603 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
605 font_track(fn
, eval(args
[2], 0), eval(args
[3], 0),
606 eval(args
[4], 0), eval(args
[5], 0));
609 static void tr_ff(char **args
)
611 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
613 for (i
= 2; i
<= NARGS
; i
++)
614 if (fn
&& args
[i
] && args
[i
][0] && args
[i
][1])
615 font_feat(fn
, args
[i
] + 1, args
[i
][0] == '+');
618 static void tr_ffsc(char **args
)
620 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
622 font_scrp(fn
, args
[2] ? args
[2] : NULL
);
625 static void tr_nm(char **args
)
632 n_ln
= eval_re(args
[1], n_ln
, 0);
634 if (args
[2] && isdigit((unsigned char) args
[2][0]))
635 n_nM
= MAX(1, eval(args
[2], 0));
636 if (args
[3] && isdigit((unsigned char) args
[3][0]))
637 n_nS
= MAX(0, eval(args
[3], 0));
638 if (args
[4] && isdigit((unsigned char) args
[4][0]))
639 n_nI
= MAX(0, eval(args
[4], 0));
642 static void tr_nn(char **args
)
644 n_nn
= args
[1] ? eval(args
[1], 0) : 1;
647 static void tr_bd(char **args
)
649 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
650 if (!args
[1] || !strcmp("S", args
[1]))
653 font_setbd(fn
, args
[2] ? eval(args
[2], 'u') : 0);
656 static void tr_it(char **args
)
660 n_itn
= eval(args
[1], 0);
666 static void tr_mc(char **args
)
669 if (s
&& charread(&s
, c_mc
) >= 0) {
671 n_mcn
= args
[2] ? eval(args
[2], 'm') : SC_EM
;
677 static void tr_tc(char **args
)
680 if (!s
|| charread(&s
, c_tc
) < 0)
684 static void tr_lc(char **args
)
687 if (!s
|| charread(&s
, c_lc
) < 0)
691 static void tr_lf(char **args
)
694 in_lf(args
[2], eval(args
[1], 0));
697 static void tr_chop(char **args
)
704 sbuf_append(&sbuf
, str_get(id
));
705 if (!sbuf_empty(&sbuf
)) {
706 sbuf_cut(&sbuf
, sbuf_len(&sbuf
) - 1);
707 str_set(id
, sbuf_buf(&sbuf
));
713 /* character translation (.tr) */
714 static struct dict
*cmap
; /* character mapping */
715 static char cmap_src
[NCMAPS
][GNLEN
]; /* source character */
716 static char cmap_dst
[NCMAPS
][GNLEN
]; /* character mapping */
717 static int cmap_n
; /* number of translated character */
719 void cmap_add(char *c1
, char *c2
)
721 int i
= dict_get(cmap
, c1
);
723 strcpy(cmap_dst
[i
], c2
);
724 } else if (cmap_n
< NCMAPS
) {
725 strcpy(cmap_src
[cmap_n
], c1
);
726 strcpy(cmap_dst
[cmap_n
], c2
);
727 dict_put(cmap
, cmap_src
[cmap_n
], cmap_n
);
732 char *cmap_map(char *c
)
734 int i
= dict_get(cmap
, c
);
735 return i
>= 0 ? cmap_dst
[i
] : c
;
738 static void tr_tr(char **args
)
741 char c1
[GNLEN
], c2
[GNLEN
];
742 while (s
&& charread(&s
, c1
) >= 0) {
743 if (charread(&s
, c2
) < 0)
749 /* character definition (.char) */
750 static char cdef_src
[NCDEFS
][GNLEN
]; /* source character */
751 static char *cdef_dst
[NCDEFS
]; /* character definition */
752 static int cdef_fn
[NCDEFS
]; /* owning font */
753 static int cdef_n
; /* number of defined characters */
754 static int cdef_expanding
; /* inside cdef_expand() call */
756 static int cdef_find(char *c
, int fn
)
759 for (i
= 0; i
< cdef_n
; i
++)
760 if ((!cdef_fn
[i
] || cdef_fn
[i
] == fn
) && !strcmp(cdef_src
[i
], c
))
765 /* return the definition of the given character */
766 char *cdef_map(char *c
, int fn
)
768 int i
= cdef_find(c
, fn
);
769 return !cdef_expanding
&& i
>= 0 ? cdef_dst
[i
] : NULL
;
772 int cdef_expand(struct wb
*wb
, char *s
, int fn
)
774 char *d
= cdef_map(s
, fn
);
783 static void cdef_remove(char *fn
, char *cs
)
787 int fp
= fn
? dev_pos(fn
) : -1;
788 if (!cs
|| charread(&cs
, c
) < 0)
790 for (i
= 0; i
< cdef_n
; i
++) {
791 if (!strcmp(cdef_src
[i
], c
)) {
792 if (!fn
|| (fp
> 0 && cdef_fn
[i
] == fp
)) {
795 cdef_src
[i
][0] = '\0';
801 static void cdef_add(char *fn
, char *cs
, char *def
)
805 if (!def
|| charread(&cs
, c
) < 0)
807 i
= cdef_find(c
, fn
? dev_pos(fn
) : -1);
809 for (i
= 0; i
< cdef_n
; i
++)
812 if (i
== cdef_n
&& cdef_n
< NCDEFS
)
815 if (i
>= 0 && i
< cdef_n
) {
816 snprintf(cdef_src
[i
], sizeof(cdef_src
[i
]), "%s", c
);
817 cdef_dst
[i
] = xmalloc(strlen(def
) + 1);
818 strcpy(cdef_dst
[i
], def
);
819 cdef_fn
[i
] = fn
? dev_pos(fn
) : 0;
823 static void tr_rchar(char **args
)
826 for (i
= 1; i
<= NARGS
; i
++)
828 cdef_remove(NULL
, args
[i
]);
831 static void tr_char(char **args
)
834 cdef_add(NULL
, args
[1], args
[2]);
836 cdef_remove(NULL
, args
[1]);
839 static void tr_ochar(char **args
)
842 cdef_add(args
[1], args
[2], args
[3]);
844 cdef_remove(args
[1], args
[2]);
847 static void tr_fmap(char **args
)
849 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
851 font_map(fn
, args
[2], args
[3]);
854 static void tr_blm(char **args
)
856 tr_bm
= args
[1] ? map(args
[1]) : -1;
859 /* read a macro argument */
860 static int tr_arg(struct sbuf
*sbuf
, int brk
, int (*next
)(void), void (*back
)(int))
867 if (c
== '\n' || c
== brk
)
869 if (c
< 0 || c
== '\n' || c
== brk
)
875 while (c
>= 0 && c
!= '\n' && (quoted
|| c
!= brk
)) {
876 if (!quoted
&& c
== ' ')
878 if (quoted
&& c
== '"') {
896 /* read macro arguments; free the returned pointer when done */
897 char *tr_args(char **args
, int brk
, int (*next
)(void), void (*back
)(int))
903 while (!tr_arg(&sbuf
, brk
, next
, back
))
906 e
= s
+ sbuf_len(&sbuf
);
907 while (n
< NARGS
&& s
&& s
< e
) {
909 if ((s
= memchr(s
, '\0', e
- s
)))
912 return sbuf_out(&sbuf
);
915 /* split the arguments in sbuf */
916 static int tr_argschop(struct sbuf
*sbuf
, char **args
)
918 char *s
= sbuf_buf(sbuf
);
919 char *e
= s
+ sbuf_len(sbuf
);
921 while (n
< NARGS
&& s
&& s
< e
) {
923 if ((s
= memchr(s
, '\0', e
- s
)))
929 /* read request arguments; trims tabs too */
930 static void mkargs_req(struct sbuf
*sbuf
)
937 while (c
== ' ' || c
== '\t')
939 while (c
>= 0 && c
!= '\n' && c
!= ' ' && c
!= '\t') {
951 if (c
< 0 || c
== '\n')
957 /* read arguments for .ds and .char */
958 static void mkargs_ds(struct sbuf
*sbuf
)
960 char *s
= read_name(n_cp
);
961 sbuf_append(sbuf
, s
);
966 sbuf_append(sbuf
, s
);
973 /* read arguments for .ochar */
974 static void mkargs_ochar(struct sbuf
*sbuf
)
976 char *s
= read_name(0);
977 sbuf_append(sbuf
, s
);
983 /* read arguments for .nr */
984 static void mkargs_reg1(struct sbuf
*sbuf
)
986 char *s
= read_name(n_cp
);
987 sbuf_append(sbuf
, s
);
993 /* do not read any arguments; for .if, .ie and .el */
994 static void mkargs_null(struct sbuf
*sbuf
)
998 /* read the whole line for .tm */
999 static void mkargs_eol(struct sbuf
*sbuf
)
1006 while (c
>= 0 && c
!= '\n') {
1016 void (*f
)(char **args
);
1017 void (*args
)(struct sbuf
*sbuf
);
1019 {TR_DIVBEG
, tr_divbeg
},
1020 {TR_DIVEND
, tr_divend
},
1021 {TR_DIVVS
, tr_divvs
},
1022 {TR_POPREN
, tr_popren
},
1023 {"ab", tr_ab
, mkargs_eol
},
1026 {"am", tr_de
, mkargs_reg1
},
1027 {"as", tr_as
, mkargs_ds
},
1034 {"ochar", tr_ochar
, mkargs_ochar
},
1037 {"char", tr_char
, mkargs_ds
},
1038 {"chop", tr_chop
, mkargs_reg1
},
1043 {"de", tr_de
, mkargs_reg1
},
1045 {"ds", tr_ds
, mkargs_ds
},
1048 {"el", tr_el
, mkargs_null
},
1061 {"fspecial", tr_fspecial
},
1063 {"fzoom", tr_fzoom
},
1065 {"hcode", tr_hcode
},
1070 {"hycost", tr_hycost
},
1071 {"hydash", tr_hydash
},
1072 {"hystop", tr_hystop
},
1074 {"ie", tr_if
, mkargs_null
},
1075 {"if", tr_if
, mkargs_null
},
1094 {"nr", tr_nr
, mkargs_reg1
},
1104 {"rchar", tr_rchar
},
1115 {"sy", tr_sy
, mkargs_eol
},
1120 {"tl", tr_tl
, mkargs_null
},
1121 {"tm", tr_tm
, mkargs_eol
},
1122 {"tr", tr_tr
, mkargs_eol
},
1127 static char *dotted(char *name
, int dot
)
1129 char *out
= xmalloc(strlen(name
) + 2);
1131 strcpy(out
+ 1, name
);
1135 /* read the next troff request; return zero if a request was executed. */
1136 int tr_nextreq(void)
1138 char *args
[NARGS
+ 3] = {NULL
};
1154 tr_argschop(&sbuf
, args
+ 1);
1155 tr_transparent(args
);
1161 if (c
< 0 || (c
!= c_cc
&& c
!= c_c2
&& (c
!= '\n' || tr_bm
< 0))) {
1167 cmd
= read_name(n_cp
);
1168 } else { /* blank line macro */
1169 cmd
= malloc(strlen(map_name(tr_bm
)) + 1);
1170 strcpy(cmd
, map_name(tr_bm
));
1173 args
[0] = dotted(cmd
, c
);
1174 req
= str_dget(map(cmd
));
1181 tr_argschop(&sbuf
, args
+ 1);
1187 buf
= tr_args(args
+ 1, -1, cp_next
, cp_back
);
1190 if (str_get(map(cmd
)))
1191 in_push(str_get(map(cmd
)), args
+ 1);
1202 while (!tr_nextreq())
1205 tr_nl
= c
== '\n' || c
< 0;
1212 for (i
= 0; i
< LEN(cmds
); i
++)
1213 str_dset(map(cmds
[i
].id
), &cmds
[i
]);
1214 cmap
= dict_make(-1, 0, 0);
1220 for (i
= 0; i
< cdef_n
; i
++)