draw: extend polygons to include arcs or splines
[neatroff.git] / tr.c
blob1b39613154847f404ecdbf14046fed7ef3c3a0c6
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 char c_pc[GNLEN] = "%"; /* page number character */
10 int c_ec = '\\'; /* escape character */
11 int c_cc = '.'; /* control character */
12 int c_c2 = '\''; /* no-break control character */
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), 'u'));
50 num_setinc(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 /* read a string argument of a macro */
112 static char *read_string(void)
114 struct sbuf sbuf;
115 int c;
116 sbuf_init(&sbuf);
117 cp_copymode(1);
118 while ((c = cp_next()) == ' ')
120 if (c == '"')
121 c = cp_next();
122 while (c > 0 && c != '\n') {
123 if (c != c_ni)
124 sbuf_add(&sbuf, c);
125 c = cp_next();
127 if (c >= 0)
128 cp_back(c);
129 cp_copymode(0);
130 return sbuf_out(&sbuf);
133 /* read a register name argument; if two, read at most two characters */
134 static char *read_name(int two)
136 struct sbuf sbuf;
137 int c = cp_next();
138 int i = 0;
139 sbuf_init(&sbuf);
140 while (c == ' ' || c == '\t' || c == c_ni)
141 c = cp_next();
142 while (c > 0 && c != ' ' && c != '\t' && c != '\n' && (!two || i < 2)) {
143 if (c != c_ni) {
144 sbuf_add(&sbuf, c);
145 i++;
147 c = cp_next();
149 if (c >= 0)
150 cp_back(c);
151 return sbuf_out(&sbuf);
155 static void macrobody(struct sbuf *sbuf, char *end)
157 int first = 1;
158 int c;
159 char *req = NULL;
160 cp_back('\n');
161 cp_copymode(1);
162 while ((c = cp_next()) >= 0) {
163 if (sbuf && !first)
164 sbuf_add(sbuf, c);
165 first = 0;
166 if (c == '\n') {
167 if ((c = cp_next()) != c_cc) {
168 cp_back(c);
169 continue;
171 req = read_name(n_cp);
172 if (!strcmp(end, req)) {
173 in_push(end, NULL);
174 cp_back(c_cc);
175 break;
177 if (sbuf) {
178 sbuf_add(sbuf, c_cc);
179 sbuf_append(sbuf, req);
181 free(req);
182 req = NULL;
185 free(req);
186 cp_copymode(0);
189 static void tr_de(char **args)
191 struct sbuf sbuf;
192 int id;
193 if (!args[1])
194 return;
195 id = map(args[1]);
196 sbuf_init(&sbuf);
197 if (args[0][1] == 'a' && args[0][2] == 'm' && str_get(id))
198 sbuf_append(&sbuf, str_get(id));
199 macrobody(&sbuf, args[2] ? args[2] : ".");
200 str_set(id, sbuf_buf(&sbuf));
201 sbuf_done(&sbuf);
204 static void tr_ig(char **args)
206 macrobody(NULL, args[1] ? args[1] : ".");
209 /* read into sbuf until stop; if stop is NULL, stop at whitespace */
210 static int read_until(struct sbuf *sbuf, char *stop,
211 int (*next)(void), void (*back)(int))
213 char cs[GNLEN], cs2[GNLEN];
214 int c;
215 while ((c = next()) >= 0) {
216 if (c == c_ni)
217 continue;
218 back(c);
219 if (c == '\n')
220 return 1;
221 if (!stop && (c == ' ' || c == '\t'))
222 return 0;
223 charnext(cs, next, back);
224 if (stop && !strcmp(stop, cs))
225 return 0;
226 charnext_str(cs2, cs);
227 sbuf_append(sbuf, cs2);
229 return 1;
232 /* evaluate .if strcmp (i.e. 'str'str') */
233 static int if_strcmp(int (*next)(void), void (*back)(int))
235 char delim[GNLEN];
236 struct sbuf s1, s2;
237 int ret;
238 charnext(delim, next, back);
239 sbuf_init(&s1);
240 sbuf_init(&s2);
241 read_until(&s1, delim, next, back);
242 read_until(&s2, delim, next, back);
243 cp_reqbeg();
244 ret = !strcmp(sbuf_buf(&s1), sbuf_buf(&s2));
245 sbuf_done(&s1);
246 sbuf_done(&s2);
247 return ret;
250 /* evaluate .if condition letters */
251 static int if_cond(int (*next)(void), void (*back)(int))
253 switch (cp_next()) {
254 case 'o':
255 return n_pg % 2;
256 case 'e':
257 return !(n_pg % 2);
258 case 't':
259 return 1;
260 case 'n':
261 return 0;
263 return 0;
266 /* evaluate .if condition */
267 static int if_eval(int (*next)(void), void (*back)(int))
269 struct sbuf sbuf;
270 int ret;
271 sbuf_init(&sbuf);
272 read_until(&sbuf, NULL, next, back);
273 ret = eval(sbuf_buf(&sbuf), '\0') > 0;
274 sbuf_done(&sbuf);
275 return ret;
278 static int eval_if(int (*next)(void), void (*back)(int))
280 int neg = 0;
281 int ret;
282 int c;
283 do {
284 c = next();
285 } while (c == ' ' || c == '\t');
286 if (c == '!') {
287 neg = 1;
288 c = next();
290 back(c);
291 if (strchr("oetn", c)) {
292 ret = if_cond(next, back);
293 } else if (!isdigit(c) && !strchr("-+*/%<=>&:.|()", c)) {
294 ret = if_strcmp(next, back);
295 } else {
296 ret = if_eval(next, back);
298 return ret != neg;
301 static int ie_cond[NIES]; /* .ie condition stack */
302 static int ie_depth;
304 static void tr_if(char **args)
306 int c = eval_if(cp_next, cp_back);
307 if (args[0][1] == 'i' && args[0][2] == 'e') /* .ie command */
308 if (ie_depth < NIES)
309 ie_cond[ie_depth++] = c;
310 cp_blk(!c);
313 static void tr_el(char **args)
315 cp_blk(ie_depth > 0 ? ie_cond[--ie_depth] : 1);
318 static void tr_na(char **args)
320 n_na = 1;
323 static int adjmode(int c, int def)
325 switch (c) {
326 case 'l':
327 return AD_L;
328 case 'r':
329 return AD_R;
330 case 'c':
331 return AD_C;
332 case 'b':
333 case 'n':
334 return AD_B;
336 return def;
339 static void tr_ad(char **args)
341 char *s = args[1];
342 n_na = 0;
343 if (!s)
344 return;
345 if (isdigit(s[0]))
346 n_j = atoi(s) & 15;
347 else
348 n_j = s[0] == 'p' ? AD_P | adjmode(s[1], AD_B) : adjmode(s[0], n_j);
351 static void tr_tm(char **args)
353 fprintf(stderr, "%s\n", args[1]);
356 static void tr_so(char **args)
358 if (args[1])
359 in_so(args[1]);
362 static void tr_nx(char **args)
364 in_nx(args[1]);
367 static void tr_ex(char **args)
369 in_ex();
372 static void tr_sy(char **args)
374 system(args[1]);
377 static void tr_lt(char **args)
379 int lt = args[1] ? eval_re(args[1], n_lt, 'm') : n_t0;
380 n_t0 = n_t0;
381 n_lt = MAX(0, lt);
384 static void tr_pc(char **args)
386 char *s = args[1];
387 if (!s || charread(&s, c_pc) < 0)
388 strcpy(c_pc, "");
391 static void tr_tl(char **args)
393 int c;
394 do {
395 c = cp_next();
396 } while (c >= 0 && (c == ' ' || c == '\t'));
397 cp_back(c);
398 ren_tl(cp_next, cp_back);
399 do {
400 c = cp_next();
401 } while (c >= 0 && c != '\n');
404 static void tr_ec(char **args)
406 c_ec = args[1] ? args[1][0] : '\\';
409 static void tr_cc(char **args)
411 c_cc = args[1] ? args[1][0] : '.';
414 static void tr_c2(char **args)
416 c_c2 = args[1] ? args[1][0] : '\'';
419 static void tr_eo(char **args)
421 c_ec = -1;
424 static void tr_hc(char **args)
426 char *s = args[1];
427 if (!s || charread(&s, c_hc) < 0)
428 strcpy(c_hc, "\\%");
431 /* sentence ending and their transparent characters */
432 static char eos_sent[NCHARS][GNLEN] = { ".", "?", "!", };
433 static int eos_sentcnt = 3;
434 static char eos_tran[NCHARS][GNLEN] = { "'", "\"", ")", "]", "*", };
435 static int eos_trancnt = 5;
437 static void tr_eos(char **args)
439 eos_sentcnt = 0;
440 eos_trancnt = 0;
441 if (args[1]) {
442 char *s = args[1];
443 while (s && charread(&s, eos_sent[eos_sentcnt]) >= 0)
444 if (eos_sentcnt < NCHARS - 1)
445 eos_sentcnt++;
447 if (args[2]) {
448 char *s = args[2];
449 while (s && charread(&s, eos_tran[eos_trancnt]) >= 0)
450 if (eos_trancnt < NCHARS - 1)
451 eos_trancnt++;
455 int c_eossent(char *s)
457 int i;
458 for (i = 0; i < eos_sentcnt; i++)
459 if (!strcmp(eos_sent[i], s))
460 return 1;
461 return 0;
464 int c_eostran(char *s)
466 int i;
467 for (i = 0; i < eos_trancnt; i++)
468 if (!strcmp(eos_tran[i], s))
469 return 1;
470 return 0;
473 /* hyphenation dashes and hyphenation inhibiting character */
474 static char hy_dash[NCHARS][GNLEN] = { "\\:", "-", "em", "en", "\\-", "--", "hy", };
475 static int hy_dashcnt = 7;
476 static char hy_stop[NCHARS][GNLEN] = { "\\%", };
477 static int hy_stopcnt = 1;
479 static void tr_nh(char **args)
481 n_hy = 0;
484 static void tr_hy(char **args)
486 n_hy = args[1] ? eval_re(args[1], n_hy, '\0') : 1;
489 static void tr_hlm(char **args)
491 n_hlm = args[1] ? eval_re(args[1], n_hlm, '\0') : 0;
494 static void tr_hycost(char **args)
496 n_hycost = args[1] ? eval_re(args[1], n_hycost, '\0') : 0;
497 n_hycost2 = args[2] ? eval_re(args[2], n_hycost2, '\0') : 0;
498 n_hycost3 = args[3] ? eval_re(args[3], n_hycost3, '\0') : 0;
501 static void tr_hydash(char **args)
503 hy_dashcnt = 0;
504 if (args[1]) {
505 char *s = args[1];
506 while (s && charread(&s, hy_dash[hy_dashcnt]) >= 0)
507 if (hy_dashcnt < NCHARS - 1)
508 hy_dashcnt++;
512 static void tr_hystop(char **args)
514 hy_stopcnt = 0;
515 if (args[1]) {
516 char *s = args[1];
517 while (s && charread(&s, hy_stop[hy_stopcnt]) >= 0)
518 if (hy_stopcnt < NCHARS - 1)
519 hy_stopcnt++;
523 int c_hydash(char *s)
525 int i;
526 for (i = 0; i < hy_dashcnt; i++)
527 if (!strcmp(hy_dash[i], s))
528 return 1;
529 return 0;
532 int c_hystop(char *s)
534 int i;
535 for (i = 0; i < hy_stopcnt; i++)
536 if (!strcmp(hy_stop[i], s))
537 return 1;
538 return 0;
541 int c_hymark(char *s)
543 return !strcmp(c_bp, s) || !strcmp(c_hc, s);
546 static void tr_pmll(char **args)
548 n_pmll = args[1] ? eval_re(args[1], n_pmll, '\0') : 0;
549 n_pmllcost = args[2] ? eval_re(args[2], n_pmllcost, '\0') : 100;
552 static void tr_lg(char **args)
554 if (args[1])
555 n_lg = eval(args[1], '\0');
558 static void tr_kn(char **args)
560 if (args[1])
561 n_kn = eval(args[1], '\0');
564 static void tr_cp(char **args)
566 if (args[1])
567 n_cp = atoi(args[1]);
570 static void tr_ss(char **args)
572 if (args[1]) {
573 n_ss = eval_re(args[1], n_ss, 0);
574 n_sss = args[2] ? eval_re(args[2], n_sss, 0) : n_ss;
578 static void tr_ssh(char **args)
580 n_ssh = args[1] ? eval_re(args[1], n_ssh, 0) : 0;
583 static void tr_cs(char **args)
585 struct font *fn = args[1] ? dev_font(dev_pos(args[1])) : NULL;
586 if (fn)
587 font_setcs(fn, args[2] ? eval(args[2], 0) : 0,
588 args[3] ? eval(args[3], 0) : 0);
591 static void tr_fzoom(char **args)
593 struct font *fn = args[1] ? dev_font(dev_pos(args[1])) : NULL;
594 if (fn)
595 font_setzoom(fn, args[2] ? eval(args[2], 0) : 0);
598 static void tr_ff(char **args)
600 struct font *fn = args[1] ? dev_font(dev_pos(args[1])) : NULL;
601 int i;
602 for (i = 2; i <= NARGS; i++)
603 if (fn && args[i] && args[i][0] && args[i][1])
604 font_feat(fn, args[i] + 1, args[i][0] == '+');
607 static void tr_nm(char **args)
609 if (!args[1]) {
610 n_nm = 0;
611 return;
613 n_nm = 1;
614 n_ln = eval_re(args[1], n_ln, 0);
615 n_ln = MAX(0, n_ln);
616 if (args[2] && isdigit(args[2][0]))
617 n_nM = MAX(1, eval(args[2], 0));
618 if (args[3] && isdigit(args[3][0]))
619 n_nS = MAX(0, eval(args[3], 0));
620 if (args[4] && isdigit(args[4][0]))
621 n_nI = MAX(0, eval(args[4], 0));
624 static void tr_nn(char **args)
626 n_nn = args[1] ? eval(args[1], 0) : 1;
629 static void tr_bd(char **args)
631 if (!args[1] || !strcmp("S", args[1]))
632 return;
633 font_setbd(dev_font(dev_pos(args[1])), args[2] ? eval(args[2], 'u') : 0);
636 static void tr_it(char **args)
638 if (args[2]) {
639 n_it = map(args[2]);
640 n_itn = eval(args[1], 0);
641 } else {
642 n_it = 0;
646 static void tr_mc(char **args)
648 char *s = args[1];
649 if (s && charread(&s, c_mc) >= 0) {
650 n_mc = 1;
651 n_mcn = args[2] ? eval(args[2], 'm') : SC_EM;
652 } else {
653 n_mc = 0;
657 static void tr_tc(char **args)
659 char *s = args[1];
660 if (!s || charread(&s, c_tc) < 0)
661 strcpy(c_tc, "");
664 static void tr_lc(char **args)
666 char *s = args[1];
667 if (!s || charread(&s, c_lc) < 0)
668 strcpy(c_lc, "");
671 static void tr_lf(char **args)
673 if (args[1])
674 in_lf(args[2], eval(args[1], 0));
677 static void tr_chop(char **args)
679 struct sbuf sbuf;
680 int id;
681 id = map(args[1]);
682 if (str_get(id)) {
683 sbuf_init(&sbuf);
684 sbuf_append(&sbuf, str_get(id));
685 if (!sbuf_empty(&sbuf)) {
686 sbuf_cut(&sbuf, sbuf_len(&sbuf) - 1);
687 str_set(id, sbuf_buf(&sbuf));
689 sbuf_done(&sbuf);
693 /* character translation (.tr) */
694 static struct dict *cmap; /* character mapping */
695 static char cmap_src[NCMAPS][GNLEN]; /* source character */
696 static char cmap_dst[NCMAPS][GNLEN]; /* character mapping */
697 static int cmap_n; /* number of translated character */
699 void cmap_add(char *c1, char *c2)
701 int i = dict_get(cmap, c1);
702 if (i >= 0) {
703 strcpy(cmap_dst[i], c2);
704 } else if (cmap_n < NCMAPS) {
705 strcpy(cmap_src[cmap_n], c1);
706 strcpy(cmap_dst[cmap_n], c2);
707 dict_put(cmap, cmap_src[cmap_n], cmap_n);
708 cmap_n++;
712 char *cmap_map(char *c)
714 int i = dict_get(cmap, c);
715 return i >= 0 ? cmap_dst[i] : c;
718 static void tr_tr(char **args)
720 char *s = args[1];
721 char c1[GNLEN], c2[GNLEN];
722 while (s && charread(&s, c1) >= 0) {
723 if (charread(&s, c2) < 0)
724 strcpy(c2, " ");
725 cmap_add(c1, c2);
729 /* character definition (.char) */
730 static char cdef_src[NCDEFS][GNLEN]; /* source character */
731 static char *cdef_dst[NCDEFS]; /* character definition */
732 static int cdef_fn[NCDEFS]; /* owning font */
733 static int cdef_n; /* number of defined characters */
734 static int cdef_expanding; /* inside cdef_expand() call */
736 static int cdef_find(char *c, int fn)
738 int i;
739 for (i = 0; i < cdef_n; i++)
740 if ((!cdef_fn[i] || cdef_fn[i] == fn) && !strcmp(cdef_src[i], c))
741 return i;
742 return -1;
745 /* return the definition of the given character */
746 char *cdef_map(char *c, int fn)
748 int i = cdef_find(c, fn);
749 return !cdef_expanding && i >= 0 ? cdef_dst[i] : NULL;
752 int cdef_expand(struct wb *wb, char *s, int fn)
754 char *d = cdef_map(s, fn);
755 if (!d)
756 return 1;
757 cdef_expanding = 1;
758 ren_parse(wb, d);
759 cdef_expanding = 0;
760 return 0;
763 static void cdef_add(char *fn, char *cs, char *def)
765 char c[GNLEN];
766 int i;
767 if (!def || charread(&cs, c) < 0)
768 return;
769 i = cdef_find(c, -1);
770 if (i < 0 && cdef_n < NCDEFS)
771 i = cdef_n++;
772 if (i >= 0) {
773 snprintf(cdef_src[i], sizeof(cdef_src[i]), "%s", c);
774 cdef_dst[i] = xmalloc(strlen(def) + 1);
775 strcpy(cdef_dst[i], def);
776 cdef_fn[i] = fn ? dev_pos(fn) : 0;
780 static void cdef_remove(char *cs)
782 char c[GNLEN];
783 int i;
784 if (!cs || charread(&cs, c) < 0)
785 return;
786 for (i = 0; i < cdef_n; i++) {
787 if (!strcmp(cdef_src[i], c)) {
788 free(cdef_dst[i]);
789 cdef_dst[i] = NULL;
790 cdef_src[i][0] = '\0';
795 static void tr_char(char **args)
797 cdef_add(NULL, args[1], args[2]);
800 static void tr_rchar(char **args)
802 int i;
803 for (i = 1; i <= NARGS; i++)
804 if (args[i])
805 cdef_remove(args[i]);
808 static void tr_ochar(char **args)
810 cdef_add(args[1], args[2], args[3]);
813 static void tr_fmap(char **args)
815 struct font *fn = args[1] ? dev_font(dev_pos(args[1])) : NULL;
816 if (fn && args[2])
817 font_map(fn, args[2], args[3]);
820 /* read a macro argument */
821 static int tr_arg(struct sbuf *sbuf, int brk, int (*next)(void), void (*back)(int))
823 int quoted = 0;
824 int c;
825 c = next();
826 while (c == ' ')
827 c = next();
828 if (c == '\n' || c == brk)
829 back(c);
830 if (c < 0 || c == '\n' || c == brk)
831 return 1;
832 if (c == '"') {
833 quoted = 1;
834 c = next();
836 while (c >= 0 && c != '\n' && (quoted || c != brk)) {
837 if (!quoted && c == ' ')
838 break;
839 if (quoted && c == '"') {
840 c = next();
841 if (c != '"')
842 break;
844 if (c == c_ec) {
845 sbuf_add(sbuf, c);
846 c = next();
848 sbuf_add(sbuf, c);
849 c = next();
851 sbuf_add(sbuf, 0);
852 if (c >= 0)
853 back(c);
854 return 0;
857 /* read macro arguments; free the returned pointer when done */
858 char *tr_args(char **args, int brk, int (*next)(void), void (*back)(int))
860 struct sbuf sbuf;
861 char *s, *e;
862 int n = 0;
863 sbuf_init(&sbuf);
864 while (!tr_arg(&sbuf, brk, next, back))
866 s = sbuf_buf(&sbuf);
867 e = s + sbuf_len(&sbuf);
868 while (n < NARGS && s && s < e) {
869 args[n++] = s;
870 if ((s = memchr(s, '\0', e - s)))
871 s++;
873 return sbuf_out(&sbuf);
876 /* split the arguments in sbuf */
877 static int tr_argschop(struct sbuf *sbuf, char **args)
879 char *s = sbuf_buf(sbuf);
880 char *e = s + sbuf_len(sbuf);
881 int n = 0;
882 while (n < NARGS && s && s < e) {
883 args[n++] = s;
884 if ((s = memchr(s, '\0', e - s)))
885 s++;
887 return n;
890 /* read request arguments; trims tabs too */
891 static void mkargs_req(struct sbuf *sbuf)
893 int n = 0;
894 int c;
895 c = cp_next();
896 while (n < NARGS) {
897 int ok = 0;
898 while (c == ' ' || c == '\t')
899 c = cp_next();
900 while (c >= 0 && c != '\n' && c != ' ' && c != '\t') {
901 if (c != c_ni)
902 sbuf_add(sbuf, c);
903 c = cp_next();
904 ok = 1;
906 if (ok) {
907 n++;
908 sbuf_add(sbuf, 0);
910 if (c == '\n')
911 cp_back(c);
912 if (c < 0 || c == '\n')
913 break;
915 jmp_eol();
918 /* read arguments for .ds */
919 static void mkargs_ds(struct sbuf *sbuf)
921 char *s = read_name(n_cp);
922 sbuf_append(sbuf, s);
923 sbuf_add(sbuf, 0);
924 free(s);
925 s = read_string();
926 sbuf_append(sbuf, s);
927 sbuf_add(sbuf, 0);
928 free(s);
929 jmp_eol();
932 /* read arguments for .char */
933 static void mkargs_def(struct sbuf *sbuf)
935 char *s = read_name(0);
936 sbuf_append(sbuf, s);
937 sbuf_add(sbuf, 0);
938 free(s);
939 s = read_string();
940 sbuf_append(sbuf, s);
941 sbuf_add(sbuf, 0);
942 free(s);
943 jmp_eol();
946 /* read arguments for .ochar */
947 static void mkargs_def3(struct sbuf *sbuf)
949 char *s = read_name(0);
950 sbuf_append(sbuf, s);
951 sbuf_add(sbuf, 0);
952 free(s);
953 mkargs_def(sbuf);
956 /* read arguments for .nr */
957 static void mkargs_reg1(struct sbuf *sbuf)
959 char *s = read_name(n_cp);
960 sbuf_append(sbuf, s);
961 sbuf_add(sbuf, 0);
962 free(s);
963 mkargs_req(sbuf);
966 /* do not read any arguments; for .if, .ie and .el */
967 static void mkargs_null(struct sbuf *sbuf)
971 /* read the whole line for .tm */
972 static void mkargs_eol(struct sbuf *sbuf)
974 int c;
975 cp_copymode(1);
976 c = cp_next();
977 while (c == ' ')
978 c = cp_next();
979 while (c >= 0 && c != '\n') {
980 if (c != c_ni)
981 sbuf_add(sbuf, c);
982 c = cp_next();
984 cp_copymode(0);
987 static struct cmd {
988 char *id;
989 void (*f)(char **args);
990 void (*args)(struct sbuf *sbuf);
991 } cmds[] = {
992 {TR_DIVBEG, tr_divbeg},
993 {TR_DIVEND, tr_divend},
994 {TR_DIVVS, tr_divvs},
995 {TR_POPREN, tr_popren},
996 {"ab", tr_ab, mkargs_eol},
997 {"ad", tr_ad},
998 {"af", tr_af},
999 {"am", tr_de, mkargs_reg1},
1000 {"as", tr_as, mkargs_ds},
1001 {"bd", tr_bd},
1002 {"bp", tr_bp},
1003 {"br", tr_br},
1004 {"c2", tr_c2},
1005 {"cc", tr_cc},
1006 {"ochar", tr_ochar, mkargs_def3},
1007 {"ce", tr_ce},
1008 {"ch", tr_ch},
1009 {"char", tr_char, mkargs_def},
1010 {"chop", tr_chop, mkargs_reg1},
1011 {"cl", tr_cl},
1012 {"cp", tr_cp},
1013 {"cs", tr_cs},
1014 {"da", tr_di},
1015 {"de", tr_de, mkargs_reg1},
1016 {"di", tr_di},
1017 {"ds", tr_ds, mkargs_ds},
1018 {"dt", tr_dt},
1019 {"ec", tr_ec},
1020 {"el", tr_el, mkargs_null},
1021 {"em", tr_em},
1022 {"eo", tr_eo},
1023 {"eos", tr_eos},
1024 {"ev", tr_ev},
1025 {"ex", tr_ex},
1026 {"fc", tr_fc},
1027 {"ff", tr_ff},
1028 {"fi", tr_fi},
1029 {"fl", tr_br},
1030 {"fmap", tr_fmap},
1031 {"fp", tr_fp},
1032 {"fspecial", tr_fspecial},
1033 {"ft", tr_ft},
1034 {"fzoom", tr_fzoom},
1035 {"hc", tr_hc},
1036 {"hcode", tr_hcode},
1037 {"hlm", tr_hlm},
1038 {"hpf", tr_hpf},
1039 {"hpfa", tr_hpfa},
1040 {"hy", tr_hy},
1041 {"hycost", tr_hycost},
1042 {"hydash", tr_hydash},
1043 {"hystop", tr_hystop},
1044 {"hw", tr_hw},
1045 {"ie", tr_if, mkargs_null},
1046 {"if", tr_if, mkargs_null},
1047 {"ig", tr_ig},
1048 {"in", tr_in},
1049 {"it", tr_it},
1050 {"kn", tr_kn},
1051 {"lc", tr_lc},
1052 {"lf", tr_lf},
1053 {"lg", tr_lg},
1054 {"ll", tr_ll},
1055 {"ls", tr_ls},
1056 {"lt", tr_lt},
1057 {"mc", tr_mc},
1058 {"mk", tr_mk},
1059 {"na", tr_na},
1060 {"ne", tr_ne},
1061 {"nf", tr_nf},
1062 {"nh", tr_nh},
1063 {"nm", tr_nm},
1064 {"nn", tr_nn},
1065 {"nr", tr_nr, mkargs_reg1},
1066 {"ns", tr_ns},
1067 {"nx", tr_nx},
1068 {"os", tr_os},
1069 {"pc", tr_pc},
1070 {"pl", tr_pl},
1071 {"pmll", tr_pmll},
1072 {"pn", tr_pn},
1073 {"po", tr_po},
1074 {"ps", tr_ps},
1075 {"rchar", tr_rchar},
1076 {"rm", tr_rm},
1077 {"rn", tr_rn},
1078 {"rr", tr_rr},
1079 {"rs", tr_rs},
1080 {"rt", tr_rt},
1081 {"so", tr_so},
1082 {"sp", tr_sp},
1083 {"ss", tr_ss},
1084 {"ssh", tr_ssh},
1085 {"sv", tr_sv},
1086 {"sy", tr_sy, mkargs_eol},
1087 {"ta", tr_ta},
1088 {"tc", tr_tc},
1089 {"ti", tr_ti},
1090 {"tl", tr_tl, mkargs_null},
1091 {"tm", tr_tm, mkargs_eol},
1092 {"tr", tr_tr, mkargs_eol},
1093 {"vs", tr_vs},
1094 {"wh", tr_wh},
1097 static char *dotted(char *name, int dot)
1099 char *out = xmalloc(strlen(name) + 2);
1100 out[0] = dot;
1101 strcpy(out + 1, name);
1102 return out;
1105 /* read the next troff request; return zero if a request was executed. */
1106 int tr_nextreq(void)
1108 char *args[NARGS + 3] = {NULL};
1109 char *cmd;
1110 struct cmd *req;
1111 struct sbuf sbuf;
1112 int c;
1113 if (!tr_nl)
1114 return 1;
1115 c = cp_next();
1116 if (c == c_ec) {
1117 int c2 = cp_next();
1118 if (c2 == '!') {
1119 args[0] = "\\!";
1120 sbuf_init(&sbuf);
1121 cp_copymode(1);
1122 mkargs_eol(&sbuf);
1123 cp_copymode(0);
1124 tr_argschop(&sbuf, args + 1);
1125 tr_transparent(args);
1126 sbuf_done(&sbuf);
1127 return 0;
1129 cp_back(c2);
1131 if (c < 0 || (c != c_cc && c != c_c2)) {
1132 cp_back(c);
1133 return 1;
1135 cp_reqbeg();
1136 cmd = read_name(n_cp);
1137 args[0] = dotted(cmd, c);
1138 req = str_dget(map(cmd));
1139 if (req) {
1140 sbuf_init(&sbuf);
1141 if (req->args)
1142 req->args(&sbuf);
1143 else
1144 mkargs_req(&sbuf);
1145 tr_argschop(&sbuf, args + 1);
1146 req->f(args);
1147 sbuf_done(&sbuf);
1148 } else {
1149 char *buf;
1150 cp_copymode(1);
1151 buf = tr_args(args + 1, -1, cp_next, cp_back);
1152 jmp_eol();
1153 cp_copymode(0);
1154 if (str_get(map(cmd)))
1155 in_push(str_get(map(cmd)), args + 1);
1156 free(buf);
1158 free(args[0]);
1159 free(cmd);
1160 return 0;
1163 int tr_next(void)
1165 int c;
1166 while (!tr_nextreq())
1168 c = cp_next();
1169 tr_nl = c == '\n' || c < 0;
1170 return c;
1173 void tr_init(void)
1175 int i;
1176 for (i = 0; i < LEN(cmds); i++)
1177 str_dset(map(cmds[i].id), &cmds[i]);
1178 cmap = dict_make(-1, 0, 0);
1181 void tr_done(void)
1183 dict_free(cmap);