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

Spaces in command are not being processed correctly. #57

Open
Figuera opened this issue Mar 3, 2022 · 3 comments
Open

Spaces in command are not being processed correctly. #57

Figuera opened this issue Mar 3, 2022 · 3 comments

Comments

@Figuera
Copy link

Figuera commented Mar 3, 2022

Commands with spaces (in file names for example) won't run no matter what.

Suppose I want to run the following command

retroarch Chrono Trigger.smc

In which "Chrono Trigger.smc" is the name of the file. In standard bash we can express the middle space by two different ways (that I know of):

  • Backslashs: retroarch Chrono\ Trigger.smc
  • Double Quotes: retroarch "Chrono Trigger.smc"

Ideally they would both work in nwg-drawer but it seems that none of these does.

I tried to go through the source code but couldn't find a clear path to fix it.

@Figuera
Copy link
Author

Figuera commented Mar 6, 2022

I wrote two functions to correct the behavior:

func parseString(s string) []string {
	var r = regexp.MustCompile(`(".*"|(\\\s|[^\s])+)`)
	var res = r.FindAllString(s, -1)
	return res
}

func parseString2(command string) []string {
	var elements []string
	var confirmed []string

	if !strings.Contains(command, "\"") {
		elements = append(elements, strings.Split(command, " ")...)
	} else {
		for strings.Contains(command, "\"") {
			quoteSplit := strings.SplitN(command, "\"", 2)
			elements = append(elements, strings.Split(quoteSplit[0], " ")...)
			quoteSplit = strings.SplitN(quoteSplit[1], "\"", 2)
			elements = append(elements, quoteSplit[0])
			command = quoteSplit[1]
		}
	}

	for i := 0; i < len(elements); i++ {
		if elements[i] != "" { // Ignore empty arguments
			element := elements[i]
			// If the argument end with "\ " then merge with next argument
			for element[len(element)-1] == '\\' {
				element = strings.Replace(element, "\\", "", 1)
				element = element + " " + elements[i+1]
				i++
			}
			confirmed = append(confirmed, element)
		}
	}

	return confirmed
}

Both seem to work fine in my testing but checking edge cases may be necessary, I don't like the second one as it is a little convoluted but running benchmarks proved that it is much faster than the regex option.

@RiedleroD
Copy link

I also encountered this issue in nwg-menu. This seems important!

@nwg-piotr
Copy link
Owner

I'll get back to it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants