Skip to content

Instantly share code, notes, and snippets.

@elazarl
Created November 5, 2013 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elazarl/7315150 to your computer and use it in GitHub Desktop.
Save elazarl/7315150 to your computer and use it in GitHub Desktop.
No backtrace for Go calls across SOs
package main
func Panic() { panic("oooh, no stacktrace") }
func make_bt_longer() {
Panic()
}
func main() {
Completer = func (text string, start, end int) (replacement string, options []string) {
make_bt_longer()
return
}
Readline("press tab and see no bt> ")
}
package main
/*
#cgo darwin CFLAGS: -I/opt/local/include
#cgo darwin LDFLAGS: -L/opt/local/lib
#cgo LDFLAGS: -lreadline
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "readline/readline.h"
#include "readline/history.h"
extern void setup_readline_completion();
*/
import "C"
import "unsafe"
func init() {
C.setup_readline_completion()
}
type CompleteFunc func (text string, start, end int) (replacement string, options []string)
var Completer CompleteFunc
//export completer
func completer(line *C.char, start, end int) **C.char {
if Completer == nil {
return nil
}
replacement, options := Completer(C.GoString(C.rl_line_buffer), start, end)
raw := C.calloc((C.size_t)(unsafe.Sizeof((*C.char)(nil))), (C.size_t)(len(options) + 2))
rv := (*[1<<31](*C.char))(raw)
rv[0] = C.CString(replacement)
for i, w := range options {
rv[i+1] = C.CString(w)
}
return (**C.char)(raw)
}
func Readline(prompt string) (string, bool) {
line := C.readline(C.CString(prompt))
if line == nil {
return "", false
}
return C.GoString(line), true
}
#include <stdio.h>
#include "readline/readline.h"
// exported in readline.go
extern char** completer(char*, int, int);
void setup_readline_completion() {
rl_attempted_completion_function = (rl_completion_func_t*)completer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment