2023-10-29 12:40:05 +00:00
|
|
|
package hcl
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <hcl.h>
|
|
|
|
#include <hcl-utl.h>
|
2023-11-05 14:33:22 +00:00
|
|
|
#include <stdlib.h> // for C.free
|
2023-10-31 16:42:47 +00:00
|
|
|
|
2024-02-24 08:32:42 +00:00
|
|
|
extern int hcl_go_cci_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg);
|
|
|
|
extern int hcl_go_udi_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg);
|
|
|
|
extern int hcl_go_udo_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg);
|
2023-10-31 16:42:47 +00:00
|
|
|
|
2024-02-24 08:32:42 +00:00
|
|
|
int hcl_cci_handler_for_go (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg) {
|
2023-11-05 13:31:33 +00:00
|
|
|
return hcl_go_cci_handler(hcl, cmd, arg);
|
2023-10-31 16:42:47 +00:00
|
|
|
}
|
2024-02-24 08:32:42 +00:00
|
|
|
int hcl_udi_handler_for_go (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg) {
|
2023-11-05 13:31:33 +00:00
|
|
|
return hcl_go_udi_handler(hcl, cmd, arg);
|
2023-10-31 16:42:47 +00:00
|
|
|
}
|
2024-02-24 08:32:42 +00:00
|
|
|
int hcl_udo_handler_for_go (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg) {
|
2023-11-05 13:31:33 +00:00
|
|
|
return hcl_go_udo_handler(hcl, cmd, arg);
|
2023-10-31 16:42:47 +00:00
|
|
|
}
|
2023-10-29 12:40:05 +00:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2023-11-03 09:04:46 +00:00
|
|
|
"bufio"
|
2023-10-29 12:40:05 +00:00
|
|
|
"fmt"
|
2023-11-03 09:04:46 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2023-10-29 12:40:05 +00:00
|
|
|
"runtime"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
type CciImpl interface {
|
2024-02-24 08:32:42 +00:00
|
|
|
Open(g *HCL, name string) (int, error)
|
2023-10-31 12:17:08 +00:00
|
|
|
Close(fd int)
|
|
|
|
Read(fd int, buf []rune) (int, error)
|
2023-10-31 16:42:47 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
type UdiImpl interface {
|
2023-10-31 16:42:47 +00:00
|
|
|
Open(g *HCL) error
|
|
|
|
Close()
|
|
|
|
Read(buf []rune) (int, error)
|
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
type UdoImpl interface {
|
2023-10-31 16:42:47 +00:00
|
|
|
Open(g *HCL) error
|
|
|
|
Close()
|
2023-10-31 12:17:08 +00:00
|
|
|
Write(data []rune) error
|
|
|
|
WriteBytes(data []byte) error
|
|
|
|
Flush() error
|
|
|
|
}
|
|
|
|
|
2023-10-29 12:40:05 +00:00
|
|
|
type HCL struct {
|
|
|
|
c *C.hcl_t
|
|
|
|
inst_no int
|
2023-11-05 14:33:22 +00:00
|
|
|
io struct {
|
|
|
|
cci CciImpl
|
|
|
|
cci_main string
|
|
|
|
udi UdiImpl
|
|
|
|
udo UdoImpl
|
|
|
|
}
|
2023-10-29 12:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Ext struct {
|
|
|
|
inst_no int
|
|
|
|
}
|
|
|
|
|
2024-09-10 03:30:26 +00:00
|
|
|
type Err struct {
|
|
|
|
Line uint
|
|
|
|
Colm uint
|
|
|
|
File string
|
|
|
|
Msg string
|
|
|
|
Tgt string
|
|
|
|
}
|
|
|
|
|
2023-11-01 07:06:28 +00:00
|
|
|
type BitMask C.hcl_bitmask_t
|
|
|
|
|
2024-01-21 16:38:13 +00:00
|
|
|
const TRAIT_LANG_ENABLE_EOL BitMask = C.HCL_TRAIT_LANG_ENABLE_EOL
|
|
|
|
|
2023-10-29 12:40:05 +00:00
|
|
|
var inst_table InstanceTable
|
|
|
|
|
|
|
|
func deregister_instance(g *HCL) {
|
|
|
|
if g.inst_no >= 0 {
|
|
|
|
inst_table.delete_instance(g.inst_no)
|
|
|
|
g.inst_no = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() (*HCL, error) {
|
|
|
|
var c *C.hcl_t
|
|
|
|
var g *HCL
|
|
|
|
var ext *Ext
|
|
|
|
var errnum C.hcl_errnum_t
|
|
|
|
|
|
|
|
c = C.hcl_openstd(C.hcl_oow_t(unsafe.Sizeof(*ext)), &errnum)
|
|
|
|
if c == nil {
|
|
|
|
var buf [64]C.hcl_uch_t
|
|
|
|
var ptr *C.hcl_uch_t
|
|
|
|
var err error
|
|
|
|
|
|
|
|
ptr = C.hcl_errnum_to_errucstr(errnum, &buf[0], C.hcl_oow_t(cap(buf)))
|
|
|
|
err = fmt.Errorf("%s", string(ucstr_to_rune_slice(ptr)))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ext = (*Ext)(unsafe.Pointer(C.hcl_getxtn(c)))
|
|
|
|
|
|
|
|
g = &HCL{c: c, inst_no: -1}
|
|
|
|
|
|
|
|
runtime.SetFinalizer(g, deregister_instance)
|
|
|
|
g.inst_no = inst_table.add_instance(c, g)
|
|
|
|
ext.inst_no = g.inst_no
|
|
|
|
|
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hcl *HCL) Close() {
|
|
|
|
C.hcl_close(hcl.c)
|
|
|
|
deregister_instance(hcl)
|
|
|
|
}
|
|
|
|
|
2024-09-10 03:30:26 +00:00
|
|
|
func (hcl *HCL) make_errinfo() *Err {
|
|
|
|
var loc C.hcl_loc_t
|
|
|
|
var err Err
|
|
|
|
var errnum C.hcl_errnum_t
|
|
|
|
|
|
|
|
err.Msg = hcl.get_errmsg()
|
|
|
|
|
|
|
|
errnum = C.hcl_geterrnum(hcl.c)
|
|
|
|
if errnum == C.HCL_ESYNERR {
|
|
|
|
var synerr C.hcl_synerr_t
|
|
|
|
C.hcl_getsynerr(hcl.c, &synerr)
|
|
|
|
loc = synerr.loc
|
|
|
|
|
|
|
|
err.Tgt = string(uchars_to_rune_slice(&synerr.tgt.val[0], uintptr(synerr.tgt.len)))
|
|
|
|
} else {
|
|
|
|
C.hcl_geterrloc(hcl.c, &loc)
|
|
|
|
}
|
|
|
|
|
|
|
|
err.Line = uint(loc.line)
|
|
|
|
err.Colm = uint(loc.colm)
|
|
|
|
if loc.file != nil {
|
|
|
|
err.File = string(ucstr_to_rune_slice(loc.file))
|
|
|
|
} else {
|
|
|
|
err.File = hcl.io.cci_main
|
|
|
|
}
|
|
|
|
return &err
|
|
|
|
}
|
|
|
|
|
2024-01-21 16:38:13 +00:00
|
|
|
func (hcl *HCL) GetTrait() BitMask {
|
|
|
|
var x C.int
|
|
|
|
var log_mask BitMask = 0
|
|
|
|
|
|
|
|
x = C.hcl_getoption(hcl.c, C.HCL_TRAIT, unsafe.Pointer(&log_mask))
|
|
|
|
if x <= -1 {
|
|
|
|
// this must not happen
|
|
|
|
panic(fmt.Errorf("unable to get log mask - %s", hcl.get_errmsg()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return log_mask
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hcl *HCL) SetTrait(log_mask BitMask) {
|
|
|
|
var x C.int
|
|
|
|
|
|
|
|
x = C.hcl_setoption(hcl.c, C.HCL_TRAIT, unsafe.Pointer(&log_mask))
|
|
|
|
if x <= -1 {
|
|
|
|
// this must not happen
|
|
|
|
panic(fmt.Errorf("unable to set log mask - %s", hcl.get_errmsg()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
func (hcl *HCL) GetLogMask() BitMask {
|
2023-11-01 07:06:28 +00:00
|
|
|
var x C.int
|
|
|
|
var log_mask BitMask = 0
|
|
|
|
|
|
|
|
x = C.hcl_getoption(hcl.c, C.HCL_LOG_MASK, unsafe.Pointer(&log_mask))
|
|
|
|
if x <= -1 {
|
|
|
|
// this must not happen
|
2023-11-05 14:33:22 +00:00
|
|
|
panic(fmt.Errorf("unable to get log mask - %s", hcl.get_errmsg()))
|
2023-11-01 07:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return log_mask
|
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
func (hcl *HCL) SetLogMask(log_mask BitMask) {
|
2023-11-01 07:06:28 +00:00
|
|
|
var x C.int
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
x = C.hcl_setoption(hcl.c, C.HCL_LOG_MASK, unsafe.Pointer(&log_mask))
|
2023-11-01 07:06:28 +00:00
|
|
|
if x <= -1 {
|
|
|
|
// this must not happen
|
2023-11-05 14:33:22 +00:00
|
|
|
panic(fmt.Errorf("unable to set log mask - %s", hcl.get_errmsg()))
|
2023-11-01 07:06:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
func (hcl *HCL) GetLogTarget() string {
|
2023-11-01 07:06:28 +00:00
|
|
|
var x C.int
|
|
|
|
var tgt *C.char
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
x = C.hcl_getoption(hcl.c, C.HCL_LOG_TARGET_BCSTR, unsafe.Pointer(&tgt))
|
2023-11-01 07:06:28 +00:00
|
|
|
if x <= -1 {
|
|
|
|
// this must not happen
|
2023-11-05 14:33:22 +00:00
|
|
|
panic(fmt.Errorf("unable to set log target - %s", hcl.get_errmsg()))
|
2023-11-01 07:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return C.GoString(tgt)
|
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
func (hcl *HCL) SetLogTarget(target string) {
|
2023-11-01 07:06:28 +00:00
|
|
|
var x C.int
|
|
|
|
var tgt *C.char
|
|
|
|
|
2024-02-24 13:53:34 +00:00
|
|
|
tgt = C.CString(target) // TODO: need error check?
|
2023-11-01 07:06:28 +00:00
|
|
|
defer C.free(unsafe.Pointer(tgt))
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
x = C.hcl_setoption(hcl.c, C.HCL_LOG_TARGET_BCSTR, unsafe.Pointer(tgt))
|
2023-11-01 07:06:28 +00:00
|
|
|
if x <= -1 {
|
2024-02-24 13:53:34 +00:00
|
|
|
// this must not happen
|
2023-11-05 14:33:22 +00:00
|
|
|
panic(fmt.Errorf("unable to set log target - %s", hcl.get_errmsg()))
|
2023-11-01 07:06:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 12:40:05 +00:00
|
|
|
func (hcl *HCL) Ignite(memsize uintptr) error {
|
2023-11-01 07:06:28 +00:00
|
|
|
var x C.int
|
|
|
|
|
|
|
|
x = C.hcl_ignite(hcl.c, C.hcl_oow_t(memsize))
|
|
|
|
if x <= -1 {
|
2024-09-10 03:30:26 +00:00
|
|
|
//return fmt.Errorf("unable to ignite - %s", hcl.get_errmsg())
|
|
|
|
return hcl.make_errinfo()
|
2023-10-29 12:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hcl *HCL) AddBuiltinPrims() error {
|
2023-11-01 07:06:28 +00:00
|
|
|
var x C.int
|
|
|
|
|
|
|
|
x = C.hcl_addbuiltinprims(hcl.c)
|
|
|
|
if x <= -1 {
|
2024-09-10 03:30:26 +00:00
|
|
|
//return fmt.Errorf("unable to add built-in primitives - %s", hcl.get_errmsg())
|
|
|
|
return hcl.make_errinfo()
|
2023-10-29 12:40:05 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
// the name of the main cci stream is required because:
|
|
|
|
// - the main stream is not handled by this IO handler
|
|
|
|
// - the feeder must read the main stream and pass data.
|
|
|
|
// - the inclusion of another file from the main stream requires the path information of the main strea.
|
|
|
|
func (hcl *HCL) AttachCCIO(cci CciImpl, main_cci_name string) error {
|
2023-10-31 16:42:47 +00:00
|
|
|
var x C.int
|
2023-11-05 14:33:22 +00:00
|
|
|
var old_cci CciImpl
|
|
|
|
var old_cci_name string
|
2023-10-31 16:42:47 +00:00
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
old_cci = hcl.io.cci
|
|
|
|
old_cci_name = hcl.io.cci_main
|
2023-10-31 16:42:47 +00:00
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
hcl.io.cci = cci
|
|
|
|
hcl.io.cci_main = main_cci_name
|
2023-11-05 07:58:45 +00:00
|
|
|
|
2024-02-24 08:32:42 +00:00
|
|
|
x = C.hcl_attachccio(hcl.c, C.hcl_io_impl_t(C.hcl_cci_handler_for_go))
|
2023-11-05 07:58:45 +00:00
|
|
|
if x <= -1 {
|
|
|
|
// restore the io handler set due to attachment failure
|
2023-11-05 14:33:22 +00:00
|
|
|
hcl.io.cci_main = old_cci_name
|
|
|
|
hcl.io.cci = old_cci
|
2024-09-10 03:30:26 +00:00
|
|
|
//return fmt.Errorf("unable to attach source input stream handler - %s", hcl.get_errmsg())
|
|
|
|
return hcl.make_errinfo()
|
2023-11-05 07:58:45 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
func (hcl *HCL) AttachUDIO(udi UdiImpl, udo UdoImpl) error {
|
2023-11-05 07:58:45 +00:00
|
|
|
var x C.int
|
2023-11-05 14:33:22 +00:00
|
|
|
var os UdiImpl
|
|
|
|
var op UdoImpl
|
2023-11-05 07:58:45 +00:00
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
os = hcl.io.udi
|
|
|
|
op = hcl.io.udo
|
2023-11-05 07:58:45 +00:00
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
hcl.io.udi = udi
|
|
|
|
hcl.io.udo = udo
|
2023-10-31 16:42:47 +00:00
|
|
|
|
2023-11-05 07:58:45 +00:00
|
|
|
x = C.hcl_attachudio(hcl.c,
|
2023-11-05 13:31:33 +00:00
|
|
|
C.hcl_io_impl_t(C.hcl_udi_handler_for_go),
|
|
|
|
C.hcl_io_impl_t(C.hcl_udo_handler_for_go))
|
2023-10-31 16:42:47 +00:00
|
|
|
if x <= -1 {
|
2023-11-05 07:58:45 +00:00
|
|
|
//restore the io handlers set due to attachment failure
|
2023-11-05 14:33:22 +00:00
|
|
|
hcl.io.udi = os
|
|
|
|
hcl.io.udo = op
|
2024-09-10 03:30:26 +00:00
|
|
|
//return fmt.Errorf("unable to attach user data stream handlers - %s", hcl.get_errmsg())
|
|
|
|
return hcl.make_errinfo()
|
2023-11-01 07:06:28 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-03 09:04:46 +00:00
|
|
|
func (hcl *HCL) BeginFeed() error {
|
|
|
|
var x C.int
|
|
|
|
|
|
|
|
x = C.hcl_beginfeed(hcl.c, nil)
|
|
|
|
if x <= -1 {
|
2024-09-10 03:30:26 +00:00
|
|
|
//return fmt.Errorf("unable to begin feeding - %s", hcl.get_errmsg())
|
|
|
|
return hcl.make_errinfo()
|
2023-11-03 09:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hcl *HCL) EndFeed() error {
|
|
|
|
var x C.int
|
|
|
|
|
|
|
|
x = C.hcl_endfeed(hcl.c)
|
|
|
|
if x <= -1 {
|
2024-09-10 03:30:26 +00:00
|
|
|
//return fmt.Errorf("unable to end feeding - %s", hcl.get_errmsg())
|
|
|
|
return hcl.make_errinfo()
|
2023-11-03 09:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-01 07:06:28 +00:00
|
|
|
func (hcl *HCL) FeedString(str string) error {
|
|
|
|
var x C.int
|
|
|
|
var q []C.hcl_uch_t
|
|
|
|
|
|
|
|
q = string_to_uchars(str)
|
|
|
|
x = C.hcl_feed(hcl.c, &q[0], C.hcl_oow_t(len(q)))
|
|
|
|
if x <= -1 {
|
2024-09-10 03:30:26 +00:00
|
|
|
//return fmt.Errorf("unable to feed string - %s", hcl.get_errmsg())
|
|
|
|
return hcl.make_errinfo()
|
2023-10-31 12:17:08 +00:00
|
|
|
}
|
2023-10-29 12:40:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-01 07:06:28 +00:00
|
|
|
func (hcl *HCL) FeedRunes(str []rune) error {
|
|
|
|
var x C.int
|
|
|
|
var q []C.hcl_uch_t
|
|
|
|
|
|
|
|
q = rune_slice_to_uchars(str)
|
|
|
|
x = C.hcl_feed(hcl.c, &q[0], C.hcl_oow_t(len(q)))
|
|
|
|
if x <= -1 {
|
2024-09-10 03:30:26 +00:00
|
|
|
//return fmt.Errorf("unable to feed runes - %s", hcl.get_errmsg())
|
|
|
|
return hcl.make_errinfo()
|
2023-11-01 07:06:28 +00:00
|
|
|
}
|
2023-10-29 12:40:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-03 09:04:46 +00:00
|
|
|
func (hcl *HCL) FeedFromReader(rdr io.Reader) error {
|
|
|
|
var err error
|
|
|
|
var n int
|
2023-11-01 07:06:28 +00:00
|
|
|
var x C.int
|
2023-11-03 09:04:46 +00:00
|
|
|
var buf [1024]byte
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, err = rdr.Read(buf[:])
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2024-09-10 03:30:26 +00:00
|
|
|
//return fmt.Errorf("unable to read bytes - %s", err.Error())
|
|
|
|
return &Err{File: hcl.io.cci_main, Msg: err.Error()}
|
2023-11-03 09:04:46 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
x = C.hcl_feedbchars(hcl.c, (*C.hcl_bch_t)(unsafe.Pointer(&buf[0])), C.hcl_oow_t(n))
|
2023-11-03 09:04:46 +00:00
|
|
|
if x <= -1 {
|
2024-09-10 03:30:26 +00:00
|
|
|
//return fmt.Errorf("unable to feed bytes - %s", hcl.get_errmsg())
|
|
|
|
return hcl.make_errinfo()
|
2023-11-03 09:04:46 +00:00
|
|
|
}
|
2023-11-01 07:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-10-29 12:40:05 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
func (hcl *HCL) FeedFromFile(file string) error {
|
2023-11-03 09:04:46 +00:00
|
|
|
var f *os.File
|
|
|
|
var err error
|
2023-11-01 07:06:28 +00:00
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
f, err = os.Open(file)
|
2023-11-03 09:04:46 +00:00
|
|
|
if err != nil {
|
2024-09-10 03:30:26 +00:00
|
|
|
//return fmt.Errorf("unable to open %s - %s", file, err.Error())
|
|
|
|
return &Err{File: file, Msg: err.Error()}
|
2023-11-01 07:06:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-03 09:04:46 +00:00
|
|
|
defer f.Close()
|
|
|
|
return hcl.FeedFromReader(bufio.NewReader(f))
|
2023-10-29 12:40:05 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 07:06:28 +00:00
|
|
|
func (hcl *HCL) Execute() error {
|
|
|
|
var x C.hcl_oop_t
|
|
|
|
|
|
|
|
x = C.hcl_execute(hcl.c)
|
|
|
|
if x == nil {
|
2024-09-10 03:30:26 +00:00
|
|
|
//return fmt.Errorf("unable to execute - %s", hcl.get_errmsg())
|
|
|
|
return hcl.make_errinfo()
|
2023-11-01 07:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: wrap C.hcl_oop_t in a go type
|
|
|
|
// and make this function to return 'x' in the wrapper
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hcl *HCL) Decode() error {
|
|
|
|
var x C.int
|
|
|
|
|
2024-01-21 16:38:13 +00:00
|
|
|
x = C.hcl_decode(hcl.c, C.hcl_getcode(hcl.c), 0, C.hcl_getbclen(hcl.c))
|
2023-11-01 07:06:28 +00:00
|
|
|
if x <= -1 {
|
2024-09-10 03:30:26 +00:00
|
|
|
//return fmt.Errorf("unable to decode byte codes - %s", hcl.get_errmsg())
|
|
|
|
return hcl.make_errinfo()
|
2023-11-01 07:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
func (hcl *HCL) get_errmsg() string {
|
2023-11-01 07:06:28 +00:00
|
|
|
return C.GoString(C.hcl_geterrbmsg(hcl.c))
|
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
func (hcl *HCL) set_errmsg(num C.hcl_errnum_t, msg string) {
|
2023-11-01 07:06:28 +00:00
|
|
|
var ptr *C.char
|
|
|
|
ptr = C.CString(msg)
|
|
|
|
defer C.free(unsafe.Pointer(ptr))
|
|
|
|
C.hcl_seterrbmsg(hcl.c, num, ptr)
|
2023-10-29 12:40:05 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 03:30:26 +00:00
|
|
|
// -----------------------------------------------------------
|
|
|
|
|
2023-10-29 12:40:05 +00:00
|
|
|
func ucstr_to_rune_slice(str *C.hcl_uch_t) []rune {
|
|
|
|
return uchars_to_rune_slice(str, uintptr(C.hcl_count_ucstr(str)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func uchars_to_rune_slice(str *C.hcl_uch_t, len uintptr) []rune {
|
|
|
|
var res []rune
|
|
|
|
var i uintptr
|
2023-11-01 07:06:28 +00:00
|
|
|
var ptr uintptr
|
2023-10-29 12:40:05 +00:00
|
|
|
|
|
|
|
// TODO: proper encoding...
|
2023-11-01 07:06:28 +00:00
|
|
|
ptr = uintptr(unsafe.Pointer(str))
|
2023-10-29 12:40:05 +00:00
|
|
|
res = make([]rune, len)
|
|
|
|
for i = 0; i < len; i++ {
|
2023-11-01 07:06:28 +00:00
|
|
|
res[i] = rune(*(*C.hcl_uch_t)(unsafe.Pointer(ptr)))
|
|
|
|
ptr += unsafe.Sizeof(*str)
|
2023-10-29 12:40:05 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
func string_to_uchars(str string) []C.hcl_uch_t {
|
2023-11-01 07:06:28 +00:00
|
|
|
var r []rune
|
|
|
|
var c []C.hcl_uch_t
|
|
|
|
var i int
|
|
|
|
|
|
|
|
// TODO: proper encoding
|
2023-11-05 14:33:22 +00:00
|
|
|
r = []rune(str)
|
2023-11-01 07:06:28 +00:00
|
|
|
c = make([]C.hcl_uch_t, len(r), len(r))
|
|
|
|
for i = 0; i < len(r); i++ {
|
|
|
|
c[i] = C.hcl_uch_t(r[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
func rune_slice_to_uchars(r []rune) []C.hcl_uch_t {
|
2023-11-01 07:06:28 +00:00
|
|
|
var c []C.hcl_uch_t
|
|
|
|
var i int
|
|
|
|
|
|
|
|
// TODO: proper encoding
|
|
|
|
c = make([]C.hcl_uch_t, len(r), len(r))
|
|
|
|
for i = 0; i < len(r); i++ {
|
|
|
|
c[i] = C.hcl_uch_t(r[i])
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2023-10-29 12:40:05 +00:00
|
|
|
func c_to_go(c *C.hcl_t) *HCL {
|
|
|
|
var ext *Ext
|
|
|
|
var inst Instance
|
|
|
|
|
|
|
|
ext = (*Ext)(unsafe.Pointer(C.hcl_getxtn(c)))
|
|
|
|
inst = inst_table.slot_to_instance(ext.inst_no)
|
|
|
|
return inst.g
|
|
|
|
}
|
2024-09-10 03:30:26 +00:00
|
|
|
|
|
|
|
// -----------------------------------------------------------
|
|
|
|
|
|
|
|
func (err* Err) Error() string {
|
|
|
|
if err.Tgt == "" {
|
|
|
|
return fmt.Sprintf("%s[%d,%d] %s", err.File, err.Line, err.Colm, err.Msg)
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s[%d,%d] %s - %s", err.File, err.Line, err.Colm, err.Msg, err.Tgt)
|
|
|
|
}
|