Commits

juster committed 0d9a346

Remove backslashes only in double quotes and brackets.

  • Participants
  • Parent commits 4e32ac1

Comments (0)

Files changed (1)

File vars/expvars

 
 {
 	for (i=2; i<=NF; i++) $i = expand($i)
-	PBVARS[$1] = $2
+	PBVARS[$1] = $2 # TODO: expanding ${arrays[@]}?
 	print
 }
 
 	while(j <= length(line)){
 		ch = substr(line, j, 1)
 
-		if (ch == "\\" || esc) { esc = !esc; j++; continue }
 		if (ch ~ /[ \t]/ && top == 0) die("Spaces must be quoted.")
 
+		if (ch == "\\" || esc) {
+			if(!esc && toks[top] !~ /[`\)]/){
+				# Don't remove backslashes inside command expansions
+				line = splice(line, "", j, j)
+			}else{
+				j++
+			}
+			esc = !esc
+		}
 		# Single quotes are simple characters when inside double-quotes.
 		# Closing single quotes cannot be escaped so just find the next one.
-		if (ch == "'" && toks[top] !~ /["`\)]/) {
+		else if (ch == "'" && toks[top] !~ /["`\)]/) {
 			start = j
 			stop = 0
 			for (j++; j <= length(line); j++) {
 }
 
 # Splice one string into another, replacing a chunk of it.
-# Params: Target str, Splicing str, Start of replaced, End of replaced.
-# This also adjusts j, the character pos in the main loop!
-function splice (rawstr, xstr, beg, end)
+# Params: Spliced string, Splicing str, Start of replaced, End of replaced.
+# Returns the result and adjusts j which is a var in expand.
+function splice(str, xstr, beg, end)
 {
-	rawstr = substr(rawstr, 1, beg-1) xstr substr(rawstr, end+1)
+	str = substr(str, 1, beg-1) xstr substr(str, end+1)
 	j = beg + length(xstr)
-	return rawstr
+	return str
 }
 
 # TODO: removing surrounding quotes, parens, brackets before passing qstr here