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
= malloc(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 char *arg_regname(char *s
, int len
);
113 static void macrobody(struct sbuf
*sbuf
, char *end
)
119 cp_wid(0); /* copy-mode; disable \w handling */
120 while ((c
= cp_next()) >= 0) {
127 arg_regname(buf
, sizeof(buf
));
128 if ((n_cp
&& end
[0] == buf
[0] && end
[1] == buf
[1]) ||
136 for (i
= 0; buf
[i
]; i
++)
137 sbuf_add(sbuf
, (unsigned char) buf
[i
]);
147 static void tr_de(char **args
)
155 if (args
[0][1] == 'a' && args
[0][2] == 'm' && str_get(id
))
156 sbuf_append(&sbuf
, str_get(id
));
157 macrobody(&sbuf
, args
[2] ? args
[2] : ".");
158 str_set(id
, sbuf_buf(&sbuf
));
162 static void tr_ig(char **args
)
164 macrobody(NULL
, args
[1] ? args
[1] : ".");
167 /* read into sbuf until stop; if stop is NULL, stop at whitespace */
168 static int read_until(struct sbuf
*sbuf
, char *stop
)
170 char cs
[GNLEN
], cs2
[GNLEN
];
172 while ((c
= cp_next()) >= 0) {
176 if (!stop
&& (c
== ' ' || c
== '\t'))
178 charnext(cs
, cp_next
, cp_back
);
179 if (stop
&& !strcmp(stop
, cs
))
181 charnext_str(cs2
, cs
);
182 sbuf_append(sbuf
, cs2
);
187 /* evaluate .if strcmp (i.e. 'str'str') */
188 static int if_strcmp(void)
193 charnext(delim
, cp_next
, cp_back
);
196 read_until(&s1
, delim
);
197 read_until(&s2
, delim
);
198 ret
= !strcmp(sbuf_buf(&s1
), sbuf_buf(&s2
));
204 /* evaluate .if condition letters */
205 static int if_cond(void)
220 /* evaluate .if condition */
221 static int if_eval(void)
226 if (!read_until(&sbuf
, NULL
))
228 ret
= eval(sbuf_buf(&sbuf
), '\0') > 0;
233 static int ie_cond
[NIES
]; /* .ie condition stack */
236 static void tr_if(char **args
)
243 } while (c
== ' ' || c
== '\t');
249 if (strchr("oetn", c
)) {
251 } else if (!isdigit(c
) && !strchr("-+*/%<=>&:.|()", c
)) {
256 if (args
[0][1] == 'i' && args
[0][2] == 'e') /* .ie command */
258 ie_cond
[ie_depth
++] = ret
!= neg
;
262 static void tr_el(char **args
)
264 cp_blk(ie_depth
> 0 ? ie_cond
[--ie_depth
] : 1);
267 static void tr_na(char **args
)
272 static int adjmode(int c
, int def
)
288 static void tr_ad(char **args
)
297 n_j
= s
[0] == 'p' ? AD_P
| adjmode(s
[1], AD_B
) : adjmode(s
[0], n_j
);
300 static void tr_tm(char **args
)
302 fprintf(stderr
, "%s\n", args
[1]);
305 static void tr_so(char **args
)
311 static void tr_nx(char **args
)
316 static void tr_ex(char **args
)
321 static void tr_sy(char **args
)
326 static void tr_lt(char **args
)
328 int lt
= args
[1] ? eval_re(args
[1], n_lt
, 'm') : n_t0
;
333 static void tr_pc(char **args
)
335 c_pc
= args
[1] ? args
[1][0] : -1;
338 static int tl_next(void)
341 if (c
>= 0 && c
== c_pc
) {
342 in_push(num_str(map("%")), NULL
);
348 static void tr_tl(char **args
)
353 } while (c
>= 0 && (c
== ' ' || c
== '\t'));
355 ren_tl(tl_next
, cp_back
);
358 } while (c
>= 0 && c
!= '\n');
361 static void tr_ec(char **args
)
363 c_ec
= args
[1] ? args
[1][0] : '\\';
366 static void tr_cc(char **args
)
368 c_ec
= args
[1] ? args
[1][0] : '.';
371 static void tr_c2(char **args
)
373 c_ec
= args
[1] ? args
[1][0] : '\'';
376 static void tr_eo(char **args
)
381 static void tr_hc(char **args
)
384 if (!s
|| charread(&s
, c_hc
) < 0)
388 static void tr_nh(char **args
)
393 static void tr_hy(char **args
)
395 n_hy
= args
[1] ? atoi(args
[1]) : 1;
398 static void tr_hyp(char **args
)
400 n_hyp
= args
[1] ? atoi(args
[1]) : 1;
403 static void tr_lg(char **args
)
406 n_lg
= atoi(args
[1]);
409 static void tr_kn(char **args
)
412 n_kn
= atoi(args
[1]);
415 static void tr_cp(char **args
)
418 n_cp
= atoi(args
[1]);
421 static void tr_ss(char **args
)
424 n_ss
= eval_re(args
[1], n_ss
, 0);
427 static void tr_cs(char **args
)
431 dev_setcs(dev_pos(args
[1]), args
[2] ? eval(args
[2], 0) : 0);
434 static void tr_nm(char **args
)
441 n_ln
= eval_re(args
[1], n_ln
, 0);
443 if (args
[2] && isdigit(args
[2][0]))
444 n_nM
= MAX(1, eval(args
[2], 0));
445 if (args
[3] && isdigit(args
[3][0]))
446 n_nS
= MAX(0, eval(args
[3], 0));
447 if (args
[4] && isdigit(args
[4][0]))
448 n_nI
= MAX(0, eval(args
[4], 0));
451 static void tr_nn(char **args
)
453 n_nn
= args
[1] ? eval(args
[1], 0) : 1;
456 static void tr_bd(char **args
)
458 if (!args
[1] || !strcmp("S", args
[1]))
460 dev_setbd(dev_pos(args
[1]), args
[2] ? eval(args
[2], 'u') : 0);
463 static void tr_it(char **args
)
467 n_itn
= eval(args
[1], 0);
473 static void tr_mc(char **args
)
476 if (s
&& charread(&s
, c_mc
) >= 0) {
478 n_mcn
= args
[2] ? eval(args
[2], 'm') : SC_EM
;
484 static void tr_tc(char **args
)
487 if (!s
|| charread(&s
, c_tc
) < 0)
491 static void tr_lc(char **args
)
494 if (!s
|| charread(&s
, c_lc
) < 0)
498 static void tr_lf(char **args
)
501 in_lf(args
[2], eval(args
[1], 0));
504 static void tr_chop(char **args
)
509 in_lf(args
[2], eval(args
[1], 0));
513 sbuf_append(&sbuf
, str_get(id
));
514 if (!sbuf_empty(&sbuf
)) {
515 sbuf_cut(&sbuf
, sbuf_len(&sbuf
) - 1);
516 str_set(id
, sbuf_buf(&sbuf
));
522 /* character translation (.tr) */
523 static char cmap_src
[NCMAPS
][GNLEN
]; /* source character */
524 static char cmap_dst
[NCMAPS
][GNLEN
]; /* character mapping */
525 static int cmap_n
; /* number of translated character */
527 static int tr_find(char *c
)
530 for (i
= 0; i
< cmap_n
; i
++)
531 if (!strcmp(c
, cmap_src
[i
]))
536 void cmap_add(char *c1
, char *c2
)
539 if (i
< 0 && cmap_n
< NCMAPS
)
542 strcpy(cmap_src
[i
], c1
);
543 strcpy(cmap_dst
[i
], c2
);
547 char *cmap_map(char *c
)
550 return i
>= 0 ? cmap_dst
[i
] : c
;
553 static void tr_tr(char **args
)
556 char c1
[GNLEN
], c2
[GNLEN
];
557 while (s
&& charread(&s
, c1
) >= 0) {
558 if (charread(&s
, c2
) < 0)
564 /* character definition (.char) */
565 static char cdef_src
[NCDEFS
][GNLEN
]; /* source character */
566 static char *cdef_dst
[NCDEFS
]; /* character definition */
567 static int cdef_fn
[NCDEFS
]; /* owning font */
568 static int cdef_n
; /* number of defined characters */
569 static int cdef_expanding
; /* inside cdef_expand() call */
571 static int cdef_find(char *c
, int fn
)
574 for (i
= 0; i
< cdef_n
; i
++)
575 if (!strcmp(cdef_src
[i
], c
) && (!cdef_fn
[i
] || cdef_fn
[i
] == fn
))
580 /* return the definition of the given character */
581 char *cdef_map(char *c
, int fn
)
583 int i
= cdef_find(c
, fn
);
584 return !cdef_expanding
&& i
>= 0 ? cdef_dst
[i
] : NULL
;
587 int cdef_expand(struct wb
*wb
, char *s
, int fn
)
589 char *d
= cdef_map(s
, fn
);
598 static void cdef_add(char *fn
, char *cs
, char *def
)
602 if (!def
|| charread(&cs
, c
) < 0)
604 i
= cdef_find(c
, -1);
605 if (i
< 0 && cdef_n
< NCDEFS
)
608 strncpy(cdef_src
[i
], c
, sizeof(cdef_src
[i
]) - 1);
609 cdef_dst
[i
] = malloc(strlen(def
) + 1);
610 strcpy(cdef_dst
[i
], def
);
611 cdef_fn
[i
] = fn
? dev_pos(fn
) : 0;
615 static void cdef_remove(char *cs
)
619 if (!cs
|| charread(&cs
, c
) < 0)
621 for (i
= 0; i
< cdef_n
; i
++) {
622 if (!strcmp(cdef_src
[i
], c
)) {
625 cdef_src
[i
][0] = '\0';
630 static void tr_char(char **args
)
632 cdef_add(NULL
, args
[1], args
[2]);
635 static void tr_rchar(char **args
)
638 for (i
= 1; i
<= NARGS
; i
++)
640 cdef_remove(args
[i
]);
643 static void tr_ochar(char **args
)
645 cdef_add(args
[1], args
[2], args
[3]);
648 static void tr_fmap(char **args
)
653 fn
= dev_font(dev_pos(args
[1]));
655 font_map(fn
, args
[2], args
[3] ? font_glyph(fn
, args
[3]) : NULL
);
658 static char *arg_regname(char *s
, int len
)
660 char *e
= n_cp
? s
+ 2 : s
+ len
;
662 while (c
== ' ' || c
== '\t')
664 while (s
< e
&& c
>= 0 && c
!= ' ' && c
!= '\t' && c
!= '\n') {
674 static char *arg_normal(char *s
, int len
)
676 char *e
= s
+ len
- 1;
686 while (s
< e
&& c
> 0 && c
!= '\n') {
687 if (!quoted
&& c
== ' ')
689 if (quoted
&& c
== '"') {
703 static char *arg_string(char *s
, int len
)
705 char *e
= s
+ len
- 1;
707 while ((c
= cp_next()) == ' ')
711 while (s
< e
&& c
> 0 && c
!= '\n') {
721 /* read macro arguments; trims tabs if rmtabs is nonzero */
722 static int mkargs(char **args
, char *buf
, int len
)
725 char *e
= buf
+ len
- 1;
731 if (c
< 0 || c
== '\n')
734 s
= arg_normal(s
, e
- s
);
742 /* read request arguments; trims tabs too */
743 static int mkargs_req(char **args
, char *buf
, int len
)
746 char *e
= buf
+ len
- 1;
750 while (n
< NARGS
&& s
< e
) {
752 while (c
== ' ' || c
== '\t')
754 while (c
>= 0 && c
!= '\n' && c
!= ' ' && c
!= '\t' && s
< e
) {
761 if (c
< 0 || c
== '\n')
768 /* read arguments for .ds */
769 static int mkargs_ds(char **args
, char *buf
, int len
)
772 char *e
= buf
+ len
- 1;
775 s
= arg_regname(s
, e
- s
);
778 s
= arg_string(s
, e
- s
);
781 if (c
>= 0 && c
!= '\n')
786 /* read arguments for commands .nr that expect a register name */
787 static int mkargs_reg1(char **args
, char *buf
, int len
)
790 char *e
= buf
+ len
- 1;
792 s
= arg_regname(s
, e
- s
);
793 return mkargs_req(args
+ 1, s
, e
- s
) + 1;
796 /* do not read arguments; for .if, .ie and .el */
797 static int mkargs_null(char **args
, char *buf
, int len
)
802 /* read the whole line for .tm */
803 static int mkargs_eol(char **args
, char *buf
, int len
)
806 char *e
= buf
+ len
- 1;
812 while (s
< e
&& c
>= 0 && c
!= '\n') {
822 void (*f
)(char **args
);
823 int (*args
)(char **args
, char *buf
, int len
);
825 {TR_DIVBEG
, tr_divbeg
},
826 {TR_DIVEND
, tr_divend
},
827 {TR_POPREN
, tr_popren
},
828 {"ab", tr_ab
, mkargs_eol
},
831 {"am", tr_de
, mkargs_reg1
},
832 {"as", tr_as
, mkargs_ds
},
841 {"char", tr_char
, mkargs_ds
},
842 {"chop", tr_chop
, mkargs_reg1
},
847 {"de", tr_de
, mkargs_reg1
},
849 {"ds", tr_ds
, mkargs_ds
},
852 {"el", tr_el
, mkargs_null
},
861 {"fspecial", tr_fspecial
},
867 {"ie", tr_if
, mkargs_null
},
868 {"if", tr_if
, mkargs_null
},
887 {"nr", tr_nr
, mkargs_reg1
},
896 {"rchar", tr_rchar
, mkargs_ds
},
906 {"sy", tr_sy
, mkargs_eol
},
910 {"tl", tr_tl
, mkargs_null
},
911 {"tm", tr_tm
, mkargs_eol
},
912 {"tr", tr_tr
, mkargs_eol
},
917 /* read the next troff request; return zero if a request was executed. */
920 char *args
[NARGS
+ 3] = {NULL
};
928 if (c
< 0 || (c
!= c_cc
&& c
!= c_c2
)) {
932 memset(args
, 0, sizeof(args
));
936 arg_regname(cmd
+ 1, sizeof(cmd
) - 1);
937 req
= str_dget(map(cmd
+ 1));
940 req
->args(args
+ 1, buf
, sizeof(buf
));
942 mkargs_req(args
+ 1, buf
, sizeof(buf
));
946 mkargs(args
+ 1, buf
, sizeof(buf
));
948 if (str_get(map(cmd
+ 1)))
949 in_push(str_get(map(cmd
+ 1)), args
+ 1);
957 while (!tr_nextreq())
960 tr_nl
= c
== '\n' || c
< 0;
967 for (i
= 0; i
< LEN(cmds
); i
++)
968 str_dset(map(cmds
[i
].id
), &cmds
[i
]);