]>
Commit | Line | Data |
---|---|---|
727ce158 | 1 | /* |
4284af58 | 2 | * The PCI Utilities -- Common Functions |
727ce158 | 3 | * |
cb6ee324 | 4 | * Copyright (c) 1997--2008 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 | 17 | void NONRET |
727ce158 MM |
18 | die(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 | ||
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 | ||
ec25b52d MM |
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 | ||
d3f768e2 MM |
47 | char * |
48 | xstrdup(char *str) | |
49 | { | |
50 | int len = strlen(str) + 1; | |
51 | char *copy = xmalloc(len); | |
52 | memcpy(copy, str, len); | |
53 | return copy; | |
54 | } | |
55 | ||
a0407443 MM |
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 | ||
553d12c8 MM |
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) | |
a0407443 | 97 | die("Unrecognized PCI access parameter: %s (see `-O help' for a list)", arg); |
553d12c8 MM |
98 | } |
99 | } | |
100 | ||
727ce158 MM |
101 | int |
102 | parse_generic_option(int i, struct pci_access *pacc, char *optarg) | |
103 | { | |
104 | switch (i) | |
105 | { | |
489233b4 | 106 | #ifdef PCI_HAVE_PM_INTEL_CONF |
727ce158 MM |
107 | case 'H': |
108 | if (!strcmp(optarg, "1")) | |
109 | pacc->method = PCI_ACCESS_I386_TYPE1; | |
110 | else if (!strcmp(optarg, "2")) | |
111 | pacc->method = PCI_ACCESS_I386_TYPE2; | |
112 | else | |
113 | die("Unknown hardware configuration type %s", optarg); | |
114 | break; | |
115 | #endif | |
489233b4 | 116 | #ifdef PCI_HAVE_PM_DUMP |
727ce158 | 117 | case 'F': |
cb6ee324 | 118 | pci_set_param(pacc, "dump.name", optarg); |
727ce158 MM |
119 | pacc->method = PCI_ACCESS_DUMP; |
120 | break; | |
121 | #endif | |
a0407443 MM |
122 | case 'A': |
123 | set_pci_method(pacc, optarg); | |
124 | break; | |
727ce158 MM |
125 | case 'G': |
126 | pacc->debugging++; | |
127 | break; | |
553d12c8 MM |
128 | case 'O': |
129 | set_pci_option(pacc, optarg); | |
130 | break; | |
727ce158 MM |
131 | default: |
132 | return 0; | |
133 | } | |
134 | return 1; | |
135 | } |