]> git.ipfire.org Git - thirdparty/pciutils.git/blame - common.c
Sylixos port
[thirdparty/pciutils.git] / common.c
CommitLineData
727ce158 1/*
4284af58 2 * The PCI Utilities -- Common Functions
727ce158 3 *
d1d7d88a 4 * Copyright (c) 1997--2016 Martin Mares <mj@ucw.cz>
727ce158
MM
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
91e37a49 17void NONRET
727ce158
MM
18die(char *msg, ...)
19{
20 va_list args;
21
22 va_start(args, msg);
81afa98c 23 fprintf(stderr, "%s: ", program_name);
727ce158
MM
24 vfprintf(stderr, msg, args);
25 fputc('\n', stderr);
26 exit(1);
27}
28
29void *
d1d7d88a 30xmalloc(size_t howmuch)
727ce158
MM
31{
32 void *p = malloc(howmuch);
33 if (!p)
d1d7d88a 34 die("Unable to allocate %d bytes of memory", (int) howmuch);
727ce158
MM
35 return p;
36}
37
ec25b52d 38void *
d1d7d88a 39xrealloc(void *ptr, size_t howmuch)
ec25b52d
MM
40{
41 void *p = realloc(ptr, howmuch);
42 if (!p)
d1d7d88a 43 die("Unable to allocate %d bytes of memory", (int) howmuch);
ec25b52d
MM
44 return p;
45}
46
d3f768e2 47char *
d1d7d88a 48xstrdup(const char *str)
d3f768e2
MM
49{
50 int len = strlen(str) + 1;
51 char *copy = xmalloc(len);
52 memcpy(copy, str, len);
53 return copy;
54}
55
a0407443
MM
56static void
57set_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
553d12c8
MM
79static void
80set_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)
a0407443 97 die("Unrecognized PCI access parameter: %s (see `-O help' for a list)", arg);
553d12c8
MM
98 }
99}
100
727ce158 101int
af34f014 102parse_generic_option(int i, struct pci_access *pacc, char *arg)
727ce158
MM
103{
104 switch (i)
105 {
489233b4 106#ifdef PCI_HAVE_PM_INTEL_CONF
727ce158 107 case 'H':
af34f014 108 if (!strcmp(arg, "1"))
727ce158 109 pacc->method = PCI_ACCESS_I386_TYPE1;
af34f014 110 else if (!strcmp(arg, "2"))
727ce158
MM
111 pacc->method = PCI_ACCESS_I386_TYPE2;
112 else
af34f014 113 die("Unknown hardware configuration type %s", arg);
727ce158
MM
114 break;
115#endif
489233b4 116#ifdef PCI_HAVE_PM_DUMP
727ce158 117 case 'F':
af34f014 118 pci_set_param(pacc, "dump.name", arg);
727ce158
MM
119 pacc->method = PCI_ACCESS_DUMP;
120 break;
121#endif
a0407443 122 case 'A':
af34f014 123 set_pci_method(pacc, arg);
a0407443 124 break;
727ce158
MM
125 case 'G':
126 pacc->debugging++;
127 break;
553d12c8 128 case 'O':
af34f014 129 set_pci_option(pacc, arg);
553d12c8 130 break;
727ce158
MM
131 default:
132 return 0;
133 }
134 return 1;
135}