]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/flist.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
17 static void flist_expand_arrays(flist_t
*fl
);
18 static void flist_expand_structs(flist_t
*fl
, void *obj
);
19 static flist_t
*flist_replicate(flist_t
*fl
);
20 static ftok_t
*flist_split(char *s
);
21 static void ftok_free(ftok_t
*ft
);
40 fa
= &ftattrtab
[f
->ftyp
];
42 ASSERT(fa
->ftyp
== f
->ftyp
);
43 ASSERT(f
->flags
& FLD_ARRAY
);
48 for (idx
= low
+ 1, prev
= fl
; idx
<= high
; idx
++) {
49 new = flist_make(f
->name
);
51 new->low
= new->high
= idx
;
52 new->flags
|= FL_OKLOW
| FL_OKHIGH
;
53 new->child
= flist_replicate(fl
->child
);
72 fa
= &ftattrtab
[f
->ftyp
];
73 ASSERT(fa
->ftyp
== f
->ftyp
);
74 ASSERT(fa
->subfld
!= NULL
);
75 ASSERT(fl
->child
== NULL
);
76 for (cf
= fa
->subfld
, prev
= NULL
; cf
->name
!= NULL
; cf
++) {
77 if (fcount(cf
, obj
, fl
->offset
) == 0)
79 if (cf
->flags
& FLD_SKIPALL
)
81 new = flist_make(cf
->name
);
96 flist_free(fl
->child
);
98 flist_free(fl
->sibling
);
110 fl
= xmalloc(sizeof(*fl
));
111 fl
->name
= xstrdup(name
);
124 const field_t
*fields
,
135 f
= findfield(fl
->name
, fields
, obj
, startoff
);
137 dbprintf(_("field %s not found\n"), fl
->name
);
141 fa
= &ftattrtab
[f
->ftyp
];
142 ASSERT(fa
->ftyp
== f
->ftyp
);
143 if (f
->flags
& FLD_ARRAY
) {
144 low
= (f
->flags
& FLD_ABASE1
) != 0;
145 high
= fcount(f
, obj
, startoff
) + low
- 1;
147 dbprintf(_("no elements in %s\n"), fl
->name
);
150 if (fl
->flags
& FL_OKHIGH
) {
151 if (fl
->low
< low
|| fl
->low
> high
||
152 fl
->high
< low
|| fl
->high
> high
) {
153 dbprintf(_("indices %d-%d for field %s "
154 "out of range %d-%d\n"),
155 fl
->low
, fl
->high
, fl
->name
,
159 } else if (fl
->flags
& FL_OKLOW
) {
160 if (fl
->low
< low
|| fl
->low
> high
) {
161 dbprintf(_("index %d for field %s out of "
163 fl
->low
, fl
->name
, low
, high
);
167 fl
->flags
|= FL_OKHIGH
;
171 fl
->flags
|= FL_OKLOW
| FL_OKHIGH
;
174 if (fl
->flags
& FL_OKLOW
) {
175 dbprintf(_("field %s is not an array\n"),
180 fl
->offset
= startoff
+ bitoffset(f
, obj
, startoff
, fl
->low
);
181 if ((fl
->child
!= NULL
|| fa
->prfunc
== NULL
) &&
182 (f
->flags
& FLD_ARRAY
) && fl
->low
!= fl
->high
)
183 flist_expand_arrays(fl
);
184 if (fa
->prfunc
== NULL
&& fl
->child
== NULL
)
185 flist_expand_structs(fl
, obj
);
187 if (fa
->subfld
== NULL
) {
188 dbprintf(_("field %s has no subfields\n"),
192 if (!flist_parse(fa
->subfld
, fl
->child
, obj
,
205 if (!(debug_state
& DEBUG_FLIST
))
208 dbprintf(_("fl@%p:\n"), fl
);
209 dbprintf(_("\tname=%s, fld=%p, child=%p, sibling=%p\n"),
210 fl
->name
, fl
->fld
, fl
->child
, fl
->sibling
);
211 dbprintf(_("\tlow=%d, high=%d, flags=%d (%s%s), offset=%d\n"),
212 fl
->low
, fl
->high
, fl
->flags
,
213 fl
->flags
& FL_OKLOW
? _("oklow ") : "",
214 fl
->flags
& FL_OKHIGH
? _("okhigh") : "", fl
->offset
);
215 dbprintf(_("\tfld->name=%s, fld->ftyp=%d (%s)\n"),
216 fl
->fld
->name
, fl
->fld
->ftyp
,
217 ftattrtab
[fl
->fld
->ftyp
].name
);
218 dbprintf(_("\tfld->flags=%d (%s%s%s%s%s)\n"), fl
->fld
->flags
,
219 fl
->fld
->flags
& FLD_ABASE1
? "abase1 " : "",
220 fl
->fld
->flags
& FLD_SKIPALL
? "skipall " : "",
221 fl
->fld
->flags
& FLD_ARRAY
? "array " : "",
222 fl
->fld
->flags
& FLD_OFFSET
? "offset " : "",
223 fl
->fld
->flags
& FLD_COUNT
? "count " : "");
225 flist_print(fl
->child
);
238 new = flist_make(f
->name
);
240 new->child
= flist_replicate(f
->child
);
241 new->sibling
= flist_replicate(f
->sibling
);
244 new->flags
= f
->flags
;
245 new->offset
= f
->offset
;
261 v
= flist_split(name
);
266 while (p
->tokty
!= TT_END
) {
267 if (p
->tokty
!= TT_NAME
)
269 nfl
= flist_make(p
->tok
);
276 if (p
->tokty
== TT_LB
) {
278 if (p
->tokty
!= TT_NUM
)
280 num
= (int)strtoul(p
->tok
, &x
, 0);
283 nfl
->flags
|= FL_OKLOW
;
286 if (p
->tokty
== TT_DASH
) {
288 if (p
->tokty
!= TT_NUM
)
290 num
= (int)strtoul(p
->tok
, &x
, 0);
293 nfl
->flags
|= FL_OKHIGH
;
297 if (p
->tokty
!= TT_RB
)
301 if (p
->tokty
== TT_DOT
) {
303 if (p
->tokty
== TT_END
)
310 dbprintf(_("bad syntax in field name %s\n"), name
);
323 static char *idchars
;
324 static char *initidchar
;
327 static char *numchars
;
328 static char *xnumchars
; /* extended for hex conversion */
330 static char punctchars
[] = "[-].";
331 static tokty_t puncttypes
[] = { TT_LB
, TT_DASH
, TT_RB
, TT_DOT
};
335 if (idchars
== NULL
) {
336 idchars
= xmalloc(26 + 10 + 1 + 1);
337 initidchar
= xmalloc(26 + 1);
338 numchars
= xmalloc(10 + 1);
339 xnumchars
= xmalloc(12 + 1);
340 for (i
= 'a'; i
<= 'z'; i
++) {
341 idchars
[i
- 'a'] = i
;
342 initidchar
[i
- 'a'] = i
;
345 for (i
= '0'; i
<= '9'; i
++) {
346 idchars
[26 + (i
- '0')] = i
;
347 numchars
[i
- '0'] = i
;
348 xnumchars
[i
- '0'] = i
;
350 idchars
[26 + 10] = '_';
351 idchars
[26 + 10 + 1] = '\0';
352 initidchar
[26] = '\0';
356 xnumchars
[12] = '\0';
359 v
= xmalloc(sizeof(*v
));
362 /* need to add string handling */
364 s
++; /* skip first quote */
365 if ((a
= strrchr(s
, '\"')) == NULL
) {
366 dbprintf(_("missing closing quote %s\n"), s
);
370 tailskip
= 1; /* skip remaing quote */
373 } else if (strchr(initidchar
, *s
)) {
374 l
= (int)strspn(s
, idchars
);
376 } else if (strchr(numchars
, *s
)) {
377 l
= (int)strspn(s
, xnumchars
);
379 } else if ((a
= strchr(punctchars
, *s
))) {
381 t
= puncttypes
[a
- punctchars
];
383 dbprintf(_("bad character in field %s\n"), s
);
390 v
= xrealloc(v
, (nv
+ 2) * sizeof(*v
));
391 v
[nv
+ 1].tok
= NULL
;
399 v
[nv
].tokty
= TT_END
;
405 const field_t
*field
,
415 fl
= flist_make(field
->name
);
417 if (field
->ftyp
== type
)
419 count
= fcount(field
, obj
, startoff
);
422 fa
= &ftattrtab
[field
->ftyp
];
426 nfl
= flist_find_ftyp(fa
->subfld
, type
, obj
, startoff
);
438 * Given a set of fields, scan for a field of the given type.
439 * Return an flist leading to the first found field
441 * Return NULL if no field of the given type is found.
445 const field_t
*fields
,
453 for (f
= fields
; f
->name
; f
++) {
454 fl
= flist_field_match(f
, type
, obj
, startoff
);
468 for (p
= ft
; p
->tok
; p
++)