1 /*-
2 * Copyright (c) 1997, 1998
3 * Nan Yang Computer Services Limited. All rights reserved.
4 *
5 * This software is distributed under the so-called ``Berkeley
6 * License'':
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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 the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Nan Yang Computer
19 * Services Limited.
20 * 4. Neither the name of the Company nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * This software is provided ``as is'', and any express or implied
25 * warranties, including, but not limited to, the implied warranties of
26 * merchantability and fitness for a particular purpose are disclaimed.
27 * In no event shall the company or contributors be liable for any
28 * direct, indirect, incidental, special, exemplary, or consequential
29 * damages (including, but not limited to, procurement of substitute
30 * goods or services; loss of use, data, or profits; or business
31 * interruption) however caused and on any theory of liability, whether
32 * in contract, strict liability, or tort (including negligence or
33 * otherwise) arising in any way out of the use of this software, even if
34 * advised of the possibility of such damage.
35 *
36 * $Id: vinumparser.c,v 1.25 2003/05/07 03:33:28 grog Exp grog $
37 */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 /*
43 * This file contains the parser for the configuration routines. It's used
44 * both in the kernel and in the user interface program, thus the separate file.
45 */
46
47 /*
48 * Go through a text and split up into text tokens. These are either non-blank
49 * sequences, or any sequence (except \0) enclosed in ' or ". Embedded ' or
50 * " characters may be escaped by \, which otherwise has no special meaning.
51 *
52 * Delimit by following with a \0, and return pointers to the starts at token [].
53 * Return the number of tokens found as the return value.
54 *
55 * This method has the restriction that a closing " or ' must be followed by
56 * grey space.
57 *
58 * Error conditions are end of line before end of quote, or no space after
59 * a closing quote. In this case, tokenize() returns -1.
60 */
61
62 #include <sys/param.h>
63 #include <dev/vinum/vinumkw.h>
64 #ifdef _KERNEL
65 #include <sys/systm.h>
66 #include <sys/conf.h>
67 #include <machine/setjmp.h>
68 /* All this mess for a single struct definition */
69 #include <sys/uio.h>
70 #include <sys/namei.h>
71 #include <sys/mount.h>
72
73 #include <dev/vinum/vinumvar.h>
74 #include <dev/vinum/vinumio.h>
75 #include <dev/vinum/vinumext.h>
76 #define iswhite(c) ((c == ' ') || (c == '\t')) /* check for white space */
77 #else /* userland */
78 #include <ctype.h>
79 #include <errno.h>
80 #include <fcntl.h>
81 #include <string.h>
82 #define iswhite isspace /* use the ctype macro */
83 #endif
84
85 /* enum keyword is defined in vinumvar.h */
86
87 #define keypair(x) { #x, kw_##x } /* create pair "foo", kw_foo */
88 #define flagkeypair(x) { "-"#x, kw_##x } /* create pair "-foo", kw_foo */
89 #define KEYWORDSET(x) {sizeof (x) / sizeof (struct _keywords), x}
90
91 /* Normal keywords. These are all the words that vinum knows. */
92 struct _keywords keywords[] =
93 {keypair(drive),
94 keypair(partition),
95 keypair(sd),
96 keypair(subdisk),
97 keypair(plex),
98 keypair(volume),
99 keypair(vol),
100 keypair(setupstate),
101 keypair(readpol),
102 keypair(org),
103 keypair(name),
104 keypair(writethrough),
105 keypair(writeback),
106 keypair(device),
107 keypair(concat),
108 keypair(raid4),
109 keypair(raid5),
110 keypair(striped),
111 keypair(plexoffset),
112 keypair(driveoffset),
113 keypair(length),
114 keypair(len),
115 keypair(size),
116 keypair(state),
117 keypair(round),
118 keypair(prefer),
119 keypair(preferred),
120 keypair(rename),
121 keypair(detached),
122 #ifndef _KERNEL /* for vinum(8) only */
123 keypair(debug),
124 keypair(stripe),
125 keypair(mirror),
126 #endif
127 keypair(attach),
128 keypair(detach),
129 keypair(printconfig),
130 keypair(saveconfig),
131 keypair(replace),
132 keypair(create),
133 keypair(read),
134 keypair(modify),
135 keypair(list),
136 keypair(l),
137 keypair(ld),
138 keypair(ls),
139 keypair(lp),
140 keypair(lv),
141 keypair(info),
142 keypair(set),
143 keypair(rm),
144 keypair(mv),
145 keypair(move),
146 keypair(init),
147 keypair(resetconfig),
148 keypair(start),
149 keypair(stop),
150 keypair(makedev),
151 keypair(help),
152 keypair(quit),
153 keypair(setdaemon),
154 keypair(getdaemon),
155 keypair(max),
156 keypair(replace),
157 keypair(readpol),
158 keypair(resetstats),
159 keypair(setstate),
160 keypair(checkparity),
161 keypair(rebuildparity),
162 keypair(dumpconfig),
163 keypair(retryerrors)
164 };
165 struct keywordset keyword_set = KEYWORDSET(keywords);
166
167 #ifndef _KERNEL
168 struct _keywords flag_keywords[] =
169 {flagkeypair(f),
170 flagkeypair(d),
171 flagkeypair(v),
172 flagkeypair(s),
173 flagkeypair(r),
174 flagkeypair(w)
175 };
176 struct keywordset flag_set = KEYWORDSET(flag_keywords);
177
178 #endif
179
180 /*
181 * Take a blank separated list of tokens and turn it into a list of
182 * individual nul-delimited strings. Build a list of pointers at
183 * token, which must have enough space for the tokens. Return the
184 * number of tokens, or -1 on error (typically a missing string
185 * delimiter).
186 */
187 int
188 tokenize(char *cptr, char *token[], int maxtoken)
189 {
190 char delim; /* delimiter for searching for the partner */
191 int tokennr; /* index of this token */
192
193 for (tokennr = 0; tokennr < maxtoken;) {
194 while (iswhite(*cptr))
195 cptr++; /* skip initial white space */
196 if ((*cptr == '\0') || (*cptr == '\n') || (*cptr == '#')) /* end of line */
197 return tokennr; /* return number of tokens found */
198 delim = *cptr;
199 token[tokennr] = cptr; /* point to it */
200 tokennr++; /* one more */
201 if (tokennr == maxtoken) /* run off the end? */
202 return tokennr;
203 if ((delim == '\'') || (delim == '"')) { /* delimitered */
204 for (;;) {
205 cptr++;
206 if ((*cptr == delim) && (cptr[-1] != '\\')) { /* found the partner */
207 cptr++; /* move on past */
208 if (!iswhite(*cptr)) /* error, no space after closing quote */
209 return -1;
210 *cptr++ = '\0'; /* delimit */
211 } else if ((*cptr == '\0') || (*cptr == '\n')) /* end of line */
212 return -1;
213 }
214 } else { /* not quoted */
215 while ((*cptr != '\0') && (!iswhite(*cptr)) && (*cptr != '\n'))
216 cptr++;
217 if (*cptr != '\0') /* not end of the line, */
218 *cptr++ = '\0'; /* delimit and move to the next */
219 }
220 }
221 return maxtoken; /* can't get here */
222 }
223
224 /* Find a keyword and return an index */
225 enum keyword
226 get_keyword(char *name, struct keywordset *keywordset)
227 {
228 int i;
229 struct _keywords *keywords = keywordset->k; /* point to the keywords */
230 if (name != NULL) { /* parameter exists */
231 for (i = 0; i < keywordset->size; i++)
232 if (!strcmp(name, keywords[i].name))
233 return (enum keyword) keywords[i].keyword;
234 }
235 return kw_invalid_keyword;
236 }
Cache object: c06db44136b23e0e1bb372a56877c885
|