Skip to content

Commit

Permalink
Revamp @define #140 : fixing tests for parseCli_2
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Sep 17, 2023
1 parent cfd1207 commit bbc6a37
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 24 deletions.
70 changes: 55 additions & 15 deletions makesure.awk
Original file line number Diff line number Diff line change
Expand Up @@ -812,42 +812,82 @@ function quicksortSwap(data, i, j, temp) {
data[i] = data[j]
data[j] = temp
}
function parseCli(line, res, pos,c,last,is_doll,c1) {
#
# parses: arg 'arg with spaces' $'arg with \' single quote' "arg ${WITH} $VARS"
#
## res[-7] = res len
## res - 0-based
## returns error if any
function parseCli_2(line, vars, res, pos,c,c1,isDoll,q,var,inDef,defVal,val,w,i) {
for (pos = 1; ;) {
while ((c = substr(line,pos,1)) == " " || c == "\t") pos++ # consume spaces
if ((c = substr(line,pos,1)) == "#" || c == "")
if (c == "#" || c == "")
return
else {
if ((is_doll = c == "$") && substr(line,pos + 1,1) == "'" || c == "'") { # start of string
if (is_doll)
if ((isDoll = substr(line,pos,2) == "$'") || c == "'" || c == "\"") { # start of string
if (isDoll)
pos++
q = isDoll ? "'" : c # quote
# consume quoted string
res[last = res[-7]++] = ""
res[last,"quote"] = is_doll ? "$" : "'"
while ((c = substr(line,++pos,1)) != "'") { # closing '
w = ""
while ((c = substr(line,++pos,1)) != q) { # closing ' or "
if (c == "")
return "unterminated argument"
else if (is_doll && c == "\\" && ((c1 = substr(line,pos + 1,1)) == "'" || c1 == c)) { # escaped ' or \
else if (c == "\\" && ((c1 = substr(line,pos + 1,1)) == "'" && isDoll || c1 == c || q == "\"" && (c1 == q || c1 == "$"))) { # escaped ' or \ or "
c = c1; pos++
} else if (c == "$" && q == "\"") {
var = ""
inDef = 0
defVal = ""
if ((c1 = substr(line,pos + 1,1)) == "{") {
for (pos++; (c = substr(line,++pos,1)) != "}";) { # till closing '}'
if (c == "")
return "unterminated argument"
if (!inDef && ":" == c && "-" == substr(line,pos + 1,1)) {
inDef = 1
c = substr(line,pos += 2,1)
}
if (inDef) {
if ("}" == c)
break
if ("\\" == c && ((c1 = substr(line,pos+1,1)) == "$" || c1 == "\\" || c1 == "}" || c1 == "\"")) {
c = c1; pos++
}
defVal = defVal c
} else
var = var c
}
} else
for (; (c = substr(line,pos + 1,1)) ~ /[_A-Za-z0-9]/; pos++)
var = var c
# print "var="var
if (var !~ /^[_A-Za-z][_A-Za-z0-9]*$/)
return "wrong var: '" var "'"
w = w ((val = vars[var]) != "" ? val : defVal)
continue
}
res[last] = res[last] c
w = w c
}
res[i=+res[-7]++,"quote"] = isDoll ? "$" : q
res[i] = w
if ((c = substr(line,++pos,1)) != "" && c != " " && c != "\t")
return "joined arguments"
} else {
# consume unquoted argument
res[last = res[-7]++] = c
w = c
while ((c = substr(line,++pos,1)) != "" && c != " " && c != "\t") { # whitespace denotes the end of arg
if (c == "'")
return "joined arguments"
res[last] = res[last] c
w = w c
}
if (w !~ /^[_A-Za-z0-9@.]+$/)
return "wrong unquoted: '" w "'"
res[i=+res[-7]++,"quote"] = "u"
res[i] = w
}
}
}
}
function reparseCli( res,i,err) {
err = parseCli($0, res)
function reparseCli( todo,res,i,err) {
err = parseCli_2($0, todo, res)
if (err) {
addError("Syntax error: " err)
die(Error)
Expand Down
6 changes: 3 additions & 3 deletions tests/11_goal_glob.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

@goal @glob 11_goal_glob*.txt
@goal @glob '11_goal_glob*.txt'
@reached_if [[ $INDEX -eq 1 ]]
@doc test goal_glob
echo "$ITEM :: $INDEX :: $TOTAL"
cat $ITEM

@goal @glob non-existent-glob*
@goal @glob 'non-existent-glob*'
echo "wtf"

@goal test1
Expand All @@ -17,5 +17,5 @@
@depends_on glob_goal_name@11_goal_glob_2.txt
@depends_on glob_goal_name@11_goal_glob_3.txt

@goal glob_goal_name @glob 11_goal_glob*.txt
@goal glob_goal_name @glob '11_goal_glob*.txt'
echo "glob_goal_name ::: $ITEM :: $INDEX :: $TOTAL"
4 changes: 2 additions & 2 deletions tests/11_goal_glob_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
@lib
echo "Unnamed lib ::: $ITEM :: $INDEX :: $TOTAL"

@goal @glob 11_goal_glob*.txt
@goal @glob '11_goal_glob*.txt'
@use_lib
echo "@glob ::: $ITEM :: $INDEX :: $TOTAL"

@goal glob_goal_name @glob 11_goal_glob*.txt
@goal glob_goal_name @glob '11_goal_glob*.txt'
@use_lib lib_name
echo "glob_goal_name ::: $ITEM :: $INDEX :: $TOTAL"

Expand Down
2 changes: 1 addition & 1 deletion tests/21_parsing.tush
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ $ cd "$MYDIR"; ./$MAKESURE -f tests/21_parsing_err1.sh
? 1

$ cd "$MYDIR"; ./$MAKESURE -f tests/21_parsing_err2.sh
@ Syntax error: joined arguments:
@ Syntax error: wrong unquoted: 'doc'text':
@ tests/21_parsing_err2.sh:4: @doc doc'text
? 1
2 changes: 1 addition & 1 deletion tests/21_parsing_spaces.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


@goal @glob 21_parsing*.txt
@goal @glob '21_parsing*.txt'
echo "$ITEM"
cat "$ITEM"

Expand Down
2 changes: 1 addition & 1 deletion tests/22_nat_order.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

@options silent

@goal nat_order_test @glob *.tush
@goal nat_order_test @glob '*.tush'
@reached_if [[ $INDEX -ge 22 ]] # only check the correct order on first 22 tests
echo "$ITEM"
2 changes: 1 addition & 1 deletion tests/22_nat_order/Makesurefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

@options silent

@goal nat_order_test @glob ../*.tush
@goal nat_order_test @glob '../*.tush'
@reached_if [[ $INDEX -ge 22 ]] # only check the correct order on first 22 tests
echo "$ITEM"

0 comments on commit bbc6a37

Please sign in to comment.