]> mj.ucw.cz Git - pciutils.git/blob - common.c
Merge remote-tracking branch 'jlledom/hurd-fix-dev-aux'
[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 #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(size_t howmuch)
31 {
32   void *p = malloc(howmuch);
33   if (!p)
34     die("Unable to allocate %d bytes of memory", (int) howmuch);
35   return p;
36 }
37
38 void *
39 xrealloc(void *ptr, size_t howmuch)
40 {
41   void *p = realloc(ptr, howmuch);
42   if (!p)
43     die("Unable to allocate %d bytes of memory", (int) howmuch);
44   return p;
45 }
46
47 char *
48 xstrdup(const char *str)
49 {
50   int len = strlen(str) + 1;
51   char *copy = xmalloc(len);
52   memcpy(copy, str, len);
53   return copy;
54 }
55
56 static void
57 set_pci_method(struct pci_access *pacc, char *arg)
58 {
59   char *name;
60   int i;
61
62   if (!strcmp(arg, "help"))
63     {
64       printf("Known PCI access methods:\n\n");
65       for (i=0; name = pci_get_method_name(i); i++)
66         if (name[0])
67           printf("%s\n", name);
68       exit(0);
69     }
70   else
71     {
72       i = pci_lookup_method(arg);
73       if (i < 0)
74         die("No such PCI access method: %s (see `-A help' for a list)", arg);
75       pacc->method = i;
76     }
77 }
78
79 static void
80 set_pci_option(struct pci_access *pacc, char *arg)
81 {
82   if (!strcmp(arg, "help"))
83     {
84       struct pci_param *p;
85       printf("Known PCI access parameters:\n\n");
86       for (p=NULL; p=pci_walk_params(pacc, p);)
87         printf("%-20s %s (%s)\n", p->param, p->help, p->value);
88       exit(0);
89     }
90   else
91     {
92       char *sep = strchr(arg, '=');
93       if (!sep)
94         die("Invalid PCI access parameter syntax: %s", arg);
95       *sep++ = 0;
96       if (pci_set_param(pacc, arg, sep) < 0)
97         die("Unrecognized PCI access parameter: %s (see `-O help' for a list)", arg);
98     }
99 }
100
101 int
102 parse_generic_option(int i, struct pci_access *pacc, char *arg)
103 {
104   switch (i)
105     {
106 #ifdef PCI_HAVE_PM_INTEL_CONF
107     case 'H':
108       if (!strcmp(arg, "1"))
109         pacc->method = PCI_ACCESS_I386_TYPE1;
110       else if (!strcmp(arg, "2"))
111         pacc->method = PCI_ACCESS_I386_TYPE2;
112       else
113         die("Unknown hardware configuration type %s", arg);
114       break;
115 #endif
116 #ifdef PCI_HAVE_PM_DUMP
117     case 'F':
118       pci_set_param(pacc, "dump.name", arg);
119       pacc->method = PCI_ACCESS_DUMP;
120       break;
121 #endif
122     case 'A':
123       set_pci_method(pacc, arg);
124       break;
125     case 'G':
126       pacc->debugging++;
127       break;
128     case 'O':
129       set_pci_option(pacc, arg);
130       break;
131     default:
132       return 0;
133     }
134   return 1;
135 }