xprintf.c revision 1.1 1 1.1 itohy /*
2 1.1 itohy * minimal printf for Human68k DOS
3 1.1 itohy *
4 1.1 itohy * written by Yasha (ITOH Yasufumi)
5 1.1 itohy * public domain
6 1.1 itohy *
7 1.1 itohy * $NetBSD: xprintf.c,v 1.1 1998/09/01 19:51:57 itohy Exp $
8 1.1 itohy */
9 1.1 itohy
10 1.1 itohy #include <sys/types.h>
11 1.1 itohy #ifdef __STDC__
12 1.1 itohy # include <stdarg.h>
13 1.1 itohy #else
14 1.1 itohy # include <varargs.h>
15 1.1 itohy #endif
16 1.1 itohy
17 1.1 itohy #include <dos.h>
18 1.1 itohy #include <dos_errno.h>
19 1.1 itohy
20 1.1 itohy #include "xprintf.h"
21 1.1 itohy
22 1.1 itohy /*
23 1.1 itohy * From ISO/IEC 9899:1990
24 1.1 itohy * 7.9.6.1 The fprintf function
25 1.1 itohy * ...
26 1.1 itohy * Environment limit
27 1.1 itohy * The minimum value for the maximum number of characters
28 1.1 itohy * produced by any single conversion shall be 509.
29 1.1 itohy *
30 1.1 itohy * so the following value shall not be smaller than 510
31 1.1 itohy * if you want to conform ANSI C (it is only a guideline
32 1.1 itohy * and maybe no sense on this code, of course :-).
33 1.1 itohy */
34 1.1 itohy #define PRINTF_BUFSZ 4096
35 1.1 itohy
36 1.1 itohy /*
37 1.1 itohy * Shift-JIS kanji support
38 1.1 itohy * (No special handling needed for EUC)
39 1.1 itohy */
40 1.1 itohy #define SJIS
41 1.1 itohy
42 1.1 itohy #ifdef SJIS
43 1.1 itohy #define UC(c) ((unsigned char) (c))
44 1.1 itohy #define IS_SJIS1(c) ((UC(c) > 0x80 && UC(c) < 0xa0) || \
45 1.1 itohy (UC(c) >= 0xe0 && UC(c) <= 0xfc))
46 1.1 itohy #define IS_SJIS2(c) (UC(c) >= 0x40 && UC(c) <= 0xfc && UC(c) != 0x7f)
47 1.1 itohy #endif
48 1.1 itohy
49 1.1 itohy #if !defined(__STDC__) && !defined(const)
50 1.1 itohy #define const
51 1.1 itohy #endif
52 1.1 itohy
53 1.1 itohy extern const char *const __progname;
54 1.1 itohy
55 1.1 itohy static char * numstr __P((char *buf, long val, int base, int sign));
56 1.1 itohy
57 1.1 itohy /*
58 1.1 itohy * convert number to string
59 1.1 itohy * buf must have enough space
60 1.1 itohy */
61 1.1 itohy static char *
62 1.1 itohy numstr(buf, val, base, sign)
63 1.1 itohy char *buf;
64 1.1 itohy long val;
65 1.1 itohy int base, sign;
66 1.1 itohy {
67 1.1 itohy unsigned long v;
68 1.1 itohy char rev[32];
69 1.1 itohy char *r = rev, *b = buf;
70 1.1 itohy
71 1.1 itohy /* negative? */
72 1.1 itohy if (sign && val < 0) {
73 1.1 itohy v = -val;
74 1.1 itohy *b++ = '-';
75 1.1 itohy } else {
76 1.1 itohy v = val;
77 1.1 itohy }
78 1.1 itohy
79 1.1 itohy /* inverse order */
80 1.1 itohy do {
81 1.1 itohy *r++ = "0123456789abcdef"[v % base];
82 1.1 itohy v /= base;
83 1.1 itohy } while (v);
84 1.1 itohy
85 1.1 itohy /* reverse string */
86 1.1 itohy while (r > rev)
87 1.1 itohy *b++ = *--r;
88 1.1 itohy
89 1.1 itohy *b = '\0';
90 1.1 itohy return buf;
91 1.1 itohy }
92 1.1 itohy
93 1.1 itohy /*
94 1.1 itohy * supported format: %x, %p, %s, %c, %d, %u, %o
95 1.1 itohy * \n is converted to \r\n
96 1.1 itohy *
97 1.1 itohy * XXX argument/parameter types are not strictly handled
98 1.1 itohy */
99 1.1 itohy size_t
100 1.1 itohy xvsnprintf(buf, len, fmt, ap)
101 1.1 itohy char *buf;
102 1.1 itohy size_t len;
103 1.1 itohy const char *fmt;
104 1.1 itohy va_list ap;
105 1.1 itohy {
106 1.1 itohy char *b = buf;
107 1.1 itohy const char *s;
108 1.1 itohy char numbuf[32];
109 1.1 itohy
110 1.1 itohy while (*fmt && len > 1) {
111 1.1 itohy if (*fmt != '%') {
112 1.1 itohy #ifdef SJIS
113 1.1 itohy if (IS_SJIS1(*fmt) && IS_SJIS2(fmt[1])) {
114 1.1 itohy if (len <= 2)
115 1.1 itohy break; /* not enough space */
116 1.1 itohy *b++ = *fmt++;
117 1.1 itohy len--;
118 1.1 itohy }
119 1.1 itohy #endif
120 1.1 itohy if (*fmt == '\n' && (b == buf || b[-1] != '\r')) {
121 1.1 itohy if (len <= 2)
122 1.1 itohy break;
123 1.1 itohy *b++ = '\r';
124 1.1 itohy len--;
125 1.1 itohy }
126 1.1 itohy *b++ = *fmt++;
127 1.1 itohy len--;
128 1.1 itohy continue;
129 1.1 itohy }
130 1.1 itohy
131 1.1 itohy /* %? */
132 1.1 itohy fmt++;
133 1.1 itohy switch (*fmt++) {
134 1.1 itohy case '%': /* "%%" -> literal % */
135 1.1 itohy *b++ = '%';
136 1.1 itohy len--;
137 1.1 itohy break;
138 1.1 itohy
139 1.1 itohy case 'd':
140 1.1 itohy s = numstr(numbuf, va_arg(ap, long), 10, 1);
141 1.1 itohy copy_string:
142 1.1 itohy for ( ; *s && len > 1; len--)
143 1.1 itohy *b++ = *s++;
144 1.1 itohy break;
145 1.1 itohy
146 1.1 itohy case 'u':
147 1.1 itohy s = numstr(numbuf, va_arg(ap, long), 10, 0);
148 1.1 itohy goto copy_string;
149 1.1 itohy
150 1.1 itohy case 'p':
151 1.1 itohy *b++ = '0';
152 1.1 itohy len--;
153 1.1 itohy if (len > 1) {
154 1.1 itohy *b++ = 'x';
155 1.1 itohy len--;
156 1.1 itohy }
157 1.1 itohy /* FALLTHROUGH */
158 1.1 itohy case 'x':
159 1.1 itohy s = numstr(numbuf, va_arg(ap, long), 16, 0);
160 1.1 itohy goto copy_string;
161 1.1 itohy
162 1.1 itohy case 'o':
163 1.1 itohy s = numstr(numbuf, va_arg(ap, long), 8, 0);
164 1.1 itohy goto copy_string;
165 1.1 itohy
166 1.1 itohy case 's':
167 1.1 itohy s = va_arg(ap, char *);
168 1.1 itohy while (*s && len > 1) {
169 1.1 itohy #ifdef SJIS
170 1.1 itohy if (IS_SJIS1(*s) && IS_SJIS2(s[1])) {
171 1.1 itohy if (len <= 2)
172 1.1 itohy goto break_loop;
173 1.1 itohy *b++ = *s++;
174 1.1 itohy len--;
175 1.1 itohy }
176 1.1 itohy #endif
177 1.1 itohy if (*s == '\n' && (b == buf || b[-1] != '\r')) {
178 1.1 itohy if (len <= 2)
179 1.1 itohy goto break_loop;
180 1.1 itohy *b++ = '\r';
181 1.1 itohy len--;
182 1.1 itohy }
183 1.1 itohy *b++ = *s++;
184 1.1 itohy len--;
185 1.1 itohy }
186 1.1 itohy break;
187 1.1 itohy
188 1.1 itohy case 'c':
189 1.1 itohy *b++ = va_arg(ap, int);
190 1.1 itohy len--;
191 1.1 itohy break;
192 1.1 itohy }
193 1.1 itohy }
194 1.1 itohy break_loop:
195 1.1 itohy
196 1.1 itohy *b = '\0';
197 1.1 itohy return (char *)b - buf;
198 1.1 itohy }
199 1.1 itohy
200 1.1 itohy #ifdef __STDC__
201 1.1 itohy #define VA_START(a, v) va_start(a, v)
202 1.1 itohy #else
203 1.1 itohy #define VA_START(a, v) va_start(a)
204 1.1 itohy #endif
205 1.1 itohy
206 1.1 itohy #ifdef __STDC__
207 1.1 itohy size_t
208 1.1 itohy xsnprintf(char *buf, size_t len, const char *fmt, ...)
209 1.1 itohy #else
210 1.1 itohy size_t
211 1.1 itohy xsnprintf(buf, len, fmt, va_alist)
212 1.1 itohy char *buf;
213 1.1 itohy size_t len;
214 1.1 itohy const char *fmt;
215 1.1 itohy va_dcl
216 1.1 itohy #endif
217 1.1 itohy {
218 1.1 itohy va_list ap;
219 1.1 itohy size_t ret;
220 1.1 itohy
221 1.1 itohy VA_START(ap, fmt);
222 1.1 itohy ret = xvsnprintf(buf, len, fmt, ap);
223 1.1 itohy va_end(ap);
224 1.1 itohy
225 1.1 itohy return ret;
226 1.1 itohy }
227 1.1 itohy
228 1.1 itohy size_t
229 1.1 itohy xvfdprintf(fd, fmt, ap)
230 1.1 itohy int fd;
231 1.1 itohy const char *fmt;
232 1.1 itohy va_list ap;
233 1.1 itohy {
234 1.1 itohy char buf[PRINTF_BUFSZ];
235 1.1 itohy size_t ret;
236 1.1 itohy
237 1.1 itohy ret = xvsnprintf(buf, sizeof buf, fmt, ap);
238 1.1 itohy if (ret)
239 1.1 itohy ret = DOS_WRITE(fd, buf, ret);
240 1.1 itohy
241 1.1 itohy return ret;
242 1.1 itohy }
243 1.1 itohy
244 1.1 itohy #ifdef __STDC__
245 1.1 itohy size_t
246 1.1 itohy xprintf(const char *fmt, ...)
247 1.1 itohy #else
248 1.1 itohy size_t
249 1.1 itohy xprintf(fmt, va_alist)
250 1.1 itohy const char *fmt;
251 1.1 itohy va_dcl
252 1.1 itohy #endif
253 1.1 itohy {
254 1.1 itohy va_list ap;
255 1.1 itohy size_t ret;
256 1.1 itohy
257 1.1 itohy VA_START(ap, fmt);
258 1.1 itohy ret = xvfdprintf(1, fmt, ap);
259 1.1 itohy va_end(ap);
260 1.1 itohy
261 1.1 itohy return ret;
262 1.1 itohy }
263 1.1 itohy
264 1.1 itohy #ifdef __STDC__
265 1.1 itohy size_t
266 1.1 itohy xerrprintf(const char *fmt, ...)
267 1.1 itohy #else
268 1.1 itohy size_t
269 1.1 itohy xerrprintf(fmt, va_alist)
270 1.1 itohy const char *fmt;
271 1.1 itohy va_dcl
272 1.1 itohy #endif
273 1.1 itohy {
274 1.1 itohy va_list ap;
275 1.1 itohy size_t ret;
276 1.1 itohy
277 1.1 itohy VA_START(ap, fmt);
278 1.1 itohy ret = xvfdprintf(2, fmt, ap);
279 1.1 itohy va_end(ap);
280 1.1 itohy
281 1.1 itohy return ret;
282 1.1 itohy }
283 1.1 itohy
284 1.1 itohy __dead void
285 1.1 itohy #ifdef __STDC__
286 1.1 itohy xerr(int eval, const char *fmt, ...)
287 1.1 itohy #else
288 1.1 itohy xerr(eval, fmt, va_alist)
289 1.1 itohy int eval;
290 1.1 itohy const char *fmt;
291 1.1 itohy va_dcl
292 1.1 itohy #endif
293 1.1 itohy {
294 1.1 itohy int e = dos_errno;
295 1.1 itohy va_list ap;
296 1.1 itohy
297 1.1 itohy xerrprintf("%s: ", __progname);
298 1.1 itohy if (fmt) {
299 1.1 itohy VA_START(ap, fmt);
300 1.1 itohy xvfdprintf(2, fmt, ap);
301 1.1 itohy va_end(ap);
302 1.1 itohy xerrprintf(": ");
303 1.1 itohy }
304 1.1 itohy xerrprintf("%s\n", dos_strerror(e));
305 1.1 itohy DOS_EXIT2(eval);
306 1.1 itohy }
307 1.1 itohy
308 1.1 itohy __dead void
309 1.1 itohy #ifdef __STDC__
310 1.1 itohy xerrx(int eval, const char *fmt, ...)
311 1.1 itohy #else
312 1.1 itohy xerrx(eval, fmt, va_alist)
313 1.1 itohy int eval;
314 1.1 itohy const char *fmt;
315 1.1 itohy va_dcl
316 1.1 itohy #endif
317 1.1 itohy {
318 1.1 itohy va_list ap;
319 1.1 itohy
320 1.1 itohy xerrprintf("%s: ", __progname);
321 1.1 itohy if (fmt) {
322 1.1 itohy VA_START(ap, fmt);
323 1.1 itohy xvfdprintf(2, fmt, ap);
324 1.1 itohy va_end(ap);
325 1.1 itohy }
326 1.1 itohy xerrprintf("\n");
327 1.1 itohy DOS_EXIT2(eval);
328 1.1 itohy }
329 1.1 itohy
330 1.1 itohy void
331 1.1 itohy #ifdef __STDC__
332 1.1 itohy xwarn(const char *fmt, ...)
333 1.1 itohy #else
334 1.1 itohy xwarn(fmt, va_alist)
335 1.1 itohy const char *fmt;
336 1.1 itohy va_dcl
337 1.1 itohy #endif
338 1.1 itohy {
339 1.1 itohy int e = dos_errno;
340 1.1 itohy va_list ap;
341 1.1 itohy
342 1.1 itohy xerrprintf("%s: ", __progname);
343 1.1 itohy if (fmt) {
344 1.1 itohy VA_START(ap, fmt);
345 1.1 itohy xvfdprintf(2, fmt, ap);
346 1.1 itohy va_end(ap);
347 1.1 itohy xerrprintf(": ");
348 1.1 itohy }
349 1.1 itohy xerrprintf("%s\n", dos_strerror(e));
350 1.1 itohy }
351 1.1 itohy
352 1.1 itohy void
353 1.1 itohy #ifdef __STDC__
354 1.1 itohy xwarnx(const char *fmt, ...)
355 1.1 itohy #else
356 1.1 itohy xwarnx(fmt, va_alist)
357 1.1 itohy const char *fmt;
358 1.1 itohy va_dcl
359 1.1 itohy #endif
360 1.1 itohy {
361 1.1 itohy va_list ap;
362 1.1 itohy
363 1.1 itohy xerrprintf("%s: ", __progname);
364 1.1 itohy if (fmt) {
365 1.1 itohy VA_START(ap, fmt);
366 1.1 itohy xvfdprintf(2, fmt, ap);
367 1.1 itohy va_end(ap);
368 1.1 itohy }
369 1.1 itohy xerrprintf("\n");
370 1.1 itohy }
371