From 4020a2401bf16788b14e5a860253f2351c33c89c Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 3 Aug 2022 13:56:20 +0000 Subject: [PATCH] some code cleanup --- lib/hcl-s.c | 147 ++++++++++++++++------------------------------------ lib/prim.c | 16 +++--- 2 files changed, 54 insertions(+), 109 deletions(-) diff --git a/lib/hcl-s.c b/lib/hcl-s.c index fc928a7..bc97019 100644 --- a/lib/hcl-s.c +++ b/lib/hcl-s.c @@ -315,29 +315,6 @@ static const hcl_bch_t* get_base_name (const hcl_bch_t* path) return (last == HCL_NULL)? path: (last + 1); } -static bb_t* bb_open (hcl_t* hcl, int fd, const char* fn) -{ - hcl_oow_t fnlen; - bb_t* bb; - - fnlen = hcl_count_bcstr(fn); - bb = (bb_t*)hcl_callocmem(hcl, HCL_SIZEOF(*bb) + (HCL_SIZEOF(hcl_bch_t) * (fnlen + 1))); - if (!bb) return HCL_NULL; - - /* copy ane empty string as a main stream's name */ - bb->fn = (hcl_bch_t*)(bb + 1); - hcl_copy_bcstr (bb->fn, fnlen + 1, fn); - bb->fd = fd; - - return bb; -/* - if (bb->fd <= -1) - { - hcl_seterrnum (hcl, HCL_EIOERR); - goto oops; - } -*/ -} static HCL_INLINE int open_input (hcl_t* hcl, hcl_ioinarg_t* arg) { @@ -350,7 +327,6 @@ static HCL_INLINE int open_input (hcl_t* hcl, hcl_ioinarg_t* arg) if (arg->includer) { /* includee */ -#if 1 /* TOOD: Do i need to skip prepending the include path if the included path is an absolute path? * it may be good for security if i don't skip it. we can lock the included files in a given directory */ hcl_oow_t ucslen, bcslen, parlen; @@ -407,22 +383,10 @@ static HCL_INLINE int open_input (hcl_t* hcl, hcl_ioinarg_t* arg) hcl_seterrnum (hcl, HCL_EIOERR); goto oops; } -#else - bb = bb_open(hcl, -1, ""); - if (!bb) goto oops; - - bb->fd = open(bb->fn, O_RDONLY, 0); - if (bb->fd <= -1) - { - hcl_seterrnum (hcl, HCL_EIOERR); - goto oops; - } -#endif } else { /* main stream */ -#if 1 hcl_oow_t pathlen = 0; bb = (bb_t*)hcl_callocmem(hcl, HCL_SIZEOF(*bb) + (HCL_SIZEOF(hcl_bch_t) * (pathlen + 1))); if (!bb) goto oops; @@ -432,10 +396,6 @@ static HCL_INLINE int open_input (hcl_t* hcl, hcl_ioinarg_t* arg) hcl_copy_bcstr (bb->fn, pathlen + 1, ""); bb->fd = xtn->proto->worker->sck; -#else - bb = bb_open(hcl, xtn->proto->worker->sck, ""); - if (!bb) goto oops; -#endif } HCL_ASSERT (hcl, bb->fd >= 0); @@ -482,11 +442,31 @@ static HCL_INLINE int read_input (hcl_t* hcl, hcl_ioinarg_t* arg) worker = xtn->proto->worker; start_over: - if (bb->fd == worker->sck) + if (arg->includer) { + /* includee */ + if (HCL_UNLIKELY(worker->server->stopreq)) + { + hcl_seterrbfmt (hcl, HCL_EGENERIC, "stop requested"); + return -1; + } + + x = read(bb->fd, &bb->buf[bb->len], HCL_COUNTOF(bb->buf) - bb->len); + if (x <= -1) + { + if (errno == EINTR) goto start_over; + hcl_seterrwithsyserr (hcl, 0, errno); + return -1; + } + + bb->len += x; + } + else + { + /* main stream */ hcl_server_t* server; - HCL_ASSERT (hcl, arg->includer == HCL_NULL); + HCL_ASSERT (hcl, bb->fd == worker->sck); server = worker->server; while (1) @@ -518,7 +498,7 @@ start_over: /* timed out - no activity on the pfd */ if (tmout > 0) { - hcl_seterrbfmt (hcl, HCL_EGENERIC, "no activity on the worker socket %d", worker->sck); + hcl_seterrbfmt (hcl, HCL_EGENERIC, "no activity on the worker socket %d", bb->fd); return -1; } } @@ -533,26 +513,6 @@ start_over: bb->len += x; } - else - { - HCL_ASSERT (hcl, arg->includer != HCL_NULL); - - if (HCL_UNLIKELY(worker->server->stopreq)) - { - hcl_seterrbfmt (hcl, HCL_EGENERIC, "stop requested"); - return -1; - } - - x = read(bb->fd, &bb->buf[bb->len], HCL_COUNTOF(bb->buf) - bb->len); - if (x <= -1) - { - if (errno == EINTR) goto start_over; - hcl_seterrwithsyserr (hcl, 0, errno); - return -1; - } - - bb->len += x; - } #if defined(HCL_OOCH_IS_UCH) bcslen = bb->len; @@ -1273,27 +1233,30 @@ static void send_error_message (hcl_server_proto_t* proto, const hcl_ooch_t* err } } +static void reformat_synerr (hcl_t* hcl) +{ + hcl_synerr_t synerr; + const hcl_ooch_t* orgmsg; + static hcl_ooch_t nullstr[] = { '\0' }; + + hcl_getsynerr (hcl, &synerr); + + orgmsg = hcl_backuperrmsg(hcl); + hcl_seterrbfmt ( + hcl, HCL_ESYNERR, + "%js%hs%.*js at %js%hsline %zu column %zu", + orgmsg, + (synerr.tgt.len > 0? " near ": ""), + synerr.tgt.len, synerr.tgt.val, + (synerr.loc.file? synerr.loc.file: nullstr), + (synerr.loc.file? " ": ""), + synerr.loc.line, synerr.loc.colm + ); +} + static void send_proto_hcl_error (hcl_server_proto_t* proto) { - hcl_errnum_t err; - - err = hcl_geterrnum(proto->hcl); - if (err == HCL_ESYNERR) - { - const hcl_ooch_t* bem; - hcl_synerr_t synerr; - static hcl_ooch_t nullstr[] = { '\0' }; - - /* concatenate the error message with the error location */ - hcl_getsynerr (proto->hcl, &synerr); - bem = hcl_backuperrmsg(proto->hcl); - hcl_seterrbfmt (proto->hcl, HCL_ESYNERR, "%js (%js%hs%zu,%zu)", - bem, - (synerr.loc.file? synerr.loc.file: nullstr), - (synerr.loc.file? ":": ""), - synerr.loc.line, synerr.loc.colm); - } - + if (hcl_geterrnum(proto->hcl) == HCL_ESYNERR) reformat_synerr (proto->hcl); send_error_message (proto, hcl_geterrmsg(proto->hcl)); } @@ -1352,24 +1315,6 @@ static int kill_server_worker (hcl_server_proto_t* proto, hcl_oow_t wid) return xret; } -static void reformat_synerr (hcl_t* hcl) -{ - hcl_synerr_t synerr; - const hcl_ooch_t* orgmsg; - - hcl_getsynerr (hcl, &synerr); - - orgmsg = hcl_backuperrmsg(hcl); - hcl_seterrbfmt ( - hcl, HCL_ESYNERR, - "%js%s%.*js at line %zu column %zu", - orgmsg, - (synerr.tgt.len > 0? " near ": ""), - synerr.tgt.len, synerr.tgt.val, - synerr.loc.line, synerr.loc.colm - ); -} - int hcl_server_proto_handle_request (hcl_server_proto_t* proto) { if (get_token(proto) <= -1) goto fail_with_errmsg; @@ -1583,7 +1528,7 @@ int hcl_server_proto_handle_request (hcl_server_proto_t* proto) } default: - hcl_seterrbfmt (proto->hcl, HCL_EINVAL, "Unknown token %.*js of type %d", proto->tok.len, proto->tok.ptr, (int)proto->tok.type); + hcl_seterrbfmt (proto->hcl, HCL_EINVAL, "Unknown token %.*js", proto->tok.len, proto->tok.ptr); goto fail_with_errmsg; } diff --git a/lib/prim.c b/lib/prim.c index d134d38..33655cb 100644 --- a/lib/prim.c +++ b/lib/prim.c @@ -957,16 +957,16 @@ static pf_t builtin_prims[] = { 1, 1, pf_object_new, 10, { 'o','b','j','e','c','t','-','n','e','w' } }, { 0, 0, hcl_pf_process_current, 15, { 'c','u','r','r','e','n','t','-','p','r','o','c','e','s','s'} }, - { 1, HCL_TYPE_MAX(hcl_oow_t), hcl_pf_process_fork, 4, { 'f','o','r','k'} }, - { 1, 1, hcl_pf_process_resume, 6, { 'r','e','s','u','m','e' } }, - { 0, 1, hcl_pf_process_suspend, 7, { 's','u','s','p','e','n','d' } }, - { 0, 1, hcl_pf_process_terminate, 9, { 't','e','r','m','i','n','a','t','e' } }, - { 0, 0, hcl_pf_process_terminate_all, 13, { 't','e','r','m','i','n','a','t','e','-','a','l','l' } }, - { 0, 0, hcl_pf_process_yield, 5, { 'y','i','e','l','d'} }, + { 1, HCL_TYPE_MAX(hcl_oow_t), hcl_pf_process_fork, 4, { 'f','o','r','k'} }, + { 1, 1, hcl_pf_process_resume, 6, { 'r','e','s','u','m','e' } }, + { 0, 1, hcl_pf_process_suspend, 7, { 's','u','s','p','e','n','d' } }, + { 0, 1, hcl_pf_process_terminate, 9, { 't','e','r','m','i','n','a','t','e' } }, + { 0, 0, hcl_pf_process_terminate_all, 13, { 't','e','r','m','i','n','a','t','e','-','a','l','l' } }, + { 0, 0, hcl_pf_process_yield, 5, { 'y','i','e','l','d'} }, - { 0, 0, hcl_pf_semaphore_new, 7, { 's','e','m','-','n','e','w'} }, - { 1, 1, hcl_pf_semaphore_wait, 8, { 's','e','m','-','w','a','i','t'} }, + { 0, 0, hcl_pf_semaphore_new, 7, { 's','e','m','-','n','e','w'} }, + { 1, 1, hcl_pf_semaphore_wait, 8, { 's','e','m','-','w','a','i','t'} }, { 1, 3, hcl_pf_semaphore_signal, 10, { 's','e','m','-','s','i','g','n','a','l'} }, { 2, 2, hcl_pf_semaphore_signal_on_input, 19, { 's','e','m','-','s','i','g','n','a','l','-','o','n','-','i','n','p','u','t'} }, { 2, 2, hcl_pf_semaphore_signal_on_output, 20, { 's','e','m','-','s','i','g','n','a','l','-','o','n','-','o','u','t','p','u','t'} },