1 /* built-in troff requests */
8 static int tr_nl
= 1; /* just read a newline */
9 static int c_pc
= '%'; /* page number character */
14 /* skip everything until the end of line */
15 static void jmp_eol(void)
20 } while (c
>= 0 && c
!= '\n');
23 static void tr_vs(char **args
)
25 int vs
= args
[1] ? eval_re(args
[1], n_v
, 'p') : n_v0
;
30 static void tr_ls(char **args
)
32 int ls
= args
[1] ? eval_re(args
[1], n_L
, 0) : n_L0
;
37 static void tr_pl(char **args
)
39 int n
= eval_re(args
[1] ? args
[1] : "11i", n_p
, 'v');
43 static void tr_nr(char **args
)
49 num_set(id
, eval_re(args
[2], num_get(id
, 0), 'u'));
50 num_inc(id
, args
[3] ? eval(args
[3], 'u') : 0);
53 static void tr_rr(char **args
)
56 for (i
= 1; i
<= NARGS
; i
++)
58 num_del(map(args
[i
]));
61 static void tr_af(char **args
)
64 num_setfmt(map(args
[1]), args
[2]);
67 static void tr_ds(char **args
)
70 str_set(map(args
[1]), args
[2]);
73 static void tr_as(char **args
)
80 s1
= str_get(reg
) ? str_get(reg
) : "";
82 s
= xmalloc(strlen(s1
) + strlen(s2
) + 1);
89 static void tr_rm(char **args
)
92 for (i
= 1; i
<= NARGS
; i
++)
97 static void tr_rn(char **args
)
101 str_rn(map(args
[1]), map(args
[2]));
104 static void tr_po(char **args
)
106 int po
= args
[1] ? eval_re(args
[1], n_o
, 'm') : n_o0
;
111 static void read_regname(char *s
)
114 int n
= n_cp
? 2 : NMLEN
- 1;
115 while (c
== ' ' || c
== '\t')
117 while (c
>= 0 && c
!= ' ' && c
!= '\t' && c
!= '\n' && --n
>= 0) {
126 static void macrobody(struct sbuf
*sbuf
, char *end
)
133 while ((c
= cp_next()) >= 0) {
138 if ((c
= cp_next()) != '.') {
143 if ((n_cp
&& end
[0] == buf
[0] && end
[1] == buf
[1]) ||
150 for (i
= 0; buf
[i
]; i
++)
151 sbuf_add(sbuf
, (unsigned char) buf
[i
]);
158 static void tr_de(char **args
)
166 if (args
[0][1] == 'a' && args
[0][2] == 'm' && str_get(id
))
167 sbuf_append(&sbuf
, str_get(id
));
168 macrobody(&sbuf
, args
[2] ? args
[2] : ".");
169 str_set(id
, sbuf_buf(&sbuf
));
173 static void tr_ig(char **args
)
175 macrobody(NULL
, args
[1] ? args
[1] : ".");
178 /* read into sbuf until stop; if stop is NULL, stop at whitespace */
179 static int read_until(struct sbuf
*sbuf
, char *stop
,
180 int (*next
)(void), void (*back
)(int))
182 char cs
[GNLEN
], cs2
[GNLEN
];
184 while ((c
= next()) >= 0) {
188 if (!stop
&& (c
== ' ' || c
== '\t'))
190 charnext(cs
, next
, back
);
191 if (stop
&& !strcmp(stop
, cs
))
193 charnext_str(cs2
, cs
);
194 sbuf_append(sbuf
, cs2
);
199 /* evaluate .if strcmp (i.e. 'str'str') */
200 static int if_strcmp(int (*next
)(void), void (*back
)(int))
205 charnext(delim
, next
, back
);
208 read_until(&s1
, delim
, next
, back
);
209 read_until(&s2
, delim
, next
, back
);
210 ret
= !strcmp(sbuf_buf(&s1
), sbuf_buf(&s2
));
216 /* evaluate .if condition letters */
217 static int if_cond(int (*next
)(void), void (*back
)(int))
232 /* evaluate .if condition */
233 static int if_eval(int (*next
)(void), void (*back
)(int))
238 if (!read_until(&sbuf
, NULL
, next
, back
))
240 ret
= eval(sbuf_buf(&sbuf
), '\0') > 0;
245 static int eval_if(int (*next
)(void), void (*back
)(int))
252 } while (c
== ' ' || c
== '\t');
258 if (strchr("oetn", c
)) {
259 ret
= if_cond(next
, back
);
260 } else if (!isdigit(c
) && !strchr("-+*/%<=>&:.|()", c
)) {
261 ret
= if_strcmp(next
, back
);
263 ret
= if_eval(next
, back
);
268 static int ie_cond
[NIES
]; /* .ie condition stack */
271 static void tr_if(char **args
)
273 int c
= eval_if(cp_next
, cp_back
);
274 if (args
[0][1] == 'i' && args
[0][2] == 'e') /* .ie command */
276 ie_cond
[ie_depth
++] = c
;
280 static void tr_el(char **args
)
282 cp_blk(ie_depth
> 0 ? ie_cond
[--ie_depth
] : 1);
285 static void tr_na(char **args
)
290 static int adjmode(int c
, int def
)
306 static void tr_ad(char **args
)
315 n_j
= s
[0] == 'p' ? AD_P
| adjmode(s
[1], AD_B
) : adjmode(s
[0], n_j
);
318 static void tr_tm(char **args
)
320 fprintf(stderr
, "%s\n", args
[1]);
323 static void tr_so(char **args
)
329 static void tr_nx(char **args
)
334 static void tr_ex(char **args
)
339 static void tr_sy(char **args
)
344 static void tr_lt(char **args
)
346 int lt
= args
[1] ? eval_re(args
[1], n_lt
, 'm') : n_t0
;
351 static void tr_pc(char **args
)
353 c_pc
= args
[1] ? args
[1][0] : -1;
356 static int tl_next(void)
359 if (c
>= 0 && c
== c_pc
) {
360 in_push(num_str(map("%")), NULL
);
366 static void tr_tl(char **args
)
371 } while (c
>= 0 && (c
== ' ' || c
== '\t'));
373 ren_tl(tl_next
, cp_back
);
376 } while (c
>= 0 && c
!= '\n');
379 static void tr_ec(char **args
)
381 c_ec
= args
[1] ? args
[1][0] : '\\';
384 static void tr_cc(char **args
)
386 c_ec
= args
[1] ? args
[1][0] : '.';
389 static void tr_c2(char **args
)
391 c_ec
= args
[1] ? args
[1][0] : '\'';
394 static void tr_eo(char **args
)
399 static void tr_hc(char **args
)
402 if (!s
|| charread(&s
, c_hc
) < 0)
406 static void tr_nh(char **args
)
411 static void tr_hy(char **args
)
413 n_hy
= args
[1] ? atoi(args
[1]) : 1;
416 static void tr_hyp(char **args
)
418 n_hyp
= args
[1] ? atoi(args
[1]) : 1;
421 static void tr_lg(char **args
)
424 n_lg
= atoi(args
[1]);
427 static void tr_kn(char **args
)
430 n_kn
= atoi(args
[1]);
433 static void tr_cp(char **args
)
436 n_cp
= atoi(args
[1]);
439 static void tr_ss(char **args
)
442 n_ss
= eval_re(args
[1], n_ss
, 0);
443 n_sss
= args
[2] ? eval_re(args
[2], n_sss
, 0) : n_ss
;
447 static void tr_ssh(char **args
)
449 n_ssh
= args
[1] ? eval_re(args
[1], n_ssh
, 0) : 0;
452 static void tr_cs(char **args
)
456 font_setcs(dev_font(dev_pos(args
[1])), args
[2] ? eval(args
[2], 0) : 0);
459 static void tr_ff(char **args
)
465 fn
= dev_font(dev_pos(args
[1]));
466 for (i
= 2; i
<= NARGS
; i
++)
467 if (fn
&& args
[i
] && args
[i
][0] && args
[i
][1])
468 font_feat(fn
, args
[i
] + 1, args
[i
][0] == '+');
471 static void tr_nm(char **args
)
478 n_ln
= eval_re(args
[1], n_ln
, 0);
480 if (args
[2] && isdigit(args
[2][0]))
481 n_nM
= MAX(1, eval(args
[2], 0));
482 if (args
[3] && isdigit(args
[3][0]))
483 n_nS
= MAX(0, eval(args
[3], 0));
484 if (args
[4] && isdigit(args
[4][0]))
485 n_nI
= MAX(0, eval(args
[4], 0));
488 static void tr_nn(char **args
)
490 n_nn
= args
[1] ? eval(args
[1], 0) : 1;
493 static void tr_bd(char **args
)
495 if (!args
[1] || !strcmp("S", args
[1]))
497 font_setbd(dev_font(dev_pos(args
[1])), args
[2] ? eval(args
[2], 'u') : 0);
500 static void tr_it(char **args
)
504 n_itn
= eval(args
[1], 0);
510 static void tr_mc(char **args
)
513 if (s
&& charread(&s
, c_mc
) >= 0) {
515 n_mcn
= args
[2] ? eval(args
[2], 'm') : SC_EM
;
521 static void tr_tc(char **args
)
524 if (!s
|| charread(&s
, c_tc
) < 0)
528 static void tr_lc(char **args
)
531 if (!s
|| charread(&s
, c_lc
) < 0)
535 static void tr_lf(char **args
)
538 in_lf(args
[2], eval(args
[1], 0));
541 static void tr_chop(char **args
)
546 in_lf(args
[2], eval(args
[1], 0));
550 sbuf_append(&sbuf
, str_get(id
));
551 if (!sbuf_empty(&sbuf
)) {
552 sbuf_cut(&sbuf
, sbuf_len(&sbuf
) - 1);
553 str_set(id
, sbuf_buf(&sbuf
));
559 /* character translation (.tr) */
560 static struct dict cmap
; /* character mapping */
561 static char cmap_src
[NCMAPS
][GNLEN
]; /* source character */
562 static char cmap_dst
[NCMAPS
][GNLEN
]; /* character mapping */
563 static int cmap_n
; /* number of translated character */
565 void cmap_add(char *c1
, char *c2
)
567 int i
= dict_get(&cmap
, c1
);
569 strcpy(cmap_dst
[i
], c2
);
570 } else if (cmap_n
< NCMAPS
) {
571 strcpy(cmap_src
[cmap_n
], c1
);
572 strcpy(cmap_dst
[cmap_n
], c2
);
573 dict_put(&cmap
, cmap_src
[cmap_n
], cmap_n
);
578 char *cmap_map(char *c
)
580 int i
= dict_get(&cmap
, c
);
581 return i
>= 0 ? cmap_dst
[i
] : c
;
584 static void tr_tr(char **args
)
587 char c1
[GNLEN
], c2
[GNLEN
];
588 while (s
&& charread(&s
, c1
) >= 0) {
589 if (charread(&s
, c2
) < 0)
595 /* character definition (.char) */
596 static char cdef_src
[NCDEFS
][GNLEN
]; /* source character */
597 static char *cdef_dst
[NCDEFS
]; /* character definition */
598 static int cdef_fn
[NCDEFS
]; /* owning font */
599 static int cdef_n
; /* number of defined characters */
600 static int cdef_expanding
; /* inside cdef_expand() call */
602 static int cdef_find(char *c
, int fn
)
605 for (i
= 0; i
< cdef_n
; i
++)
606 if ((!cdef_fn
[i
] || cdef_fn
[i
] == fn
) && !strcmp(cdef_src
[i
], c
))
611 /* return the definition of the given character */
612 char *cdef_map(char *c
, int fn
)
614 int i
= cdef_find(c
, fn
);
615 return !cdef_expanding
&& i
>= 0 ? cdef_dst
[i
] : NULL
;
618 int cdef_expand(struct wb
*wb
, char *s
, int fn
)
620 char *d
= cdef_map(s
, fn
);
629 static void cdef_add(char *fn
, char *cs
, char *def
)
633 if (!def
|| charread(&cs
, c
) < 0)
635 i
= cdef_find(c
, -1);
636 if (i
< 0 && cdef_n
< NCDEFS
)
639 strncpy(cdef_src
[i
], c
, sizeof(cdef_src
[i
]) - 1);
640 cdef_dst
[i
] = xmalloc(strlen(def
) + 1);
641 strcpy(cdef_dst
[i
], def
);
642 cdef_fn
[i
] = fn
? dev_pos(fn
) : 0;
646 static void cdef_remove(char *cs
)
650 if (!cs
|| charread(&cs
, c
) < 0)
652 for (i
= 0; i
< cdef_n
; i
++) {
653 if (!strcmp(cdef_src
[i
], c
)) {
656 cdef_src
[i
][0] = '\0';
661 static void tr_char(char **args
)
663 cdef_add(NULL
, args
[1], args
[2]);
666 static void tr_rchar(char **args
)
669 for (i
= 1; i
<= NARGS
; i
++)
671 cdef_remove(args
[i
]);
674 static void tr_ochar(char **args
)
676 cdef_add(args
[1], args
[2], args
[3]);
679 static void tr_fmap(char **args
)
684 fn
= dev_font(dev_pos(args
[1]));
686 font_map(fn
, args
[2], args
[3] ? font_glyph(fn
, args
[3]) : NULL
);
689 static void arg_regname(struct sbuf
*sbuf
)
693 sbuf_append(sbuf
, reg
);
697 static void arg_string(struct sbuf
*sbuf
)
700 while ((c
= cp_next()) == ' ')
704 while (c
> 0 && c
!= '\n') {
713 static int mkargs_arg(struct sbuf
*sbuf
, int (*next
)(void), void (*back
)(int))
722 if (c
< 0 || c
== '\n')
728 while (c
>= 0 && c
!= '\n') {
729 if (!quoted
&& c
== ' ')
731 if (quoted
&& c
== '"') {
745 /* read macro arguments */
746 int tr_readargs(char **args
, struct sbuf
*sbuf
, int (*next
)(void), void (*back
)(int))
751 idx
[n
] = sbuf_len(sbuf
);
752 if (mkargs_arg(sbuf
, next
, back
))
756 for (i
= 0; i
< n
; i
++)
757 args
[i
] = sbuf_buf(sbuf
) + idx
[i
];
761 /* read macro arguments; trims tabs if rmtabs is nonzero */
762 static int mkargs(char **args
, struct sbuf
*sbuf
)
764 int n
= tr_readargs(args
, sbuf
, cp_next
, cp_back
);
769 /* read request arguments; trims tabs too */
770 static int mkargs_req(char **args
, struct sbuf
*sbuf
)
777 idx
[n
] = sbuf_len(sbuf
);
778 while (c
== ' ' || c
== '\t')
780 while (c
>= 0 && c
!= '\n' && c
!= ' ' && c
!= '\t') {
784 if (sbuf_len(sbuf
) > idx
[n
])
789 if (c
< 0 || c
== '\n')
792 for (i
= 0; i
< n
; i
++)
793 args
[i
] = sbuf_buf(sbuf
) + idx
[i
];
798 /* read arguments for .ds */
799 static int mkargs_ds(char **args
, struct sbuf
*sbuf
)
803 idx
[n
++] = sbuf_len(sbuf
);
805 idx
[n
++] = sbuf_len(sbuf
);
810 for (i
= 0; i
< n
; i
++)
811 args
[i
] = sbuf_buf(sbuf
) + idx
[i
];
815 /* read arguments for commands .nr that expect a register name */
816 static int mkargs_reg1(char **args
, struct sbuf
*sbuf
)
819 int idx0
= sbuf_len(sbuf
);
821 n
= mkargs_req(args
+ 1, sbuf
) + 1;
822 args
[0] = sbuf_buf(sbuf
) + idx0
;
826 /* do not read arguments; for .if, .ie and .el */
827 static int mkargs_null(char **args
, struct sbuf
*sbuf
)
832 /* read the whole line for .tm */
833 static int mkargs_eol(char **args
, struct sbuf
*sbuf
)
835 int idx0
= sbuf_len(sbuf
);
840 while (c
>= 0 && c
!= '\n') {
844 args
[0] = sbuf_buf(sbuf
) + idx0
;
850 void (*f
)(char **args
);
851 int (*args
)(char **args
, struct sbuf
*sbuf
);
853 {TR_DIVBEG
, tr_divbeg
},
854 {TR_DIVEND
, tr_divend
},
855 {TR_POPREN
, tr_popren
},
856 {"ab", tr_ab
, mkargs_eol
},
859 {"am", tr_de
, mkargs_reg1
},
860 {"as", tr_as
, mkargs_ds
},
869 {"char", tr_char
, mkargs_ds
},
870 {"chop", tr_chop
, mkargs_reg1
},
875 {"de", tr_de
, mkargs_reg1
},
877 {"ds", tr_ds
, mkargs_ds
},
880 {"el", tr_el
, mkargs_null
},
890 {"fspecial", tr_fspecial
},
899 {"ie", tr_if
, mkargs_null
},
900 {"if", tr_if
, mkargs_null
},
919 {"nr", tr_nr
, mkargs_reg1
},
928 {"rchar", tr_rchar
, mkargs_ds
},
939 {"sy", tr_sy
, mkargs_eol
},
943 {"tl", tr_tl
, mkargs_null
},
944 {"tm", tr_tm
, mkargs_eol
},
945 {"tr", tr_tr
, mkargs_eol
},
950 /* read the next troff request; return zero if a request was executed. */
953 char *args
[NARGS
+ 3] = {NULL
};
961 if (c
< 0 || (c
!= c_cc
&& c
!= c_c2
)) {
965 memset(args
, 0, sizeof(args
));
969 read_regname(cmd
+ 1);
971 req
= str_dget(map(cmd
+ 1));
974 req
->args(args
+ 1, &sbuf
);
976 mkargs_req(args
+ 1, &sbuf
);
980 mkargs(args
+ 1, &sbuf
);
982 if (str_get(map(cmd
+ 1)))
983 in_push(str_get(map(cmd
+ 1)), args
+ 1);
992 while (!tr_nextreq())
995 tr_nl
= c
== '\n' || c
< 0;
1002 for (i
= 0; i
< LEN(cmds
); i
++)
1003 str_dset(map(cmds
[i
].id
), &cmds
[i
]);
1004 dict_init(&cmap
, NCMAPS
, -1, 0, 0);