]> git.ipfire.org Git - thirdparty/pciutils.git/blob - common.c
7e53157773dcfebacfc47428f647e0a9c3a0db67
[thirdparty/pciutils.git] / common.c
1 /*
2 * The PCI Utilities -- Common Functions
3 *
4 * Copyright (c) 1997--2006 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_option(struct pci_access *pacc, char *arg)
49 {
50 if (!strcmp(arg, "help"))
51 {
52 struct pci_param *p;
53 printf("Known PCI access parameters:\n\n");
54 for (p=NULL; p=pci_walk_params(pacc, p);)
55 printf("%-20s %s (%s)\n", p->param, p->help, p->value);
56 exit(0);
57 }
58 else
59 {
60 char *sep = strchr(arg, '=');
61 if (!sep)
62 die("Invalid PCI access parameter syntax: %s", arg);
63 *sep++ = 0;
64 if (pci_set_param(pacc, arg, sep) < 0)
65 die("Unrecognized PCI access parameter: %s", arg);
66 }
67 }
68
69 int
70 parse_generic_option(int i, struct pci_access *pacc, char *optarg)
71 {
72 switch (i)
73 {
74 #ifdef PCI_HAVE_PM_LINUX_PROC
75 case 'P':
76 pacc->method_params[PCI_ACCESS_PROC_BUS_PCI] = optarg;
77 pacc->method = PCI_ACCESS_PROC_BUS_PCI;
78 break;
79 #endif
80 #ifdef PCI_HAVE_PM_INTEL_CONF
81 case 'H':
82 if (!strcmp(optarg, "1"))
83 pacc->method = PCI_ACCESS_I386_TYPE1;
84 else if (!strcmp(optarg, "2"))
85 pacc->method = PCI_ACCESS_I386_TYPE2;
86 else
87 die("Unknown hardware configuration type %s", optarg);
88 break;
89 #endif
90 #ifdef PCI_HAVE_PM_DUMP
91 case 'F':
92 pacc->method_params[PCI_ACCESS_DUMP] = optarg;
93 pacc->method = PCI_ACCESS_DUMP;
94 break;
95 #endif
96 case 'G':
97 pacc->debugging++;
98 break;
99 case 'O':
100 set_pci_option(pacc, optarg);
101 break;
102 default:
103 return 0;
104 }
105 return 1;
106 }