-
Notifications
You must be signed in to change notification settings - Fork 0
/
tstruct_handler.h
224 lines (178 loc) · 4.83 KB
/
tstruct_handler.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
/*
* Stage 1
*
* Define the structure and prototype
*/
#undef TSTRUCT
#define TSTRUCT(name, desc, entries) \
struct name { \
entries \
}; \
extern void print_ ## name(struct name *tstruct, int pad);
#undef TSTRUCT_entries
#define TSTRUCT_entries(args...) args
#undef __field
#define __field(type, name) \
type name;
#undef l_field
#define l_field(type, name) \
type name;
#undef _sfield
#define _sfield(type, name) \
type name;
#undef rsfield
#define rsfield(type, name) \
type name;
#undef __array
#define __array(type, name, elems) \
type name[elems];
#undef c_array
#define c_array(type, name, elems) \
type name[elems];
#undef s_array
#define s_array(type, name, elems) \
type name[elems];
#undef r_array
#define r_array(type, name, elems) \
type name[elems];
#undef v_array
#define v_array(type, name, elems) \
type name[elems];
#undef s_array_struct
#define s_array_struct(type, name, elems, field_cap) \
struct type name[elems];
#undef r_field
#define r_field(type, name) \
type name;
#undef __array_struct
#define __array_struct(type, name, elems, field_cap) \
struct type name[elems];
#include TSTRUCT_INCLUDE(TSTRUCT_INC_FILE)
#ifdef TSTRUCT_SOURCE
/*
* Stage 2
*
* Find the longest string for printing.
*
* Note: The compiler squashes the size.
*/
#undef TSTRUCT
#define TSTRUCT(name, desc, entries) \
static int name ## _pad(int pad)\
{ \
entries \
return pad; \
}
#undef TSTRUCT_entries
#define TSTRUCT_entries(args...) args
#undef __field
#define __field(type, name) \
pad = max(pad, sizeof(__stringify(name)));
#undef l_field
#define l_field(type, name) __field(type, name)
#undef _sfield
#define _sfield(type, name)
#undef rsfield
#define rsfield(type, name)
#undef r_field
#define r_field(type, name)
#undef __array
#define __array(type, name, elems) \
pad = max(pad, sizeof(__stringify(name)) + 5);
#undef c_array
#define c_array(type, name, elems) \
pad = max(pad, sizeof(__stringify(name)));
#undef s_array
#define s_array(type, name, elems) \
pad = max(pad, sizeof(__stringify(name)));
#undef r_array
#define r_array(type, name, elems)
#undef v_array
#define v_array(type, name, elems)
#undef __array_struct
#define __array_struct(type, name, elem, field_cap) \
pad = max(pad, max(type ## _pad(0), sizeof(__stringify(name)) + 4));
#undef s_array_struct
#define s_array_struct(type, name, elems, field_cap) \
pad = max(pad, max(type ## _pad(0), sizeof(__stringify(name)) + 4));
#include TSTRUCT_INCLUDE(TSTRUCT_INC_FILE)
/*
* Stage 3
*
* Print the structure
*/
#undef TSTRUCT
#define TSTRUCT(name, _desc, entries) \
static void __print_ ## name(struct name *tstruct, int pad, char **c) \
{ \
int i; \
const char *desc = _desc; \
pad = name ## _pad(pad); \
(void)i; \
if (desc) \
printf("%s:\n", desc); \
entries \
} \
void print_ ## name(struct name *tstruct, int pad) \
{ \
__print_ ## name(tstruct, pad, NULL); \
}
#undef TSTRUCT_entries
#define TSTRUCT_entries(args...) args
#define __field_fmt(name, fmt) \
printf("%-*s : "fmt"\n", pad, __stringify(name), tstruct->name);
#undef __field
#define __field(type, name) __field_fmt(name, "%x")
#undef l_field
#define l_field(type, name) __field_fmt(name, "%llx");
#undef _sfield
#define _sfield(type, name) \
if (c) \
*c += sprintf(*c, "%s:%x ", __stringify(name), tstruct->name); \
else \
printf(" %s:%x", __stringify(name), tstruct->name);
#undef rsfield
#define rsfield(type, name)
#undef r_field
#define r_field(type, name)
#undef __array
#define __array(type, name, elems) \
printf("%-*s :\n", pad, __stringify(name)); \
for (i = 0; i < elems; i++) \
printf("%-*s[%3d] : %lx\n", pad - 5, \
__stringify(name), i, \
(uint64_t)tstruct->name[i]);
#undef c_array
#define c_array(type, name, elems) \
printf("%-*s : %-.*s\n", pad, __stringify(name), \
elems, tstruct->name);
#undef s_array
#define s_array(type, name, elems) \
printf("%-*s : ", pad, __stringify(name)); \
for (i = 0; i < elems; i++) \
printf("%lx", (uint64_t)tstruct->name[i]); \
printf("\n");
#undef r_array
#define r_array(type, name, elems)
#undef v_array
#define v_array(type, name, elems)
#undef __array_struct
#define __array_struct(type, name, elems, field_cap) \
printf("%-*s :\n", pad, __stringify(name)); \
for (i = 0; i < tstruct->field_cap; i++) { \
printf("%-*s[%3d] :\n", pad - 5, __stringify(name), i); \
print_ ## type(&tstruct->name[i], pad); \
}
#undef s_array_struct
#define s_array_struct(type, name, elems, field_cap) \
for (i = 0; i < tstruct->field_cap; i++) { \
int start; \
char buf[1024]; \
char *c = buf; \
start = printf("%-*s[%3d] :", pad - 5, __stringify(name), i); \
__print_ ## type(&tstruct->name[i], 0, &c); \
c += sprintf(c, "\n"); \
print_word_wrapped(buf, pad + 3, start); \
}
#include TSTRUCT_INCLUDE(TSTRUCT_INC_FILE)
#endif