]> mj.ucw.cz Git - pciutils.git/blob - params.c
ac756ad79a313661e13f3d67cf85ec5e3cd43bf6
[pciutils.git] / 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 void
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;
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 }
54
55 int
56 pci_set_param_internal(struct pci_access *acc, char *param, char *value, int copy)
57 {
58   struct pci_param *p;
59
60   for (p=acc->params; p; p=p->next)
61     if (!strcmp(p->param, param))
62       {
63         if (p->value_malloced)
64           pci_mfree(p->value);
65         p->value_malloced = copy;
66         if (copy)
67           p->value = pci_strdup(acc, value);
68         else
69           p->value = value;
70         return 0;
71       }
72   return -1;
73 }
74
75 int
76 pci_set_param(struct pci_access *acc, char *param, char *value)
77 {
78   return pci_set_param_internal(acc, param, value, 1);
79 }
80
81 void
82 pci_free_params(struct pci_access *acc)
83 {
84   struct pci_param *p;
85
86   while (p = acc->params)
87     {
88       acc->params = p->next;
89       if (p->value_malloced)
90         pci_mfree(p->value);
91       pci_mfree(p);
92     }
93 }
94
95 struct pci_param *
96 pci_walk_params(struct pci_access *acc, struct pci_param *prev)
97 {
98   /* So far, the params form a simple linked list, but this can change in the future */
99   if (!prev)
100     return acc->params;
101   else
102     return prev->next;
103 }