Continue untangling the disklabel. Add sector index reservation fields
[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  * $DragonFly: src/sys/dev/raid/vinum/vinumparser.c,v 1.7 2007/05/16 05:20:21 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 "vinumkw.h"
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/buf.h>
69 #include <sys/proc.h>
70 #include <sys/namei.h>
71 #include <sys/diskslice.h>
72 #include <sys/mount.h>
73
74 #include "vinumvar.h"
75 #include "vinumio.h"
76 #include "vinumext.h"
77 #define iswhite(c) ((c == ' ') || (c == '\t'))              /* check for white space */
78
79 /* enum keyword is defined in vinumvar.h */
80
81 #define keypair(x) { #x, kw_##x }                           /* create pair "foo", kw_foo */
82 #define flagkeypair(x) { "-"#x, kw_##x }                    /* create pair "-foo", kw_foo */
83 #define KEYWORDSET(x) {sizeof (x) / sizeof (struct _keywords), x}
84
85 /* Normal keywords.  These are all the words that vinum knows. */
86 struct _keywords keywords[] =
87 {keypair(drive),
88     keypair(partition),
89     keypair(sd),
90     keypair(subdisk),
91     keypair(plex),
92     keypair(volume),
93     keypair(vol),
94     keypair(setupstate),
95     keypair(readpol),
96     keypair(org),
97     keypair(name),
98     keypair(writethrough),
99     keypair(writeback),
100     keypair(raw),
101     keypair(device),
102     keypair(concat),
103     keypair(raid4),
104     keypair(raid5),
105     keypair(striped),
106     keypair(plexoffset),
107     keypair(driveoffset),
108     keypair(length),
109     keypair(len),
110     keypair(size),
111     keypair(state),
112     keypair(round),
113     keypair(prefer),
114     keypair(rename),
115     keypair(detached),
116     keypair(attach),
117     keypair(detach),
118     keypair(printconfig),
119     keypair(saveconfig),
120     keypair(replace),
121     keypair(create),
122     keypair(read),
123     keypair(modify),
124     keypair(list),
125     keypair(l),
126     keypair(ld),
127     keypair(ls),
128     keypair(lp),
129     keypair(lv),
130     keypair(info),
131     keypair(set),
132     keypair(rm),
133     keypair(mv),
134     keypair(move),
135     keypair(init),
136     keypair(label),
137     keypair(resetconfig),
138     keypair(start),
139     keypair(stop),
140     keypair(makedev),
141     keypair(help),
142     keypair(quit),
143     keypair(setdaemon),
144     keypair(getdaemon),
145     keypair(max),
146     keypair(replace),
147     keypair(readpol),
148     keypair(resetstats),
149     keypair(setstate),
150     keypair(checkparity),
151     keypair(rebuildparity),
152     keypair(dumpconfig),
153     keypair(retryerrors)
154 };
155 struct keywordset keyword_set = KEYWORDSET(keywords);
156
157 /*
158  * Take a blank separated list of tokens and turn it into a list of
159  * individual nul-delimited strings.  Build a list of pointers at
160  * token, which must have enough space for the tokens.  Return the
161  * number of tokens, or -1 on error (typically a missing string
162  * delimiter).
163  */
164 int
165 tokenize(char *cptr, char *token[])
166 {
167     char delim;                                             /* delimiter for searching for the partner */
168     int tokennr;                                            /* index of this token */
169     tokennr = 0;                                            /* none found yet */
170
171     for (;;) {
172         while (iswhite(*cptr))
173             cptr++;                                         /* skip initial white space */
174         if ((*cptr == '\0') || (*cptr == '\n') || (*cptr == '#')) /* end of line */
175             return tokennr;                                 /* return number of tokens found */
176         delim = *cptr;
177         token[tokennr] = cptr;                              /* point to it */
178         tokennr++;                                          /* one more */
179         /* XXX this is broken.  It leaves superfluous \\ characters in the text */
180         if ((delim == '\'') || (delim == '"')) {            /* delimitered */
181             for (;;) {
182                 cptr++;
183                 if ((*cptr == delim) && (cptr[-1] != '\\')) { /* found the partner */
184                     cptr++;                                 /* move on past */
185                     if (!iswhite(*cptr))                    /* error, no space after closing quote */
186                         return -1;
187                     *cptr++ = '\0';                         /* delimit */
188                 } else if ((*cptr == '\0') || (*cptr == '\n')) /* end of line */
189                     return -1;
190             }
191         } else {                                            /* not quoted */
192             while ((*cptr != '\0') && (!iswhite(*cptr)) && (*cptr != '\n'))
193                 cptr++;
194             if (*cptr != '\0')                              /* not end of the line, */
195                 *cptr++ = '\0';                             /* delimit and move to the next */
196         }
197     }
198 }
199
200 /* Find a keyword and return an index */
201 enum keyword
202 get_keyword(char *name, struct keywordset *keywordset)
203 {
204     int i;
205     struct _keywords *keywords = keywordset->k;             /* point to the keywords */
206     if (name != NULL) {                                     /* parameter exists */
207         for (i = 0; i < keywordset->size; i++)
208             if (!strcmp(name, keywords[i].name))
209                 return (enum keyword) keywords[i].keyword;
210     }
211     return kw_invalid_keyword;
212 }