ren: add .nm and .nn
[neatroff.git] / tr.c
blobd05293cb16608065e1b852b50da3bdc2c1b7fa70
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 void schar_read(char *d, int (*next)(void))
168 d[0] = next();
169 d[1] = '\0';
170 if (d[0] == c_ni) {
171 d[1] = next();
172 d[2] = '\0';
174 if (d[0] == c_ec) {
175 d[1] = next();
176 d[2] = '\0';
177 if (d[1] == '(') {
178 d[2] = next();
179 d[3] = next();
180 d[4] = '\0';
185 int schar_jump(char *d, int (*next)(void), void (*back)(int))
187 int c, i;
188 for (i = 0; d[i]; i++)
189 if ((c = next()) != d[i])
190 break;
191 if (d[i]) {
192 back(c);
193 while (i > 0)
194 back(d[--i]);
195 return 1;
197 return 0;
200 /* read into sbuf until stop; if stop is NULL, stop at whitespace */
201 static int read_until(struct sbuf *sbuf, char *stop)
203 int c;
204 while ((c = cp_next()) >= 0) {
205 cp_back(c);
206 if (c == '\n')
207 return 1;
208 if (!stop && (c == ' ' || c == '\t'))
209 return 0;
210 if (stop && !schar_jump(stop, cp_next, cp_back))
211 return 0;
212 sbuf_add(sbuf, cp_next());
214 return 1;
217 /* evaluate .if strcmp (i.e. 'str'str') */
218 static int if_strcmp(void)
220 char delim[GNLEN];
221 struct sbuf s1, s2;
222 int ret;
223 schar_read(delim, cp_next);
224 sbuf_init(&s1);
225 sbuf_init(&s2);
226 read_until(&s1, delim);
227 read_until(&s2, delim);
228 ret = !strcmp(sbuf_buf(&s1), sbuf_buf(&s2));
229 sbuf_done(&s1);
230 sbuf_done(&s2);
231 return ret;
234 /* evaluate .if condition letters */
235 static int if_cond(void)
237 switch (cp_next()) {
238 case 'o':
239 return n_pg % 2;
240 case 'e':
241 return !(n_pg % 2);
242 case 't':
243 return 1;
244 case 'n':
245 return 0;
247 return 0;
250 /* evaluate .if condition */
251 static int if_eval(void)
253 struct sbuf sbuf;
254 int ret;
255 sbuf_init(&sbuf);
256 if (!read_until(&sbuf, NULL))
257 cp_back(' ');
258 ret = eval(sbuf_buf(&sbuf), '\0') > 0;
259 sbuf_done(&sbuf);
260 return ret;
263 static int ie_cond[NIES]; /* .ie condition stack */
264 static int ie_depth;
266 static void tr_if(char **args)
268 int neg = 0;
269 int ret;
270 int c;
271 do {
272 c = cp_next();
273 } while (c == ' ' || c == '\t');
274 if (c == '!') {
275 neg = 1;
276 c = cp_next();
278 cp_back(c);
279 if (strchr("oetn", c)) {
280 ret = if_cond();
281 } else if (!isdigit(c) && !strchr("-+*/%<=>&:.|()", c)) {
282 ret = if_strcmp();
283 } else {
284 ret = if_eval();
286 if (args[0][1] == 'i' && args[0][2] == 'e') /* .ie command */
287 if (ie_depth < NIES)
288 ie_cond[ie_depth++] = ret != neg;
289 cp_blk(ret == neg);
292 static void tr_el(char **args)
294 cp_blk(ie_depth > 0 ? ie_cond[--ie_depth] : 1);
297 static void tr_na(char **args)
299 n_na = 1;
302 static void tr_ad(char **args)
304 n_na = 0;
305 if (!args[1])
306 return;
307 switch (args[1][0]) {
308 case '0' + AD_L:
309 case 'l':
310 n_j = AD_L;
311 break;
312 case '0' + AD_R:
313 case 'r':
314 n_j = AD_R;
315 break;
316 case '0' + AD_C:
317 case 'c':
318 n_j = AD_C;
319 break;
320 case '0' + AD_B:
321 case 'b':
322 case 'n':
323 n_j = AD_B;
324 break;
328 static void tr_tm(char **args)
330 fprintf(stderr, "%s\n", args[1]);
333 static void tr_so(char **args)
335 if (args[1])
336 in_so(args[1]);
339 static void tr_nx(char **args)
341 in_nx(args[1]);
344 static void tr_ex(char **args)
346 in_ex();
349 static void tr_sy(char **args)
351 system(args[1]);
354 static void tr_lt(char **args)
356 int lt = args[1] ? eval_re(args[1], n_lt, 'm') : n_t0;
357 n_t0 = n_t0;
358 n_lt = MAX(0, lt);
361 static void tr_pc(char **args)
363 c_pc = args[1] ? args[1][0] : -1;
366 static int tl_next(void)
368 int c = cp_next();
369 if (c >= 0 && c == c_pc) {
370 in_push(num_str(REG('%', '\0')), NULL);
371 c = cp_next();
373 return c;
376 static void tr_tl(char **args)
378 int c;
379 do {
380 c = cp_next();
381 } while (c >= 0 && (c == ' ' || c == '\t'));
382 cp_back(c);
383 ren_tl(tl_next, cp_back);
384 do {
385 c = cp_next();
386 } while (c >= 0 && c != '\n');
389 static void tr_ec(char **args)
391 c_ec = args[1] ? args[1][0] : '\\';
394 static void tr_cc(char **args)
396 c_ec = args[1] ? args[1][0] : '.';
399 static void tr_c2(char **args)
401 c_ec = args[1] ? args[1][0] : '\'';
404 static void tr_eo(char **args)
406 c_ec = -1;
409 static void tr_hc(char **args)
411 strcpy(c_hc, args[1] ? args[1] : "\\%");
414 static void tr_nh(char **args)
416 n_hy = 0;
419 static void tr_hy(char **args)
421 n_hy = args[1] ? atoi(args[1]) : 1;
424 static void tr_lg(char **args)
426 if (args[1])
427 n_lg = atoi(args[1]);
430 static void tr_kn(char **args)
432 if (args[1])
433 n_kn = atoi(args[1]);
436 static void tr_cp(char **args)
438 if (args[1])
439 n_cp = atoi(args[1]);
442 static void tr_ss(char **args)
444 if (args[1])
445 n_ss = eval_re(args[1], n_ss, 0);
448 static void tr_cs(char **args)
450 if (!args[1])
451 return;
452 dev_setcs(dev_pos(args[1]), args[2] ? eval(args[2], 0) : 0);
455 static void tr_nm(char **args)
457 if (!args[1]) {
458 n_nm = 0;
459 return;
461 n_nm = 1;
462 n_ln = eval_re(args[1], n_ln, 0);
463 n_ln = MAX(0, n_ln);
464 if (args[2] && isdigit(args[2][0]))
465 n_nM = MAX(1, eval(args[2], 0));
466 if (args[3] && isdigit(args[3][0]))
467 n_nS = MAX(0, eval(args[3], 0));
468 if (args[4] && isdigit(args[4][0]))
469 n_nI = MAX(0, eval(args[4], 0));
472 static void tr_nn(char **args)
474 n_nn = args[1] ? eval(args[1], 0) : 1;
477 static void tr_bd(char **args)
479 if (!args[1] || !strcmp("S", args[1]))
480 return;
481 dev_setbd(dev_pos(args[1]), args[2] ? eval(args[2], 'u') : 0);
484 static char *arg_regname(char *s, int len)
486 char *e = n_cp ? s + 2 : s + len;
487 int c = cp_next();
488 while (c == ' ' || c == '\t')
489 c = cp_next();
490 while (s < e && c >= 0 && c != ' ' && c != '\t' && c != '\n') {
491 *s++ = c;
492 c = cp_next();
494 if (c >= 0)
495 cp_back(c);
496 *s++ = '\0';
497 return s;
500 static char *arg_normal(char *s, int len)
502 char *e = s + len - 1;
503 int quoted = 0;
504 int c;
505 c = cp_next();
506 while (c == ' ')
507 c = cp_next();
508 if (c == '"') {
509 quoted = 1;
510 c = cp_next();
512 while (s < e && c > 0 && c != '\n') {
513 if (!quoted && c == ' ')
514 break;
515 if (quoted && c == '"') {
516 c = cp_next();
517 if (c != '"')
518 break;
520 *s++ = c;
521 c = cp_next();
523 if (c >= 0)
524 cp_back(c);
525 *s++ = '\0';
526 return s;
529 static char *arg_string(char *s, int len)
531 char *e = s + len - 1;
532 int c;
533 while ((c = cp_next()) == ' ')
535 if (c == '"')
536 c = cp_next();
537 while (s < e && c > 0 && c != '\n') {
538 *s++ = c;
539 c = cp_next();
541 *s++ = '\0';
542 if (c >= 0)
543 cp_back(c);
544 return s;
547 /* read macro arguments; trims tabs if rmtabs is nonzero */
548 static int mkargs(char **args, char *buf, int len)
550 char *s = buf;
551 char *e = buf + len - 1;
552 int c;
553 int n = 0;
554 while (n < NARGS) {
555 char *r = s;
556 c = cp_next();
557 if (c < 0 || c == '\n')
558 return n;
559 cp_back(c);
560 s = arg_normal(s, e - s);
561 if (*r != '\0')
562 args[n++] = r;
564 jmp_eol();
565 return n;
568 /* read request arguments; trims tabs too */
569 static int mkargs_req(char **args, char *buf, int len)
571 char *r, *s = buf;
572 char *e = buf + len - 1;
573 int c;
574 int n = 0;
575 c = cp_next();
576 while (n < NARGS && s < e) {
577 r = s;
578 while (c == ' ' || c == '\t')
579 c = cp_next();
580 while (c >= 0 && c != '\n' && c != ' ' && c != '\t' && s < e) {
581 *s++ = c;
582 c = cp_next();
584 *s++ = '\0';
585 if (*r != '\0')
586 args[n++] = r;
587 if (c < 0 || c == '\n')
588 return n;
590 jmp_eol();
591 return n;
594 /* read arguments for .ds */
595 static int mkargs_ds(char **args, char *buf, int len)
597 char *s = buf;
598 char *e = buf + len - 1;
599 int c;
600 args[0] = s;
601 s = arg_regname(s, e - s);
602 args[1] = s;
603 cp_wid(0);
604 s = arg_string(s, e - s);
605 cp_wid(1);
606 c = cp_next();
607 if (c >= 0 && c != '\n')
608 jmp_eol();
609 return 2;
612 /* read arguments for commands .nr that expect a register name */
613 static int mkargs_reg1(char **args, char *buf, int len)
615 char *s = buf;
616 char *e = buf + len - 1;
617 args[0] = s;
618 s = arg_regname(s, e - s);
619 return mkargs_req(args + 1, s, e - s) + 1;
622 /* do not read arguments; for .if, .ie and .el */
623 static int mkargs_null(char **args, char *buf, int len)
625 return 0;
628 /* read the whole line for .tm */
629 static int mkargs_eol(char **args, char *buf, int len)
631 char *s = buf;
632 char *e = buf + len - 1;
633 int c;
634 args[0] = s;
635 c = cp_next();
636 while (c == ' ')
637 c = cp_next();
638 while (s < e && c >= 0 && c != '\n') {
639 *s++ = c;
640 c = cp_next();
642 *s = '\0';
643 return 1;
646 static struct cmd {
647 char *id;
648 void (*f)(char **args);
649 int (*args)(char **args, char *buf, int len);
650 } cmds[] = {
651 {TR_DIVBEG, tr_divbeg},
652 {TR_DIVEND, tr_divend},
653 {TR_EJECT, tr_eject},
654 {"ab", tr_ab, mkargs_eol},
655 {"ad", tr_ad},
656 {"af", tr_af},
657 {"am", tr_de, mkargs_reg1},
658 {"as", tr_as, mkargs_ds},
659 {"bd", tr_bd},
660 {"bp", tr_bp},
661 {"br", tr_br},
662 {"c2", tr_c2},
663 {"cc", tr_cc},
664 {"ce", tr_ce},
665 {"ch", tr_ch},
666 {"cp", tr_cp},
667 {"cs", tr_cs},
668 {"da", tr_di},
669 {"de", tr_de, mkargs_reg1},
670 {"di", tr_di},
671 {"ds", tr_ds, mkargs_ds},
672 {"dt", tr_dt},
673 {"ec", tr_ec},
674 {"el", tr_el, mkargs_null},
675 {"em", tr_em},
676 {"eo", tr_eo},
677 {"ev", tr_ev},
678 {"ex", tr_ex},
679 {"fc", tr_fc},
680 {"fi", tr_fi},
681 {"fp", tr_fp},
682 {"ft", tr_ft},
683 {"hc", tr_hc},
684 {"hy", tr_hy},
685 {"hw", tr_hw},
686 {"ie", tr_if, mkargs_null},
687 {"if", tr_if, mkargs_null},
688 {"ig", tr_ig},
689 {"in", tr_in},
690 {"kn", tr_kn},
691 {"lg", tr_lg},
692 {"ll", tr_ll},
693 {"ls", tr_ls},
694 {"lt", tr_lt},
695 {"mk", tr_mk},
696 {"na", tr_na},
697 {"ne", tr_ne},
698 {"nf", tr_nf},
699 {"nh", tr_nh},
700 {"nm", tr_nm},
701 {"nn", tr_nn},
702 {"nr", tr_nr, mkargs_reg1},
703 {"ns", tr_ns},
704 {"nx", tr_nx},
705 {"os", tr_os},
706 {"pc", tr_pc},
707 {"pl", tr_pl},
708 {"pn", tr_pn},
709 {"po", tr_po},
710 {"ps", tr_ps},
711 {"rm", tr_rm},
712 {"rn", tr_rn},
713 {"rr", tr_rr},
714 {"rs", tr_rs},
715 {"rt", tr_rt},
716 {"so", tr_so},
717 {"sp", tr_sp},
718 {"ss", tr_ss},
719 {"sv", tr_sv},
720 {"sy", tr_sy, mkargs_eol},
721 {"ta", tr_ta},
722 {"ti", tr_ti},
723 {"tl", tr_tl, mkargs_null},
724 {"tm", tr_tm, mkargs_eol},
725 {"vs", tr_vs},
726 {"wh", tr_wh},
729 int tr_next(void)
731 int c = cp_next();
732 int nl = c == '\n';
733 char *args[NARGS + 3] = {NULL};
734 char cmd[RNLEN];
735 char buf[LNLEN];
736 struct cmd *req;
737 while (tr_nl && c >= 0 && (c == c_cc || c == c_c2)) {
738 nl = 1;
739 memset(args, 0, sizeof(args));
740 args[0] = cmd;
741 cmd[0] = c;
742 req = NULL;
743 arg_regname(cmd + 1, sizeof(cmd) - 1);
744 req = str_dget(map(cmd + 1));
745 if (req) {
746 if (req->args)
747 req->args(args + 1, buf, sizeof(buf));
748 else
749 mkargs_req(args + 1, buf, sizeof(buf));
750 req->f(args);
751 } else {
752 cp_wid(0);
753 mkargs(args + 1, buf, sizeof(buf));
754 cp_wid(1);
755 if (str_get(map(cmd + 1)))
756 in_push(str_get(map(cmd + 1)), args + 1);
758 c = cp_next();
759 nl = c == '\n';
761 tr_nl = c < 0 || nl;
762 return c;
765 void tr_init(void)
767 int i;
768 for (i = 0; i < LEN(cmds); i++)
769 str_dset(map(cmds[i].id), &cmds[i]);
772 void tr_first(void)
774 cp_back(tr_next());
775 tr_nl = 1;