]> git.ipfire.org Git - thirdparty/pciutils.git/blob - common.c
ed39b87148f6c726178f206f460c39c39975c56f
[thirdparty/pciutils.git] / common.c
1 /*
2 * The PCI Utilities -- Common Functions
3 *
4 * Copyright (c) 1997--2008 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 #include <unistd.h>
14
15 #include "pciutils.h"
16
17 void NONRET
18 die(char *msg, ...)
19 {
20 va_list args;
21
22 va_start(args, msg);
23 fprintf(stderr, "%s: ", program_name);
24 vfprintf(stderr, msg, args);
25 fputc('\n', stderr);
26 exit(1);
27 }
28
29 void *
30 xmalloc(unsigned int howmuch)
31 {
32 void *p = malloc(howmuch);
33 if (!p)
34 die("Unable to allocate %d bytes of memory", howmuch);
35 return p;
36 }
37
38 void *
39 xrealloc(void *ptr, unsigned int howmuch)
40 {
41 void *p = realloc(ptr, howmuch);
42 if (!p)
43 die("Unable to allocate %d bytes of memory", howmuch);
44 return p;
45 }
46
47 static void
48 set_pci_method(struct pci_access *pacc, char *arg)
49 {
50 char *name;
51 int i;
52
53 if (!strcmp(arg, "help"))
54 {
55 printf("Known PCI access methods:\n\n");
56 for (i=0; name = pci_get_method_name(i); i++)
57 if (name[0])
58 printf("%s\n", name);
59 exit(0);
60 }
61 else
62 {
63 i = pci_lookup_method(arg);
64 if (i < 0)
65 die("No such PCI access method: %s (see `-A help' for a list)", arg);
66 pacc->method = i;
67 }
68 }
69
70 static void
71 set_pci_option(struct pci_access *pacc, char *arg)
72 {
73 if (!strcmp(arg, "help"))
74 {
75 struct pci_param *p;
76 printf("Known PCI access parameters:\n\n");
77 for (p=NULL; p=pci_walk_params(pacc, p);)
78 printf("%-20s %s (%s)\n", p->param, p->help, p->value);
79 exit(0);
80 }
81 else
82 {
83 char *sep = strchr(arg, '=');
84 if (!sep)
85 die("Invalid PCI access parameter syntax: %s", arg);
86 *sep++ = 0;
87 if (pci_set_param(pacc, arg, sep) < 0)
88 die("Unrecognized PCI access parameter: %s (see `-O help' for a list)", arg);
89 }
90 }
91
92 int
93 parse_generic_option(int i, struct pci_access *pacc, char *optarg)
94 {
95 switch (i)
96 {
97 #ifdef PCI_HAVE_PM_INTEL_CONF
98 case 'H':
99 if (!strcmp(optarg, "1"))
100 pacc->method = PCI_ACCESS_I386_TYPE1;
101 else if (!strcmp(optarg, "2"))
102 pacc->method = PCI_ACCESS_I386_TYPE2;
103 else
104 die("Unknown hardware configuration type %s", optarg);
105 break;
106 #endif
107 #ifdef PCI_HAVE_PM_DUMP
108 case 'F':
109 pci_set_param(pacc, "dump.name", optarg);
110 pacc->method = PCI_ACCESS_DUMP;
111 break;
112 #endif
113 case 'A':
114 set_pci_method(pacc, optarg);
115 break;
116 case 'G':
117 pacc->debugging++;
118 break;
119 case 'O':
120 set_pci_option(pacc, optarg);
121 break;
122 default:
123 return 0;
124 }
125 return 1;
126 }