From fdbe17729eed05bfab17e25fd16a9ab08c715735 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 14 May 2024 20:52:25 +0900 Subject: [PATCH] enhanced formatting functions to accept hcl as the first parameter in order to extend hcl_fmttobcstr() adn hcl_fmttoucstr() to handle %O and %J --- bin/hcl.c | 5 + bin/hclx.c | 7 +- lib/err.c | 36 ++--- lib/fmt.c | 367 +++++++++++++++++++++++-------------------------- lib/hcl-fmt.h | 9 +- lib/hcl-prv.h | 3 +- lib/hcl-x.h | 2 +- lib/hcl.h | 26 +++- lib/prim.c | 8 +- lib/print.c | 85 ++++++------ lib/x-client.c | 17 ++- lib/x-server.c | 88 +++++++++--- 12 files changed, 361 insertions(+), 292 deletions(-) diff --git a/bin/hcl.c b/bin/hcl.c index ec6a824..796774c 100644 --- a/bin/hcl.c +++ b/bin/hcl.c @@ -834,8 +834,13 @@ int main (int argc, char* argv[]) /*trait |= HCL_TRAIT_NOGC;*/ trait |= HCL_TRAIT_AWAIT_PROCS; + #if 0 if (enable_block) trait |= HCL_TRAIT_LANG_ENABLE_BLOCK; if (nl_terminator) trait |= HCL_TRAIT_LANG_ENABLE_EOL; + #else + trait |= HCL_TRAIT_LANG_ENABLE_BLOCK; + trait |= HCL_TRAIT_LANG_ENABLE_EOL; + #endif hcl_setoption (hcl, HCL_TRAIT, &trait); } diff --git a/bin/hclx.c b/bin/hclx.c index ab2a355..6c11f69 100644 --- a/bin/hclx.c +++ b/bin/hclx.c @@ -706,7 +706,7 @@ oops: } /* -------------------------------------------------------------- */ -static int client_on_packet (hcl_xproto_t* proto, hcl_xpkt_type_t type, const void* data, hcl_oow_t len) +static int client_on_packet (hcl_client_t* client, hcl_xpkt_type_t type, const void* data, hcl_oow_t len) { if (type == HCL_XPKT_STDOUT) { @@ -721,6 +721,11 @@ static int client_on_packet (hcl_xproto_t* proto, hcl_xpkt_type_t type, const vo /* error notification */ if (len > 0) fprintf (stderr, "ERROR: %.*s\n", (int)len, data); } + else if (type == HCL_XPKT_RETVAL) + { + if (len > 0) fprintf (stderr, "RETURN VALUE: %.*s\n", (int)len, data); + hcl_client_stop (client); + } return 1; } diff --git a/lib/err.c b/lib/err.c index 1a25fd5..f1b22a1 100644 --- a/lib/err.c +++ b/lib/err.c @@ -270,6 +270,16 @@ const hcl_uch_t* hcl_geterrumsg (hcl_t* hcl) #endif } +hcl_oow_t hcl_copyerrbmsg (hcl_t* hcl, hcl_bch_t* buf, hcl_oow_t len) +{ + return hcl_copy_bcstr(buf, len, hcl_geterrbmsg(hcl)); +} + +hcl_oow_t hcl_copyerrumsg (hcl_t* hcl, hcl_uch_t* buf, hcl_oow_t len) +{ + return hcl_copy_ucstr(buf, len, hcl_geterrumsg(hcl)); +} + const hcl_ooch_t* hcl_backuperrmsg (hcl_t* hcl) { hcl_copy_oocstr (hcl->errmsg.tmpbuf.ooch, HCL_COUNTOF(hcl->errmsg.tmpbuf.ooch), hcl_geterrmsg(hcl)); @@ -305,9 +315,8 @@ void hcl_seterrumsg (hcl_t* hcl, hcl_errnum_t errnum, const hcl_uch_t* errmsg) } -static int err_bcs (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) +static int err_bcs (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) { - hcl_t* hcl = (hcl_t*)fmtout->ctx; hcl_oow_t max; max = HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len - 1; @@ -328,9 +337,8 @@ static int err_bcs (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) return 1; /* success */ } -static int err_ucs (hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) +static int err_ucs (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) { - hcl_t* hcl = (hcl_t*)fmtout->ctx; hcl_oow_t max; max = HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len - 1; @@ -360,11 +368,10 @@ void hcl_seterrbfmt (hcl_t* hcl, hcl_errnum_t errnum, const hcl_bch_t* fmt, ...) HCL_MEMSET (&fo, 0, HCL_SIZEOF(fo)); fo.putbchars = err_bcs; fo.putuchars = err_ucs; - fo.putobj = hcl_fmt_object_; - fo.ctx = hcl; + fo.putobj = hcl_fmt_object; va_start (ap, fmt); - hcl_bfmt_outv (&fo, fmt, ap); + hcl_bfmt_outv (hcl, &fo, fmt, ap); va_end (ap); hcl->errnum = errnum; @@ -382,11 +389,10 @@ void hcl_seterrufmt (hcl_t* hcl, hcl_errnum_t errnum, const hcl_uch_t* fmt, ...) HCL_MEMSET (&fo, 0, HCL_SIZEOF(fo)); fo.putbchars = err_bcs; fo.putuchars = err_ucs; - fo.putobj = hcl_fmt_object_; - fo.ctx = hcl; + fo.putobj = hcl_fmt_object; va_start (ap, fmt); - hcl_ufmt_outv (&fo, fmt, ap); + hcl_ufmt_outv (hcl, &fo, fmt, ap); va_end (ap); hcl->errnum = errnum; @@ -405,10 +411,9 @@ void hcl_seterrbfmtv (hcl_t* hcl, hcl_errnum_t errnum, const hcl_bch_t* fmt, va_ HCL_MEMSET (&fo, 0, HCL_SIZEOF(fo)); fo.putbchars = err_bcs; fo.putuchars = err_ucs; - fo.putobj = hcl_fmt_object_; - fo.ctx = hcl; + fo.putobj = hcl_fmt_object; - hcl_bfmt_outv (&fo, fmt, ap); + hcl_bfmt_outv (hcl, &fo, fmt, ap); hcl->errnum = errnum; HCL_MEMSET (&hcl->errloc, 0, HCL_SIZEOF(hcl->errloc)); } @@ -424,10 +429,9 @@ void hcl_seterrufmtv (hcl_t* hcl, hcl_errnum_t errnum, const hcl_uch_t* fmt, va_ HCL_MEMSET (&fo, 0, HCL_SIZEOF(fo)); fo.putbchars = err_bcs; fo.putuchars = err_ucs; - fo.putobj = hcl_fmt_object_; - fo.ctx = hcl; + fo.putobj = hcl_fmt_object; - hcl_ufmt_outv (&fo, fmt, ap); + hcl_ufmt_outv (hcl, &fo, fmt, ap); hcl->errnum = errnum; HCL_MEMSET (&hcl->errloc, 0, HCL_SIZEOF(hcl->errloc)); } diff --git a/lib/fmt.c b/lib/fmt.c index 459b61f..cbd7aea 100644 --- a/lib/fmt.c +++ b/lib/fmt.c @@ -339,47 +339,47 @@ static hcl_bch_t* sprintn_upper (hcl_bch_t* nbuf, hcl_uintmax_t num, int base, h /* ------------------------------------------------------------------------- */ -#define PUT_BCH(fmtout,c,n) do { \ +#define PUT_BCH(hcl,fmtout,c,n) do { \ if (n > 0) { \ hcl_oow_t _yy; \ hcl_bch_t _cc = c; \ for (_yy = 0; _yy < n; _yy++) \ { \ int _xx; \ - if ((_xx = fmtout->putbchars(fmtout, &_cc, 1)) <= -1) goto oops; \ + if ((_xx = fmtout->putbchars(hcl, fmtout, &_cc, 1)) <= -1) goto oops; \ if (_xx == 0) goto done; \ fmtout->count++; \ } \ } \ } while (0) -#define PUT_BCS(fmtout,ptr,len) do { \ +#define PUT_BCS(hcl,fmtout,ptr,len) do { \ if (len > 0) { \ int _xx; \ - if ((_xx = fmtout->putbchars(fmtout, ptr, len)) <= -1) goto oops; \ + if ((_xx = fmtout->putbchars(hcl, fmtout, ptr, len)) <= -1) goto oops; \ if (_xx == 0) goto done; \ fmtout->count += len; \ } \ } while (0) -#define PUT_UCH(fmtout,c,n) do { \ +#define PUT_UCH(hcl,fmtout,c,n) do { \ if (n > 0) { \ hcl_oow_t _yy; \ hcl_uch_t _cc = c; \ for (_yy = 0; _yy < n; _yy++) \ { \ int _xx; \ - if ((_xx = fmtout->putuchars(fmtout, &_cc, 1)) <= -1) goto oops; \ + if ((_xx = fmtout->putuchars(hcl, fmtout, &_cc, 1)) <= -1) goto oops; \ if (_xx == 0) goto done; \ fmtout->count++; \ } \ } \ } while (0) -#define PUT_UCS(fmtout,ptr,len) do { \ +#define PUT_UCS(hcl,fmtout,ptr,len) do { \ if (len > 0) { \ int _xx; \ - if ((_xx = fmtout->putuchars(fmtout, ptr, len)) <= -1) goto oops; \ + if ((_xx = fmtout->putuchars(hcl, fmtout, ptr, len)) <= -1) goto oops; \ if (_xx == 0) goto done; \ fmtout->count += len; \ } \ @@ -387,25 +387,25 @@ static hcl_bch_t* sprintn_upper (hcl_bch_t* nbuf, hcl_uintmax_t num, int base, h #if defined(HCL_OOCH_IS_BCH) -# define PUT_OOCH(fmtout,c,n) PUT_BCH(fmtout,c,n) -# define PUT_OOCS(fmtout,ptr,len) PUT_BCS(fmtout,ptr,len) +# define PUT_OOCH(hcl,fmtout,c,n) PUT_BCH(hcl,fmtout,c,n) +# define PUT_OOCS(hcl,fmtout,ptr,len) PUT_BCS(hcl,fmtout,ptr,len) #else -# define PUT_OOCH(fmtout,c,n) PUT_UCH(fmtout,c,n) -# define PUT_OOCS(fmtout,ptr,len) PUT_UCS(fmtout,ptr,len) +# define PUT_OOCH(hcl,fmtout,c,n) PUT_UCH(hcl,fmtout,c,n) +# define PUT_OOCS(hcl,fmtout,ptr,len) PUT_UCS(hcl,fmtout,ptr,len) #endif #define BYTE_PRINTABLE(x) ((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z') || (x >= '0' && x <= '9') || (x == ' ')) -#define PUT_BYTE_IN_HEX(fmtout,byte,extra_flags) do { \ +#define PUT_BYTE_IN_HEX(hcl,fmtout,byte,extra_flags) do { \ hcl_bch_t __xbuf[3]; \ hcl_byte_to_bcstr ((byte), __xbuf, HCL_COUNTOF(__xbuf), (16 | (extra_flags)), '0'); \ - PUT_BCH(fmtout, __xbuf[0], 1); \ - PUT_BCH(fmtout, __xbuf[1], 1); \ + PUT_BCH(hcl, fmtout, __xbuf[0], 1); \ + PUT_BCH(hcl, fmtout, __xbuf[1], 1); \ } while (0) /* ------------------------------------------------------------------------- */ -static int fmt_outv (hcl_fmtout_t* fmtout, va_list ap) +static int fmt_outv (hcl_t* hcl, hcl_fmtout_t* fmtout, va_list ap) { const hcl_uint8_t* fmtptr, * percent; int fmtchsz; @@ -486,11 +486,11 @@ static int fmt_outv (hcl_fmtout_t* fmtout, va_list ap) { if (bch == '\0') { - PUT_BCS (fmtout, start, end - start - 1); + PUT_BCS (hcl, fmtout, start, end - start - 1); goto done; } } - PUT_BCS (fmtout, start, end - start - 1); + PUT_BCS (hcl, fmtout, start, end - start - 1); fmtptr = (const hcl_uint8_t*)end; percent = (const hcl_uint8_t*)(end - 1); } @@ -504,11 +504,11 @@ static int fmt_outv (hcl_fmtout_t* fmtout, va_list ap) { if (uch == '\0') { - PUT_UCS (fmtout, start, end - start - 1); + PUT_UCS (hcl, fmtout, start, end - start - 1); goto done; } } - PUT_UCS (fmtout, start, end - start - 1); + PUT_UCS (hcl, fmtout, start, end - start - 1); fmtptr = (const hcl_uint8_t*)end; percent = (const hcl_uint8_t*)(end - 1); } @@ -781,9 +781,9 @@ static int fmt_outv (hcl_fmtout_t* fmtout, va_list ap) print_lowercase_c: /* precision 0 doesn't kill the letter */ width--; - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_BCH (fmtout, padc, width); - PUT_BCH (fmtout, bch, 1); - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_BCH (fmtout, padc, width); + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_BCH (hcl, fmtout, padc, width); + PUT_BCH (hcl, fmtout, bch, 1); + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_BCH (hcl, fmtout, padc, width); break; } @@ -800,9 +800,9 @@ static int fmt_outv (hcl_fmtout_t* fmtout, va_list ap) /* precision 0 doesn't kill the letter */ width--; - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_UCH (fmtout, padc, width); - PUT_UCH (fmtout, uch, 1); - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_UCH (fmtout, padc, width); + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_UCH (hcl, fmtout, padc, width); + PUT_UCH (hcl, fmtout, uch, 1); + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_UCH (hcl, fmtout, padc, width); break; } @@ -832,9 +832,9 @@ static int fmt_outv (hcl_fmtout_t* fmtout, va_list ap) width -= n; - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_BCH (fmtout, padc, width); - PUT_BCS (fmtout, bsp, n); - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_BCH (fmtout, padc, width); + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_BCH (hcl, fmtout, padc, width); + PUT_BCS (hcl, fmtout, bsp, n); + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_BCH (hcl, fmtout, padc, width); break; } @@ -864,9 +864,9 @@ static int fmt_outv (hcl_fmtout_t* fmtout, va_list ap) width -= n; - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_UCH (fmtout, padc, width); - PUT_UCS (fmtout, usp, n); - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_UCH (fmtout, padc, width); + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_UCH (hcl, fmtout, padc, width); + PUT_UCS (hcl, fmtout, usp, n); + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_UCH (hcl, fmtout, padc, width); break; } @@ -926,25 +926,25 @@ static int fmt_outv (hcl_fmtout_t* fmtout, va_list ap) width -= (n * k_hex_width); } - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); while (n--) { if ((lm_flag & LF_H) && BYTE_PRINTABLE(*bsp)) { - PUT_BCH (fmtout, *bsp, 1); + PUT_BCH (hcl, fmtout, *bsp, 1); } else { hcl_bch_t xbuf[3]; hcl_byte_to_bcstr (*bsp, xbuf, HCL_COUNTOF(xbuf), (16 | (uch == 'k'? HCL_BYTE_TO_BCSTR_LOWERCASE: 0)), '0'); - if (lm_flag & (LF_H | LF_L)) PUT_BCS (fmtout, "\\x", 2); - PUT_BCS (fmtout, xbuf, 2); + if (lm_flag & (LF_H | LF_L)) PUT_BCS (hcl, fmtout, "\\x", 2); + PUT_BCS (hcl, fmtout, xbuf, 2); } bsp++; } - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); break; } @@ -985,53 +985,53 @@ static int fmt_outv (hcl_fmtout_t* fmtout, va_list ap) } } - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); while (n--) { if ((lm_flag & LF_H) && BYTE_PRINTABLE(*usp)) { - PUT_OOCH(fmtout, *usp, 1); + PUT_OOCH (hcl, fmtout, *usp, 1); } else if (!(lm_flag & LF_L) && *usp <= 0xFFFF) { hcl_uint16_t u16 = *usp; int extra_flags = ((uch) == 'w'? HCL_BYTE_TO_BCSTR_LOWERCASE: 0); - PUT_BCS(fmtout, "\\u", 2); - PUT_BYTE_IN_HEX(fmtout, (u16 >> 8) & 0xFF, extra_flags); - PUT_BYTE_IN_HEX(fmtout, u16 & 0xFF, extra_flags); + PUT_BCS (hcl, fmtout, "\\u", 2); + PUT_BYTE_IN_HEX (hcl, fmtout, (u16 >> 8) & 0xFF, extra_flags); + PUT_BYTE_IN_HEX (hcl, fmtout, u16 & 0xFF, extra_flags); } else { hcl_uint32_t u32 = *usp; int extra_flags = ((uch) == 'w'? HCL_BYTE_TO_BCSTR_LOWERCASE: 0); - PUT_BCS(fmtout, "\\u", 2); - PUT_BYTE_IN_HEX(fmtout, (u32 >> 24) & 0xFF, extra_flags); - PUT_BYTE_IN_HEX(fmtout, (u32 >> 16) & 0xFF, extra_flags); - PUT_BYTE_IN_HEX(fmtout, (u32 >> 8) & 0xFF, extra_flags); - PUT_BYTE_IN_HEX(fmtout, u32 & 0xFF, extra_flags); + PUT_BCS (hcl, fmtout, "\\u", 2); + PUT_BYTE_IN_HEX (hcl, fmtout, (u32 >> 24) & 0xFF, extra_flags); + PUT_BYTE_IN_HEX (hcl, fmtout, (u32 >> 16) & 0xFF, extra_flags); + PUT_BYTE_IN_HEX (hcl, fmtout, (u32 >> 8) & 0xFF, extra_flags); + PUT_BYTE_IN_HEX (hcl, fmtout, u32 & 0xFF, extra_flags); } usp++; } - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); break; } case 'O': /* object - ignore precision, width, adjustment */ { - if (!fmtout->putobj) goto invalid_format; - if (fmtout->putobj(fmtout, va_arg(ap, hcl_oop_t)) <= -1) goto oops; + if (HCL_UNLIKELY(!fmtout->putobj)) goto invalid_format; + if (fmtout->putobj(hcl, fmtout, va_arg(ap, hcl_oop_t)) <= -1) goto oops; break; } case 'J': { hcl_bitmask_t tmp; - if (!fmtout->putobj) goto invalid_format; + if (HCL_UNLIKELY(!fmtout->putobj)) goto invalid_format; tmp = fmtout->mask; fmtout->mask |= HCL_LOG_PREFER_JSON; - if (fmtout->putobj(fmtout, va_arg(ap, hcl_oop_t)) <= -1) goto oops; + if (fmtout->putobj(hcl, fmtout, va_arg(ap, hcl_oop_t)) <= -1) goto oops; fmtout->mask = tmp; break; } @@ -1233,7 +1233,7 @@ static int fmt_outv (hcl_fmtout_t* fmtout, va_list ap) bsp = fb.out.ptr; n = 0; while (bsp[n] != '\0') n++; - PUT_BCS (fmtout, bsp, n); + PUT_BCS (hcl, fmtout, bsp, n); break; } #endif @@ -1353,49 +1353,49 @@ static int fmt_outv (hcl_fmtout_t* fmtout, va_list ap) if (!(flagc & FLAGC_LEFTADJ) && !(flagc & FLAGC_ZEROPAD) && width > 0 && (width -= tmp) > 0) { - PUT_OOCH (fmtout, padc, width); + PUT_OOCH (hcl, fmtout, padc, width); width = 0; } - if (neg) PUT_OOCH (fmtout, '-', 1); - else if (flagc & FLAGC_SIGN) PUT_OOCH (fmtout, '+', 1); - else if (flagc & FLAGC_SPACE) PUT_OOCH (fmtout, ' ', 1); + if (neg) PUT_OOCH (hcl, fmtout, '-', 1); + else if (flagc & FLAGC_SIGN) PUT_OOCH (hcl, fmtout, '+', 1); + else if (flagc & FLAGC_SPACE) PUT_OOCH (hcl, fmtout, ' ', 1); if ((flagc & FLAGC_SHARP) && num != 0) { if (base == 2) { - PUT_OOCH (fmtout, '#', 1); - PUT_OOCH (fmtout, 'b', 1); + PUT_OOCH (hcl, fmtout, '#', 1); + PUT_OOCH (hcl, fmtout, 'b', 1); } if (base == 8) { - PUT_OOCH (fmtout, '#', 1); - PUT_OOCH (fmtout, 'o', 1); + PUT_OOCH (hcl, fmtout, '#', 1); + PUT_OOCH (hcl, fmtout, 'o', 1); } else if (base == 16) { - PUT_OOCH (fmtout, '#', 1); - PUT_OOCH (fmtout, 'x', 1); + PUT_OOCH (hcl, fmtout, '#', 1); + PUT_OOCH (hcl, fmtout, 'x', 1); } } if ((flagc & FLAGC_DOT) && precision > numlen) { /* extra zeros for precision specified */ - PUT_OOCH (fmtout, '0', precision - numlen); + PUT_OOCH (hcl, fmtout, '0', precision - numlen); } if (!(flagc & FLAGC_LEFTADJ) && width > 0 && (width -= tmp) > 0) { - PUT_OOCH (fmtout, padc, width); + PUT_OOCH (hcl, fmtout, padc, width); } - while (*nbufp) PUT_OOCH (fmtout, *nbufp--, 1); /* output actual digits */ + while (*nbufp) PUT_OOCH (hcl, fmtout, *nbufp--, 1); /* output actual digits */ if ((flagc & FLAGC_LEFTADJ) && width > 0 && (width -= tmp) > 0) { - PUT_OOCH (fmtout, padc, width); + PUT_OOCH (hcl, fmtout, padc, width); } break; @@ -1403,10 +1403,10 @@ static int fmt_outv (hcl_fmtout_t* fmtout, va_list ap) switch (fmtout->fmt_type) { case HCL_FMTOUT_FMT_TYPE_BCH: - PUT_BCS (fmtout, (const hcl_bch_t*)percent, (fmtptr - percent) / fmtchsz); + PUT_BCS (hcl, fmtout, (const hcl_bch_t*)percent, (fmtptr - percent) / fmtchsz); break; case HCL_FMTOUT_FMT_TYPE_UCH: - PUT_UCS (fmtout, (const hcl_uch_t*)percent, (fmtptr - percent) / fmtchsz); + PUT_UCS (hcl, fmtout, (const hcl_uch_t*)percent, (fmtptr - percent) / fmtchsz); break; } break; @@ -1415,10 +1415,10 @@ static int fmt_outv (hcl_fmtout_t* fmtout, va_list ap) switch (fmtout->fmt_type) { case HCL_FMTOUT_FMT_TYPE_BCH: - PUT_BCS (fmtout, (const hcl_bch_t*)percent, (fmtptr - percent) / fmtchsz); + PUT_BCS (hcl, fmtout, (const hcl_bch_t*)percent, (fmtptr - percent) / fmtchsz); break; case HCL_FMTOUT_FMT_TYPE_UCH: - PUT_UCS (fmtout, (const hcl_uch_t*)percent, (fmtptr - percent) / fmtchsz); + PUT_UCS (hcl, fmtout, (const hcl_uch_t*)percent, (fmtptr - percent) / fmtchsz); break; } /* @@ -1447,7 +1447,7 @@ oops: return -1; } -int hcl_bfmt_outv (hcl_fmtout_t* fmtout, const hcl_bch_t* fmt, va_list ap) +int hcl_bfmt_outv (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_bch_t* fmt, va_list ap) { int n; const void* fmt_str; @@ -1459,14 +1459,14 @@ int hcl_bfmt_outv (hcl_fmtout_t* fmtout, const hcl_bch_t* fmt, va_list ap) fmtout->fmt_type = HCL_FMTOUT_FMT_TYPE_BCH; fmtout->fmt_str = fmt; - n = fmt_outv(fmtout, ap); + n = fmt_outv(hcl, fmtout, ap); fmtout->fmt_str = fmt_str; fmtout->fmt_type = fmt_type; return n; } -int hcl_ufmt_outv (hcl_fmtout_t* fmtout, const hcl_uch_t* fmt, va_list ap) +int hcl_ufmt_outv (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_uch_t* fmt, va_list ap) { int n; const void* fmt_str; @@ -1478,14 +1478,14 @@ int hcl_ufmt_outv (hcl_fmtout_t* fmtout, const hcl_uch_t* fmt, va_list ap) fmtout->fmt_type = HCL_FMTOUT_FMT_TYPE_UCH; fmtout->fmt_str = fmt; - n = fmt_outv(fmtout, ap); + n = fmt_outv(hcl, fmtout, ap); fmtout->fmt_str = fmt_str; fmtout->fmt_type = fmt_type; return n; } -int hcl_bfmt_out (hcl_fmtout_t* fmtout, const hcl_bch_t* fmt, ...) +int hcl_bfmt_out (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_bch_t* fmt, ...) { va_list ap; int n; @@ -1499,7 +1499,7 @@ int hcl_bfmt_out (hcl_fmtout_t* fmtout, const hcl_bch_t* fmt, ...) fmtout->fmt_str = fmt; va_start (ap, fmt); - n = fmt_outv(fmtout, ap); + n = fmt_outv(hcl, fmtout, ap); va_end (ap); fmtout->fmt_str = fmt_str; @@ -1507,7 +1507,7 @@ int hcl_bfmt_out (hcl_fmtout_t* fmtout, const hcl_bch_t* fmt, ...) return n; } -int hcl_ufmt_out (hcl_fmtout_t* fmtout, const hcl_uch_t* fmt, ...) +int hcl_ufmt_out (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_uch_t* fmt, ...) { va_list ap; int n; @@ -1521,7 +1521,7 @@ int hcl_ufmt_out (hcl_fmtout_t* fmtout, const hcl_uch_t* fmt, ...) fmtout->fmt_str = fmt; va_start (ap, fmt); - n = fmt_outv(fmtout, ap); + n = fmt_outv(hcl, fmtout, ap); va_end (ap); fmtout->fmt_str = fmt_str; @@ -1533,9 +1533,8 @@ int hcl_ufmt_out (hcl_fmtout_t* fmtout, const hcl_uch_t* fmt, ...) * FORMATTED LOG OUTPUT * -------------------------------------------------------------------------- */ -static int log_oocs (hcl_fmtout_t* fmtout, const hcl_ooch_t* ptr, hcl_oow_t len) +static int log_oocs (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_ooch_t* ptr, hcl_oow_t len) { - hcl_t* hcl = (hcl_t*)fmtout->ctx; hcl_oow_t rem; if (len <= 0) return 1; @@ -1628,9 +1627,8 @@ redo: #if defined(HCL_OOCH_IS_BCH) #define log_bcs log_oocs -static int log_ucs (hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) +static int log_ucs (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) { - hcl_t* hcl = (hcl_t*)fmtout->ctx; hcl_bch_t bcs[128]; hcl_oow_t bcslen, rem; @@ -1639,8 +1637,8 @@ static int log_ucs (hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) { len = rem; bcslen = HCL_COUNTOF(bcs); - hcl_conv_uchars_to_bchars_with_cmgr(ptr, &len, bcs, &bcslen, HCL_CMGR(hcl)); - log_bcs(fmtout, bcs, bcslen); + hcl_conv_uchars_to_bchars_with_cmgr (ptr, &len, bcs, &bcslen, HCL_CMGR(hcl)); + log_bcs (hcl, fmtout, bcs, bcslen); rem -= len; ptr += len; } @@ -1652,9 +1650,8 @@ static int log_ucs (hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) #define log_ucs log_oocs -static int log_bcs (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) +static int log_bcs (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) { - hcl_t* hcl = (hcl_t*)fmtout->ctx; hcl_uch_t ucs[64]; hcl_oow_t ucslen, rem; @@ -1663,8 +1660,8 @@ static int log_bcs (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) { len = rem; ucslen = HCL_COUNTOF(ucs); - hcl_conv_bchars_to_uchars_with_cmgr(ptr, &len, ucs, &ucslen, HCL_CMGR(hcl), 1); - log_ucs(fmtout, ucs, ucslen); + hcl_conv_bchars_to_uchars_with_cmgr (ptr, &len, ucs, &ucslen, HCL_CMGR(hcl), 1); + log_ucs (hcl, fmtout, ucs, ucslen); rem -= len; ptr += len; } @@ -1711,14 +1708,13 @@ hcl_ooi_t hcl_logbfmtv (hcl_t* hcl, hcl_bitmask_t mask, const hcl_bch_t* fmt, va HCL_MEMSET (&fo, 0, HCL_SIZEOF(fo)); fo.fmt_type = HCL_FMTOUT_FMT_TYPE_BCH; fo.fmt_str = fmt; - fo.ctx = hcl; fo.mask = mask; fo.mmgr = HCL_MMGR(hcl); fo.putbchars = log_bcs; fo.putuchars = log_ucs; - fo.putobj = hcl_fmt_object_; + fo.putobj = hcl_fmt_object; - x = fmt_outv(&fo, ap); + x = fmt_outv(hcl, &fo, ap); if (hcl->log.len > 0 && hcl->log.ptr[hcl->log.len - 1] == '\n') { @@ -1779,14 +1775,13 @@ hcl_ooi_t hcl_logufmtv (hcl_t* hcl, hcl_bitmask_t mask, const hcl_uch_t* fmt, va HCL_MEMSET (&fo, 0, HCL_SIZEOF(fo)); fo.fmt_type = HCL_FMTOUT_FMT_TYPE_UCH; fo.fmt_str = fmt; - fo.ctx = hcl; fo.mask = mask; fo.mmgr = HCL_MMGR(hcl); fo.putbchars = log_bcs; fo.putuchars = log_ucs; - fo.putobj = hcl_fmt_object_; + fo.putobj = hcl_fmt_object; - x = fmt_outv(&fo, ap); + x = fmt_outv(hcl, &fo, ap); if (hcl->log.len > 0 && hcl->log.ptr[hcl->log.len - 1] == '\n') { @@ -1813,10 +1808,8 @@ hcl_ooi_t hcl_logufmt (hcl_t* hcl, hcl_bitmask_t mask, const hcl_uch_t* fmt, ... * PRINT SUPPORT * -------------------------------------------------------------------------- */ -static int print_bcs (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) +static int print_bcs (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) { - hcl_t* hcl = (hcl_t*)fmtout->ctx; - #if 0 #if defined(HCL_OOCH_IS_UCH) hcl_oow_t ucslen, bcslen; @@ -1889,10 +1882,8 @@ static int print_bcs (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) return 1; /* success */ } -static int print_ucs (hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) +static int print_ucs (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) { - hcl_t* hcl = (hcl_t*)fmtout->ctx; - if (HCL_UNLIKELY(!hcl->io.udo_wrtr)) { hcl_seterrbmsg (hcl, HCL_EINVAL, "no user-defined output handler"); @@ -1956,14 +1947,13 @@ hcl_ooi_t hcl_prbfmtv (hcl_t* hcl, const hcl_bch_t* fmt, va_list ap) HCL_MEMSET (&fo, 0, HCL_SIZEOF(fo)); fo.fmt_type = HCL_FMTOUT_FMT_TYPE_BCH; fo.fmt_str = fmt; - fo.ctx = hcl; fo.mask = 0; fo.mmgr = HCL_MMGR(hcl); fo.putbchars = print_bcs; fo.putuchars = print_ucs; - fo.putobj = hcl_fmt_object_; + fo.putobj = hcl_fmt_object; - x = fmt_outv(&fo, ap); + x = fmt_outv(hcl, &fo, ap); return (x <= -1)? -1: fo.count; } @@ -1989,14 +1979,13 @@ hcl_ooi_t hcl_prufmtv (hcl_t* hcl, const hcl_uch_t* fmt, va_list ap) HCL_MEMSET (&fo, 0, HCL_SIZEOF(fo)); fo.fmt_type = HCL_FMTOUT_FMT_TYPE_UCH; fo.fmt_str = fmt; - fo.ctx = hcl; fo.mask = 0; fo.mmgr = HCL_MMGR(hcl); fo.putbchars = print_bcs; fo.putuchars = print_ucs; - fo.putobj = hcl_fmt_object_; + fo.putobj = hcl_fmt_object; - x = fmt_outv(&fo, ap); + x = fmt_outv(hcl, &fo, ap); return (x <= -1)? -1: fo.count; } @@ -2017,9 +2006,8 @@ hcl_ooi_t hcl_prufmt (hcl_t* hcl, const hcl_uch_t* fmt, ...) * SUPPORT FOR FORMATTED OUTPUT TO BE USED BY BUILTIN PRIMITIVE FUNCTIONS * -------------------------------------------------------------------------- */ -static int sprint_bcs (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) +static int sprint_bcs (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) { - hcl_t* hcl = (hcl_t*)fmtout->ctx; hcl_oow_t unused, oolen, blen; unused = hcl->sprintf.xbuf.capa - hcl->sprintf.xbuf.len; @@ -2056,9 +2044,8 @@ static int sprint_bcs (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len return 1; /* success */ } -static int sprint_ucs (hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) +static int sprint_ucs (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) { - hcl_t* hcl = (hcl_t*)fmtout->ctx; hcl_oow_t unused, oolen, ulen; unused = hcl->sprintf.xbuf.capa - hcl->sprintf.xbuf.len; @@ -2106,10 +2093,8 @@ static int sprint_ucs (hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len else { ch = *(fmt); (fmt)++; }\ } while(0) -static HCL_INLINE int format_stack_args (hcl_fmtout_t* fmtout, hcl_ooi_t nargs, int rcv_is_fmtstr) +static HCL_INLINE int format_stack_args (hcl_t* hcl, hcl_fmtout_t* fmtout, hcl_ooi_t nargs, int rcv_is_fmtstr) { - hcl_t* hcl = (hcl_t*)fmtout->ctx; - const hcl_ooch_t* fmtptr, * fmtend; const hcl_ooch_t* checkpoint, * percent; @@ -2146,11 +2131,11 @@ static HCL_INLINE int format_stack_args (hcl_fmtout_t* fmtout, hcl_ooi_t nargs, hcl_ooi_t i; /* if the first argument is not a valid formatting string, * print all arguments as objects */ - if (fmtout->putobj(fmtout, arg) <= -1) goto oops; + if (fmtout->putobj(hcl, fmtout, arg) <= -1) goto oops; for (i = arg_state.idx; i < nargs; i++) { arg = HCL_STACK_GETARG(hcl, nargs, i); - if (fmtout->putobj(fmtout, arg) <= -1) goto oops; + if (fmtout->putobj(hcl, fmtout, arg) <= -1) goto oops; } return 0; } @@ -2173,11 +2158,11 @@ static HCL_INLINE int format_stack_args (hcl_fmtout_t* fmtout, hcl_ooi_t nargs, { /* fmt is not advanced when it is length-bounded. * so not fmt - checkpoint - 1 */ - PUT_OOCS (fmtout, checkpoint, fmtptr - checkpoint); + PUT_OOCS (hcl, fmtout, checkpoint, fmtptr - checkpoint); goto done; } } - PUT_OOCS (fmtout, checkpoint, fmtptr - checkpoint - 1); + PUT_OOCS (hcl, fmtout, checkpoint, fmtptr - checkpoint - 1); percent = fmtptr - 1; @@ -2407,48 +2392,48 @@ static HCL_INLINE int format_stack_args (hcl_fmtout_t* fmtout, hcl_ooi_t nargs, if (!(flagc & FLAGC_LEFTADJ) && !(flagc & FLAGC_ZEROPAD) && width > extra) { width -= extra; - PUT_OOCH (fmtout, padc, width); + PUT_OOCH (hcl, fmtout, padc, width); width = 0; } - if (neg) PUT_OOCH (fmtout, '-', 1); - else if (flagc & FLAGC_SIGN) PUT_OOCH (fmtout, '+', 1); - else if (flagc & FLAGC_SPACE) PUT_OOCH (fmtout, ' ', 1); + if (neg) PUT_OOCH (hcl, fmtout, '-', 1); + else if (flagc & FLAGC_SIGN) PUT_OOCH (hcl, fmtout, '+', 1); + else if (flagc & FLAGC_SPACE) PUT_OOCH (hcl, fmtout, ' ', 1); if (!(flagc & FLAGC_LEFTADJ) && width > extra) { width -= extra; - PUT_OOCH (fmtout, padc, width); + PUT_OOCH (hcl, fmtout, padc, width); } if (nslen < scale + 1) { - PUT_OOCH (fmtout, '0', 1); + PUT_OOCH (hcl, fmtout, '0', 1); if (precision > 0) { - PUT_OOCH (fmtout, '.', 1); - PUT_OOCH (fmtout, '0', scale - nslen); - PUT_OOCS (fmtout, nsptr, nslen); + PUT_OOCH (hcl, fmtout, '.', 1); + PUT_OOCH (hcl, fmtout, '0', scale - nslen); + PUT_OOCS (hcl, fmtout, nsptr, nslen); } } else { - if (nslen > 0) PUT_OOCS (fmtout, nsptr, nslen - scale); + if (nslen > 0) PUT_OOCS (hcl, fmtout, nsptr, nslen - scale); if (precision > 0) { - PUT_OOCH (fmtout, '.', 1); - if (nslen > 0) PUT_OOCS (fmtout, &nsptr[nslen - scale], scale); + PUT_OOCH (hcl, fmtout, '.', 1); + if (nslen > 0) PUT_OOCS (hcl, fmtout, &nsptr[nslen - scale], scale); } } if (precision > scale) { /* trailing zeros in the fractional part */ - PUT_OOCH (fmtout, '0', precision - scale); + PUT_OOCH (hcl, fmtout, '0', precision - scale); } if ((flagc & FLAGC_LEFTADJ) && width > extra) { width -= extra; - PUT_OOCH (fmtout, padc, width); + PUT_OOCH (hcl, fmtout, padc, width); } break; } @@ -2466,9 +2451,9 @@ static HCL_INLINE int format_stack_args (hcl_fmtout_t* fmtout, hcl_ooi_t nargs, /* precision 0 doesn't kill the letter */ width--; - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); - PUT_OOCH (fmtout, ooch, 1); - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); + PUT_OOCH (hcl, fmtout, ooch, 1); + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); break; case 's': @@ -2496,9 +2481,9 @@ static HCL_INLINE int format_stack_args (hcl_fmtout_t* fmtout, hcl_ooi_t nargs, } width -= oosl; - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); - PUT_OOCS (fmtout, oosp, oosl); - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); + PUT_OOCS (hcl, fmtout, oosp, oosl); + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); break; } @@ -2517,9 +2502,9 @@ static HCL_INLINE int format_stack_args (hcl_fmtout_t* fmtout, hcl_ooi_t nargs, } width -= bsl; - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); - PUT_BCS (fmtout, (const hcl_bch_t*)bsp, bsl); - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); + PUT_BCS (hcl, fmtout, (const hcl_bch_t*)bsp, bsl); + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); break; } @@ -2575,13 +2560,13 @@ static HCL_INLINE int format_stack_args (hcl_fmtout_t* fmtout, hcl_ooi_t nargs, width -= (n * k_hex_width); } - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); while (n--) { if ((lm_flag & LF_H) && BYTE_PRINTABLE(*bsp)) { - PUT_BCH (fmtout, *bsp, 1); + PUT_BCH (hcl, fmtout, *bsp, 1); } else { @@ -2593,13 +2578,13 @@ static HCL_INLINE int format_stack_args (hcl_fmtout_t* fmtout, hcl_ooi_t nargs, if (ch == 'k' || ch == 'w') flagged_radix |= HCL_BYTE_TO_BCSTR_LOWERCASE; #endif hcl_byte_to_bcstr (*bsp, xbuf, HCL_COUNTOF(xbuf), flagged_radix, '0'); - if (lm_flag & (LF_H | LF_L)) PUT_BCS (fmtout, "\\x", 2); - PUT_BCS (fmtout, xbuf, 2); + if (lm_flag & (LF_H | LF_L)) PUT_BCS (hcl, fmtout, "\\x", 2); + PUT_BCS (hcl, fmtout, xbuf, 2); } bsp++; } - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); break; default: @@ -2643,43 +2628,43 @@ static HCL_INLINE int format_stack_args (hcl_fmtout_t* fmtout, hcl_ooi_t nargs, width -= uwid; } - if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); while (n--) { if ((lm_flag & LF_H) && BYTE_PRINTABLE(*usp)) { - PUT_OOCH(fmtout, *usp, 1); + PUT_OOCH (hcl, fmtout, *usp, 1); } else if (!(lm_flag & LF_L) && *usp <= 0xFFFF) { hcl_uint16_t u16 = *usp; int extra_flags = ((ch) == 'w'? HCL_BYTE_TO_BCSTR_LOWERCASE: 0); - PUT_BCS(fmtout, "\\u", 2); - PUT_BYTE_IN_HEX(fmtout, (u16 >> 8) & 0xFF, extra_flags); - PUT_BYTE_IN_HEX(fmtout, u16 & 0xFF, extra_flags); + PUT_BCS (hcl, fmtout, "\\u", 2); + PUT_BYTE_IN_HEX (hcl, fmtout, (u16 >> 8) & 0xFF, extra_flags); + PUT_BYTE_IN_HEX (hcl, fmtout, u16 & 0xFF, extra_flags); } else { hcl_uint32_t u32 = *usp; int extra_flags = ((ch) == 'w'? HCL_BYTE_TO_BCSTR_LOWERCASE: 0); - PUT_BCS(fmtout, "\\u", 2); - PUT_BYTE_IN_HEX(fmtout, (u32 >> 24) & 0xFF, extra_flags); - PUT_BYTE_IN_HEX(fmtout, (u32 >> 16) & 0xFF, extra_flags); - PUT_BYTE_IN_HEX(fmtout, (u32 >> 8) & 0xFF, extra_flags); - PUT_BYTE_IN_HEX(fmtout, u32 & 0xFF, extra_flags); + PUT_BCS (hcl, fmtout, "\\u", 2); + PUT_BYTE_IN_HEX (hcl, fmtout, (u32 >> 24) & 0xFF, extra_flags); + PUT_BYTE_IN_HEX (hcl, fmtout, (u32 >> 16) & 0xFF, extra_flags); + PUT_BYTE_IN_HEX (hcl, fmtout, (u32 >> 8) & 0xFF, extra_flags); + PUT_BYTE_IN_HEX (hcl, fmtout, u32 & 0xFF, extra_flags); } usp++; } - if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); + if ((flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (hcl, fmtout, padc, width); break; } #endif case 'O': /* object - ignore precision, width, adjustment */ GET_NEXT_ARG_TO (hcl, nargs, &arg_state, arg); - if (fmtout->putobj(fmtout, arg) <= -1) goto oops; + if (fmtout->putobj(hcl, fmtout, arg) <= -1) goto oops; break; case 'J': @@ -2688,7 +2673,7 @@ static HCL_INLINE int format_stack_args (hcl_fmtout_t* fmtout, hcl_ooi_t nargs, GET_NEXT_ARG_TO (hcl, nargs, &arg_state, arg); tmp = fmtout->mask; fmtout->mask |= HCL_LOG_PREFER_JSON; - if (fmtout->putobj(fmtout, arg) <= -1) goto oops; + if (fmtout->putobj(hcl, fmtout, arg) <= -1) goto oops; fmtout->mask = tmp; break; } @@ -2763,59 +2748,59 @@ static HCL_INLINE int format_stack_args (hcl_fmtout_t* fmtout, hcl_ooi_t nargs, if (!(flagc & FLAGC_LEFTADJ) && !(flagc & FLAGC_ZEROPAD) && width > 0 && (width -= extra) > 0) { - PUT_OOCH (fmtout, padc, width); + PUT_OOCH (hcl, fmtout, padc, width); width = 0; } - if (neg) PUT_OOCH (fmtout, '-', 1); - else if (flagc & FLAGC_SIGN) PUT_OOCH (fmtout, '+', 1); - else if (flagc & FLAGC_SPACE) PUT_OOCH (fmtout, ' ', 1); + if (neg) PUT_OOCH (hcl, fmtout, '-', 1); + else if (flagc & FLAGC_SIGN) PUT_OOCH (hcl, fmtout, '+', 1); + else if (flagc & FLAGC_SPACE) PUT_OOCH (hcl, fmtout, ' ', 1); if ((flagc & FLAGC_SHARP) && arg != HCL_SMOOI_TO_OOP(0)) { if (radix == 2) { - PUT_OOCH (fmtout, '#', 1); - PUT_OOCH (fmtout, 'b', 1); + PUT_OOCH (hcl, fmtout, '#', 1); + PUT_OOCH (hcl, fmtout, 'b', 1); } if (radix == 8) { - PUT_OOCH (fmtout, '#', 1); - PUT_OOCH (fmtout, 'o', 1); + PUT_OOCH (hcl, fmtout, '#', 1); + PUT_OOCH (hcl, fmtout, 'o', 1); } else if (radix == 16) { - PUT_OOCH (fmtout, '#', 1); - PUT_OOCH (fmtout, 'x', 1); + PUT_OOCH (hcl, fmtout, '#', 1); + PUT_OOCH (hcl, fmtout, 'x', 1); } } if ((flagc & FLAGC_DOT) && precision > nslen) { /* extra zeros for precision specified */ - PUT_OOCH (fmtout, '0', precision - nslen); + PUT_OOCH (hcl, fmtout, '0', precision - nslen); } if (!(flagc & FLAGC_LEFTADJ) && width > 0 && (width -= extra) > 0) { - PUT_OOCH (fmtout, padc, width); + PUT_OOCH (hcl, fmtout, padc, width); } - PUT_OOCS (fmtout, nsptr, nslen); + PUT_OOCS (hcl, fmtout, nsptr, nslen); if ((flagc & FLAGC_LEFTADJ) && width > 0 && (width -= extra) > 0) { - PUT_OOCH (fmtout, padc, width); + PUT_OOCH (hcl, fmtout, padc, width); } break; } invalid_format: - PUT_OOCS (fmtout, percent, fmtptr - percent); + PUT_OOCS (hcl, fmtout, percent, fmtptr - percent); break; default: - PUT_OOCS (fmtout, percent, fmtptr - percent); + PUT_OOCS (hcl, fmtout, percent, fmtptr - percent); /* * Since we ignore an formatting argument it is no * longer safe to obey the remaining formatting @@ -2840,15 +2825,14 @@ int hcl_strfmtcallstack (hcl_t* hcl, hcl_ooi_t nargs) hcl_fmtout_t fo; HCL_MEMSET (&fo, 0, HCL_SIZEOF(fo)); - fo.ctx = hcl; fo.putbchars = sprint_bcs; fo.putuchars = sprint_ucs; - fo.putobj = hcl_fmt_object_; + fo.putobj = hcl_fmt_object; /* format_stack_args doesn't use fmt_str and fmt_type. * it takes the format string from the stack. */ hcl->sprintf.xbuf.len = 0; - return format_stack_args(&fo, nargs, 0); + return format_stack_args(hcl, &fo, nargs, 0); } int hcl_prfmtcallstack (hcl_t* hcl, hcl_ooi_t nargs) @@ -2857,16 +2841,14 @@ int hcl_prfmtcallstack (hcl_t* hcl, hcl_ooi_t nargs) hcl_fmtout_t fo; HCL_MEMSET (&fo, 0, HCL_SIZEOF(fo)); - fo.mask = 0; fo.mmgr = HCL_MMGR(hcl); - fo.ctx = hcl; fo.putbchars = print_bcs; fo.putuchars = print_ucs; - fo.putobj = hcl_fmt_object_; + fo.putobj = hcl_fmt_object; /* format_stack_args doesn't use fmt_str and fmt_type. * it takes the format string from the stack. */ - return format_stack_args(&fo, nargs, 0); + return format_stack_args(hcl, &fo, nargs, 0); } int hcl_logfmtcallstack (hcl_t* hcl, hcl_ooi_t nargs) @@ -2889,14 +2871,13 @@ int hcl_logfmtcallstack (hcl_t* hcl, hcl_ooi_t nargs) } fo.mmgr = HCL_MMGR(hcl); - fo.ctx = hcl; fo.putbchars = log_bcs; fo.putuchars = log_ucs; - fo.putobj = hcl_fmt_object_; + fo.putobj = hcl_fmt_object; /* format_stack_args doesn't use fmt_str and fmt_type. * it takes the format string from the stack. */ - return format_stack_args(&fo, nargs, 0); + return format_stack_args(hcl, &fo, nargs, 0); } /* -------------------------------------------------------------------------- @@ -2912,7 +2893,7 @@ struct fmt_uch_buf_t }; typedef struct fmt_uch_buf_t fmt_uch_buf_t; -static int fmt_put_bchars_to_uch_buf (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) +static int fmt_put_bchars_to_uch_buf (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) { fmt_uch_buf_t* b = (fmt_uch_buf_t*)fmtout->ctx; hcl_oow_t bcslen, ucslen; @@ -2938,7 +2919,7 @@ static int fmt_put_bchars_to_uch_buf (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr return 1; /* success. carry on */ } -static int fmt_put_uchars_to_uch_buf (hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) +static int fmt_put_uchars_to_uch_buf (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) { fmt_uch_buf_t* b = (fmt_uch_buf_t*)fmtout->ctx; hcl_oow_t n; @@ -2966,6 +2947,7 @@ hcl_oow_t hcl_vfmttoucstr (hcl_t* hcl, hcl_uch_t* buf, hcl_oow_t bufsz, const hc fo.mmgr = hcl->_mmgr; fo.putbchars = fmt_put_bchars_to_uch_buf; fo.putuchars = fmt_put_uchars_to_uch_buf; + fo.putobj = hcl_fmt_object; fo.ctx = &fb; HCL_MEMSET (&fb, 0, HCL_SIZEOF(fb)); @@ -2973,7 +2955,7 @@ hcl_oow_t hcl_vfmttoucstr (hcl_t* hcl, hcl_uch_t* buf, hcl_oow_t bufsz, const hc fb.ptr = buf; fb.capa = bufsz - 1; - if (hcl_ufmt_outv(&fo, fmt, ap) <= -1) return -1; + if (hcl_ufmt_outv(hcl, &fo, fmt, ap) <= -1) return -1; buf[fb.len] = '\0'; return fb.len; @@ -3002,8 +2984,7 @@ struct fmt_bch_buf_t }; typedef struct fmt_bch_buf_t fmt_bch_buf_t; - -static int fmt_put_bchars_to_bch_buf (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) +static int fmt_put_bchars_to_bch_buf (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len) { fmt_bch_buf_t* b = (fmt_bch_buf_t*)fmtout->ctx; hcl_oow_t n; @@ -3020,8 +3001,7 @@ static int fmt_put_bchars_to_bch_buf (hcl_fmtout_t* fmtout, const hcl_bch_t* ptr return 1; /* success */ } - -static int fmt_put_uchars_to_bch_buf (hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) +static int fmt_put_uchars_to_bch_buf (hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len) { fmt_bch_buf_t* b = (fmt_bch_buf_t*)fmtout->ctx; hcl_oow_t bcslen, ucslen; @@ -3058,6 +3038,7 @@ hcl_oow_t hcl_vfmttobcstr (hcl_t* hcl, hcl_bch_t* buf, hcl_oow_t bufsz, const hc fo.mmgr = hcl->_mmgr; fo.putbchars = fmt_put_bchars_to_bch_buf; fo.putuchars = fmt_put_uchars_to_bch_buf; + fo.putobj = hcl_fmt_object; fo.ctx = &fb; HCL_MEMSET (&fb, 0, HCL_SIZEOF(fb)); @@ -3065,7 +3046,7 @@ hcl_oow_t hcl_vfmttobcstr (hcl_t* hcl, hcl_bch_t* buf, hcl_oow_t bufsz, const hc fb.ptr = buf; fb.capa = bufsz - 1; - if (hcl_bfmt_outv(&fo, fmt, ap) <= -1) return -1; + if (hcl_bfmt_outv(hcl, &fo, fmt, ap) <= -1) return -1; buf[fb.len] = '\0'; return fb.len; diff --git a/lib/hcl-fmt.h b/lib/hcl-fmt.h index 8633eeb..8f068a1 100644 --- a/lib/hcl-fmt.h +++ b/lib/hcl-fmt.h @@ -145,18 +145,21 @@ enum hcl_fmt_intmax_flag_t typedef struct hcl_fmtout_t hcl_fmtout_t; typedef int (*hcl_fmtout_putbchars_t) ( + hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_bch_t* ptr, hcl_oow_t len ); typedef int (*hcl_fmtout_putuchars_t) ( + hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_uch_t* ptr, hcl_oow_t len ); typedef int (*hcl_fmtout_putobj_t) ( + hcl_t* hcl, hcl_fmtout_t* fmtout, hcl_oop_t obj ); @@ -168,7 +171,6 @@ enum hcl_fmtout_fmt_type_t }; typedef enum hcl_fmtout_fmt_type_t hcl_fmtout_fmt_type_t; - struct hcl_fmtout_t { hcl_oow_t count; /* out */ @@ -180,7 +182,6 @@ struct hcl_fmtout_t hcl_bitmask_t mask; /* in */ void* ctx; /* in */ - /* internally set as input */ hcl_fmtout_fmt_type_t fmt_type; const void* fmt_str; @@ -356,12 +357,14 @@ HCL_EXPORT int hcl_fmt_uintmax_to_ucstr ( * FORMATTED OUTPUT * ========================================================================= */ HCL_EXPORT int hcl_bfmt_outv ( + hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_bch_t* fmt, va_list ap ); HCL_EXPORT int hcl_ufmt_outv ( + hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_uch_t* fmt, va_list ap @@ -369,12 +372,14 @@ HCL_EXPORT int hcl_ufmt_outv ( HCL_EXPORT int hcl_bfmt_out ( + hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_bch_t* fmt, ... ); HCL_EXPORT int hcl_ufmt_out ( + hcl_t* hcl, hcl_fmtout_t* fmtout, const hcl_uch_t* fmt, ... diff --git a/lib/hcl-prv.h b/lib/hcl-prv.h index 618b7a6..08500af 100644 --- a/lib/hcl-prv.h +++ b/lib/hcl-prv.h @@ -1891,7 +1891,8 @@ hcl_pfbase_t* hcl_querymod ( /* ========================================================================= */ /* fmt.c */ /* ========================================================================= */ -int hcl_fmt_object_ ( +int hcl_fmt_object ( + hcl_t* hcl, hcl_fmtout_t* fmtout, hcl_oop_t oop ); diff --git a/lib/hcl-x.h b/lib/hcl-x.h index bcec8d3..662429a 100644 --- a/lib/hcl-x.h +++ b/lib/hcl-x.h @@ -163,7 +163,7 @@ typedef void (*hcl_client_log_write_t) ( ); typedef int (*hcl_client_on_packet_t) ( - hcl_xproto_t* proto, + hcl_client_t* client, hcl_xpkt_type_t type, const void* data, hcl_oow_t len diff --git a/lib/hcl.h b/lib/hcl.h index 82a011a..aa2b0b5 100644 --- a/lib/hcl.h +++ b/lib/hcl.h @@ -2255,19 +2255,33 @@ HCL_EXPORT const hcl_ooch_t* hcl_geterrstr ( ); HCL_EXPORT const hcl_uch_t* hcl_geterrumsg ( - hcl_t* hio + hcl_t* hcl ); HCL_EXPORT const hcl_bch_t* hcl_geterrbmsg ( - hcl_t* hio + hcl_t* hcl +); + +HCL_EXPORT hcl_oow_t hcl_copyerrbmsg ( + hcl_t* hcl, + hcl_bch_t* buf, + hcl_oow_t len +); + +HCL_EXPORT hcl_oow_t hcl_copyerrumsg ( + hcl_t* hcl, + hcl_uch_t* buf, + hcl_oow_t len ); #if defined(HCL_OOCH_IS_UCH) -# define hcl_geterrmsg hcl_geterrumsg -# define hcl_seterrmsg hcl_seterrumsg +# define hcl_geterrmsg hcl_geterrumsg +# define hcl_seterrmsg hcl_seterrumsg +# define hcl_copyerrmsg hcl_copyerrumsg #else -# define hcl_geterrmsg hcl_geterrbmsg -# define hcl_seterrmsg hcl_seterrbmsg +# define hcl_geterrmsg hcl_geterrbmsg +# define hcl_seterrmsg hcl_seterrbmsg +# define hcl_copyerrmsg hcl_copyerrbmsg #endif HCL_EXPORT const hcl_ooch_t* hcl_backuperrmsg ( diff --git a/lib/prim.c b/lib/prim.c index d9eb6d8..50a4393 100644 --- a/lib/prim.c +++ b/lib/prim.c @@ -800,7 +800,7 @@ static hcl_pfrc_t pf_va_context (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs) static hcl_pfrc_t pf_va_count (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs) { hcl_oop_context_t ctx; - hcl_ooi_t attr_mask, va, fixed_nargs, nrvars, nlvars, nvaargs; + hcl_ooi_t attr_mask, /*va,*/ fixed_nargs, nrvars, nlvars, nvaargs; if (nargs >= 1) { @@ -818,7 +818,7 @@ static hcl_pfrc_t pf_va_count (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs) attr_mask = HCL_OOP_TO_SMOOI(ctx->attr_mask); - va = GET_BLK_MASK_VA(attr_mask); + /*va = GET_BLK_MASK_VA(attr_mask);*/ fixed_nargs = GET_BLK_MASK_NARGS(attr_mask); nrvars = GET_BLK_MASK_NRVARS(attr_mask); nlvars = GET_BLK_MASK_NLVARS(attr_mask); @@ -835,7 +835,7 @@ static hcl_pfrc_t pf_va_count (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs) static hcl_pfrc_t pf_va_get (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs) { hcl_oop_context_t ctx; - hcl_ooi_t attr_mask, va, fixed_nargs, nrvars, nlvars, nvaargs; + hcl_ooi_t attr_mask, /*va,*/ fixed_nargs, nrvars, nlvars, nvaargs; hcl_oow_t index; if (nargs >= 2) @@ -853,7 +853,7 @@ static hcl_pfrc_t pf_va_get (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs) } attr_mask = HCL_OOP_TO_SMOOI(ctx->attr_mask); - va = GET_BLK_MASK_VA(attr_mask); + /*va = GET_BLK_MASK_VA(attr_mask);*/ fixed_nargs = GET_BLK_MASK_NARGS(attr_mask); nrvars = GET_BLK_MASK_NRVARS(attr_mask); nlvars = GET_BLK_MASK_NLVARS(attr_mask); diff --git a/lib/print.c b/lib/print.c index 50925ce..b629729 100644 --- a/lib/print.c +++ b/lib/print.c @@ -121,12 +121,12 @@ static struct { 11, { '#','<','I','N','S','T','A','N','C','E','>' } } }; -static HCL_INLINE int print_single_char (hcl_fmtout_t* fmtout, hcl_ooch_t ch) +static HCL_INLINE int print_single_char (hcl_t* hcl, hcl_fmtout_t* fmtout, hcl_ooch_t ch) { hcl_oochu_t chu = (hcl_oochu_t)ch; if (chu == '\\' || chu == '\"') { - if (hcl_bfmt_out(fmtout, "\\%jc", chu) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "\\%jc", chu) <= -1) return -1; } #if defined(HCL_OOCH_IS_UCH) else if (chu < ' ') @@ -180,31 +180,30 @@ static HCL_INLINE int print_single_char (hcl_fmtout_t* fmtout, hcl_ooch_t ch) #if (HCL_SIZEOF_OOCH_T >= 2) if (chu >= 0x100u) { - if (hcl_bfmt_out(fmtout, "\\u%04X", chu) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "\\u%04X", chu) <= -1) return -1; } else #endif { - if (hcl_bfmt_out(fmtout, "\\x%02X", chu) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "\\x%02X", chu) <= -1) return -1; } } } else { - if (hcl_bfmt_out(fmtout, "\\%jc", escaped) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "\\%jc", escaped) <= -1) return -1; } } else { - if (hcl_bfmt_out(fmtout, "%jc", ch) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "%jc", ch) <= -1) return -1; } return 0; } -int hcl_fmt_object_ (hcl_fmtout_t* fmtout, hcl_oop_t obj) +int hcl_fmt_object (hcl_t* hcl, hcl_fmtout_t* fmtout, hcl_oop_t obj) { - hcl_t* hcl = (hcl_t*)fmtout->ctx; hcl_oop_t cur; print_stack_t ps; int brand; @@ -254,23 +253,23 @@ next: switch ((brand = HCL_BRANDOF(hcl, obj))) { case HCL_BRAND_SMOOI: - if (hcl_bfmt_out(fmtout, "%zd", HCL_OOP_TO_SMOOI(obj)) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "%zd", HCL_OOP_TO_SMOOI(obj)) <= -1) return -1; goto done; case HCL_BRAND_SMPTR: - if (hcl_bfmt_out(fmtout, "#p%zX", (hcl_oow_t)HCL_OOP_TO_SMPTR(obj)) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "#p%zX", (hcl_oow_t)HCL_OOP_TO_SMPTR(obj)) <= -1) return -1; goto done; case HCL_BRAND_ERROR: - if (hcl_bfmt_out(fmtout, "#e%zd", (hcl_ooi_t)HCL_OOP_TO_ERROR(obj)) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "#e%zd", (hcl_ooi_t)HCL_OOP_TO_ERROR(obj)) <= -1) return -1; goto done; case HCL_BRAND_CHARACTER: { hcl_ooch_t ch = HCL_OOP_TO_CHAR(obj); - if (hcl_bfmt_out(fmtout, "\'") <= -1 || - print_single_char(fmtout, ch) <= -1 || - hcl_bfmt_out(fmtout, "\'") <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "\'") <= -1 || + print_single_char(hcl, fmtout, ch) <= -1 || + hcl_bfmt_out(hcl, fmtout, "\'") <= -1) return -1; goto done; } @@ -302,7 +301,7 @@ next: if (!tmp) return -1; HCL_ASSERT (hcl, (hcl_oop_t)tmp == hcl->_nil); - if (hcl_bfmt_out(fmtout, "%.*js", hcl->inttostr.xbuf.len, hcl->inttostr.xbuf.ptr) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "%.*js", hcl->inttostr.xbuf.len, hcl->inttostr.xbuf.ptr) <= -1) return -1; break; } @@ -317,11 +316,11 @@ next: { if (scale == 0) { - if (hcl_bfmt_out(fmtout, "0.") <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "0.") <= -1) return -1; } else { - if (hcl_bfmt_out(fmtout, "0.%0*d", scale, 0) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "0.%0*d", scale, 0) <= -1) return -1; } } else @@ -339,13 +338,13 @@ next: { if (scale == len) { - if (hcl_bfmt_out(fmtout, "%.*js0.%.*js", + if (hcl_bfmt_out(hcl, fmtout, "%.*js0.%.*js", adj, hcl->inttostr.xbuf.ptr, len, &hcl->inttostr.xbuf.ptr[adj]) <= -1) return -1; } else { - if (hcl_bfmt_out(fmtout, "%.*js0.%0*d%.*js", + if (hcl_bfmt_out(hcl, fmtout, "%.*js0.%0*d%.*js", adj, hcl->inttostr.xbuf.ptr, scale - len, 0, len, &hcl->inttostr.xbuf.ptr[adj]) <= -1) return -1; @@ -355,7 +354,7 @@ next: { hcl_ooi_t ndigits; ndigits = hcl->inttostr.xbuf.len - scale; - if (hcl_bfmt_out(fmtout, "%.*js.%.*js", ndigits, hcl->inttostr.xbuf.ptr, scale, &hcl->inttostr.xbuf.ptr[ndigits]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "%.*js.%.*js", ndigits, hcl->inttostr.xbuf.ptr, scale, &hcl->inttostr.xbuf.ptr[ndigits]) <= -1) return -1; } } break; @@ -385,7 +384,7 @@ next: /* Any needs for special action if SYNT(obj) is true? * I simply treat the syntax symbol as a normal symbol * for printing currently. */ - if (hcl_bfmt_out(fmtout, "%.*js", HCL_OBJ_GET_SIZE(obj), HCL_OBJ_GET_CHAR_SLOT(obj)) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "%.*js", HCL_OBJ_GET_SIZE(obj), HCL_OBJ_GET_CHAR_SLOT(obj)) <= -1) return -1; break; case HCL_BRAND_STRING: @@ -406,17 +405,17 @@ next: if (escape) { - if (hcl_bfmt_out(fmtout, "\"") <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "\"") <= -1) return -1; for (i = 0; i < HCL_OBJ_GET_SIZE(obj); i++) { ch = ((hcl_oop_char_t)obj)->slot[i]; - if (print_single_char(fmtout, ch) <= -1) return -1; + if (print_single_char(hcl, fmtout, ch) <= -1) return -1; } - if (hcl_bfmt_out(fmtout, "\"") <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "\"") <= -1) return -1; } else { - if (hcl_bfmt_out(fmtout, "\"%.*js\"", HCL_OBJ_GET_SIZE(obj), HCL_OBJ_GET_CHAR_SLOT(obj)) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "\"%.*js\"", HCL_OBJ_GET_SIZE(obj), HCL_OBJ_GET_CHAR_SLOT(obj)) <= -1) return -1; } break; } @@ -429,7 +428,7 @@ next: * request to output in the json format */ concode = HCL_OBJ_GET_FLAGS_SYNCODE(obj); - if (hcl_bfmt_out(fmtout, opening_parens[concode][0]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, opening_parens[concode][0]) <= -1) return -1; cur = obj; /* TODO: for MLIST, print : after the first element. @@ -466,7 +465,7 @@ next: if (!HCL_OOP_IS_POINTER(cur) || HCL_OBJ_GET_FLAGS_BRAND(cur) != HCL_BRAND_CONS) { /* The CDR part does not point to a pair. */ - if (hcl_bfmt_out(fmtout, " . ") <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, " . ") <= -1) return -1; /* Push NIL so that the HCL_IS_NIL(hcl,p) test in * the 'if' statement above breaks the loop @@ -483,11 +482,11 @@ next: } /* The CDR part points to a pair. proceed to it */ - if (hcl_bfmt_out(fmtout, breakers[0][0]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, breakers[0][0]) <= -1) return -1; } while (1); - if (hcl_bfmt_out(fmtout, closing_parens[concode][0]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, closing_parens[concode][0]) <= -1) return -1; break; } @@ -495,11 +494,11 @@ next: { hcl_oow_t arridx; - if (hcl_bfmt_out(fmtout, opening_parens[HCL_CONCODE_ARRAY][json]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, opening_parens[HCL_CONCODE_ARRAY][json]) <= -1) return -1; if (HCL_OBJ_GET_SIZE(obj) <= 0) { - if (hcl_bfmt_out(fmtout, closing_parens[HCL_CONCODE_ARRAY][json]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, closing_parens[HCL_CONCODE_ARRAY][json]) <= -1) return -1; break; } arridx = 0; @@ -527,7 +526,7 @@ next: obj = ((hcl_oop_oop_t)obj)->slot[arridx]; if (arridx > 0) { - if (hcl_bfmt_out(fmtout, breakers[0][json]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, breakers[0][json]) <= -1) return -1; } /* Jump to the 'next' label so that the object * pointed to by 'obj' is printed. Once it @@ -547,16 +546,16 @@ next: case HCL_BRAND_BYTE_ARRAY: { hcl_oow_t i; - if (hcl_bfmt_out(fmtout, opening_parens[HCL_CONCODE_BYTEARRAY][json]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, opening_parens[HCL_CONCODE_BYTEARRAY][json]) <= -1) return -1; if (HCL_OBJ_GET_SIZE(obj) > 0) { - if (hcl_bfmt_out(fmtout, "%d", ((hcl_oop_byte_t)obj)->slot[0]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "%d", ((hcl_oop_byte_t)obj)->slot[0]) <= -1) return -1; for (i = 1; i < HCL_OBJ_GET_SIZE(obj); i++) { - if (hcl_bfmt_out(fmtout, "%hs%d", breakers[0][json], ((hcl_oop_byte_t)obj)->slot[i]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "%hs%d", breakers[0][json], ((hcl_oop_byte_t)obj)->slot[i]) <= -1) return -1; } } - if (hcl_bfmt_out(fmtout, closing_parens[HCL_CONCODE_BYTEARRAY][json]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, closing_parens[HCL_CONCODE_BYTEARRAY][json]) <= -1) return -1; break; } @@ -565,13 +564,13 @@ next: hcl_oow_t bucidx, bucsize, buctally; hcl_oop_dic_t dic; - if (hcl_bfmt_out(fmtout, opening_parens[HCL_CONCODE_DIC][json]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, opening_parens[HCL_CONCODE_DIC][json]) <= -1) return -1; dic = (hcl_oop_dic_t)obj; HCL_ASSERT (hcl, HCL_OOP_IS_SMOOI(dic->tally)); if (HCL_OOP_TO_SMOOI(dic->tally) <= 0) { - if (hcl_bfmt_out(fmtout, closing_parens[HCL_CONCODE_DIC][json]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, closing_parens[HCL_CONCODE_DIC][json]) <= -1) return -1; break; } bucidx = 0; @@ -597,7 +596,7 @@ next: if (bucidx >= bucsize) { /* done. scanned the entire bucket */ - if (hcl_bfmt_out(fmtout, closing_parens[HCL_CONCODE_DIC][json]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, closing_parens[HCL_CONCODE_DIC][json]) <= -1) return -1; break; } @@ -638,7 +637,7 @@ next: if (buctally > 0) { - if (hcl_bfmt_out(fmtout, breakers[buctally & 1][json]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, breakers[buctally & 1][json]) <= -1) return -1; } /* Jump to the 'next' label so that the object @@ -709,7 +708,7 @@ next: return -1; print_word: - if (hcl_bfmt_out(fmtout, "%.*js", word[word_index].len, word[word_index].ptr) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, "%.*js", word[word_index].len, word[word_index].ptr) <= -1) return -1; break; } @@ -727,14 +726,14 @@ done: goto resume_array; case PRINT_STACK_ARRAY_END: - if (hcl_bfmt_out(fmtout, closing_parens[HCL_CONCODE_ARRAY][json]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, closing_parens[HCL_CONCODE_ARRAY][json]) <= -1) return -1; break; case PRINT_STACK_DIC: goto resume_dic; case PRINT_STACK_DIC_END: - if (hcl_bfmt_out(fmtout, closing_parens[HCL_CONCODE_DIC][json]) <= -1) return -1; + if (hcl_bfmt_out(hcl, fmtout, closing_parens[HCL_CONCODE_DIC][json]) <= -1) return -1; break; default: diff --git a/lib/x-client.c b/lib/x-client.c index bd0cf5c..17a45d6 100644 --- a/lib/x-client.c +++ b/lib/x-client.c @@ -446,6 +446,15 @@ struct proto_xtn_t }; typedef struct proto_xtn_t proto_xtn_t; +static int proto_on_packet (hcl_xproto_t* proto, hcl_xpkt_type_t type, const void* data, hcl_oow_t len) +{ + proto_xtn_t* proto_xtn; + hcl_client_t* client; + proto_xtn = hcl_xproto_getxtn(proto); + client = proto_xtn->client; + return client->prim.on_packet(client, type, data, len); +} + static int client_connect_to_server (hcl_client_t* client, const char* ipaddr) { hcl_sckaddr_t sckaddr; @@ -516,7 +525,7 @@ static int client_connect_to_server (hcl_client_t* client, const char* ipaddr) hcl_sys_set_nonblock(sck, 1); /* make it nonblocking after connection has been established */ HCL_MEMSET (&proto, 0, HCL_SIZEOF(proto_cb)); - proto_cb.on_packet = client->prim.on_packet; + proto_cb.on_packet = proto_on_packet; proto = hcl_xproto_open(hcl_client_getmmgr(client), &proto_cb, HCL_SIZEOF(*proto_xtn)); if (HCL_UNLIKELY(!proto)) @@ -635,7 +644,7 @@ hcl_client_logbfmt(client, HCL_LOG_STDERR, "ON CONTROL EVENT \n"); static void on_remote_event (hcl_client_t* client, struct pollfd* pfd, int shut_wr_after_req) { -hcl_client_logbfmt(client, HCL_LOG_STDERR, "ON REMOTE EVENT \n"); +//hcl_client_logbfmt(client, HCL_LOG_STDERR, "ON REMOTE EVENT \n"); if (pfd->revents & POLLOUT) { @@ -718,7 +727,7 @@ static void on_local_in_event (hcl_client_t* client, struct pollfd* pfd) ssize_t n; hcl_uint8_t buf[128]; -hcl_client_logbfmt(client, HCL_LOG_STDERR, "local in on %d\n", pfd->fd); +//hcl_client_logbfmt(client, HCL_LOG_STDERR, "local in on %d\n", pfd->fd); n = read(pfd->fd, buf, HCL_SIZEOF(buf)); if (n <= -1) { @@ -825,7 +834,7 @@ hcl_client_logbfmt(client, HCL_LOG_STDERR, "staritg client loop... ...\n"); { if (!pfd[i].revents) continue; -//hcl_client_logbfmt(client, HCL_LOG_STDERR, "EVENT ON %d mux[%d], remote[%d], local[%d]\n", pfd[i].fd, client->mux_pipe[0], client->remote.sck, client->local.in); +//hcl_client_logbfmt(client, HCL_LOG_STDERR, "EVENT ON %d mux[%d], remote[%d], local[%d]\n", pfd[i].fd, client->mux_pipe[0], client->remote.sck, client->local.in); if (pfd[i].fd == client->mux_pipe[0]) { on_control_event (client, &pfd[i]); diff --git a/lib/x-server.c b/lib/x-server.c index 2c9ba63..5ef49d7 100644 --- a/lib/x-server.c +++ b/lib/x-server.c @@ -248,12 +248,12 @@ struct hcl_server_t }; /* ========================================================================= */ -static int send_stdout_bytes (hcl_xproto_t* proto, int xpkt_code, const hcl_bch_t* data, hcl_oow_t len); +static int send_bytes (hcl_xproto_t* proto, hcl_xpkt_type_t xpkt_code, const hcl_bch_t* data, hcl_oow_t len); #if defined(HCL_OOCH_IS_UCH) -static int send_stdout_chars (hcl_xproto_t* proto, int xpkt_code, const hcl_ooch_t* data, hcl_oow_t len); +static int send_chars (hcl_xproto_t* proto, hcl_xpkt_type_t xpkt_code, const hcl_ooch_t* data, hcl_oow_t len); #else -#define send_stdout_chars(proto,xpkt_code,data,len) send_stdout_bytes(proto,xpkt_code,data,len) +#define send_chars(proto,xpkt_code,data,len) send_bytes(proto,xpkt_code,data,len) #endif /* ========================================================================= */ @@ -550,7 +550,7 @@ printf ("IO CLOSE SOMETHING...........\n"); hcl_io_udoarg_t* outarg = (hcl_io_udoarg_t*)arg; printf ("IO WRITE SOMETHING...........\n"); - if (send_stdout_chars(xtn->worker->proto, HCL_XPKT_STDOUT, outarg->ptr, outarg->len) <= -1) + if (send_chars(xtn->worker->proto, HCL_XPKT_STDOUT, outarg->ptr, outarg->len) <= -1) { /* TODO: change error code and message. propagage the errormessage from proto */ hcl_seterrbfmt (hcl, HCL_EIOERR, "failed to write message via proto"); @@ -570,7 +570,7 @@ printf ("IO WRITE SOMETHING...........\n"); hcl_io_udoarg_t* outarg = (hcl_io_udoarg_t*)arg; printf ("IO WRITE SOMETHING BYTES...........\n"); - if (send_stdout_bytes(xtn->worker->proto, HCL_XPKT_STDOUT, outarg->ptr, outarg->len) <= -1) + if (send_bytes(xtn->worker->proto, HCL_XPKT_STDOUT, outarg->ptr, outarg->len) <= -1) { /* TODO: change error code and message. propagage the errormessage from proto */ hcl_seterrbfmt (hcl, HCL_EIOERR, "failed to write message via proto"); @@ -584,6 +584,10 @@ printf ("IO WRITE SOMETHING BYTES...........\n"); return 0; } + case HCL_IO_FLUSH: +/* TODO: flush data... */ + return 0; + default: hcl_seterrnum (hcl, HCL_EINTERN); return -1; @@ -681,7 +685,7 @@ static int on_fed_cnode (hcl_t* hcl, hcl_cnode_t* obj) if (hcl_compile(hcl, obj, flags) <= -1) { const hcl_bch_t* errmsg = hcl_geterrbmsg(hcl); - send_stdout_bytes(proto, HCL_XPKT_ERROR, errmsg, hcl_count_bcstr(errmsg)); + send_bytes(proto, HCL_XPKT_ERROR, errmsg, hcl_count_bcstr(errmsg)); /* TODO: ignore the whole line??? */ } @@ -946,15 +950,51 @@ printf ("FEEDING [%.*s]\n", (int)len, data); printf ("EXECUTING hcl_executing......\n"); hcl_decode (hcl, hcl_getcode(hcl), 0, hcl_getbclen(hcl)); + if (hcl_feedpending(hcl)) + { + /* TODO: change the message */ + if (send_bytes(proto, HCL_XPKT_ERROR, "feed more", 9) <=-1) + { + /* TODO: error handling */ + } + break; + } retv = hcl_execute(hcl); - hcl_flushudio (hcl); - hcl_clearcode (hcl); - if (!retv) + if (HCL_UNLIKELY(!retv)) { + hcl_bch_t errmsg[512]; + hcl_oow_t errlen; + /* TODO: backup error message...and create a new message */ + /* save error message before other calls override erro info */ + errlen = hcl_copyerrbmsg(hcl, errmsg, HCL_COUNTOF(errmsg)); + + hcl_flushudio (hcl); + hcl_clearcode (hcl); + + if (send_bytes(proto, HCL_XPKT_ERROR, errmsg, errlen) <= -1) + { + /* TODO: error handling */ + } + goto oops; } + else + { + hcl_bch_t rvbuf[512]; /* TODO make this dynamic in side? */ + hcl_oow_t rvlen; + + hcl_flushudio (hcl); + hcl_clearcode (hcl); + + /* TODO or make hcl_fmtXXXX that accepts the output function */ + rvlen = hcl_fmttobcstr(hcl, rvbuf, HCL_COUNTOF(rvbuf), "[%O]", retv); + if (send_bytes(proto, HCL_XPKT_RETVAL, rvbuf, rvlen) <= -1) + { + } + } + break; } @@ -986,7 +1026,7 @@ oops: return -1; } -static int send_stdout_bytes (hcl_xproto_t* proto, int xpkt_code, const hcl_bch_t* data, hcl_oow_t len) +static int send_bytes (hcl_xproto_t* proto, hcl_xpkt_type_t xpkt_type, const hcl_bch_t* data, hcl_oow_t len) { hcl_server_worker_t* worker; hcl_xpkt_hdr_t hdr; @@ -995,28 +1035,33 @@ static int send_stdout_bytes (hcl_xproto_t* proto, int xpkt_code, const hcl_bch_ hcl_uint16_t seglen; worker = proto_to_worker(proto); - HCL_ASSERT (worker->hcl, xpkt_code == HCL_XPKT_STDOUT || xpkt_code == HCL_XPKT_STDERR || xpkt_code == HCL_XPKT_ERROR); ptr = cur = data; end = data + len; printf ("SENDING BYTES [%.*s]\n", (int)len, data); - while (ptr < end) + do { + int nv; + while (cur != end && cur - ptr < HCL_XPKT_MAX_PLD_LEN) cur++; seglen = cur - ptr; hdr.id = 1; /* TODO: */ - hdr.type = xpkt_code | (((seglen >> 8) & 0x0F) << 4); + hdr.type = xpkt_type | (((seglen >> 8) & 0x0F) << 4); hdr.len = seglen & 0xFF; - iov[0].iov_base = &hdr; - iov[0].iov_len = HCL_SIZEOF(hdr); - iov[1].iov_base = ptr; - iov[1].iov_len = seglen; + nv = 0; + iov[nv].iov_base = &hdr; + iov[nv++].iov_len = HCL_SIZEOF(hdr); + if (seglen > 0) + { + iov[nv].iov_base = ptr; + iov[nv++].iov_len = seglen; + } - if (hcl_sys_send_iov(worker->sck, iov, 2) <= -1) + if (hcl_sys_send_iov(worker->sck, iov, nv) <= -1) { /* TODO: error message */ fprintf (stderr, "Unable to sendmsg on %d - %s\n", worker->sck, strerror(errno)); @@ -1025,12 +1070,13 @@ fprintf (stderr, "Unable to sendmsg on %d - %s\n", worker->sck, strerror(errno)) ptr = cur; } + while (ptr < end); return 0; } #if defined(HCL_OOCH_IS_UCH) -static int send_stdout_chars (hcl_xproto_t* proto, int xpkt_code, const hcl_ooch_t* data, hcl_oow_t len) +static int send_chars (hcl_xproto_t* proto, hcl_xpkt_type_t xpkt_type, const hcl_ooch_t* data, hcl_oow_t len) { hcl_server_worker_t* worker; const hcl_ooch_t* ptr, * end; @@ -1050,7 +1096,7 @@ static int send_stdout_chars (hcl_xproto_t* proto, int xpkt_code, const hcl_ooch n = hcl_convutobchars(worker->hcl, ptr, &pln, tmp, &tln); if (n <= -1 && n != -2) return -1; - if (send_stdout_bytes(proto, xpkt_code, tmp, tln) <= -1) return -1; + if (send_bytes(proto, xpkt_type, tmp, tln) <= -1) return -1; ptr += pln; } @@ -1791,7 +1837,7 @@ static int setup_listeners (hcl_server_t* server, const hcl_bch_t* addrs) optval = 1; setsockopt (srv_fd, SOL_SOCKET, SO_REUSEADDR, &optval, HCL_SIZEOF(int)); - hcl_sys_set_nonblock (srv_fd, 1); + hcl_sys_set_nonblock (srv_fd, 1); /* the listening socket is non-blocking unlike accepted sockets */ hcl_sys_set_cloexec (srv_fd, 1); if (bind(srv_fd, (struct sockaddr*)&srv_addr, srv_len) == -1)