]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
da23017d NS |
2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
dfc130f3 | 4 | * |
da23017d NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
2bd0ea18 | 7 | * published by the Free Software Foundation. |
dfc130f3 | 8 | * |
da23017d NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
dfc130f3 | 13 | * |
da23017d NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
2bd0ea18 NS |
17 | */ |
18 | ||
6b803e5a | 19 | #include "libxfs.h" |
2bd0ea18 | 20 | #include "command.h" |
2bd0ea18 NS |
21 | #include "type.h" |
22 | #include "faddr.h" | |
23 | #include "fprint.h" | |
24 | #include "field.h" | |
25 | #include "io.h" | |
26 | #include "print.h" | |
27 | #include "bit.h" | |
28 | #include "flist.h" | |
29 | #include "strvec.h" | |
30 | #include "output.h" | |
31 | #include "sig.h" | |
32 | #include "write.h" | |
33 | ||
34 | static void print_allfields(const struct field *fields); | |
35 | static int print_f(int argc, char **argv); | |
36 | static void print_flist_1(struct flist *flist, char **pfx, int parentoff); | |
37 | static void print_somefields(const struct field *fields, int argc, | |
38 | char **argv); | |
39 | ||
40 | static const cmdinfo_t print_cmd = | |
9ee7055c AM |
41 | { "print", "p", print_f, 0, -1, 0, N_("[value]..."), |
42 | N_("print field values"), NULL }; | |
2bd0ea18 NS |
43 | |
44 | static void | |
45 | print_allfields( | |
46 | const field_t *fields) | |
47 | { | |
48 | flist_t *flist; | |
49 | #ifdef DEBUG | |
50 | int i; | |
51 | #endif | |
52 | ||
53 | flist = flist_make(""); | |
54 | flist->fld = fields; | |
55 | #ifndef DEBUG | |
56 | (void)flist_parse(fields, flist, iocur_top->data, 0); | |
57 | #else | |
58 | i = flist_parse(fields, flist, iocur_top->data, 0); | |
59 | ASSERT(i == 1); | |
60 | #endif | |
61 | flist_print(flist); | |
62 | print_flist(flist); | |
63 | flist_free(flist); | |
64 | } | |
65 | ||
66 | static int | |
67 | print_f( | |
68 | int argc, | |
69 | char **argv) | |
70 | { | |
71 | pfunc_t pf; | |
72 | ||
73 | if (cur_typ == NULL) { | |
9ee7055c | 74 | dbprintf(_("no current type\n")); |
2bd0ea18 NS |
75 | return 0; |
76 | } | |
77 | pf = cur_typ->pfunc; | |
78 | if (pf == NULL) { | |
9ee7055c | 79 | dbprintf(_("no print function for type %s\n"), cur_typ->name); |
2bd0ea18 NS |
80 | return 0; |
81 | } | |
82 | argc--; | |
83 | argv++; | |
84 | (*pf)(DB_READ, cur_typ->fields, argc, argv); | |
85 | return 0; | |
86 | } | |
87 | ||
88 | void | |
89 | print_flist( | |
90 | flist_t *flist) | |
91 | { | |
92 | char **pfx; | |
93 | ||
94 | pfx = new_strvec(0); | |
95 | print_flist_1(flist, pfx, 0); | |
96 | free_strvec(pfx); | |
97 | } | |
98 | ||
99 | static void | |
100 | print_flist_1( | |
101 | flist_t *flist, | |
102 | char **ppfx, | |
103 | int parentoff) | |
104 | { | |
105 | char buf[16]; | |
106 | const field_t *f; | |
107 | const ftattr_t *fa; | |
108 | flist_t *fl; | |
109 | int low; | |
110 | int neednl; | |
111 | char **pfx; | |
112 | ||
113 | for (fl = flist; fl && !seenint(); fl = fl->sibling) { | |
114 | pfx = copy_strvec(ppfx); | |
115 | if (fl->name[0]) | |
116 | add_strvec(&pfx, fl->name); | |
117 | if (fl->flags & FL_OKLOW) { | |
118 | add_strvec(&pfx, "["); | |
2d9475a4 | 119 | snprintf(buf, sizeof(buf), "%d", fl->low); |
2bd0ea18 NS |
120 | add_strvec(&pfx, buf); |
121 | if (fl->low != fl->high) { | |
122 | add_strvec(&pfx, "-"); | |
2d9475a4 | 123 | snprintf(buf, sizeof(buf), "%d", fl->high); |
2bd0ea18 NS |
124 | add_strvec(&pfx, buf); |
125 | } | |
126 | add_strvec(&pfx, "]"); | |
127 | } | |
128 | if (fl->child) { | |
129 | if (fl->name[0]) | |
130 | add_strvec(&pfx, "."); | |
131 | print_flist_1(fl->child, pfx, fl->offset); | |
132 | } else { | |
133 | f = fl->fld; | |
134 | fa = &ftattrtab[f->ftyp]; | |
135 | ASSERT(fa->ftyp == f->ftyp); | |
136 | print_strvec(pfx); | |
137 | dbprintf(" = "); | |
138 | if (fl->flags & FL_OKLOW) | |
139 | low = fl->low; | |
140 | else | |
141 | low = 0; | |
142 | if (fa->prfunc) { | |
143 | neednl = fa->prfunc(iocur_top->data, fl->offset, | |
144 | fcount(f, iocur_top->data, parentoff), | |
145 | fa->fmtstr, | |
146 | fsize(f, iocur_top->data, parentoff, 0), | |
147 | fa->arg, low, | |
148 | (f->flags & FLD_ARRAY) != 0); | |
149 | if (neednl) | |
150 | dbprintf("\n"); | |
151 | } else { | |
152 | ASSERT(fa->arg & FTARG_OKEMPTY); | |
9ee7055c | 153 | dbprintf(_("(empty)\n")); |
2bd0ea18 NS |
154 | } |
155 | } | |
156 | free_strvec(pfx); | |
157 | } | |
158 | } | |
159 | ||
160 | void | |
161 | print_init(void) | |
162 | { | |
163 | add_command(&print_cmd); | |
164 | } | |
165 | ||
166 | void | |
167 | print_sarray( | |
168 | void *obj, | |
169 | int bit, | |
170 | int count, | |
171 | int size, | |
172 | int base, | |
173 | int array, | |
174 | const field_t *flds, | |
175 | int skipnms) | |
176 | { | |
177 | int bitoff; | |
178 | const field_t *f; | |
179 | const ftattr_t *fa; | |
180 | int first; | |
181 | int i; | |
182 | ||
183 | ASSERT(bitoffs(bit) == 0); | |
184 | if (skipnms == 0) { | |
185 | for (f = flds, first = 1; f->name; f++) { | |
186 | if (f->flags & FLD_SKIPALL) | |
187 | continue; | |
188 | dbprintf("%c%s", first ? '[' : ',', f->name); | |
189 | first = 0; | |
190 | } | |
191 | dbprintf("] "); | |
192 | } | |
193 | for (i = 0, bitoff = bit; | |
194 | i < count && !seenint(); | |
195 | i++, bitoff += size) { | |
196 | if (array) | |
197 | dbprintf("%d:", i + base); | |
198 | for (f = flds, first = 1; f->name; f++) { | |
199 | if (f->flags & FLD_SKIPALL) | |
200 | continue; | |
201 | fa = &ftattrtab[f->ftyp]; | |
202 | ASSERT(fa->ftyp == f->ftyp); | |
203 | dbprintf("%c", first ? '[' : ','); | |
204 | first = 0; | |
205 | if (fa->prfunc) | |
206 | fa->prfunc(obj, | |
207 | bitoff + | |
208 | bitoffset(f, obj, bitoff, i + base), | |
209 | fcount(f, obj, bitoff), fa->fmtstr, | |
210 | fsize(f, obj, bitoff, i + base), | |
211 | fa->arg, (f->flags & FLD_ABASE1) != 0, | |
212 | f->flags & FLD_ARRAY); | |
213 | else { | |
214 | ASSERT(fa->arg & FTARG_OKEMPTY); | |
9ee7055c | 215 | dbprintf(_("(empty)")); |
2bd0ea18 NS |
216 | } |
217 | } | |
218 | dbprintf("]"); | |
219 | if (i < count - 1) | |
220 | dbprintf(" "); | |
221 | } | |
222 | } | |
223 | ||
224 | static void | |
225 | print_somefields( | |
226 | const field_t *fields, | |
227 | int argc, | |
228 | char **argv) | |
229 | { | |
230 | const ftattr_t *fa; | |
231 | flist_t *fl; | |
232 | flist_t *lfl; | |
233 | flist_t *nfl; | |
234 | ||
235 | fl = lfl = NULL; | |
236 | while (argc > 0) { | |
237 | nfl = flist_scan(*argv); | |
238 | if (!nfl) { | |
239 | if (fl) | |
240 | flist_free(fl); | |
241 | return; | |
242 | } | |
243 | if (lfl) | |
244 | lfl->sibling = nfl; | |
245 | else | |
246 | fl = nfl; | |
247 | lfl = nfl; | |
248 | argc--; | |
249 | argv++; | |
250 | } | |
251 | if (fields->name[0] == '\0') { | |
252 | fa = &ftattrtab[fields->ftyp]; | |
253 | ASSERT(fa->ftyp == fields->ftyp); | |
254 | fields = fa->subfld; | |
255 | } | |
256 | if (!flist_parse(fields, fl, iocur_top->data, 0)) { | |
257 | flist_free(fl); | |
258 | return; | |
259 | } | |
260 | flist_print(fl); | |
261 | print_flist(fl); | |
262 | flist_free(fl); | |
263 | } | |
264 | ||
265 | /*ARGSUSED*/ | |
266 | void | |
267 | print_string( | |
268 | const field_t *fields, | |
269 | int argc, | |
270 | char **argv) | |
271 | { | |
272 | char *cp; | |
273 | ||
274 | if (argc != 0) | |
9ee7055c | 275 | dbprintf(_("no arguments allowed\n")); |
2bd0ea18 NS |
276 | dbprintf("\""); |
277 | for (cp = iocur_top->data; | |
278 | cp < (char *)iocur_top->data + iocur_top->len && *cp && | |
279 | !seenint(); | |
280 | cp++) | |
281 | dbprintf("%c", *cp); | |
282 | dbprintf("\"\n"); | |
283 | } | |
284 | ||
285 | void | |
286 | print_struct( | |
287 | const field_t *fields, | |
288 | int argc, | |
289 | char **argv) | |
290 | { | |
291 | if (argc == 0) | |
292 | print_allfields(fields); | |
293 | else | |
294 | print_somefields(fields, argc, argv); | |
295 | } |