Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bash: fix path containing space with ~ prefix #1114

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func complete(cmd *cobra.Command, args []string) (string, error) {
case "bash": // TODO what about oil and such?
LOG.Printf("COMP_LINE is %#v", os.Getenv("COMP_LINE"))
LOG.Printf("COMP_POINT is %#v", os.Getenv("COMP_POINT"))
LOG.Printf("COMP_WORDBREAKS is %#v", os.Getenv("COMP_WORDBREAKS"))
var err error
args, err = bash.Patch(args) // handle redirects
LOG.Printf("patching args to %#v", args)
Expand Down
44 changes: 30 additions & 14 deletions internal/shell/bash/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,36 @@ var sanitizer = strings.NewReplacer(
"\t", ``,
)

var valueReplacer = strings.NewReplacer(
var escapingQuotedReplacer = strings.NewReplacer(
`\`, `\\`,
`"`, `\"`,
`$`, `\$`,
"`", "\\`",
)

var escapingReplacer = strings.NewReplacer(
`\`, `\\`,
`&`, `\&`,
`<`, `\<`,
`>`, `\>`,
"`", "\\`",
`'`, `\'`,
`"`, `\"`,
`{`, `\{`,
`}`, `\}`,
`$`, `\$`,
`#`, `\#`,
`|`, `\|`,
`?`, `\?`,
`(`, `\(`,
`)`, `\)`,
`;`, `\;`,
` `, `\ `,
`[`, `\[`,
`]`, `\]`,
`*`, `\*`,
)

var displayReplacer = strings.NewReplacer(
`${`, `\\\${`,
)
Expand Down Expand Up @@ -81,19 +104,11 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
nospace = nospace || meta.Nospace.Matches(val.Value)

vals[index] = sanitizer.Replace(val.Value)
if requiresQuoting(vals[index]) {
vals[index] = valueReplacer.Replace(vals[index])
switch {
case strings.HasPrefix(vals[index], "~"): // assume homedir expansion
if splitted := strings.SplitAfterN(vals[index], "/", 2); len(splitted) == 2 {
vals[index] = fmt.Sprintf(`%v"%v"`, splitted[0], splitted[1])
} else {
// TODO homedir expansion won't work this way, but shouldn't reach this point anyway.
vals[index] = fmt.Sprintf(`~"%v"`, strings.TrimPrefix(vals[index], "~"))
}
default:
vals[index] = fmt.Sprintf(`"%v"`, vals[index])
}
switch {
case strings.HasPrefix(vals[index], "~"): // assume homedir expansion
vals[index] = escapingReplacer.Replace(vals[index])
case requiresQuoting(vals[index]):
vals[index] = fmt.Sprintf(`"%v"`, escapingQuotedReplacer.Replace(vals[index]))
}
} else {
nospace = true
Expand All @@ -111,6 +126,7 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu

func requiresQuoting(s string) bool {
chars := " \t\r\n`" + `[]{}()<>;|$&:*#`
chars += `'"`
chars += os.Getenv("COMP_WORDBREAKS")
chars += `\`
return strings.ContainsAny(s, chars)
Expand Down