loader(8): adjustment & man update
[dragonfly.git] / sys / boot / dloader / subs.c
1 /*
2  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <stand.h>
36 #include <string.h>
37 #include "bootstrap.h"
38 #include "dloader.h"
39
40 static void dvar_free(dvar_t *lastp);
41
42 dvar_t dvbase;
43 dvar_t *dvlastp = &dvbase;
44
45 dvar_t
46 dvar_get(const char *name)
47 {
48         dvar_t var;
49
50         for (var = dvbase; var; var = var->next) {
51                 if (strcmp(name, var->name) == 0)
52                         return(var);
53         }
54         return(NULL);
55 }
56
57 void
58 dvar_set(const char *name, char **data, int count)
59 {
60         dvar_t var;
61
62         for (var = dvbase; var; var = var->next) {
63                 if (strcmp(name, var->name) == 0)
64                         break;
65         }
66         if (var == NULL) {
67                 var = malloc(sizeof(*var) + strlen(name) + 1);
68                 var->name = (char *)(void *)(var + 1);
69                 strcpy(var->name, name);
70                 var->next = NULL;
71                 *dvlastp = var;
72                 dvlastp = &var->next;
73         } else {
74                 while (--var->count >= 0)
75                         free(var->data[var->count]);
76                 free(var->data);
77                 /* var->data = NULL; not needed */
78         }
79         var->count = count;
80         var->data = malloc(sizeof(char *) * (count + 1));
81         var->data[count] = NULL;
82         while (--count >= 0)
83                 var->data[count] = strdup(data[count]);
84 }
85
86 void
87 dvar_unset(const char *name)
88 {
89         dvar_t *lastp;
90         dvar_t var;
91         char *p;
92
93         lastp = &dvbase;
94         if ((p = strchr(name, '*')) != NULL) {
95                 while ((var = *lastp) != NULL) {
96                         if (strlen(var->name) >= p - name &&
97                             strncmp(var->name, name, p - name) == 0) {
98                                 dvar_free(lastp);
99                         } else {
100                                 lastp = &var->next;
101                         }
102                 }
103         } else {
104                 while ((var = *lastp) != NULL) {
105                         if (strcmp(name, var->name) == 0) {
106                                 dvar_free(lastp);
107                                 break;
108                         }
109                         lastp = &var->next;
110                 }
111         }
112 }
113
114 dvar_t
115 dvar_first(void)
116 {
117         return(dvbase);
118 }
119
120 dvar_t
121 dvar_next(dvar_t var)
122 {
123         return(var->next);
124 }
125
126 static void
127 dvar_free(dvar_t *lastp)
128 {
129         dvar_t dvar = *lastp;
130
131         if (dvlastp == &dvar->next)
132                 dvlastp = lastp;
133         *lastp = dvar->next;
134         while (--dvar->count >= 0)
135                 free(dvar->data[dvar->count]);
136         free(dvar->data);
137         free(dvar);
138 }
139
140 int
141 dvar_istrue(dvar_t var)
142 {
143         int retval = 0;
144
145         if (var != NULL && (strcasecmp(var->data[0], "yes") == 0 ||
146             strcasecmp(var->data[0], "true") == 0 ||
147             strcasecmp(var->data[0], "on") == 0 ||
148             strcasecmp(var->data[0], "1") == 0))
149                 retval = 1;
150
151         return (retval);
152 }