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
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
* All Rights Reserved.
*/
#include "libxfs.h"
#include "command.h"
#include "type.h"
#include "faddr.h"
#include "fprint.h"
#include "field.h"
#include "io.h"
#include "print.h"
#include "bit.h"
#include "flist.h"
#include "strvec.h"
#include "output.h"
#include "sig.h"
#include "write.h"
static void print_allfields(const struct field *fields);
static int print_f(int argc, char **argv);
static void print_flist_1(struct flist *flist, char **pfx, int parentoff);
static void print_somefields(const struct field *fields, int argc,
char **argv);
static const cmdinfo_t print_cmd =
{ "print", "p", print_f, 0, -1, 0, N_("[value]..."),
N_("print field values"), NULL };
static void
print_allfields(
const field_t *fields)
{
flist_t *flist;
#ifdef DEBUG
int i;
#endif
flist = flist_make("");
flist->fld = fields;
#ifndef DEBUG
(void)flist_parse(fields, flist, iocur_top->data, 0);
#else
i = flist_parse(fields, flist, iocur_top->data, 0);
ASSERT(i == 1);
#endif
flist_print(flist);
print_flist(flist);
flist_free(flist);
}
static int
print_f(
int argc,
char **argv)
{
pfunc_t pf;
if (cur_typ == NULL) {
dbprintf(_("no current type\n"));
return 0;
}
pf = cur_typ->pfunc;
if (pf == NULL) {
dbprintf(_("no print function for type %s\n"), cur_typ->name);
return 0;
}
argc--;
argv++;
(*pf)(DB_READ, cur_typ->fields, argc, argv);
return 0;
}
void
print_flist(
flist_t *flist)
{
char **pfx;
pfx = new_strvec(0);
print_flist_1(flist, pfx, 0);
free_strvec(pfx);
}
static void
print_flist_1(
flist_t *flist,
char **ppfx,
int parentoff)
{
char buf[16];
const field_t *f;
const ftattr_t *fa;
flist_t *fl;
int low;
int count;
int neednl;
char **pfx;
for (fl = flist; fl && !seenint(); fl = fl->sibling) {
pfx = copy_strvec(ppfx);
if (fl->name[0])
add_strvec(&pfx, fl->name);
if (fl->flags & FL_OKLOW) {
add_strvec(&pfx, "[");
snprintf(buf, sizeof(buf), "%d", fl->low);
add_strvec(&pfx, buf);
if (fl->low != fl->high) {
add_strvec(&pfx, "-");
snprintf(buf, sizeof(buf), "%d", fl->high);
add_strvec(&pfx, buf);
}
add_strvec(&pfx, "]");
}
if (fl->child) {
if (fl->name[0])
add_strvec(&pfx, ".");
print_flist_1(fl->child, pfx, fl->offset);
} else {
f = fl->fld;
fa = &ftattrtab[f->ftyp];
ASSERT(fa->ftyp == f->ftyp);
print_strvec(pfx);
dbprintf(" = ");
if (fl->flags & FL_OKLOW)
low = fl->low;
else
low = 0;
count = fcount(f, iocur_top->data, parentoff);
if (fl->flags & FL_OKHIGH)
count = min(count, fl->high - low + 1);
if (fa->prfunc) {
int fsz;
int bitlen;
/* Don't read an array off the end of the buffer */
fsz = fsize(f, iocur_top->data, parentoff, 0);
bitlen = iocur_top->len * NBBY;
if ((f->flags & FLD_ARRAY) &&
fl->offset + (count * fsz) > bitlen) {
count = (bitlen - fl->offset) / fsz;
}
neednl = fa->prfunc(iocur_top->data, fl->offset,
count, fa->fmtstr, fsz, fa->arg, low,
(f->flags & FLD_ARRAY) != 0);
if (neednl)
dbprintf("\n");
} else if (fa->arg & FTARG_OKEMPTY) {
dbprintf(_("(empty)\n"));
} else {
dbprintf(_("Unrecognized metadata or type mismatch\n"));
}
}
free_strvec(pfx);
}
}
void
print_init(void)
{
add_command(&print_cmd);
}
void
print_sarray(
void *obj,
int bit,
int count,
int size,
int base,
int array,
const field_t *flds,
int skipnms)
{
int bitoff;
const field_t *f;
const ftattr_t *fa;
int first;
int i;
ASSERT(bitoffs(bit) == 0);
if (skipnms == 0) {
for (f = flds, first = 1; f->name; f++) {
if (f->flags & FLD_SKIPALL)
continue;
dbprintf("%c%s", first ? '[' : ',', f->name);
first = 0;
}
dbprintf("] ");
}
for (i = 0, bitoff = bit;
i < count && !seenint();
i++, bitoff += size) {
if (array)
dbprintf("\n%d:", i + base);
for (f = flds, first = 1; f->name; f++) {
if (f->flags & FLD_SKIPALL)
continue;
fa = &ftattrtab[f->ftyp];
ASSERT(fa->ftyp == f->ftyp);
dbprintf("%c", first ? '[' : ',');
first = 0;
if (fa->prfunc)
fa->prfunc(obj,
bitoff +
bitoffset(f, obj, bitoff, i + base),
fcount(f, obj, bitoff), fa->fmtstr,
fsize(f, obj, bitoff, i + base),
fa->arg, (f->flags & FLD_ABASE1) != 0,
f->flags & FLD_ARRAY);
else {
ASSERT(fa->arg & FTARG_OKEMPTY);
dbprintf(_("(empty)"));
}
}
dbprintf("]");
if (i < count - 1)
dbprintf(" ");
}
}
static void
print_somefields(
const field_t *fields,
int argc,
char **argv)
{
const ftattr_t *fa;
flist_t *fl;
flist_t *lfl;
flist_t *nfl;
fl = lfl = NULL;
while (argc > 0) {
nfl = flist_scan(*argv);
if (!nfl) {
if (fl)
flist_free(fl);
return;
}
if (lfl)
lfl->sibling = nfl;
else
fl = nfl;
lfl = nfl;
argc--;
argv++;
}
if (fields->name[0] == '\0') {
fa = &ftattrtab[fields->ftyp];
ASSERT(fa->ftyp == fields->ftyp);
fields = fa->subfld;
}
if (!flist_parse(fields, fl, iocur_top->data, 0)) {
flist_free(fl);
return;
}
flist_print(fl);
print_flist(fl);
flist_free(fl);
}
/*ARGSUSED*/
void
print_string(
const field_t *fields,
int argc,
char **argv)
{
char *cp;
if (argc != 0)
dbprintf(_("no arguments allowed\n"));
dbprintf("\"");
for (cp = iocur_top->data;
cp < (char *)iocur_top->data + iocur_top->len && *cp &&
!seenint();
cp++)
dbprintf("%c", *cp);
dbprintf("\"\n");
}
void
print_struct(
const field_t *fields,
int argc,
char **argv)
{
if (argc == 0)
print_allfields(fields);
else
print_somefields(fields, argc, argv);
}
|