Esempio n. 1
0
func Readline(prompt string) (string, error) {
	c_prompt := C.CString(prompt)
	defer C.free(unsafe.Pointer(c_prompt))

	c_line := C.readline(c_prompt)
	defer C.free(unsafe.Pointer(c_line))
	line := C.GoString(c_line)

	if c_line == nil {
		return "", errors.New("C.readline call failed")
	}
	C.add_history(c_line)

	// append to file
	f, e := os.OpenFile(history_path, os.O_APPEND|os.O_WRONLY, 0600)
	if e == nil {
		defer f.Close()

		_, e = f.WriteString(line + "\n")
		if e != nil {
			fmt.Printf("error writing to history")
		}
	}

	return line, nil
}
Esempio n. 2
0
func (c *console) Run() {
	defer c.Stopped()

	re := regexp.MustCompile(`[\w\:\.]+`)
	prompt := C.CString("god> ")
	defer C.free(unsafe.Pointer(prompt))

	var line string

	for !c.StopRequested() {
		cline := C.readline(prompt)
		defer C.free(unsafe.Pointer(cline))
		if cline == nil {
			fmt.Printf("\n")
			break
		}

		C.add_history(cline)
		line = C.GoString(cline)
		args := re.FindAllString(line, -1)
		if len(args) > 0 {
			f := c.funcs[args[0]]
			if f != nil {
				var ret interface{}
				ext.PCall(
					func() {
						ret = f(args[1:])
					})
				ext.LogInfo("RUN_COMMAND\t%s\t%s\t%v\n", args[0], args[1:], ret)
			}
		}
	}
}
Esempio n. 3
0
// Read a line with the given prompt. The prompt can contain ANSI
// escape sequences, they will be escaped as necessary.
func String(prompt string) (string, error) {
	prompt = "\x1b[0m" + prompt // Prepend a 'reset' ANSI escape sequence
	prompt = escapeSeq.ReplaceAllString(prompt, promptStartIgnore+"$0"+promptEndIgnore)
	p := C.CString(prompt)
	rp := C.readline(p)
	s := C.GoString(rp)
	C.free(unsafe.Pointer(p))
	if rp != nil {
		C.free(unsafe.Pointer(rp))
		return s, nil
	}
	return s, io.EOF
}
Esempio n. 4
0
// Rlwrap prompts the user with the given prompt string and calls the
// underlying readline function. If the input stream closes, Rlwrap returns an
// EOF error.
func Rlwrap(prompt string, record bool) (string, error) {
	p := C.CString(prompt)
	defer C.free(unsafe.Pointer(p))

	ret := C.readline(p)
	if ret == nil {
		return "", errors.New("EOF")
	}
	defer C.free(unsafe.Pointer(ret))

	if record {
		C.add_history(ret)
	}

	return C.GoString(ret), nil
}
Esempio n. 5
0
// ReadLine prints a prompt and then reads and returns a single line of text from the user.
// If ReadLine encounters an EOF while reading the line, and the line is empty at that point, then true is returned.
// Otherwise, the line is ended just as if a newline had been typed.
// (See readline http://cnswww.cns.cwru.edu/php/chet/readline/readline.html#IDX190)
func ReadLine(prompt string) (string, bool) {
	var cprompt *C.char
	if len(prompt) != 0 {
		cprompt = C.CString(prompt)
	}
	cline := C.readline(cprompt)
	if cprompt != nil {
		C.free(unsafe.Pointer(cprompt))
	}
	if cline == nil {
		return "", true
	}
	line := C.GoString(cline)
	C.free(unsafe.Pointer(cline))
	return line, false
}
Esempio n. 6
0
func ReadLine(prompt *string) *string {
	var cPrompt *C.char
	if prompt != nil {
		cPrompt = C.CString(*prompt)
	}
	cLine := C.readline(cPrompt)
	if cPrompt != nil {
		C.free(unsafe.Pointer(cPrompt))
	}
	if cLine == nil {
		return nil
	}

	line := C.GoString(cLine)
	C.free(unsafe.Pointer(cLine))
	return &line
}
Esempio n. 7
0
func ReadLine(prompt *string) *string {
	var p *C.char

	//readline allows an empty prompt(NULL)
	if prompt != nil {
		p = C.CString(*prompt)
	}

	ret := C.readline(p)

	if p != nil {
		C.free(unsafe.Pointer(p))
	}

	if ret == nil {
		return nil
	} //EOF

	s := C.GoString(ret)
	C.free(unsafe.Pointer(ret))
	return &s
}