akaros/kern/src/ns/parse.c
<<
>>
Prefs
   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
   4 *                                (www.vitanuova.com)
   5 * Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
   6 *
   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.
  10 *
  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:
  17 *
  18 * The above copyright notice and this permission notice shall be included in
  19 * all copies or substantial portions of the Software.
  20 *
  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
  27 * SOFTWARE. */
  28
  29#include <slab.h>
  30#include <kmalloc.h>
  31#include <kref.h>
  32#include <string.h>
  33#include <stdio.h>
  34#include <assert.h>
  35#include <error.h>
  36#include <cpio.h>
  37#include <pmap.h>
  38#include <smp.h>
  39#include <net/ip.h>
  40
  41/*
  42 * Generous estimate of number of fields, including terminal NULL pointer
  43 */
  44static int ncmdfield(char *p, int n)
  45{
  46        int white, nwhite;
  47        char *ep;
  48        int nf;
  49
  50        if (p == NULL)
  51                return 1;
  52
  53        nf = 0;
  54        ep = p + n;
  55        white = 1;      /* first text will start field */
  56        while (p < ep) {
  57                /* UTF is irrelevant */
  58                nwhite = (strchr(" \t\r\n", *p++ & 0xFF) != 0);
  59                if (white && !nwhite)   /* beginning of field */
  60                        nf++;
  61                white = nwhite;
  62        }
  63        return nf + 1;  /* +1 for NULL */
  64}
  65
  66/*
  67 *  parse a command written to a device
  68 */
  69struct cmdbuf *parsecmd(char *p, size_t n)
  70{
  71        ERRSTACK(1);
  72        struct cmdbuf *volatile cb;
  73        int nf;
  74        char *sp;
  75
  76        nf = ncmdfield(p, n);
  77
  78        /* allocate Cmdbuf plus string pointers plus copy of string including \0
  79         */
  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]);
  84
  85        if (current != NULL && waserror()) {
  86                kfree(cb);
  87                nexterror();
  88        }
  89        memmove(cb->buf, p, n);
  90        if (current != NULL)
  91                poperror();
  92
  93        /* dump new line and null terminate */
  94        if (n > 0 && cb->buf[n - 1] == '\n')
  95                n--;
  96        cb->buf[n] = '\0';
  97
  98        cb->nf = tokenize(cb->buf, cb->f, nf - 1);
  99        cb->f[cb->nf] = NULL;
 100
 101        return cb;
 102}
 103
 104/*
 105 * Reconstruct original message, for error diagnostic
 106 */
 107void cmderror(struct cmdbuf *cb, char *s)
 108{
 109        int i;
 110        char *p, *e;
 111
 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++) {
 116                if (i > 0)
 117                        p = seprintf(p, e, " ");
 118                p = seprintf(p, e, "%s", cb->f[i]);
 119        }
 120        seprintf(p, e, "\"");
 121        error(EFAIL, "%s", get_cur_genbuf());
 122}
 123
 124void debugcmd(struct cmdbuf *cb)
 125{
 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]);
 129        }
 130}
 131
 132/*
 133 * Look up entry in table
 134 */
 135struct cmdtab *lookupcmd(struct cmdbuf *cb, struct cmdtab *ctab, int nctab)
 136{
 137        int i;
 138        struct cmdtab *ct;
 139
 140        if (cb->nf == 0)
 141                error(EFAIL, "empty control message");
 142
 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)
 146                                continue;
 147                if (ct->narg != 0 && ct->narg != cb->nf)
 148                        cmderror(cb, "wrong number of args");
 149                return ct;
 150        }
 151
 152        cmderror(cb, "unknown control message");
 153        return NULL;
 154}
 155