diff --git a/complete.go b/complete.go index 3f742e84..79c32521 100644 --- a/complete.go +++ b/complete.go @@ -256,7 +256,7 @@ func matchFile(s string) (matches []string, longest string) { name := f.Name() if isRoot(s) || filepath.Base(s) != s { - name = filepath.Join(filepath.Dir(s), f.Name()) + name = filepath.Join(filepath.Dir(unescape(s)), f.Name()) } name = escape(name) diff --git a/eval_test.go b/eval_test.go index 24947c63..6c30db81 100644 --- a/eval_test.go +++ b/eval_test.go @@ -67,8 +67,8 @@ var gEvalTests = []struct { { `echo "hello\zworld"`, - []string{"echo", "helloworld", "\n"}, - []expr{&callExpr{"echo", []string{"helloworld"}, 1}}, + []string{"echo", `hello\zworld`, "\n"}, + []expr{&callExpr{"echo", []string{`hello\zworld`}, 1}}, }, { @@ -90,7 +90,7 @@ var gEvalTests = []struct { }, { - `echo hello\ world`, + "echo hello\\\tworld", []string{"echo", "hello\tworld", "\n"}, []expr{&callExpr{"echo", []string{"hello\tworld"}, 1}}, }, @@ -109,8 +109,8 @@ var gEvalTests = []struct { { `echo hello\zworld`, - []string{"echo", "helloworld", "\n"}, - []expr{&callExpr{"echo", []string{"helloworld"}, 1}}, + []string{"echo", "hellozworld", "\n"}, + []expr{&callExpr{"echo", []string{"hellozworld"}, 1}}, }, { diff --git a/misc.go b/misc.go index f9c2dc2a..eea86dc9 100644 --- a/misc.go +++ b/misc.go @@ -41,22 +41,12 @@ func runeSliceWidthRange(rs []rune, beg, end int) []rune { return nil } -// This function is used to escape whitespaces with backlashes in a given -// string. If a whitespace is already escaped then it is not escaped again. +// This function is used to escape whitespaces and special characters with +// backlashes in a given string. func escape(s string) string { - esc := false buf := make([]rune, 0, len(s)) for _, r := range s { - if r == '\\' { - esc = true - continue - } - if esc { - esc = false - buf = append(buf, '\\', r) - continue - } - if unicode.IsSpace(r) { + if unicode.IsSpace(r) || r == '\\' || r == ';' || r == '#' { buf = append(buf, '\\') } buf = append(buf, r) @@ -65,24 +55,24 @@ func escape(s string) string { } // This function is used to remove backlashes that are used to escape -// whitespaces in a given string. +// whitespaces and special characters in a given string. func unescape(s string) string { esc := false buf := make([]rune, 0, len(s)) for _, r := range s { + if esc { + if !unicode.IsSpace(r) && r != '\\' && r != ';' && r != '#' { + buf = append(buf, '\\') + } + buf = append(buf, r) + esc = false + continue + } if r == '\\' { esc = true continue } - if esc { - esc = false - if unicode.IsSpace(r) { - buf = append(buf, r) - } else { - buf = append(buf, '\\', r) - } - continue - } + esc = false buf = append(buf, r) } return string(buf) diff --git a/misc_test.go b/misc_test.go index 72881b84..a3833011 100644 --- a/misc_test.go +++ b/misc_test.go @@ -80,8 +80,12 @@ func TestEscape(t *testing.T) { {"foo", "foo"}, {"foo bar", `foo\ bar`}, {"foo bar", `foo\ \ bar`}, - {`foo\ bar`, `foo\ bar`}, - {`foo\bar`, `foo\bar`}, + {`foo\bar`, `foo\\bar`}, + {`foo\ bar`, `foo\\\ bar`}, + {`foo;bar`, `foo\;bar`}, + {`foo#bar`, `foo\#bar`}, + {`foo\tbar`, `foo\\tbar`}, + {"foo\tbar", "foo\\\tbar"}, } for _, test := range tests { @@ -98,9 +102,14 @@ func TestUnescape(t *testing.T) { }{ {"", ""}, {"foo", "foo"}, - {`foo\bar`, `foo\bar`}, {`foo\ bar`, "foo bar"}, {`foo\ \ bar`, "foo bar"}, + {`foo\\bar`, `foo\bar`}, + {`foo\\\ bar`, `foo\ bar`}, + {`foo\;bar`, `foo;bar`}, + {`foo\#bar`, `foo#bar`}, + {`foo\\tbar`, `foo\tbar`}, + {"foo\\\tbar", "foo\tbar"}, } for _, test := range tests { diff --git a/scan.go b/scan.go index 61598767..a339e036 100644 --- a/scan.go +++ b/scan.go @@ -216,25 +216,24 @@ scan: for !s.eof && s.chr != '"' { if s.chr == '\\' { s.next() - switch s.chr { - case '"', '\\': + switch { + case s.chr == '"' || s.chr == '\\': buf = append(buf, s.chr) - case 'a': + case s.chr == 'a': buf = append(buf, '\a') - case 'b': + case s.chr == 'b': buf = append(buf, '\b') - case 'f': + case s.chr == 'f': buf = append(buf, '\f') - case 'n': + case s.chr == 'n': buf = append(buf, '\n') - case 'r': + case s.chr == 'r': buf = append(buf, '\r') - case 't': + case s.chr == 't': buf = append(buf, '\t') - case 'v': + case s.chr == 'v': buf = append(buf, '\v') - } - if isDigit(s.chr) { + case isDigit(s.chr): var oct []byte for isDigit(s.chr) { oct = append(oct, s.chr) @@ -245,13 +244,14 @@ scan: log.Printf("scanning: %s", err) } buf = append(buf, byte(n)) - } else { - s.next() + continue + default: + buf = append(buf, '\\', s.chr) } - } else { - buf = append(buf, s.chr) s.next() } + buf = append(buf, s.chr) + s.next() } s.typ = tokenIdent s.tok = string(buf) @@ -293,12 +293,9 @@ scan: for !s.eof && !isSpace(s.chr) && s.chr != ';' && s.chr != '#' { if s.chr == '\\' { s.next() - if isSpace(s.chr) || s.chr == '\\' { - buf = append(buf, s.chr) - s.next() - } else { - s.next() - } + buf = append(buf, s.chr) + s.next() + continue } buf = append(buf, s.chr) s.next()