]>
git.ipfire.org Git - thirdparty/pciutils.git/blob - lib/names-parse.c
2 * The PCI Library -- Parsing of the ID list
4 * Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
19 #ifdef PCI_COMPRESSED_IDS
21 typedef gzFile pci_file
;
22 #define pci_gets(f, l, s) gzgets(f, l, s)
23 #define pci_eof(f) gzeof(f)
25 static pci_file
pci_open(struct pci_access
*a
)
31 result
= gzopen(a
->id_file_name
, "rb");
34 len
= strlen(a
->id_file_name
);
35 if (len
< 3 || memcmp(a
->id_file_name
+ len
- 3, ".gz", 3) != 0)
37 new_name
= malloc(len
- 2);
38 memcpy(new_name
, a
->id_file_name
, len
- 3);
39 new_name
[len
- 3] = 0;
40 pci_set_name_list_path(a
, new_name
, 1);
41 return gzopen(a
->id_file_name
, "rb");
44 #define pci_close(f) gzclose(f)
45 #define PCI_ERROR(f, err) \
48 gzerror(f, &errnum); \
49 if (errnum >= 0) err = NULL; \
50 else if (errnum == Z_ERRNO) err = "I/O error"; \
51 else err = zError(errnum); \
54 typedef FILE * pci_file
;
55 #define pci_gets(f, l, s) fgets(l, s, f)
56 #define pci_eof(f) feof(f)
57 #define pci_open(a) fopen(a->id_file_name, "r")
58 #define pci_close(f) fclose(f)
59 #define PCI_ERROR(f, err) if (!err && ferror(f)) err = "I/O error";
62 static int id_hex(char *p
, int cnt
)
68 if (*p
>= '0' && *p
<= '9')
70 else if (*p
>= 'a' && *p
<= 'f')
72 else if (*p
>= 'A' && *p
<= 'F')
81 static inline int id_white_p(int c
)
83 return (c
== ' ') || (c
== '\t');
87 static const char *id_parse_list(struct pci_access
*a
, pci_file f
, int *lino
)
91 int id1
=0, id2
=0, id3
=0, id4
=0;
94 static const char parse_error
[] = "Parse error";
97 while (pci_gets(f
, line
, sizeof(line
)))
101 while (*p
&& *p
!= '\n' && *p
!= '\r')
103 if (!*p
&& !pci_eof(f
))
104 return "Line too long";
106 if (p
> line
&& (p
[-1] == ' ' || p
[-1] == '\t'))
110 while (id_white_p(*p
))
112 if (!*p
|| *p
== '#')
120 if (!nest
) /* Top-level entries */
122 if (p
[0] == 'C' && p
[1] == ' ') /* Class block */
124 if ((id1
= id_hex(p
+2, 2)) < 0 || !id_white_p(p
[4]))
129 else if (p
[0] == 'S' && p
[1] == ' ')
130 { /* Generic subsystem block */
131 if ((id1
= id_hex(p
+2, 4)) < 0 || p
[6])
133 if (!pci_id_lookup(a
, 0, ID_VENDOR
, id1
, 0, 0, 0))
134 return "Vendor does not exist";
135 cat
= ID_GEN_SUBSYSTEM
;
138 else if (p
[0] >= 'A' && p
[0] <= 'Z' && p
[1] == ' ')
139 { /* Unrecognized block (RFU) */
145 if ((id1
= id_hex(p
, 4)) < 0 || !id_white_p(p
[4]))
152 else if (cat
== ID_UNKNOWN
) /* Nested entries in RFU blocks are skipped */
154 else if (nest
== 1) /* Nesting level 1 */
160 if ((id2
= id_hex(p
, 4)) < 0 || !id_white_p(p
[4]))
166 case ID_GEN_SUBSYSTEM
:
167 if ((id2
= id_hex(p
, 4)) < 0 || !id_white_p(p
[4]))
175 if ((id2
= id_hex(p
, 2)) < 0 || !id_white_p(p
[2]))
184 else if (nest
== 2) /* Nesting level 2 */
189 if ((id3
= id_hex(p
, 4)) < 0 || !id_white_p(p
[4]) || (id4
= id_hex(p
+5, 4)) < 0 || !id_white_p(p
[9]))
197 if ((id3
= id_hex(p
, 2)) < 0 || !id_white_p(p
[2]))
206 else /* Nesting level 3 or more */
208 while (id_white_p(*p
))
212 if (pci_id_insert(a
, cat
, id1
, id2
, id3
, id4
, p
, SRC_LOCAL
))
213 return "Duplicate entry";
219 pci_load_name_list(struct pci_access
*a
)
225 pci_free_name_list(a
);
226 a
->id_load_attempted
= 1;
227 if (!(f
= pci_open(a
)))
229 err
= id_parse_list(a
, f
, &lino
);
233 a
->error("%s at %s, line %d\n", err
, a
->id_file_name
, lino
);
238 pci_free_name_list(struct pci_access
*a
)
240 pci_id_cache_flush(a
);
243 a
->id_load_attempted
= 0;
247 pci_set_name_list_path(struct pci_access
*a
, char *name
, int to_be_freed
)
250 free(a
->id_file_name
);
251 a
->id_file_name
= name
;
252 a
->free_id_name
= to_be_freed
;