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
44 * Generous estimate of number of fields, including terminal NULL pointer
46 static int ncmdfield(char *p, int n)
57 white = 1; /* first text will start field */
59 nwhite = (strchr(" \t\r\n", *p++ & 0xFF) != 0); /* UTF is irrelevant */
60 if (white && !nwhite) /* beginning of field */
64 return nf + 1; /* +1 for NULL */
68 * parse a command written to a device
70 struct cmdbuf *parsecmd(char *p, int n)
73 struct cmdbuf *volatile cb;
79 /* allocate Cmdbuf plus string pointers plus copy of string including \0 */
80 sp = kzmalloc(sizeof(*cb) + nf * sizeof(char *) + n + 1, 0);
81 cb = (struct cmdbuf *)sp;
82 cb->f = (char **)(&cb[1]);
83 cb->buf = (char *)(&cb->f[nf]);
85 if (current != NULL && waserror()) {
89 memmove(cb->buf, p, n);
93 /* dump new line and null terminate */
94 if (n > 0 && cb->buf[n - 1] == '\n')
98 cb->nf = tokenize(cb->buf, cb->f, nf - 1);
105 * Reconstruct original message, for error diagnostic
107 void cmderror(struct cmdbuf *cb, char *s)
112 p = get_cur_genbuf();
113 e = p + GENBUF_SZ - 10;
114 p = seprintf(p, e, "%s \"", s);
115 for (i = 0; i < cb->nf; i++) {
117 p = seprintf(p, e, " ");
118 p = seprintf(p, e, "%s", cb->f[i]);
120 seprintf(p, e, "\"");
121 error(EFAIL, get_cur_genbuf());
124 void debugcmd(struct cmdbuf *cb)
126 printk("cb %p, nr %d\n", cb, cb->nf);
127 for (int i = 0; i < cb->nf; i++) {
128 printk("%d: %s\n", i, cb->f[i]);
133 * Look up entry in table
135 struct cmdtab *lookupcmd(struct cmdbuf *cb, struct cmdtab *ctab, int nctab)
141 error(EFAIL, "empty control message");
143 for (ct = ctab, i = 0; i < nctab; i++, ct++) {
144 if (strcmp(ct->cmd, "*") != 0) /* wildcard always matches */
145 if (strcmp(ct->cmd, cb->f[0]) != 0)
147 if (ct->narg != 0 && ct->narg != cb->nf)
148 cmderror(cb, "wrong number of args");
152 cmderror(cb, "unknown control message");