diff --git a/Makefile.am b/Makefile.am index ade16c2..d325e29 100644 --- a/Makefile.am +++ b/Makefile.am @@ -42,15 +42,18 @@ hclgo_DEPENDENCIES = hclgo.bin ## let the linker to move hclgo.bin to the actual target hclgo_LINK = mv -f hclgo.bin hclgo$(EXEEXT) || echo "FAILED TO LINK" -hclgo.bin: +hclgo.bin: lib/libhcl.la cp -pf $(srcdir)/go.mod $(builddir)/go.mod >/dev/null 2>&1 || true chmod u+w $(builddir)/go.mod ## with `make distcheck`, the echo's redirection to the file fails without this permission change sed -r -i -e "/^[[:space:]]*replace[[:space:]]+cfg[[:space:]]*=>/d" $(builddir)/go.mod echo -e "\nreplace cfg => $(abs_builddir)/go/cfg" >> $(builddir)/go.mod [ -f $(srcdir)/go.sum ] && cp -pf $(srcdir)/go.sum $(builddir)/go.sum >/dev/null 2>&1 || true + ## --------------------------------------------------------------- + CC=$(CC) \ CGO_CFLAGS="-I$(abs_srcdir)/lib -I$(abs_builddir)/lib $(CFLAGS)" \ CGO_LDFLAGS="-L$(abs_builddir)/lib -L$(abs_builddir)/lib/.libs -lhcl -ldl -lquadmath" \ go build -C $(srcdir) -x -o $(abs_builddir)/hclgo.bin -modfile $(abs_builddir)/go.mod + ## --------------------------------------------------------------- go clean -C $(srcdir) -x -modfile $(abs_builddir)/go.mod rm -rf $(builddir)/go.mod $(builddir)/go.sum diff --git a/Makefile.in b/Makefile.in index ff91cdb..fc1fea1 100644 --- a/Makefile.in +++ b/Makefile.in @@ -937,12 +937,13 @@ distclean-local: clean-local: rm -rf $(builddir)/go.mod $(builddir)/go.sum -@ENABLE_HCLGO_TRUE@hclgo.bin: +@ENABLE_HCLGO_TRUE@hclgo.bin: lib/libhcl.la @ENABLE_HCLGO_TRUE@ cp -pf $(srcdir)/go.mod $(builddir)/go.mod >/dev/null 2>&1 || true @ENABLE_HCLGO_TRUE@ chmod u+w $(builddir)/go.mod ## with `make distcheck`, the echo's redirection to the file fails without this permission change @ENABLE_HCLGO_TRUE@ sed -r -i -e "/^[[:space:]]*replace[[:space:]]+cfg[[:space:]]*=>/d" $(builddir)/go.mod @ENABLE_HCLGO_TRUE@ echo -e "\nreplace cfg => $(abs_builddir)/go/cfg" >> $(builddir)/go.mod @ENABLE_HCLGO_TRUE@ [ -f $(srcdir)/go.sum ] && cp -pf $(srcdir)/go.sum $(builddir)/go.sum >/dev/null 2>&1 || true +@ENABLE_HCLGO_TRUE@ CC=$(CC) \ @ENABLE_HCLGO_TRUE@ CGO_CFLAGS="-I$(abs_srcdir)/lib -I$(abs_builddir)/lib $(CFLAGS)" \ @ENABLE_HCLGO_TRUE@ CGO_LDFLAGS="-L$(abs_builddir)/lib -L$(abs_builddir)/lib/.libs -lhcl -ldl -lquadmath" \ @ENABLE_HCLGO_TRUE@ go build -C $(srcdir) -x -o $(abs_builddir)/hclgo.bin -modfile $(abs_builddir)/go.mod diff --git a/go/cb.go b/go/cb.go index 10bdb86..71f03be 100644 --- a/go/cb.go +++ b/go/cb.go @@ -80,8 +80,8 @@ func (io *IOHandleTable) slot_to_io_handle(slot int) IOHandle { var io_tab IOHandleTable = IOHandleTable{} -//export go_read_handler -func go_read_handler(c *C.hcl_t, cmd C.hcl_iocmd_t, arg unsafe.Pointer) C.int { +//export hcl_go_read_handler +func hcl_go_read_handler(c *C.hcl_t, cmd C.hcl_iocmd_t, arg unsafe.Pointer) C.int { var ( g *HCL err error @@ -154,8 +154,8 @@ func go_read_handler(c *C.hcl_t, cmd C.hcl_iocmd_t, arg unsafe.Pointer) C.int { return -1 } -//export go_scan_handler -func go_scan_handler(c *C.hcl_t, cmd C.hcl_iocmd_t, arg unsafe.Pointer) C.int { +//export hcl_go_scan_handler +func hcl_go_scan_handler(c *C.hcl_t, cmd C.hcl_iocmd_t, arg unsafe.Pointer) C.int { var ( g *HCL err error @@ -202,8 +202,8 @@ func go_scan_handler(c *C.hcl_t, cmd C.hcl_iocmd_t, arg unsafe.Pointer) C.int { return -1 } -//export go_print_handler -func go_print_handler(c *C.hcl_t, cmd C.hcl_iocmd_t, arg unsafe.Pointer) C.int { +//export hcl_go_print_handler +func hcl_go_print_handler(c *C.hcl_t, cmd C.hcl_iocmd_t, arg unsafe.Pointer) C.int { var ( g *HCL err error diff --git a/go/hcl.go b/go/hcl.go index 924cd0f..6333e69 100644 --- a/go/hcl.go +++ b/go/hcl.go @@ -3,6 +3,20 @@ package hcl /* #include #include + +extern int hcl_go_read_handler (hcl_t hcl, hcl_iocmd_t cmd, void* arg); +extern int hcl_go_scan_handler (hcl_t hcl, hcl_iocmd_t cmd, void* arg); +extern int hcl_go_print_handler (hcl_t hcl, hcl_iocmd_t cmd, void* arg); + +int hcl_read_handler_for_go (hcl_t hcl, hcl_iocmd_t cmd, void* arg) { + return hcl_go_read_handler(hcl, cmd, arg); +} +int hcl_scan_handler_for_go (hcl_t hcl, hcl_iocmd_t cmd, void* arg) { + return hcl_go_scan_handler(hcl, cmd, arg); +} +int hcl_print_handler_for_go (hcl_t hcl, hcl_iocmd_t cmd, void* arg) { + return hcl_go_print_handler(hcl, cmd, arg); +} */ import "C" @@ -12,24 +26,36 @@ import ( "unsafe" ) -type IOImpl interface { +type IOReadImpl interface { Open(g *HCL, name string, includer_name string) (int, error) Close(fd int) Read(fd int, buf []rune) (int, error) +} + +type IOScanImpl interface { + Open(g *HCL) error + Close() + Read(buf []rune) (int, error) +} + +type IOPrintImpl interface { + Open(g *HCL) error + Close() Write(data []rune) error WriteBytes(data []byte) error Flush() error } +type IOImplSet struct { + r IOReadImpl + s IOScanImpl + p IOPrintImpl +} + type HCL struct { c *C.hcl_t inst_no int - - io struct { - r IOImpl - s IOImpl - p IOImpl - } + io IOImplSet } type Ext struct { @@ -92,12 +118,22 @@ func (hcl *HCL) AddBuiltinPrims() error { return nil } -func (hcl *HCL) AttachIO(r IOImpl, w IOImpl, p IOImpl) error { +func (hcl *HCL) AttachIO(r IOReadImpl, s IOScanImpl, p IOPrintImpl) error { + var x C.int + var io IOImplSet + + io = hcl.io + hcl.io.r = r hcl.io.s = s hcl.io.p = p - if C.hcl_attachio(hcl.c, go_read_handler, go_scan_handler, go_print_handler) <= -1 { - // TODO: restore to the old value.. + + x = C.hcl_attachio(hcl.c, + C.hcl_ioimpl_t(C.hcl_read_handler_for_go), + C.hcl_ioimpl_t(C.hcl_scan_handler_for_go), + C.hcl_ioimpl_t(C.hcl_print_handler_for_go)) + if x <= -1 { + hcl.io = io // restore the set return fmt.Errorf("unable to attach I/O handlers: %s", string(ucstr_to_rune_slice(C.hcl_geterrstr(hcl.c)))) } return nil diff --git a/main.go b/main.go index 85e97dd..659bf7e 100644 --- a/main.go +++ b/main.go @@ -126,14 +126,14 @@ func main() { goto oops } - err = x.AttachIO(rfh, sfh, pfh) + err = x.AttachIO(&rfh, &sfh, &pfh) if err != nil { fmt.Printf("Error - %s", err.Error()) } - x.FeedString(`(printf ">>>>>>>>> [%d]\n" (+ 30 455)) + x.FeedString([]rune(`(printf ">>>>>>>>> [%d]\n" (+ 30 455)) (printf ">>>>>>>>> [%d]\n" (+ 11 455)) #include "a.hcl" - (printf ">>>>>>>>> [%d]\n" (+ 20 455))`) + (printf ">>>>>>>>> [%d]\n" (+ 20 455))`)) x.EndFeed() x.Execute()