1 /* built-in troff requests */
8 static int tr_nl
= 1; /* just read a newline */
9 static int c_pc
= '%'; /* page number character */
14 /* skip everything until the end of line */
15 static void jmp_eol(void)
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
;
30 static void tr_ls(char **args
)
32 int ls
= args
[1] ? eval_re(args
[1], n_L
, 0) : n_L0
;
37 static void tr_pl(char **args
)
39 int n
= eval_re(args
[1] ? args
[1] : "11i", n_p
, 'v');
43 static void tr_nr(char **args
)
49 num_set(id
, eval_re(args
[2], num_get(id
, 0), 'u'));
50 num_inc(id
, args
[3] ? eval(args
[3], 'u') : 0);
53 static void tr_rr(char **args
)
56 for (i
= 1; i
<= NARGS
; i
++)
58 num_del(map(args
[i
]));
61 static void tr_af(char **args
)
64 num_setfmt(map(args
[1]), args
[2]);
67 static void tr_ds(char **args
)
70 str_set(map(args
[1]), args
[2]);
73 static void tr_as(char **args
)
80 s1
= str_get(reg
) ? str_get(reg
) : "";
82 s
= malloc(strlen(s1
) + strlen(s2
) + 1);
89 static void tr_rm(char **args
)
92 for (i
= 1; i
<= NARGS
; i
++)
97 static void tr_rn(char **args
)
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
;
111 static void read_regname(char *s
)
114 int n
= n_cp
? 2 : NMLEN
- 1;
115 while (c
== ' ' || c
== '\t')
117 while (c
>= 0 && c
!= ' ' && c
!= '\t' && c
!= '\n' && --n
>= 0) {
126 static void macrobody(struct sbuf
*sbuf
, char *end
)
133 while ((c
= cp_next()) >= 0) {
141 if ((n_cp
&& end
[0] == buf
[0] && end
[1] == buf
[1]) ||
149 for (i
= 0; buf
[i
]; i
++)
150 sbuf_add(sbuf
, (unsigned char) buf
[i
]);
160 static void tr_de(char **args
)
168 if (args
[0][1] == 'a' && args
[0][2] == 'm' && str_get(id
))
169 sbuf_append(&sbuf
, str_get(id
));
170 macrobody(&sbuf
, args
[2] ? args
[2] : ".");
171 str_set(id
, sbuf_buf(&sbuf
));
175 static void tr_ig(char **args
)
177 macrobody(NULL
, args
[1] ? args
[1] : ".");
180 /* read into sbuf until stop; if stop is NULL, stop at whitespace */
181 static int read_until(struct sbuf
*sbuf
, char *stop
,
182 int (*next
)(void), void (*back
)(int))
184 char cs
[GNLEN
], cs2
[GNLEN
];
186 while ((c
= next()) >= 0) {
190 if (!stop
&& (c
== ' ' || c
== '\t'))
192 charnext(cs
, next
, back
);
193 if (stop
&& !strcmp(stop
, cs
))
195 charnext_str(cs2
, cs
);
196 sbuf_append(sbuf
, cs2
);
201 /* evaluate .if strcmp (i.e. 'str'str') */
202 static int if_strcmp(int (*next
)(void), void (*back
)(int))
207 charnext(delim
, next
, back
);
210 read_until(&s1
, delim
, next
, back
);
211 read_until(&s2
, delim
, next
, back
);
212 ret
= !strcmp(sbuf_buf(&s1
), sbuf_buf(&s2
));
218 /* evaluate .if condition letters */
219 static int if_cond(int (*next
)(void), void (*back
)(int))
234 /* evaluate .if condition */
235 static int if_eval(int (*next
)(void), void (*back
)(int))
240 if (!read_until(&sbuf
, NULL
, next
, back
))
242 ret
= eval(sbuf_buf(&sbuf
), '\0') > 0;
247 static int eval_if(int (*next
)(void), void (*back
)(int))
254 } while (c
== ' ' || c
== '\t');
260 if (strchr("oetn", c
)) {
261 ret
= if_cond(next
, back
);
262 } else if (!isdigit(c
) && !strchr("-+*/%<=>&:.|()", c
)) {
263 ret
= if_strcmp(next
, back
);
265 ret
= if_eval(next
, back
);
270 static int ie_cond
[NIES
]; /* .ie condition stack */
273 static void tr_if(char **args
)
275 int c
= eval_if(cp_next
, cp_back
);
276 if (args
[0][1] == 'i' && args
[0][2] == 'e') /* .ie command */
278 ie_cond
[ie_depth
++] = c
;
282 static void tr_el(char **args
)
284 cp_blk(ie_depth
> 0 ? ie_cond
[--ie_depth
] : 1);
287 static void tr_na(char **args
)
292 static int adjmode(int c
, int def
)
308 static void tr_ad(char **args
)
317 n_j
= s
[0] == 'p' ? AD_P
| adjmode(s
[1], AD_B
) : adjmode(s
[0], n_j
);
320 static void tr_tm(char **args
)
322 fprintf(stderr
, "%s\n", args
[1]);
325 static void tr_so(char **args
)
331 static void tr_nx(char **args
)
336 static void tr_ex(char **args
)
341 static void tr_sy(char **args
)
346 static void tr_lt(char **args
)
348 int lt
= args
[1] ? eval_re(args
[1], n_lt
, 'm') : n_t0
;
353 static void tr_pc(char **args
)
355 c_pc
= args
[1] ? args
[1][0] : -1;
358 static int tl_next(void)
361 if (c
>= 0 && c
== c_pc
) {
362 in_push(num_str(map("%")), NULL
);
368 static void tr_tl(char **args
)
373 } while (c
>= 0 && (c
== ' ' || c
== '\t'));
375 ren_tl(tl_next
, cp_back
);
378 } while (c
>= 0 && c
!= '\n');
381 static void tr_ec(char **args
)
383 c_ec
= args
[1] ? args
[1][0] : '\\';
386 static void tr_cc(char **args
)
388 c_ec
= args
[1] ? args
[1][0] : '.';
391 static void tr_c2(char **args
)
393 c_ec
= args
[1] ? args
[1][0] : '\'';
396 static void tr_eo(char **args
)
401 static void tr_hc(char **args
)
404 if (!s
|| charread(&s
, c_hc
) < 0)
408 static void tr_nh(char **args
)
413 static void tr_hy(char **args
)
415 n_hy
= args
[1] ? atoi(args
[1]) : 1;
418 static void tr_hyp(char **args
)
420 n_hyp
= args
[1] ? atoi(args
[1]) : 1;
423 static void tr_lg(char **args
)
426 n_lg
= atoi(args
[1]);
429 static void tr_kn(char **args
)
432 n_kn
= atoi(args
[1]);
435 static void tr_cp(char **args
)
438 n_cp
= atoi(args
[1]);
441 static void tr_ss(char **args
)
444 n_ss
= eval_re(args
[1], n_ss
, 0);
445 n_sss
= args
[2] ? eval_re(args
[2], n_sss
, 0) : n_ss
;
449 static void tr_ssh(char **args
)
451 n_ssh
= args
[1] ? eval_re(args
[1], n_ssh
, 0) : 0;
454 static void tr_cs(char **args
)
458 dev_setcs(dev_pos(args
[1]), args
[2] ? eval(args
[2], 0) : 0);
461 static void tr_nm(char **args
)
468 n_ln
= eval_re(args
[1], n_ln
, 0);
470 if (args
[2] && isdigit(args
[2][0]))
471 n_nM
= MAX(1, eval(args
[2], 0));
472 if (args
[3] && isdigit(args
[3][0]))
473 n_nS
= MAX(0, eval(args
[3], 0));
474 if (args
[4] && isdigit(args
[4][0]))
475 n_nI
= MAX(0, eval(args
[4], 0));
478 static void tr_nn(char **args
)
480 n_nn
= args
[1] ? eval(args
[1], 0) : 1;
483 static void tr_bd(char **args
)
485 if (!args
[1] || !strcmp("S", args
[1]))
487 dev_setbd(dev_pos(args
[1]), args
[2] ? eval(args
[2], 'u') : 0);
490 static void tr_it(char **args
)
494 n_itn
= eval(args
[1], 0);
500 static void tr_mc(char **args
)
503 if (s
&& charread(&s
, c_mc
) >= 0) {
505 n_mcn
= args
[2] ? eval(args
[2], 'm') : SC_EM
;
511 static void tr_tc(char **args
)
514 if (!s
|| charread(&s
, c_tc
) < 0)
518 static void tr_lc(char **args
)
521 if (!s
|| charread(&s
, c_lc
) < 0)
525 static void tr_lf(char **args
)
528 in_lf(args
[2], eval(args
[1], 0));
531 static void tr_chop(char **args
)
536 in_lf(args
[2], eval(args
[1], 0));
540 sbuf_append(&sbuf
, str_get(id
));
541 if (!sbuf_empty(&sbuf
)) {
542 sbuf_cut(&sbuf
, sbuf_len(&sbuf
) - 1);
543 str_set(id
, sbuf_buf(&sbuf
));
549 /* character translation (.tr) */
550 static char cmap_src
[NCMAPS
][GNLEN
]; /* source character */
551 static char cmap_dst
[NCMAPS
][GNLEN
]; /* character mapping */
552 static int cmap_n
; /* number of translated character */
554 static int tr_find(char *c
)
557 for (i
= 0; i
< cmap_n
; i
++)
558 if (!strcmp(c
, cmap_src
[i
]))
563 void cmap_add(char *c1
, char *c2
)
566 if (i
< 0 && cmap_n
< NCMAPS
)
569 strcpy(cmap_src
[i
], c1
);
570 strcpy(cmap_dst
[i
], c2
);
574 char *cmap_map(char *c
)
577 return i
>= 0 ? cmap_dst
[i
] : c
;
580 static void tr_tr(char **args
)
583 char c1
[GNLEN
], c2
[GNLEN
];
584 while (s
&& charread(&s
, c1
) >= 0) {
585 if (charread(&s
, c2
) < 0)
591 /* character definition (.char) */
592 static char cdef_src
[NCDEFS
][GNLEN
]; /* source character */
593 static char *cdef_dst
[NCDEFS
]; /* character definition */
594 static int cdef_fn
[NCDEFS
]; /* owning font */
595 static int cdef_n
; /* number of defined characters */
596 static int cdef_expanding
; /* inside cdef_expand() call */
598 static int cdef_find(char *c
, int fn
)
601 for (i
= 0; i
< cdef_n
; i
++)
602 if (!strcmp(cdef_src
[i
], c
) && (!cdef_fn
[i
] || cdef_fn
[i
] == fn
))
607 /* return the definition of the given character */
608 char *cdef_map(char *c
, int fn
)
610 int i
= cdef_find(c
, fn
);
611 return !cdef_expanding
&& i
>= 0 ? cdef_dst
[i
] : NULL
;
614 int cdef_expand(struct wb
*wb
, char *s
, int fn
)
616 char *d
= cdef_map(s
, fn
);
625 static void cdef_add(char *fn
, char *cs
, char *def
)
629 if (!def
|| charread(&cs
, c
) < 0)
631 i
= cdef_find(c
, -1);
632 if (i
< 0 && cdef_n
< NCDEFS
)
635 strncpy(cdef_src
[i
], c
, sizeof(cdef_src
[i
]) - 1);
636 cdef_dst
[i
] = malloc(strlen(def
) + 1);
637 strcpy(cdef_dst
[i
], def
);
638 cdef_fn
[i
] = fn
? dev_pos(fn
) : 0;
642 static void cdef_remove(char *cs
)
646 if (!cs
|| charread(&cs
, c
) < 0)
648 for (i
= 0; i
< cdef_n
; i
++) {
649 if (!strcmp(cdef_src
[i
], c
)) {
652 cdef_src
[i
][0] = '\0';
657 static void tr_char(char **args
)
659 cdef_add(NULL
, args
[1], args
[2]);
662 static void tr_rchar(char **args
)
665 for (i
= 1; i
<= NARGS
; i
++)
667 cdef_remove(args
[i
]);
670 static void tr_ochar(char **args
)
672 cdef_add(args
[1], args
[2], args
[3]);
675 static void tr_fmap(char **args
)
680 fn
= dev_font(dev_pos(args
[1]));
682 font_map(fn
, args
[2], args
[3] ? font_glyph(fn
, args
[3]) : NULL
);
685 static void arg_regname(struct sbuf
*sbuf
)
689 sbuf_append(sbuf
, reg
);
693 static void arg_string(struct sbuf
*sbuf
)
696 while ((c
= cp_next()) == ' ')
700 while (c
> 0 && c
!= '\n') {
709 static int mkargs_arg(struct sbuf
*sbuf
, int (*next
)(void), void (*back
)(int))
718 if (c
< 0 || c
== '\n')
724 while (c
>= 0 && c
!= '\n') {
725 if (!quoted
&& c
== ' ')
727 if (quoted
&& c
== '"') {
741 /* read macro arguments */
742 int tr_readargs(char **args
, struct sbuf
*sbuf
, int (*next
)(void), void (*back
)(int))
747 idx
[n
] = sbuf_len(sbuf
);
748 if (mkargs_arg(sbuf
, next
, back
))
752 for (i
= 0; i
< n
; i
++)
753 args
[i
] = sbuf_buf(sbuf
) + idx
[i
];
757 /* read macro arguments; trims tabs if rmtabs is nonzero */
758 static int mkargs(char **args
, struct sbuf
*sbuf
)
760 int n
= tr_readargs(args
, sbuf
, cp_next
, cp_back
);
765 /* read request arguments; trims tabs too */
766 static int mkargs_req(char **args
, struct sbuf
*sbuf
)
773 idx
[n
] = sbuf_len(sbuf
);
774 while (c
== ' ' || c
== '\t')
776 while (c
>= 0 && c
!= '\n' && c
!= ' ' && c
!= '\t') {
780 if (sbuf_len(sbuf
) > idx
[n
])
785 if (c
< 0 || c
== '\n')
788 for (i
= 0; i
< n
; i
++)
789 args
[i
] = sbuf_buf(sbuf
) + idx
[i
];
794 /* read arguments for .ds */
795 static int mkargs_ds(char **args
, struct sbuf
*sbuf
)
799 idx
[n
++] = sbuf_len(sbuf
);
801 idx
[n
++] = sbuf_len(sbuf
);
806 for (i
= 0; i
< n
; i
++)
807 args
[i
] = sbuf_buf(sbuf
) + idx
[i
];
811 /* read arguments for commands .nr that expect a register name */
812 static int mkargs_reg1(char **args
, struct sbuf
*sbuf
)
815 int idx0
= sbuf_len(sbuf
);
817 n
= mkargs_req(args
+ 1, sbuf
) + 1;
818 args
[0] = sbuf_buf(sbuf
) + idx0
;
822 /* do not read arguments; for .if, .ie and .el */
823 static int mkargs_null(char **args
, struct sbuf
*sbuf
)
828 /* read the whole line for .tm */
829 static int mkargs_eol(char **args
, struct sbuf
*sbuf
)
831 int idx0
= sbuf_len(sbuf
);
836 while (c
>= 0 && c
!= '\n') {
840 args
[0] = sbuf_buf(sbuf
) + idx0
;
846 void (*f
)(char **args
);
847 int (*args
)(char **args
, struct sbuf
*sbuf
);
849 {TR_DIVBEG
, tr_divbeg
},
850 {TR_DIVEND
, tr_divend
},
851 {TR_POPREN
, tr_popren
},
852 {"ab", tr_ab
, mkargs_eol
},
855 {"am", tr_de
, mkargs_reg1
},
856 {"as", tr_as
, mkargs_ds
},
865 {"char", tr_char
, mkargs_ds
},
866 {"chop", tr_chop
, mkargs_reg1
},
871 {"de", tr_de
, mkargs_reg1
},
873 {"ds", tr_ds
, mkargs_ds
},
876 {"el", tr_el
, mkargs_null
},
885 {"fspecial", tr_fspecial
},
893 {"ie", tr_if
, mkargs_null
},
894 {"if", tr_if
, mkargs_null
},
913 {"nr", tr_nr
, mkargs_reg1
},
922 {"rchar", tr_rchar
, mkargs_ds
},
933 {"sy", tr_sy
, mkargs_eol
},
937 {"tl", tr_tl
, mkargs_null
},
938 {"tm", tr_tm
, mkargs_eol
},
939 {"tr", tr_tr
, mkargs_eol
},
944 /* read the next troff request; return zero if a request was executed. */
947 char *args
[NARGS
+ 3] = {NULL
};
955 if (c
< 0 || (c
!= c_cc
&& c
!= c_c2
)) {
959 memset(args
, 0, sizeof(args
));
963 read_regname(cmd
+ 1);
965 req
= str_dget(map(cmd
+ 1));
968 req
->args(args
+ 1, &sbuf
);
970 mkargs_req(args
+ 1, &sbuf
);
974 mkargs(args
+ 1, &sbuf
);
976 if (str_get(map(cmd
+ 1)))
977 in_push(str_get(map(cmd
+ 1)), args
+ 1);
986 while (!tr_nextreq())
989 tr_nl
= c
== '\n' || c
< 0;
996 for (i
= 0; i
< LEN(cmds
); i
++)
997 str_dset(map(cmds
[i
].id
), &cmds
[i
]);