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
)
344 static void tr_ad(char **args
)
350 if (isdigit((unsigned char) s
[0]))
353 n_j
= s
[0] == 'p' ? AD_P
| adjmode(s
[1], AD_B
) : adjmode(s
[0], n_j
);
356 static void tr_tm(char **args
)
358 fprintf(stderr
, "%s\n", args
[1] ? args
[1] : "");
361 static void tr_so(char **args
)
367 static void tr_nx(char **args
)
372 static void tr_ex(char **args
)
377 static void tr_sy(char **args
)
382 static void tr_lt(char **args
)
384 int lt
= args
[1] ? eval_re(args
[1], n_lt
, 'm') : n_t0
;
389 static void tr_pc(char **args
)
392 if (!s
|| charread(&s
, c_pc
) < 0)
396 static void tr_tl(char **args
)
401 } while (c
>= 0 && (c
== ' ' || c
== '\t'));
403 ren_tl(cp_next
, cp_back
);
406 } while (c
>= 0 && c
!= '\n');
409 static void tr_ec(char **args
)
411 c_ec
= args
[1] ? args
[1][0] : '\\';
414 static void tr_cc(char **args
)
416 c_cc
= args
[1] ? args
[1][0] : '.';
419 static void tr_c2(char **args
)
421 c_c2
= args
[1] ? args
[1][0] : '\'';
424 static void tr_eo(char **args
)
429 static void tr_hc(char **args
)
432 if (!s
|| charread(&s
, c_hc
) < 0)
436 /* sentence ending and their transparent characters */
437 static char eos_sent
[NCHARS
][GNLEN
] = { ".", "?", "!", };
438 static int eos_sentcnt
= 3;
439 static char eos_tran
[NCHARS
][GNLEN
] = { "'", "\"", ")", "]", "*", };
440 static int eos_trancnt
= 5;
442 static void tr_eos(char **args
)
448 while (s
&& charread(&s
, eos_sent
[eos_sentcnt
]) >= 0)
449 if (eos_sentcnt
< NCHARS
- 1)
454 while (s
&& charread(&s
, eos_tran
[eos_trancnt
]) >= 0)
455 if (eos_trancnt
< NCHARS
- 1)
460 int c_eossent(char *s
)
463 for (i
= 0; i
< eos_sentcnt
; i
++)
464 if (!strcmp(eos_sent
[i
], s
))
469 int c_eostran(char *s
)
472 for (i
= 0; i
< eos_trancnt
; i
++)
473 if (!strcmp(eos_tran
[i
], s
))
478 /* hyphenation dashes and hyphenation inhibiting character */
479 static char hy_dash
[NCHARS
][GNLEN
] = { "\\:", "-", "em", "en", "\\-", "--", "hy", };
480 static int hy_dashcnt
= 7;
481 static char hy_stop
[NCHARS
][GNLEN
] = { "\\%", };
482 static int hy_stopcnt
= 1;
484 static void tr_nh(char **args
)
489 static void tr_hy(char **args
)
491 n_hy
= args
[1] ? eval_re(args
[1], n_hy
, '\0') : 1;
494 static void tr_hlm(char **args
)
496 n_hlm
= args
[1] ? eval_re(args
[1], n_hlm
, '\0') : 0;
499 static void tr_hycost(char **args
)
501 n_hycost
= args
[1] ? eval_re(args
[1], n_hycost
, '\0') : 0;
502 n_hycost2
= args
[2] ? eval_re(args
[2], n_hycost2
, '\0') : 0;
503 n_hycost3
= args
[3] ? eval_re(args
[3], n_hycost3
, '\0') : 0;
506 static void tr_hydash(char **args
)
511 while (s
&& charread(&s
, hy_dash
[hy_dashcnt
]) >= 0)
512 if (hy_dashcnt
< NCHARS
- 1)
517 static void tr_hystop(char **args
)
522 while (s
&& charread(&s
, hy_stop
[hy_stopcnt
]) >= 0)
523 if (hy_stopcnt
< NCHARS
- 1)
528 int c_hydash(char *s
)
531 for (i
= 0; i
< hy_dashcnt
; i
++)
532 if (!strcmp(hy_dash
[i
], s
))
537 int c_hystop(char *s
)
540 for (i
= 0; i
< hy_stopcnt
; i
++)
541 if (!strcmp(hy_stop
[i
], s
))
546 int c_hymark(char *s
)
548 return !strcmp(c_bp
, s
) || !strcmp(c_hc
, s
);
551 static void tr_pmll(char **args
)
553 n_pmll
= args
[1] ? eval_re(args
[1], n_pmll
, '\0') : 0;
554 n_pmllcost
= args
[2] ? eval_re(args
[2], n_pmllcost
, '\0') : 100;
557 static void tr_lg(char **args
)
560 n_lg
= eval(args
[1], '\0');
563 static void tr_kn(char **args
)
566 n_kn
= eval(args
[1], '\0');
569 static void tr_cp(char **args
)
572 n_cp
= atoi(args
[1]);
575 static void tr_ss(char **args
)
578 n_ss
= eval_re(args
[1], n_ss
, 0);
579 n_sss
= args
[2] ? eval_re(args
[2], n_sss
, 0) : n_ss
;
583 static void tr_ssh(char **args
)
585 n_ssh
= args
[1] ? eval_re(args
[1], n_ssh
, 0) : 0;
588 static void tr_cs(char **args
)
590 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
592 font_setcs(fn
, args
[2] ? eval(args
[2], 0) : 0,
593 args
[3] ? eval(args
[3], 0) : 0);
596 static void tr_fzoom(char **args
)
598 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
600 font_setzoom(fn
, args
[2] ? eval(args
[2], 0) : 0);
603 static void tr_tkf(char **args
)
605 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
607 font_track(fn
, eval(args
[2], 0), eval(args
[3], 0),
608 eval(args
[4], 0), eval(args
[5], 0));
611 static void tr_ff(char **args
)
613 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
615 for (i
= 2; i
<= NARGS
; i
++)
616 if (fn
&& args
[i
] && args
[i
][0] && args
[i
][1])
617 font_feat(fn
, args
[i
] + 1, args
[i
][0] == '+');
620 static void tr_ffsc(char **args
)
622 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
624 font_scrp(fn
, args
[2] ? args
[2] : NULL
);
627 static void tr_nm(char **args
)
634 n_ln
= eval_re(args
[1], n_ln
, 0);
636 if (args
[2] && isdigit((unsigned char) args
[2][0]))
637 n_nM
= MAX(1, eval(args
[2], 0));
638 if (args
[3] && isdigit((unsigned char) args
[3][0]))
639 n_nS
= MAX(0, eval(args
[3], 0));
640 if (args
[4] && isdigit((unsigned char) args
[4][0]))
641 n_nI
= MAX(0, eval(args
[4], 0));
644 static void tr_nn(char **args
)
646 n_nn
= args
[1] ? eval(args
[1], 0) : 1;
649 static void tr_bd(char **args
)
651 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
652 if (!args
[1] || !strcmp("S", args
[1]))
655 font_setbd(fn
, args
[2] ? eval(args
[2], 'u') : 0);
658 static void tr_it(char **args
)
662 n_itn
= eval(args
[1], 0);
668 static void tr_mc(char **args
)
671 if (s
&& charread(&s
, c_mc
) >= 0) {
673 n_mcn
= args
[2] ? eval(args
[2], 'm') : SC_EM
;
679 static void tr_tc(char **args
)
682 if (!s
|| charread(&s
, c_tc
) < 0)
686 static void tr_lc(char **args
)
689 if (!s
|| charread(&s
, c_lc
) < 0)
693 static void tr_lf(char **args
)
696 in_lf(args
[2], eval(args
[1], 0));
699 static void tr_chop(char **args
)
706 sbuf_append(&sbuf
, str_get(id
));
707 if (!sbuf_empty(&sbuf
)) {
708 sbuf_cut(&sbuf
, sbuf_len(&sbuf
) - 1);
709 str_set(id
, sbuf_buf(&sbuf
));
715 /* character translation (.tr) */
716 static struct dict
*cmap
; /* character mapping */
717 static char cmap_src
[NCMAPS
][GNLEN
]; /* source character */
718 static char cmap_dst
[NCMAPS
][GNLEN
]; /* character mapping */
719 static int cmap_n
; /* number of translated character */
721 void cmap_add(char *c1
, char *c2
)
723 int i
= dict_get(cmap
, c1
);
725 strcpy(cmap_dst
[i
], c2
);
726 } else if (cmap_n
< NCMAPS
) {
727 strcpy(cmap_src
[cmap_n
], c1
);
728 strcpy(cmap_dst
[cmap_n
], c2
);
729 dict_put(cmap
, cmap_src
[cmap_n
], cmap_n
);
734 char *cmap_map(char *c
)
736 int i
= dict_get(cmap
, c
);
737 return i
>= 0 ? cmap_dst
[i
] : c
;
740 static void tr_tr(char **args
)
743 char c1
[GNLEN
], c2
[GNLEN
];
744 while (s
&& charread(&s
, c1
) >= 0) {
745 if (charread(&s
, c2
) < 0)
751 /* character definition (.char) */
752 static char cdef_src
[NCDEFS
][GNLEN
]; /* source character */
753 static char *cdef_dst
[NCDEFS
]; /* character definition */
754 static int cdef_fn
[NCDEFS
]; /* owning font */
755 static int cdef_n
; /* number of defined characters */
756 static int cdef_expanding
; /* inside cdef_expand() call */
758 static int cdef_find(char *c
, int fn
)
761 for (i
= 0; i
< cdef_n
; i
++)
762 if ((!cdef_fn
[i
] || cdef_fn
[i
] == fn
) && !strcmp(cdef_src
[i
], c
))
767 /* return the definition of the given character */
768 char *cdef_map(char *c
, int fn
)
770 int i
= cdef_find(c
, fn
);
771 return !cdef_expanding
&& i
>= 0 ? cdef_dst
[i
] : NULL
;
774 int cdef_expand(struct wb
*wb
, char *s
, int fn
)
776 char *d
= cdef_map(s
, fn
);
785 static void cdef_remove(char *fn
, char *cs
)
789 int fp
= fn
? dev_pos(fn
) : -1;
790 if (!cs
|| charread(&cs
, c
) < 0)
792 for (i
= 0; i
< cdef_n
; i
++) {
793 if (!strcmp(cdef_src
[i
], c
)) {
794 if (!fn
|| (fp
> 0 && cdef_fn
[i
] == fp
)) {
797 cdef_src
[i
][0] = '\0';
803 static void cdef_add(char *fn
, char *cs
, char *def
)
807 if (!def
|| charread(&cs
, c
) < 0)
809 i
= cdef_find(c
, fn
? dev_pos(fn
) : -1);
811 for (i
= 0; i
< cdef_n
; i
++)
814 if (i
== cdef_n
&& cdef_n
< NCDEFS
)
817 if (i
>= 0 && i
< cdef_n
) {
818 snprintf(cdef_src
[i
], sizeof(cdef_src
[i
]), "%s", c
);
819 cdef_dst
[i
] = xmalloc(strlen(def
) + 1);
820 strcpy(cdef_dst
[i
], def
);
821 cdef_fn
[i
] = fn
? dev_pos(fn
) : 0;
825 static void tr_rchar(char **args
)
828 for (i
= 1; i
<= NARGS
; i
++)
830 cdef_remove(NULL
, args
[i
]);
833 static void tr_char(char **args
)
836 cdef_add(NULL
, args
[1], args
[2]);
838 cdef_remove(NULL
, args
[1]);
841 static void tr_ochar(char **args
)
844 cdef_add(args
[1], args
[2], args
[3]);
846 cdef_remove(args
[1], args
[2]);
849 static void tr_fmap(char **args
)
851 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
853 font_map(fn
, args
[2], args
[3]);
856 static void tr_blm(char **args
)
858 tr_bm
= args
[1] ? map(args
[1]) : -1;
861 /* read a macro argument */
862 static int tr_arg(struct sbuf
*sbuf
, int brk
, int (*next
)(void), void (*back
)(int))
869 if (c
== '\n' || c
== brk
)
871 if (c
< 0 || c
== '\n' || c
== brk
)
877 while (c
>= 0 && c
!= '\n' && (quoted
|| c
!= brk
)) {
878 if (!quoted
&& c
== ' ')
880 if (quoted
&& c
== '"') {
898 /* read macro arguments; free the returned pointer when done */
899 char *tr_args(char **args
, int brk
, int (*next
)(void), void (*back
)(int))
905 while (!tr_arg(&sbuf
, brk
, next
, back
))
908 e
= s
+ sbuf_len(&sbuf
);
909 while (n
< NARGS
&& s
&& s
< e
) {
911 if ((s
= memchr(s
, '\0', e
- s
)))
914 return sbuf_out(&sbuf
);
917 /* split the arguments in sbuf */
918 static int tr_argschop(struct sbuf
*sbuf
, char **args
)
920 char *s
= sbuf_buf(sbuf
);
921 char *e
= s
+ sbuf_len(sbuf
);
923 while (n
< NARGS
&& s
&& s
< e
) {
925 if ((s
= memchr(s
, '\0', e
- s
)))
931 /* read request arguments; trims tabs too */
932 static void mkargs_req(struct sbuf
*sbuf
)
939 while (c
== ' ' || c
== '\t')
941 while (c
>= 0 && c
!= '\n' && c
!= ' ' && c
!= '\t') {
953 if (c
< 0 || c
== '\n')
959 /* read arguments for .ds and .char */
960 static void mkargs_ds(struct sbuf
*sbuf
)
962 char *s
= read_name(n_cp
);
963 sbuf_append(sbuf
, s
);
968 sbuf_append(sbuf
, s
);
975 /* read arguments for .ochar */
976 static void mkargs_ochar(struct sbuf
*sbuf
)
978 char *s
= read_name(0);
979 sbuf_append(sbuf
, s
);
985 /* read arguments for .nr */
986 static void mkargs_reg1(struct sbuf
*sbuf
)
988 char *s
= read_name(n_cp
);
989 sbuf_append(sbuf
, s
);
995 /* do not read any arguments; for .if, .ie and .el */
996 static void mkargs_null(struct sbuf
*sbuf
)
1000 /* read the whole line for .tm */
1001 static void mkargs_eol(struct sbuf
*sbuf
)
1008 while (c
>= 0 && c
!= '\n') {
1018 void (*f
)(char **args
);
1019 void (*args
)(struct sbuf
*sbuf
);
1021 {TR_DIVBEG
, tr_divbeg
},
1022 {TR_DIVEND
, tr_divend
},
1023 {TR_DIVVS
, tr_divvs
},
1024 {TR_POPREN
, tr_popren
},
1027 {"ab", tr_ab
, mkargs_eol
},
1030 {"am", tr_de
, mkargs_reg1
},
1031 {"as", tr_as
, mkargs_ds
},
1038 {"ochar", tr_ochar
, mkargs_ochar
},
1041 {"char", tr_char
, mkargs_ds
},
1042 {"chop", tr_chop
, mkargs_reg1
},
1047 {"de", tr_de
, mkargs_reg1
},
1049 {"ds", tr_ds
, mkargs_ds
},
1052 {"el", tr_el
, mkargs_null
},
1065 {"fspecial", tr_fspecial
},
1067 {"fzoom", tr_fzoom
},
1069 {"hcode", tr_hcode
},
1074 {"hycost", tr_hycost
},
1075 {"hydash", tr_hydash
},
1076 {"hystop", tr_hystop
},
1078 {"ie", tr_if
, mkargs_null
},
1079 {"if", tr_if
, mkargs_null
},
1099 {"nr", tr_nr
, mkargs_reg1
},
1109 {"rchar", tr_rchar
},
1120 {"sy", tr_sy
, mkargs_eol
},
1126 {"tl", tr_tl
, mkargs_null
},
1127 {"tm", tr_tm
, mkargs_eol
},
1128 {"tr", tr_tr
, mkargs_eol
},
1133 static char *dotted(char *name
, int dot
)
1135 char *out
= xmalloc(strlen(name
) + 2);
1137 strcpy(out
+ 1, name
);
1141 /* execute a built-in request */
1142 void tr_req(int reg
, char **args
)
1144 struct cmd
*req
= str_dget(reg
);
1149 /* read the next troff request; return zero if a request was executed. */
1150 int tr_nextreq(void)
1152 char *args
[NARGS
+ 3] = {NULL
};
1168 tr_argschop(&sbuf
, args
+ 1);
1169 tr_transparent(args
);
1175 if (c
< 0 || (c
!= c_cc
&& c
!= c_c2
&& (c
!= '\n' || tr_bm
< 0))) {
1181 cmd
= read_name(n_cp
);
1182 } else { /* blank line macro */
1183 cmd
= malloc(strlen(map_name(tr_bm
)) + 1);
1184 strcpy(cmd
, map_name(tr_bm
));
1187 args
[0] = dotted(cmd
, c
);
1188 req
= str_dget(map(cmd
));
1195 tr_argschop(&sbuf
, args
+ 1);
1201 buf
= tr_args(args
+ 1, -1, cp_next
, cp_back
);
1204 if (str_get(map(cmd
)))
1205 in_push(str_get(map(cmd
)), args
+ 1);
1216 while (!tr_nextreq())
1219 tr_nl
= c
== '\n' || c
< 0;
1226 for (i
= 0; i
< LEN(cmds
); i
++)
1227 str_dset(map(cmds
[i
].id
), &cmds
[i
]);
1228 cmap
= dict_make(-1, 0, 0);
1234 for (i
= 0; i
< cdef_n
; i
++)