Skip to content

Commit

Permalink
Implement smart func to split command line into command and arguments
Browse files Browse the repository at this point in the history
strings.Fields did not count quotes, which causes some application
files to fail to start up.
  • Loading branch information
yamnikov-oleg committed Feb 3, 2017
1 parent 33caa82 commit 1e98b9d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (iter UiTreeIter) Execute() {
}
var val glib.GValue
Ui.ListStore.GetValue(iter.TreeIter, 2, &val)
cmd := strings.Fields(val.GetString())
cmd := SplitCommandline(val.GetString())
exec.Command(cmd[0], cmd[1:]...).Start()
MakeHistRecord(HistRecord{
Name: iter.Name(),
Expand Down
20 changes: 20 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (
EnvVarRegexp = regexp.MustCompile(`\$(\w+)`)
UrlSchemaRegexp = regexp.MustCompile(`^\w+://`)
HttpUrlSchemaRegexp = regexp.MustCompile(`^(\w+\.)+(\w+)(/.*)?$`)
CmdlineComponentRegexp = regexp.MustCompile(`(?:([^'"\s]+)|'([^']+)'|"([^"]+)")`)
)

func EscapeAmpersand(s string) string {
Expand Down Expand Up @@ -55,3 +56,22 @@ func IsUrl(query string) bool {
func IsHttpUrl(query string) bool {
return HttpUrlSchemaRegexp.MatchString(query)
}

func SplitCommandline(cmdline string) []string {
components := []string{}

matches := CmdlineComponentRegexp.FindAllStringSubmatch(cmdline, -1)
for _, match := range matches{
fstNonEmptyGrp := match[0]
for _, g := range match[1:] {
if g != "" {
fstNonEmptyGrp = g
break
}
}

components = append(components, fstNonEmptyGrp)
}

return components
}

0 comments on commit 1e98b9d

Please sign in to comment.