wb: interpret unknown escapes as normal characters
[neatroff.git] / out.c
blobc6b601bcaac8278128b0c9fcea65a0ce9b1d5854
1 /* generating troff output */
2 #include <ctype.h>
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include "roff.h"
9 static int out_nl = 1;
11 /* output troff code; newlines may appear only at the end of s */
12 static void out_out(char *s, va_list ap)
14 out_nl = strchr(s, '\n') != NULL;
15 vfprintf(stdout, s, ap);
18 /* output troff code; no preceding newline is necessary */
19 static void outnn(char *s, ...)
21 va_list ap;
22 va_start(ap, s);
23 out_out(s, ap);
24 va_end(ap);
27 /* output troff cmd; should appear after a newline */
28 void out(char *s, ...)
30 va_list ap;
31 if (!out_nl)
32 outnn("\n");
33 va_start(ap, s);
34 out_out(s, ap);
35 va_end(ap);
38 static int o_s = 10;
39 static int o_f = 1;
40 static int o_m = 0;
42 static void out_ps(int n)
44 if (o_s != n) {
45 o_s = n;
46 out("s%d\n", o_s);
50 static void out_ft(int n)
52 if (n >= 0 && o_f != n) {
53 o_f = n;
54 out("f%d\n", o_f);
58 static void out_clr(int n)
60 if (n >= 0 && o_m != n) {
61 o_m = n;
62 out("m%s\n", clr_str(o_m));
66 static int tok_num(char **s, int scale)
68 char tok[ILNLEN];
69 char *d = tok;
70 while (isspace(**s))
71 (*s)++;
72 while (**s && !isspace(**s))
73 *d++ = *(*s)++;
74 *d = '\0';
75 return eval(tok, scale);
78 static void out_draw(char *s)
80 int c = *s++;
81 out("D%c", c);
82 switch (tolower(c)) {
83 case 'l':
84 outnn(" %d", tok_num(&s, 'm'));
85 outnn(" %d", tok_num(&s, 'v'));
86 outnn(" ."); /* dpost requires this */
87 break;
88 case 'c':
89 outnn(" %d", tok_num(&s, 'm'));
90 break;
91 case 'e':
92 outnn(" %d", tok_num(&s, 'm'));
93 outnn(" %d", tok_num(&s, 'v'));
94 break;
95 case 'a':
96 outnn(" %d", tok_num(&s, 'm'));
97 outnn(" %d", tok_num(&s, 'v'));
98 outnn(" %d", tok_num(&s, 'm'));
99 outnn(" %d", tok_num(&s, 'v'));
100 break;
101 case '~':
102 case 'p':
103 outnn(" %d", tok_num(&s, 'm'));
104 outnn(" %d", tok_num(&s, 'v'));
105 while (*s) {
106 outnn(" %d", tok_num(&s, 'm'));
107 outnn(" %d", tok_num(&s, 'v'));
109 break;
111 outnn("\n");
114 static void outg(char *c, int fn)
116 int ofn = o_f;
117 out_ft(fn);
118 if (utf8one(c))
119 outnn("c%s%s", c, c[1] ? "\n" : "");
120 else
121 out("C%s\n", c[0] == c_ec && c[1] == '(' ? c + 2 : c);
122 out_ft(ofn);
125 static void outc(char *c)
127 struct glyph *g = dev_glyph(c, o_f);
128 int cwid = charwid(o_f, o_s, g ? g->wid : SC_DW);
129 int bwid = charwid_base(o_f, o_s, g ? g->wid : SC_DW);
130 if (g && font_mapped(g->font, c))
131 c = g->name;
132 if (dev_getcs(o_f))
133 outnn("h%d", (cwid - bwid) / 2);
134 outg(c, g ? dev_fontpos(g->font) : o_f);
135 if (dev_getbd(o_f)) {
136 outnn("h%d", dev_getbd(o_f) - 1);
137 outg(c, g ? dev_fontpos(g->font) : o_f);
138 outnn("h%d", -dev_getbd(o_f) + 1);
140 if (dev_getcs(o_f))
141 outnn("h%d", -(cwid - bwid) / 2);
142 outnn("h%d", cwid);
145 void out_line(char *s)
147 char c[ILNLEN + GNLEN * 4];
148 int t;
149 while ((t = escread(&s, c)) >= 0) {
150 if (!t) {
151 if (c[0] == c_ni || (c[0] == '\\' && c[1] == '\\')) {
152 c[0] = c[1];
153 c[1] = '\0';
155 if (c[0] == '\t' || c[0] == '\x01' ||
156 !strcmp(c_hc, c) || !strcmp(c_bp, c))
157 continue;
158 outc(cmap_map(c));
159 continue;
161 switch (t) {
162 case 'D':
163 out_draw(c);
164 break;
165 case 'f':
166 out_ft(dev_pos(c));
167 break;
168 case 'h':
169 outnn("h%d", eval(c, 'm'));
170 break;
171 case 'm':
172 if (!n_cp)
173 out_clr(clr_get(c));
174 break;
175 case 's':
176 out_ps(eval_re(c, o_s, '\0'));
177 break;
178 case 'v':
179 outnn("v%d", eval(c, 'v'));
180 break;
181 case 'X':
182 out("x X %s\n", c);
183 break;