-
Notifications
You must be signed in to change notification settings - Fork 1
/
print.h
306 lines (268 loc) · 7.51 KB
/
print.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* My very own version of printf.
*
* This Works is placed under the terms of the Copyright Less License,
* see file COPYRIGHT.CLL. USE AT OWN RISK, ABSOLUTELY NO WARRANTY.
*
* 1) vprintf(str, va_list) is written as tino_print(ctx, "%v", TINO_VA_LIST)
* 2) There are no sideffects on the TINO_VA_LIST
* 3) The format is slightly different to printf!
*
* This currently is a stub!
* "%v" currently is only supported at the beginning or end of a format string!
* Everything else is handled with vsnprintf()
*
* Format: %[!][[+|-| ][0]xval][.xval][ext..][width][type]
* Format: %{future}
*
* !: argument is ignored (not rendered)
* width: hh h l ll
* xval: * or val
* val: Number or '(expr)'
* type:
* - % percent-character
* - c character
* - di Integer
* - n offset (stores number of characters output so far)
* - ouxX octal unsinged hex HEX
* - ps Pointer C-String
* types (NEW): ANSI/SUS/ISO uses so far: #%'+-0123456789 aAcCdeEfFgGhiIjlLmnopsStuxXz
* - P void *, size_t
* - V visual character: 'c', "'", NUL, etc.
* - v TINO_VA_LIST: v this is a recursion!
* - B TINO_BUF *
* ext:
* - @[val]: argument number for reordering. * is not supported here.
* %{future}:
* - Future extension, reserved
*
* Not supported:
*
* - ' I locales
* - aAeEfFgGL Floating Point
* - CjmqStz whatever
* - lc ls wchar_t
*/
/* @ extension explained:
*
* @ must come after size.
*
* @0 is the same as @ and denotes the same argument:
* tino_print(NULL, "Dec %d Octal %@o Hex %@x\n", 42);
*
* @* is not supported for apparently reasons. Else it works straight
* forward, a * in the size is fetched before the argument as usual
* and is not counted. Note that you must give the stars on the first
* definition, later you can leave them away. Example:
* tino_print(NULL, "Short %-*.*s: Long %-*@s\n", 5, 4, "thisisalongtext");
*
* There may not be any holes, following is an error:
* tino_print(NULL, "%@3d\n", "hello", 5, 42)
* To fix that with:
* tino_print(NULL, "%!s%!d%d\n", "hello", 5, 42)
* Also following would be ok then:
* tino_print(NULL, "%@3d %@2d %@1s\n", "hello", 5.5, 42)
*/
/* %v extension explained:
*
* A TINO_VA_LIST is fetched like any other object (this is a pointer).
*
* It then is rendered recursively (as the formatting is in the TINO_VA_LIST).
*
* A size is not yet supported.
*
* Indexing is not yet supported. Indexing might look like: %=5d
*/
#ifndef tino_INC_print_h
#define tino_INC_print_h
#include "io.h"
#include "buf_printf.h"
typedef struct tino_print_ctx *TINO_PRINT_CTX;
struct tino_print_ctx
{
TINO_BUF buf;
int err;
int (*fn)(TINO_PRINT_CTX, const void *, int); /* BUF,len>=0 | NULL,byte>=0 | NULL,-1 flush */
uintptr_t u;
};
static int
tino_print_buf(TINO_BUF *buf, const void *p, int l)
{
if (p)
tino_buf_add_nO(buf, p, l);
else if (l>=0)
tino_buf_add_cO(buf, l);
return 0;
}
static int
tino_print__self(TINO_PRINT_CTX c, const void *p, int l)
{
return tino_print_buf(&c->buf, p, l);
}
static int
tino_print_err(TINO_PRINT_CTX c, int err)
{
xDP(("(%p, %d)", c, err));
return err>=0
? c ? c->err : -1
: c ? c->err=err : err;
}
static int
tino_print__fn(TINO_PRINT_CTX c, const void *ptr, int len)
{
return !c || c->err
? tino_print_err(c, len)
: (c->err = (c->fn ? c->fn : tino_print__self)(c, ptr, len));
}
static int
tino_print__flush(TINO_PRINT_CTX c)
{
return tino_print__fn(c, NULL, -1);
}
static int
tino_print__c(TINO_PRINT_CTX c, char chr)
{
return tino_print__fn(c, NULL, ((int)chr)&0xff);
}
static int
tino_print__p(TINO_PRINT_CTX c, const void *s, int len)
{
return s && len>=0 ? tino_print__fn(c, s, len) : tino_print_err(c, len);
}
static int
tino_print__s(TINO_PRINT_CTX c, const char *s)
{
return tino_print__fn(c, s, strlen(s));
}
/* Initialize a context.
*
* Please note that C either can be NULL to allocate a context,
* or it must be pre-initialized with NULL.
*/
static TINO_PRINT_CTX
tino_print_ctxO(TINO_PRINT_CTX *c)
{
TINO_PRINT_CTX ctx;
if (c && *c)
return *c;
ctx = tino_alloc0O(sizeof *ctx);
if (c)
*c = ctx;
return ctx;
}
static void
tino_print_freeO(TINO_PRINT_CTX *c)
{
TINO_PRINT_CTX ctx;
if (!c || !*c) return;
ctx = *c;
*c = 0;
TINO_XXX;
tino_buf_freeO(&ctx->buf);
tino_freeO(ctx);
}
#define TINO_PRINT_CTX(TYPE, NAME, CODE) \
static int tino_print_fn_##NAME(TINO_PRINT_CTX ctx, const void *data, int len) { TYPE NAME=(TYPE)ctx->u; return CODE; } \
static TINO_PRINT_CTX \
tino_print_ctx_##NAME(TINO_PRINT_CTX c, TYPE NAME) \
{ \
tino_print_ctxO(&c); \
tino_buf_resetO(&c->buf); \
c->u = (uintptr_t)NAME; \
c->fn = tino_print_fn_##NAME; \
return c; \
}
TINO_PRINT_CTX(TINO_BUF *,buf, tino_print_buf(buf, data, len))
TINO_PRINT_CTX(FILE *,fd, (data ? len ? 1==fwrite(data,len,1, fd) : len : len>=0 ? EOF==fputc(len,fd) : fflush(fd)))
TINO_PRINT_CTX(int,io, data ? tino_io_write(io,data,len) : len>=0 ? tino_io_put(io,len) : tino_io_flush_write(io))
static TINO_PRINT_CTX tino_vprintO(TINO_PRINT_CTX c, TINO_VA_LIST list);
static TINO_PRINT_CTX
tino_vprint_(TINO_PRINT_CTX c, TINO_VA_LIST list)
{
const char *s;
xDP(("(%p %p) %p:'%s'%p", c, list, list, TINO_VA_STR(list), TINO_VA_GET(list)));
tino_print_ctxO(&c);
xDP(("() %p", c));
#if 0
for (s=TINO_VA_STR(list); *s; )
s = tino_vprint_step(c, s, list);
int len;
if (s[0]=='%' && s[1]=='%')
{
tino_print__c(c, '%');
s += 2;
continue;
}
len = tino_str_nclen(s, '%');
if (len>0)
{
tino_print__fn(c, s, len);
s += len;
continue;
}
{
len++;
s += len;
continue;
}
if (!s[len])
return;
}
#else
s = TINO_VA_STR(list);
while (tino_str_startswith(s, "%v"))
{
TINO_VA_LIST src;
src = TINO_VA_ARG(list, TINO_VA_LIST);
xDP(("() %%v=%p:'%s'%p", src, TINO_VA_STR(src), TINO_VA_GET(src)));
tino_vprintO(c, src);
s += 2;
}
if (!*s)
return c;
if (tino_str_endswith(s, "%v"))
{
char *tmp;
TINO_VA_LIST src;
tmp = tino_strdupO(s);
tmp[strlen(tmp)-2] = 0;
TINO_VA_STR(list) = tmp;
tino_vprint_(c, list);
tino_freeO(tmp);
TINO_VA_STR(list) = 0;
src = TINO_VA_ARG(list, TINO_VA_LIST);
xDP(("() %%v=%p:'%s'%p", src, TINO_VA_STR(src), TINO_VA_GET(src)));
return tino_vprintO(c, src);
}
/* adjust the format pointer */
TINO_VA_STR(list) = s;
/* Just do a normal printf for now */
s = tino_str_vprintf(list);
tino_print__s(c, s);
tino_free_constO(s);
#endif
return c;
}
static TINO_PRINT_CTX
tino_vprintO(TINO_PRINT_CTX c, TINO_VA_LIST list)
{
tino_va_list list2;
tino_va_copy(list2, *list);
xDP(("(%p %p) %p:'%s'%p %p:'%p'%p", c, list, list, TINO_VA_STR(list), TINO_VA_GET(list), &list2, tino_va_str(list2), tino_va_get(list2)));
c = tino_vprint_(c, &list2);
xDP(("() %p %p:'%s'%p %p:'%p'%p", c, list, TINO_VA_STR(list), TINO_VA_GET(list), &list2, tino_va_str(list2), tino_va_get(list2)));
tino_va_end(list2);
return c;
}
static TINO_PRINT_CTX
tino_printO(TINO_PRINT_CTX c, const char *format, ...)
{
tino_va_list list;
tino_va_start(list, format);
xDP(("(%p '%s') %p:'%s'%p", c, format, &list, tino_va_str(list), tino_va_get(list)));
c = tino_vprintO(c, &list);
xDP(("() %p %p:'%s'%p", c, &list, tino_va_str(list), tino_va_get(list)));
tino_va_end(list);
return c;
}
#endif