Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / raid / vinum / vinumparser.c
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.21 2000/12/20 03:44:13 grog Exp grog $
37  * $FreeBSD: src/sys/dev/vinum/vinumparser.c,v 1.20.2.5 2001/05/28 05:56:27 grog Exp $
38  */
39
40 /*
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.
43  */
44
45 /*
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.
49  *
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.
52  *
53  * This method has the restriction that a closing " or ' must be followed by
54  * grey space.
55  *
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.
58  */
59
60 #include <sys/param.h>
61 #include <dev/vinum/vinumkw.h>
62 #ifdef _KERNEL
63 #include <sys/systm.h>
64 #include <sys/conf.h>
65 #include <machine/setjmp.h>
66 /* All this mess for a single struct definition */
67 #include <sys/uio.h>
68 #include <sys/namei.h>
69 #include <sys/disklabel.h>
70 #include <sys/mount.h>
71
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 */
76 #else /* userland */
77 #include <ctype.h>
78 #include <errno.h>
79 #include <fcntl.h>
80 #define iswhite isspace                                     /* use the ctype macro */
81 #endif
82
83 /* enum keyword is defined in vinumvar.h */
84
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}
88
89 /* Normal keywords.  These are all the words that vinum knows. */
90 struct _keywords keywords[] =
91 {keypair(drive),
92     keypair(partition),
93     keypair(sd),
94     keypair(subdisk),
95     keypair(plex),
96     keypair(volume),
97     keypair(vol),
98     keypair(setupstate),
99     keypair(readpol),
100     keypair(org),
101     keypair(name),
102     keypair(writethrough),
103     keypair(writeback),
104     keypair(raw),
105     keypair(device),
106     keypair(concat),
107     keypair(raid4),
108     keypair(raid5),
109     keypair(striped),
110     keypair(plexoffset),
111     keypair(driveoffset),
112     keypair(length),
113     keypair(len),
114     keypair(size),
115     keypair(state),
116     keypair(round),
117     keypair(prefer),
118     keypair(rename),
119     keypair(detached),
120 #ifndef _KERNEL                                             /* for vinum(8) only */
121 #ifdef VINUMDEBUG
122     keypair(debug),
123 #endif
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(label),
148     keypair(resetconfig),
149     keypair(start),
150     keypair(stop),
151     keypair(makedev),
152     keypair(help),
153     keypair(quit),
154     keypair(setdaemon),
155     keypair(getdaemon),
156     keypair(max),
157     keypair(replace),
158     keypair(readpol),
159     keypair(resetstats),
160     keypair(setstate),
161     keypair(checkparity),
162     keypair(rebuildparity),
163     keypair(dumpconfig),
164     keypair(retryerrors)
165 };
166 struct keywordset keyword_set = KEYWORDSET(keywords);
167
168 #ifndef _KERNEL
169 struct _keywords flag_keywords[] =
170 {flagkeypair(f),
171     flagkeypair(d),
172     flagkeypair(v),
173     flagkeypair(s),
174     flagkeypair(r),
175     flagkeypair(w)
176 };
177 struct keywordset flag_set = KEYWORDSET(flag_keywords);
178
179 #endif
180
181 /*
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
186  * delimiter).
187  */
188 int
189 tokenize(char *cptr, char *token[])
190 {
191     char delim;                                             /* delimiter for searching for the partner */
192     int tokennr;                                            /* index of this token */
193     tokennr = 0;                                            /* none found yet */
194
195     for (;;) {
196         while (iswhite(*cptr))
197             cptr++;                                         /* skip initial white space */
198         if ((*cptr == '\0') || (*cptr == '\n') || (*cptr == '#')) /* end of line */
199             return tokennr;                                 /* return number of tokens found */
200         delim = *cptr;
201         token[tokennr] = cptr;                              /* point to it */
202         tokennr++;                                          /* one more */
203         /* XXX this is broken.  It leaves superfluous \\ characters in the text */
204         if ((delim == '\'') || (delim == '"')) {            /* delimitered */
205             for (;;) {
206                 cptr++;
207                 if ((*cptr == delim) && (cptr[-1] != '\\')) { /* found the partner */
208                     cptr++;                                 /* move on past */
209                     if (!iswhite(*cptr))                    /* error, no space after closing quote */
210                         return -1;
211                     *cptr++ = '\0';                         /* delimit */
212                 } else if ((*cptr == '\0') || (*cptr == '\n')) /* end of line */
213                     return -1;
214             }
215         } else {                                            /* not quoted */
216             while ((*cptr != '\0') && (!iswhite(*cptr)) && (*cptr != '\n'))
217                 cptr++;
218             if (*cptr != '\0')                              /* not end of the line, */
219                 *cptr++ = '\0';                             /* delimit and move to the next */
220         }
221     }
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 }