1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
package session
import (
"fmt"
"strings"
"github.com/evilsocket/islazy/str"
)
func ParseCommands(line string) []string {
args := []string{}
buf := ""
singleQuoted := false
doubleQuoted := false
finish := false
line = strings.Replace(line, `""`, `"<empty>"`, -1)
line = strings.Replace(line, `''`, `"<empty>"`, -1)
for _, c := range line {
switch c {
case ';':
if !singleQuoted && !doubleQuoted {
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 {
buf = strings.Replace(buf, `<empty>`, `""`, -1)
args = append(args, buf)
finish = false
buf = ""
}
}
if len(buf) > 0 {
buf = strings.Replace(buf, `<empty>`, `""`, -1)
args = append(args, buf)
}
cmds := make([]string, 0)
for _, cmd := range args {
cmd = str.Trim(cmd)
if cmd != "" || (len(cmd) > 0 && cmd[0] != '#') {
cmds = append(cmds, cmd)
}
}
return cmds
}
func (s *Session) parseEnvTokens(str string) (string, error) {
for _, m := range reEnvVarCapture.FindAllString(str, -1) {
varName := strings.Trim(strings.Replace(m, "env.", "", -1), "{}")
if found, value := s.Env.Get(varName); found {
str = strings.Replace(str, m, value, -1)
} else {
return "", fmt.Errorf("variable '%s' is not defined", varName)
}
}
return str, nil
}
|