updated hak_open()/hak_openstd()/etc to accept the pointer to hak_errinf_t to convey an error message upon instantiation failure.

added hak_errbint_t and hak_erruinf_t and related functions
enhanced the wchar_t detection for wide_char yes:4w
This commit is contained in:
2025-10-06 00:59:20 +09:00
parent 78f43c8b4f
commit 56dfb3630e
13 changed files with 298 additions and 143 deletions

View File

@ -30,6 +30,7 @@
#include <hak.h>
#include <hak-chr.h>
#include <hak-cmgr.h>
#include <hak-str.h>
#include <hak-utl.h>
#include <hak-opt.h>
@ -563,7 +564,7 @@ static int on_fed_cnode_in_interactive_mode (hak_t* hak, hak_cnode_t* obj)
/*print_error(hak, "compile"); */
xtn->feed.pos = xtn->feed.len; /* arrange to discard the rest of the line */
return -1; /* this causes the feed function to fail and
the error hander for to print the error message */
the error handler to print the error message */
}
xtn->feed.ncompexprs++;
@ -826,6 +827,7 @@ int main (int argc, char* argv[])
hak_t* hak = HAK_NULL;
xtn_t* xtn;
hak_cb_t hakcb;
hak_errinf_t errinf;
hak_bci_t c;
static hak_bopt_lng_t lopt[] =
@ -922,10 +924,21 @@ int main (int argc, char* argv[])
if ((opt.ind + 1) != argc && (opt.ind + 2) != argc && !show_info) goto print_usage;
#endif
hak = hak_openstd(HAK_SIZEOF(xtn_t), HAK_NULL);
hak = hak_openstd(HAK_SIZEOF(xtn_t), &errinf);
if (HAK_UNLIKELY(!hak))
{
printf ("ERROR: cannot open hak\n");
const hak_bch_t* msg;
#if defined(HAK_OOCH_IS_UCH)
hak_bch_t msgbuf[HAK_ERRMSG_CAPA];
hak_oow_t msglen, wcslen;
msglen = HAK_COUNTOF(msgbuf);
hak_conv_ucstr_to_bcstr_with_cmgr(errinf.msg, &wcslen, msgbuf, &msglen, hak_get_cmgr_by_id(HAK_CMGR_UTF8));
msg = msgbuf;
#else
msg = errinf.msg;
#endif
printf("ERROR: cannot open hak - %s\n", msg);
goto oops;
}

View File

@ -2172,7 +2172,7 @@ static int compile_break_p1 (hak_t* hak)
HAK_ASSERT(hak, cf->opcode == COP_COMPILE_BREAK_P1);
HAK_ASSERT(hak, cf->operand != HAK_NULL);
jip = cf->u._break.jump_inst_pos;;
jip = cf->u._break.jump_inst_pos;
/* HAK_CODE_LONG_PARAM_SIZE + 1 => size of the long JUMP_FORWARD instruction */
jump_offset = hak->code.bc.len - jip - (HAK_CODE_LONG_PARAM_SIZE + 1);

View File

