cp: handle conditional blocks with no spaces between .ie/.if and \{
[neatroff.git] / tr.c
blob8084dcdbc9b1ae29f100c1040580ef705ec76438
1 /* built-in troff requests */
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "roff.h"
8 static int tr_nl = 1; /* just read a newline */
9 static int c_pc = '%'; /* page number character */
10 int c_ec = '\\';
11 int c_cc = '.';
12 int c_c2 = '\'';
14 /* skip everything until the end of line */
15 static void jmp_eol(void)
17 int c;
18 do {
19 c = cp_next();
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;
26 n_v0 = n_v;
27 n_v = MAX(0, vs);
30 static void tr_ls(char **args)
32 int ls = args[1] ? eval_re(args[1], n_L, 0) : n_L0;
33 n_L0 = n_L;
34 n_L = MAX(1, ls);
37 static void tr_pl(char **args)
39 int n = eval_re(args[1] ? args[1] : "11i", n_p, 'v');
40 n_p = MAX(0, n);
43 static void tr_nr(char **args)
45 int id;
46 if (!args[2])
47 return;
48 id = map(args[1]);
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)
55 int i;
56 for (i = 1; i <= NARGS; i++)
57 if (args[i])
58 num_del(map(args[i]));
61 static void tr_af(char **args)
63 if (args[2])
64 num_setfmt(map(args[1]), args[2]);
67 static void tr_ds(char **args)
69 if (args[2])
70 str_set(map(args[1]), args[2]);
73 static void tr_as(char **args)
75 int reg;
76 char *s1, *s2, *s;
77 if (!args[2])
78 return;
79 reg = map(args[1]);
80 s1 = str_get(reg) ? str_get(reg) : "";
81 s2 = args[2];
82 s = xmalloc(strlen(s1) + strlen(s2) + 1);
83 strcpy(s, s1);
84 strcat(s, s2);
85 str_set(reg, s);
86 free(s);
89 static void tr_rm(char **args)
91 int i;
92 for (i = 1; i <= NARGS; i++)
93 if (args[i])
94 str_rm(map(args[i]));
97 static void tr_rn(char **args)
99 if (!args[2])
100 return;
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;
107 n_o0 = n_o;
108 n_o = MAX(0, po);
111 static void read_regname(char *s)
113 int c = cp_next();
114 int n = n_cp ? 2 : NMLEN - 1;
115 while (c == ' ' || c == '\t')
116 c = cp_next();
117 while (c >= 0 && c != ' ' && c != '\t' && c != '\n' && --n >= 0) {
118 *s++ = c;
119 c = cp_next();
121 if (c >= 0)
122 cp_back(c);
123 *s = '\0';
126 static void macrobody(struct sbuf *sbuf, char *end)
128 char buf[NMLEN];
129 int i, c;
130 int first = 1;
131 cp_back('\n');
132 cp_copymode(1);
133 while ((c = cp_next()) >= 0) {
134 if (sbuf && !first)
135 sbuf_add(sbuf, c);
136 first = 0;
137 if (c == '\n') {
138 if ((c = cp_next()) != '.') {
139 cp_back(c);
140 continue;
142 read_regname(buf);
143 if ((n_cp && end[0] == buf[0] && end[1] == buf[1]) ||
144 !strcmp(end, buf)) {
145 jmp_eol();
146 break;
148 if (sbuf) {
149 sbuf_add(sbuf, '.');
150 for (i = 0; buf[i]; i++)
151 sbuf_add(sbuf, (unsigned char) buf[i]);
155 cp_copymode(0);
158 static void tr_de(char **args)
160 struct sbuf sbuf;
161 int id;
162 if (!args[1])
163 return;
164 id = map(args[1]);
165 sbuf_init(&sbuf);
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));
170 sbuf_done(&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];
183 int c;
184 while ((c = next()) >= 0) {
185 back(c);
186 if (c == '\n')
187 return 1;
188 if (!stop && (c == ' ' || c == '\t'))
189 return 0;
190 charnext(cs, next, back);
191 if (stop && !strcmp(stop, cs))
192 return 0;
193 charnext_str(cs2, cs);
194 sbuf_append(sbuf, cs2);
196 return 1;
199 /* evaluate .if strcmp (i.e. 'str'str') */
200 static int if_strcmp(int (*next)(void), void (*back)(int))
202 char delim[GNLEN];
203 struct sbuf s1, s2;
204 int ret;
205 charnext(delim, next, back);
206 sbuf_init(&s1);
207 sbuf_init(&s2);
208 read_until(&s1, delim, next, back);
209 read_until(&s2, delim, next, back);
210 ret = !strcmp(sbuf_buf(&s1), sbuf_buf(&s2));
211 sbuf_done(&s1);
212 sbuf_done(&s2);
213 return ret;
216 /* evaluate .if condition letters */
217 static int if_cond(int (*next)(void), void (*back)(int))
219 switch (cp_next()) {
220 case 'o':
221 return n_pg % 2;
222 case 'e':
223 return !(n_pg % 2);
224 case 't':
225 return 1;
226 case 'n':
227 return 0;
229 return 0;
232 /* evaluate .if condition */
233 static int if_eval(int (*next)(void), void (*back)(int))
235 struct sbuf sbuf;
236 int ret;
237 sbuf_init(&sbuf);
238 if (!read_until(&sbuf, NULL, next, back))
239 back(' ');
240 ret = eval(sbuf_buf(&sbuf), '\0') > 0;
241 sbuf_done(&sbuf);
242 return ret;
245 static int eval_if(int (*next)(void), void (*back)(int))
247 int neg = 0;
248 int ret;
249 int c;
250 do {
251 c = next();
252 } while (c == ' ' || c == '\t');
253 if (c == '!') {
254 neg = 1;
255 c = next();
257 back(c);
258 if (strchr("oetn", c)) {
259 ret = if_cond(next, back);
260 } else if (!isdigit(c) && !strchr("-+*/%<=>&:.|()", c)) {
261 ret = if_strcmp(next, back);
262 } else {
263 ret = if_eval(next, back);
265 return ret != neg;
268 static int ie_cond[NIES]; /* .ie condition stack */
269 static int ie_depth;
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 */
275 if (ie_depth < NIES)
276 ie_cond[ie_depth++] = c;
277 cp_blk(!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)
287 n_na = 1;
290 static int adjmode(int c, int def)
292 switch (c) {
293 case 'l':
294 return AD_L;
295 case 'r':
296 return AD_R;
297 case 'c':
298 return AD_C;
299 case 'b':
300 case 'n':
301 return AD_B;
303 return def;
306 static void tr_ad(char **args)
308 char *s = args[1];
309 n_na = 0;
310 if (!s)
311 return;
312 if (isdigit(s[0]))
313 n_j = atoi(s) & 15;
314 else
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)
325 if (args[1])
326 in_so(args[1]);
329 static void tr_nx(char **args)
331 in_nx(args[1]);
334 static void tr_ex(char **args)
336 in_ex();
339 static void tr_sy(char **args)
341 system(args[1]);
344 static void tr_lt(char **args)
346 int lt = args[1] ? eval_re(args[1], n_lt, 'm') : n_t0;
347 n_t0 = n_t0;
348 n_lt = MAX(0, lt);
351 static void tr_pc(char **args)
353 c_pc = args[1] ? args[1][0] : -1;
356 static int tl_next(void)
358 int c = cp_next();
359 if (c >= 0 && c == c_pc) {
360 in_push(num_str(map("%")), NULL);
361 c = cp_next();
363 return c;
366 static void tr_tl(char **args)
368 int c;
369 do {
370 c = cp_next();
371 } while (c >= 0 && (c == ' ' || c == '\t'));
372 cp_back(c);
373 ren_tl(tl_next, cp_back);
374 do {
375 c = cp_next();
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)
396 c_ec = -1;
399 static void tr_hc(char **args)
401 char *s = args[1];
402 if (!s || charread(&s, c_hc) < 0)
403 strcpy(c_hc, "\\%");
406 static void tr_nh(char **args)
408 n_hy = 0;
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)
423 if (args[1])
424 n_lg = atoi(args[1]);
427 static void tr_kn(char **args)
429 if (args[1])
430 n_kn = atoi(args[1]);
433 static void tr_cp(char **args)
435 if (args[1])
436 n_cp = atoi(args[1]);
439 static void tr_ss(char **args)
441 if (args[1]) {
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)
454 if (!args[1])
455 return;
456 font_setcs(dev_font(dev_pos(args[1])), args[2] ? eval(args[2], 0) : 0);
459 static void tr_ff(char **args)
461 struct font *fn;
462 int i;
463 if (!args[2])
464 return;
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)
473 if (!args[1]) {
474 n_nm = 0;
475 return;
477 n_nm = 1;
478 n_ln = eval_re(args[1], n_ln, 0);
479 n_ln = MAX(0, n_ln);
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]))
496 return;
497 font_setbd(dev_font(dev_pos(args[1])), args[2] ? eval(args[2], 'u') : 0);
500 static void tr_it(char **args)
502 if (args[2]) {
503 n_it = map(args[2]);
504 n_itn = eval(args[1], 0);
505 } else {
506 n_it = 0;
510 static void tr_mc(char **args)
512 char *s = args[1];
513 if (s && charread(&s, c_mc) >= 0) {
514 n_mc = 1;
515 n_mcn = args[2] ? eval(args[2], 'm') : SC_EM;
516 } else {
517 n_mc = 0;
521 static void tr_tc(char **args)
523 char *s = args[1];
524 if (!s || charread(&s, c_tc) < 0)
525 strcpy(c_tc, "");
528 static void tr_lc(char **args)
530 char *s = args[1];
531 if (!s || charread(&s, c_lc) < 0)
532 strcpy(c_lc, "");
535 static void tr_lf(char **args)
537 if (args[1])
538 in_lf(args[2], eval(args[1], 0));
541 static void tr_chop(char **args)
543 struct sbuf sbuf;
544 int id;
545 if (args[1])
546 in_lf(args[2], eval(args[1], 0));
547 id = map(args[1]);
548 if (str_get(id)) {
549 sbuf_init(&sbuf);
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));
555 sbuf_done(&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);
568 if (i >= 0) {
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);
574 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)
586 char *s = args[1];
587 char c1[GNLEN], c2[GNLEN];
588 while (s && charread(&s, c1) >= 0) {
589 if (charread(&s, c2) < 0)
590 strcpy(c2, " ");
591 cmap_add(c1, c2);
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)
604 int i;
605 for (i = 0; i < cdef_n; i++)
606 if ((!cdef_fn[i] || cdef_fn[i] == fn) && !strcmp(cdef_src[i], c))
607 return i;
608 return -1;
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);
621 if (!d)
622 return 1;
623 cdef_expanding = 1;
624 ren_parse(wb, d);
625 cdef_expanding = 0;
626 return 0;
629 static void cdef_add(char *fn, char *cs, char *def)
631 char c[GNLEN];
632 int i;
633 if (!def || charread(&cs, c) < 0)
634 return;
635 i = cdef_find(c, -1);
636 if (i < 0 && cdef_n < NCDEFS)
637 i = cdef_n++;
638 if (i >= 0) {
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)
648 char c[GNLEN];
649 int i;
650 if (!cs || charread(&cs, c) < 0)
651 return;
652 for (i = 0; i < cdef_n; i++) {
653 if (!strcmp(cdef_src[i], c)) {
654 free(cdef_dst[i]);
655 cdef_dst[i] = NULL;
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)
668 int i;
669 for (i = 1; i <= NARGS; i++)
670 if (args[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)
681 struct font *fn;
682 if (!args[2])
683 return;
684 fn = dev_font(dev_pos(args[1]));
685 if (fn)
686 font_map(fn, args[2], args[3] ? font_glyph(fn, args[3]) : NULL);
689 static void arg_regname(struct sbuf *sbuf)
691 char reg[NMLEN];
692 read_regname(reg);
693 sbuf_append(sbuf, reg);
694 sbuf_add(sbuf, 0);
697 static void arg_string(struct sbuf *sbuf)
699 int c;
700 while ((c = cp_next()) == ' ')
702 if (c == '"')
703 c = cp_next();
704 while (c > 0 && c != '\n') {
705 sbuf_add(sbuf, c);
706 c = cp_next();
708 sbuf_add(sbuf, 0);
709 if (c >= 0)
710 cp_back(c);
713 static int mkargs_arg(struct sbuf *sbuf, int (*next)(void), void (*back)(int))
715 int quoted = 0;
716 int c;
717 c = next();
718 while (c == ' ')
719 c = next();
720 if (c == '\n')
721 back(c);
722 if (c < 0 || c == '\n')
723 return 1;
724 if (c == '"') {
725 quoted = 1;
726 c = next();
728 while (c >= 0 && c != '\n') {
729 if (!quoted && c == ' ')
730 break;
731 if (quoted && c == '"') {
732 c = next();
733 if (c != '"')
734 break;
736 sbuf_add(sbuf, c);
737 c = next();
739 sbuf_add(sbuf, 0);
740 if (c >= 0)
741 back(c);
742 return 0;
745 /* read macro arguments */
746 int tr_readargs(char **args, struct sbuf *sbuf, int (*next)(void), void (*back)(int))
748 int idx[NARGS];
749 int i, n = 0;
750 while (n < NARGS) {
751 idx[n] = sbuf_len(sbuf);
752 if (mkargs_arg(sbuf, next, back))
753 break;
754 n++;
756 for (i = 0; i < n; i++)
757 args[i] = sbuf_buf(sbuf) + idx[i];
758 return n;
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);
765 jmp_eol();
766 return n;
769 /* read request arguments; trims tabs too */
770 static int mkargs_req(char **args, struct sbuf *sbuf)
772 int idx[NARGS];
773 int i, n = 0;
774 int c;
775 c = cp_next();
776 while (n < NARGS) {
777 idx[n] = sbuf_len(sbuf);
778 while (c == ' ' || c == '\t')
779 c = cp_next();
780 while (c >= 0 && c != '\n' && c != ' ' && c != '\t') {
781 sbuf_add(sbuf, c);
782 c = cp_next();
784 if (sbuf_len(sbuf) > idx[n])
785 n++;
786 sbuf_add(sbuf, 0);
787 if (c == '\n')
788 cp_back(c);
789 if (c < 0 || c == '\n')
790 break;
792 for (i = 0; i < n; i++)
793 args[i] = sbuf_buf(sbuf) + idx[i];
794 jmp_eol();
795 return n;
798 /* read arguments for .ds */
799 static int mkargs_ds(char **args, struct sbuf *sbuf)
801 int idx[NARGS];
802 int i, n = 0;
803 idx[n++] = sbuf_len(sbuf);
804 arg_regname(sbuf);
805 idx[n++] = sbuf_len(sbuf);
806 cp_copymode(1);
807 arg_string(sbuf);
808 cp_copymode(0);
809 jmp_eol();
810 for (i = 0; i < n; i++)
811 args[i] = sbuf_buf(sbuf) + idx[i];
812 return n;
815 /* read arguments for commands .nr that expect a register name */
816 static int mkargs_reg1(char **args, struct sbuf *sbuf)
818 int n;
819 int idx0 = sbuf_len(sbuf);
820 arg_regname(sbuf);
821 n = mkargs_req(args + 1, sbuf) + 1;
822 args[0] = sbuf_buf(sbuf) + idx0;
823 return n;
826 /* do not read arguments; for .if, .ie and .el */
827 static int mkargs_null(char **args, struct sbuf *sbuf)
829 return 0;
832 /* read the whole line for .tm */
833 static int mkargs_eol(char **args, struct sbuf *sbuf)
835 int idx0 = sbuf_len(sbuf);
836 int c;
837 c = cp_next();
838 while (c == ' ')
839 c = cp_next();
840 while (c >= 0 && c != '\n') {
841 sbuf_add(sbuf, c);
842 c = cp_next();
844 args[0] = sbuf_buf(sbuf) + idx0;
845 return 1;
848 static struct cmd {
849 char *id;
850 void (*f)(char **args);
851 int (*args)(char **args, struct sbuf *sbuf);
852 } cmds[] = {
853 {TR_DIVBEG, tr_divbeg},
854 {TR_DIVEND, tr_divend},
855 {TR_POPREN, tr_popren},
856 {"ab", tr_ab, mkargs_eol},
857 {"ad", tr_ad},
858 {"af", tr_af},
859 {"am", tr_de, mkargs_reg1},
860 {"as", tr_as, mkargs_ds},
861 {"bd", tr_bd},
862 {"bp", tr_bp},
863 {"br", tr_br},
864 {"c2", tr_c2},
865 {"cc", tr_cc},
866 {"ochar", tr_ochar},
867 {"ce", tr_ce},
868 {"ch", tr_ch},
869 {"char", tr_char, mkargs_ds},
870 {"chop", tr_chop, mkargs_reg1},
871 {"cl", tr_cl},
872 {"cp", tr_cp},
873 {"cs", tr_cs},
874 {"da", tr_di},
875 {"de", tr_de, mkargs_reg1},
876 {"di", tr_di},
877 {"ds", tr_ds, mkargs_ds},
878 {"dt", tr_dt},
879 {"ec", tr_ec},
880 {"el", tr_el, mkargs_null},
881 {"em", tr_em},
882 {"eo", tr_eo},
883 {"ev", tr_ev},
884 {"ex", tr_ex},
885 {"fc", tr_fc},
886 {"ff", tr_ff},
887 {"fi", tr_fi},
888 {"fmap", tr_fmap},
889 {"fp", tr_fp},
890 {"fspecial", tr_fspecial},
891 {"ft", tr_ft},
892 {"hc", tr_hc},
893 {"hcode", tr_hcode},
894 {"hpf", tr_hpf},
895 {"hpfa", tr_hpfa},
896 {"hy", tr_hy},
897 {"hyp", tr_hyp},
898 {"hw", tr_hw},
899 {"ie", tr_if, mkargs_null},
900 {"if", tr_if, mkargs_null},
901 {"ig", tr_ig},
902 {"in", tr_in},
903 {"it", tr_it},
904 {"kn", tr_kn},
905 {"lc", tr_lc},
906 {"lf", tr_lf},
907 {"lg", tr_lg},
908 {"ll", tr_ll},
909 {"ls", tr_ls},
910 {"lt", tr_lt},
911 {"mc", tr_mc},
912 {"mk", tr_mk},
913 {"na", tr_na},
914 {"ne", tr_ne},
915 {"nf", tr_nf},
916 {"nh", tr_nh},
917 {"nm", tr_nm},
918 {"nn", tr_nn},
919 {"nr", tr_nr, mkargs_reg1},
920 {"ns", tr_ns},
921 {"nx", tr_nx},
922 {"os", tr_os},
923 {"pc", tr_pc},
924 {"pl", tr_pl},
925 {"pn", tr_pn},
926 {"po", tr_po},
927 {"ps", tr_ps},
928 {"rchar", tr_rchar, mkargs_ds},
929 {"rm", tr_rm},
930 {"rn", tr_rn},
931 {"rr", tr_rr},
932 {"rs", tr_rs},
933 {"rt", tr_rt},
934 {"so", tr_so},
935 {"sp", tr_sp},
936 {"ss", tr_ss},
937 {"ssh", tr_ssh},
938 {"sv", tr_sv},
939 {"sy", tr_sy, mkargs_eol},
940 {"ta", tr_ta},
941 {"tc", tr_tc},
942 {"ti", tr_ti},
943 {"tl", tr_tl, mkargs_null},
944 {"tm", tr_tm, mkargs_eol},
945 {"tr", tr_tr, mkargs_eol},
946 {"vs", tr_vs},
947 {"wh", tr_wh},
950 /* read the next troff request; return zero if a request was executed. */
951 int tr_nextreq(void)
953 char *args[NARGS + 3] = {NULL};
954 char cmd[RNLEN + 1];
955 struct cmd *req;
956 struct sbuf sbuf;
957 int c;
958 if (!tr_nl)
959 return 1;
960 c = cp_next();
961 if (c < 0 || (c != c_cc && c != c_c2)) {
962 cp_back(c);
963 return 1;
965 memset(args, 0, sizeof(args));
966 args[0] = cmd;
967 cmd[0] = c;
968 req = NULL;
969 cp_prefixblock(' ');
970 read_regname(cmd + 1);
971 sbuf_init(&sbuf);
972 req = str_dget(map(cmd + 1));
973 if (req) {
974 if (req->args)
975 req->args(args + 1, &sbuf);
976 else
977 mkargs_req(args + 1, &sbuf);
978 req->f(args);
979 } else {
980 cp_copymode(1);
981 mkargs(args + 1, &sbuf);
982 cp_copymode(0);
983 if (str_get(map(cmd + 1)))
984 in_push(str_get(map(cmd + 1)), args + 1);
986 sbuf_done(&sbuf);
987 return 0;
990 int tr_next(void)
992 int c;
993 while (!tr_nextreq())
995 c = cp_next();
996 tr_nl = c == '\n' || c < 0;
997 return c;
1000 void tr_init(void)
1002 int i;
1003 for (i = 0; i < LEN(cmds); i++)
1004 str_dset(map(cmds[i].id), &cmds[i]);
1005 dict_init(&cmap, NCMAPS, -1, 0, 0);