This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
rdflags.cpp
142 lines (132 loc) · 4.2 KB
/
rdflags.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Copyright (c) 2015 Piotr Stolarz
scriptext: Various scripting utilities WinDbg extension
Distributed under the GNU General Public License (the License)
see accompanying file LICENSE for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
*/
#include <common.h>
/* exported; see header for details */
size_t read_flags(const char *pc_in, flag_desc_t *p_fdsc)
{
int state=0;
char arg_delim;
size_t i, j, arg_i;
/* clear output fields */
for (j=0; p_fdsc[j].c_flag; j++) {
p_fdsc[j].is_pres=0;
p_fdsc[j].has_dups=0;
p_fdsc[j].has_esc=0;
p_fdsc[j].arg_len=0;
p_fdsc[j].pc_arg=NULL;
}
for (i=0; pc_in[i] && state>=0; i++)
{
switch (state)
{
/* read until flag prefix: '-' */
case 0:
if (!isspace(pc_in[i])) {
if (pc_in[i]=='-') state=1;
else {
/* no more flags; finish parsing */
i--; state=-1;
}
}
break;
/* recognize flag marker */
case 1:
if (isspace(pc_in[i])) {
/* no flag provided; prepare for reading the next flag */
state=0;
} else
if (pc_in[i]=='"' || pc_in[i]=='\'') {
/* arg to unknown flag; finish parsing */
i--; state=-1;
} else
if (pc_in[i]=='-') {
/* ignore sequences of '-' (no long arg supported) */
} else {
for (j=0; p_fdsc[j].c_flag; j++)
{
if (p_fdsc[j].c_flag==pc_in[i]) {
if (p_fdsc[j].is_pres) {
p_fdsc[j].has_dups=1;
p_fdsc[j].has_esc=0;
p_fdsc[j].arg_len=0;
p_fdsc[j].pc_arg=NULL;
} else {
p_fdsc[j].is_pres=1;
}
break;
}
}
if (p_fdsc[j].c_flag) {
if (p_fdsc[j].allow_arg) {
arg_i=j;
state=2;
}
} else {
/* unknown flag, ignore it and read the next one */
}
}
break;
/* recognize flag arg delimiter
input:
arg_i: index of flag desc
*/
case 2:
/* read until start of arg */
if (!isspace(pc_in[i])) {
if (pc_in[i]=='-') {
/* no arg provided */
state=1; continue;
} else
if (pc_in[i]=='"') {
arg_delim='"';
p_fdsc[arg_i].pc_arg = (char*)&pc_in[i+1];
} else
if (pc_in[i]=='\'') {
arg_delim='\'';
p_fdsc[arg_i].pc_arg = (char*)&pc_in[i+1];
} else {
arg_delim=0;
p_fdsc[arg_i].arg_len++;
p_fdsc[arg_i].pc_arg = (char*)&pc_in[i];
}
state=3;
}
break;
/* read flag arg
input:
arg_delim; 0:white space
arg_i: index of flag desc */
case 3:
if (!arg_delim) {
if (isspace(pc_in[i])) state=0;
else
if (pc_in[i]=='-') state=1;
else
p_fdsc[arg_i].arg_len++;
} else {
if (arg_delim==pc_in[i]) state=1;
else {
if (pc_in[i]=='\\') state=4;
p_fdsc[arg_i].arg_len++;
}
}
break;
/* escaped char in apostrophed flag arg */
case 4:
if (pc_in[i]=='"' || pc_in[i]=='\'') {
p_fdsc[arg_i].has_esc=1;
}
p_fdsc[arg_i].arg_len++;
state=3;
break;
}
}
return i;
}