]> git.ipfire.org Git - thirdparty/pciutils.git/blob - common.c
libpci: win32-cfgmgr32: Fix parsing paths in NT format
[thirdparty/pciutils.git] / common.c
1 /*
2 * The PCI Utilities -- Common Functions
3 *
4 * Copyright (c) 1997--2016 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13
14 #include "pciutils.h"
15
16 void NONRET
17 die(char *msg, ...)
18 {
19 va_list args;
20
21 va_start(args, msg);
22 fprintf(stderr, "%s: ", program_name);
23 vfprintf(stderr, msg, args);
24 fputc('\n', stderr);
25 exit(1);
26 }
27
28 void *
29 xmalloc(size_t howmuch)
30 {
31 void *p = malloc(howmuch);
32 if (!p)
33 die("Unable to allocate %d bytes of memory", (int) howmuch);
34 return p;
35 }
36
37 void *
38 xrealloc(void *ptr, size_t howmuch)
39 {
40 void *p = realloc(ptr, howmuch);
41 if (!p)
42 die("Unable to allocate %d bytes of memory", (int) howmuch);
43 return p;
44 }
45
46 char *
47 xstrdup(const char *str)
48 {
49 int len = strlen(str) + 1;
50 char *copy = xmalloc(len);
51 memcpy(copy, str, len);
52 return copy;
53 }
54
55 static void
56 set_pci_method(struct pci_access *pacc, char *arg)
57 {
58 char *name;
59 int i;
60
61 if (!strcmp(arg, "help"))
62 {
63 printf("Known PCI access methods:\n\n");
64 for (i=0; name = pci_get_method_name(i); i++)
65 if (name[0])
66 printf("%s\n", name);
67 exit(0);
68 }
69 else
70 {
71 i = pci_lookup_method(arg);
72 if (i < 0)
73 die("No such PCI access method: %s (see `-A help' for a list)", arg);
74 pacc->method = i;
75 }
76 }
77
78 static void
79 set_pci_option(struct pci_access *pacc, char *arg)
80 {
81 if (!strcmp(arg, "help"))
82 {
83 struct pci_param *p;
84 printf("Known PCI access parameters:\n\n");
85 for (p=NULL; p=pci_walk_params(pacc, p);)
86 printf("%-20s %s (%s)\n", p->param, p->help, p->value);
87 exit(0);
88 }
89 else
90 {
91 char *sep = strchr(arg, '=');
92 if (!sep)
93 die("Invalid PCI access parameter syntax: %s", arg);
94 *sep++ = 0;
95 if (pci_set_param(pacc, arg, sep) < 0)
96 die("Unrecognized PCI access parameter: %s (see `-O help' for a list)", arg);
97 }
98 }
99
100 int
101 parse_generic_option(int i, struct pci_access *pacc, char *arg)
102 {
103 switch (i)
104 {
105 #ifdef PCI_HAVE_PM_INTEL_CONF
106 case 'H':
107 if (!strcmp(arg, "1"))
108 pacc->method = PCI_ACCESS_I386_TYPE1;
109 else if (!strcmp(arg, "2"))
110 pacc->method = PCI_ACCESS_I386_TYPE2;
111 else
112 die("Unknown hardware configuration type %s", arg);
113 break;
114 #endif
115 #ifdef PCI_HAVE_PM_DUMP
116 case 'F':
117 pci_set_param(pacc, "dump.name", arg);
118 pacc->method = PCI_ACCESS_DUMP;
119 break;
120 #endif
121 case 'A':
122 set_pci_method(pacc, arg);
123 break;
124 case 'G':
125 pacc->debugging++;
126 break;
127 case 'O':
128 set_pci_option(pacc, arg);
129 break;
130 default:
131 return 0;
132 }
133 return 1;
134 }