hyph: drop non-alpha characters for HY_FIRST2 and HY_FINAL2
[neatroff.git] / tr.c
blob3754dbcffadf0a1f321537864ae750bf20276af9
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(REG('%', '\0')), 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 */
497 static char tr_src[NTR][GNLEN];
498 static char tr_dst[NTR][GNLEN];
499 static int tr_n;
501 static int tr_find(char *c)
503 int i;
504 for (i = 0; i < tr_n; i++)
505 if (!strcmp(c, tr_src[i]))
506 return i;
507 return -1;
510 void tr_add(char *c1, char *c2)
512 int i = tr_find(c1);
513 if (i < 0 && tr_n < NTR)
514 i = tr_n++;
515 if (i >= 0) {
516 strcpy(tr_src[i], c1);
517 strcpy(tr_dst[i], c2);
521 char *tr_map(char *c)
523 int i = tr_find(c);
524 return i >= 0 ? tr_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 tr_add(c1, c2);
538 static char *arg_regname(char *s, int len)
540 char *e = n_cp ? s + 2 : s + len;
541 int c = cp_next();
542 while (c == ' ' || c == '\t')
543 c = cp_next();
544 while (s < e && c >= 0 && c != ' ' && c != '\t' && c != '\n') {
545 *s++ = c;
546 c = cp_next();
548 if (c >= 0)
549 cp_back(c);
550 *s++ = '\0';
551 return s;
554 static char *arg_normal(char *s, int len)
556 char *e = s + len - 1;
557 int quoted = 0;
558 int c;
559 c = cp_next();
560 while (c == ' ')
561 c = cp_next();
562 if (c == '"') {
563 quoted = 1;
564 c = cp_next();
566 while (s < e && c > 0 && c != '\n') {
567 if (!quoted && c == ' ')
568 break;
569 if (quoted && c == '"') {
570 c = cp_next();
571 if (c != '"')
572 break;
574 *s++ = c;
575 c = cp_next();
577 if (c >= 0)
578 cp_back(c);
579 *s++ = '\0';
580 return s;
583 static char *arg_string(char *s, int len)
585 char *e = s + len - 1;
586 int c;
587 while ((c = cp_next()) == ' ')
589 if (c == '"')
590 c = cp_next();
591 while (s < e && c > 0 && c != '\n') {
592 *s++ = c;
593 c = cp_next();
595 *s++ = '\0';
596 if (c >= 0)
597 cp_back(c);
598 return s;
601 /* read macro arguments; trims tabs if rmtabs is nonzero */
602 static int mkargs(char **args, char *buf, int len)
604 char *s = buf;
605 char *e = buf + len - 1;
606 int c;
607 int n = 0;
608 while (n < NARGS) {
609 char *r = s;
610 c = cp_next();
611 if (c < 0 || c == '\n')
612 return n;
613 cp_back(c);
614 s = arg_normal(s, e - s);
615 if (*r != '\0')
616 args[n++] = r;
618 jmp_eol();
619 return n;
622 /* read request arguments; trims tabs too */
623 static int mkargs_req(char **args, char *buf, int len)
625 char *r, *s = buf;
626 char *e = buf + len - 1;
627 int c;
628 int n = 0;
629 c = cp_next();
630 while (n < NARGS && s < e) {
631 r = s;
632 while (c == ' ' || c == '\t')
633 c = cp_next();
634 while (c >= 0 && c != '\n' && c != ' ' && c != '\t' && s < e) {
635 *s++ = c;
636 c = cp_next();
638 *s++ = '\0';
639 if (*r != '\0')
640 args[n++] = r;
641 if (c < 0 || c == '\n')
642 return n;
644 jmp_eol();
645 return n;
648 /* read arguments for .ds */
649 static int mkargs_ds(char **args, char *buf, int len)
651 char *s = buf;
652 char *e = buf + len - 1;
653 int c;
654 args[0] = s;
655 s = arg_regname(s, e - s);
656 args[1] = s;
657 cp_wid(0);
658 s = arg_string(s, e - s);
659 cp_wid(1);
660 c = cp_next();
661 if (c >= 0 && c != '\n')
662 jmp_eol();
663 return 2;
666 /* read arguments for commands .nr that expect a register name */
667 static int mkargs_reg1(char **args, char *buf, int len)
669 char *s = buf;
670 char *e = buf + len - 1;
671 args[0] = s;
672 s = arg_regname(s, e - s);
673 return mkargs_req(args + 1, s, e - s) + 1;
676 /* do not read arguments; for .if, .ie and .el */
677 static int mkargs_null(char **args, char *buf, int len)
679 return 0;
682 /* read the whole line for .tm */
683 static int mkargs_eol(char **args, char *buf, int len)
685 char *s = buf;
686 char *e = buf + len - 1;
687 int c;
688 args[0] = s;
689 c = cp_next();
690 while (c == ' ')
691 c = cp_next();
692 while (s < e && c >= 0 && c != '\n') {
693 *s++ = c;
694 c = cp_next();
696 *s = '\0';
697 return 1;
700 static struct cmd {
701 char *id;
702 void (*f)(char **args);
703 int (*args)(char **args, char *buf, int len);
704 } cmds[] = {
705 {TR_DIVBEG, tr_divbeg},
706 {TR_DIVEND, tr_divend},
707 {TR_EJECT, tr_eject},
708 {"ab", tr_ab, mkargs_eol},
709 {"ad", tr_ad},
710 {"af", tr_af},
711 {"am", tr_de, mkargs_reg1},
712 {"as", tr_as, mkargs_ds},
713 {"bd", tr_bd},
714 {"bp", tr_bp},
715 {"br", tr_br},
716 {"c2", tr_c2},
717 {"cc", tr_cc},
718 {"ce", tr_ce},
719 {"ch", tr_ch},
720 {"cp", tr_cp},
721 {"cs", tr_cs},
722 {"da", tr_di},
723 {"de", tr_de, mkargs_reg1},
724 {"di", tr_di},
725 {"ds", tr_ds, mkargs_ds},
726 {"dt", tr_dt},
727 {"ec", tr_ec},
728 {"el", tr_el, mkargs_null},
729 {"em", tr_em},
730 {"eo", tr_eo},
731 {"ev", tr_ev},
732 {"ex", tr_ex},
733 {"fc", tr_fc},
734 {"fi", tr_fi},
735 {"fp", tr_fp},
736 {"ft", tr_ft},
737 {"hc", tr_hc},
738 {"hy", tr_hy},
739 {"hw", tr_hw},
740 {"ie", tr_if, mkargs_null},
741 {"if", tr_if, mkargs_null},
742 {"ig", tr_ig},
743 {"in", tr_in},
744 {"it", tr_it},
745 {"kn", tr_kn},
746 {"lc", tr_lc},
747 {"lf", tr_lf},
748 {"lg", tr_lg},
749 {"ll", tr_ll},
750 {"ls", tr_ls},
751 {"lt", tr_lt},
752 {"mc", tr_mc},
753 {"mk", tr_mk},
754 {"na", tr_na},
755 {"ne", tr_ne},
756 {"nf", tr_nf},
757 {"nh", tr_nh},
758 {"nm", tr_nm},
759 {"nn", tr_nn},
760 {"nr", tr_nr, mkargs_reg1},
761 {"ns", tr_ns},
762 {"nx", tr_nx},
763 {"os", tr_os},
764 {"pc", tr_pc},
765 {"pl", tr_pl},
766 {"pn", tr_pn},
767 {"po", tr_po},
768 {"ps", tr_ps},
769 {"rm", tr_rm},
770 {"rn", tr_rn},
771 {"rr", tr_rr},
772 {"rs", tr_rs},
773 {"rt", tr_rt},
774 {"so", tr_so},
775 {"sp", tr_sp},
776 {"ss", tr_ss},
777 {"sv", tr_sv},
778 {"sy", tr_sy, mkargs_eol},
779 {"ta", tr_ta},
780 {"tc", tr_tc},
781 {"ti", tr_ti},
782 {"tl", tr_tl, mkargs_null},
783 {"tm", tr_tm, mkargs_eol},
784 {"tr", tr_tr, mkargs_eol},
785 {"vs", tr_vs},
786 {"wh", tr_wh},
789 int tr_next(void)
791 int c = cp_next();
792 int nl = c == '\n';
793 char *args[NARGS + 3] = {NULL};
794 char cmd[RNLEN];
795 char buf[LNLEN];
796 struct cmd *req;
797 while (tr_nl && c >= 0 && (c == c_cc || c == c_c2)) {
798 nl = 1;
799 memset(args, 0, sizeof(args));
800 args[0] = cmd;
801 cmd[0] = c;
802 req = NULL;
803 arg_regname(cmd + 1, sizeof(cmd) - 1);
804 req = str_dget(map(cmd + 1));
805 if (req) {
806 if (req->args)
807 req->args(args + 1, buf, sizeof(buf));
808 else
809 mkargs_req(args + 1, buf, sizeof(buf));
810 req->f(args);
811 } else {
812 cp_wid(0);
813 mkargs(args + 1, buf, sizeof(buf));
814 cp_wid(1);
815 if (str_get(map(cmd + 1)))
816 in_push(str_get(map(cmd + 1)), args + 1);
818 c = cp_next();
819 nl = c == '\n';
821 tr_nl = c < 0 || nl;
822 return c;
825 void tr_init(void)
827 int i;
828 for (i = 0; i < LEN(cmds); i++)
829 str_dset(map(cmds[i].id), &cmds[i]);
832 void tr_first(void)
834 cp_back(tr_next());
835 tr_nl = 1;