@ -274,6 +274,60 @@ const hak_uch_t* hak_geterrumsg (hak_t* hak)
#endif
}
void hak_geterrbinf (hak_t* hak, hak_errbinf_t* errinf)
{
#if defined(HAK_OOCH_IS_BCH)
errinf->num = hak->errnum;
errinf->loc = hak->errloc;
hak_copy_oocstr(errinf->msg, HAK_COUNTOF(errinf->msg), (hak->errmsg[0] == '\0'? hak_errnum_to_errstr(hak->errnum): hak->errmsg));
#else
const hak_ooch_t* msg;
hak_oow_t wcslen, mbslen;
/*errinf->num = hak->errnum;*/
errinf->loc.line = hak->errloc.line;
errinf->loc.colm = hak->errloc.colm;
if (!hak->errloc.file) errinf->loc.file = HAK_NULL;
else
{
mbslen = HAK_COUNTOF(hak->errmsg.xerrlocfile);
hak_conv_ucstr_to_bcstr_with_cmgr(hak->errloc.file, &wcslen, hak->errmsg.xerrlocfile, &mbslen, hak->_cmgr);
errinf->loc.file = hak->errmsg.xerrlocfile; /* this can be truncated and is transient */
}
msg = (hak->errmsg.buf[0] == '\0')? hak_errnum_to_errstr(hak->errnum): hak->errmsg.buf;
mbslen = HAK_COUNTOF(errinf->msg);
hak_conv_ucstr_to_bcstr_with_cmgr(msg, &wcslen, errinf->msg, &mbslen, hak->_cmgr);
#endif
}
void hak_geterruinf (hak_t* hak, hak_erruinf_t* errinf)
{
#if defined(HAK_OOCH_IS_BCH)
const hak_ooch_t* msg;
hak_oow_t wcslen, mbslen;
/*errinf->num = hak->errnum;*/
errinf->loc.line = hak->errloc.line;
errinf->loc.colm = hak->errloc.colm;
if (!hak->errloc.file) errinf->loc.file = HAK_NULL;
else
{
wcslen = HAK_COUNTOF(hak->xerrlocfile);
hak_conv_bcstr_to_ucstr_with_cmgr(hak->errloc.file, &mbslen, hak->xerrlocfile, &wcslen, hak->_cmgr, 1);
errinf->loc.file = hak->xerrlocfile; /* this can be truncated and is transient */
}
msg = (hak->errmsg[0] == '\0')? hak_errnum_to_errstr(hak->errnum): hak->errmsg;
wcslen = HAK_COUNTOF(errinf->msg);
hak_conv_bcstr_to_ucstr_with_cmgr(msg, &mbslen, errinf->msg, &wcslen, hak->_cmgr, 1);
#else
errinf->num = hak->errnum;
errinf->loc = hak->errloc;
hak_copy_oocstr(errinf->msg, HAK_COUNTOF(errinf->msg), (hak->errmsg.buf[0] == '\0'? hak_errnum_to_errstr(hak->errnum): hak->errmsg.buf));
#endif
}
hak_oow_t hak_copyerrbmsg (hak_t* hak, hak_bch_t* buf, hak_oow_t len)
{
return hak_copy_bcstr(buf, len, hak_geterrbmsg(hak));

View File

@ -420,21 +420,23 @@ typedef unsigned char hak_bchu_t; /* unsigned version of hak_bch_t for
#define HAK_SIZEOF_BCI_T HAK_SIZEOF_INT
#if defined(HAK_WIDE_CHAR_SIZE) && (HAK_WIDE_CHAR_SIZE >= 4)
# if defined(__GNUC__) && defined(__CHAR32_TYPE__)
# if defined(__WCHAR_TYPE__) && defined(__SIZEOF_WCHAR_T__) && (__SIZEOF_WCHAR_T__ == HAK_WIDE_CHAR_SIZE)
typedef __WCHAR_TYPE__ hak_uch_t;
# elif defined(__GNUC__) && defined(__CHAR32_TYPE__)
typedef __CHAR32_TYPE__ hak_uch_t;
# else
typedef hak_uint32_t hak_uch_t;
# endif
typedef hak_uint32_t hak_uchu_t; /* same as hak_uch_t as it is already unsigned */
typedef hak_uint32_t hak_uchu_t;
# define HAK_SIZEOF_UCH_T 4
#elif defined(__GNUC__) && defined(__CHAR16_TYPE__)
typedef __CHAR16_TYPE__ hak_uch_t;
typedef hak_uint16_t hak_uchu_t; /* same as hak_uch_t as it is already unsigned */
typedef hak_uint16_t hak_uchu_t;
# define HAK_SIZEOF_UCH_T 2
#else
typedef hak_uint16_t hak_uch_t;
typedef hak_uint16_t hak_uchu_t; /* same as hak_uch_t as it is already unsigned */
typedef hak_uint16_t hak_uchu_t;
# define HAK_SIZEOF_UCH_T 2
#endif

View File

@ -111,7 +111,7 @@ HAK_EXPORT hak_json_t* hak_json_open (
hak_mmgr_t* mmgr,
hak_oow_t xtnsize,
hak_json_prim_t* prim,
hak_errnum_t* errnum
hak_errinf_t* errinf
);
HAK_EXPORT void hak_json_close (

View File

@ -251,7 +251,7 @@ HAK_EXPORT hak_server_t* hak_server_open (
hak_mmgr_t* mmgr,
hak_oow_t xtnsize,
hak_server_prim_t* prim,
hak_errnum_t* errnum
hak_errinf_t* errinf
);
HAK_EXPORT void hak_server_close (
@ -372,7 +372,7 @@ HAK_EXPORT hak_client_t* hak_client_open (
hak_mmgr_t* mmgr,
hak_oow_t xtnsize,
hak_client_prim_t* prim,
hak_errnum_t* errnum
hak_errinf_t* errinf
);
HAK_EXPORT void hak_client_close (

View File

@ -24,7 +24,7 @@
#include "hak-prv.h"
hak_t* hak_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, const hak_vmprim_t* vmprim, hak_errnum_t* errnum)
hak_t* hak_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, const hak_vmprim_t* vmprim, hak_errinf_t* errinf)
{
hak_t* hak;
@ -32,17 +32,22 @@ hak_t* hak_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, const hak_vmprim_t* vmprim
HAK_ASSERT(hak, HAK_SIZEOF(hak_oow_t) == HAK_SIZEOF(hak_oop_t));
hak = (hak_t*)HAK_MMGR_ALLOC(mmgr, HAK_SIZEOF(*hak) + xtnsize);
if (hak)
if (HAK_LIKELY(hak))
{
if (hak_init(hak, mmgr, vmprim) <= -1)
{
if (errnum) *errnum = hak->errnum;
if (errinf) hak_geterrinf(hak, errinf);
HAK_MMGR_FREE(mmgr, hak);
hak = HAK_NULL;
}
else HAK_MEMSET(hak + 1, 0, xtnsize);
}
else if (errnum) *errnum = HAK_ESYSMEM;
else if (errinf)
{
HAK_MEMSET(errinf, 0, HAK_SIZEOF(*errinf));
errinf->num = HAK_ESYSMEM;
hak_copy_oocstr(errinf->msg, HAK_COUNTOF(errinf->msg), hak_errnum_to_errstr(errinf->num));
}
return hak;
}
@ -440,7 +445,6 @@ int hak_setoption (hak_t* hak, hak_option_t id, const void* value)
hak->option.log_maxcapa = *(hak_oow_t*)value;
break;
case HAK_LOG_TARGET_BCSTR:
{
hak_bch_t* v1;

View File

@ -39,6 +39,30 @@ typedef struct hak_mod_t hak_mod_t;
/* ========================================================================== */
struct hak_bloc_t
{
hak_oow_t line; /**< line */
hak_oow_t colm; /**< column */
const hak_bch_t* file; /**< file specified in #include */
};
typedef struct hak_bloc_t hak_bloc_t;
struct hak_uloc_t
{
hak_oow_t line; /**< line */
hak_oow_t colm; /**< column */
const hak_uch_t* file; /**< file specified in #include */
};
typedef struct hak_uloc_t hak_uloc_t;
#if defined(HAK_OOCH_IS_UCH)
typedef hak_uloc_t hak_loc_t;
#else
typedef hak_bloc_t hak_loc_t;
#endif
/* ========================================================================== */
/**
* The hak_errnum_t type defines the error codes.
*/
@ -179,6 +203,40 @@ enum hak_synerrnum_t
};
typedef enum hak_synerrnum_t hak_synerrnum_t;
/* ========================================================================== */
#define HAK_ERRMSG_CAPA (2048)
/**
* The hak_errbinf_t type defines a placeholder for error information.
*/
struct hak_errbinf_t
{
hak_oow_t _instsize;
hak_errnum_t num; /**< error number */
hak_bch_t msg[HAK_ERRMSG_CAPA]; /**< error message */
hak_bloc_t loc; /**< error location */
};
typedef struct hak_errbinf_t hak_errbinf_t;
struct hak_erruinf_t
{
hak_oow_t _instsize;
hak_errnum_t num; /**< error number */
hak_uch_t msg[HAK_ERRMSG_CAPA]; /**< error message */
hak_uloc_t loc; /**< error location */
};
typedef struct hak_erruinf_t hak_erruinf_t;
#if defined(HAK_OOCH_IS_UCH)
typedef hak_erruinf_t hak_errinf_t;
#else
typedef hak_errbinf_t hak_errinf_t;
#endif
/* ========================================================================== */
enum hak_option_t
{
HAK_TRAIT,
@ -1255,14 +1313,6 @@ enum hak_io_cmd_t
};
typedef enum hak_io_cmd_t hak_io_cmd_t;
struct hak_loc_t
{
hak_oow_t line; /**< line */
hak_oow_t colm; /**< column */
const hak_ooch_t* file; /**< file specified in #include */
};
typedef struct hak_loc_t hak_loc_t;
struct hak_lxc_t
{
hak_ooci_t c; /**< character */
@ -1649,14 +1699,15 @@ struct hak_t
hak_bch_t bch[HAK_ERRMSG_CAPA];
hak_uch_t uch[HAK_ERRMSG_CAPA];
} tmpbuf;
#if defined(HAK_OOCH_IS_BCH)
hak_uch_t xerrmsg[HAK_ERRMSG_CAPA];
#else
#if defined(HAK_OOCH_IS_UCH)
hak_bch_t xerrmsg[HAK_ERRMSG_CAPA * 2];
hak_bch_t xerrlocfile[256 * 2];
#else
hak_uch_t xerrmsg[HAK_ERRMSG_CAPA];
hak_uch_t xerrlocfile[256];
#endif
hak_ooch_t buf[HAK_ERRMSG_CAPA];
hak_oow_t len;
} errmsg;
hak_loc_t errloc;
int shuterr;
@ -2130,7 +2181,7 @@ HAK_EXPORT hak_t* hak_open (
hak_mmgr_t* mmgr,
hak_oow_t xtnsize,
const hak_vmprim_t* vmprim,
hak_errnum_t* errnum
hak_errinf_t* errinf
);
/**/
@ -2140,13 +2191,13 @@ HAK_EXPORT hak_t* hak_open (
HAK_EXPORT hak_t* hak_openstdwithmmgr (
hak_mmgr_t* mmgr,
hak_oow_t xtnsize,
hak_errnum_t* errnum
hak_errinf_t* errinf
);
/**/
HAK_EXPORT hak_t* hak_openstd (
hak_oow_t xtnsize,
hak_errnum_t* errnum
hak_errinf_t* errinf
);
/**
@ -2281,6 +2332,21 @@ HAK_EXPORT const hak_bch_t* hak_geterrbmsg (
hak_t* hak
);
HAK_EXPORT void hak_geterrbinf (
hak_t* hak,
hak_errbinf_t* errinf
);
HAK_EXPORT void hak_geterruinf (
hak_t* hak,
hak_erruinf_t* errinf
);
#if defined(HAK_OOCH_IS_UCH)
# define hak_geterrinf hak_geterruinf
#else
# define hak_geterrinf hak_geterrbinf
#endif
HAK_EXPORT hak_oow_t hak_copyerrbmsg (
hak_t* hak,
hak_bch_t* buf,

View File

@ -911,7 +911,7 @@ oops:
/* ========================================================================= */
hak_json_t* hak_json_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_json_prim_t* prim, hak_errnum_t* errnum)
hak_json_t* hak_json_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_json_prim_t* prim, hak_errinf_t* errinf)
{
hak_json_t* json;
hak_t* hak;
@ -920,11 +920,16 @@ hak_json_t* hak_json_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_json_prim_t*
json = (hak_json_t*)HAK_MMGR_ALLOC(mmgr, HAK_SIZEOF(*json) + xtnsize);
if (!json)
{
if (errnum) *errnum = HAK_ESYSMEM;
if (errinf)
{
HAK_MEMSET(errinf, 0, HAK_SIZEOF(*errinf));
errinf->num = HAK_ESYSMEM;
hak_copy_oocstr(errinf->msg, HAK_COUNTOF(errinf->msg), hak_errnum_to_errstr(errinf->num));
}
return HAK_NULL;
}
hak = hak_openstdwithmmgr(mmgr, HAK_SIZEOF(*xtn), errnum);
hak = hak_openstdwithmmgr(mmgr, HAK_SIZEOF(*xtn), errinf);
if (!hak)
{
HAK_MMGR_FREE(mmgr, json);

View File

@ -4089,7 +4089,7 @@ static LONG WINAPI msw_exception_filter (struct _EXCEPTION_POINTERS* exinfo)
}
#endif
hak_t* hak_openstdwithmmgr (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_errnum_t* errnum)
hak_t* hak_openstdwithmmgr (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_errinf_t* errinf)
{
hak_t* hak;
hak_vmprim_t vmprim;
@ -4116,7 +4116,7 @@ hak_t* hak_openstdwithmmgr (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_errnum_t* e
vmprim.vm_getsig = vm_getsig;
vmprim.vm_setsig = vm_setsig;
hak = hak_open(mmgr, HAK_SIZEOF(xtn_t) + xtnsize, &vmprim, errnum);
hak = hak_open(mmgr, HAK_SIZEOF(xtn_t) + xtnsize, &vmprim, errinf);
if (HAK_UNLIKELY(!hak)) return HAK_NULL;
/* adjust the object size by the sizeof xtn_t so that hak_getxtn() returns the right pointer. */
@ -4133,7 +4133,7 @@ hak_t* hak_openstdwithmmgr (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_errnum_t* e
cb.vm_cleanup = cb_vm_cleanup;
if (hak_regcb(hak, &cb) == HAK_NULL)
{
if (errnum) *errnum = HAK_ERRNUM(hak);
if (errinf) hak_geterrinf(hak, errinf);
hak_close(hak);
return HAK_NULL;
}
@ -4145,9 +4145,9 @@ hak_t* hak_openstdwithmmgr (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_errnum_t* e
return hak;
}
hak_t* hak_openstd (hak_oow_t xtnsize, hak_errnum_t* errnum)
hak_t* hak_openstd (hak_oow_t xtnsize, hak_errinf_t* errinf)
{
return hak_openstdwithmmgr(&sys_mmgr, xtnsize, errnum);
return hak_openstdwithmmgr(&sys_mmgr, xtnsize, errinf);
}
/* --------------------------------------------------------------------- */

View File

@ -156,7 +156,7 @@ static void client_log_write_for_dummy (hak_t* hak, hak_bitmask_t mask, const ha
client->prim.log_write (client, mask, msg, len);
}
hak_client_t* hak_client_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_client_prim_t* prim, hak_errnum_t* errnum)
hak_client_t* hak_client_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_client_prim_t* prim, hak_errinf_t* errinf)
{
hak_client_t* client = HAK_NULL;
hak_t* hak = HAK_NULL;
@ -166,20 +166,25 @@ hak_client_t* hak_client_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_client_p
client = (hak_client_t*)HAK_MMGR_ALLOC(mmgr, HAK_SIZEOF(*client) + xtnsize);
if (HAK_UNLIKELY(!client))
{
if (errnum) *errnum = HAK_ESYSMEM;
return HAK_NULL;
if (errinf)
{
HAK_MEMSET(errinf, 0, HAK_SIZEOF(*errinf));
errinf->num = HAK_ESYSMEM;
hak_copy_oocstr(errinf->msg, HAK_COUNTOF(errinf->msg), hak_errnum_to_errstr(errinf->num));
}
goto oops;
}
hak = hak_openstdwithmmgr(mmgr, HAK_SIZEOF(*xtn), errnum);
if (HAK_UNLIKELY(!hak))
{
HAK_MMGR_FREE (mmgr, client);
return HAK_NULL;
}
hak = hak_openstdwithmmgr(mmgr, HAK_SIZEOF(*xtn), errinf);
if (HAK_UNLIKELY(!hak)) goto oops;
if (hak_sys_open_pipes(pfd, 1) <= -1)
{
if (errnum) *errnum = hak->vmprim.syserrstrb(hak, 0, errno, HAK_NULL, 0);
if (errinf)
{
hak_seterrbfmtwithsyserr(hak, 0, errno, HAK_NULL, 0);
hak_geterrinf(hak, errinf);
}
goto oops;
}
@ -673,7 +678,7 @@ hak_client_logbfmt(client, HAK_LOG_STDERR, "send error - %hs\n", strerror(errno)
hak_uint8_t* bptr;
ssize_t x;
bptr = hak_xproto_getbuf(client->remote.proto, &bcap);;
bptr = hak_xproto_getbuf(client->remote.proto, &bcap);
x = recv(client->remote.sck, bptr, bcap, 0);
if (x <= -1)
{

View File

@ -1105,7 +1105,7 @@ static int send_chars (hak_xproto_t* proto, hak_xpkt_type_t xpkt_type, const hak
/* ========================================================================= */
hak_server_t* hak_server_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_server_prim_t* prim, hak_errnum_t* errnum)
hak_server_t* hak_server_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_server_prim_t* prim, hak_errinf_t* errinf)
{
hak_server_t* server = HAK_NULL;
hak_t* hak = HAK_NULL;
@ -1117,26 +1117,32 @@ hak_server_t* hak_server_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_server_p
server = (hak_server_t*)HAK_MMGR_ALLOC(mmgr, HAK_SIZEOF(*server) + xtnsize);
if (!server)
{
if (errnum) *errnum = HAK_ESYSMEM;
return HAK_NULL;
esysmem:
if (errinf)
{
HAK_MEMSET(errinf, 0, HAK_SIZEOF(*errinf));
errinf->num = HAK_ESYSMEM;
hak_copy_oocstr(errinf->msg, HAK_COUNTOF(errinf->msg), hak_errnum_to_errstr(errinf->num));
}
goto oops;
}
hak = hak_openstdwithmmgr(mmgr, HAK_SIZEOF(*xtn), errnum);
hak = hak_openstdwithmmgr(mmgr, HAK_SIZEOF(*xtn), errinf);
if (!hak) goto oops;
/* replace the vmprim.log_write function */
hak->vmprim.log_write = server_log_write_for_dummy;
tmr = hak_tmr_open(hak, 0, 1024); /* TOOD: make the timer's default size configurable */
if (!tmr)
{
if (errnum) *errnum = HAK_ESYSMEM;
goto oops;
}
if (!tmr) goto esysmem;
if (hak_sys_open_pipes(pfd, 1) <= -1)
{
if (errnum) *errnum = hak->vmprim.syserrstrb(hak, 0, errno, HAK_NULL, 0);
if (errinf)
{
hak_seterrbfmtwithsyserr(hak, 0, errno, HAK_NULL, 0);
hak_geterrinf(hak, errinf);
}
goto oops;
}
@ -1463,7 +1469,7 @@ static int worker_step (hak_server_worker_t* worker)
hak_oow_t bcap;
hak_uint8_t* bptr;
bptr = hak_xproto_getbuf(proto, &bcap);;
bptr = hak_xproto_getbuf(proto, &bcap);
x = recv(worker->sck, bptr, bcap, 0);
if (x <= -1)
{