waserror() audit
[akaros.git] / kern / src / ns / tokenize.c
1 // INFERNO
2 #include <vfs.h>
3 #include <kfs.h>
4 #include <slab.h>
5 #include <kmalloc.h>
6 #include <kref.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include <error.h>
11 #include <cpio.h>
12 #include <pmap.h>
13 #include <smp.h>
14 #include <ip.h>
15
16 static char qsep[] = " \t\r\n";
17
18 static char*
19 qtoken(char *s)
20 {
21         int quoting;
22         char *t;
23
24         quoting = 0;
25         t = s;  /* s is output string, t is input string */
26         while(*t!='\0' && (quoting || /*utfrune*/strchr(qsep, *t)==NULL)){
27                 if(*t != '\''){
28                         *s++ = *t++;
29                         continue;
30                 }
31                 /* *t is a quote */
32                 if(!quoting){
33                         quoting = 1;
34                         t++;
35                         continue;
36                 }
37                 /* quoting and we're on a quote */
38                 if(t[1] != '\''){
39                         /* end of quoted section; absorb closing quote */
40                         t++;
41                         quoting = 0;
42                         continue;
43                 }
44                 /* doubled quote; fold one quote into two */
45                 t++;
46                 *s++ = *t++;
47         }
48         if(*s != '\0'){
49                 *s = '\0';
50                 if(t == s)
51                         t++;
52         }
53         return t;
54 }
55
56 int
57 tokenize(char *s, char **args, int maxargs)
58 {
59         int nargs;
60
61         for(nargs=0; nargs<maxargs; nargs++){
62                 while(*s!='\0' && /*utfrune*/strchr(qsep, *s)!=NULL)
63                         s++;
64                 if(*s == '\0')
65                         break;
66                 args[nargs] = s;
67                 s = qtoken(s);
68         }
69
70         return nargs;
71 }