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.
17 #ifdef PCI_COMPRESSED_IDS
19 typedef gzFile pci_file;
20 #define pci_gets(f, l, s) gzgets(f, l, s)
21 #define pci_eof(f) gzeof(f)
23 static pci_file pci_open(struct pci_access *a)
29 result = gzopen(a->id_file_name, "rb");
32 len = strlen(a->id_file_name);
33 if (len < 3 || memcmp(a->id_file_name + len - 3, ".gz", 3) != 0)
35 new_name = malloc(len - 2);
36 memcpy(new_name, a->id_file_name, len - 3);
37 new_name[len - 3] = 0;
38 pci_set_name_list_path(a, new_name, 1);
39 return gzopen(a->id_file_name, "rb");
42 #define pci_close(f) gzclose(f)
43 #define PCI_ERROR(f, err) \
46 gzerror(f, &errnum); \
47 if (errnum >= 0) err = NULL; \
48 else if (errnum == Z_ERRNO) err = "I/O error"; \
49 else err = zError(errnum); \
52 typedef FILE * pci_file;
53 #define pci_gets(f, l, s) fgets(l, s, f)
54 #define pci_eof(f) feof(f)
55 #define pci_open(a) fopen(a->id_file_name, "r")
56 #define pci_close(f) fclose(f)
57 #define PCI_ERROR(f, err) if (!err && ferror(f)) err = "I/O error";
60 static int id_hex(char *p, int cnt)
66 if (*p >= '0' && *p <= '9')
68 else if (*p >= 'a' && *p <= 'f')
70 else if (*p >= 'A' && *p <= 'F')
79 static inline int id_white_p(int c)
81 return (c == ' ') || (c == '\t');
85 static const char *id_parse_list(struct pci_access *a, pci_file f, int *lino)
89 int id1=0, id2=0, id3=0, id4=0;
92 static const char parse_error[] = "Parse error";
95 while (pci_gets(f, line, sizeof(line)))
99 while (*p && *p != '\n' && *p != '\r')
101 if (!*p && !pci_eof(f))
102 return "Line too long";
104 if (p > line && (p[-1] == ' ' || p[-1] == '\t'))
108 while (id_white_p(*p))
110 if (!*p || *p == '#')
118 if (!nest) /* Top-level entries */
120 if (p[0] == 'C' && p[1] == ' ') /* Class block */
122 if ((id1 = id_hex(p+2, 2)) < 0 || !id_white_p(p[4]))
127 else if (p[0] == 'S' && p[1] == ' ')
128 { /* Generic subsystem block */
129 if ((id1 = id_hex(p+2, 4)) < 0 || p[6])
131 if (!pci_id_lookup(a, 0, ID_VENDOR, id1, 0, 0, 0))
132 return "Vendor does not exist";
133 cat = ID_GEN_SUBSYSTEM;
136 else if (p[0] >= 'A' && p[0] <= 'Z' && p[1] == ' ')
137 { /* Unrecognized block (RFU) */
143 if ((id1 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
150 else if (cat == ID_UNKNOWN) /* Nested entries in RFU blocks are skipped */
152 else if (nest == 1) /* Nesting level 1 */
158 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
164 case ID_GEN_SUBSYSTEM:
165 if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
173 if ((id2 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
182 else if (nest == 2) /* Nesting level 2 */
187 if ((id3 = id_hex(p, 4)) < 0 || !id_white_p(p[4]) || (id4 = id_hex(p+5, 4)) < 0 || !id_white_p(p[9]))
195 if ((id3 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
204 else /* Nesting level 3 or more */
206 while (id_white_p(*p))
210 if (pci_id_insert(a, cat, id1, id2, id3, id4, p, SRC_LOCAL))
211 return "Duplicate entry";
217 pci_load_name_list(struct pci_access *a)
223 pci_free_name_list(a);
224 a->id_load_failed = 1;
225 if (!(f = pci_open(a)))
227 err = id_parse_list(a, f, &lino);
231 a->error("%s at %s, line %d\n", err, a->id_file_name, lino);
232 a->id_load_failed = 0;
237 pci_free_name_list(struct pci_access *a)
239 pci_id_cache_flush(a);
241 a->id_load_failed = 0;
245 pci_set_name_list_path(struct pci_access *a, char *name, int to_be_freed)
248 free(a->id_file_name);
249 a->id_file_name = name;
250 a->free_id_name = to_be_freed;