Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sbin / 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  * $DragonFly: src/sbin/vinum/vinumparser.c,v 1.2 2003/06/17 04:28:33 dillon Exp $
39  */
40
41 /*
42  * This file contains the parser for the configuration routines.  It's used
43  * both in the kernel and in the user interface program, thus the separate file.
44  */
45
46 /*
47  * Go through a text and split up into text tokens.  These are either non-blank
48  * sequences, or any sequence (except \0) enclosed in ' or ".  Embedded ' or
49  * " characters may be escaped by \, which otherwise has no special meaning.
50  *
51  * Delimit by following with a \0, and return pointers to the starts at token [].
52  * Return the number of tokens found as the return value.
53  *
54  * This method has the restriction that a closing " or ' must be followed by
55  * grey space.
56  *
57  * Error conditions are end of line before end of quote, or no space after
58  * a closing quote.  In this case, tokenize() returns -1.
59  */
60
61 #include <sys/param.h>
62 #include <dev/vinum/vinumkw.h>
63 #ifdef _KERNEL
64 #include <sys/systm.h>
65 #include <sys/conf.h>
66 #include <machine/setjmp.h>
67 /* All this mess for a single struct definition */
68 #include <sys/uio.h>
69 #include <sys/namei.h>
70 #include <sys/disklabel.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 #define iswhite isspace                                     /* use the ctype macro */
82 #endif
83
84 /* enum keyword is defined in vinumvar.h */
85
86 #define keypair(x) { #x, kw_##x }                           /* create pair "foo", kw_foo */
87 #define flagkeypair(x) { "-"#x, kw_##x }                    /* create pair "-foo", kw_foo */
88 #define KEYWORDSET(x) {sizeof (x) / sizeof (struct _keywords), x}
89
90 /* Normal keywords.  These are all the words that vinum knows. */
91 struct _keywords keywords[] =
92 {keypair(drive),
93     keypair(partition),
94     keypair(sd),
95     keypair(subdisk),
96     keypair(plex),
97     keypair(volume),
98     keypair(vol),
99     keypair(setupstate),
100     keypair(readpol),
101     keypair(org),
102     keypair(name),
103     keypair(writethrough),
104     keypair(writeback),
105     keypair(raw),
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(rename),
120     keypair(detached),
121 #ifndef _KERNEL                                             /* for vinum(8) only */
122 #ifdef VINUMDEBUG
123     keypair(debug),
124 #endif
125     keypair(stripe),
126     keypair(mirror),
127 #endif
128     keypair(attach),
129     keypair(detach),
130     keypair(printconfig),
131     keypair(saveconfig),
132     keypair(replace),
133     keypair(create),
134     keypair(read),
135     keypair(modify),
136     keypair(list),
137     keypair(l),
138     keypair(ld),
139     keypair(ls),
140     keypair(lp),
141     keypair(lv),
142     keypair(info),
143     keypair(set),
144     keypair(rm),
145     keypair(mv),
146     keypair(move),
147     keypair(init),
148     keypair(label),
149     keypair(resetconfig),
150     keypair(start),
151     keypair(stop),
152     keypair(makedev),
153     keypair(help),
154     keypair(quit),
155     keypair(setdaemon),
156     keypair(getdaemon),
157     keypair(max),
158     keypair(replace),
159     keypair(readpol),
160     keypair(resetstats),
161     keypair(setstate),
162     keypair(checkparity),
163     keypair(rebuildparity),
164     keypair(dumpconfig),
165     keypair(retryerrors)
166 };
167 struct keywordset keyword_set = KEYWORDSET(keywords);
168
169 #ifndef _KERNEL
170 struct _keywords flag_keywords[] =
171 {flagkeypair(f),
172     flagkeypair(d),
173     flagkeypair(v),
174     flagkeypair(s),
175     flagkeypair(r),
176     flagkeypair(w)
177 };
178 struct keywordset flag_set = KEYWORDSET(flag_keywords);
179
180 #endif
181
182 /*
183  * Take a blank separated list of tokens and turn it into a list of
184  * individual nul-delimited strings.  Build a list of pointers at
185  * token, which must have enough space for the tokens.  Return the
186  * number of tokens, or -1 on error (typically a missing string
187  * delimiter).
188  */
189 int
190 tokenize(char *cptr, char *token[])
191 {
192     char delim;                                             /* delimiter for searching for the partner */
193     int tokennr;                                            /* index of this token */
194     tokennr = 0;                                            /* none found yet */
195
196     for (;;) {
197         while (iswhite(*cptr))
198             cptr++;                                         /* skip initial white space */
199         if ((*cptr == '\0') || (*cptr == '\n') || (*cptr == '#')) /* end of line */
200             return tokennr;                                 /* return number of tokens found */
201         delim = *cptr;
202         token[tokennr] = cptr;                              /* point to it */
203         tokennr++;                                          /* one more */
204         /* XXX this is broken.  It leaves superfluous \\ characters in the text */
205         if ((delim == '\'') || (delim == '"')) {            /* delimitered */
206             for (;;) {
207                 cptr++;
208                 if ((*cptr == delim) && (cptr[-1] != '\\')) { /* found the partner */
209                     cptr++;                                 /* move on past */
210                     if (!iswhite(*cptr))                    /* error, no space after closing quote */
211                         return -1;
212                     *cptr++ = '\0';                         /* delimit */
213                 } else if ((*cptr == '\0') || (*cptr == '\n')) /* end of line */
214                     return -1;
215             }
216         } else {                                            /* not quoted */
217             while ((*cptr != '\0') && (!iswhite(*cptr)) && (*cptr != '\n'))
218                 cptr++;
219             if (*cptr != '\0')                              /* not end of the line, */
220                 *cptr++ = '\0';                             /* delimit and move to the next */
221         }
222     }
223 }
224
225 /* Find a keyword and return an index */
226 enum keyword
227 get_keyword(char *name, struct keywordset *keywordset)
228 {
229     int i;
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;
235     }
236     return kw_invalid_keyword;
237 }