Commits

Taru Karttunen committed 16d0c2a

Pristine from upstream

Comments (0)

Files changed (3)

+# Copyright 2009 The Go Authors.  All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+include $(GOROOT)/src/Make.$(GOARCH)
+
+TARG=readline
+CGOFILES=readline.go
+CGO_LDFLAGS=-lreadline
+
+CLEANFILES+=readline_test
+
+include $(GOROOT)/src/Make.pkg
+
+%: install %.go
+	$(GC) $*.go
+	$(LD) -o $@ $*.$O
+// Wrapper around the GNU readline(3) library
+
+package readline
+
+// #include <readline/readline.h>
+// #include <readline/history.h>
+// #include <stdlib.h>
+import "C"
+import "unsafe"
+
+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
+}
+
+func AddHistory(s string) {
+	p := C.CString(s);
+	C.add_history(p);
+	C.free(unsafe.Pointer(p))
+}
+// test program for the readline package
+package main
+
+import "readline"
+
+func main() {
+	prompt := "by your command> ";
+
+	//loop until ReadLine returns nil (signalling EOF)
+	L: for {
+		switch result := readline.ReadLine(&prompt); true {
+		case result == nil: break L //exit loop
+
+		case *result != "": //ignore blank lines
+			println(*result);
+			readline.AddHistory(*result); //allow user to recall this line
+		}
+	}
+}