1 /* Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
2 * Portions Copyright © 1997-1999 Vita Nuova Limited
3 * Portions Copyright © 2000-2007 Vita Nuova Holdings Limited
5 * Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
7 * Modified for the Akaros operating system:
8 * Copyright (c) 2013-2014 The Regents of the University of California
9 * Copyright (c) 2013-2015 Google Inc.
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42 * Generous estimate of number of fields, including terminal NULL pointer
44 static int ncmdfield(char *p, int n)
55 white = 1; /* first text will start field */
57 nwhite = (strchr(" \t\r\n", *p++ & 0xFF) != 0); /* UTF is irrelevant */
58 if (white && !nwhite) /* beginning of field */
62 return nf + 1; /* +1 for NULL */
66 * parse a command written to a device
68 struct cmdbuf *parsecmd(char *p, int n)
71 struct cmdbuf *volatile cb;
77 /* allocate Cmdbuf plus string pointers plus copy of string including \0 */
78 sp = kzmalloc(sizeof(*cb) + nf * sizeof(char *) + n + 1, 0);
79 cb = (struct cmdbuf *)sp;
80 cb->f = (char **)(&cb[1]);
81 cb->buf = (char *)(&cb->f[nf]);
83 if (current != NULL && waserror()) {
87 memmove(cb->buf, p, n);
91 /* dump new line and null terminate */
92 if (n > 0 && cb->buf[n - 1] == '\n')
96 cb->nf = tokenize(cb->buf, cb->f, nf - 1);
103 * Reconstruct original message, for error diagnostic
105 void cmderror(struct cmdbuf *cb, char *s)
110 p = get_cur_genbuf();
111 e = p + GENBUF_SZ - 10;
112 p = seprintf(p, e, "%s \"", s);
113 for (i = 0; i < cb->nf; i++) {
115 p = seprintf(p, e, " ");
116 p = seprintf(p, e, "%s", cb->f[i]);
118 seprintf(p, e, "\"");
119 error(EFAIL, get_cur_genbuf());
122 void debugcmd(struct cmdbuf *cb)
124 printk("cb %p, nr %d\n", cb, cb->nf);
125 for (int i = 0; i < cb->nf; i++) {
126 printk("%d: %s\n", i, cb->f[i]);
131 * Look up entry in table
133 struct cmdtab *lookupcmd(struct cmdbuf *cb, struct cmdtab *ctab, int nctab)
139 error(EFAIL, "empty control message");
141 for (ct = ctab, i = 0; i < nctab; i++, ct++) {
142 if (strcmp(ct->cmd, "*") != 0) /* wildcard always matches */
143 if (strcmp(ct->cmd, cb->f[0]) != 0)
145 if (ct->narg != 0 && ct->narg != cb->nf)
146 cmderror(cb, "wrong number of args");
150 cmderror(cb, "unknown control message");