in: do not fclose(stdin), even if it has been renamed with .lf
[neatroff.git] / tr.c
blobf869a5fe98deb12a4a61448a41aeca5c48e51fde
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "roff.h"
7 static int tr_nl = 1;
8 static int c_pc = '%'; /* page number character */
9 int c_ec = '\\';
10 int c_cc = '.';
11 int c_c2 = '\'';
13 /* skip everything until the end of line */
14 static void jmp_eol(void)
16 int c;
17 do {
18 c = cp_next();
19 } while (c >= 0 && c != '\n');
22 static void tr_vs(char **args)
24 int vs = args[1] ? eval_re(args[1], n_v, 'p') : n_v0;
25 n_v0 = n_v;
26 n_v = MAX(0, vs);
29 static void tr_ls(char **args)
31 int ls = args[1] ? eval_re(args[1], n_L, 0) : n_L0;
32 n_L0 = n_L;
33 n_L = MAX(1, ls);
36 static void tr_pl(char **args)
38 int n = eval_re(args[1] ? args[1] : "11i", n_p, 'v');
39 n_p = MAX(0, n);
42 static void tr_nr(char **args)
44 int id;
45 if (!args[2])
46 return;
47 id = map(args[1]);
48 num_set(id, eval_re(args[2], num_get(id, 0), 'u'));
49 num_inc(id, args[3] ? eval(args[3], 'u') : 0);
52 static void tr_rr(char **args)
54 int i;
55 for (i = 1; i <= NARGS; i++)
56 if (args[i])
57 num_del(map(args[i]));
60 static void tr_af(char **args)
62 if (args[2])
63 num_setfmt(map(args[1]), args[2]);
66 static void tr_ds(char **args)
68 if (args[2])
69 str_set(map(args[1]), args[2]);
72 static void tr_as(char **args)
74 int reg;
75 char *s1, *s2, *s;
76 if (!args[2])
77 return;
78 reg = map(args[1]);
79 s1 = str_get(reg) ? str_get(reg) : "";
80 s2 = args[2];
81 s = malloc(strlen(s1) + strlen(s2) + 1);
82 strcpy(s, s1);
83 strcat(s, s2);
84 str_set(reg, s);
85 free(s);
88 static void tr_rm(char **args)
90 int i;
91 for (i = 1; i <= NARGS; i++)
92 if (args[i])
93 str_rm(map(args[i]));
96 static void tr_rn(char **args)
98 if (!args[2])
99 return;
100 str_rn(map(args[1]), map(args[2]));
103 static void tr_po(char **args)
105 int po = args[1] ? eval_re(args[1], n_o, 'm') : n_o0;
106 n_o0 = n_o;
107 n_o = MAX(0, po);
110 static char *arg_regname(char *s, int len);
112 static void macrobody(struct sbuf *sbuf, char *end)
114 char buf[NMLEN];
115 int i, c;
116 int first = 1;
117 cp_back('\n');
118 cp_wid(0); /* copy-mode; disable \w handling */
119 while ((c = cp_next()) >= 0) {
120 if (sbuf && !first)
121 sbuf_add(sbuf, c);
122 first = 0;
123 if (c == '\n') {
124 c = cp_next();
125 if (c == '.') {
126 arg_regname(buf, sizeof(buf));
127 if ((n_cp && end[0] == buf[0] && end[1] == buf[1]) ||
128 !strcmp(end, buf)) {
129 jmp_eol();
130 break;
132 if (!sbuf)
133 continue;
134 sbuf_add(sbuf, '.');
135 for (i = 0; buf[i]; i++)
136 sbuf_add(sbuf, (unsigned char) buf[i]);
137 continue;
139 if (sbuf && c >= 0)
140 sbuf_add(sbuf, c);
143 cp_wid(1);
146 static void tr_de(char **args)
148 struct sbuf sbuf;
149 int id;
150 if (!args[1])
151 return;
152 id = map(args[1]);
153 sbuf_init(&sbuf);
154 if (args[0][1] == 'a' && args[0][2] == 'm' && str_get(id))
155 sbuf_append(&sbuf, str_get(id));
156 macrobody(&sbuf, args[2] ? args[2] : ".");
157 str_set(id, sbuf_buf(&sbuf));
158 sbuf_done(&sbuf);
161 static void tr_ig(char **args)
163 macrobody(NULL, args[1] ? args[1] : ".");
166 /* read into sbuf until stop; if stop is NULL, stop at whitespace */
167 static int read_until(struct sbuf *sbuf, char *stop)
169 char cs[GNLEN], cs2[GNLEN];
170 int c;
171 while ((c = cp_next()) >= 0) {
172 cp_back(c);
173 if (c == '\n')
174 return 1;
175 if (!stop && (c == ' ' || c == '\t'))
176 return 0;
177 charnext(cs, cp_next, cp_back);
178 if (stop && !strcmp(stop, cs))
179 return 0;
180 charnext_str(cs2, cs);
181 sbuf_append(sbuf, cs2);
183 return 1;
186 /* evaluate .if strcmp (i.e. 'str'str') */
187 static int if_strcmp(void)
189 char delim[GNLEN];
190 struct sbuf s1, s2;
191 int ret;
192 charnext(delim, cp_next, cp_back);
193 sbuf_init(&s1);
194 sbuf_init(&s2);
195 read_until(&s1, delim);
196 read_until(&s2, delim);
197 ret = !strcmp(sbuf_buf(&s1), sbuf_buf(&s2));
198 sbuf_done(&s1);
199 sbuf_done(&s2);
200 return ret;
203 /* evaluate .if condition letters */
204 static int if_cond(void)
206 switch (cp_next()) {
207 case 'o':
208 return n_pg % 2;
209 case 'e':
210 return !(n_pg % 2);
211 case 't':
212 return 1;
213 case 'n':
214 return 0;
216 return 0;
219 /* evaluate .if condition */
220 static int if_eval(void)
222 struct sbuf sbuf;
223 int ret;
224 sbuf_init(&sbuf);
225 if (!read_until(&sbuf, NULL))
226 cp_back(' ');
227 ret = eval(sbuf_buf(&sbuf), '\0') > 0;
228 sbuf_done(&sbuf);
229 return ret;
232 static int ie_cond[NIES]; /* .ie condition stack */
233 static int ie_depth;
235 static void tr_if(char **args)
237 int neg = 0;
238 int ret;
239 int c;
240 do {
241 c = cp_next();
242 } while (c == ' ' || c == '\t');
243 if (c == '!') {
244 neg = 1;
245 c = cp_next();
247 cp_back(c);
248 if (strchr("oetn", c)) {
249 ret = if_cond();
250 } else if (!isdigit(c) && !strchr("-+*/%<=>&:.|()", c)) {
251 ret = if_strcmp();
252 } else {
253 ret = if_eval();
255 if (args[0][1] == 'i' && args[0][2] == 'e') /* .ie command */
256 if (ie_depth < NIES)
257 ie_cond[ie_depth++] = ret != neg;
258 cp_blk(ret == neg);
261 static void tr_el(char **args)
263 cp_blk(ie_depth > 0 ? ie_cond[--ie_depth] : 1);
266 static void tr_na(char **args)
268 n_na = 1;
271 static void tr_ad(char **args)
273 n_na = 0;
274 if (!args[1])
275 return;
276 switch (args[1][0]) {
277 case '0' + AD_L:
278 case 'l':
279 n_j = AD_L;
280 break;
281 case '0' + AD_R:
282 case 'r':
283 n_j = AD_R;
284 break;
285 case '0' + AD_C:
286 case 'c':
287 n_j = AD_C;
288 break;
289 case '0' + AD_B:
290 case 'b':
291 case 'n':
292 n_j = AD_B;
293 break;
297 static void tr_tm(char **args)
299 fprintf(stderr, "%s\n", args[1]);
302 static void tr_so(char **args)
304 if (args[1])
305 in_so(args[1]);
308 static void tr_nx(char **args)
310 in_nx(args[1]);
313 static void tr_ex(char **args)
315 in_ex();
318 static void tr_sy(char **args)
320 system(args[1]);
323 static void tr_lt(char **args)
325 int lt = args[1] ? eval_re(args[1], n_lt, 'm') : n_t0;
326 n_t0 = n_t0;
327 n_lt = MAX(0, lt);
330 static void tr_pc(char **args)
332 c_pc = args[1] ? args[1][0] : -1;
335 static int tl_next(void)
337 int c = cp_next();
338 if (c >= 0 && c == c_pc) {
339 in_push(num_str(map("%")), NULL);
340 c = cp_next();
342 return c;
345 static void tr_tl(char **args)
347 int c;
348 do {
349 c = cp_next();
350 } while (c >= 0 && (c == ' ' || c == '\t'));
351 cp_back(c);
352 ren_tl(tl_next, cp_back);
353 do {
354 c = cp_next();
355 } while (c >= 0 && c != '\n');
358 static void tr_ec(char **args)
360 c_ec = args[1] ? args[1][0] : '\\';
363 static void tr_cc(char **args)
365 c_ec = args[1] ? args[1][0] : '.';
368 static void tr_c2(char **args)
370 c_ec = args[1] ? args[1][0] : '\'';
373 static void tr_eo(char **args)
375 c_ec = -1;
378 static void tr_hc(char **args)
380 char *s = args[1];
381 if (!s || charread(&s, c_hc) < 0)
382 strcpy(c_hc, "\\%");
385 static void tr_nh(char **args)
387 n_hy = 0;
390 static void tr_hy(char **args)
392 n_hy = args[1] ? atoi(args[1]) : 1;
395 static void tr_lg(char **args)
397 if (args[1])
398 n_lg = atoi(args[1]);
401 static void tr_kn(char **args)
403 if (args[1])
404 n_kn = atoi(args[1]);
407 static void tr_cp(char **args)
409 if (args[1])
410 n_cp = atoi(args[1]);
413 static void tr_ss(char **args)
415 if (args[1])
416 n_ss = eval_re(args[1], n_ss, 0);
419 static void tr_cs(char **args)
421 if (!args[1])
422 return;
423 dev_setcs(dev_pos(args[1]), args[2] ? eval(args[2], 0) : 0);
426 static void tr_nm(char **args)
428 if (!args[1]) {
429 n_nm = 0;
430 return;
432 n_nm = 1;
433 n_ln = eval_re(args[1], n_ln, 0);
434 n_ln = MAX(0, n_ln);
435 if (args[2] && isdigit(args[2][0]))
436 n_nM = MAX(1, eval(args[2], 0));
437 if (args[3] && isdigit(args[3][0]))
438 n_nS = MAX(0, eval(args[3], 0));
439 if (args[4] && isdigit(args[4][0]))
440 n_nI = MAX(0, eval(args[4], 0));
443 static void tr_nn(char **args)
445 n_nn = args[1] ? eval(args[1], 0) : 1;
448 static void tr_bd(char **args)
450 if (!args[1] || !strcmp("S", args[1]))
451 return;
452 dev_setbd(dev_pos(args[1]), args[2] ? eval(args[2], 'u') : 0);
455 static void tr_it(char **args)
457 if (args[2]) {
458 n_it = map(args[2]);
459 n_itn = eval(args[1], 0);
460 } else {
461 n_it = 0;
465 static void tr_mc(char **args)
467 char *s = args[1];
468 if (s && charread(&s, c_mc) >= 0) {
469 n_mc = 1;
470 n_mcn = args[2] ? eval(args[2], 'm') : SC_EM;
471 } else {
472 n_mc = 0;
476 static void tr_tc(char **args)
478 char *s = args[1];
479 if (!s || charread(&s, c_tc) < 0)
480 strcpy(c_tc, "");
483 static void tr_lc(char **args)
485 char *s = args[1];
486 if (!s || charread(&s, c_lc) < 0)
487 strcpy(c_lc, "");
490 static void tr_lf(char **args)
492 if (args[1])
493 in_lf(args[2], eval(args[1], 0));
496 /* character translation (.tr) */
497 static char cmap_src[NCMAPS][GNLEN]; /* source character */
498 static char cmap_dst[NCMAPS][GNLEN]; /* character mapping */
499 static int cmap_n; /* number of translated character */
501 static int tr_find(char *c)
503 int i;
504 for (i = 0; i < cmap_n; i++)
505 if (!strcmp(c, cmap_src[i]))
506 return i;
507 return -1;
510 void cmap_add(char *c1, char *c2)
512 int i = tr_find(c1);
513 if (i < 0 && cmap_n < NCMAPS)
514 i = cmap_n++;
515 if (i >= 0) {
516 strcpy(cmap_src[i], c1);
517 strcpy(cmap_dst[i], c2);
521 char *cmap_map(char *c)
523 int i = tr_find(c);
524 return i >= 0 ? cmap_dst[i] : c;
527 static void tr_tr(char **args)
529 char *s = args[1];
530 char c1[GNLEN], c2[GNLEN];
531 while (s && charread(&s, c1) >= 0) {
532 if (charread(&s, c2) < 0)
533 strcpy(c2, " ");
534 cmap_add(c1, c2);
538 /* character definition (.char) */
539 static char cdef_src[NCDEFS][GNLEN]; /* source character */
540 static char *cdef_dst[NCDEFS]; /* character definition */
541 static int cdef_n; /* number of defined characters */
542 static int cdef_expanding; /* inside cdef_expand() call */
544 static int cdef_find(char *c)
546 int i;
547 for (i = 0; i < cdef_n; i++)
548 if (!strcmp(cdef_src[i], c))
549 return i;
550 return -1;
553 /* return the definition of the given character */
554 char *cdef_map(char *c)
556 int i = cdef_find(c);
557 return !cdef_expanding && i >= 0 ? cdef_dst[i] : NULL;
560 int cdef_expand(struct wb *wb, char *s)
562 char *d = cdef_map(s);
563 if (!d)
564 return 1;
565 cdef_expanding = 1;
566 ren_parse(wb, d);
567 cdef_expanding = 0;
568 return 0;
571 static void tr_char(char **args)
573 char c[GNLEN];
574 char *s = args[1];
575 int i;
576 if (!args[2] || charread(&s, c) < 0)
577 return;
578 i = cdef_find(c);
579 if (i < 0 && cdef_n < NCDEFS)
580 i = cdef_n++;
581 if (i >= 0) {
582 strncpy(cdef_src[i], c, sizeof(cdef_src[i]) - 1);
583 cdef_dst[i] = malloc(strlen(args[2]) + 1);
584 strcpy(cdef_dst[i], args[2]);
588 static void tr_rchar(char **args)
590 char c[GNLEN];
591 char *s;
592 int i;
593 for (i = 1; i <= NARGS; i++) {
594 s = args[i];
595 if (s && charread(&s, c) >= 0) {
596 if (cdef_find(c) >= 0) {
597 free(cdef_dst[cdef_find(c)]);
598 cdef_dst[cdef_find(c)] = NULL;
604 static char *arg_regname(char *s, int len)
606 char *e = n_cp ? s + 2 : s + len;
607 int c = cp_next();
608 while (c == ' ' || c == '\t')
609 c = cp_next();
610 while (s < e && c >= 0 && c != ' ' && c != '\t' && c != '\n') {
611 *s++ = c;
612 c = cp_next();
614 if (c >= 0)
615 cp_back(c);
616 *s++ = '\0';
617 return s;
620 static char *arg_normal(char *s, int len)
622 char *e = s + len - 1;
623 int quoted = 0;
624 int c;
625 c = cp_next();
626 while (c == ' ')
627 c = cp_next();
628 if (c == '"') {
629 quoted = 1;
630 c = cp_next();
632 while (s < e && c > 0 && c != '\n') {
633 if (!quoted && c == ' ')
634 break;
635 if (quoted && c == '"') {
636 c = cp_next();
637 if (c != '"')
638 break;
640 *s++ = c;
641 c = cp_next();
643 if (c >= 0)
644 cp_back(c);
645 *s++ = '\0';
646 return s;
649 static char *arg_string(char *s, int len)
651 char *e = s + len - 1;
652 int c;
653 while ((c = cp_next()) == ' ')
655 if (c == '"')
656 c = cp_next();
657 while (s < e && c > 0 && c != '\n') {
658 *s++ = c;
659 c = cp_next();
661 *s++ = '\0';
662 if (c >= 0)
663 cp_back(c);
664 return s;
667 /* read macro arguments; trims tabs if rmtabs is nonzero */
668 static int mkargs(char **args, char *buf, int len)
670 char *s = buf;
671 char *e = buf + len - 1;
672 int c;
673 int n = 0;
674 while (n < NARGS) {
675 char *r = s;
676 c = cp_next();
677 if (c < 0 || c == '\n')
678 return n;
679 cp_back(c);
680 s = arg_normal(s, e - s);
681 if (*r != '\0')
682 args[n++] = r;
684 jmp_eol();
685 return n;
688 /* read request arguments; trims tabs too */
689 static int mkargs_req(char **args, char *buf, int len)
691 char *r, *s = buf;
692 char *e = buf + len - 1;
693 int c;
694 int n = 0;
695 c = cp_next();
696 while (n < NARGS && s < e) {
697 r = s;
698 while (c == ' ' || c == '\t')
699 c = cp_next();
700 while (c >= 0 && c != '\n' && c != ' ' && c != '\t' && s < e) {
701 *s++ = c;
702 c = cp_next();
704 *s++ = '\0';
705 if (*r != '\0')
706 args[n++] = r;
707 if (c < 0 || c == '\n')
708 return n;
710 jmp_eol();
711 return n;
714 /* read arguments for .ds */
715 static int mkargs_ds(char **args, char *buf, int len)
717 char *s = buf;
718 char *e = buf + len - 1;
719 int c;
720 args[0] = s;
721 s = arg_regname(s, e - s);
722 args[1] = s;
723 cp_wid(0);
724 s = arg_string(s, e - s);
725 cp_wid(1);
726 c = cp_next();
727 if (c >= 0 && c != '\n')
728 jmp_eol();
729 return 2;
732 /* read arguments for commands .nr that expect a register name */
733 static int mkargs_reg1(char **args, char *buf, int len)
735 char *s = buf;
736 char *e = buf + len - 1;
737 args[0] = s;
738 s = arg_regname(s, e - s);
739 return mkargs_req(args + 1, s, e - s) + 1;
742 /* do not read arguments; for .if, .ie and .el */
743 static int mkargs_null(char **args, char *buf, int len)
745 return 0;
748 /* read the whole line for .tm */
749 static int mkargs_eol(char **args, char *buf, int len)
751 char *s = buf;
752 char *e = buf + len - 1;
753 int c;
754 args[0] = s;
755 c = cp_next();
756 while (c == ' ')
757 c = cp_next();
758 while (s < e && c >= 0 && c != '\n') {
759 *s++ = c;
760 c = cp_next();
762 *s = '\0';
763 return 1;
766 static struct cmd {
767 char *id;
768 void (*f)(char **args);
769 int (*args)(char **args, char *buf, int len);
770 } cmds[] = {
771 {TR_DIVBEG, tr_divbeg},
772 {TR_DIVEND, tr_divend},
773 {TR_EJECT, tr_eject},
774 {"ab", tr_ab, mkargs_eol},
775 {"ad", tr_ad},
776 {"af", tr_af},
777 {"am", tr_de, mkargs_reg1},
778 {"as", tr_as, mkargs_ds},
779 {"bd", tr_bd},
780 {"bp", tr_bp},
781 {"br", tr_br},
782 {"c2", tr_c2},
783 {"cc", tr_cc},
784 {"ce", tr_ce},
785 {"ch", tr_ch},
786 {"char", tr_char, mkargs_ds},
787 {"cl", tr_cl},
788 {"cp", tr_cp},
789 {"cs", tr_cs},
790 {"da", tr_di},
791 {"de", tr_de, mkargs_reg1},
792 {"di", tr_di},
793 {"ds", tr_ds, mkargs_ds},
794 {"dt", tr_dt},
795 {"ec", tr_ec},
796 {"el", tr_el, mkargs_null},
797 {"em", tr_em},
798 {"eo", tr_eo},
799 {"ev", tr_ev},
800 {"ex", tr_ex},
801 {"fc", tr_fc},
802 {"fi", tr_fi},
803 {"fp", tr_fp},
804 {"ft", tr_ft},
805 {"hc", tr_hc},
806 {"hy", tr_hy},
807 {"hw", tr_hw},
808 {"ie", tr_if, mkargs_null},
809 {"if", tr_if, mkargs_null},
810 {"ig", tr_ig},
811 {"in", tr_in},
812 {"it", tr_it},
813 {"kn", tr_kn},
814 {"lc", tr_lc},
815 {"lf", tr_lf},
816 {"lg", tr_lg},
817 {"ll", tr_ll},
818 {"ls", tr_ls},
819 {"lt", tr_lt},
820 {"mc", tr_mc},
821 {"mk", tr_mk},
822 {"na", tr_na},
823 {"ne", tr_ne},
824 {"nf", tr_nf},
825 {"nh", tr_nh},
826 {"nm", tr_nm},
827 {"nn", tr_nn},
828 {"nr", tr_nr, mkargs_reg1},
829 {"ns", tr_ns},
830 {"nx", tr_nx},
831 {"os", tr_os},
832 {"pc", tr_pc},
833 {"pl", tr_pl},
834 {"pn", tr_pn},
835 {"po", tr_po},
836 {"ps", tr_ps},
837 {"rchar", tr_rchar, mkargs_ds},
838 {"rm", tr_rm},
839 {"rn", tr_rn},
840 {"rr", tr_rr},
841 {"rs", tr_rs},
842 {"rt", tr_rt},
843 {"so", tr_so},
844 {"sp", tr_sp},
845 {"ss", tr_ss},
846 {"sv", tr_sv},
847 {"sy", tr_sy, mkargs_eol},
848 {"ta", tr_ta},
849 {"tc", tr_tc},
850 {"ti", tr_ti},
851 {"tl", tr_tl, mkargs_null},
852 {"tm", tr_tm, mkargs_eol},
853 {"tr", tr_tr, mkargs_eol},
854 {"vs", tr_vs},
855 {"wh", tr_wh},
858 int tr_next(void)
860 int c = cp_next();
861 int nl = c == '\n';
862 char *args[NARGS + 3] = {NULL};
863 char cmd[RNLEN];
864 char buf[LNLEN];
865 struct cmd *req;
866 while (tr_nl && c >= 0 && (c == c_cc || c == c_c2)) {
867 nl = 1;
868 memset(args, 0, sizeof(args));
869 args[0] = cmd;
870 cmd[0] = c;
871 req = NULL;
872 arg_regname(cmd + 1, sizeof(cmd) - 1);
873 req = str_dget(map(cmd + 1));
874 if (req) {
875 if (req->args)
876 req->args(args + 1, buf, sizeof(buf));
877 else
878 mkargs_req(args + 1, buf, sizeof(buf));
879 req->f(args);
880 } else {
881 cp_wid(0);
882 mkargs(args + 1, buf, sizeof(buf));
883 cp_wid(1);
884 if (str_get(map(cmd + 1)))
885 in_push(str_get(map(cmd + 1)), args + 1);
887 c = cp_next();
888 nl = c == '\n';
890 tr_nl = c < 0 || nl;
891 return c;
894 void tr_init(void)
896 int i;
897 for (i = 0; i < LEN(cmds); i++)
898 str_dset(map(cmds[i].id), &cmds[i]);
901 void tr_first(void)
903 cp_back(tr_next());
904 tr_nl = 1;