]> mj.ucw.cz Git - pciutils.git/blob - lib/params.c
Merge branch 'amiga'
[pciutils.git] / lib / params.c
1 /*
2  *      The PCI Library -- Parameters
3  *
4  *      Copyright (c) 2008--2023 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 <stdlib.h>
13 #include <string.h>
14
15 #include "internal.h"
16
17 char *
18 pci_get_param(struct pci_access *acc, char *param)
19 {
20   struct pci_param *p;
21
22   for (p=acc->params; p; p=p->next)
23     if (!strcmp(p->param, param))
24       return p->value;
25   return NULL;
26 }
27
28 struct pci_param *
29 pci_define_param(struct pci_access *acc, char *param, char *value, char *help)
30 {
31   struct pci_param *p, **pp;
32
33   for (pp=&acc->params; p = *pp; pp=&p->next)
34     {
35       int cmp = strcmp(p->param, param);
36       if (!cmp)
37         {
38           if (strcmp(p->value, value) || strcmp(p->help, help))
39             acc->error("Parameter %s re-defined differently", param);
40           return p;
41         }
42       if (cmp > 0)
43         break;
44     }
45
46   p = pci_malloc(acc, sizeof(*p));
47   p->next = *pp;
48   *pp = p;
49   p->param = param;
50   p->value = value;
51   p->value_malloced = 0;
52   p->help = help;
53   return p;
54 }
55
56 int
57 pci_set_param_internal(struct pci_access *acc, char *param, char *value, int copy)
58 {
59   struct pci_param *p;
60
61   for (p=acc->params; p; p=p->next)
62     if (!strcmp(p->param, param))
63       {
64         if (p->value_malloced)
65           pci_mfree(p->value);
66         p->value_malloced = copy;
67         if (copy)
68           p->value = pci_strdup(acc, value);
69         else
70           p->value = value;
71         return 0;
72       }
73   return -1;
74 }
75
76 int
77 pci_set_param(struct pci_access *acc, char *param, char *value)
78 {
79   return pci_set_param_internal(acc, param, value, 1);
80 }
81
82 void
83 pci_free_params(struct pci_access *acc)
84 {
85   struct pci_param *p;
86
87   while (p = acc->params)
88     {
89       acc->params = p->next;
90       if (p->value_malloced)
91         pci_mfree(p->value);
92       pci_mfree(p);
93     }
94 }
95
96 struct pci_param *
97 pci_walk_params(struct pci_access *acc, struct pci_param *prev)
98 {
99   /* So far, the params form a simple linked list, but this can change in the future */
100   if (!prev)
101     return acc->params;
102   else
103     return prev->next;
104 }