示例#1
0
文件: gosh.go 项目: kissthink/gosh-2
func main() {
	shell.Sh.InitShell()
	env := shell.Sh.GetEnv()

	for {
		prompt := env.GetEnvVar("prompt")
		input := readline.ReadLine(&prompt)

		if *input == "" {
			continue
		}

		if env.VarCmp("history", "on") == true {
			readline.AddHistory(*input)
		}

		c := cmd.ParseInput(*input)
		builtin := builtins.CheckBuiltin(c)
		if builtin != nil {
			if err := builtin(c); err != nil {
				fmt.Println(err)
			}
			continue
		} else {
			if err := executeCommand(c); err != nil {
				fmt.Println(err)
			}
		}
	}
}
示例#2
0
// Parse a single command and execute it. (REPL without the loop)
// Return true when the quit command is given.
func (s Session) repl() bool {
	prompt := fmt.Sprintf(colorize(C_Prompt, "%s://%s%s: "), s.scheme, s.host, *s.path)
	var err error
	var line string
	for {
		pline := readline.ReadLine(&prompt)
		if err != nil {
			fmt.Println()
			return true
		}
		line = *pline
		line = strings.Trim(line, "\n")
		line = strings.Trim(line, "\r")
		if line != "" {
			break
		}
	}
	if match, _ := regexp.MatchString("^(/[^ \t]*)|(\\.\\.)$", line); match {
		if line == "/" || line == "//" {
			*s.path = "/"
		} else {
			*s.path = strings.Replace(path.Clean(path.Join(*s.path, line)), "\\", "/", -1)
			if len(line) > 1 && line[len(line)-1] == '/' {
				*s.path += "/"
			}
		}
		return false
	}
	re := regexp.MustCompile("^([a-zA-Z][a-zA-Z0-9\\-]+):(.*)")
	if match := re.FindStringSubmatch(line); match != nil {
		key := match[1]
		val := strings.TrimSpace(match[2])
		if len(val) > 0 {
			s.headers.Set(key, val)
		}
		return false
	}
	re = regexp.MustCompile("^([A-Z]+)(.*)")
	if match := re.FindStringSubmatch(line); match != nil {
		method := match[1]
		p := strings.TrimSpace(match[2])
		trailingSlash := (len(*s.path) > 1) && ((*s.path)[len(*s.path)-1] == '/')
		if len(p) == 0 {
			p = "/"
		} else {
			trailingSlash = p[len(p)-1] == '/'
		}
		p = strings.Replace(path.Clean(path.Join(*s.path, p)), "\\", "/", -1)
		if trailingSlash {
			p += "/"
		}
		data := ""
		if method == "POST" || method == "PUT" {
			prompt = colorize(C_Prompt, "...: ")
			pline := readline.ReadLine(&prompt)
			if pline == nil {
				return false
			}
			data = *pline
		}
		readline.AddHistory(line)
		s.perform(method, s.scheme+"://"+s.host+p, data)
		return false
	}
	if line == ".h" || line == ".headers" {
		for key, arr := range s.headers {
			for _, val := range arr {
				fmt.Println(key + ": " + val)
			}
		}
		return false
	}
	if line == ".c" || line == ".cookies" {
		for _, cookie := range *s.cookies {
			for key, val := range cookie.Items {
				fmt.Println(key + ": " + val)
			}
		}
		return false
	}
	if line == ".v" || line == ".verbose" {
		*verbose = !*verbose
		return false
	}
	if line == ".o" || line == ".options" {
		fmt.Printf("useSSL=%v, rememberCookies=%v, verbose=%v\n", *useSSL, *rememberCookies, *verbose)
		return false
	}
	if line == ".?" || line == ".help" {
		fmt.Println(".headers, .h    show active request headers\n" +
			".options, .o    show options\n" +
			".cookies, .c    show client cookies\n" +
			".help, .?       display this message\n" +
			".exit, .q, ^D   exit console\n")
		return false
	}
	if line == ".q" || line == ".exit" {
		return true
	}
	fmt.Fprintln(os.Stderr, "unknown command:", line)
	return false
}