8 static int c_pc
= '%'; /* page number character */
13 /* skip everything until the end of line */
14 static void jmp_eol(void)
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
;
29 static void tr_ls(char **args
)
31 int ls
= args
[1] ? eval_re(args
[1], n_L
, 0) : n_L0
;
36 static void tr_pl(char **args
)
38 int n
= eval_re(args
[1] ? args
[1] : "11i", n_p
, 'v');
42 static void tr_nr(char **args
)
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
)
55 for (i
= 1; i
<= NARGS
; i
++)
57 num_del(map(args
[i
]));
60 static void tr_af(char **args
)
63 num_setfmt(map(args
[1]), args
[2]);
66 static void tr_ds(char **args
)
69 str_set(map(args
[1]), args
[2]);
72 static void tr_as(char **args
)
79 s1
= str_get(reg
) ? str_get(reg
) : "";
81 s
= malloc(strlen(s1
) + strlen(s2
) + 1);
88 static void tr_rm(char **args
)
91 for (i
= 1; i
<= NARGS
; i
++)
96 static void tr_rn(char **args
)
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
;
110 static char *arg_regname(char *s
, int len
);
112 static void macrobody(struct sbuf
*sbuf
, char *end
)
118 cp_wid(0); /* copy-mode; disable \w handling */
119 while ((c
= cp_next()) >= 0) {
126 arg_regname(buf
, sizeof(buf
));
127 if ((n_cp
&& end
[0] == buf
[0] && end
[1] == buf
[1]) ||
135 for (i
= 0; buf
[i
]; i
++)
136 sbuf_add(sbuf
, (unsigned char) buf
[i
]);
146 static void tr_de(char **args
)
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
));
161 static void tr_ig(char **args
)
163 macrobody(NULL
, args
[1] ? args
[1] : ".");
166 void schar_read(char *d
, int (*next
)(void))
185 int schar_jump(char *d
, int (*next
)(void), void (*back
)(int))
188 for (i
= 0; d
[i
]; i
++)
189 if ((c
= next()) != d
[i
])
200 /* read into sbuf until stop; if stop is NULL, stop at whitespace */
201 static int read_until(struct sbuf
*sbuf
, char *stop
)
204 while ((c
= cp_next()) >= 0) {
208 if (!stop
&& (c
== ' ' || c
== '\t'))
210 if (stop
&& !schar_jump(stop
, cp_next
, cp_back
))
212 sbuf_add(sbuf
, cp_next());
217 /* evaluate .if strcmp (i.e. 'str'str') */
218 static int if_strcmp(void)
223 schar_read(delim
, cp_next
);
226 read_until(&s1
, delim
);
227 read_until(&s2
, delim
);
228 ret
= !strcmp(sbuf_buf(&s1
), sbuf_buf(&s2
));
234 /* evaluate .if condition letters */
235 static int if_cond(void)
250 /* evaluate .if condition */
251 static int if_eval(void)
256 if (!read_until(&sbuf
, NULL
))
258 ret
= eval(sbuf_buf(&sbuf
), '\0') > 0;
263 static int ie_cond
[NIES
]; /* .ie condition stack */
266 static void tr_if(char **args
)
273 } while (c
== ' ' || c
== '\t');
279 if (strchr("oetn", c
)) {
281 } else if (!isdigit(c
) && !strchr("-+*/%<=>&:.|()", c
)) {
286 if (args
[0][1] == 'i' && args
[0][2] == 'e') /* .ie command */
288 ie_cond
[ie_depth
++] = 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
)
302 static void tr_ad(char **args
)
307 switch (args
[1][0]) {
328 static void tr_tm(char **args
)
330 fprintf(stderr
, "%s\n", args
[1]);
333 static void tr_so(char **args
)
339 static void tr_nx(char **args
)
345 static void tr_ex(char **args
)
350 static void tr_sy(char **args
)
355 static void tr_lt(char **args
)
357 int lt
= args
[1] ? eval_re(args
[1], n_lt
, 'm') : n_t0
;
362 static void tr_pc(char **args
)
364 c_pc
= args
[1] ? args
[1][0] : -1;
367 static int tl_next(void)
370 if (c
>= 0 && c
== c_pc
) {
371 in_push(num_str(REG('%', '\0')), NULL
);
377 static void tr_tl(char **args
)
382 } while (c
>= 0 && (c
== ' ' || c
== '\t'));
384 ren_tl(tl_next
, cp_back
);
387 } while (c
>= 0 && c
!= '\n');
390 static void tr_ec(char **args
)
392 c_ec
= args
[1] ? args
[1][0] : '\\';
395 static void tr_cc(char **args
)
397 c_ec
= args
[1] ? args
[1][0] : '.';
400 static void tr_c2(char **args
)
402 c_ec
= args
[1] ? args
[1][0] : '\'';
405 static void tr_eo(char **args
)
410 static void tr_hc(char **args
)
412 strcpy(c_hc
, args
[1] ? args
[1] : "\\%");
415 static void tr_nh(char **args
)
420 static void tr_hy(char **args
)
422 n_hy
= args
[1] ? atoi(args
[1]) : 1;
425 static void tr_lg(char **args
)
428 n_lg
= atoi(args
[1]);
431 static void tr_kn(char **args
)
434 n_kn
= atoi(args
[1]);
437 static void tr_cp(char **args
)
440 n_cp
= atoi(args
[1]);
443 static char *arg_regname(char *s
, int len
)
445 char *e
= n_cp
? s
+ 2 : s
+ len
;
447 while (c
== ' ' || c
== '\t')
449 while (s
< e
&& c
>= 0 && c
!= ' ' && c
!= '\t' && c
!= '\n') {
459 static char *arg_normal(char *s
, int len
)
461 char *e
= s
+ len
- 1;
471 while (s
< e
&& c
> 0 && c
!= '\n') {
472 if (!quoted
&& c
== ' ')
474 if (quoted
&& c
== '"') {
488 static char *arg_string(char *s
, int len
)
490 char *e
= s
+ len
- 1;
492 while ((c
= cp_next()) == ' ')
496 while (s
< e
&& c
> 0 && c
!= '\n') {
506 /* read macro arguments; trims tabs if rmtabs is nonzero */
507 static int mkargs(char **args
, char *buf
, int len
)
510 char *e
= buf
+ len
- 1;
516 if (c
< 0 || c
== '\n')
519 s
= arg_normal(s
, e
- s
);
527 /* read request arguments; trims tabs too */
528 static int mkargs_req(char **args
, char *buf
, int len
)
531 char *e
= buf
+ len
- 1;
535 while (n
< NARGS
&& s
< e
) {
537 while (c
== ' ' || c
== '\t')
539 while (c
>= 0 && c
!= '\n' && c
!= ' ' && c
!= '\t' && s
< e
) {
546 if (c
< 0 || c
== '\n')
553 /* read arguments for .ds */
554 static int mkargs_ds(char **args
, char *buf
, int len
)
557 char *e
= buf
+ len
- 1;
560 s
= arg_regname(s
, e
- s
);
563 s
= arg_string(s
, e
- s
);
566 if (c
>= 0 && c
!= '\n')
571 /* read arguments for commands .nr that expect a register name */
572 static int mkargs_reg1(char **args
, char *buf
, int len
)
575 char *e
= buf
+ len
- 1;
577 s
= arg_regname(s
, e
- s
);
578 return mkargs_req(args
+ 1, s
, e
- s
) + 1;
581 /* do not read arguments; for .if, .ie and .el */
582 static int mkargs_null(char **args
, char *buf
, int len
)
587 /* read the whole line for .tm */
588 static int mkargs_eol(char **args
, char *buf
, int len
)
591 char *e
= buf
+ len
- 1;
597 while (s
< e
&& c
>= 0 && c
!= '\n') {
607 void (*f
)(char **args
);
608 int (*args
)(char **args
, char *buf
, int len
);
610 {TR_DIVBEG
, tr_divbeg
},
611 {TR_DIVEND
, tr_divend
},
612 {TR_EJECT
, tr_eject
},
613 {"ab", tr_ab
, mkargs_eol
},
616 {"am", tr_de
, mkargs_reg1
},
617 {"as", tr_as
, mkargs_ds
},
626 {"de", tr_de
, mkargs_reg1
},
628 {"ds", tr_ds
, mkargs_ds
},
631 {"el", tr_el
, mkargs_null
},
643 {"ie", tr_if
, mkargs_null
},
644 {"if", tr_if
, mkargs_null
},
657 {"nr", tr_nr
, mkargs_reg1
},
674 {"sy", tr_sy
, mkargs_eol
},
677 {"tl", tr_tl
, mkargs_null
},
678 {"tm", tr_tm
, mkargs_eol
},
687 char *args
[NARGS
+ 3] = {NULL
};
691 while (tr_nl
&& c
>= 0 && (c
== c_cc
|| c
== c_c2
)) {
693 memset(args
, 0, sizeof(args
));
697 arg_regname(cmd
+ 1, sizeof(cmd
) - 1);
698 req
= str_dget(map(cmd
+ 1));
701 req
->args(args
+ 1, buf
, sizeof(buf
));
703 mkargs_req(args
+ 1, buf
, sizeof(buf
));
707 mkargs(args
+ 1, buf
, sizeof(buf
));
709 if (str_get(map(cmd
+ 1)))
710 in_push(str_get(map(cmd
+ 1)), args
+ 1);
722 for (i
= 0; i
< LEN(cmds
); i
++)
723 str_dset(map(cmds
[i
].id
), &cmds
[i
]);