Coverage Report

Created: 2023-09-25 06:32

/src/plan9port/src/lib9/tokenize.c
Line
Count
Source (jump to first uncovered line)
1
#include <lib9.h>
2
3
static char qsep[] = " \t\r\n";
4
5
static char*
6
qtoken(char *s, char *sep)
7
0
{
8
0
  int quoting;
9
0
  char *t;
10
11
0
  quoting = 0;
12
0
  t = s;  /* s is output string, t is input string */
13
0
  while(*t!='\0' && (quoting || utfrune(sep, *t)==nil)){
14
0
    if(*t != '\''){
15
0
      *s++ = *t++;
16
0
      continue;
17
0
    }
18
    /* *t is a quote */
19
0
    if(!quoting){
20
0
      quoting = 1;
21
0
      t++;
22
0
      continue;
23
0
    }
24
    /* quoting and we're on a quote */
25
0
    if(t[1] != '\''){
26
      /* end of quoted section; absorb closing quote */
27
0
      t++;
28
0
      quoting = 0;
29
0
      continue;
30
0
    }
31
    /* doubled quote; fold one quote into two */
32
0
    t++;
33
0
    *s++ = *t++;
34
0
  }
35
0
  if(*s != '\0'){
36
0
    *s = '\0';
37
0
    if(t == s)
38
0
      t++;
39
0
  }
40
0
  return t;
41
0
}
42
43
static char*
44
etoken(char *t, char *sep)
45
0
{
46
0
  int quoting;
47
48
  /* move to end of next token */
49
0
  quoting = 0;
50
0
  while(*t!='\0' && (quoting || utfrune(sep, *t)==nil)){
51
0
    if(*t != '\''){
52
0
      t++;
53
0
      continue;
54
0
    }
55
    /* *t is a quote */
56
0
    if(!quoting){
57
0
      quoting = 1;
58
0
      t++;
59
0
      continue;
60
0
    }
61
    /* quoting and we're on a quote */
62
0
    if(t[1] != '\''){
63
      /* end of quoted section; absorb closing quote */
64
0
      t++;
65
0
      quoting = 0;
66
0
      continue;
67
0
    }
68
    /* doubled quote; fold one quote into two */
69
0
    t += 2;
70
0
  }
71
0
  return t;
72
0
}
73
74
int
75
gettokens(char *s, char **args, int maxargs, char *sep)
76
0
{
77
0
  int nargs;
78
79
0
  for(nargs=0; nargs<maxargs; nargs++){
80
0
    while(*s!='\0' && utfrune(sep, *s)!=nil)
81
0
      *s++ = '\0';
82
0
    if(*s == '\0')
83
0
      break;
84
0
    args[nargs] = s;
85
0
    s = etoken(s, sep);
86
0
  }
87
88
0
  return nargs;
89
0
}
90
91
int
92
tokenize(char *s, char **args, int maxargs)
93
0
{
94
0
  int nargs;
95
96
0
  for(nargs=0; nargs<maxargs; nargs++){
97
0
    while(*s!='\0' && utfrune(qsep, *s)!=nil)
98
0
      s++;
99
0
    if(*s == '\0')
100
0
      break;
101
0
    args[nargs] = s;
102
0
    s = qtoken(s, qsep);
103
0
  }
104
105
0
  return nargs;
106
0
}