akaros/user/ndblib/ndbparse.c
<<
>>
Prefs
   1/* 
   2 * This file is part of the UCB release of Plan 9. It is subject to the license
   3 * terms in the LICENSE file found in the top-level directory of this
   4 * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
   5 * part of the UCB release of Plan 9, including this file, may be copied,
   6 * modified, propagated, or distributed except according to the terms contained
   7 * in the LICENSE file.
   8 */
   9#include <stdlib.h>
  10
  11#include <stdio.h>
  12#include <parlib/parlib.h>
  13#include <unistd.h>
  14#include <signal.h>
  15#include <fcntl.h>
  16#include <ctype.h>
  17#include <iplib/iplib.h>
  18#include <ndblib/ndb.h>
  19
  20/*
  21 *  Parse a data base entry.  Entries may span multiple
  22 *  lines.  An entry starts on a left margin.  All subsequent
  23 *  lines must be indented by white space.  An entry consists
  24 *  of tuples of the forms:
  25 *      attribute-name
  26 *      attribute-name=value
  27 *      attribute-name="value with white space"
  28 *
  29 *  The parsing returns a 2-dimensional structure.  The first
  30 *  dimension joins all tuples. All tuples on the same line
  31 *  form a ring along the second dimension.
  32 */
  33
  34/*
  35 *  parse the next entry in the file
  36 */
  37struct ndbtuple*
  38ndbparse(struct ndb *db)
  39{
  40        char *line;
  41        struct ndbtuple *t;
  42        struct ndbtuple *first, *last;
  43        int len;
  44
  45        first = last = 0;
  46        for(;;){
  47                if((line = fgets(db->buf, sizeof(db->buf),db->b)) == 0)
  48                        break;
  49                len = strlen(db->buf);
  50                if(line[len-1] != '\n')
  51                        break;
  52                if(first && !isspace(*line) && *line != '#'){
  53                        fseek(db->b, -len, 1);
  54                        break;
  55                }
  56                t = _ndbparseline(line);
  57                if(t == 0)
  58                        continue;
  59                if(first)
  60                        last->entry = t;
  61                else
  62                        first = t;
  63                last = t;
  64                while(last->entry)
  65                        last = last->entry;
  66        }
  67        ndbsetmalloctag(first, getcallerpc(&db));
  68        return first;
  69}
  70