-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_cli_0_lib.awk
More file actions
48 lines (46 loc) · 1.51 KB
/
Copy pathparse_cli_0_lib.awk
File metadata and controls
48 lines (46 loc) · 1.51 KB
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
# we will parse
# aaa 'bbb "\' ccc' dd -> [aaa],[bbb "' ccc],[dd]
# 'a\nb' -> [a\nb] // only ' can be escaped, all rest is literal
# aaa'bbb' -> error
# 'bbb'aaa -> error
# 'aaa''bbb' -> error
# 'bbb -> error
# a 'b # c' # comment -> [a],[b # c] // parse comments
## res[-7] = res len
## res - 0-based
## returns error if any
function parseCli(line, res, pos,c,last) {
for(pos=1;;) {
trace(0,line,pos)
while((c = substr(line,pos,1))==" " || c == "\t") pos++ # consume spaces
trace(1,line,pos)
if (c=="#" || c=="")
return
else if (c == "'") { # start of string
# consume quoted string
res[last = res[-7]++] = ""
while((c = substr(line,++pos,1)) != "'") { # closing '
trace(2,line,pos)
if (c=="")
return "unterminated argument"
else if (c=="\\" && substr(line,pos+1,1)=="'") { # escaped '
c = "'"; pos++
}
res[last] = res[last] c
}
trace(3,line,pos)
if((c = substr(line,++pos,1)) != "" && c != " " && c != "\t")
return "joined arguments"
} else {
# consume unquoted argument
res[last = res[-7]++] = c
while((c = substr(line,++pos,1)) != "" && c != " " && c != "\t") { # whitespace denotes end of arg
trace(4,line,pos)
if(c=="'")
return "joined arguments"
res[last] = res[last] c
}
}
}
}
function trace(m,t,pos) { if (Trace) printf "%10s pos %d: %s\n", m, pos, substr(t,pos,10) "..." }