2 * Copyright (c) 1997, 1998
3 * Nan Yang Computer Services Limited. All rights reserved.
5 * This software is distributed under the so-called ``Berkeley
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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.
36 * $Id: vinumparser.c,v 1.21 2000/12/20 03:44:13 grog Exp grog $
41 * This file contains the parser for the configuration routines. It's used
42 * both in the kernel and in the user interface program, thus the separate file.
46 * Go through a text and split up into text tokens. These are either non-blank
47 * sequences, or any sequence (except \0) enclosed in ' or ". Embedded ' or
48 * " characters may be escaped by \, which otherwise has no special meaning.
50 * Delimit by following with a \0, and return pointers to the starts at token [].
51 * Return the number of tokens found as the return value.
53 * This method has the restriction that a closing " or ' must be followed by
56 * Error conditions are end of line before end of quote, or no space after
57 * a closing quote. In this case, tokenize() returns -1.
60 #include <sys/param.h>
61 #include <dev/vinum/vinumkw.h>
63 #include <sys/systm.h>
65 #include <machine/setjmp.h>
66 /* All this mess for a single struct definition */
68 #include <sys/namei.h>
69 #include <sys/disklabel.h>
70 #include <sys/mount.h>
72 #include <dev/vinum/vinumvar.h>
73 #include <dev/vinum/vinumio.h>
74 #include <dev/vinum/vinumext.h>
75 #define iswhite(c) ((c == ' ') || (c == '\t')) /* check for white space */
80 #define iswhite isspace /* use the ctype macro */
83 /* enum keyword is defined in vinumvar.h */
85 #define keypair(x) { #x, kw_##x } /* create pair "foo", kw_foo */
86 #define flagkeypair(x) { "-"#x, kw_##x } /* create pair "-foo", kw_foo */
87 #define KEYWORDSET(x) {sizeof (x) / sizeof (struct _keywords), x}
89 /* Normal keywords. These are all the words that vinum knows. */
90 struct _keywords keywords[] =
102 keypair(writethrough),
111 keypair(driveoffset),
120 #ifndef _KERNEL /* for vinum(8) only */
129 keypair(printconfig),
148 keypair(resetconfig),
161 keypair(checkparity),
162 keypair(rebuildparity),
166 struct keywordset keyword_set = KEYWORDSET(keywords);
169 struct _keywords flag_keywords[] =
177 struct keywordset flag_set = KEYWORDSET(flag_keywords);
182 * Take a blank separated list of tokens and turn it into a list of
183 * individual nul-delimited strings. Build a list of pointers at
184 * token, which must have enough space for the tokens. Return the
185 * number of tokens, or -1 on error (typically a missing string
189 tokenize(char *cptr, char *token[], int maxtoken)
191 char delim; /* delimiter for searching for the partner */
192 int tokennr; /* index of this token */
194 for (tokennr = 0; tokennr < maxtoken;) {
195 while (iswhite(*cptr))
196 cptr++; /* skip initial white space */
197 if ((*cptr == '\0') || (*cptr == '\n') || (*cptr == '#')) /* end of line */
198 return tokennr; /* return number of tokens found */
200 token[tokennr] = cptr; /* point to it */
201 tokennr++; /* one more */
202 if (tokennr == maxtoken) /* run off the end? */
204 if ((delim == '\'') || (delim == '"')) { /* delimitered */
207 if ((*cptr == delim) && (cptr[-1] != '\\')) { /* found the partner */
208 cptr++; /* move on past */
209 if (!iswhite(*cptr)) /* error, no space after closing quote */
211 *cptr++ = '\0'; /* delimit */
212 } else if ((*cptr == '\0') || (*cptr == '\n')) /* end of line */
215 } else { /* not quoted */
216 while ((*cptr != '\0') && (!iswhite(*cptr)) && (*cptr != '\n'))
218 if (*cptr != '\0') /* not end of the line, */
219 *cptr++ = '\0'; /* delimit and move to the next */
222 return maxtoken; /* can't get here */
225 /* Find a keyword and return an index */
227 get_keyword(char *name, struct keywordset *keywordset)
230 struct _keywords *keywords = keywordset->k; /* point to the keywords */
231 if (name != NULL) { /* parameter exists */
232 for (i = 0; i < keywordset->size; i++)
233 if (!strcmp(name, keywords[i].name))
234 return (enum keyword) keywords[i].keyword;
236 return kw_invalid_keyword;