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
)
71 str_set(map(args
[1]), args
[2]);
74 static void tr_as(char **args
)
81 s1
= str_get(reg
) ? str_get(reg
) : "";
83 s
= xmalloc(strlen(s1
) + strlen(s2
) + 1);
90 static void tr_rm(char **args
)
93 for (i
= 1; i
<= NARGS
; i
++)
98 static void tr_rn(char **args
)
102 str_rn(map(args
[1]), map(args
[2]));
105 static void tr_po(char **args
)
107 int po
= args
[1] ? eval_re(args
[1], n_o
, 'm') : n_o0
;
112 /* read a string argument of a macro */
113 static char *read_string(void)
119 while ((c
= cp_next()) == ' ')
123 while (c
> 0 && c
!= '\n') {
131 return sbuf_out(&sbuf
);
134 /* read a register name argument; if two, read at most two characters */
135 static char *read_name(int two
)
141 while (c
== ' ' || c
== '\t' || c
== c_ni
)
143 while (c
> 0 && c
!= ' ' && c
!= '\t' && c
!= '\n' && (!two
|| i
< 2)) {
152 return sbuf_out(&sbuf
);
156 static void macrobody(struct sbuf
*sbuf
, char *end
)
163 while ((c
= cp_next()) >= 0) {
168 if ((c
= cp_next()) != c_cc
) {
172 req
= read_name(n_cp
);
173 if (!strcmp(end
, req
)) {
179 sbuf_add(sbuf
, c_cc
);
180 sbuf_append(sbuf
, req
);
190 static void tr_de(char **args
)
198 if (args
[0][1] == 'a' && args
[0][2] == 'm' && str_get(id
))
199 sbuf_append(&sbuf
, str_get(id
));
200 macrobody(&sbuf
, args
[2] ? args
[2] : ".");
201 str_set(id
, sbuf_buf(&sbuf
));
205 static void tr_ig(char **args
)
207 macrobody(NULL
, args
[1] ? args
[1] : ".");
210 /* read into sbuf until stop; if stop is NULL, stop at whitespace */
211 static int read_until(struct sbuf
*sbuf
, char *stop
,
212 int (*next
)(void), void (*back
)(int))
214 char cs
[GNLEN
], cs2
[GNLEN
];
216 while ((c
= next()) >= 0) {
222 if (!stop
&& (c
== ' ' || c
== '\t'))
224 charnext(cs
, next
, back
);
225 if (stop
&& !strcmp(stop
, cs
))
227 charnext_str(cs2
, cs
);
228 sbuf_append(sbuf
, cs2
);
233 /* evaluate .if strcmp (i.e. 'str'str') */
234 static int if_strcmp(int (*next
)(void), void (*back
)(int))
239 charnext(delim
, next
, back
);
242 read_until(&s1
, delim
, next
, back
);
243 read_until(&s2
, delim
, next
, back
);
245 ret
= !strcmp(sbuf_buf(&s1
), sbuf_buf(&s2
));
251 /* evaluate .if condition letters */
252 static int if_cond(int (*next
)(void), void (*back
)(int))
267 /* evaluate .if condition */
268 static int if_eval(int (*next
)(void), void (*back
)(int))
273 read_until(&sbuf
, NULL
, next
, back
);
274 ret
= eval(sbuf_buf(&sbuf
), '\0') > 0;
279 static int eval_if(int (*next
)(void), void (*back
)(int))
286 } while (c
== ' ' || c
== '\t');
292 if (strchr("oetn", c
)) {
293 ret
= if_cond(next
, back
);
294 } else if (!isdigit(c
) && !strchr("-+*/%<=>&:.|()", c
)) {
295 ret
= if_strcmp(next
, back
);
297 ret
= if_eval(next
, back
);
302 static int ie_cond
[NIES
]; /* .ie condition stack */
305 static void tr_if(char **args
)
307 int c
= eval_if(cp_next
, cp_back
);
308 if (args
[0][1] == 'i' && args
[0][2] == 'e') /* .ie command */
310 ie_cond
[ie_depth
++] = c
;
314 static void tr_el(char **args
)
316 cp_blk(ie_depth
> 0 ? ie_cond
[--ie_depth
] : 1);
319 static void tr_na(char **args
)
324 static int adjmode(int c
, int def
)
340 static void tr_ad(char **args
)
349 n_j
= s
[0] == 'p' ? AD_P
| adjmode(s
[1], AD_B
) : adjmode(s
[0], n_j
);
352 static void tr_tm(char **args
)
354 fprintf(stderr
, "%s\n", args
[1]);
357 static void tr_so(char **args
)
363 static void tr_nx(char **args
)
368 static void tr_ex(char **args
)
373 static void tr_sy(char **args
)
378 static void tr_lt(char **args
)
380 int lt
= args
[1] ? eval_re(args
[1], n_lt
, 'm') : n_t0
;
385 static void tr_pc(char **args
)
388 if (!s
|| charread(&s
, c_pc
) < 0)
392 static void tr_tl(char **args
)
397 } while (c
>= 0 && (c
== ' ' || c
== '\t'));
399 ren_tl(cp_next
, cp_back
);
402 } while (c
>= 0 && c
!= '\n');
405 static void tr_ec(char **args
)
407 c_ec
= args
[1] ? args
[1][0] : '\\';
410 static void tr_cc(char **args
)
412 c_cc
= args
[1] ? args
[1][0] : '.';
415 static void tr_c2(char **args
)
417 c_c2
= args
[1] ? args
[1][0] : '\'';
420 static void tr_eo(char **args
)
425 static void tr_hc(char **args
)
428 if (!s
|| charread(&s
, c_hc
) < 0)
432 /* sentence ending and their transparent characters */
433 static char eos_sent
[NCHARS
][GNLEN
] = { ".", "?", "!", };
434 static int eos_sentcnt
= 3;
435 static char eos_tran
[NCHARS
][GNLEN
] = { "'", "\"", ")", "]", "*", };
436 static int eos_trancnt
= 5;
438 static void tr_eos(char **args
)
444 while (s
&& charread(&s
, eos_sent
[eos_sentcnt
]) >= 0)
445 if (eos_sentcnt
< NCHARS
- 1)
450 while (s
&& charread(&s
, eos_tran
[eos_trancnt
]) >= 0)
451 if (eos_trancnt
< NCHARS
- 1)
456 int c_eossent(char *s
)
459 for (i
= 0; i
< eos_sentcnt
; i
++)
460 if (!strcmp(eos_sent
[i
], s
))
465 int c_eostran(char *s
)
468 for (i
= 0; i
< eos_trancnt
; i
++)
469 if (!strcmp(eos_tran
[i
], s
))
474 /* hyphenation dashes and hyphenation inhibiting character */
475 static char hy_dash
[NCHARS
][GNLEN
] = { "\\:", "-", "em", "en", "\\-", "--", "hy", };
476 static int hy_dashcnt
= 7;
477 static char hy_stop
[NCHARS
][GNLEN
] = { "\\%", };
478 static int hy_stopcnt
= 1;
480 static void tr_nh(char **args
)
485 static void tr_hy(char **args
)
487 n_hy
= args
[1] ? eval_re(args
[1], n_hy
, '\0') : 1;
490 static void tr_hlm(char **args
)
492 n_hlm
= args
[1] ? eval_re(args
[1], n_hlm
, '\0') : 0;
495 static void tr_hycost(char **args
)
497 n_hycost
= args
[1] ? eval_re(args
[1], n_hycost
, '\0') : 0;
498 n_hycost2
= args
[2] ? eval_re(args
[2], n_hycost2
, '\0') : 0;
499 n_hycost3
= args
[3] ? eval_re(args
[3], n_hycost3
, '\0') : 0;
502 static void tr_hydash(char **args
)
507 while (s
&& charread(&s
, hy_dash
[hy_dashcnt
]) >= 0)
508 if (hy_dashcnt
< NCHARS
- 1)
513 static void tr_hystop(char **args
)
518 while (s
&& charread(&s
, hy_stop
[hy_stopcnt
]) >= 0)
519 if (hy_stopcnt
< NCHARS
- 1)
524 int c_hydash(char *s
)
527 for (i
= 0; i
< hy_dashcnt
; i
++)
528 if (!strcmp(hy_dash
[i
], s
))
533 int c_hystop(char *s
)
536 for (i
= 0; i
< hy_stopcnt
; i
++)
537 if (!strcmp(hy_stop
[i
], s
))
542 int c_hymark(char *s
)
544 return !strcmp(c_bp
, s
) || !strcmp(c_hc
, s
);
547 static void tr_pmll(char **args
)
549 n_pmll
= args
[1] ? eval_re(args
[1], n_pmll
, '\0') : 0;
550 n_pmllcost
= args
[2] ? eval_re(args
[2], n_pmllcost
, '\0') : 100;
553 static void tr_lg(char **args
)
556 n_lg
= eval(args
[1], '\0');
559 static void tr_kn(char **args
)
562 n_kn
= eval(args
[1], '\0');
565 static void tr_cp(char **args
)
568 n_cp
= atoi(args
[1]);
571 static void tr_ss(char **args
)
574 n_ss
= eval_re(args
[1], n_ss
, 0);
575 n_sss
= args
[2] ? eval_re(args
[2], n_sss
, 0) : n_ss
;
579 static void tr_ssh(char **args
)
581 n_ssh
= args
[1] ? eval_re(args
[1], n_ssh
, 0) : 0;
584 static void tr_cs(char **args
)
586 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
588 font_setcs(fn
, args
[2] ? eval(args
[2], 0) : 0,
589 args
[3] ? eval(args
[3], 0) : 0);
592 static void tr_fzoom(char **args
)
594 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
596 font_setzoom(fn
, args
[2] ? eval(args
[2], 0) : 0);
599 static void tr_tkf(char **args
)
601 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
603 font_track(fn
, eval(args
[2], 0), eval(args
[3], 0),
604 eval(args
[4], 0), eval(args
[5], 0));
607 static void tr_ff(char **args
)
609 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
611 for (i
= 2; i
<= NARGS
; i
++)
612 if (fn
&& args
[i
] && args
[i
][0] && args
[i
][1])
613 font_feat(fn
, args
[i
] + 1, args
[i
][0] == '+');
616 static void tr_ffsc(char **args
)
618 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
620 font_scrp(fn
, args
[2] ? args
[2] : NULL
);
623 static void tr_nm(char **args
)
630 n_ln
= eval_re(args
[1], n_ln
, 0);
632 if (args
[2] && isdigit(args
[2][0]))
633 n_nM
= MAX(1, eval(args
[2], 0));
634 if (args
[3] && isdigit(args
[3][0]))
635 n_nS
= MAX(0, eval(args
[3], 0));
636 if (args
[4] && isdigit(args
[4][0]))
637 n_nI
= MAX(0, eval(args
[4], 0));
640 static void tr_nn(char **args
)
642 n_nn
= args
[1] ? eval(args
[1], 0) : 1;
645 static void tr_bd(char **args
)
647 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
648 if (!args
[1] || !strcmp("S", args
[1]))
651 font_setbd(fn
, args
[2] ? eval(args
[2], 'u') : 0);
654 static void tr_it(char **args
)
658 n_itn
= eval(args
[1], 0);
664 static void tr_mc(char **args
)
667 if (s
&& charread(&s
, c_mc
) >= 0) {
669 n_mcn
= args
[2] ? eval(args
[2], 'm') : SC_EM
;
675 static void tr_tc(char **args
)
678 if (!s
|| charread(&s
, c_tc
) < 0)
682 static void tr_lc(char **args
)
685 if (!s
|| charread(&s
, c_lc
) < 0)
689 static void tr_lf(char **args
)
692 in_lf(args
[2], eval(args
[1], 0));
695 static void tr_chop(char **args
)
702 sbuf_append(&sbuf
, str_get(id
));
703 if (!sbuf_empty(&sbuf
)) {
704 sbuf_cut(&sbuf
, sbuf_len(&sbuf
) - 1);
705 str_set(id
, sbuf_buf(&sbuf
));
711 /* character translation (.tr) */
712 static struct dict
*cmap
; /* character mapping */
713 static char cmap_src
[NCMAPS
][GNLEN
]; /* source character */
714 static char cmap_dst
[NCMAPS
][GNLEN
]; /* character mapping */
715 static int cmap_n
; /* number of translated character */
717 void cmap_add(char *c1
, char *c2
)
719 int i
= dict_get(cmap
, c1
);
721 strcpy(cmap_dst
[i
], c2
);
722 } else if (cmap_n
< NCMAPS
) {
723 strcpy(cmap_src
[cmap_n
], c1
);
724 strcpy(cmap_dst
[cmap_n
], c2
);
725 dict_put(cmap
, cmap_src
[cmap_n
], cmap_n
);
730 char *cmap_map(char *c
)
732 int i
= dict_get(cmap
, c
);
733 return i
>= 0 ? cmap_dst
[i
] : c
;
736 static void tr_tr(char **args
)
739 char c1
[GNLEN
], c2
[GNLEN
];
740 while (s
&& charread(&s
, c1
) >= 0) {
741 if (charread(&s
, c2
) < 0)
747 /* character definition (.char) */
748 static char cdef_src
[NCDEFS
][GNLEN
]; /* source character */
749 static char *cdef_dst
[NCDEFS
]; /* character definition */
750 static int cdef_fn
[NCDEFS
]; /* owning font */
751 static int cdef_n
; /* number of defined characters */
752 static int cdef_expanding
; /* inside cdef_expand() call */
754 static int cdef_find(char *c
, int fn
)
757 for (i
= 0; i
< cdef_n
; i
++)
758 if ((!cdef_fn
[i
] || cdef_fn
[i
] == fn
) && !strcmp(cdef_src
[i
], c
))
763 /* return the definition of the given character */
764 char *cdef_map(char *c
, int fn
)
766 int i
= cdef_find(c
, fn
);
767 return !cdef_expanding
&& i
>= 0 ? cdef_dst
[i
] : NULL
;
770 int cdef_expand(struct wb
*wb
, char *s
, int fn
)
772 char *d
= cdef_map(s
, fn
);
781 static void cdef_remove(char *fn
, char *cs
)
785 int fp
= fn
? dev_pos(fn
) : -1;
786 if (!cs
|| charread(&cs
, c
) < 0)
788 for (i
= 0; i
< cdef_n
; i
++) {
789 if (!strcmp(cdef_src
[i
], c
)) {
790 if (!fn
|| (fp
> 0 && cdef_fn
[i
] == fp
)) {
793 cdef_src
[i
][0] = '\0';
799 static void cdef_add(char *fn
, char *cs
, char *def
)
803 if (!def
|| charread(&cs
, c
) < 0)
805 i
= cdef_find(c
, fn
? dev_pos(fn
) : -1);
807 for (i
= 0; i
< cdef_n
; i
++)
810 if (i
== cdef_n
&& cdef_n
< NCDEFS
)
813 if (i
>= 0 && i
< cdef_n
) {
814 snprintf(cdef_src
[i
], sizeof(cdef_src
[i
]), "%s", c
);
815 cdef_dst
[i
] = xmalloc(strlen(def
) + 1);
816 strcpy(cdef_dst
[i
], def
);
817 cdef_fn
[i
] = fn
? dev_pos(fn
) : 0;
821 static void tr_rchar(char **args
)
824 for (i
= 1; i
<= NARGS
; i
++)
826 cdef_remove(NULL
, args
[i
]);
829 static void tr_char(char **args
)
832 cdef_add(NULL
, args
[1], args
[2]);
834 cdef_remove(NULL
, args
[1]);
837 static void tr_ochar(char **args
)
840 cdef_add(args
[1], args
[2], args
[3]);
842 cdef_remove(args
[1], args
[2]);
845 static void tr_fmap(char **args
)
847 struct font
*fn
= args
[1] ? dev_font(dev_pos(args
[1])) : NULL
;
849 font_map(fn
, args
[2], args
[3]);
852 static void tr_blm(char **args
)
854 tr_bm
= args
[1] ? map(args
[1]) : -1;
857 /* read a macro argument */
858 static int tr_arg(struct sbuf
*sbuf
, int brk
, int (*next
)(void), void (*back
)(int))
865 if (c
== '\n' || c
== brk
)
867 if (c
< 0 || c
== '\n' || c
== brk
)
873 while (c
>= 0 && c
!= '\n' && (quoted
|| c
!= brk
)) {
874 if (!quoted
&& c
== ' ')
876 if (quoted
&& c
== '"') {
894 /* read macro arguments; free the returned pointer when done */
895 char *tr_args(char **args
, int brk
, int (*next
)(void), void (*back
)(int))
901 while (!tr_arg(&sbuf
, brk
, next
, back
))
904 e
= s
+ sbuf_len(&sbuf
);
905 while (n
< NARGS
&& s
&& s
< e
) {
907 if ((s
= memchr(s
, '\0', e
- s
)))
910 return sbuf_out(&sbuf
);
913 /* split the arguments in sbuf */
914 static int tr_argschop(struct sbuf
*sbuf
, char **args
)
916 char *s
= sbuf_buf(sbuf
);
917 char *e
= s
+ sbuf_len(sbuf
);
919 while (n
< NARGS
&& s
&& s
< e
) {
921 if ((s
= memchr(s
, '\0', e
- s
)))
927 /* read request arguments; trims tabs too */
928 static void mkargs_req(struct sbuf
*sbuf
)
935 while (c
== ' ' || c
== '\t')
937 while (c
>= 0 && c
!= '\n' && c
!= ' ' && c
!= '\t') {
949 if (c
< 0 || c
== '\n')
955 /* read arguments for .ds */
956 static void mkargs_ds(struct sbuf
*sbuf
)
958 char *s
= read_name(n_cp
);
959 sbuf_append(sbuf
, s
);
963 sbuf_append(sbuf
, s
);
969 /* read arguments for .nr */
970 static void mkargs_reg1(struct sbuf
*sbuf
)
972 char *s
= read_name(n_cp
);
973 sbuf_append(sbuf
, s
);
979 /* do not read any arguments; for .if, .ie and .el */
980 static void mkargs_null(struct sbuf
*sbuf
)
984 /* read the whole line for .tm */
985 static void mkargs_eol(struct sbuf
*sbuf
)
992 while (c
>= 0 && c
!= '\n') {
1002 void (*f
)(char **args
);
1003 void (*args
)(struct sbuf
*sbuf
);
1005 {TR_DIVBEG
, tr_divbeg
},
1006 {TR_DIVEND
, tr_divend
},
1007 {TR_DIVVS
, tr_divvs
},
1008 {TR_POPREN
, tr_popren
},
1009 {"ab", tr_ab
, mkargs_eol
},
1012 {"am", tr_de
, mkargs_reg1
},
1013 {"as", tr_as
, mkargs_ds
},
1020 {"ochar", tr_ochar
},
1024 {"chop", tr_chop
, mkargs_reg1
},
1029 {"de", tr_de
, mkargs_reg1
},
1031 {"ds", tr_ds
, mkargs_ds
},
1034 {"el", tr_el
, mkargs_null
},
1047 {"fspecial", tr_fspecial
},
1049 {"fzoom", tr_fzoom
},
1051 {"hcode", tr_hcode
},
1056 {"hycost", tr_hycost
},
1057 {"hydash", tr_hydash
},
1058 {"hystop", tr_hystop
},
1060 {"ie", tr_if
, mkargs_null
},
1061 {"if", tr_if
, mkargs_null
},
1080 {"nr", tr_nr
, mkargs_reg1
},
1090 {"rchar", tr_rchar
},
1101 {"sy", tr_sy
, mkargs_eol
},
1106 {"tl", tr_tl
, mkargs_null
},
1107 {"tm", tr_tm
, mkargs_eol
},
1108 {"tr", tr_tr
, mkargs_eol
},
1113 static char *dotted(char *name
, int dot
)
1115 char *out
= xmalloc(strlen(name
) + 2);
1117 strcpy(out
+ 1, name
);
1121 /* read the next troff request; return zero if a request was executed. */
1122 int tr_nextreq(void)
1124 char *args
[NARGS
+ 3] = {NULL
};
1140 tr_argschop(&sbuf
, args
+ 1);
1141 tr_transparent(args
);
1147 if (c
< 0 || (c
!= c_cc
&& c
!= c_c2
&& (c
!= '\n' || tr_bm
< 0))) {
1153 cmd
= read_name(n_cp
);
1154 } else { /* blank line macro */
1155 cmd
= malloc(strlen(map_name(tr_bm
)) + 1);
1156 strcpy(cmd
, map_name(tr_bm
));
1159 args
[0] = dotted(cmd
, c
);
1160 req
= str_dget(map(cmd
));
1167 tr_argschop(&sbuf
, args
+ 1);
1173 buf
= tr_args(args
+ 1, -1, cp_next
, cp_back
);
1176 if (str_get(map(cmd
)))
1177 in_push(str_get(map(cmd
)), args
+ 1);
1188 while (!tr_nextreq())
1191 tr_nl
= c
== '\n' || c
< 0;
1198 for (i
= 0; i
< LEN(cmds
); i
++)
1199 str_dset(map(cmds
[i
].id
), &cmds
[i
]);
1200 cmap
= dict_make(-1, 0, 0);
1206 for (i
= 0; i
< cdef_n
; i
++)