mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 21:13:18 -07:00
Fix #54: cmd parsing improved. It is possible set multiple command on ticker.commands with single or double quote.
This commit is contained in:
parent
0fee675299
commit
b83823283d
1 changed files with 55 additions and 2 deletions
|
@ -52,14 +52,67 @@ type Session struct {
|
||||||
Events *EventPool `json:"-"`
|
Events *EventPool `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseCommands(buffer string) []string {
|
func ParseCommands(line string) []string {
|
||||||
|
args := []string{}
|
||||||
|
buf := ""
|
||||||
|
|
||||||
|
singleQuoted := false
|
||||||
|
doubleQuoted := false
|
||||||
|
finish := false
|
||||||
|
|
||||||
|
for _, c := range line {
|
||||||
|
switch c {
|
||||||
|
case ';':
|
||||||
|
if singleQuoted == false && doubleQuoted == false {
|
||||||
|
finish = true
|
||||||
|
} else {
|
||||||
|
buf += string(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
case '"':
|
||||||
|
if doubleQuoted {
|
||||||
|
// finish of quote
|
||||||
|
doubleQuoted = false
|
||||||
|
} else if singleQuoted {
|
||||||
|
// quote initiated with ', so we ignore it
|
||||||
|
buf += string(c)
|
||||||
|
} else {
|
||||||
|
// quote init here
|
||||||
|
doubleQuoted = true
|
||||||
|
}
|
||||||
|
|
||||||
|
case '\'':
|
||||||
|
if singleQuoted {
|
||||||
|
singleQuoted = false
|
||||||
|
} else if doubleQuoted {
|
||||||
|
buf += string(c)
|
||||||
|
} else {
|
||||||
|
singleQuoted = true
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
buf += string(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
if finish {
|
||||||
|
args = append(args, buf)
|
||||||
|
finish = false
|
||||||
|
buf = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(buf) > 0 {
|
||||||
|
args = append(args, buf)
|
||||||
|
}
|
||||||
|
|
||||||
cmds := make([]string, 0)
|
cmds := make([]string, 0)
|
||||||
for _, cmd := range strings.Split(buffer, ";") {
|
for _, cmd := range args {
|
||||||
cmd = core.Trim(cmd)
|
cmd = core.Trim(cmd)
|
||||||
if cmd != "" || (len(cmd) > 0 && cmd[0] != '#') {
|
if cmd != "" || (len(cmd) > 0 && cmd[0] != '#') {
|
||||||
cmds = append(cmds, cmd)
|
cmds = append(cmds, cmd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmds
|
return cmds
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue