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