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