]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/print.c
libxfs: sanitize agcount on load
[thirdparty/xfsprogs-dev.git] / db / print.c
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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
7 * published by the Free Software Foundation.
8 *
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.
13 *
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
17 */
18
19 #include "libxfs.h"
20 #include "command.h"
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 =
41 { "print", "p", print_f, 0, -1, 0, N_("[value]..."),
42 N_("print field values"), NULL };
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) {
74 dbprintf(_("no current type\n"));
75 return 0;
76 }
77 pf = cur_typ->pfunc;
78 if (pf == NULL) {
79 dbprintf(_("no print function for type %s\n"), cur_typ->name);
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 count;
111 int neednl;
112 char **pfx;
113
114 for (fl = flist; fl && !seenint(); fl = fl->sibling) {
115 pfx = copy_strvec(ppfx);
116 if (fl->name[0])
117 add_strvec(&pfx, fl->name);
118 if (fl->flags & FL_OKLOW) {
119 add_strvec(&pfx, "[");
120 snprintf(buf, sizeof(buf), "%d", fl->low);
121 add_strvec(&pfx, buf);
122 if (fl->low != fl->high) {
123 add_strvec(&pfx, "-");
124 snprintf(buf, sizeof(buf), "%d", fl->high);
125 add_strvec(&pfx, buf);
126 }
127 add_strvec(&pfx, "]");
128 }
129 if (fl->child) {
130 if (fl->name[0])
131 add_strvec(&pfx, ".");
132 print_flist_1(fl->child, pfx, fl->offset);
133 } else {
134 f = fl->fld;
135 fa = &ftattrtab[f->ftyp];
136 ASSERT(fa->ftyp == f->ftyp);
137 print_strvec(pfx);
138 dbprintf(" = ");
139 if (fl->flags & FL_OKLOW)
140 low = fl->low;
141 else
142 low = 0;
143 count = fcount(f, iocur_top->data, parentoff);
144 if (fl->flags & FL_OKHIGH)
145 count = min(count, fl->high - low + 1);
146 if (fa->prfunc) {
147 neednl = fa->prfunc(iocur_top->data, fl->offset,
148 count, fa->fmtstr,
149 fsize(f, iocur_top->data, parentoff, 0),
150 fa->arg, low,
151 (f->flags & FLD_ARRAY) != 0);
152 if (neednl)
153 dbprintf("\n");
154 } else {
155 ASSERT(fa->arg & FTARG_OKEMPTY);
156 dbprintf(_("(empty)\n"));
157 }
158 }
159 free_strvec(pfx);
160 }
161 }
162
163 void
164 print_init(void)
165 {
166 add_command(&print_cmd);
167 }
168
169 void
170 print_sarray(
171 void *obj,
172 int bit,
173 int count,
174 int size,
175 int base,
176 int array,
177 const field_t *flds,
178 int skipnms)
179 {
180 int bitoff;
181 const field_t *f;
182 const ftattr_t *fa;
183 int first;
184 int i;
185
186 ASSERT(bitoffs(bit) == 0);
187 if (skipnms == 0) {
188 for (f = flds, first = 1; f->name; f++) {
189 if (f->flags & FLD_SKIPALL)
190 continue;
191 dbprintf("%c%s", first ? '[' : ',', f->name);
192 first = 0;
193 }
194 dbprintf("] ");
195 }
196 for (i = 0, bitoff = bit;
197 i < count && !seenint();
198 i++, bitoff += size) {
199 if (array)
200 dbprintf("\n%d:", i + base);
201 for (f = flds, first = 1; f->name; f++) {
202 if (f->flags & FLD_SKIPALL)
203 continue;
204 fa = &ftattrtab[f->ftyp];
205 ASSERT(fa->ftyp == f->ftyp);
206 dbprintf("%c", first ? '[' : ',');
207 first = 0;
208 if (fa->prfunc)
209 fa->prfunc(obj,
210 bitoff +
211 bitoffset(f, obj, bitoff, i + base),
212 fcount(f, obj, bitoff), fa->fmtstr,
213 fsize(f, obj, bitoff, i + base),
214 fa->arg, (f->flags & FLD_ABASE1) != 0,
215 f->flags & FLD_ARRAY);
216 else {
217 ASSERT(fa->arg & FTARG_OKEMPTY);
218 dbprintf(_("(empty)"));
219 }
220 }
221 dbprintf("]");
222 if (i < count - 1)
223 dbprintf(" ");
224 }
225 }
226
227 static void
228 print_somefields(
229 const field_t *fields,
230 int argc,
231 char **argv)
232 {
233 const ftattr_t *fa;
234 flist_t *fl;
235 flist_t *lfl;
236 flist_t *nfl;
237
238 fl = lfl = NULL;
239 while (argc > 0) {
240 nfl = flist_scan(*argv);
241 if (!nfl) {
242 if (fl)
243 flist_free(fl);
244 return;
245 }
246 if (lfl)
247 lfl->sibling = nfl;
248 else
249 fl = nfl;
250 lfl = nfl;
251 argc--;
252 argv++;
253 }
254 if (fields->name[0] == '\0') {
255 fa = &ftattrtab[fields->ftyp];
256 ASSERT(fa->ftyp == fields->ftyp);
257 fields = fa->subfld;
258 }
259 if (!flist_parse(fields, fl, iocur_top->data, 0)) {
260 flist_free(fl);
261 return;
262 }
263 flist_print(fl);
264 print_flist(fl);
265 flist_free(fl);
266 }
267
268 /*ARGSUSED*/
269 void
270 print_string(
271 const field_t *fields,
272 int argc,
273 char **argv)
274 {
275 char *cp;
276
277 if (argc != 0)
278 dbprintf(_("no arguments allowed\n"));
279 dbprintf("\"");
280 for (cp = iocur_top->data;
281 cp < (char *)iocur_top->data + iocur_top->len && *cp &&
282 !seenint();
283 cp++)
284 dbprintf("%c", *cp);
285 dbprintf("\"\n");
286 }
287
288 void
289 print_struct(
290 const field_t *fields,
291 int argc,
292 char **argv)
293 {
294 if (argc == 0)
295 print_allfields(fields);
296 else
297 print_somefields(fields, argc, argv);
298 }