From 97133f8c120718ecd8e943082beefe0b36db7fe6 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 11 Jan 2023 23:59:41 +0900 Subject: [PATCH] fixed quite some segmentation faults in http-thr.c. similar fixes must be done on other task implementations such as http-file.c --- bin/t01.c | 2 +- bin/t06.c | 10 +- bin/webs.c | 16 +- lib/chr.c | 40 +-- lib/dhcp-svr.c | 14 +- lib/dns-cli.c | 38 +-- lib/dns.c | 38 +-- lib/ecs-imp.h | 12 +- lib/err.c | 14 +- lib/fcgi-cli.c | 146 +++++++-- lib/fmt-imp.h | 46 +-- lib/fmt.c | 162 +++++----- lib/hio-chr.h | 2 +- lib/hio-cmn.h | 70 ++--- lib/hio-dhcp.h | 2 +- lib/hio-dns.h | 20 +- lib/hio-ecs.h | 40 +-- lib/hio-fmt.h | 90 +++--- lib/hio-htb.h | 126 ++++---- lib/hio-htrd.h | 14 +- lib/hio-htre.h | 26 +- lib/hio-http.h | 55 +++- lib/hio-json.h | 16 +- lib/hio-nwif.h | 2 +- lib/hio-opt.h | 22 +- lib/hio-pro.h | 2 +- lib/hio-prv.h | 8 +- lib/hio-pty.h | 2 +- lib/hio-rad.h | 56 ++-- lib/hio-sck.h | 8 +- lib/hio-shw.h | 2 +- lib/hio-skad.h | 10 +- lib/hio-str.h | 56 ++-- lib/hio-utl.h | 32 +- lib/hio.c | 160 +++++----- lib/hio.h | 42 +-- lib/htb.c | 84 ++--- lib/htrd.c | 228 +++++++------- lib/htre.c | 28 +- lib/http-cgi.c | 57 ++-- lib/http-fcgi.c | 58 ++-- lib/http-file.c | 41 +-- lib/http-prv.h | 1 + lib/http-svr.c | 108 ++++--- lib/http-thr.c | 664 ++++++++++++++++++++------------------- lib/http-txt.c | 17 +- lib/http.c | 23 +- lib/json.c | 48 +-- lib/mar-cli.c | 30 +- lib/mar.c | 44 +-- lib/md5.c | 16 +- lib/nwif.c | 50 +-- lib/opt-imp.h | 30 +- lib/path.c | 98 +++--- lib/pipe.c | 16 +- lib/pro.c | 44 +-- lib/pty.c | 18 +- lib/rad-msg.c | 108 +++---- lib/sck.c | 123 ++++---- lib/shw.c | 8 +- lib/skad.c | 132 ++++---- lib/sys-ass.c | 8 +- lib/sys-err.c | 14 +- lib/sys-log.c | 18 +- lib/sys-mux.c | 40 +-- lib/sys-prv.h | 2 +- lib/sys.c | 2 +- lib/tar.c | 14 +- lib/thr.c | 52 ++-- lib/tmr.c | 4 +- lib/uch-case.h | 2 +- lib/utf8.c | 28 +- lib/utl-mime.c | 4 +- lib/utl-siph.c | 10 +- lib/utl-str.c | 806 ++++++++++++++++++++++++------------------------ lib/utl.c | 60 ++-- 76 files changed, 2372 insertions(+), 2167 deletions(-) diff --git a/bin/t01.c b/bin/t01.c index 45e8ca6..ae3c2cc 100644 --- a/bin/t01.c +++ b/bin/t01.c @@ -848,7 +848,7 @@ static int print_qparam (hio_bcs_t* key, hio_bcs_t* val, void* ctx) return 0; } -static void on_htts_thr_request (hio_t* hio, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx) +static void on_htts_thr_request (hio_svc_htts_t* hio, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx) { FILE* fp; int i; diff --git a/bin/t06.c b/bin/t06.c index d012cbf..6afb6cb 100644 --- a/bin/t06.c +++ b/bin/t06.c @@ -27,7 +27,7 @@ static int print_qparam (hio_bcs_t* key, hio_bcs_t* val, void* ctx) return 0; } -static void on_htts_thr_request (hio_t* hio, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx) +static void on_htts_thr_request (hio_svc_htts_t* htts, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx) { FILE* fp; int i; @@ -62,7 +62,7 @@ static void on_htts_thr_request (hio_t* hio, hio_dev_thr_iopair_t* iop, hio_svc_ fclose (fp); } -static void on_htts_thr2_request (hio_t* hio, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx) +static void on_htts_thr2_request (hio_svc_htts_t* htts, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx) { FILE* fp, * sf; @@ -113,7 +113,7 @@ int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* { hio_t* hio = hio_svc_htts_gethio(htts); // hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck); - hio_http_method_t mth; +// hio_http_method_t mth; /* percent-decode the query path to the original buffer * since i'm not going to need it in the original form @@ -149,7 +149,7 @@ if (hio_htre_getcontentlen(req) > 0) } #endif - mth = hio_htre_getqmethodtype(req); +// mth = hio_htre_getqmethodtype(req); /* determine what to do once the header fields are all received. * i don't want to delay this until the contents are received. * if you don't like this behavior, you must implement your own @@ -208,7 +208,7 @@ if (hio_htre_getcontentlen(req) > 0) { hio_skad_t fcgis_addr; hio_bcstrtoskad(hio, "127.0.0.1:9000", &fcgis_addr); - x = hio_svc_htts_dofcgi(htts, csck, req, &fcgis_addr, 0); + x = hio_svc_htts_dofcgi(htts, csck, req, &fcgis_addr, "", qpath + 5, 0); } else x = hio_svc_htts_dofile(htts, csck, req, "", qpath, "text/plain", 0, HIO_NULL); diff --git a/bin/webs.c b/bin/webs.c index df38753..b11567a 100644 --- a/bin/webs.c +++ b/bin/webs.c @@ -51,15 +51,17 @@ static void untar_write_status_code (int fd, int code) } } -static void untar (hio_t* hio, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx) +static void untar (hio_svc_htts_t* htts, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx) { FILE* wfp = HIO_NULL; + hio_t* hio; htts_ext_t* ext; hio_tar_t* tar = HIO_NULL; hio_uint8_t buf[4096]; ssize_t n; - ext = hio_svc_htts_getxtn(tfi->htts); + hio = hio_svc_htts_gethio(htts); + ext = hio_svc_htts_getxtn(htts); /* TODO: error handling on write() failure */ wfp = fdopen(iop->wfd, "w"); @@ -317,11 +319,19 @@ static int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_ /* don't care about the path for now. TODO: make this secure and reasonable */ if (hio_svc_htts_dothr(htts, csck, req, untar, HIO_NULL, 0) <= -1) goto oops; } + else if (mth == HIO_HTTP_OPTIONS) + { + //if (hio_svc_htts_dofun(htts, csck, req, options, HIO_NULL, 0) <= -1) goto oops; + } else if (hio_comp_bcstr(qpath_ext, ".php", 0) == 0) { hio_skad_t skad; hio_bcstrtoskad(hio, "10.30.0.133:9000", &skad); - if (hio_svc_htts_dofcgi(htts, csck, req, &skad, 0) <= -1) goto oops; + /*if (hio_svc_htts_dofcgi(htts, csck, req, &skad, ext->ai->docroot, qpath, 0) <= -1) goto oops;*/ + /* if the document root is relative, it is hard to gurantee that the same document is + * true to the fcgi server which is a different process. so map it a blank string for now. + * TODO: accept a separate document root for the fcgi server and use it below */ + if (hio_svc_htts_dofcgi(htts, csck, req, &skad, "", qpath, 0) <= -1) goto oops; /*if (hio_svc_htts_dotxt(htts, csck, req, HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR, "text/plain", "what the...", 0) <= -1) goto oops;*/ } else // if (mth == HIO_HTTP_GET || mth == HIO_HTTP_POST) diff --git a/lib/chr.c b/lib/chr.c index e4915d9..01ae0ad 100644 --- a/lib/chr.c +++ b/lib/chr.c @@ -108,7 +108,7 @@ int hio_is_uch_blank (hio_uch_t c) hio_uch_t hio_to_uch_upper (hio_uch_t c) { hio_uchu_t uc = (hio_uchu_t)c; - if (uc >= 0 && uc <= UCH_CASE_MAX) + if (uc >= 0 && uc <= UCH_CASE_MAX) { uch_case_page_t* page; page = uch_case_map[UCH_CASE_MAP_INDEX(uc)]; @@ -120,7 +120,7 @@ hio_uch_t hio_to_uch_upper (hio_uch_t c) hio_uch_t hio_to_uch_lower (hio_uch_t c) { hio_uchu_t uc = (hio_uchu_t)c; - if (uc >= 0 && uc <= UCH_CASE_MAX) + if (uc >= 0 && uc <= UCH_CASE_MAX) { uch_case_page_t* page; page = uch_case_map[UCH_CASE_MAP_INDEX(uc)]; @@ -219,17 +219,17 @@ int hio_ucstr_to_uch_prop (const hio_uch_t* name, hio_uch_prop_t* id) kwp = &prop_tab[mid]; n = hio_comp_ucstr_bcstr(name, kwp->name, 0); - if (n > 0) + if (n > 0) { /* if left, right, mid were of hio_oow_t, - * you would need the following line. + * you would need the following line. if (mid == 0) break; */ right = mid - 1; } else if (n < 0) left = mid + 1; else - { + { *id = kwp->class; return 0; } @@ -251,17 +251,17 @@ int hio_uchars_to_uch_prop (const hio_uch_t* name, hio_oow_t len, hio_uch_prop_t kwp = &prop_tab[mid]; n = hio_comp_uchars_bcstr(name, len, kwp->name, 0); - if (n < 0) + if (n < 0) { /* if left, right, mid were of hio_oow_t, - * you would need the following line. + * you would need the following line. if (mid == 0) break; */ right = mid - 1; } else if (n > 0) left = mid + 1; - else - { + else + { *id = kwp->class; return 0; } @@ -285,17 +285,17 @@ int hio_bcstr_to_bch_prop (const hio_bch_t* name, hio_bch_prop_t* id) kwp = &prop_tab[mid]; n = hio_comp_bcstr(name, kwp->name, 0); - if (n > 0) + if (n > 0) { /* if left, right, mid were of hio_oow_t, - * you would need the following line. + * you would need the following line. if (mid == 0) break; */ right = mid - 1; } else if (n < 0) left = mid + 1; else - { + { *id = kwp->class; return 0; } @@ -317,17 +317,17 @@ int hio_bchars_to_bch_prop (const hio_bch_t* name, hio_oow_t len, hio_bch_prop_t kwp = &prop_tab[mid]; n = hio_comp_bchars_bcstr(name, len, kwp->name, 0); - if (n < 0) + if (n < 0) { /* if left, right, mid were of hio_oow_t, - * you would need the following line. + * you would need the following line. if (mid == 0) break; */ right = mid - 1; } else if (n > 0) left = mid + 1; else - { + { *id = kwp->class; return 0; } @@ -339,16 +339,16 @@ int hio_bchars_to_bch_prop (const hio_bch_t* name, hio_oow_t len, hio_bch_prop_t /* ----------------------------------------------------------------------- */ /* - * See http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c + * See http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c */ -struct interval +struct interval { int first; int last; }; /* auxiliary function for binary search in interval table */ -static int bisearch(hio_uch_t ucs, const struct interval *table, int max) +static int bisearch(hio_uch_t ucs, const struct interval *table, int max) { int min = 0; int mid; @@ -473,7 +473,7 @@ int hio_get_ucwidth (hio_uch_t uc) (uc >= 0xff00 && uc <= 0xff60) || /* Fullwidth Forms */ (uc >= 0xffe0 && uc <= 0xffe6) #if (HIO_SIZEOF_UCH_T > 2) - || + || (uc >= 0x20000 && uc <= 0x2fffd) || (uc >= 0x30000 && uc <= 0x3fffd) #endif @@ -483,5 +483,5 @@ int hio_get_ucwidth (hio_uch_t uc) } } - return 1; + return 1; } diff --git a/lib/dhcp-svr.c b/lib/dhcp-svr.c index 9f3210f..4ae1352 100644 --- a/lib/dhcp-svr.c +++ b/lib/dhcp-svr.c @@ -34,11 +34,11 @@ struct hio_svc_dhcs_t }; -/* +/* binding address link address for relay. lo ip address of lo unspec -lo/::1, [::1]:547 -lo/[::1]:544 [::1]:544 +lo/::1, [::1]:547 +lo/[::1]:544 [::1]:544 lo/[::1]:544/xxx [::1]:544 xxx join multicast group?? @@ -60,7 +60,7 @@ static hio_dev_sck_t* open_socket (hio_t* hio, hio_svc_dhcs_cfg_t* cfg) int f; f = hio_skad_get_family(&cfg->bind_addr); - if (f != HIO_AF_INET && f != HIO_AF_INET6) + if (f != HIO_AF_INET && f != HIO_AF_INET6) { hio_seterrbfmt (hio, HIO_EINVAL, "invalid bind address family"); goto oops; @@ -69,7 +69,7 @@ static hio_dev_sck_t* open_socket (hio_t* hio, hio_svc_dhcs_cfg_t* cfg) HIO_MEMSET (&m, 0, HIO_SIZEOF(m)); m.type = (f == HIO_AF_INET? HIO_DEV_SCK_UDP4: HIO_DEV_SCK_UDP6); //m.options = HIO_DEV_SCK_MAKE_LENIENT; - //m.on_write = + //m.on_write = //m.on_read = ... //m.on_connect = ... //m.on_disconnect = ... @@ -142,8 +142,8 @@ oops: if (dhcs) { if (sck) hio_dev_sck_kill (sck); -/*TODO: - for (i = 0; i < local_nbinds; i++) +/*TODO: + for (i = 0; i < local_nbinds; i++) { if (dhcs->sck[i]) hio_dev_sck_kill(dhcs->sck[i]) }*/ diff --git a/lib/dns-cli.c b/lib/dns-cli.c index 206f891..3254306 100644 --- a/lib/dns-cli.c +++ b/lib/dns-cli.c @@ -44,7 +44,7 @@ struct hio_svc_dnc_t hio_dev_sck_t* tcp_sck; hio_skad_t serv_addr; - hio_ntime_t send_tmout; + hio_ntime_t send_tmout; hio_ntime_t reply_tmout; /* default reply timeout */ /* For a question sent out, it may wait for a corresponding answer. @@ -52,8 +52,8 @@ struct hio_svc_dnc_t * to this value over udp and to 1 over tcp. if max_tries is 0, * it sends out the question but never waits for a response. * For a non-question message sent out, it never waits for a response - * regardless of max_tries. */ - hio_oow_t max_tries; + * regardless of max_tries. */ + hio_oow_t max_tries; hio_dns_cookie_t cookie; @@ -139,7 +139,7 @@ static hio_dns_msg_t* make_dns_msg (hio_svc_dnc_t* dnc, hio_dns_bhdr_t* bdns, hi msgxtn->on_done = on_done; msgxtn->wtmout = dnc->send_tmout; msgxtn->rtmout = dnc->reply_tmout; - msgxtn->rmaxtries = dnc->max_tries; + msgxtn->rmaxtries = dnc->max_tries; msgxtn->rtries = 0; msgxtn->servaddr = dnc->serv_addr; @@ -178,7 +178,7 @@ static int handle_tcp_packet (hio_dev_sck_t* dev, hio_dns_pkt_t* pkt, hio_uint16 hio_uint16_t id; hio_dns_msg_t* reqmsg; - if (!pkt->qr) + if (!pkt->qr) { HIO_DEBUG0 (hio, "DNC - dropping dns request received over tcp ...\n"); /* TODO: add source info */ return 0; /* drop request. nothing to do */ @@ -277,7 +277,7 @@ static int on_tcp_read (hio_dev_sck_t* dev, const void* data, hio_iolen_t dlen, } else { - if (HIO_LIKELY(rem >= 2)) + if (HIO_LIKELY(rem >= 2)) { pktlen = ((hio_uint16_t)*(hio_uint8_t*)dptr << 8) | *((hio_uint8_t*)dptr + 1); dptr += 2; rem -= 2; @@ -408,7 +408,7 @@ static void on_tcp_connect (hio_dev_sck_t* dev) dnc_dns_msg_xtn_t* reqmsgxtn = dnc_dns_msg_getxtn(reqmsg); hio_dns_msg_t* nextreqmsg = reqmsgxtn->next; - if (reqmsgxtn->dev == dev && reqmsgxtn->rtries == 0) + if (reqmsgxtn->dev == dev && reqmsgxtn->rtries == 0) { if (write_dns_msg_over_tcp(dev, reqmsg) <= -1) { @@ -455,7 +455,7 @@ static void on_tcp_disconnect (hio_dev_sck_t* dev) } /* let's forget about the tcp socket */ - dnc->tcp_sck = HIO_NULL; + dnc->tcp_sck = HIO_NULL; } static int switch_reqmsg_transport_to_tcp (hio_svc_dnc_t* dnc, hio_dns_msg_t* reqmsg) @@ -501,14 +501,14 @@ static int switch_reqmsg_transport_to_tcp (hio_svc_dnc_t* dnc, hio_dns_msg_t* re cinfo.remoteaddr = reqmsgxtn->servaddr; cinfo.connect_tmout = reqmsgxtn->rtmout; /* TOOD: create a separate connect timeout or treate rtmout as a whole transaction time and calculate the remaining time from the transaction start, and use it */ - if (hio_dev_sck_connect(dnc->tcp_sck, &cinfo) <= -1) + if (hio_dev_sck_connect(dnc->tcp_sck, &cinfo) <= -1) { hio_dev_sck_kill (dnc->tcp_sck); dnc->tcp_sck = HIO_NULL; return -1; /* the connect request hasn't been honored. */ } } - + /* switch the belonging device to the tcp socket since the connect request has been acknowledged. */ HIO_ASSERT (hio, reqmsgxtn->rtmridx == HIO_TMRIDX_INVALID); /* ensure no timer job scheduled at this moment */ reqmsgxtn->dev = dnc->tcp_sck; @@ -546,13 +546,13 @@ static int on_udp_read (hio_dev_sck_t* dev, const void* data, hio_iolen_t dlen, return 0; } - if (HIO_UNLIKELY(dlen < HIO_SIZEOF(*pkt))) + if (HIO_UNLIKELY(dlen < HIO_SIZEOF(*pkt))) { HIO_DEBUG0 (hio, "DNC - dns packet too small from ....\n"); /* TODO: add source packet */ return 0; /* drop */ } pkt = (hio_dns_pkt_t*)data; - if (!pkt->qr) + if (!pkt->qr) { HIO_DEBUG0 (hio, "DNC - dropping dns request received ...\n"); /* TODO: add source info */ return 0; /* drop request */ @@ -898,7 +898,7 @@ static void on_dnc_resolve (hio_svc_dnc_t* dnc, hio_dns_msg_t* reqmsg, hio_errnu goto done; } - if (pi->hdr.rcode != HIO_DNS_RCODE_NOERROR) + if (pi->hdr.rcode != HIO_DNS_RCODE_NOERROR) { status = HIO_EINVAL; goto no_data; @@ -928,7 +928,7 @@ static void on_dnc_resolve (hio_svc_dnc_t* dnc, hio_dns_msg_t* reqmsg, hio_errnu * the query type i stored in the extension space. */ switch (resolxtn->qtype) { - case HIO_DNS_RRT_Q_ANY: + case HIO_DNS_RRT_Q_ANY: case HIO_DNS_RRT_Q_AFXR: /* AFXR doesn't make sense in the brief mode. just treat it like ANY */ /* no A or AAAA found. so give the first entry in the answer */ goto match_found; @@ -1003,7 +1003,7 @@ hio_dns_msg_t* hio_svc_dnc_resolve (hio_svc_dnc_t* dnc, const hio_bch_t* qname, beopt_cookie.code = HIO_DNS_EOPT_COOKIE; beopt_cookie.dptr = &dnc->cookie.data; - beopt_cookie.dlen = HIO_DNS_COOKIE_CLIENT_LEN; + beopt_cookie.dlen = HIO_DNS_COOKIE_CLIENT_LEN; if (dnc->cookie.server_len > 0) beopt_cookie.dlen += dnc->cookie.server_len; /* compute the client cookie */ @@ -1031,9 +1031,9 @@ hio_dns_msg_t* hio_svc_dnc_resolve (hio_svc_dnc_t* dnc, const hio_bch_t* qname, * * ASSUMPTIONS: * the eopt entries are at the back of the packet. - * only 1 eopt entry(HIO_DNS_EOPT_COOKIE) has been added. - * - * manipulate the data length of the EDNS0 RR and COOKIE option + * only 1 eopt entry(HIO_DNS_EOPT_COOKIE) has been added. + * + * manipulate the data length of the EDNS0 RR and COOKIE option * as if the server cookie data has not been added. */ hio_dns_rrtr_t* edns_rrtr; @@ -1100,7 +1100,7 @@ int hio_svc_dnc_checkclientcookie (hio_svc_dnc_t* dnc, hio_dns_msg_t* reqmsg, hi /* TODO: afxr client ... */ -/* TODO: trace function to do its own recursive resolution?... +/* TODO: trace function to do its own recursive resolution?... hio_dns_msg_t* hio_svc_dnc_trace (hio_svc_dnc_t* dnc, const hio_bch_t* qname, hio_dns_rrt_t qtype, int resolve_flags, hio_svc_dnc_on_resolve_t on_resolve, hio_oow_t xtnsize) { } diff --git a/lib/dns.c b/lib/dns.c index a037c67..eaf51bf 100644 --- a/lib/dns.c +++ b/lib/dns.c @@ -93,7 +93,7 @@ static hio_oow_t to_dn_capa (const hio_bch_t* str) while (!DN_AT_END(cur)); } - capa++; + capa++; /* the length includes the terminating zero. */ return capa; @@ -366,7 +366,7 @@ static int parse_answer_rr (hio_t* hio, hio_dns_rr_part_t rr_part, hio_oow_t pos HIO_MEMCPY (&mx->preference, pi->_ptr, 2); pi->_ptr += 2; - mx->preference = hio_ntoh16(mx->preference); + mx->preference = hio_ntoh16(mx->preference); mx->exchange = (hio_bch_t*)pi->_rrdptr; if (parse_domain_name(hio, pi) <= -1) goto oops; } @@ -423,7 +423,7 @@ static int parse_answer_rr (hio_t* hio, hio_dns_rr_part_t rr_part, hio_oow_t pos verbatim: pi->_ptr += dlen; pi->_rrdlen += dlen; - if (pi->_rrdptr) + if (pi->_rrdptr) { HIO_MEMCPY (pi->_rrdptr, rrtr + 1, dlen); /* copy actual data */ pi->_rrdptr += dlen; @@ -451,7 +451,7 @@ static int parse_answer_rr (hio_t* hio, hio_dns_rr_part_t rr_part, hio_oow_t pos brr[pos].dptr = xrrdptr2; /* this length may be different from the length in the header as transformation is performed on some RR data. * for a domain name, it's inclusive of the termining null. */ - brr[pos].dlen = pi->_rrdptr - xrrdptr2; + brr[pos].dlen = pi->_rrdptr - xrrdptr2; } return 0; @@ -470,10 +470,10 @@ hio_dns_pkt_info_t* hio_dns_make_pkt_info (hio_t* hio, const hio_dns_pkt_t* pkt, HIO_MEMSET (&pib, 0, HIO_SIZEOF(pib)); - /* pib is used as the initial workspace and also indicates that it's the first run. + /* pib is used as the initial workspace and also indicates that it's the first run. * at the second run, pii is set to a dynamically allocated memory block large enough * to hold actual data. */ - pii = &pib; + pii = &pib; redo: pii->_start = (hio_uint8_t*)pkt; @@ -484,8 +484,8 @@ redo: pii->hdr.qr = pkt->qr & 0x01; pii->hdr.opcode = pkt->opcode & 0x0F; pii->hdr.aa = pkt->aa & 0x01; - pii->hdr.tc = pkt->tc & 0x01; - pii->hdr.rd = pkt->rd & 0x01; + pii->hdr.tc = pkt->tc & 0x01; + pii->hdr.rd = pkt->rd & 0x01; pii->hdr.ra = pkt->ra & 0x01; pii->hdr.ad = pkt->ad & 0x01; pii->hdr.cd = pkt->cd & 0x01; @@ -526,10 +526,10 @@ redo: pii->rr.ns = (hio_dns_brr_t*)&pii->rr.an[pib.ancount]; pii->rr.ar = (hio_dns_brr_t*)&pii->rr.ns[pib.nscount]; pii->_rrdptr = (hio_uint8_t*)&pii->rr.ar[pib.arcount]; - - /* _rrdptr points to the beginning of memory where additional data will + + /* _rrdptr points to the beginning of memory where additional data will * be held for some RRs. _rrdlen is the length of total additional data. - * the additional data refers to the data that is pointed to by the + * the additional data refers to the data that is pointed to by the * breakdown RRs(hio_dns_bqr_t/hio_dns_brr_t) but is not stored in them. */ goto redo; @@ -570,7 +570,7 @@ static int encode_rrdata_in_dns_msg (hio_t* hio, const hio_dns_brr_t* rr, hio_ui case HIO_DNS_RRT_MF: case HIO_DNS_RRT_MG: case HIO_DNS_RRT_MR:*/ - case HIO_DNS_RRT_CNAME: + case HIO_DNS_RRT_CNAME: case HIO_DNS_RRT_NS: case HIO_DNS_RRT_PTR: /* just a normal domain name */ @@ -623,7 +623,7 @@ static int encode_rrdata_in_dns_msg (hio_t* hio, const hio_dns_brr_t* rr, hio_ui } break; } - + case HIO_DNS_RRT_SOA: { /* soa */ @@ -708,7 +708,7 @@ hio_dns_msg_t* hio_dns_make_msg (hio_t* hio, hio_dns_bhdr_t* bhdr, hio_dns_bqr_t for (i = 0; i < qr_count; i++) { dnlen = to_dn_capa(qr[i].qname); - if (HIO_UNLIKELY(dnlen <= 0)) + if (HIO_UNLIKELY(dnlen <= 0)) { hio_seterrnum (hio, HIO_EINVAL); return HIO_NULL; @@ -720,7 +720,7 @@ hio_dns_msg_t* hio_dns_make_msg (hio_t* hio, hio_dns_bhdr_t* bhdr, hio_dns_bqr_t { hio_uint16_t rrdata_len; dnlen = to_dn_capa(rr[i].rrname); - if (HIO_UNLIKELY(dnlen <= 0)) + if (HIO_UNLIKELY(dnlen <= 0)) { hio_seterrnum (hio, HIO_EINVAL); return HIO_NULL; @@ -750,7 +750,7 @@ hio_dns_msg_t* hio_dns_make_msg (hio_t* hio, hio_dns_bhdr_t* bhdr, hio_dns_bqr_t pktlen += edns_dlen; } - else + else { if (HIO_UNLIKELY(bhdr->rcode > 0x0F)) { @@ -796,7 +796,7 @@ hio_dns_msg_t* hio_dns_make_msg (hio_t* hio, hio_dns_bhdr_t* bhdr, hio_dns_bqr_t dnlen = to_dn(rr[i].rrname, dn); HIO_ASSERT (hio, dnlen > 0); - + rrtr = (hio_dns_rrtr_t*)(dn + dnlen); rrtr->rrtype = hio_hton16(rr[i].rrtype); rrtr->rrclass = hio_hton16(rr[i].rrclass); @@ -853,8 +853,8 @@ hio_dns_msg_t* hio_dns_make_msg (hio_t* hio, hio_dns_bhdr_t* bhdr, hio_dns_bqr_t pkt->qr = bhdr->qr & 0x01; pkt->opcode = bhdr->opcode & 0x0F; pkt->aa = bhdr->aa & 0x01; - pkt->tc = bhdr->tc & 0x01; - pkt->rd = bhdr->rd & 0x01; + pkt->tc = bhdr->tc & 0x01; + pkt->rd = bhdr->rd & 0x01; pkt->ra = bhdr->ra & 0x01; pkt->ad = bhdr->ad & 0x01; pkt->cd = bhdr->cd & 0x01; diff --git a/lib/ecs-imp.h b/lib/ecs-imp.h index 0ec35a4..96678e4 100644 --- a/lib/ecs-imp.h +++ b/lib/ecs-imp.h @@ -128,7 +128,7 @@ hio_oow_t FN(setcapa) (str_t* str, hio_oow_t capa) hio_oow_t FN(setlen) (str_t* str, hio_oow_t len) { if (len == str->val.len) return len; - if (len < str->val.len) + if (len < str->val.len) { str->val.len = len; str->val.ptr[len] = '\0'; @@ -191,7 +191,7 @@ hio_oow_t FN(ncpy) (str_t* str, const char_t* s, hio_oow_t len) /* if the current capacity is 0 and the string len to copy is 0 * we can't simply pass 'len' as the new capapcity. * ecs_setcapa() won't do anything the current capacity of 0 - * is the same as new capacity required. note that when str->capa + * is the same as new capacity required. note that when str->capa * is 0, str->val.ptr is HIO_NULL. However, this is copying operation. * Copying a zero-length string may indicate that str->val.ptr must * not be HIO_NULL. so I simply pass 1 as the new capacity */ @@ -213,11 +213,11 @@ hio_oow_t FN(cat) (str_t* str, const char_t* s) static int FN(resize_for_ncat) (str_t* str, hio_oow_t len) { - if (len > str->capa - str->val.len) + if (len > str->capa - str->val.len) { hio_oow_t ncapa, mincapa; - /* let the minimum capacity be as large as + /* let the minimum capacity be as large as * to fit in the new substring */ mincapa = str->val.len + len; @@ -266,7 +266,7 @@ hio_oow_t FN(ncat) (str_t* str, const char_t* s, hio_oow_t len) if (n <= -1) return (hio_oow_t)-1; if (n == 0) return str->val.len; - if (len > str->capa - str->val.len) + if (len > str->capa - str->val.len) { /* copy as many characters as the number of cells available. * if the capacity has been decreased, len is adjusted here */ @@ -358,7 +358,7 @@ hio_oow_t FN(amend) (str_t* str, hio_oow_t pos, hio_oow_t len, const char_t* rep HIO_MEMMOVE (&str->val.ptr[pos + repl_len], &str->val.ptr[pos + len], HIO_SIZEOF(*repl) * (old_ecs_len - (pos + len))); } - if (repl_len > 0) HIO_MEMMOVE (&str->val.ptr[pos], repl, HIO_SIZEOF(*repl) * repl_len); + if (repl_len > 0) HIO_MEMMOVE (&str->val.ptr[pos], repl, HIO_SIZEOF(*repl) * repl_len); return str->val.len; } diff --git a/lib/err.c b/lib/err.c index 158ccdd..8c721e6 100644 --- a/lib/err.c +++ b/lib/err.c @@ -71,7 +71,7 @@ static hio_ooch_t* errstr[] = }; -/* -------------------------------------------------------------------------- +/* -------------------------------------------------------------------------- * ERROR NUMBER TO STRING CONVERSION * -------------------------------------------------------------------------- */ const hio_ooch_t* hio_errnum_to_errstr (hio_errnum_t errnum) @@ -80,7 +80,7 @@ const hio_ooch_t* hio_errnum_to_errstr (hio_errnum_t errnum) return (errnum >= 0 && errnum < HIO_COUNTOF(errstr))? errstr[errnum]: e_unknown; } -/* -------------------------------------------------------------------------- +/* -------------------------------------------------------------------------- * ERROR NUMBER/MESSAGE HANDLING * -------------------------------------------------------------------------- */ const hio_ooch_t* hio_geterrstr (hio_t* hio) @@ -145,8 +145,8 @@ const hio_ooch_t* hio_backuperrmsg (hio_t* hio) void hio_seterrnum (hio_t* hio, hio_errnum_t errnum) { if (hio->_shuterr) return; - hio->errnum = errnum; - hio->errmsg.len = 0; + hio->errnum = errnum; + hio->errmsg.len = 0; } static int err_bcs (hio_fmtout_t* fmtout, const hio_bch_t* ptr, hio_oow_t len) @@ -297,12 +297,12 @@ void hio_seterrbfmtwithsyserr (hio_t* hio, int syserr_type, int syserr_code, con va_list ap; if (hio->_shuterr) return; - + /* if (hio->vmprim.syserrstrb) {*/ errnum = hio_sys_syserrstrb(hio, syserr_type, syserr_code, hio->errmsg.tmpbuf.bch, HIO_COUNTOF(hio->errmsg.tmpbuf.bch)); - + va_start (ap, fmt); hio_seterrbfmtv (hio, errnum, fmt, ap); va_end (ap); @@ -355,7 +355,7 @@ void hio_seterrufmtwithsyserr (hio_t* hio, int syserr_type, int syserr_code, con va_list ap; if (hio->_shuterr) return; - + /*if (hio->vmprim.syserrstrb) {*/ errnum = hio_sys_syserrstrb(hio, syserr_type, syserr_code, hio->errmsg.tmpbuf.bch, HIO_COUNTOF(hio->errmsg.tmpbuf.bch)); diff --git a/lib/fcgi-cli.c b/lib/fcgi-cli.c index b889ff8..b076986 100644 --- a/lib/fcgi-cli.c +++ b/lib/fcgi-cli.c @@ -46,6 +46,7 @@ struct hio_svc_fcgic_t struct hio_svc_fcgic_sess_t { + int active; hio_oow_t sid; hio_svc_fcgic_conn_t* conn; hio_svc_fcgic_on_read_t on_read; @@ -65,20 +66,29 @@ struct hio_svc_fcgic_conn_t hio_oow_t free; /* the index to the first free session slot */ } sess; + struct + { + enum + { + R_AWAITING_HEADER, + R_AWAITING_BODY + } state; + + hio_uint8_t type; + hio_uint16_t id; + hio_uint16_t content_len; + hio_uint8_t padding_len; + + hio_uint8_t buf[1024]; /* TODO: make it smaller... */ + hio_oow_t len; + } r; /* space to parse incoming reply header */ + hio_svc_fcgic_conn_t* next; }; struct fcgic_sck_xtn_t { hio_svc_fcgic_conn_t* conn; -#if 0 - struct - { - hio_uint8_t* ptr; - hio_oow_t len; - hio_oow_t capa; - } rbuf; /* used by tcp socket */ -#endif }; typedef struct fcgic_sck_xtn_t fcgic_sck_xtn_t; @@ -111,13 +121,14 @@ typedef struct fcgic_fcgi_msg_xtn_t fcgic_fcgi_msg_xtn_t; #endif static int make_connection_socket (hio_svc_fcgic_conn_t* conn); +static void release_session (hio_svc_fcgic_sess_t* sess); static void sck_on_disconnect (hio_dev_sck_t* sck) { fcgic_sck_xtn_t* sck_xtn = hio_dev_sck_getxtn(sck); hio_svc_fcgic_conn_t* conn = sck_xtn->conn; -printf ("DISCONNECT SOCKET .................. sck->%p conn->%p\n", sck, conn); +printf ("DISCONNECT SOCKET .................. sck->%p conn->%p\n", sck, conn); if (conn) { @@ -130,6 +141,18 @@ printf ("DISCONNECT SOCKET .................. sck->%p conn->%p\n", sck, conn); make_connection_socket(conn); /* don't care about failure for now */ } #else + hio_oow_t i; + for (i = 0; i < conn->sess.capa; i++) + { + hio_svc_fcgic_sess_t* sess; + sess = &conn->sess.ptr[i + 1]; + if (sess->active) + { + /* TODO: release the session???*/ + release_session (sess); /* TODO: is this correct?? */ + /* or do we fire a callback??? */ + } + } conn->dev = HIO_NULL; #endif } @@ -137,7 +160,14 @@ printf ("DISCONNECT SOCKET .................. sck->%p conn->%p\n", sck, conn); static void sck_on_connect (hio_dev_sck_t* sck) { - printf ("CONNECTED >>>>>>>>>>>>>>>>>>>>>>>>>>\n"); + fcgic_sck_xtn_t* sck_xtn = hio_dev_sck_getxtn(sck); + hio_svc_fcgic_conn_t* conn = sck_xtn->conn; + +printf ("CONNECTED >>>>>>>>>>>>>>>>>>>>>>>>>>\n"); + + /* reinitialize the input parsing information */ + HIO_MEMSET (&conn->r, 0, HIO_SIZEOF(conn->r)); + conn->r.state = R_AWAITING_HEADER; } static int sck_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wrctx, const hio_skad_t* dstaddr) @@ -145,23 +175,91 @@ static int sck_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wrctx, con return 0; } -static int sck_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t len, const hio_skad_t* srcaddr) +static int sck_on_read (hio_dev_sck_t* sck, const void* data, hio_iolen_t dlen, const hio_skad_t* srcaddr) { fcgic_sck_xtn_t* sck_xtn = hio_dev_sck_getxtn(sck); hio_svc_fcgic_conn_t* conn = sck_xtn->conn; + hio_t* hio = conn->fcgic->hio; - if (len == 0) + if (dlen <= -1) { + /* error or timeout */ +/* fire all related fcgi sessions?? -> handled on disconnect */ + } + else if (dlen == 0) + { + /* EOF */ hio_dev_sck_halt (sck); +/* fire all related fcgi sessions?? -> handled on disconnect?? */ } else { - /* TODO: parse the reply ..*/ + do + { + if (conn->r.state == R_AWAITING_HEADER) + { + hio_fcgi_record_header_t* h; + hio_iolen_t reqlen, cplen; + HIO_ASSERT (hio, conn->r.len < HIO_SIZEOF(*h)); - /*sess = get_session(using the session id);*/ - /*sess->on_read (sess, ptr, len);*/ + reqlen = HIO_SIZEOF(*h) - conn->r.len; + cplen = (dlen > reqlen)? reqlen: dlen; + HIO_MEMCPY (&conn->r.buf[conn->r.len], data, cplen); + conn->r.len += cplen; + + data += cplen; + dlen -= cplen; + + if (conn->r.len < HIO_SIZEOF(*h)) + { + /* not enough data to complete a header*/ + HIO_ASSERT (hio, dlen == 0); + break; + } + + h = (hio_fcgi_record_header_t*)conn->r.buf; + conn->r.type = h->type; + conn->r.id = hio_ntoh16(h->id); + conn->r.content_len = hio_ntoh16(h->content_len); + conn->r.padding_len = hio_ntoh16(h->padding_len); + conn->r.len = 0; + conn->r.state == R_AWAITING_BODY; + } + else /* R_AWAITING_BODY */ + { + switch (conn->r.type) + { + case HIO_FCGI_END_REQUEST: + + case HIO_FCGI_STDOUT: + case HIO_FCGI_STDERR: + + default: + /* discard the record */ + goto done; + } + + + if (conn->r.id >= 1 && conn->r.id <= conn->sess.capa) + { + hio_svc_fcgic_sess_t* sess; + sess = &conn->sess.ptr[conn->r.id - 1]; + if (sess->active) + { + sess->on_read (sess, data, dlen); + /* TODO: return code check??? */ + } + } + else + { + /* invalid sid */ + /* TODO: logging or something*/ + } + } + } while (dlen > 0); } +done: return 0; } @@ -218,7 +316,7 @@ static int make_connection_socket (hio_svc_fcgic_conn_t* conn) return -1; } - printf ("MAKING CONNECTION %p %p\n", conn->dev, sck); +printf ("MAKING CONNECTION %p %p\n", conn->dev, sck); HIO_ASSERT (hio, conn->dev == HIO_NULL); conn->dev = sck; @@ -236,8 +334,9 @@ static hio_svc_fcgic_conn_t* get_connection (hio_svc_fcgic_t* fcgic, const hio_s if (hio_equal_skads(&conn->addr, fcgis_addr, 1)) { if (conn->sess.free != INVALID_SID || - conn->sess.capa <= (CONN_SESS_CAPA_MAX - CONN_SESS_INC)) + conn->sess.capa <= (CONN_SESS_CAPA_MAX - CONN_SESS_INC)) { + /* the connection has room for more sessions */ if (!conn->dev) make_connection_socket(conn); /* conn->dev will still be null if connection fails*/ return conn; } @@ -274,7 +373,7 @@ static void free_connections (hio_svc_fcgic_t* fcgic) while (conn) { next = conn->next; - if (conn->dev) + if (conn->dev) { struct fcgic_sck_xtn_t* sck_xtn; sck_xtn = hio_dev_sck_getxtn(conn->dev); @@ -307,6 +406,8 @@ static hio_svc_fcgic_sess_t* new_session (hio_svc_fcgic_t* fcgic, const hio_skad for (i = conn->sess.capa ; i < newcapa; i++) { + /* management records use 0 for requestId. + * but application records have a nonzero requestId. */ newptr[i].sid = i + 1; newptr[i].conn = conn; } @@ -322,6 +423,7 @@ static hio_svc_fcgic_sess_t* new_session (hio_svc_fcgic_t* fcgic, const hio_skad sess->sid = conn->sess.free; sess->on_read = on_read; + sess->active = 1; HIO_ASSERT (hio, sess->conn == conn); HIO_ASSERT (hio, sess->conn->fcgic == fcgic); @@ -330,6 +432,7 @@ static hio_svc_fcgic_sess_t* new_session (hio_svc_fcgic_t* fcgic, const hio_skad static void release_session (hio_svc_fcgic_sess_t* sess) { + sess->active = 0; sess->sid = sess->conn->sess.free; sess->conn->sess.free = sess->sid; } @@ -344,7 +447,7 @@ hio_svc_fcgic_t* hio_svc_fcgic_start (hio_t* hio, const hio_svc_fcgic_tmout_t* t fcgic->hio = hio; fcgic->svc_stop = (hio_svc_stop_t)hio_svc_fcgic_stop; - if (tmout) + if (tmout) { fcgic->tmout = *tmout; fcgic->tmout_set = 1; @@ -370,6 +473,8 @@ void hio_svc_fcgic_stop (hio_svc_fcgic_t* fcgic) HIO_SVCL_UNLINK_SVC (fcgic); hio_freemem (hio, fcgic); + + HIO_DEBUG1 (hio, "FCGIC - STOPPED SERVICE %p\n", fcgic); } hio_svc_fcgic_sess_t* hio_svc_fcgic_tie (hio_svc_fcgic_t* fcgic, const hio_skad_t* addr, hio_svc_fcgic_on_read_t on_read) @@ -429,6 +534,9 @@ int hio_svc_fcgic_writeparam (hio_svc_fcgic_sess_t* sess, const void* key, hio_i return -1; } +/* TODO: buffer key value pairs. flush on the end of param of buffer full. +* can merge multipl key values pairs in one FCGI_PARAMS packets.... +*/ HIO_MEMSET (&h, 0, HIO_SIZEOF(h)); h.version = HIO_FCGI_VERSION; h.type = HIO_FCGI_PARAMS; diff --git a/lib/fmt-imp.h b/lib/fmt-imp.h index 1ef5cad..e70ec76 100644 --- a/lib/fmt-imp.h +++ b/lib/fmt-imp.h @@ -23,7 +23,7 @@ */ static int fmt_uintmax ( - char_t* buf, int size, + char_t* buf, int size, hio_uintmax_t value, int base_and_flags, int prec, char_t fillchar, char_t signchar, const char_t* prefix) { @@ -39,10 +39,10 @@ static int fmt_uintmax ( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ": "0123456789abcdefghijklmnopqrstuvwxyz"; - if ((base_and_flags & HIO_FMT_INTMAX_NOZERO) && value == 0) + if ((base_and_flags & HIO_FMT_INTMAX_NOZERO) && value == 0) { - p = tmp; - if (base_and_flags & HIO_FMT_INTMAX_ZEROLEAD) + p = tmp; + if (base_and_flags & HIO_FMT_INTMAX_ZEROLEAD) { /* NOZERO emits no digit, ZEROLEAD emits 1 digit. * so it emits '0' */ @@ -61,7 +61,7 @@ static int fmt_uintmax ( hio_uintmax_t v = value; /* store the resulting numeric string into 'tmp' first */ - p = tmp; + p = tmp; do { *p++ = xbasestr[v % base]; @@ -71,11 +71,11 @@ static int fmt_uintmax ( /* reslen is the length of the resulting string without padding. */ reslen = (int)(p - tmp); - + /* precision specified the minum number of digits to produce. - * so if the precision is larger that the digits produced, + * so if the precision is larger that the digits produced, * reslen should be adjusted to precision */ - if (prec > reslen) + if (prec > reslen) { /* if the precision is greater than the actual digits * made from the value, 0 is inserted in front. @@ -84,12 +84,12 @@ static int fmt_uintmax ( preczero = prec - reslen; reslen = prec; } - else + else { preczero = 0; - if ((base_and_flags & HIO_FMT_INTMAX_ZEROLEAD) && value != 0) + if ((base_and_flags & HIO_FMT_INTMAX_ZEROLEAD) && value != 0) { - /* if value is zero, 0 is emitted from it. + /* if value is zero, 0 is emitted from it. * so ZEROLEAD don't need to add another 0. */ preczero++; reslen++; @@ -135,10 +135,10 @@ static int fmt_uintmax ( if (prefix) while (*prefix && bp < be) *bp++ = *prefix++; /* add 0s for precision */ - while (preczero > 0 && bp < be) - { + while (preczero > 0 && bp < be) + { *bp++ = '0'; - preczero--; + preczero--; } /* copy the numeric string to the destination buffer */ @@ -167,10 +167,10 @@ static int fmt_uintmax ( if (prefix) while (*prefix && bp < be) *bp++ = *prefix++; /* add 0s for precision */ - while (preczero > 0 && bp < be) - { + while (preczero > 0 && bp < be) + { *bp++ = '0'; - preczero--; + preczero--; } /* copy the numeric string to the destination buffer */ @@ -192,10 +192,10 @@ static int fmt_uintmax ( if (prefix) while (*prefix && bp < be) *bp++ = *prefix++; /* add 0s for precision */ - while (preczero > 0 && bp < be) - { + while (preczero > 0 && bp < be) + { *bp++ = '0'; - preczero--; + preczero--; } /* copy the numeric string to the destination buffer */ @@ -211,10 +211,10 @@ static int fmt_uintmax ( if (prefix) while (*prefix && bp < be) *bp++ = *prefix++; /* add 0s for precision */ - while (preczero > 0 && bp < be) - { + while (preczero > 0 && bp < be) + { *bp++ = '0'; - preczero--; + preczero--; } /* copy the numeric string to the destination buffer */ diff --git a/lib/fmt.c b/lib/fmt.c index 9cedc23..826d42f 100644 --- a/lib/fmt.c +++ b/lib/fmt.c @@ -23,7 +23,7 @@ */ /* - * This file contains a formatted output routine derived from kvprintf() + * This file contains a formatted output routine derived from kvprintf() * of FreeBSD. It has been heavily modified and bug-fixed. */ @@ -70,7 +70,7 @@ #include /* for snrintf(). used for floating-point number formatting */ #if defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && (__WATCOMC__ < 1200)) -# define snprintf _snprintf +# define snprintf _snprintf # if !defined(HAVE_SNPRINTF) # define HAVE_SNPRINTF # endif @@ -81,7 +81,7 @@ #endif -/* Max number conversion buffer length: +/* Max number conversion buffer length: * hio_intmax_t in base 2, plus NUL byte. */ #define MAXNBUF (HIO_SIZEOF(hio_intmax_t) * HIO_BITS_PER_BYTE + 1) @@ -106,7 +106,7 @@ static struct { hio_uint8_t flag; /* for single occurrence */ hio_uint8_t dflag; /* for double occurrence */ -} lm_tab[26] = +} lm_tab[26] = { { 0, 0 }, /* a */ { 0, 0 }, /* b */ @@ -137,7 +137,7 @@ static struct }; -enum +enum { FLAGC_DOT = (1 << 0), FLAGC_SPACE = (1 << 1), @@ -152,14 +152,14 @@ enum FLAGC_LENMOD = (1 << 10) /* length modifier */ }; -static const hio_bch_t hex2ascii_lower[] = +static const hio_bch_t hex2ascii_lower[] = { '0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z' }; -static const hio_bch_t hex2ascii_upper[] = +static const hio_bch_t hex2ascii_upper[] = { '0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F','G','H','I','J','K','L','M', @@ -311,10 +311,10 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) fmtptr = (const hio_uint8_t*)fmtout->fmt_str; switch (fmtout->fmt_type) { - case HIO_FMTOUT_FMT_TYPE_BCH: + case HIO_FMTOUT_FMT_TYPE_BCH: fmtchsz = HIO_SIZEOF_BCH_T; break; - case HIO_FMTOUT_FMT_TYPE_UCH: + case HIO_FMTOUT_FMT_TYPE_UCH: fmtchsz = HIO_SIZEOF_UCH_T; break; } @@ -347,9 +347,9 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) { const hio_bch_t* start, * end; start = end = (const hio_bch_t*)fmtptr; - while ((bch = *end++) != '%' || stop) + while ((bch = *end++) != '%' || stop) { - if (bch == '\0') + if (bch == '\0') { PUT_BCS (fmtout, start, end - start - 1); goto done; @@ -365,9 +365,9 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) { const hio_uch_t* start, * end; start = end = (const hio_uch_t*)fmtptr; - while ((uch = *end++) != '%' || stop) + while ((uch = *end++) != '%' || stop) { - if (uch == '\0') + if (uch == '\0') { PUT_UCS (fmtout, start, end - start - 1); goto done; @@ -380,15 +380,15 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) goto handle_percent; handle_percent: - padc = ' '; + padc = ' '; width = 0; precision = 0; neg = 0; sign = 0; - lm_flag = 0; lm_dflag = 0; flagc = 0; + lm_flag = 0; lm_dflag = 0; flagc = 0; sprintn = sprintn_lower; reswitch: switch (fmtout->fmt_type) { - case HIO_FMTOUT_FMT_TYPE_BCH: + case HIO_FMTOUT_FMT_TYPE_BCH: uch = *(const hio_bch_t*)fmtptr; break; case HIO_FMTOUT_FMT_TYPE_UCH: @@ -397,7 +397,7 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) } fmtptr += fmtchsz; - switch (uch) + switch (uch) { case '%': /* %% */ bch = uch; @@ -409,7 +409,7 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) flagc |= FLAGC_DOT; goto reswitch; - case '#': + case '#': if (flagc & (FLAGC_WIDTH | FLAGC_DOT | FLAGC_LENMOD)) goto invalid_format; flagc |= FLAGC_SHARP; goto reswitch; @@ -442,30 +442,30 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) goto reswitch; case '*': /* take the length from the parameter */ - if (flagc & FLAGC_DOT) + if (flagc & FLAGC_DOT) { if (flagc & (FLAGC_STAR2 | FLAGC_PRECISION)) goto invalid_format; flagc |= FLAGC_STAR2; precision = va_arg(ap, hio_ooi_t); /* this deviates from the standard printf that accepts 'int' */ - if (precision < 0) + if (precision < 0) { - /* if precision is less than 0, + /* if precision is less than 0, * treat it as if no .precision is specified */ flagc &= ~FLAGC_DOT; precision = 0; } - } - else + } + else { if (flagc & (FLAGC_STAR1 | FLAGC_WIDTH)) goto invalid_format; flagc |= FLAGC_STAR1; width = va_arg(ap, hio_ooi_t); /* it deviates from the standard printf that accepts 'int' */ - if (width < 0) + if (width < 0) { /* - if (flagc & FLAGC_LEFTADJ) + if (flagc & FLAGC_LEFTADJ) flagc &= ~FLAGC_LEFTADJ; else */ @@ -489,12 +489,12 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) case '5': case '6': case '7': case '8': case '9': { if (flagc & FLAGC_LENMOD) goto invalid_format; - for (n = 0;; fmtptr += fmtchsz) + for (n = 0;; fmtptr += fmtchsz) { n = n * 10 + uch - '0'; switch (fmtout->fmt_type) { - case HIO_FMTOUT_FMT_TYPE_BCH: + case HIO_FMTOUT_FMT_TYPE_BCH: uch = *(const hio_bch_t*)fmtptr; break; case HIO_FMTOUT_FMT_TYPE_UCH: @@ -503,13 +503,13 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) } if (uch < '0' || uch > '9') break; } - if (flagc & FLAGC_DOT) + if (flagc & FLAGC_DOT) { if (flagc & FLAGC_STAR2) goto invalid_format; precision = n; flagc |= FLAGC_PRECISION; } - else + else { if (flagc & FLAGC_STAR1) goto invalid_format; width = n; @@ -548,7 +548,7 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) goto invalid_format; } } - else + else { lm_flag |= lm_tab[uch - 'a'].flag; goto reswitch; @@ -556,10 +556,10 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) break; case 'L': /* long double */ - if (flagc & FLAGC_LENMOD) + if (flagc & FLAGC_LENMOD) { /* conflict with other length modifier */ - goto invalid_format; + goto invalid_format; } flagc |= FLAGC_LENMOD; lm_flag |= LF_LD; @@ -569,7 +569,7 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) if (flagc & FLAGC_LENMOD) { /* conflict with other length modifier */ - goto invalid_format; + goto invalid_format; } flagc |= FLAGC_LENMOD; lm_flag |= LF_QD; @@ -591,12 +591,12 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) *(va_arg(ap, short int*)) = fmtout->count; else if (lm_flag & LF_C) /* hh */ *(va_arg(ap, char*)) = fmtout->count; - else if (flagc & FLAGC_LENMOD) + else if (flagc & FLAGC_LENMOD) goto invalid_format; else *(va_arg(ap, int*)) = fmtout->count; break; - + /* signed integer conversions */ case 'd': case 'i': /* signed conversion */ @@ -606,7 +606,7 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) /* end of signed integer conversions */ /* unsigned integer conversions */ - case 'o': + case 'o': base = 8; goto handle_nosign; case 'u': @@ -634,7 +634,7 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) case 'c': { /* zeropad must not take effect for 'c' */ - if (flagc & FLAGC_ZEROPAD) padc = ' '; + if (flagc & FLAGC_ZEROPAD) padc = ' '; if (lm_flag & LF_L) goto uppercase_c; #if defined(HIO_OOCH_IS_UCH) if (lm_flag & LF_J) goto uppercase_c; @@ -742,12 +742,12 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) const hio_uint8_t* bsp; hio_oow_t k_hex_width; - /* zeropad must not take effect for 'k' and 'K' - * + /* zeropad must not take effect for 'k' and 'K' + * * 'h' & 'l' is not used to differentiate hio_bch_t and hio_uch_t - * because 'k' means hio_byte_t. - * 'l', results in uppercase hexadecimal letters. - * 'h' drops the leading \x in the output + * because 'k' means hio_byte_t. + * 'l', results in uppercase hexadecimal letters. + * 'h' drops the leading \x in the output * -------------------------------------------------------- * hk -> \x + non-printable in lowercase hex * k -> all in lowercase hex @@ -792,9 +792,9 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); - while (n--) + while (n--) { - if ((lm_flag & LF_H) && BYTE_PRINTABLE(*bsp)) + if ((lm_flag & LF_H) && BYTE_PRINTABLE(*bsp)) { PUT_BCH (fmtout, *bsp, 1); } @@ -816,7 +816,7 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) case 'W': { /* unicode string in unicode escape sequence. - * + * * hw -> \uXXXX, \UXXXXXXXX, printable-byte(only in ascii range) * w -> \uXXXX, \UXXXXXXXX * lw -> all in \UXXXXXXXX @@ -830,7 +830,7 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) if (flagc & FLAGC_DOT) { /* if precision is specifed, it doesn't stop at the value of zero unlike 's' or 'S' */ - for (n = 0; n < precision; n++) + for (n = 0; n < precision; n++) { if ((lm_flag & LF_H) && BYTE_PRINTABLE(usp[n])) uwid = 1; else if (!(lm_flag & LF_L) && usp[n] <= 0xFFFF) uwid = 6; @@ -851,13 +851,13 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) if (!(flagc & FLAGC_LEFTADJ) && width > 0) PUT_OOCH (fmtout, padc, width); - while (n--) + while (n--) { - if ((lm_flag & LF_H) && BYTE_PRINTABLE(*usp)) + if ((lm_flag & LF_H) && BYTE_PRINTABLE(*usp)) { PUT_OOCH(fmtout, *usp, 1); } - else if (!(lm_flag & LF_L) && *usp <= 0xFFFF) + else if (!(lm_flag & LF_L) && *usp <= 0xFFFF) { hio_uint16_t u16 = *usp; int extra_flags = ((uch) == 'w'? HIO_BYTE_TO_BCSTR_LOWERCASE: 0); @@ -934,7 +934,7 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) { /* hio_flt_t is limited to double or long double */ - /* precedence goes to double if sizeof(double) == sizeof(long double) + /* precedence goes to double if sizeof(double) == sizeof(long double) * for example, %Lf didn't work on some old platforms. * so i prefer the format specifier with no modifier. */ @@ -998,18 +998,18 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) if (flagc & FLAGC_ZEROPAD) fb.fmt.ptr[fmtlen++] = '0'; if (flagc & FLAGC_STAR1) fb.fmt.ptr[fmtlen++] = '*'; - else if (flagc & FLAGC_WIDTH) + else if (flagc & FLAGC_WIDTH) { fmtlen += hio_fmt_uintmax_to_bcstr( - &fb.fmt.ptr[fmtlen], fb.fmt.capa - fmtlen, + &fb.fmt.ptr[fmtlen], fb.fmt.capa - fmtlen, width, 10, -1, '\0', HIO_NULL); } if (flagc & FLAGC_DOT) fb.fmt.ptr[fmtlen++] = '.'; if (flagc & FLAGC_STAR2) fb.fmt.ptr[fmtlen++] = '*'; - else if (flagc & FLAGC_PRECISION) + else if (flagc & FLAGC_PRECISION) { fmtlen += hio_fmt_uintmax_to_bcstr( - &fb.fmt.ptr[fmtlen], fb.fmt.capa - fmtlen, + &fb.fmt.ptr[fmtlen], fb.fmt.capa - fmtlen, precision, 10, -1, '\0', HIO_NULL); } @@ -1026,7 +1026,7 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) #if defined(HAVE_SNPRINTF) /* nothing special here */ #else - /* best effort to avoid buffer overflow when no snprintf is available. + /* best effort to avoid buffer overflow when no snprintf is available. * i really can't do much if it happens. */ newcapa = precision + width + 32; if (fb.out.capa < newcapa) @@ -1176,14 +1176,14 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) num = va_arg(ap, int); number: - if (sign && (hio_intmax_t)num < 0) + if (sign && (hio_intmax_t)num < 0) { neg = 1; num = -(hio_intmax_t)num; } nbufp = sprintn(nbuf, num, base, &tmp); - if ((flagc & FLAGC_SHARP) && num != 0) + if ((flagc & FLAGC_SHARP) && num != 0) { if (base == 2 || base == 16) tmp += 2; else if (base == 8) tmp += 1; @@ -1193,7 +1193,7 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) else if (flagc & FLAGC_SPACE) tmp++; numlen = (int)((const hio_bch_t*)nbufp - (const hio_bch_t*)nbuf); - if ((flagc & FLAGC_DOT) && precision > numlen) + if ((flagc & FLAGC_DOT) && precision > numlen) { /* extra zeros for precision specified */ tmp += (precision - numlen); @@ -1209,18 +1209,18 @@ static int fmt_outv (hio_fmtout_t* fmtout, va_list ap) else if (flagc & FLAGC_SIGN) PUT_OOCH (fmtout, '+', 1); else if (flagc & FLAGC_SPACE) PUT_OOCH (fmtout, ' ', 1); - if ((flagc & FLAGC_SHARP) && num != 0) + if ((flagc & FLAGC_SHARP) && num != 0) { - if (base == 2) + if (base == 2) { PUT_OOCH (fmtout, '0', 1); PUT_OOCH (fmtout, 'b', 1); } - if (base == 8) + if (base == 8) { PUT_OOCH (fmtout, '0', 1); - } - else if (base == 16) + } + else if (base == 16) { PUT_OOCH (fmtout, '0', 1); PUT_OOCH (fmtout, 'x', 1); @@ -1376,7 +1376,7 @@ int hio_ufmt_out (hio_fmtout_t* fmtout, const hio_uch_t* fmt, ...) return n; } -/* -------------------------------------------------------------------------- +/* -------------------------------------------------------------------------- * FORMATTED LOG OUTPUT * -------------------------------------------------------------------------- */ @@ -1409,7 +1409,7 @@ redo: hio_ooch_t* tmp; max = HIO_TYPE_MAX(hio_oow_t) - hio->log.len; - if (len > max) + if (len > max) { /* data too big. */ rem += len - max; @@ -1429,7 +1429,7 @@ redo: /* +1 to handle line ending injection more easily */ tmp = hio_reallocmem(hio, hio->log.ptr, (newcapa + 1) * HIO_SIZEOF(*tmp)); - if (!tmp) + if (!tmp) { make_do: if (hio->log.len > 0) @@ -1525,21 +1525,21 @@ hio_ooi_t hio_logbfmtv (hio_t* hio, hio_bitmask_t mask, const hio_bch_t* fmt, va int x; hio_fmtout_t fo; - /* there may internal log calls even if this feature is disabled. + /* there may internal log calls even if this feature is disabled. * the explicit check is required */ - if (HIO_UNLIKELY(!(hio->_features & HIO_FEATURE_LOG))) return -1; + if (HIO_UNLIKELY(!(hio->_features & HIO_FEATURE_LOG))) return -1; - if (hio->log.default_type_mask & HIO_LOG_ALL_TYPES) + if (hio->log.default_type_mask & HIO_LOG_ALL_TYPES) { /* if a type is given, it's not untyped any more. * mask off the UNTYPED bit */ - mask &= ~HIO_LOG_UNTYPED; + mask &= ~HIO_LOG_UNTYPED; /* if the default_type_mask has the UNTYPED bit on, * it'll get turned back on */ mask |= (hio->log.default_type_mask & HIO_LOG_ALL_TYPES); } - else if (!(mask & HIO_LOG_ALL_TYPES)) + else if (!(mask & HIO_LOG_ALL_TYPES)) { /* no type is set in the given mask and no default type is set. * make it UNTYPED. */ @@ -1555,7 +1555,7 @@ hio_ooi_t hio_logbfmtv (hio_t* hio, hio_bitmask_t mask, const hio_bch_t* fmt, va fo.putbchars = log_bcs; fo.putuchars = log_ucs; - if (hio->option.log_mask & HIO_LOG_GUARDED) + if (hio->option.log_mask & HIO_LOG_GUARDED) { hio_sys_locklog (hio); x = fmt_outv(&fo, ap); @@ -1596,21 +1596,21 @@ hio_ooi_t hio_logufmtv (hio_t* hio, hio_bitmask_t mask, const hio_uch_t* fmt, va int x; hio_fmtout_t fo; - /* there may internal log calls even if this feature is disabled. + /* there may internal log calls even if this feature is disabled. * the explicit check is required */ if (HIO_UNLIKELY(!(hio->_features & HIO_FEATURE_LOG))) return -1; - if (hio->log.default_type_mask & HIO_LOG_ALL_TYPES) + if (hio->log.default_type_mask & HIO_LOG_ALL_TYPES) { /* if a type is given, it's not untyped any more. * mask off the UNTYPED bit */ - mask &= ~HIO_LOG_UNTYPED; + mask &= ~HIO_LOG_UNTYPED; /* if the default_type_mask has the UNTYPED bit on, * it'll get turned back on */ mask |= (hio->log.default_type_mask & HIO_LOG_ALL_TYPES); } - else if (!(mask & HIO_LOG_ALL_TYPES)) + else if (!(mask & HIO_LOG_ALL_TYPES)) { /* no type is set in the given mask and no default type is set. * make it UNTYPED. */ @@ -1626,7 +1626,7 @@ hio_ooi_t hio_logufmtv (hio_t* hio, hio_bitmask_t mask, const hio_uch_t* fmt, va fo.putbchars = log_bcs; fo.putuchars = log_ucs; - if (hio->option.log_mask & HIO_LOG_GUARDED) + if (hio->option.log_mask & HIO_LOG_GUARDED) { hio_sys_locklog (hio); x = fmt_outv(&fo, ap); @@ -1682,7 +1682,7 @@ hio_ooi_t hio_logufmt (hio_t* hio, hio_bitmask_t mask, const hio_uch_t* fmt, ... /* ------------------------------------------------------------------------------------- */ int hio_fmt_intmax_to_bcstr ( - hio_bch_t* buf, int size, + hio_bch_t* buf, int size, hio_intmax_t value, int base_and_flags, int prec, hio_bch_t fillchar, const hio_bch_t* prefix) { @@ -1714,7 +1714,7 @@ int hio_fmt_intmax_to_bcstr ( } int hio_fmt_uintmax_to_bcstr ( - hio_bch_t* buf, int size, + hio_bch_t* buf, int size, hio_uintmax_t value, int base_and_flags, int prec, hio_bch_t fillchar, const hio_bch_t* prefix) { @@ -1740,7 +1740,7 @@ int hio_fmt_uintmax_to_bcstr ( /* ------------------------------------------------------------------------------------- */ int hio_fmt_intmax_to_ucstr ( - hio_uch_t* buf, int size, + hio_uch_t* buf, int size, hio_intmax_t value, int base_and_flags, int prec, hio_uch_t fillchar, const hio_uch_t* prefix) { @@ -1772,7 +1772,7 @@ int hio_fmt_intmax_to_ucstr ( } int hio_fmt_uintmax_to_ucstr ( - hio_uch_t* buf, int size, + hio_uch_t* buf, int size, hio_uintmax_t value, int base_and_flags, int prec, hio_uch_t fillchar, const hio_uch_t* prefix) { diff --git a/lib/hio-chr.h b/lib/hio-chr.h index 11a563e..1750191 100644 --- a/lib/hio-chr.h +++ b/lib/hio-chr.h @@ -149,7 +149,7 @@ HIO_EXPORT int hio_is_bch_type (hio_bch_t c, hio_bch_prop_t type); # if __has_builtin(__builtin_tolower) # define hio_to_bch_lower __builtin_tolower # endif -#elif (__GNUC__ >= 14) +#elif (__GNUC__ >= 14) # define hio_is_bch_upper __builtin_isupper # define hio_is_bch_lower __builtin_islower # define hio_is_bch_alpha __builtin_isalpha diff --git a/lib/hio-cmn.h b/lib/hio-cmn.h index e7ddc51..c003aa6 100644 --- a/lib/hio-cmn.h +++ b/lib/hio-cmn.h @@ -25,7 +25,7 @@ #ifndef _HIO_CMN_H_ #define _HIO_CMN_H_ -/* WARNING: NEVER CHANGE/DELETE THE FOLLOWING HIO_HAVE_CFG_H DEFINITION. +/* WARNING: NEVER CHANGE/DELETE THE FOLLOWING HIO_HAVE_CFG_H DEFINITION. * IT IS USED FOR DEPLOYMENT BY MAKEFILE.AM */ /*#define HIO_HAVE_CFG_H*/ @@ -49,7 +49,7 @@ #if defined(EMSCRIPTEN) # if defined(HIO_SIZEOF___INT128) -# undef HIO_SIZEOF___INT128 +# undef HIO_SIZEOF___INT128 # define HIO_SIZEOF___INT128 0 # endif # if defined(HIO_SIZEOF_LONG) && defined(HIO_SIZEOF_INT) && (HIO_SIZEOF_LONG > HIO_SIZEOF_INT) @@ -291,7 +291,7 @@ typedef hio_int64_t hio_intptr_t; typedef hio_uint32_t hio_ushortptr_t; typedef hio_int32_t hio_shortptr_t; -#elif defined(HIO_HAVE_UINT128_T) && (HIO_SIZEOF_VOID_P == 16) +#elif defined(HIO_HAVE_UINT128_T) && (HIO_SIZEOF_VOID_P == 16) typedef hio_uint128_t hio_uintptr_t; typedef hio_int128_t hio_intptr_t; typedef hio_uint64_t hio_ushortptr_t; @@ -405,7 +405,7 @@ typedef unsigned char hio_bchu_t; /* unsigned version of hio_bch_t for # define HIO_SIZEOF_UCH_T 4 #elif defined(__GNUC__) && defined(__CHAR16_TYPE__) - typedef __CHAR16_TYPE__ hio_uch_t; + typedef __CHAR16_TYPE__ hio_uch_t; typedef hio_uint16_t hio_uchu_t; /* same as hio_uch_t as it is already unsigned */ # define HIO_SIZEOF_UCH_T 2 #else @@ -613,7 +613,7 @@ struct hio_ntime_t #endif /* make a bit mask that can mask off low n bits */ -#define HIO_LBMASK(type,n) (~(~((type)0) << (n))) +#define HIO_LBMASK(type,n) (~(~((type)0) << (n))) #define HIO_LBMASK_SAFE(type,n) (((n) < HIO_BITSOF(type))? HIO_LBMASK(type,n): ~(type)0) /* make a bit mask that can mask off hig n bits */ @@ -637,7 +637,7 @@ struct hio_ntime_t (value = (((type)(value)) | (((bits) & HIO_LBMASK(type,length)) << (offset)))) -/** +/** * The HIO_BITS_MAX() macros calculates the maximum value that the 'nbits' * bits of an unsigned integer of the given 'type' can hold. * \code @@ -652,12 +652,12 @@ struct hio_ntime_t * ========================================================================= */ typedef struct hio_mmgr_t hio_mmgr_t; -/** +/** * allocate a memory chunk of the size \a n. * \return pointer to a memory chunk on success, #HIO_NULL on failure. */ typedef void* (*hio_mmgr_alloc_t) (hio_mmgr_t* mmgr, hio_oow_t n); -/** +/** * resize a memory chunk pointed to by \a ptr to the size \a n. * \return pointer to a memory chunk on success, #HIO_NULL on failure. */ @@ -670,13 +670,13 @@ typedef void (*hio_mmgr_free_t) (hio_mmgr_t* mmgr, void* ptr); /** * The hio_mmgr_t type defines the memory management interface. * As the type is merely a structure, it is just used as a single container - * for memory management functions with a pointer to user-defined data. - * The user-defined data pointer \a ctx is passed to each memory management - * function whenever it is called. You can allocate, reallocate, and free + * for memory management functions with a pointer to user-defined data. + * The user-defined data pointer \a ctx is passed to each memory management + * function whenever it is called. You can allocate, reallocate, and free * a memory chunk. * * For example, a hio_xxx_open() function accepts a pointer of the hio_mmgr_t - * type and the xxx object uses it to manage dynamic data within the object. + * type and the xxx object uses it to manage dynamic data within the object. */ struct hio_mmgr_t { @@ -693,12 +693,12 @@ struct hio_mmgr_t #define HIO_MMGR_ALLOC(mmgr,size) ((mmgr)->alloc(mmgr,size)) /** - * The HIO_MMGR_REALLOC() macro resizes a memory block pointed to by \a ptr + * The HIO_MMGR_REALLOC() macro resizes a memory block pointed to by \a ptr * to the \a size bytes using the \a mmgr memory manager. */ #define HIO_MMGR_REALLOC(mmgr,ptr,size) ((mmgr)->realloc(mmgr,ptr,size)) -/** +/** * The HIO_MMGR_FREE() macro deallocates the memory block pointed to by \a ptr. */ #define HIO_MMGR_FREE(mmgr,ptr) ((mmgr)->free(mmgr,ptr)) @@ -710,7 +710,7 @@ struct hio_mmgr_t typedef struct hio_cmgr_t hio_cmgr_t; typedef hio_oow_t (*hio_cmgr_bctouc_t) ( - const hio_bch_t* mb, + const hio_bch_t* mb, hio_oow_t size, hio_uch_t* wc ); @@ -722,8 +722,8 @@ typedef hio_oow_t (*hio_cmgr_uctobc_t) ( ); /** - * The hio_cmgr_t type defines the character-level interface to - * multibyte/wide-character conversion. This interface doesn't + * The hio_cmgr_t type defines the character-level interface to + * multibyte/wide-character conversion. This interface doesn't * provide any facility to store conversion state in a context * independent manner. This leads to the limitation that it can * handle a stateless multibyte encoding only. @@ -741,7 +741,7 @@ struct hio_cmgr_t #if defined(_WIN32) || (defined(__WATCOMC__) && !defined(__WINDOWS_386__)) # define HIO_IMPORT __declspec(dllimport) # define HIO_EXPORT __declspec(dllexport) -# define HIO_PRIVATE +# define HIO_PRIVATE #elif defined(__GNUC__) && (__GNUC__>=4) # define HIO_IMPORT __attribute__((visibility("default"))) # define HIO_EXPORT __attribute__((visibility("default"))) @@ -758,12 +758,12 @@ struct hio_cmgr_t # define HIO_INLINE inline # define HIO_HAVE_INLINE #elif defined(__GNUC__) && defined(__GNUC_GNU_INLINE__) - /* gcc disables inline when -std=c89 or -ansi is used. + /* gcc disables inline when -std=c89 or -ansi is used. * so use __inline__ supported by gcc regardless of the options */ # define HIO_INLINE /*extern*/ __inline__ # define HIO_HAVE_INLINE #else -# define HIO_INLINE +# define HIO_INLINE # undef HIO_HAVE_INLINE #endif @@ -833,7 +833,7 @@ struct hio_cmgr_t */ -#if defined(__has_builtin) +#if defined(__has_builtin) #if __has_builtin(__builtin_ctz) #define HIO_HAVE_BUILTIN_CTZ #endif @@ -845,41 +845,41 @@ struct hio_cmgr_t #endif #if __has_builtin(__builtin_uadd_overflow) - #define HIO_HAVE_BUILTIN_UADD_OVERFLOW + #define HIO_HAVE_BUILTIN_UADD_OVERFLOW #endif #if __has_builtin(__builtin_uaddl_overflow) - #define HIO_HAVE_BUILTIN_UADDL_OVERFLOW + #define HIO_HAVE_BUILTIN_UADDL_OVERFLOW #endif #if __has_builtin(__builtin_uaddll_overflow) - #define HIO_HAVE_BUILTIN_UADDLL_OVERFLOW + #define HIO_HAVE_BUILTIN_UADDLL_OVERFLOW #endif #if __has_builtin(__builtin_umul_overflow) - #define HIO_HAVE_BUILTIN_UMUL_OVERFLOW + #define HIO_HAVE_BUILTIN_UMUL_OVERFLOW #endif #if __has_builtin(__builtin_umull_overflow) - #define HIO_HAVE_BUILTIN_UMULL_OVERFLOW + #define HIO_HAVE_BUILTIN_UMULL_OVERFLOW #endif #if __has_builtin(__builtin_umulll_overflow) - #define HIO_HAVE_BUILTIN_UMULLL_OVERFLOW + #define HIO_HAVE_BUILTIN_UMULLL_OVERFLOW #endif #if __has_builtin(__builtin_sadd_overflow) - #define HIO_HAVE_BUILTIN_SADD_OVERFLOW + #define HIO_HAVE_BUILTIN_SADD_OVERFLOW #endif #if __has_builtin(__builtin_saddl_overflow) - #define HIO_HAVE_BUILTIN_SADDL_OVERFLOW + #define HIO_HAVE_BUILTIN_SADDL_OVERFLOW #endif #if __has_builtin(__builtin_saddll_overflow) - #define HIO_HAVE_BUILTIN_SADDLL_OVERFLOW + #define HIO_HAVE_BUILTIN_SADDLL_OVERFLOW #endif #if __has_builtin(__builtin_smul_overflow) - #define HIO_HAVE_BUILTIN_SMUL_OVERFLOW + #define HIO_HAVE_BUILTIN_SMUL_OVERFLOW #endif #if __has_builtin(__builtin_smull_overflow) - #define HIO_HAVE_BUILTIN_SMULL_OVERFLOW + #define HIO_HAVE_BUILTIN_SMULL_OVERFLOW #endif #if __has_builtin(__builtin_smulll_overflow) - #define HIO_HAVE_BUILTIN_SMULLL_OVERFLOW + #define HIO_HAVE_BUILTIN_SMULLL_OVERFLOW #endif #if __has_builtin(__builtin_expect) @@ -919,7 +919,7 @@ struct hio_cmgr_t #elif defined(__GNUC__) && defined(__GNUC_MINOR__) - #if (__GNUC__ >= 4) + #if (__GNUC__ >= 4) #define HIO_HAVE_SYNC_LOCK_TEST_AND_SET #define HIO_HAVE_SYNC_LOCK_RELEASE @@ -975,7 +975,7 @@ struct hio_cmgr_t #if defined(__GNUC__) # define HIO_PACKED __attribute__((__packed__)) #else -# define HIO_PACKED +# define HIO_PACKED #endif /* ========================================================================= diff --git a/lib/hio-dhcp.h b/lib/hio-dhcp.h index 9b9fdbc..71b0437 100644 --- a/lib/hio-dhcp.h +++ b/lib/hio-dhcp.h @@ -97,7 +97,7 @@ typedef struct hio_dhcp6_pktinf_t hio_dhcp6_pktinf_t; /* ---------------------------------------------------------------- */ -typedef struct hio_svc_dhcs_t hio_svc_dhcs_t; +typedef struct hio_svc_dhcs_t hio_svc_dhcs_t; /* ---------------------------------------------------------------- */ diff --git a/lib/hio-dns.h b/lib/hio-dns.h index c0524a1..234ef7f 100644 --- a/lib/hio-dns.h +++ b/lib/hio-dns.h @@ -81,7 +81,7 @@ enum hio_dns_rrt_t HIO_DNS_RRT_NS = 2, HIO_DNS_RRT_MD = 3, /* mail destination. RFC973 replaced this with MX*/ HIO_DNS_RRT_MF = 4, /* mail forwarder. RFC973 replaced this with MX */ - + HIO_DNS_RRT_CNAME = 5, HIO_DNS_RRT_SOA = 6, @@ -103,7 +103,7 @@ enum hio_dns_rrt_t HIO_DNS_RRT_RRSIG = 46, /* - * [RFC1035] + * [RFC1035] * QTYPE fields appear in the question part of a query. QTYPES are a * superset of TYPEs, hence all TYPEs are valid QTYPEs. In addition, the * following QTYPEs are defined: @@ -127,7 +127,7 @@ enum hio_dns_rrc_t HIO_DNS_RRC_NONE = 254, /* - * + * * QCLASS fields appear in the question section of a query. QCLASS values * are a superset of CLASS values; every CLASS is a valid QCLASS. In * addition to CLASS values, the following QCLASSes are defined: @@ -305,7 +305,7 @@ typedef struct hio_dns_brr_t hio_dns_brr_t; #if 0 /* A RDATA */ -struct hio_dns_brrd_a_t +struct hio_dns_brrd_a_t { }; typedef struct hio_dns_brrd_a_t hio_dns_brrd_a_t; @@ -330,7 +330,7 @@ typedef struct hio_dns_brrd_mx_t hio_dns_brrd_mx_t; struct hio_dns_brrd_soa_t { hio_bch_t* mname; - hio_bch_t* rname; + hio_bch_t* rname; hio_uint32_t serial; hio_uint32_t refresh; hio_uint32_t retry; @@ -355,7 +355,7 @@ struct hio_dns_bedns_t hio_uint16_t uplen; /* udp payload len - will be placed in the qclass field of RR. */ /* the ttl field(32 bits) of RR holds extended rcode, version, dnssecok */ - hio_uint8_t version; + hio_uint8_t version; hio_uint8_t dnssecok; hio_oow_t beonum; /* option count */ @@ -454,7 +454,7 @@ struct hio_dns_pkt_info_t { int exist; hio_uint16_t uplen; /* udp payload len - will be placed in the qclass field of RR. */ - hio_uint8_t version; + hio_uint8_t version; hio_uint8_t dnssecok; hio_dns_cookie_t cookie; } edns; @@ -539,10 +539,10 @@ HIO_EXPORT hio_dns_msg_t* hio_svc_dnc_resolve ( ); /* - * -1: cookie in the request but no client cookie in the response. this may be ok or not ok depending on your policy + * -1: cookie in the request but no client cookie in the response. this may be ok or not ok depending on your policy * 0: client cookie mismatch in the request in the response * 1: client cookie match in the request in the response - * 2: no client cookie in the requset. so it deson't case about the response + * 2: no client cookie in the requset. so it deson't case about the response */ HIO_EXPORT int hio_svc_dnc_checkclientcookie ( hio_svc_dnc_t* dnc, @@ -587,7 +587,7 @@ HIO_EXPORT void hio_dns_free_msg ( hio_dns_msg_t* msg ); -/* +/* * return the pointer to the client cookie data in the packet. * if cookie is not HIO_NULL, it copies the client cookie there. */ diff --git a/lib/hio-ecs.h b/lib/hio-ecs.h index 12b64e0..533845f 100644 --- a/lib/hio-ecs.h +++ b/lib/hio-ecs.h @@ -29,8 +29,8 @@ #include /** string pointer and length as a aggregate */ -#define HIO_BECS_BCS(s) (&((s)->val)) -#define HIO_BECS_CS(s) (&((s)->val)) +#define HIO_BECS_BCS(s) (&((s)->val)) +#define HIO_BECS_CS(s) (&((s)->val)) /** string length */ #define HIO_BECS_LEN(s) ((s)->val.len) /** string pointer */ @@ -40,13 +40,13 @@ /** string capacity */ #define HIO_BECS_CAPA(s) ((s)->capa) /** character at the given position */ -#define HIO_BECS_CHAR(s,idx) ((s)->val.ptr[idx]) +#define HIO_BECS_CHAR(s,idx) ((s)->val.ptr[idx]) /**< last character. unsafe if length <= 0 */ #define HIO_BECS_LASTCHAR(s) ((s)->val.ptr[(s)->val.len-1]) /** string pointer and length as a aggregate */ -#define HIO_UECS_UCS(s) (&((s)->val)) -#define HIO_UECS_CS(s ) (&((s)->val)) +#define HIO_UECS_UCS(s) (&((s)->val)) +#define HIO_UECS_CS(s ) (&((s)->val)) /** string length */ #define HIO_UECS_LEN(s) ((s)->val.len) /** string pointer */ @@ -56,7 +56,7 @@ /** string capacity */ #define HIO_UECS_CAPA(s) ((s)->capa) /** character at the given position */ -#define HIO_UECS_CHAR(s,idx) ((s)->val.ptr[idx]) +#define HIO_UECS_CHAR(s,idx) ((s)->val.ptr[idx]) /**< last character. unsafe if length <= 0 */ #define HIO_UECS_LASTCHAR(s) ((s)->val.ptr[(s)->val.len-1]) @@ -138,7 +138,7 @@ HIO_EXPORT void hio_becs_close ( /** * The hio_becs_init() function initializes a dynamically resizable string - * If the parameter capa is 0, it doesn't allocate the internal buffer + * If the parameter capa is 0, it doesn't allocate the internal buffer * in advance and always succeeds. * \return 0 on success, -1 on failure. */ @@ -217,7 +217,7 @@ static HIO_INLINE hio_oow_t hio_becs_getcapa (hio_becs_t* becs) { return HIO_BEC * The hio_becs_setcapa() function sets the new capacity. If the new capacity * is smaller than the old, the overflowing characters are removed from * from the buffer. - * \return (hio_oow_t)-1 on failure, new capacity on success + * \return (hio_oow_t)-1 on failure, new capacity on success */ HIO_EXPORT hio_oow_t hio_becs_setcapa ( hio_becs_t* becs, @@ -235,7 +235,7 @@ static HIO_INLINE hio_oow_t hio_becs_getlen (hio_becs_t* becs) { return HIO_BECS /** * The hio_becs_setlen() function changes the string length. - * \return (hio_oow_t)-1 on failure, new length on success + * \return (hio_oow_t)-1 on failure, new length on success */ HIO_EXPORT hio_oow_t hio_becs_setlen ( hio_becs_t* becs, @@ -312,25 +312,25 @@ HIO_EXPORT hio_oow_t hio_becs_amend ( ); HIO_EXPORT hio_oow_t hio_becs_vfcat ( - hio_becs_t* str, + hio_becs_t* str, const hio_bch_t* fmt, va_list ap ); HIO_EXPORT hio_oow_t hio_becs_fcat ( - hio_becs_t* str, + hio_becs_t* str, const hio_bch_t* fmt, ... ); HIO_EXPORT hio_oow_t hio_becs_vfmt ( - hio_becs_t* str, + hio_becs_t* str, const hio_bch_t* fmt, va_list ap ); HIO_EXPORT hio_oow_t hio_becs_fmt ( - hio_becs_t* str, + hio_becs_t* str, const hio_bch_t* fmt, ... ); @@ -352,7 +352,7 @@ HIO_EXPORT void hio_uecs_close ( /** * The hio_uecs_init() function initializes a dynamically resizable string - * If the parameter capa is 0, it doesn't allocate the internal buffer + * If the parameter capa is 0, it doesn't allocate the internal buffer * in advance and always succeeds. * \return 0 on success, -1 on failure. */ @@ -431,7 +431,7 @@ static HIO_INLINE hio_oow_t hio_uecs_getcapa (hio_uecs_t* uecs) { return HIO_UEC * The hio_uecs_setcapa() function sets the new capacity. If the new capacity * is smaller than the old, the overflowing characters are removed from * from the buffer. - * \return (hio_oow_t)-1 on failure, new capacity on success + * \return (hio_oow_t)-1 on failure, new capacity on success */ HIO_EXPORT hio_oow_t hio_uecs_setcapa ( hio_uecs_t* uecs, @@ -449,7 +449,7 @@ static HIO_INLINE hio_oow_t hio_uecs_getlen (hio_uecs_t* uecs) { return HIO_UECS /** * The hio_uecs_setlen() function changes the string length. - * \return (hio_oow_t)-1 on failure, new length on success + * \return (hio_oow_t)-1 on failure, new length on success */ HIO_EXPORT hio_oow_t hio_uecs_setlen ( hio_uecs_t* uecs, @@ -527,25 +527,25 @@ HIO_EXPORT hio_oow_t hio_uecs_amend ( ); HIO_EXPORT hio_oow_t hio_uecs_vfcat ( - hio_uecs_t* str, + hio_uecs_t* str, const hio_uch_t* fmt, va_list ap ); HIO_EXPORT hio_oow_t hio_uecs_fcat ( - hio_uecs_t* str, + hio_uecs_t* str, const hio_uch_t* fmt, ... ); HIO_EXPORT hio_oow_t hio_uecs_vfmt ( - hio_uecs_t* str, + hio_uecs_t* str, const hio_uch_t* fmt, va_list ap ); HIO_EXPORT hio_oow_t hio_uecs_fmt ( - hio_uecs_t* str, + hio_uecs_t* str, const hio_uch_t* fmt, ... ); diff --git a/lib/hio-fmt.h b/lib/hio-fmt.h index fd59441..7a92307 100644 --- a/lib/hio-fmt.h +++ b/lib/hio-fmt.h @@ -49,7 +49,7 @@ typedef int (*hio_fmtout_putuchars_t) ( hio_oow_t len ); -enum hio_fmtout_fmt_type_t +enum hio_fmtout_fmt_type_t { HIO_FMTOUT_FMT_TYPE_BCH = 0, HIO_FMTOUT_FMT_TYPE_UCH @@ -73,9 +73,9 @@ struct hio_fmtout_t }; /* ========================================================================= - * INTMAX FORMATTING + * INTMAX FORMATTING * ========================================================================= */ -/** +/** * The hio_fmt_intmax_flag_t type defines enumerators to change the * behavior of hio_fmt_intmax() and hio_fmt_uintmax(). */ @@ -222,22 +222,22 @@ HIO_EXPORT int hio_ufmt_out ( * ========================================================================= */ /** - * The hio_fmt_intmax_to_bcstr() function formats an integer \a value to a - * multibyte string according to the given base and writes it to a buffer - * pointed to by \a buf. It writes to the buffer at most \a size characters - * including the terminating null. The base must be between 2 and 36 inclusive - * and can be ORed with zero or more #hio_fmt_intmax_to_bcstr_flag_t enumerators. - * This ORed value is passed to the function via the \a base_and_flags + * The hio_fmt_intmax_to_bcstr() function formats an integer \a value to a + * multibyte string according to the given base and writes it to a buffer + * pointed to by \a buf. It writes to the buffer at most \a size characters + * including the terminating null. The base must be between 2 and 36 inclusive + * and can be ORed with zero or more #hio_fmt_intmax_to_bcstr_flag_t enumerators. + * This ORed value is passed to the function via the \a base_and_flags * parameter. If the formatted string is shorter than \a bufsize, the redundant - * slots are filled with the fill character \a fillchar if it is not a null + * slots are filled with the fill character \a fillchar if it is not a null * character. The filling behavior is determined by the flags shown below: * - * - If #HIO_FMT_INTMAX_TO_BCSTR_FILLRIGHT is set in \a base_and_flags, slots + * - If #HIO_FMT_INTMAX_TO_BCSTR_FILLRIGHT is set in \a base_and_flags, slots * after the formatting string are filled. - * - If #HIO_FMT_INTMAX_TO_BCSTR_FILLCENTER is set in \a base_and_flags, slots + * - If #HIO_FMT_INTMAX_TO_BCSTR_FILLCENTER is set in \a base_and_flags, slots * before the formatting string are filled. However, if it contains the * sign character, the slots between the sign character and the digit part - * are filled. + * are filled. * - If neither #HIO_FMT_INTMAX_TO_BCSTR_FILLRIGHT nor #HIO_FMT_INTMAX_TO_BCSTR_FILLCENTER * , slots before the formatting string are filled. * @@ -249,26 +249,26 @@ HIO_EXPORT int hio_ufmt_out ( * * The terminating null is not added if #HIO_FMT_INTMAX_TO_BCSTR_NONULL is set; * The #HIO_FMT_INTMAX_TO_BCSTR_UPPERCASE flag indicates that the function should - * use the uppercase letter for a alphabetic digit; + * use the uppercase letter for a alphabetic digit; * You can set #HIO_FMT_INTMAX_TO_BCSTR_NOTRUNC if you require lossless formatting. - * The #HIO_FMT_INTMAX_TO_BCSTR_PLUSSIGN flag and #HIO_FMT_INTMAX_TO_BCSTR_EMPTYSIGN - * ensures that the plus sign and a space is added for a positive integer + * The #HIO_FMT_INTMAX_TO_BCSTR_PLUSSIGN flag and #HIO_FMT_INTMAX_TO_BCSTR_EMPTYSIGN + * ensures that the plus sign and a space is added for a positive integer * including 0 respectively. * The #HIO_FMT_INTMAX_TO_BCSTR_ZEROLEAD flag ensures that the numeric string * begins with '0' before applying the prefix. * You can set the #HIO_FMT_INTMAX_TO_BCSTR_NOZERO flag if you want the value of - * 0 to produce nothing. If both #HIO_FMT_INTMAX_TO_BCSTR_NOZERO and + * 0 to produce nothing. If both #HIO_FMT_INTMAX_TO_BCSTR_NOZERO and * #HIO_FMT_INTMAX_TO_BCSTR_ZEROLEAD are specified, '0' is still produced. - * + * * If \a prefix is not #HIO_NULL, it is inserted before the digits. - * + * * \return - * - -1 if the base is not between 2 and 36 inclusive. - * - negated number of characters required for lossless formatting + * - -1 if the base is not between 2 and 36 inclusive. + * - negated number of characters required for lossless formatting * - if \a bufsize is 0. * - if #HIO_FMT_INTMAX_TO_BCSTR_NOTRUNC is set and \a bufsize is less than * the minimum required for lossless formatting. - * - number of characters written to the buffer excluding a terminating + * - number of characters written to the buffer excluding a terminating * null in all other cases. */ HIO_EXPORT int hio_fmt_intmax_to_bcstr ( @@ -282,25 +282,25 @@ HIO_EXPORT int hio_fmt_intmax_to_bcstr ( ); /** - * The hio_fmt_intmax_to_ucstr() function formats an integer \a value to a - * wide-character string according to the given base and writes it to a buffer - * pointed to by \a buf. It writes to the buffer at most \a size characters - * including the terminating null. The base must be between 2 and 36 inclusive - * and can be ORed with zero or more #hio_fmt_intmax_to_ucstr_flag_t enumerators. - * This ORed value is passed to the function via the \a base_and_flags + * The hio_fmt_intmax_to_ucstr() function formats an integer \a value to a + * wide-character string according to the given base and writes it to a buffer + * pointed to by \a buf. It writes to the buffer at most \a size characters + * including the terminating null. The base must be between 2 and 36 inclusive + * and can be ORed with zero or more #hio_fmt_intmax_to_ucstr_flag_t enumerators. + * This ORed value is passed to the function via the \a base_and_flags * parameter. If the formatted string is shorter than \a bufsize, the redundant - * slots are filled with the fill character \a fillchar if it is not a null + * slots are filled with the fill character \a fillchar if it is not a null * character. The filling behavior is determined by the flags shown below: * - * - If #HIO_FMT_INTMAX_TO_UCSTR_FILLRIGHT is set in \a base_and_flags, slots + * - If #HIO_FMT_INTMAX_TO_UCSTR_FILLRIGHT is set in \a base_and_flags, slots * after the formatting string are filled. - * - If #HIO_FMT_INTMAX_TO_UCSTR_FILLCENTER is set in \a base_and_flags, slots + * - If #HIO_FMT_INTMAX_TO_UCSTR_FILLCENTER is set in \a base_and_flags, slots * before the formatting string are filled. However, if it contains the * sign character, the slots between the sign character and the digit part - * are filled. + * are filled. * - If neither #HIO_FMT_INTMAX_TO_UCSTR_FILLRIGHT nor #HIO_FMT_INTMAX_TO_UCSTR_FILLCENTER * , slots before the formatting string are filled. - * + * * The \a precision parameter specified the minimum number of digits to * produce from the \ value. If \a value produces fewer digits than * \a precision, the actual digits are padded with '0' to meet the precision @@ -309,26 +309,26 @@ HIO_EXPORT int hio_fmt_intmax_to_bcstr ( * * The terminating null is not added if #HIO_FMT_INTMAX_TO_UCSTR_NONULL is set; * The #HIO_FMT_INTMAX_TO_UCSTR_UPPERCASE flag indicates that the function should - * use the uppercase letter for a alphabetic digit; + * use the uppercase letter for a alphabetic digit; * You can set #HIO_FMT_INTMAX_TO_UCSTR_NOTRUNC if you require lossless formatting. - * The #HIO_FMT_INTMAX_TO_UCSTR_PLUSSIGN flag and #HIO_FMT_INTMAX_TO_UCSTR_EMPTYSIGN - * ensures that the plus sign and a space is added for a positive integer + * The #HIO_FMT_INTMAX_TO_UCSTR_PLUSSIGN flag and #HIO_FMT_INTMAX_TO_UCSTR_EMPTYSIGN + * ensures that the plus sign and a space is added for a positive integer * including 0 respectively. * The #HIO_FMT_INTMAX_TO_UCSTR_ZEROLEAD flag ensures that the numeric string * begins with 0 before applying the prefix. * You can set the #HIO_FMT_INTMAX_TO_UCSTR_NOZERO flag if you want the value of - * 0 to produce nothing. If both #HIO_FMT_INTMAX_TO_UCSTR_NOZERO and + * 0 to produce nothing. If both #HIO_FMT_INTMAX_TO_UCSTR_NOZERO and * #HIO_FMT_INTMAX_TO_UCSTR_ZEROLEAD are specified, '0' is still produced. * * If \a prefix is not #HIO_NULL, it is inserted before the digits. - * + * * \return - * - -1 if the base is not between 2 and 36 inclusive. - * - negated number of characters required for lossless formatting + * - -1 if the base is not between 2 and 36 inclusive. + * - negated number of characters required for lossless formatting * - if \a bufsize is 0. * - if #HIO_FMT_INTMAX_TO_UCSTR_NOTRUNC is set and \a bufsize is less than * the minimum required for lossless formatting. - * - number of characters written to the buffer excluding a terminating + * - number of characters written to the buffer excluding a terminating * null in all other cases. */ HIO_EXPORT int hio_fmt_intmax_to_ucstr ( @@ -343,8 +343,8 @@ HIO_EXPORT int hio_fmt_intmax_to_ucstr ( /** - * The hio_fmt_uintmax_to_bcstr() function formats an unsigned integer \a value - * to a multibyte string buffer. It behaves the same as hio_fmt_intmax_to_bcstr() + * The hio_fmt_uintmax_to_bcstr() function formats an unsigned integer \a value + * to a multibyte string buffer. It behaves the same as hio_fmt_intmax_to_bcstr() * except that it handles an unsigned integer. */ HIO_EXPORT int hio_fmt_uintmax_to_bcstr ( @@ -358,8 +358,8 @@ HIO_EXPORT int hio_fmt_uintmax_to_bcstr ( ); /** - * The hio_fmt_uintmax_to_ucstr() function formats an unsigned integer \a value - * to a unicode string buffer. It behaves the same as hio_fmt_intmax_to_ucstr() + * The hio_fmt_uintmax_to_ucstr() function formats an unsigned integer \a value + * to a unicode string buffer. It behaves the same as hio_fmt_intmax_to_ucstr() * except that it handles an unsigned integer. */ HIO_EXPORT int hio_fmt_uintmax_to_ucstr ( diff --git a/lib/hio-htb.h b/lib/hio-htb.h index b1f218a..0a6dd03 100644 --- a/lib/hio-htb.h +++ b/lib/hio-htb.h @@ -28,7 +28,7 @@ #include /**@file - * This file provides a hash table encapsulated in the #hio_htb_t type that + * This file provides a hash table encapsulated in the #hio_htb_t type that * maintains buckets for key/value pairs with the same key hash chained under * the same bucket. Its interface is very close to #hio_rbt_t. * @@ -36,31 +36,31 @@ * in the randome order. * @code * #include - * + * * static hio_htb_walk_t walk (hio_htb_t* htb, hio_htb_pair_t* pair, void* ctx) * { * hio_printf (HIO_T("key = %d, value = %d\n"), * *(int*)HIO_HTB_KPTR(pair), *(int*)HIO_HTB_VPTR(pair)); * return HIO_HTB_WALK_FORWARD; * } - * + * * int main () * { * hio_htb_t* s1; * int i; - * + * * hio_open_stdsios (); * s1 = hio_htb_open (HIO_MMGR_GETDFL(), 0, 30, 75, 1, 1); // error handling skipped * hio_htb_setstyle (s1, hio_get_htb_style(HIO_HTB_STYLE_INLINE_COPIERS)); - * + * * for (i = 0; i < 20; i++) * { * int x = i * 20; * hio_htb_insert (s1, &i, HIO_SIZEOF(i), &x, HIO_SIZEOF(x)); // eror handling skipped * } - * + * * hio_htb_walk (s1, walk, HIO_NULL); - * + * * hio_htb_close (s1); * hio_close_stdsios (); * return 0; @@ -71,7 +71,7 @@ typedef struct hio_htb_t hio_htb_t; typedef struct hio_htb_pair_t hio_htb_pair_t; -/** +/** * The hio_htb_walk_t type defines values that the callback function can * return to control hio_htb_walk(). */ @@ -101,7 +101,7 @@ typedef enum hio_htb_id_t hio_htb_id_t; */ typedef void* (*hio_htb_copier_t) ( hio_htb_t* htb /* hash table */, - void* dptr /* pointer to a key or a value */, + void* dptr /* pointer to a key or a value */, hio_oow_t dlen /* length of a key or a value */ ); @@ -124,17 +124,17 @@ typedef void (*hio_htb_freeer_t) ( * integer otherwise. */ typedef int (*hio_htb_comper_t) ( - const hio_htb_t* htb, /**< hash table */ + const hio_htb_t* htb, /**< hash table */ const void* kptr1, /**< key pointer */ - hio_oow_t klen1, /**< key length */ - const void* kptr2, /**< key pointer */ + hio_oow_t klen1, /**< key length */ + const void* kptr2, /**< key pointer */ hio_oow_t klen2 /**< key length */ ); /** - * The hio_htb_keeper_t type defines a value keeper that is called when + * The hio_htb_keeper_t type defines a value keeper that is called when * a value is retained in the context that it should be destroyed because - * it is identical to a new value. Two values are identical if their + * it is identical to a new value. Two values are identical if their * pointers and lengths are equal. */ typedef void (*hio_htb_keeper_t) ( @@ -145,7 +145,7 @@ typedef void (*hio_htb_keeper_t) ( /** * The hio_htb_sizer_t type defines a bucket size claculator that is called - * when hash table should resize the bucket. The current bucket size + 1 is + * when hash table should resize the bucket. The current bucket size + 1 is * passed as the hint. */ typedef hio_oow_t (*hio_htb_sizer_t) ( @@ -173,12 +173,12 @@ typedef hio_htb_walk_t (*hio_htb_walker_t) ( /** * The hio_htb_cbserter_t type defines a callback function for hio_htb_cbsert(). - * The hio_htb_cbserter() function calls it to allocate a new pair for the + * The hio_htb_cbserter() function calls it to allocate a new pair for the * key pointed to by @a kptr of the length @a klen and the callback context * @a ctx. The second parameter @a pair is passed the pointer to the existing * pair for the key or #HIO_NULL in case of no existing key. The callback * must return a pointer to a new or a reallocated pair. When reallocating the - * existing pair, this callback must destroy the existing pair and return the + * existing pair, this callback must destroy the existing pair and return the * newly reallocated pair. It must return #HIO_NULL for failure. */ typedef hio_htb_pair_t* (*hio_htb_cbserter_t) ( @@ -193,8 +193,8 @@ typedef hio_htb_pair_t* (*hio_htb_cbserter_t) ( /** * The hio_htb_pair_t type defines hash table pair. A pair is composed of a key * and a value. It maintains pointers to the beginning of a key and a value - * plus their length. The length is scaled down with the scale factor - * specified in an owning hash table. + * plus their length. The length is scaled down with the scale factor + * specified in an owning hash table. */ struct hio_htb_pair_t { @@ -202,7 +202,7 @@ struct hio_htb_pair_t hio_ptl_t val; /* management information below */ - hio_htb_pair_t* next; + hio_htb_pair_t* next; }; typedef struct hio_htb_style_t hio_htb_style_t; @@ -319,15 +319,15 @@ HIO_EXPORT const hio_htb_style_t* hio_get_htb_style ( ); /** - * The hio_htb_open() function creates a hash table with a dynamic array + * The hio_htb_open() function creates a hash table with a dynamic array * bucket and a list of values chained. The initial capacity should be larger * than 0. The load factor should be between 0 and 100 inclusive and the load * factor of 0 disables bucket resizing. If you need extra space associated * with hash table, you may pass a non-zero value for @a xtnsize. - * The HIO_HTB_XTN() macro and the hio_htb_getxtn() function return the + * The HIO_HTB_XTN() macro and the hio_htb_getxtn() function return the * pointer to the beginning of the extension. - * The @a kscale and @a vscale parameters specify the unit of the key and - * value size. + * The @a kscale and @a vscale parameters specify the unit of the key and + * value size. * @return #hio_htb_t pointer on success, #HIO_NULL on failure. */ HIO_EXPORT hio_htb_t* hio_htb_open ( @@ -380,7 +380,7 @@ HIO_EXPORT const hio_htb_style_t* hio_htb_getstyle ( ); /** - * The hio_htb_setstyle() function sets internal manipulation callback + * The hio_htb_setstyle() function sets internal manipulation callback * functions for data construction, destruction, resizing, hashing, etc. * The callback structure pointed to by \a style must outlive the hash * table pointed to by \a htb as the hash table doesn't copy the contents @@ -399,7 +399,7 @@ HIO_EXPORT hio_oow_t hio_htb_getsize ( ); /** - * The hio_htb_getcapa() function gets the number of slots allocated + * The hio_htb_getcapa() function gets the number of slots allocated * in a hash bucket. */ HIO_EXPORT hio_oow_t hio_htb_getcapa ( @@ -407,10 +407,10 @@ HIO_EXPORT hio_oow_t hio_htb_getcapa ( ); /** - * The hio_htb_search() function searches a hash table to find a pair with a + * The hio_htb_search() function searches a hash table to find a pair with a * matching key. It returns the pointer to the pair found. If it fails * to find one, it returns HIO_NULL. - * @return pointer to the pair with a maching key, + * @return pointer to the pair with a maching key, * or #HIO_NULL if no match is found. */ HIO_EXPORT hio_htb_pair_t* hio_htb_search ( @@ -420,12 +420,12 @@ HIO_EXPORT hio_htb_pair_t* hio_htb_search ( ); /** - * The hio_htb_upsert() function searches a hash table for the pair with a + * The hio_htb_upsert() function searches a hash table for the pair with a * matching key. If one is found, it updates the pair. Otherwise, it inserts - * a new pair with the key and value given. It returns the pointer to the + * a new pair with the key and value given. It returns the pointer to the * pair updated or inserted. - * @return pointer to the updated or inserted pair on success, - * #HIO_NULL on failure. + * @return pointer to the updated or inserted pair on success, + * #HIO_NULL on failure. */ HIO_EXPORT hio_htb_pair_t* hio_htb_upsert ( hio_htb_t* htb, /**< hash table */ @@ -437,9 +437,9 @@ HIO_EXPORT hio_htb_pair_t* hio_htb_upsert ( /** * The hio_htb_ensert() function inserts a new pair with the key and the value - * given. If there exists a pair with the key given, the function returns + * given. If there exists a pair with the key given, the function returns * the pair containing the key. - * @return pointer to a pair on success, #HIO_NULL on failure. + * @return pointer to a pair on success, #HIO_NULL on failure. */ HIO_EXPORT hio_htb_pair_t* hio_htb_ensert ( hio_htb_t* htb, /**< hash table */ @@ -451,9 +451,9 @@ HIO_EXPORT hio_htb_pair_t* hio_htb_ensert ( /** * The hio_htb_insert() function inserts a new pair with the key and the value - * given. If there exists a pair with the key given, the function returns + * given. If there exists a pair with the key given, the function returns * #HIO_NULL without channging the value. - * @return pointer to the pair created on success, #HIO_NULL on failure. + * @return pointer to the pair created on success, #HIO_NULL on failure. */ HIO_EXPORT hio_htb_pair_t* hio_htb_insert ( hio_htb_t* htb, /**< hash table */ @@ -477,7 +477,7 @@ HIO_EXPORT hio_htb_pair_t* hio_htb_update ( ); /** - * The hio_htb_cbsert() function inserts a key/value pair by delegating pair + * The hio_htb_cbsert() function inserts a key/value pair by delegating pair * allocation to a callback function. Depending on the callback function, * it may behave like hio_htb_insert(), hio_htb_upsert(), hio_htb_update(), * hio_htb_ensert(), or totally differently. The sample code below inserts @@ -494,7 +494,7 @@ HIO_EXPORT hio_htb_pair_t* hio_htb_update ( * HIO_HTB_VLEN(pair), HIO_HTB_VPTR(pair), (int)HIO_HTB_VLEN(pair)); * return HIO_HTB_WALK_FORWARD; * } - * + * * hio_htb_pair_t* cbserter ( * hio_htb_t* htb, hio_htb_pair_t* pair, * void* kptr, hio_oow_t klen, void* ctx) @@ -502,54 +502,54 @@ HIO_EXPORT hio_htb_pair_t* hio_htb_update ( * hio_cstr_t* v = (hio_cstr_t*)ctx; * if (pair == HIO_NULL) * { - * // no existing key for the key + * // no existing key for the key * return hio_htb_allocpair (htb, kptr, klen, v->ptr, v->len); * } * else * { - * // a pair with the key exists. - * // in this sample, i will append the new value to the old value + * // a pair with the key exists. + * // in this sample, i will append the new value to the old value * // separated by a comma * hio_htb_pair_t* new_pair; * hio_ooch_t comma = HIO_T(','); * hio_uint8_t* vptr; - * - * // allocate a new pair, but without filling the actual value. - * // note vptr is given HIO_NULL for that purpose + * + * // allocate a new pair, but without filling the actual value. + * // note vptr is given HIO_NULL for that purpose * new_pair = hio_htb_allocpair ( - * htb, kptr, klen, HIO_NULL, HIO_HTB_VLEN(pair) + 1 + v->len); + * htb, kptr, klen, HIO_NULL, HIO_HTB_VLEN(pair) + 1 + v->len); * if (new_pair == HIO_NULL) return HIO_NULL; - * - * // fill in the value space + * + * // fill in the value space * vptr = HIO_HTB_VPTR(new_pair); * hio_memcpy (vptr, HIO_HTB_VPTR(pair), HIO_HTB_VLEN(pair)*HIO_SIZEOF(hio_ooch_t)); * vptr += HIO_HTB_VLEN(pair)*HIO_SIZEOF(hio_ooch_t); * hio_memcpy (vptr, &comma, HIO_SIZEOF(hio_ooch_t)); * vptr += HIO_SIZEOF(hio_ooch_t); * hio_memcpy (vptr, v->ptr, v->len*HIO_SIZEOF(hio_ooch_t)); - * - * // this callback requires the old pair to be destroyed + * + * // this callback requires the old pair to be destroyed * hio_htb_freepair (htb, pair); - * - * // return the new pair + * + * // return the new pair * return new_pair; * } * } - * + * * int main () * { * hio_htb_t* s1; * int i; * hio_ooch_t* keys[] = { HIO_T("one"), HIO_T("two"), HIO_T("three") }; * hio_ooch_t* vals[] = { HIO_T("1"), HIO_T("2"), HIO_T("3"), HIO_T("4"), HIO_T("5") }; - * + * * hio_open_stdsios (); * s1 = hio_htb_open ( * HIO_MMGR_GETDFL(), 0, 10, 70, * HIO_SIZEOF(hio_ooch_t), HIO_SIZEOF(hio_ooch_t) - * ); // note error check is skipped + * ); // note error check is skipped * hio_htb_setstyle (s1, hio_get_htb_style(HIO_HTB_STYLE_INLINE_COPIERS)); - * + * * for (i = 0; i < HIO_COUNTOF(vals); i++) * { * hio_cstr_t ctx; @@ -560,7 +560,7 @@ HIO_EXPORT hio_htb_pair_t* hio_htb_update ( * ); // note error check is skipped * } * hio_htb_walk (s1, print_map_pair, HIO_NULL); - * + * * hio_htb_close (s1); * hio_close_stdsios (); * return 0; @@ -576,7 +576,7 @@ HIO_EXPORT hio_htb_pair_t* hio_htb_cbsert ( ); /** - * The hio_htb_delete() function deletes a pair with a matching key + * The hio_htb_delete() function deletes a pair with a matching key * @return 0 on success, -1 on failure */ HIO_EXPORT int hio_htb_delete ( @@ -615,7 +615,7 @@ HIO_EXPORT hio_htb_pair_t* hio_htb_getfirstpair ( ); /** - * The hio_htb_getnextpair() function returns the pointer to the next pair + * The hio_htb_getnextpair() function returns the pointer to the next pair * to the current pair @a pair in a hash table. */ HIO_EXPORT hio_htb_pair_t* hio_htb_getnextpair ( @@ -624,11 +624,11 @@ HIO_EXPORT hio_htb_pair_t* hio_htb_getnextpair ( ); /** - * The hio_htb_allocpair() function allocates a pair for a key and a value + * The hio_htb_allocpair() function allocates a pair for a key and a value * given. But it does not chain the pair allocated into the hash table @a htb. - * Use this function at your own risk. + * Use this function at your own risk. * - * Take note of he following special behavior when the copier is + * Take note of he following special behavior when the copier is * #HIO_HTB_COPIER_INLINE. * - If @a kptr is #HIO_NULL, the key space of the size @a klen is reserved but * not propagated with any data. @@ -637,8 +637,8 @@ HIO_EXPORT hio_htb_pair_t* hio_htb_getnextpair ( */ HIO_EXPORT hio_htb_pair_t* hio_htb_allocpair ( hio_htb_t* htb, - void* kptr, - hio_oow_t klen, + void* kptr, + hio_oow_t klen, void* vptr, hio_oow_t vlen ); diff --git a/lib/hio-htrd.h b/lib/hio-htrd.h index 19122db..146a43a 100644 --- a/lib/hio-htrd.h +++ b/lib/hio-htrd.h @@ -104,8 +104,8 @@ struct hio_htrd_t { hio_becs_t raw; /* buffer to hold raw octets */ hio_becs_t tra; /* buffer for handling trailers */ - } b; - } fed; + } b; + } fed; hio_htre_t re; int clean; @@ -127,7 +127,7 @@ HIO_EXPORT hio_htrd_t* hio_htrd_open ( * The hio_htrd_close() function destroys a htrd processor. */ HIO_EXPORT void hio_htrd_close ( - hio_htrd_t* htrd + hio_htrd_t* htrd ); HIO_EXPORT int hio_htrd_init ( @@ -172,8 +172,8 @@ HIO_EXPORT void hio_htrd_setrecbs ( ); /** - * The hio_htrd_feed() function accepts htrd request octets and invokes a - * callback function if it has processed a proper htrd request. + * The hio_htrd_feed() function accepts htrd request octets and invokes a + * callback function if it has processed a proper htrd request. */ HIO_EXPORT int hio_htrd_feed ( hio_htrd_t* htrd, /**< htrd */ @@ -184,9 +184,9 @@ HIO_EXPORT int hio_htrd_feed ( /** * The hio_htrd_halt() function indicates the end of feeeding - * if the current response should be processed until the + * if the current response should be processed until the * connection is closed. - */ + */ HIO_EXPORT int hio_htrd_halt ( hio_htrd_t* htrd ); diff --git a/lib/hio-htre.h b/lib/hio-htre.h index f4145a8..895f476 100644 --- a/lib/hio-htre.h +++ b/lib/hio-htre.h @@ -1,4 +1,4 @@ -/* +/* Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -79,7 +79,7 @@ enum hio_http_method_t HIO_HTTP_MERGE, HIO_HTTP_BASELINE_CONTROL, HIO_HTTP_MKACTIVITY, - + /* microsoft */ HIO_HTTP_BPROPFIND, HIO_HTTP_BPROPPATCH, @@ -122,9 +122,9 @@ enum hio_http_status_t }; typedef enum hio_http_status_t hio_http_status_t; -/* - * You should not manipulate an object of the #hio_htre_t - * type directly since it's complex. Use #hio_htrd_t to +/* + * You should not manipulate an object of the #hio_htre_t + * type directly since it's complex. Use #hio_htrd_t to * create an object of the hio_htre_t type. */ @@ -153,7 +153,7 @@ struct hio_htre_hdrval_t hio_htre_hdrval_t* next; }; -struct hio_htre_t +struct hio_htre_t { hio_t* hio; @@ -169,7 +169,7 @@ struct hio_htre_t union { - struct + struct { struct { @@ -222,7 +222,7 @@ struct hio_htre_t /* header table */ hio_htb_t hdrtab; hio_htb_t trailers; - + /* content octets */ hio_becs_t content; @@ -286,12 +286,12 @@ HIO_EXPORT void hio_htre_clear ( ); HIO_EXPORT const hio_htre_hdrval_t* hio_htre_getheaderval ( - const hio_htre_t* re, + const hio_htre_t* re, const hio_bch_t* key ); HIO_EXPORT const hio_htre_hdrval_t* hio_htre_gettrailerval ( - const hio_htre_t* re, + const hio_htre_t* re, const hio_bch_t* key ); @@ -310,8 +310,8 @@ HIO_EXPORT int hio_htre_walktrailers ( /** * The hio_htre_addcontent() function adds a content semgnet pointed to by * @a ptr of @a len bytes to the content buffer. If @a re is already completed - * or discarded, this function returns 0 without adding the segment to the - * content buffer. + * or discarded, this function returns 0 without adding the segment to the + * content buffer. * @return 1 on success, -1 on failure, 0 if adding is skipped. */ HIO_EXPORT int hio_htre_addcontent ( @@ -334,7 +334,7 @@ HIO_EXPORT void hio_htre_unsetconcb ( HIO_EXPORT void hio_htre_setconcb ( hio_htre_t* re, - hio_htre_concb_t concb, + hio_htre_concb_t concb, void* ctx ); diff --git a/lib/hio-http.h b/lib/hio-http.h index 45f9ca4..411de5e 100644 --- a/lib/hio-http.h +++ b/lib/hio-http.h @@ -44,18 +44,18 @@ enum hio_http_range_type_t typedef enum hio_http_range_type_t hio_http_range_type_t; /** * The hio_http_range_t type defines a structure that can represent - * a value for the \b Range: http header. + * a value for the \b Range: http header. * - * If type is #HIO_HTTP_RANGE_PREFIX, 'to' is meaningless and 'from' indicates - * the number of bytes from the start. + * If type is #HIO_HTTP_RANGE_PREFIX, 'to' is meaningless and 'from' indicates + * the number of bytes from the start. * - 500- => from the 501st bytes all the way to the back. - * - * If type is #HIO_HTTP_RANGE_SUFFIX, 'from' is meaningless and 'to' indicates - * the number of bytes from the back. + * + * If type is #HIO_HTTP_RANGE_SUFFIX, 'from' is meaningless and 'to' indicates + * the number of bytes from the back. * - -500 => last 500 bytes * * If type is #HIO_HTTP_RANGE_PROPER, 'from' and 'to' represents a proper range - * where the value of 0 indicates the first byte. This doesn't require any + * where the value of 0 indicates the first byte. This doesn't require any * adjustment. * - 0-999 => first 1000 bytes * - 99- => from the 100th bytes to the end. @@ -98,8 +98,8 @@ struct hio_svc_htts_rsrc_t HIO_SVC_HTTS_RSRC_HEADER; }; -#define HIO_SVC_HTTS_RSRC_ATTACH(rsrc, var) do { (var) = (rsrc); ++(rsrc)->rsrc_refcnt; } while(0) -#define HIO_SVC_HTTS_RSRC_DETACH(rsrc_var) do { if (--(rsrc_var)->rsrc_refcnt == 0) { hio_svc_htts_rsrc_t* __rsrc_tmp = (rsrc_var); (rsrc_var) = HIO_NULL; hio_svc_htts_rsrc_kill(__rsrc_tmp); } else { (rsrc_var) = HIO_NULL; } } while(0) +#define HIO_SVC_HTTS_RSRC_REF(rsrc, var) do { (var) = (rsrc); ++(rsrc)->rsrc_refcnt; } while(0) +#define HIO_SVC_HTTS_RSRC_UNREF(rsrc_var) do { if (--(rsrc_var)->rsrc_refcnt == 0) { hio_svc_htts_rsrc_t* __rsrc_tmp = (rsrc_var); (rsrc_var) = HIO_NULL; hio_svc_htts_rsrc_kill(__rsrc_tmp); } else { (rsrc_var) = HIO_NULL; } } while(0) /* -------------------------------------------------------------- */ @@ -113,8 +113,6 @@ typedef int (*hio_svc_htts_proc_req_t) ( /* -------------------------------------------------------------- */ struct hio_svc_htts_thr_func_info_t { - hio_svc_htts_t* htts; - hio_http_method_t req_method; hio_http_version_t req_version; hio_bch_t* req_path; @@ -129,7 +127,7 @@ struct hio_svc_htts_thr_func_info_t typedef struct hio_svc_htts_thr_func_info_t hio_svc_htts_thr_func_info_t; typedef void (*hio_svc_htts_thr_func_t) ( - hio_t* hio, + hio_svc_htts_t* htts, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx @@ -137,6 +135,28 @@ typedef void (*hio_svc_htts_thr_func_t) ( /* -------------------------------------------------------------- */ +struct hio_svc_htts_fun_func_info_t +{ + hio_http_method_t req_method; + hio_http_version_t req_version; + hio_bch_t* req_path; + hio_bch_t* req_param; + int req_x_http_method_override; /* -1 or hio_http_method_t */ + + /* TODO: header table */ + + hio_skad_t client_addr; + hio_skad_t server_addr; +}; +typedef struct hio_svc_htts_fun_func_info_t hio_svc_htts_fun_func_info_t; + +typedef void (*hio_svc_htts_fun_func_t) ( + hio_svc_htts_t* htts, + void* ctx +); + +/* -------------------------------------------------------------- */ + enum hio_svc_htts_cgi_option_t { HIO_SVC_HTTS_CGI_NO_100_CONTINUE = (1 << 0) @@ -240,7 +260,7 @@ HIO_EXPORT int hio_is_perenced_http_bcstr ( * \return the length of the output string. */ HIO_EXPORT hio_oow_t hio_perdec_http_bcstr ( - const hio_bch_t* str, + const hio_bch_t* str, hio_bch_t* buf, hio_oow_t* ndecs ); @@ -250,7 +270,7 @@ HIO_EXPORT hio_oow_t hio_perdec_http_bcstr ( * It doesn't insert the terminating null. */ HIO_EXPORT hio_oow_t hio_perdec_http_bcs ( - const hio_bcs_t* str, + const hio_bcs_t* str, hio_bch_t* buf, hio_oow_t* ndecs ); @@ -265,7 +285,7 @@ HIO_EXPORT hio_oow_t hio_perdec_http_bcs ( * string that would have been written if the buffer has been large enough. */ HIO_EXPORT hio_oow_t hio_perenc_http_bcstr ( - const hio_bch_t* str, + const hio_bch_t* str, hio_bch_t* buf, hio_oow_t len, int opt /**< 0 or bitwise-OR'ed of #hio_perenc_http_bcstr_opt_t */ @@ -302,6 +322,7 @@ HIO_EXPORT hio_oow_t hio_escape_html_bcstr ( hio_bch_t* buf, hio_oow_t len ); + /* ------------------------------------------------------------------------- */ /* HTTP SERVER SERVICE */ /* ------------------------------------------------------------------------- */ @@ -373,6 +394,8 @@ HIO_EXPORT int hio_svc_htts_dofcgi ( hio_dev_sck_t* csck, hio_htre_t* req, const hio_skad_t* fcgis_addr, + const hio_bch_t* docroot, + const hio_bch_t* script, int options /**< 0 or bitwise-Ored of #hio_svc_htts_file_option_t enumerators */ ); @@ -418,7 +441,7 @@ HIO_EXPORT void hio_svc_htts_rsrc_kill ( HIO_EXPORT void hio_svc_htts_fmtgmtime ( - hio_svc_htts_t* htts, + hio_svc_htts_t* htts, const hio_ntime_t* nt, hio_bch_t* buf, hio_oow_t len diff --git a/lib/hio-json.h b/lib/hio-json.h index 0ca8584..39505a9 100644 --- a/lib/hio-json.h +++ b/lib/hio-json.h @@ -27,7 +27,7 @@ #include -/** +/** * The hio_json_t type defines a simple json parser. */ typedef struct hio_json_t hio_json_t; @@ -91,15 +91,15 @@ struct hio_json_state_node_t struct { - /* 0: ready to get key (at the beginning or got comma), + /* 0: ready to get key (at the beginning or got comma), * 1: got key, 2: got colon, 3: got value */ - int state; + int state; } io; /* in object */ struct { int escaped; int digit_count; - /* acc is always of unicode type to handle \u and \U. + /* acc is always of unicode type to handle \u and \U. * in the bch mode, it will get converted to a utf8 stream. */ hio_uch_t acc; } sv; @@ -109,7 +109,7 @@ struct hio_json_state_node_t int digit_count; /* for a character, no way to support the unicode character * in the bch mode */ - hio_ooch_t acc; + hio_ooch_t acc; } cv; struct { @@ -122,7 +122,7 @@ struct hio_json_state_node_t enum hio_json_option_t { /* allow an unquoted word as an object key */ - HIO_JSON_PERMIT_WORD_KEY = ((hio_bitmask_t)1 << 0), + HIO_JSON_PERMIT_WORD_KEY = ((hio_bitmask_t)1 << 0), /* a comma as a separator is not mandatory */ HIO_JSON_OPTIONAL_COMMA = ((hio_bitmask_t)1 << 1), @@ -259,14 +259,14 @@ HIO_EXPORT void hio_json_resetfeedloc ( * The hio_json_feed() function processes the raw data. * * If stop_if_ever_complted is 0, it returns 0 on success. If the value pointed to by - * rem is greater 0 after the call, processing is not complete and more feeding is + * rem is greater 0 after the call, processing is not complete and more feeding is * required. Incomplete feeding may be caused by incomplete byte sequences or incomplete * json object. * * If stop_if_ever_completed is non-zero, it returns 0 upon incomplet byte sequence or * incomplete json object. It returns 1 if it sees the first complete json object. It stores * the size of remaning raw data in the memory pointed to by rem. - * + * * The function returns -1 upon failure. */ HIO_EXPORT int hio_json_feed ( diff --git a/lib/hio-nwif.h b/lib/hio-nwif.h index 99dad02..c2fec12 100644 --- a/lib/hio-nwif.h +++ b/lib/hio-nwif.h @@ -65,7 +65,7 @@ struct hio_ifcfg_t /* ---------------- */ - /* TODO: add hwaddr?? */ + /* TODO: add hwaddr?? */ /* i support ethernet only currently */ hio_uint8_t ethw[6]; /* out */ }; diff --git a/lib/hio-opt.h b/lib/hio-opt.h index 7dc7652..46ab069 100644 --- a/lib/hio-opt.h +++ b/lib/hio-opt.h @@ -28,8 +28,8 @@ #include "hio-cmn.h" /** \file - * This file defines functions and data structures to process - * command-line arguments. + * This file defines functions and data structures to process + * command-line arguments. */ typedef struct hio_uopt_t hio_uopt_t; @@ -52,7 +52,7 @@ struct hio_uopt_t hio_uch_t* arg; /* argument associated with an option */ /* output */ - const hio_uch_t* lngopt; + const hio_uch_t* lngopt; /* input + output */ int ind; /* index into parent argv vector */ @@ -81,7 +81,7 @@ struct hio_bopt_t hio_bch_t* arg; /**< argument associated with an option */ /* output */ - const hio_bch_t* lngopt; + const hio_bch_t* lngopt; /* input + output */ int ind; /**< index into parent argv vector */ @@ -96,31 +96,31 @@ extern "C" { /** * The hio_getuopt() function processes the \a argc command-line arguments - * pointed to by \a argv as configured in \a opt. It can process two - * different option styles: a single character starting with '-', and a - * long name starting with '--'. + * pointed to by \a argv as configured in \a opt. It can process two + * different option styles: a single character starting with '-', and a + * long name starting with '--'. * * A character in \a opt.str is treated as a single character option. Should * it require a parameter, specify ':' after it. * - * Two special returning option characters indicate special error conditions. + * Two special returning option characters indicate special error conditions. * - \b ? indicates a bad option stored in the \a opt->opt field. * - \b : indicates a bad parameter for an option stored in the \a opt->opt field. * * @return an option character on success, #HIO_UCI_EOF on no more options. */ HIO_EXPORT hio_uci_t hio_getuopt ( - int argc, /* argument count */ + int argc, /* argument count */ hio_uch_t* const* argv, /* argument array */ hio_uopt_t* opt /* option configuration */ ); /** - * The hio_getbopt() function is analogous to hio_getuopt() except that + * The hio_getbopt() function is analogous to hio_getuopt() except that * it accepts character strings of the #hio_bch_t type. */ HIO_EXPORT hio_bci_t hio_getbopt ( - int argc, /* argument count */ + int argc, /* argument count */ hio_bch_t* const* argv, /* argument array */ hio_bopt_t* opt /* option configuration */ ); diff --git a/lib/hio-pro.h b/lib/hio-pro.h index 47eec23..721a18c 100644 --- a/lib/hio-pro.h +++ b/lib/hio-pro.h @@ -107,7 +107,7 @@ enum hio_dev_pro_make_flag_t HIO_DEV_PRO_SHELL = (1 << 13), /* perform no waitpid() on a child process upon device destruction. - * you should set this flag if your application has automatic child + * you should set this flag if your application has automatic child * process reaping enabled. for instance, SIGCHLD is set to SIG_IGN * on POSIX.1-2001 compliant systems */ HIO_DEV_PRO_FORGET_CHILD = (1 << 14), diff --git a/lib/hio-prv.h b/lib/hio-prv.h index b713c01..95aab12 100644 --- a/lib/hio-prv.h +++ b/lib/hio-prv.h @@ -97,7 +97,7 @@ #endif -/* i don't want an error raised inside the callback to override +/* i don't want an error raised inside the callback to override * the existing error number and message. */ #define HIO_SYS_WRITE_LOG(hio,mask,ptr,len) do { \ int __shuterr = (hio)->_shuterr; \ @@ -132,8 +132,8 @@ void hio_firetmrjobs ( /** - * The hio_gettmrtmout() function gets the remaining time until the first - * scheduled job is to be triggered. It stores in \a tmout the difference between + * The hio_gettmrtmout() function gets the remaining time until the first + * scheduled job is to be triggered. It stores in \a tmout the difference between * the given time \a tm and the scheduled time and returns 1. If there is no * job scheduled, it returns 0. */ @@ -156,7 +156,7 @@ void hio_sys_fini ( ); void hio_sys_assertfail ( - hio_t* hio, + hio_t* hio, const hio_bch_t* expr, const hio_bch_t* file, hio_oow_t line diff --git a/lib/hio-pty.h b/lib/hio-pty.h index fee7976..c01e5ae 100644 --- a/lib/hio-pty.h +++ b/lib/hio-pty.h @@ -68,7 +68,7 @@ enum hio_dev_pty_make_flag_t HIO_DEV_PTY_UCMD = (1 << 12), /* cmd is hio_uch_t* */ HIO_DEV_PTY_SHELL = (1 << 13), /* perform no waitpid() on a child process upon device destruction. - * you should set this flag if your application has automatic child + * you should set this flag if your application has automatic child * process reaping enabled. for instance, SIGCHLD is set to SIG_IGN * on POSIX.1-2001 compliant systems */ HIO_DEV_PTY_FORGET_CHILD = (1 << 14), diff --git a/lib/hio-rad.h b/lib/hio-rad.h index 4581626..39be11e 100644 --- a/lib/hio-rad.h +++ b/lib/hio-rad.h @@ -28,7 +28,7 @@ #include #include -/* ----------------------------------------------------------- +/* ----------------------------------------------------------- * RARIUS MESSAGE DEFINITIONS * ----------------------------------------------------------- */ @@ -81,7 +81,7 @@ typedef struct hio_rad_attr_uint64_t hio_rad_attr_uint64_t; #endif #include -struct hio_rad_hdr_t +struct hio_rad_hdr_t { hio_uint8_t code; /* hio_rad_code_t */ hio_uint8_t id; @@ -166,9 +166,9 @@ struct hio_rad_attr_uint64_t typedef int (*hio_rad_attr_walker_t) ( - const hio_rad_hdr_t* hdr, + const hio_rad_hdr_t* hdr, hio_uint32_t vendor, /* in host-byte order */ - const hio_rad_attr_hdr_t* attr, + const hio_rad_attr_hdr_t* attr, void* ctx ); @@ -177,7 +177,7 @@ typedef int (*hio_rad_attr_walker_t) ( #define HIO_RAD_ATTR_IS_EXTENDED(attrtype) ((attrtype) >= HIO_RAD_ATTR_EXTENDED_1 && (attrtype) <= HIO_RAD_ATTR_EXTENDED_6) /* The attribute code is an attribute type encoded in 2 byte integer. */ -#define HIO_RAD_ATTR_CODE_MAKE(hi,lo) ((hio_uint16_t)((hi) & 0xFF) << 8 | ((lo) & 0xFF)) +#define HIO_RAD_ATTR_CODE_MAKE(hi,lo) ((hio_uint16_t)((hi) & 0xFF) << 8 | ((lo) & 0xFF)) #define HIO_RAD_ATTR_CODE_HI(attrtype) (((attrtype) >> 8) & 0xFF) #define HIO_RAD_ATTR_CODE_LO(attrtype) ((attrtype) & 0xFF) @@ -191,7 +191,7 @@ typedef int (*hio_rad_attr_walker_t) ( enum hio_rad_attr_code_t { /* ----------------------------------------------------------- - * 1 byte attribute types. they can be used as a code or a type + * 1 byte attribute types. they can be used as a code or a type * ----------------------------------------------------------- */ HIO_RAD_ATTR_USER_NAME = 1, /* string */ HIO_RAD_ATTR_USER_PASSWORD = 2, /* string encrypted */ @@ -293,7 +293,7 @@ HIO_EXPORT void hio_rad_initialize ( HIO_EXPORT int hio_rad_walk_attributes ( const hio_rad_hdr_t* hdr, - hio_rad_attr_walker_t walker, + hio_rad_attr_walker_t walker, void* ctx ); @@ -380,9 +380,9 @@ HIO_EXPORT int hio_rad_delete_attribute ( ); HIO_EXPORT int hio_rad_delete_extended_attribute ( - hio_rad_hdr_t* auth, + hio_rad_hdr_t* auth, hio_uint8_t xtype, - hio_uint8_t attrtype, + hio_uint8_t attrtype, int index ); @@ -423,7 +423,7 @@ HIO_EXPORT hio_rad_vsattr_hdr_t* hio_rad_insert_vendor_specific_attribute ( hio_rad_hdr_t* auth, int max, hio_uint32_t vendor, - hio_uint8_t attrtype, + hio_uint8_t attrtype, const void* ptr, hio_uint8_t len ); @@ -433,7 +433,7 @@ HIO_EXPORT hio_rad_xvsattr_hdr_t* hio_rad_insert_extended_vendor_specific_attrib int max, hio_uint32_t vendor, hio_uint8_t xtype, /* HIO_RAD_ATTR_EXTENDED_X */ - hio_uint8_t attrtype, + hio_uint8_t attrtype, const void* ptr, hio_uint8_t len, hio_uint8_t lxflags @@ -441,41 +441,41 @@ HIO_EXPORT hio_rad_xvsattr_hdr_t* hio_rad_insert_extended_vendor_specific_attrib HIO_EXPORT hio_rad_attr_hdr_t* hio_rad_insert_attribute_with_bcstr ( - hio_rad_hdr_t* auth, - int max, + hio_rad_hdr_t* auth, + int max, hio_uint32_t vendor, /* in host-byte order */ - hio_uint8_t id, + hio_uint8_t id, const hio_bch_t* value ); HIO_EXPORT hio_rad_attr_hdr_t* hio_rad_insert_attribute_ucstr ( - hio_rad_hdr_t* auth, - int max, + hio_rad_hdr_t* auth, + int max, hio_uint32_t vendor, /* in host-byte order */ - hio_uint8_t id, + hio_uint8_t id, const hio_uch_t* value ); HIO_EXPORT hio_rad_attr_hdr_t* hio_rad_insert_attribute_with_bchars ( - hio_rad_hdr_t* auth, - int max, + hio_rad_hdr_t* auth, + int max, hio_uint32_t vendor, /* in host-byte order */ - hio_uint8_t id, + hio_uint8_t id, const hio_bch_t* value, hio_uint8_t length ); HIO_EXPORT hio_rad_attr_hdr_t* hio_rad_insert_attribute_with_uchars ( - hio_rad_hdr_t* auth, - int max, + hio_rad_hdr_t* auth, + int max, hio_uint32_t vendor, /* in host-byte order */ - hio_uint8_t id, + hio_uint8_t id, const hio_uch_t* value, hio_uint8_t length ); HIO_EXPORT hio_rad_attr_hdr_t* hio_rad_insert_uint32_attribute ( - hio_rad_hdr_t* auth, + hio_rad_hdr_t* auth, int max, hio_uint32_t vendor, /* in host-byte order */ hio_uint8_t id, @@ -483,7 +483,7 @@ HIO_EXPORT hio_rad_attr_hdr_t* hio_rad_insert_uint32_attribute ( ); HIO_EXPORT hio_rad_attr_hdr_t* hio_rad_insert_ipv6prefix_attribute ( - hio_rad_hdr_t* auth, + hio_rad_hdr_t* auth, int max, hio_uint32_t vendor, /* in host-byte order */ hio_uint8_t id, @@ -503,7 +503,7 @@ HIO_EXPORT hio_rad_attr_hdr_t* hio_rad_insert_giga_attribute ( #endif /* ----------------------------------------------------------------------- */ - + HIO_EXPORT int hio_rad_set_user_password ( hio_rad_hdr_t* auth, int max, @@ -521,11 +521,11 @@ HIO_EXPORT void hio_rad_copy_authenticator ( ); HIO_EXPORT int hio_rad_set_authenticator ( - hio_rad_hdr_t* req, + hio_rad_hdr_t* req, const hio_bch_t* secret ); -/* +/* * verify an accounting request. * the authenticator of an access request is filled randomly. * so this function doesn't apply diff --git a/lib/hio-sck.h b/lib/hio-sck.h index d1aa451..34075dc 100644 --- a/lib/hio-sck.h +++ b/lib/hio-sck.h @@ -107,7 +107,7 @@ struct hio_iphdr_t typedef struct hio_iphdr_t hio_iphdr_t; -struct HIO_PACKED hio_icmphdr_t +struct HIO_PACKED hio_icmphdr_t { hio_uint8_t type; /* message type */ hio_uint8_t code; /* subcode */ @@ -189,7 +189,7 @@ typedef struct hio_icmphdr_t hio_icmphdr_t; enum hio_dev_sck_ioctl_cmd_t { - HIO_DEV_SCK_BIND, + HIO_DEV_SCK_BIND, HIO_DEV_SCK_CONNECT, HIO_DEV_SCK_LISTEN }; @@ -377,7 +377,7 @@ struct hio_dev_sck_t int state; - /* remote peer address for a stream socket. valid if one of the + /* remote peer address for a stream socket. valid if one of the * followings is set in state: * HIO_DEV_TCP_ACCEPTING_SSL * HIO_DEV_TCP_ACCEPTED @@ -387,7 +387,7 @@ struct hio_dev_sck_t * * also used as a placeholder to store source address for * a stateless socket */ - hio_skad_t remoteaddr; + hio_skad_t remoteaddr; /* local socket address */ hio_skad_t localaddr; diff --git a/lib/hio-shw.h b/lib/hio-shw.h index 85b8e7b..b4b62c2 100644 --- a/lib/hio-shw.h +++ b/lib/hio-shw.h @@ -25,7 +25,7 @@ #ifndef _HIO_SHW_H_ #define _HIO_SHW_H_ -/* system handle wrapper - +/* system handle wrapper - * turn a raw system handle/file descriptor to a device object */ #include diff --git a/lib/hio-skad.h b/lib/hio-skad.h index 1b4dc40..3b27510 100644 --- a/lib/hio-skad.h +++ b/lib/hio-skad.h @@ -86,8 +86,8 @@ typedef struct hio_skad_t hio_skad_t; #define HIO_IP4AD_STRLEN (15) /* not including the terminating '\0' */ #define HIO_IP6AD_STRLEN (45) /* not including the terminating '\0'. pure IPv6 address, not including the scope(e.g. %10, %eth0) */ -/* size large enough to hold the ip address plus port number. - * [IPV6ADDR%SCOPE]:PORT -> 9 for [] % : and PORT +/* size large enough to hold the ip address plus port number. + * [IPV6ADDR%SCOPE]:PORT -> 9 for [] % : and PORT * Let's reserve 16 for SCOPE and not include the terminting '\0' */ #define HIO_SKAD_IP_STRLEN (HIO_IP6AD_STRLEN + 25) @@ -101,7 +101,7 @@ typedef struct hio_skad_t hio_skad_t; #include struct HIO_PACKED hio_ethad_t { - hio_uint8_t v[HIO_ETHAD_LEN]; + hio_uint8_t v[HIO_ETHAD_LEN]; }; typedef struct hio_ethad_t hio_ethad_t; @@ -113,7 +113,7 @@ typedef struct hio_ip4ad_t hio_ip4ad_t; struct HIO_PACKED hio_ip6ad_t { - hio_uint8_t v[HIO_IP6AD_LEN]; + hio_uint8_t v[HIO_IP6AD_LEN]; }; typedef struct hio_ip6ad_t hio_ip6ad_t; #include @@ -182,7 +182,7 @@ HIO_EXPORT void hio_skad_init_for_ip_with_bytes ( hio_skad_t* skad, hio_uint16_t port, const hio_uint8_t* bytes, - hio_oow_t len + hio_oow_t len ); HIO_EXPORT void hio_skad_init_for_eth ( diff --git a/lib/hio-str.h b/lib/hio-str.h index 6a75bae..b5bd48b 100644 --- a/lib/hio-str.h +++ b/lib/hio-str.h @@ -40,7 +40,7 @@ -/* ========================================================================= +/* ========================================================================= * STRING * ========================================================================= */ enum hio_trim_flag_t @@ -641,7 +641,7 @@ HIO_EXPORT int hio_fnmat_bchars_i ( #define HIO_BYTE_TO_BCSTR_LOWERCASE HIO_BYTE_TO_OOCSTR_LOWERCASE HIO_EXPORT hio_oow_t hio_byte_to_bcstr ( - hio_uint8_t byte, + hio_uint8_t byte, hio_bch_t* buf, hio_oow_t size, int flagged_radix, @@ -649,7 +649,7 @@ HIO_EXPORT hio_oow_t hio_byte_to_bcstr ( ); HIO_EXPORT hio_oow_t hio_byte_to_ucstr ( - hio_uint8_t byte, + hio_uint8_t byte, hio_uch_t* buf, hio_oow_t size, int flagged_radix, @@ -665,32 +665,32 @@ HIO_EXPORT hio_oow_t hio_byte_to_ucstr ( /* ------------------------------------------------------------------------- */ HIO_EXPORT hio_oow_t hio_intmax_to_ucstr ( - hio_intmax_t value, - int radix, + hio_intmax_t value, + int radix, const hio_uch_t* prefix, hio_uch_t* buf, hio_oow_t size ); HIO_EXPORT hio_oow_t hio_intmax_to_bcstr ( - hio_intmax_t value, - int radix, + hio_intmax_t value, + int radix, const hio_bch_t* prefix, hio_bch_t* buf, hio_oow_t size ); HIO_EXPORT hio_oow_t hio_uintmax_to_ucstr ( - hio_uintmax_t value, - int radix, + hio_uintmax_t value, + int radix, const hio_uch_t* prefix, hio_uch_t* buf, hio_oow_t size ); HIO_EXPORT hio_oow_t hio_uintmax_to_bcstr ( - hio_uintmax_t value, - int radix, + hio_uintmax_t value, + int radix, const hio_bch_t* prefix, hio_bch_t* buf, hio_oow_t size @@ -818,7 +818,7 @@ templateINT_T int digit, negative = 0; int base = HIO_CHARS_TO_INT_GET_OPTION_BASE(option); - p = str; + p = str; end = str + len; if (HIO_CHARS_TO_INT_GET_OPTION_LTRIM(option)) @@ -830,7 +830,7 @@ templateINT_T /* check for a sign */ while (p < end) { - if (*p == '-') + if (*p == '-') { negative = ~negative; p++; @@ -841,9 +841,9 @@ templateINT_T /* check for a binary/octal/hexadecimal notation */ rem = end - p; - if (base == 0) + if (base == 0) { - if (rem >= 1 && *p == '0') + if (rem >= 1 && *p == '0') { p++; @@ -851,7 +851,7 @@ templateINT_T else if (*p == 'x' || *p == 'X') { p++; base = 16; - } + } else if (*p == 'b' || *p == 'B') { p++; base = 2; @@ -859,14 +859,14 @@ templateINT_T else base = 8; } else base = 10; - } + } else if (rem >= 2 && base == 16) { - if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ @@ -910,7 +910,7 @@ templateINT_T /* base 8: at least a zero digit has been seen. * other case: p > pp to be able to have at least 1 meaningful digit. */ - if (is_sober) *is_sober = (base == 8 || p > pp); + if (is_sober) *is_sober = (base == 8 || p > pp); if (HIO_CHARS_TO_INT_GET_OPTION_RTRIM(option)) { @@ -931,7 +931,7 @@ templateUINT int digit; int base = HIO_CHARS_TO_UINT_GET_OPTION_BASE(option); - p = str; + p = str; end = str + len; if (HIO_CHARS_TO_UINT_GET_OPTION_LTRIM(option)) @@ -949,9 +949,9 @@ templateUINT /* check for a binary/octal/hexadecimal notation */ rem = end - p; - if (base == 0) + if (base == 0) { - if (rem >= 1 && *p == '0') + if (rem >= 1 && *p == '0') { p++; @@ -959,7 +959,7 @@ templateUINT else if (*p == 'x' || *p == 'X') { p++; base = 16; - } + } else if (*p == 'b' || *p == 'B') { p++; base = 2; @@ -967,14 +967,14 @@ templateUINT else base = 8; } else base = 10; - } + } else if (rem >= 2 && base == 16) { - if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ @@ -1018,7 +1018,7 @@ templateUINT /* base 8: at least a zero digit has been seen. * other case: p > pp to be able to have at least 1 meaningful digit. */ - if (is_sober) *is_sober = (base == 8 || p > pp); + if (is_sober) *is_sober = (base == 8 || p > pp); if (HIO_CHARS_TO_UINT_GET_OPTION_RTRIM(option)) { diff --git a/lib/hio-utl.h b/lib/hio-utl.h index 52c7b5b..121c4a2 100644 --- a/lib/hio-utl.h +++ b/lib/hio-utl.h @@ -323,7 +323,7 @@ HIO_EXPORT hio_cmgr_t* hio_get_utf8_cmgr ( ); /** - * The hio_conv_uchars_to_utf8() function converts a unicode character string \a ucs + * The hio_conv_uchars_to_utf8() function converts a unicode character string \a ucs * to a UTF8 string and writes it into the buffer pointed to by \a bcs, but * not more than \a bcslen bytes including the terminating null. * @@ -372,7 +372,7 @@ HIO_EXPORT int hio_conv_uchars_to_utf8 ( * n = hio_conv_utf8_to_uchars (bcs, &bcslen, ucs, &ucslen); * if (n <= -1) { invalid/incomplenete sequence or buffer to small } * \endcode - * + * * The resulting \a ucslen can still be greater than 0 even if the return * value is negative. The value indiates the number of characters converted * before the error has occurred. @@ -421,19 +421,19 @@ HIO_EXPORT hio_oow_t hio_utf8_to_uc ( * TIME CALCULATION WITH OVERFLOW/UNDERFLOW DETECTION * ========================================================================= */ -/** +/** * The hio_add_ntime() function adds two time structures pointed to by \a x and \a y * and stores the result in the structure pointed to by \a z. If it detects overflow/ * underflow, it stores the largest/least possible value respectively. * You may use the HIO_ADD_NTIME() macro if overflow/underflow check isn't needed. */ HIO_EXPORT void hio_add_ntime ( - hio_ntime_t* z, + hio_ntime_t* z, const hio_ntime_t* x, const hio_ntime_t* y ); -/** +/** * The hio_sub_ntime() function subtracts the time value \a y from the time value \a x * and stores the result in the structure pointed to by \a z. If it detects overflow/ * underflow, it stores the largest/least possible value respectively. @@ -490,9 +490,9 @@ static HIO_INLINE hio_uint32_t hio_bswap32 (hio_uint32_t x) __asm__ volatile ("bswapl %0" : "=r"(x) : "0"(x)); return x; #else - return ((x >> 24)) | - ((x >> 8) & ((hio_uint32_t)0xff << 8)) | - ((x << 8) & ((hio_uint32_t)0xff << 16)) | + return ((x >> 24)) | + ((x >> 8) & ((hio_uint32_t)0xff << 8)) | + ((x << 8) & ((hio_uint32_t)0xff << 16)) | ((x << 24)); #endif } @@ -507,13 +507,13 @@ static HIO_INLINE hio_uint64_t hio_bswap64 (hio_uint64_t x) __asm__ volatile ("bswapq %0" : "=r"(x) : "0"(x)); return x; #else - return ((x >> 56)) | - ((x >> 40) & ((hio_uint64_t)0xff << 8)) | - ((x >> 24) & ((hio_uint64_t)0xff << 16)) | - ((x >> 8) & ((hio_uint64_t)0xff << 24)) | - ((x << 8) & ((hio_uint64_t)0xff << 32)) | - ((x << 24) & ((hio_uint64_t)0xff << 40)) | - ((x << 40) & ((hio_uint64_t)0xff << 48)) | + return ((x >> 56)) | + ((x >> 40) & ((hio_uint64_t)0xff << 8)) | + ((x >> 24) & ((hio_uint64_t)0xff << 16)) | + ((x >> 8) & ((hio_uint64_t)0xff << 24)) | + ((x << 8) & ((hio_uint64_t)0xff << 32)) | + ((x << 24) & ((hio_uint64_t)0xff << 40)) | + ((x << 40) & ((hio_uint64_t)0xff << 48)) | ((x << 56)); #endif } @@ -522,7 +522,7 @@ static HIO_INLINE hio_uint64_t hio_bswap64 (hio_uint64_t x) #if defined(HIO_HAVE_UINT128_T) static HIO_INLINE hio_uint128_t hio_bswap128 (hio_uint128_t x) { - return ((x >> 120)) | + return ((x >> 120)) | ((x >> 104) & ((hio_uint128_t)0xff << 8)) | ((x >> 88) & ((hio_uint128_t)0xff << 16)) | ((x >> 72) & ((hio_uint128_t)0xff << 24)) | diff --git a/lib/hio.c b/lib/hio.c index 3a7b063..89dcd3d 100644 --- a/lib/hio.c +++ b/lib/hio.c @@ -25,7 +25,7 @@ #include "hio-prv.h" #include #include /* malloc, free, etc */ - + #define DEV_CAP_ALL_WATCHED (HIO_DEV_CAP_IN_WATCHED | HIO_DEV_CAP_OUT_WATCHED | HIO_DEV_CAP_PRI_WATCHED) static void clear_unneeded_cfmbs (hio_t* hio); @@ -120,7 +120,7 @@ int hio_init (hio_t* hio, hio_mmgr_t* mmgr, hio_cmgr_t* cmgr, hio_bitmask_t feat * routine still function despite some side-effects when * reallocation fails */ /* +1 required for consistency with put_oocs and put_ooch in fmtout.c */ - hio->log.ptr = hio_allocmem(hio, (hio->log.capa + 1) * HIO_SIZEOF(*hio->log.ptr)); + hio->log.ptr = hio_allocmem(hio, (hio->log.capa + 1) * HIO_SIZEOF(*hio->log.ptr)); if (HIO_UNLIKELY(!hio->log.ptr)) goto oops; /* inititalize the system-side logging */ @@ -179,7 +179,7 @@ void hio_fini (hio_t* hio) hio_svc_t* svc; svc = HIO_SVCL_FIRST_SVC(&hio->actsvc); - if (svc->svc_stop) + if (svc->svc_stop) { /* the stop callback must unregister itself */ svc->svc_stop (svc); @@ -208,7 +208,7 @@ void hio_fini (hio_t* hio) for (dev = HIO_DEVL_FIRST_DEV(&hio->zmbdev); !HIO_DEVL_IS_NIL_DEV(&hio->zmbdev, dev); ) { kill_and_free_device (dev, 1); - if (HIO_DEVL_FIRST_DEV(&hio->zmbdev) == dev) + if (HIO_DEVL_FIRST_DEV(&hio->zmbdev) == dev) { /* the deive has not been freed. go on to the next one */ next_dev = dev->dev_next; @@ -228,7 +228,7 @@ void hio_fini (hio_t* hio) while (!HIO_DEVL_IS_EMPTY(&diehard)) { /* if the kill method returns failure, it can leak some resource - * because the device is freed regardless of the failure when 2 + * because the device is freed regardless of the failure when 2 * is given to kill_and_free_device(). */ dev = HIO_DEVL_FIRST_DEV(&diehard); HIO_ASSERT (hio, !(dev->dev_cap & (HIO_DEV_CAP_ACTIVE | HIO_DEV_CAP_HALTED | HIO_DEV_CAP_ZOMBIE))); @@ -287,7 +287,7 @@ int hio_setoption (hio_t* hio, hio_option_t id, const void* value) v1 = hio_dupbcstr(hio, value, HIO_NULL); if (HIO_UNLIKELY(!v1)) return -1; - + v2 = hio_dupbtoucstr(hio, value, HIO_NULL, 1); if (HIO_UNLIKELY(!v2)) { @@ -459,7 +459,7 @@ static void fire_cwq_handlers (hio_t* hio) hio_dev_t* dev_to_halt; cwq = HIO_CWQ_HEAD(&hio->cwq); - if (cwq->dev->dev_evcb->on_write(cwq->dev, cwq->olen, cwq->ctx, &cwq->dstaddr) <= -1) + if (cwq->dev->dev_evcb->on_write(cwq->dev, cwq->olen, cwq->ctx, &cwq->dstaddr) <= -1) { dev_to_halt = cwq->dev; } @@ -483,7 +483,7 @@ static void fire_cwq_handlers (hio_t* hio) hio_freemem (hio, cwq); } - if (dev_to_halt) + if (dev_to_halt) { HIO_DEBUG2 (hio, "DEV(%p) - halting a device for on_write error upon write completion[1] - %js\n", dev_to_halt, hio_geterrmsg(hio)); hio_dev_halt (dev_to_halt); @@ -506,7 +506,7 @@ static void fire_cwq_handlers_for_dev (hio_t* hio, hio_dev_t* dev, int for_kill) hio_dev_t* dev_to_halt; hio_oow_t cwqfl_index; - if (cwq->dev->dev_evcb->on_write(cwq->dev, cwq->olen, cwq->ctx, &cwq->dstaddr) <= -1) + if (cwq->dev->dev_evcb->on_write(cwq->dev, cwq->olen, cwq->ctx, &cwq->dstaddr) <= -1) { dev_to_halt = cwq->dev; } @@ -563,7 +563,7 @@ static HIO_INLINE void handle_event (hio_t* hio, hio_dev_t* dev, int events, int * >= 1 - everything is ok. */ x = dev->dev_evcb->ready(dev, xevents); if (x <= -1) - { + { HIO_DEBUG2 (hio, "DEV(%p) - halting a device for ready callback error - %js\n", dev, hio_geterrmsg(hio)); hio_dev_halt (dev); return; @@ -634,9 +634,9 @@ static HIO_INLINE void handle_event (hio_t* hio, hio_dev_t* dev, int events, int /* finished writing a single write request */ int y, out_closed = 0; - if (q->len <= 0 && (dev->dev_cap & HIO_DEV_CAP_STREAM)) + if (q->len <= 0 && (dev->dev_cap & HIO_DEV_CAP_STREAM)) { - /* it was a zero-length write request. + /* it was a zero-length write request. * for a stream, it is to close the output. */ dev->dev_cap |= HIO_DEV_CAP_OUT_CLOSED; dev->dev_cap |= HIO_DEV_CAP_RENEW_REQUIRED; @@ -657,7 +657,7 @@ static HIO_INLINE void handle_event (hio_t* hio, hio_dev_t* dev, int events, int if (out_closed) { - /* drain all pending requests. + /* drain all pending requests. * callbacks are skipped for drained requests */ while (!HIO_WQ_IS_EMPTY(&dev->wq)) { @@ -736,13 +736,13 @@ static HIO_INLINE void handle_event (hio_t* hio, hio_dev_t* dev, int events, int else /*if (x >= 1) */ { /* call on_write() callbacks enqueued fro the device before calling on_read(). - * if on_write() callback is delayed, there can be out-of-order execution + * if on_write() callback is delayed, there can be out-of-order execution * between on_read() and on_write() callbacks. for instance, if a write request - * is started from within on_read() callback, and the input data is available + * is started from within on_read() callback, and the input data is available * in the next iteration of this loop, the on_read() callback is triggered * before the on_write() callbacks scheduled before that on_read() callback. */ #if 0 - if (dev->cw_count > 0) + if (dev->cw_count > 0) { fire_cwq_handlers_for_dev (hio, dev); /* it will still invoke the on_read() callbak below even if @@ -751,12 +751,12 @@ static HIO_INLINE void handle_event (hio_t* hio, hio_dev_t* dev, int events, int #else /* currently fire_cwq_handlers_for_dev() scans the entire cwq list. * i might as well triggger handlers for all devices */ - fire_cwq_handlers (hio); + fire_cwq_handlers (hio); #endif - if (len <= 0 && (dev->dev_cap & HIO_DEV_CAP_STREAM)) + if (len <= 0 && (dev->dev_cap & HIO_DEV_CAP_STREAM)) { - /* EOF received. for a stream device, a zero-length + /* EOF received. for a stream device, a zero-length * read is interpreted as EOF. */ dev->dev_cap |= HIO_DEV_CAP_IN_CLOSED; dev->dev_cap |= HIO_DEV_CAP_RENEW_REQUIRED; @@ -765,7 +765,7 @@ static HIO_INLINE void handle_event (hio_t* hio, hio_dev_t* dev, int events, int if (dev->dev_evcb->on_read(dev, hio->bigbuf, len, &srcaddr) <= -1 || (dev->dev_cap & HIO_DEV_CAP_OUT_CLOSED)) { - /* 1. input ended and its reporting failed or + /* 1. input ended and its reporting failed or * 2. input ended and no writing is possible */ if (dev->dev_cap & HIO_DEV_CAP_OUT_CLOSED) HIO_DEBUG1 (hio, "DEV(%p) - halting a stream device on input EOF as output is also closed\n", dev); @@ -806,7 +806,7 @@ static HIO_INLINE void handle_event (hio_t* hio, hio_dev_t* dev, int events, int if (dev) { if (events & (HIO_DEV_EVENT_ERR | HIO_DEV_EVENT_HUP)) - { + { /* if error or hangup has been reported on the device, * halt the device. this check is performed after * EPOLLIN or EPOLLOUT check because EPOLLERR or EPOLLHUP @@ -823,7 +823,7 @@ static HIO_INLINE void handle_event (hio_t* hio, hio_dev_t* dev, int events, int dev->dev_cap |= HIO_DEV_CAP_IN_CLOSED | HIO_DEV_CAP_OUT_CLOSED; dev->dev_cap |= HIO_DEV_CAP_RENEW_REQUIRED; } - else if (dev && rdhup) + else if (dev && rdhup) { if (events & (HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT | HIO_DEV_EVENT_PRI)) { @@ -862,7 +862,7 @@ static void clear_unneeded_cfmbs (hio_t* hio) while (!HIO_CFMBL_IS_NIL_CFMB(&hio->cfmb, cur)) { next = HIO_CFMBL_NEXT_CFMB(cur); - if (cur->cfmb_checker(hio, cur)) + if (cur->cfmb_checker(hio, cur)) { HIO_CFMBL_UNLINK_CFMB (cur); hio_freemem (hio, cur); @@ -874,7 +874,7 @@ static void clear_unneeded_cfmbs (hio_t* hio) static void kill_all_halted_devices (hio_t* hio) { /* kill all halted devices */ - while (!HIO_DEVL_IS_EMPTY(&hio->hltdev)) + while (!HIO_DEVL_IS_EMPTY(&hio->hltdev)) { hio_dev_t* dev = HIO_DEVL_FIRST_DEV(&hio->hltdev); HIO_DEBUG1 (hio, "MIO - Killing HALTED device %p\n", dev); @@ -892,12 +892,12 @@ static HIO_INLINE int __exec (hio_t* hio) /* execute callbacks for completed write operations */ fire_cwq_handlers (hio); - /* execute the scheduled jobs before checking devices with the + /* execute the scheduled jobs before checking devices with the * multiplexer. the scheduled jobs can safely destroy the devices */ hio_firetmrjobs (hio, HIO_NULL, HIO_NULL); - /* execute callbacks for completed write operations again in case - * some works initiated in the timer jobs have complted and added to CWQ. + /* execute callbacks for completed write operations again in case + * some works initiated in the timer jobs have complted and added to CWQ. * e.g. write() in a timer job gets completed immediately. */ fire_cwq_handlers (hio); @@ -917,7 +917,7 @@ static HIO_INLINE int __exec (hio_t* hio) tmout.nsec = 0; } - if (hio_sys_waitmux(hio, &tmout, handle_event) <= -1) + if (hio_sys_waitmux(hio, &tmout, handle_event) <= -1) { HIO_DEBUG0 (hio, "MIO - WARNING - Failed to wait on mutiplexer\n"); ret = -1; @@ -992,7 +992,7 @@ hio_dev_t* hio_dev_make (hio_t* hio, hio_oow_t dev_size, hio_dev_mth_t* dev_mth, { hio_dev_t* dev = HIO_NULL; - if (dev_size < HIO_SIZEOF(hio_dev_t)) + if (dev_size < HIO_SIZEOF(hio_dev_t)) { hio_seterrnum (hio, HIO_EINVAL); if (dev_mth->fail_before_make) dev_mth->fail_before_make (make_ctx); @@ -1000,7 +1000,7 @@ hio_dev_t* hio_dev_make (hio_t* hio, hio_oow_t dev_size, hio_dev_mth_t* dev_mth, } dev = (hio_dev_t*)hio_callocmem(hio, dev_size); - if (HIO_UNLIKELY(!dev)) + if (HIO_UNLIKELY(!dev)) { if (dev_mth->fail_before_make) dev_mth->fail_before_make (make_ctx); goto oops; @@ -1013,7 +1013,7 @@ hio_dev_t* hio_dev_make (hio_t* hio, hio_oow_t dev_size, hio_dev_mth_t* dev_mth, dev->dev_cap = HIO_DEV_CAP_IN | HIO_DEV_CAP_OUT; dev->dev_mth = dev_mth; dev->dev_evcb = dev_evcb; - HIO_INIT_NTIME (&dev->rtmout, 0, 0); + HIO_INIT_NTIME (&dev->rtmout, 0, 0); dev->rtmridx = HIO_TMRIDX_INVALID; HIO_WQ_INIT (&dev->wq); dev->cw_count = 0; @@ -1027,7 +1027,7 @@ hio_dev_t* hio_dev_make (hio_t* hio, hio_oow_t dev_size, hio_dev_mth_t* dev_mth, HIO_ASSERT (hio, dev->dev_prev == HIO_NULL); HIO_ASSERT (hio, dev->dev_next == HIO_NULL); - /* set some internal capability bits according to the capabilities + /* set some internal capability bits according to the capabilities * removed by the device making callback for convenience sake. */ dev->dev_cap &= HIO_DEV_CAP_ALL_MASK; /* keep valid capability bits only. drop all internal-use bits */ if (!(dev->dev_cap & HIO_DEV_CAP_IN)) dev->dev_cap |= HIO_DEV_CAP_IN_CLOSED; @@ -1046,16 +1046,16 @@ oops_after_make: if (kill_and_free_device(dev, 0) <= -1) { /* schedule a timer job that reattempts to destroy the device */ - if (schedule_kill_zombie_job(dev) <= -1) + if (schedule_kill_zombie_job(dev) <= -1) { /* job scheduling failed. i have no choice but to * destroy the device now. - * + * * NOTE: this while loop can block the process * if the kill method keep returning failure */ while (kill_and_free_device(dev, 1) <= -1) { - if (hio->stopreq != HIO_STOPREQ_NONE) + if (hio->stopreq != HIO_STOPREQ_NONE) { /* i can't wait until destruction attempt gets * fully successful. there is a chance that some @@ -1082,7 +1082,7 @@ static int kill_and_free_device (hio_dev_t* dev, int force) HIO_ASSERT (hio, !(dev->dev_cap & HIO_DEV_CAP_HALTED)); HIO_DEBUG1 (hio, "MIO - Calling kill method on device %p\n", dev); - if (dev->dev_mth->kill(dev, force) <= -1) + if (dev->dev_mth->kill(dev, force) <= -1) { HIO_DEBUG1 (hio, "MIO - Failure by kill method on device %p\n", dev); @@ -1126,7 +1126,7 @@ static void kill_zombie_job_handler (hio_t* hio, const hio_ntime_t* now, hio_tmr /* i have to choice but to free up the devide by force */ while (kill_and_free_device(dev, 1) <= -1) { - if (hio->stopreq != HIO_STOPREQ_NONE) + if (hio->stopreq != HIO_STOPREQ_NONE) { /* i can't wait until destruction attempt gets * fully successful. there is a chance that some @@ -1223,7 +1223,7 @@ kill_device: /* i have no choice but to free up the devide by force */ while (kill_and_free_device(dev, 1) <= -1) { - if (hio->stopreq != HIO_STOPREQ_NONE) + if (hio->stopreq != HIO_STOPREQ_NONE) { /* i can't wait until destruction attempt gets * fully successful. there is a chance that some @@ -1279,10 +1279,10 @@ int hio_dev_watch (hio_dev_t* dev, hio_dev_watch_cmd_t cmd, int events) * it's different from not hanving HIO_DEV_CAP_IN and HIO_DEV_CAP_OUT. * a non-virtual device without the capabilities still gets attention * of the system multiplexer for hangup and error. */ - if (dev->dev_cap & HIO_DEV_CAP_VIRTUAL) + if (dev->dev_cap & HIO_DEV_CAP_VIRTUAL) { /* UGLY HACK - you may start a device with VIRTUAL set upon creation when START is attempted. - * later, if you mask off VIRTUAL, you may perform normal IO and call + * later, if you mask off VIRTUAL, you may perform normal IO and call * hio_dev_watch() with UPDATE. if SUSPENDED is set, UPDATE works */ if (cmd == HIO_DEV_WATCH_START) dev->dev_cap |= HIO_DEV_CAP_WATCH_SUSPENDED; /* END UGLY HACK */ @@ -1296,8 +1296,8 @@ int hio_dev_watch (hio_dev_t* dev, hio_dev_watch_cmd_t cmd, int events) { case HIO_DEV_WATCH_START: /* request input watching when a device is started. - * if the device is set with HIO_DEV_CAP_IN_DISABLED and/or - * is not set with HIO_DEV_CAP_IN, input wathcing is excluded + * if the device is set with HIO_DEV_CAP_IN_DISABLED and/or + * is not set with HIO_DEV_CAP_IN, input wathcing is excluded * after this 'switch' block */ events = HIO_DEV_EVENT_IN; mux_cmd = HIO_SYS_MUX_CMD_INSERT; @@ -1309,7 +1309,7 @@ int hio_dev_watch (hio_dev_t* dev, hio_dev_watch_cmd_t cmd, int events) * output watching is requested only if there're enqueued data for writing. * if you want to enable input watching while renewing, call this function like this. * hio_dev_wtach (dev, HIO_DEV_WATCH_RENEW, HIO_DEV_EVENT_IN); - * if you want input watching disabled while renewing, call this function like this. + * if you want input watching disabled while renewing, call this function like this. * hio_dev_wtach (dev, HIO_DEV_WATCH_RENEW, 0); */ if (HIO_WQ_IS_EMPTY(&dev->wq)) events &= ~HIO_DEV_EVENT_OUT; else events |= HIO_DEV_EVENT_OUT; @@ -1338,7 +1338,7 @@ int hio_dev_watch (hio_dev_t* dev, hio_dev_watch_cmd_t cmd, int events) * by reducing the variety of event bits that the caller has to handle. */ if ((events & HIO_DEV_EVENT_IN) && !(dev->dev_cap & (HIO_DEV_CAP_IN_CLOSED | HIO_DEV_CAP_IN_DISABLED))) { - if (dev->dev_cap & HIO_DEV_CAP_IN) + if (dev->dev_cap & HIO_DEV_CAP_IN) { if (dev->dev_cap & HIO_DEV_CAP_PRI) dev_cap |= HIO_DEV_CAP_PRI_WATCHED; dev_cap |= HIO_DEV_CAP_IN_WATCHED; @@ -1373,11 +1373,11 @@ static void on_read_timeout (hio_t* hio, const hio_ntime_t* now, hio_tmrjob_t* j dev = (hio_dev_t*)job->ctx; hio_seterrnum (hio, HIO_ETMOUT); - x = dev->dev_evcb->on_read(dev, HIO_NULL, -1, HIO_NULL); + x = dev->dev_evcb->on_read(dev, HIO_NULL, -1, HIO_NULL); HIO_ASSERT (hio, dev->rtmridx == HIO_TMRIDX_INVALID); - if (x <= -1) + if (x <= -1) { HIO_DEBUG2 (hio, "DEV(%p) - halting a device for on_read error upon timeout - %js\n", dev, hio_geterrmsg(hio)); hio_dev_halt (dev); @@ -1437,7 +1437,7 @@ update_timer: tmrjob.idxptr = &dev->rtmridx; dev->rtmridx = hio_instmrjob(hio, &tmrjob); - if (dev->rtmridx == HIO_TMRIDX_INVALID) + if (dev->rtmridx == HIO_TMRIDX_INVALID) { /* if timer registration fails, timeout will never be triggered */ return -1; @@ -1467,13 +1467,13 @@ static void on_write_timeout (hio_t* hio, const hio_ntime_t* now, hio_tmrjob_t* dev = q->dev; hio_seterrnum (hio, HIO_ETMOUT); - x = dev->dev_evcb->on_write(dev, -1, q->ctx, &q->dstaddr); + x = dev->dev_evcb->on_write(dev, -1, q->ctx, &q->dstaddr); HIO_ASSERT (hio, q->tmridx == HIO_TMRIDX_INVALID); HIO_WQ_UNLINK(q); hio_freemem (hio, q); - if (x <= -1) + if (x <= -1) { HIO_DEBUG2 (hio, "DEV(%p) - halting a device for on_write error upon timeout - %js\n", dev, hio_geterrmsg(hio)); hio_dev_halt (dev); @@ -1577,7 +1577,7 @@ static HIO_INLINE int __enqueue_pending_write (hio_dev_t* dev, hio_iolen_t olen, tmrjob.idxptr = &q->tmridx; q->tmridx = hio_instmrjob(hio, &tmrjob); - if (q->tmridx == HIO_TMRIDX_INVALID) + if (q->tmridx == HIO_TMRIDX_INVALID) { hio_freemem (hio, q); return -1; @@ -1650,7 +1650,7 @@ static HIO_INLINE int __enqueue_pending_sendfile (hio_dev_t* dev, hio_iolen_t ol tmrjob.idxptr = &q->tmridx; q->tmridx = hio_instmrjob(hio, &tmrjob); - if (q->tmridx == HIO_TMRIDX_INVALID) + if (q->tmridx == HIO_TMRIDX_INVALID) { hio_freemem (hio, q); return -1; @@ -1690,9 +1690,9 @@ static HIO_INLINE int __dev_write (hio_dev_t* dev, const void* data, hio_iolen_t uptr = data; urem = len; - if (!HIO_WQ_IS_EMPTY(&dev->wq)) + if (!HIO_WQ_IS_EMPTY(&dev->wq)) { - /* the writing queue is not empty. + /* the writing queue is not empty. * enqueue this request immediately */ goto enqueue_data; } @@ -1705,17 +1705,17 @@ static HIO_INLINE int __dev_write (hio_dev_t* dev, const void* data, hio_iolen_t ulen = urem; x = dev->dev_mth->write(dev, data, &ulen, dstaddr); if (x <= -1) return -1; - else if (x == 0) + else if (x == 0) { - /* [NOTE] - * the write queue is empty at this moment. a zero-length + /* [NOTE] + * the write queue is empty at this moment. a zero-length * request for a stream device can still get enqueued if the * write callback returns 0 though i can't figure out if there - * is a compelling reason to do so + * is a compelling reason to do so */ goto enqueue_data; /* enqueue remaining data */ } - else + else { /* the write callback should return at most the number of requested * bytes. but returning more is harmless as urem is of a signed type. @@ -1735,7 +1735,7 @@ static HIO_INLINE int __dev_write (hio_dev_t* dev, const void* data, hio_iolen_t } /* if i trigger the write completion callback here, the performance - * may increase, but there can be annoying recursion issues if the + * may increase, but there can be annoying recursion issues if the * callback requests another writing operation. it's imperative to * delay the callback until this write function is finished. * ---> if (dev->dev_evcb->on_write(dev, len, wrctx, dstaddr) <= -1) return -1; */ @@ -1751,7 +1751,7 @@ static HIO_INLINE int __dev_write (hio_dev_t* dev, const void* data, hio_iolen_t /* partial writing is still considered ok for a non-stream device. */ - /* read the comment in the 'if' block above for why i enqueue the write completion event + /* read the comment in the 'if' block above for why i enqueue the write completion event * instead of calling the event callback here... * ---> if (dev->dev_evcb->on_write(dev, ulen, wrctx, dstaddr) <= -1) return -1; */ goto enqueue_completed_write; @@ -1785,7 +1785,7 @@ static HIO_INLINE int __dev_writev (hio_dev_t* dev, hio_iovec_t* iov, hio_iolen_ for (i = 0; i < iovcnt; i++) len += iov[i].iov_len; urem = len; - if (!HIO_WQ_IS_EMPTY(&dev->wq)) + if (!HIO_WQ_IS_EMPTY(&dev->wq)) { /* if the writing queue is not empty, enqueue this request immediately */ goto enqueue_data; @@ -1802,13 +1802,13 @@ static HIO_INLINE int __dev_writev (hio_dev_t* dev, hio_iovec_t* iov, hio_iolen_ dcnt = iovcnt - index; x = dev->dev_mth->writev(dev, &iov[index], &dcnt, dstaddr); if (x <= -1) return -1; - else if (x == 0) + else if (x == 0) { - /* [NOTE] - * the write queue is empty at this moment. a zero-length + /* [NOTE] + * the write queue is empty at this moment. a zero-length * request for a stream device can still get enqueued if the * write callback returns 0 though i can't figure out if there - * is a compelling reason to do so + * is a compelling reason to do so */ goto enqueue_data; /* enqueue remaining data */ } @@ -1840,7 +1840,7 @@ static HIO_INLINE int __dev_writev (hio_dev_t* dev, hio_iovec_t* iov, hio_iolen_ } /* if i trigger the write completion callback here, the performance - * may increase, but there can be annoying recursion issues if the + * may increase, but there can be annoying recursion issues if the * callback requests another writing operation. it's imperative to * delay the callback until this write function is finished. * ---> if (dev->dev_evcb->on_write(dev, len, wrctx, dstaddr) <= -1) return -1; */ @@ -1858,7 +1858,7 @@ static HIO_INLINE int __dev_writev (hio_dev_t* dev, hio_iovec_t* iov, hio_iolen_ urem -= dcnt; /* partial writing is still considered ok for a non-stream device. */ - /* read the comment in the 'if' block above for why i enqueue the write completion event + /* read the comment in the 'if' block above for why i enqueue the write completion event * instead of calling the event callback here... * ---> if (dev->dev_evcb->on_write(dev, ulen, wrctx, dstaddr) <= -1) return -1; */ goto enqueue_completed_write; @@ -1895,9 +1895,9 @@ static int __dev_sendfile (hio_dev_t* dev, hio_syshnd_t in_fd, hio_foff_t foff, uoff = foff; urem = len; - if (!HIO_WQ_IS_EMPTY(&dev->wq)) + if (!HIO_WQ_IS_EMPTY(&dev->wq)) { - /* the writing queue is not empty. + /* the writing queue is not empty. * enqueue this request immediately */ goto enqueue_data; } @@ -1910,17 +1910,17 @@ static int __dev_sendfile (hio_dev_t* dev, hio_syshnd_t in_fd, hio_foff_t foff, ulen = urem; x = dev->dev_mth->sendfile(dev, in_fd, uoff, &ulen); if (x <= -1) return -1; - else if (x == 0) + else if (x == 0) { - /* [NOTE] - * the write queue is empty at this moment. a zero-length + /* [NOTE] + * the write queue is empty at this moment. a zero-length * request for a stream device can still get enqueued if the * write callback returns 0 though i can't figure out if there - * is a compelling reason to do so + * is a compelling reason to do so */ goto enqueue_data; /* enqueue remaining data */ } - else + else { /* the write callback should return at most the number of requested * bytes. but returning more is harmless as urem is of a signed type. @@ -1940,7 +1940,7 @@ static int __dev_sendfile (hio_dev_t* dev, hio_syshnd_t in_fd, hio_foff_t foff, } /* if i trigger the write completion callback here, the performance - * may increase, but there can be annoying recursion issues if the + * may increase, but there can be annoying recursion issues if the * callback requests another writing operation. it's imperative to * delay the callback until this write function is finished. * ---> if (dev->dev_evcb->on_write(dev, len, wrctx, dstaddr) <= -1) return -1; */ @@ -1996,8 +1996,8 @@ int hio_dev_timedsendfile (hio_dev_t* dev, hio_syshnd_t in_fd, hio_foff_t foff, void hio_gettime (hio_t* hio, hio_ntime_t* now) { hio_sys_gettime (hio, now); - /* in hio_init(), hio->init_time has been set to the initialization time. - * the time returned here gets offset by hio->init_time and + /* in hio_init(), hio->init_time has been set to the initialization time. + * the time returned here gets offset by hio->init_time and * thus becomes relative to it. this way, it is kept small such that it * can be represented in a small integer with leaving almost zero chance * of overflow. */ @@ -2062,9 +2062,9 @@ static int fmt_put_bchars_to_uch_buf (hio_fmtout_t* fmtout, const hio_bch_t* ptr ucslen = b->capa - b->len; n = hio_conv_bchars_to_uchars_with_cmgr(ptr, &bcslen, &b->ptr[b->len], &ucslen, (b->hio? b->hio->_cmgr: hio_get_utf8_cmgr()), 1); b->len += ucslen; - if (n <= -1) + if (n <= -1) { - if (n == -2) + if (n == -2) { return 0; /* buffer full. stop */ } @@ -2248,7 +2248,7 @@ hio_oow_t hio_dev_cap_to_bcstr (hio_bitmask_t cap, hio_bch_t* buf, hio_oow_t siz if (cap & HIO_DEV_CAP_WATCH_STARTED) len += hio_copy_bcstr(&buf[len], size - len, "watch_started|"); if (cap & HIO_DEV_CAP_WATCH_SUSPENDED) len += hio_copy_bcstr(&buf[len], size - len, "watch_suspended|"); if (cap & HIO_DEV_CAP_WATCH_REREG_REQUIRED) len += hio_copy_bcstr(&buf[len], size - len, "watch_rereg_required|"); - + if (buf[len - 1] == '|') buf[--len] = '\0'; return len; } diff --git a/lib/hio.h b/lib/hio.h index fe1418c..cbe65d7 100644 --- a/lib/hio.h +++ b/lib/hio.h @@ -175,7 +175,7 @@ typedef int (*hio_log_writer_t) ( hio_bitmask_t mask, const hio_bch_t* dptr, hio_oow_t dlen -); +); /* ========================================================================= */ @@ -187,7 +187,7 @@ typedef struct hio_tmrjob_t hio_tmrjob_t; typedef void (*hio_tmrjob_handler_t) ( hio_t* hio, - const hio_ntime_t* now, + const hio_ntime_t* now, hio_tmrjob_t* tmrjob ); @@ -205,22 +205,22 @@ struct hio_dev_mth_t { /* ------------------------------------------------------------------ */ /* mandatory. called in hio_dev_make() */ - int (*make) (hio_dev_t* dev, void* ctx); + int (*make) (hio_dev_t* dev, void* ctx); /* ------------------------------------------------------------------ */ /* mandatory. called in hio_dev_kill(). also called in hio_dev_make() upon * failure after make() success. - * + * * when 'force' is 0, the return value of -1 causes the device to be a * zombie. the kill method is called periodically on a zombie device * until the method returns 0. * * when 'force' is 1, the called should not return -1. If it does, the * method is called once more only with the 'force' value of 2. - * + * * when 'force' is 2, the device is destroyed regardless of the return value. */ - int (*kill) (hio_dev_t* dev, int force); + int (*kill) (hio_dev_t* dev, int force); /* optional. called if hio_dev_make() fails before the make() method is called */ void (*fail_before_make) (void* ctx); @@ -256,7 +256,7 @@ struct hio_dev_evcb_t * when 1 is returned, the main loop attempts to read more data without*/ int (*on_read) (hio_dev_t* dev, const void* data, hio_iolen_t len, const hio_devaddr_t* srcaddr); - /* return -1 on failure, 0 on success. + /* return -1 on failure, 0 on success. * wrlen is the length of data written. it is the length of the originally * posted writing request for a stream device. For a non stream device, it * may be shorter than the originally posted length. */ @@ -318,7 +318,7 @@ struct hio_cwq_t hio_cwq_t* q_next; hio_cwq_t* q_prev; - hio_iolen_t olen; + hio_iolen_t olen; void* ctx; hio_dev_t* dev; hio_devaddr_t dstaddr; @@ -382,7 +382,7 @@ struct hio_wq_t hio_wq_t wq; \ hio_oow_t cw_count; \ hio_dev_t* dev_prev; \ - hio_dev_t* dev_next + hio_dev_t* dev_next struct hio_dev_t { @@ -431,7 +431,7 @@ enum hio_dev_cap_t HIO_DEV_CAP_ALL_MASK = (HIO_DEV_CAP_VIRTUAL | HIO_DEV_CAP_IN | HIO_DEV_CAP_OUT | HIO_DEV_CAP_PRI | HIO_DEV_CAP_STREAM | HIO_DEV_CAP_IN_DISABLED | HIO_DEV_CAP_OUT_UNQUEUEABLE), /* ------------------------------------------------------------------- - * the followings bits are for internal use only. + * the followings bits are for internal use only. * never set these bits to the dev_cap field. * ------------------------------------------------------------------- */ HIO_DEV_CAP_IN_CLOSED = ((hio_bitmask_t)1 << 10), @@ -523,7 +523,7 @@ struct hio_cfmb_t #define HIO_CFMBL_PREV_CFMB(cfmb) ((cfmb)->cfmb_prev) #define HIO_CFMBL_NEXT_CFMB(cfmb) ((cfmb)->cfmb_next) /* ========================================================================= - * SERVICE + * SERVICE * ========================================================================= */ typedef void (*hio_svc_stop_t) (hio_svc_t* svc); @@ -532,12 +532,12 @@ typedef void (*hio_svc_stop_t) (hio_svc_t* svc); hio_t* hio; \ hio_svc_stop_t svc_stop; \ hio_svc_t* svc_prev; \ - hio_svc_t* svc_next + hio_svc_t* svc_next -/* the stop callback is called if it's not NULL and the service is still +/* the stop callback is called if it's not NULL and the service is still * alive when hio_close() is reached. it still calls HIO_SVCL_UNLINK_SVC() * if the stop callback is NULL. The stop callback, if specified, must - * call HIO_SVCL_UNLINK_SVC(). */ + * call HIO_SVCL_UNLINK_SVC(). */ struct hio_svc_t { @@ -833,7 +833,7 @@ static HIO_INLINE hio_errnum_t hio_geterrnum (hio_t* hio) { return hio->errnum; #endif HIO_EXPORT void hio_seterrnum ( - hio_t* hio, + hio_t* hio, hio_errnum_t errnum ); @@ -985,14 +985,14 @@ HIO_EXPORT int hio_dev_timedread ( ); /** - * The hio_dev_write() function posts a writing request. + * The hio_dev_write() function posts a writing request. * It attempts to write data immediately if there is no pending requests. * If writing fails, it returns -1. If writing succeeds, it calls the - * on_write callback. If the callback fails, it returns -1. If the callback + * on_write callback. If the callback fails, it returns -1. If the callback * succeeds, it returns 1. If no immediate writing is possible, the request * is enqueued to a pending request list. If enqueing gets successful, * it returns 0. otherwise it returns -1. - */ + */ HIO_EXPORT int hio_dev_write ( hio_dev_t* dev, const void* data, @@ -1045,7 +1045,7 @@ HIO_EXPORT int hio_dev_timedsendfile ( void* wrctx ); /* ========================================================================= - * SERVICE + * SERVICE * ========================================================================= */ #if defined(HIO_HAVE_INLINE) @@ -1190,7 +1190,7 @@ HIO_EXPORT int hio_convutobchars ( ); /** - * The hio_convbtoucstr() function converts a null-terminated byte string + * The hio_convbtoucstr() function converts a null-terminated byte string * to a wide string. */ HIO_EXPORT int hio_convbtoucstr ( @@ -1422,7 +1422,7 @@ HIO_EXPORT hio_ooi_t hio_logufmtv ( const hio_uch_t* fmt, va_list ap ); - + #if defined(HIO_OOCH_IS_UCH) # define hio_logoofmt hio_logufmt # define hio_logoofmtv hio_logufmtv diff --git a/lib/htb.c b/lib/htb.c index 1bd32ff..5e04f12 100644 --- a/lib/htb.c +++ b/lib/htb.c @@ -76,7 +76,7 @@ HIO_INLINE pair_t* hio_htb_allocpair (hio_htb_t* htb, void* kptr, hio_oow_t klen * the actual key area */ if (kptr) HIO_MEMCPY (KPTR(n), kptr, KTOB(htb,klen)); } - else + else { KPTR(n) = kcop(htb, kptr, klen); if (KPTR(n) == HIO_NULL) @@ -94,13 +94,13 @@ HIO_INLINE pair_t* hio_htb_allocpair (hio_htb_t* htb, void* kptr, hio_oow_t klen else if (vcop == HIO_HTB_COPIER_INLINE) { VPTR(n) = n + 1; - if (kcop == HIO_HTB_COPIER_INLINE) + if (kcop == HIO_HTB_COPIER_INLINE) VPTR(n) = (hio_uint8_t*)VPTR(n) + HIO_ALIGN_POW2(KTOB(htb,klen), HIO_SIZEOF_VOID_P); /* if vptr is HIO_NULL, the inline copier does not fill * the actual value area */ if (vptr) HIO_MEMCPY (VPTR(n), vptr, VTOB(htb,vlen)); } - else + else { VPTR(n) = vcop (htb, vptr, vlen); if (VPTR(n) != HIO_NULL) @@ -117,19 +117,19 @@ HIO_INLINE pair_t* hio_htb_allocpair (hio_htb_t* htb, void* kptr, hio_oow_t klen HIO_INLINE void hio_htb_freepair (hio_htb_t* htb, pair_t* pair) { - if (htb->style->freeer[HIO_HTB_KEY] != HIO_NULL) + if (htb->style->freeer[HIO_HTB_KEY] != HIO_NULL) htb->style->freeer[HIO_HTB_KEY] (htb, KPTR(pair), KLEN(pair)); if (htb->style->freeer[HIO_HTB_VAL] != HIO_NULL) htb->style->freeer[HIO_HTB_VAL] (htb, VPTR(pair), VLEN(pair)); - hio_freemem (htb->hio, pair); + hio_freemem (htb->hio, pair); } static HIO_INLINE pair_t* change_pair_val (hio_htb_t* htb, pair_t* pair, void* vptr, hio_oow_t vlen) { - if (VPTR(pair) == vptr && VLEN(pair) == vlen) + if (VPTR(pair) == vptr && VLEN(pair) == vlen) { /* if the old value and the new value are the same, - * it just calls the handler for this condition. + * it just calls the handler for this condition. * No value replacement occurs. */ if (htb->style->keeper != HIO_NULL) { @@ -163,7 +163,7 @@ static HIO_INLINE pair_t* change_pair_val (hio_htb_t* htb, pair_t* pair, void* v return p; } } - else + else { void* nvptr = vcop(htb, vptr, vlen); if (HIO_UNLIKELY(!nvptr)) return HIO_NULL; @@ -172,7 +172,7 @@ static HIO_INLINE pair_t* change_pair_val (hio_htb_t* htb, pair_t* pair, void* v } /* free up the old value */ - if (htb->style->freeer[HIO_HTB_VAL] != HIO_NULL) + if (htb->style->freeer[HIO_HTB_VAL] != HIO_NULL) { htb->style->freeer[HIO_HTB_VAL] (htb, ovptr, ovlen); } @@ -278,11 +278,11 @@ void hio_htb_close (hio_htb_t* htb) int hio_htb_init (hio_htb_t* htb, hio_t* hio, hio_oow_t capa, int factor, int kscale, int vscale) { - /* The initial capacity should be greater than 0. + /* The initial capacity should be greater than 0. * Otherwise, it is adjusted to 1 in the release mode */ HIO_ASSERT (hio, capa > 0); - /* The load factor should be between 0 and 100 inclusive. + /* The load factor should be between 0 and 100 inclusive. * In the release mode, a value out of the range is adjusted to 100 */ HIO_ASSERT (hio, factor >= 0 && factor <= 100); @@ -351,7 +351,7 @@ pair_t* hio_htb_search (const hio_htb_t* htb, const void* kptr, hio_oow_t klen) hc = htb->style->hasher(htb,kptr,klen) % htb->capa; pair = htb->bucket[hc]; - while (pair != HIO_NULL) + while (pair != HIO_NULL) { if (htb->style->comper(htb, KPTR(pair), KLEN(pair), kptr, klen) == 0) { @@ -374,9 +374,9 @@ static HIO_INLINE int reorganize (hio_htb_t* htb) { new_capa = htb->style->sizer (htb, htb->capa + 1); - /* if no change in capacity, return success + /* if no change in capacity, return success * without reorganization */ - if (new_capa == htb->capa) return 0; + if (new_capa == htb->capa) return 0; /* adjust to 1 if the new capacity is not reasonable */ if (new_capa <= 0) new_capa = 1; @@ -389,7 +389,7 @@ static HIO_INLINE int reorganize (hio_htb_t* htb) } new_buck = (pair_t**)hio_allocmem(htb->hio, new_capa * HIO_SIZEOF(pair_t*)); - if (HIO_UNLIKELY(!new_buck)) + if (HIO_UNLIKELY(!new_buck)) { /* reorganization is disabled once it fails */ htb->threshold = 0; @@ -403,7 +403,7 @@ static HIO_INLINE int reorganize (hio_htb_t* htb) { pair_t* pair = htb->bucket[i]; - while (pair != HIO_NULL) + while (pair != HIO_NULL) { pair_t* next = NEXT(pair); @@ -439,11 +439,11 @@ static HIO_INLINE pair_t* insert (hio_htb_t* htb, void* kptr, hio_oow_t klen, vo pair = htb->bucket[hc]; prev = HIO_NULL; - while (pair != HIO_NULL) + while (pair != HIO_NULL) { next = NEXT(pair); - if (htb->style->comper (htb, KPTR(pair), KLEN(pair), kptr, klen) == 0) + if (htb->style->comper (htb, KPTR(pair), KLEN(pair), kptr, klen) == 0) { /* found a pair with a matching key */ switch (opt) @@ -451,25 +451,25 @@ static HIO_INLINE pair_t* insert (hio_htb_t* htb, void* kptr, hio_oow_t klen, vo case UPSERT: case UPDATE: p = change_pair_val (htb, pair, vptr, vlen); - if (p == HIO_NULL) + if (p == HIO_NULL) { /* error in changing the value */ - return HIO_NULL; + return HIO_NULL; } - if (p != pair) + if (p != pair) { /* old pair destroyed. new pair reallocated. * relink to include the new pair but to drop * the old pair. */ if (prev == HIO_NULL) htb->bucket[hc] = p; else NEXT(prev) = p; - NEXT(p) = next; + NEXT(p) = next; } return p; case ENSERT: /* return existing pair */ - return pair; + return pair; case INSERT: /* return failure */ @@ -482,7 +482,7 @@ static HIO_INLINE pair_t* insert (hio_htb_t* htb, void* kptr, hio_oow_t klen, vo pair = next; } - if (opt == UPDATE) + if (opt == UPDATE) { hio_seterrnum (htb->hio, HIO_ENOENT); return HIO_NULL; @@ -492,7 +492,7 @@ static HIO_INLINE pair_t* insert (hio_htb_t* htb, void* kptr, hio_oow_t klen, vo { /* ingore reorganization error as it simply means * more bucket collision and performance penalty. */ - if (reorganize(htb) == 0) + if (reorganize(htb) == 0) { hc = htb->style->hasher(htb,kptr,klen) % htb->capa; } @@ -540,27 +540,27 @@ pair_t* hio_htb_cbsert (hio_htb_t* htb, void* kptr, hio_oow_t klen, cbserter_t c pair = htb->bucket[hc]; prev = HIO_NULL; - while (pair != HIO_NULL) + while (pair != HIO_NULL) { next = NEXT(pair); - if (htb->style->comper(htb, KPTR(pair), KLEN(pair), kptr, klen) == 0) + if (htb->style->comper(htb, KPTR(pair), KLEN(pair), kptr, klen) == 0) { /* found a pair with a matching key */ p = cbserter(htb, pair, kptr, klen, ctx); - if (p == HIO_NULL) + if (p == HIO_NULL) { /* error returned by the callback function */ - return HIO_NULL; + return HIO_NULL; } - if (p != pair) + if (p != pair) { /* old pair destroyed. new pair reallocated. * relink to include the new pair but to drop * the old pair. */ if (prev == HIO_NULL) htb->bucket[hc] = p; else NEXT(prev) = p; - NEXT(p) = next; + NEXT(p) = next; } return p; } @@ -600,11 +600,11 @@ int hio_htb_delete (hio_htb_t* htb, const void* kptr, hio_oow_t klen) pair = htb->bucket[hc]; prev = HIO_NULL; - while (pair != HIO_NULL) + while (pair != HIO_NULL) { - if (htb->style->comper(htb, KPTR(pair), KLEN(pair), kptr, klen) == 0) + if (htb->style->comper(htb, KPTR(pair), KLEN(pair), kptr, klen) == 0) { - if (prev == HIO_NULL) + if (prev == HIO_NULL) htb->bucket[hc] = NEXT(pair); else NEXT(prev) = NEXT(pair); @@ -627,11 +627,11 @@ void hio_htb_clear (hio_htb_t* htb) hio_oow_t i; pair_t* pair, * next; - for (i = 0; i < htb->capa; i++) + for (i = 0; i < htb->capa; i++) { pair = htb->bucket[i]; - while (pair != HIO_NULL) + while (pair != HIO_NULL) { next = NEXT(pair); hio_htb_freepair (htb, pair); @@ -648,11 +648,11 @@ void hio_htb_walk (hio_htb_t* htb, walker_t walker, void* ctx) hio_oow_t i; pair_t* pair, * next; - for (i = 0; i < htb->capa; i++) + for (i = 0; i < htb->capa; i++) { pair = htb->bucket[i]; - while (pair != HIO_NULL) + while (pair != HIO_NULL) { next = NEXT(pair); if (walker(htb, pair, ctx) == HIO_HTB_WALK_STOP) return; @@ -676,7 +676,7 @@ pair_t* hio_htb_getfirstpair (hio_htb_t* htb, hio_htb_itr_t* itr) for (i = 0; i < htb->capa; i++) { pair = htb->bucket[i]; - if (pair) + if (pair) { itr->buckno = i; itr->pair = pair; @@ -693,7 +693,7 @@ pair_t* hio_htb_getnextpair (hio_htb_t* htb, hio_htb_itr_t* itr) pair_t* pair; pair = NEXT(itr->pair); - if (pair) + if (pair) { /* no change in bucket number */ itr->pair = pair; @@ -703,7 +703,7 @@ pair_t* hio_htb_getnextpair (hio_htb_t* htb, hio_htb_itr_t* itr) for (i = itr->buckno + 1; i < htb->capa; i++) { pair = htb->bucket[i]; - if (pair) + if (pair) { itr->buckno = i; itr->pair = pair; @@ -718,7 +718,7 @@ hio_oow_t hio_htb_dflhash (const hio_htb_t* htb, const void* kptr, hio_oow_t kle { hio_oow_t h; HIO_HASH_BYTES (h, kptr, klen); - return h ; + return h ; } int hio_htb_dflcomp (const hio_htb_t* htb, const void* kptr1, hio_oow_t klen1, const void* kptr2, hio_oow_t klen2) diff --git a/lib/htrd.c b/lib/htrd.c index c4a152f..aeb32a1 100644 --- a/lib/htrd.c +++ b/lib/htrd.c @@ -88,7 +88,7 @@ static HIO_INLINE int xdigit_to_num (hio_bch_t c) static HIO_INLINE int push_to_buffer (hio_htrd_t* htrd, hio_becs_t* octb, const hio_bch_t* ptr, hio_oow_t len) { - if (hio_becs_ncat(octb, ptr, len) == (hio_oow_t)-1) + if (hio_becs_ncat(octb, ptr, len) == (hio_oow_t)-1) { htrd->errnum = HIO_HTRD_ENOMEM; return -1; @@ -102,20 +102,20 @@ static HIO_INLINE int push_content (hio_htrd_t* htrd, const hio_bch_t* ptr, hio_ if (htrd->recbs.push_content) return htrd->recbs.push_content(htrd, &htrd->re, ptr, len); - if (hio_htre_addcontent(&htrd->re, ptr, len) <= -1) + if (hio_htre_addcontent(&htrd->re, ptr, len) <= -1) { htrd->errnum = HIO_HTRD_ENOMEM; return -1; } - /* hio_htre_addcontent() returns 1 on full success and 0 if adding is + /* hio_htre_addcontent() returns 1 on full success and 0 if adding is * skipped. i treat both as success */ return 0; } static HIO_INLINE void clear_feed (hio_htrd_t* htrd) { - /* clear necessary part of the request/response before + /* clear necessary part of the request/response before * reading the next request/response */ htrd->clean = 1; hio_htre_clear (&htrd->re); @@ -250,7 +250,7 @@ static hio_bch_t* parse_initial_line (hio_htrd_t* htrd, hio_bch_t* line) /* skip spaces */ do p++; while (is_space_octet(*p)); - + n = digit_to_num(*p); if (n <= -1) goto badre; @@ -260,7 +260,7 @@ static hio_bch_t* parse_initial_line (hio_htrd_t* htrd, hio_bch_t* line) { status = status * 10 + n; p++; - } + } while ((n = digit_to_num(*p)) >= 0); if (!is_space_octet(*p)) goto badre; @@ -275,15 +275,15 @@ static hio_bch_t* parse_initial_line (hio_htrd_t* htrd, hio_bch_t* line) /* skip spaces */ do p++; while (is_space_octet(*p)); - + tmp.ptr = p; tmp.len = 0; - while (*p != '\0' && *p != '\n') + while (*p != '\0' && *p != '\n') { if (!is_space_octet(*p)) tmp.len = p - tmp.ptr + 1; p++; } - + /* if the line does not end with a new line, it is a bad request */ if (*p != '\n') goto badre; @@ -309,7 +309,7 @@ static hio_bch_t* parse_initial_line (hio_htrd_t* htrd, hio_bch_t* line) if (HIO_UNLIKELY(*p == '\0')) goto badre; else if (is_space_octet(*p) || *p == '?' || *p == '#') { - tmp.len = p - tmp.ptr; + tmp.len = p - tmp.ptr; if (tmp.len <= 0) goto badre; break; } @@ -373,10 +373,10 @@ static hio_bch_t* parse_initial_line (hio_htrd_t* htrd, hio_bch_t* line) htrd->re.u.q.path.len = hio_canon_bcstr_path(qpath, qpath, 0); } } - + /* skip spaces after the url part */ do { p++; } while (is_space_octet(*p)); - + tmp.ptr = p; /* check protocol version */ if ((p[0] == 'H' || p[0] == 'h') && @@ -396,7 +396,7 @@ static hio_bch_t* parse_initial_line (hio_htrd_t* htrd, hio_bch_t* line) else goto badre; } else goto badre; - + tmp.len = p - tmp.ptr; /* skip trailing spaces on the line */ @@ -408,10 +408,10 @@ static hio_bch_t* parse_initial_line (hio_htrd_t* htrd, hio_bch_t* line) ((hio_bch_t*)tmp.ptr)[tmp.len] = '\0'; htrd->re.verstr = tmp.ptr; } - + /* adjust Connection: Keep-Alive for HTTP 1.1 or later. * this is initial. it can be adjusted further in capture_connection(). */ - if (htrd->re.version.major > 1 || + if (htrd->re.version.major > 1 || (htrd->re.version.major == 1 && htrd->re.version.minor >= 1)) { htrd->re.flags |= HIO_HTRE_ATTR_KEEPALIVE; @@ -462,7 +462,7 @@ static int capture_connection (hio_htrd_t* htrd, hio_htb_pair_t* pair) val = HIO_HTB_VPTR(pair); while (val->next) val = val->next; - /* The value for Connection: may get comma-separated. + /* The value for Connection: may get comma-separated. * so use hio_find_bcstr_word_in_bcstr() instead of hio_comp_bcstr(). */ if (hio_find_bcstr_word_in_bcstr(val->ptr, "close", ',', 1)) @@ -535,7 +535,7 @@ static int capture_content_length (hio_htrd_t* htrd, hio_htb_pair_t* pair) if ((htrd->re.flags & HIO_HTRE_ATTR_CHUNKED) && len > 0) { - /* content-length is greater than 0 + /* content-length is greater than 0 * while transfer-encoding: chunked is specified. */ htrd->errnum = HIO_HTRD_EBADRE; return -1; @@ -551,13 +551,13 @@ static int capture_expect (hio_htrd_t* htrd, hio_htb_pair_t* pair) hio_htre_hdrval_t* val; /* Expect is included */ - htrd->re.flags |= HIO_HTRE_ATTR_EXPECT; + htrd->re.flags |= HIO_HTRE_ATTR_EXPECT; val = HIO_HTB_VPTR(pair); - while (val) + while (val) { /* Expect: 100-continue is included */ - if (hio_comp_bcstr(val->ptr, "100-continue", 1) == 0) htrd->re.flags |= HIO_HTRE_ATTR_EXPECT100; + if (hio_comp_bcstr(val->ptr, "100-continue", 1) == 0) htrd->re.flags |= HIO_HTRE_ATTR_EXPECT100; val = val->next; } @@ -610,7 +610,7 @@ static HIO_INLINE int capture_key_header (hio_htrd_t* htrd, hio_htb_pair_t* pair const hio_bch_t* ptr; hio_oow_t len; int (*handler) (hio_htrd_t*, hio_htb_pair_t*); - } hdrtab[] = + } hdrtab[] = { { "Connection", 10, capture_connection }, { "Content-Length", 14, capture_content_length }, @@ -649,7 +649,7 @@ struct hdr_cbserter_ctx_t }; static hio_htb_pair_t* hdr_cbserter ( - hio_htb_t* htb, hio_htb_pair_t* pair, + hio_htb_t* htb, hio_htb_pair_t* pair, void* kptr, hio_oow_t klen, void* ctx) { struct hdr_cbserter_ctx_t* tx = (struct hdr_cbserter_ctx_t*)ctx; @@ -657,7 +657,7 @@ static hio_htb_pair_t* hdr_cbserter ( if (pair == HIO_NULL) { /* the key is new. let's create a new pair. */ - hio_htb_pair_t* p; + hio_htb_pair_t* p; hio_htre_hdrval_t *val; val = hio_allocmem(htb->hio, HIO_SIZEOF(*val)); @@ -673,12 +673,12 @@ static hio_htb_pair_t* hdr_cbserter ( val->next = HIO_NULL; p = hio_htb_allocpair(htb, kptr, klen, val, 0); - if (HIO_UNLIKELY(!p)) + if (HIO_UNLIKELY(!p)) { hio_freemem (htb->hio, val); tx->htrd->errnum = HIO_HTRD_ENOMEM; } - else + else { if (capture_key_header(tx->htrd, p) <= -1) { @@ -693,30 +693,30 @@ static hio_htb_pair_t* hdr_cbserter ( } else { - /* RFC2616 - * Multiple message-header fields with the same field-name - * MAY be present in a message if and only if the entire - * field-value for that header field is defined as a - * comma-separated list [i.e., #(values)]. It MUST be possible - * to combine the multiple header fields into one + /* RFC2616 + * Multiple message-header fields with the same field-name + * MAY be present in a message if and only if the entire + * field-value for that header field is defined as a + * comma-separated list [i.e., #(values)]. It MUST be possible + * to combine the multiple header fields into one * "field-name: field-value" pair, without changing the semantics - * of the message, by appending each subsequent field-value - * to the first, each separated by a comma. The order in which + * of the message, by appending each subsequent field-value + * to the first, each separated by a comma. The order in which * header fields with the same field-name are received is therefore * significant to the interpretation of the combined field value, - * and thus a proxy MUST NOT change the order of these field values - * when a message is forwarded. + * and thus a proxy MUST NOT change the order of these field values + * when a message is forwarded. * RFC6265 defines the syntax for Set-Cookie and Cookie. * this seems to be conflicting with RFC2616. - * - * Origin servers SHOULD NOT fold multiple Set-Cookie header fields - * into a single header field. The usual mechanism for folding HTTP - * headers fields (i.e., as defined in [RFC2616]) might change the + * + * Origin servers SHOULD NOT fold multiple Set-Cookie header fields + * into a single header field. The usual mechanism for folding HTTP + * headers fields (i.e., as defined in [RFC2616]) might change the * semantics of the Set-Cookie header field because the %x2C (",") - * character is used by Set-Cookie in a way that conflicts with + * character is used by Set-Cookie in a way that conflicts with * such folding. - * + * * So i just maintain the list of values for a key instead of * folding them. */ @@ -743,7 +743,7 @@ static hio_htb_pair_t* hdr_cbserter ( /* find the tail */ while (tmp->next) tmp = tmp->next; /* append it to the list*/ - tmp->next = val; + tmp->next = val; if (capture_key_header(tx->htrd, pair) <= -1) return HIO_NULL; return pair; @@ -774,12 +774,12 @@ hio_bch_t* parse_header_field (hio_htrd_t* htrd, hio_bch_t* line, hio_htb_t* tab } name.len = last - name.ptr; - if (*p != ':') + if (*p != ':') { if (!(htrd->option & HIO_HTRD_STRICT)) { while (is_space_octet(*p)) p++; - if (*p == '\n') + if (*p == '\n') { /* ignore a line without a colon */ p++; @@ -802,13 +802,13 @@ hio_bch_t* parse_header_field (hio_htrd_t* htrd, hio_bch_t* line, hio_htb_t* tab value.len = last - value.ptr; if (*p != '\n') goto badhdr; /* not ending with a new line */ - /* peep at the beginning of the next line to check if it is + /* peep at the beginning of the next line to check if it is * the continuation */ if (is_purespace_octet (*++p)) { - /* RFC: HTTP/1.0 headers may be folded onto multiple lines if - * each continuation line begins with a space or horizontal tab. - * All linear whitespace, including folding, has the same semantics + /* RFC: HTTP/1.0 headers may be folded onto multiple lines if + * each continuation line begins with a space or horizontal tab. + * All linear whitespace, including folding, has the same semantics * as SP. */ hio_bch_t* cpydst; @@ -816,14 +816,14 @@ hio_bch_t* parse_header_field (hio_htrd_t* htrd, hio_bch_t* line, hio_htb_t* tab if (*(cpydst-1) == '\r') cpydst--; /* process all continued lines */ - do + do { while (*p != '\0' && *p != '\n') { - *cpydst = *p++; + *cpydst = *p++; if (!is_space_octet(*cpydst++)) last = cpydst; - } - + } + value.len = last - value.ptr; if (*p != '\n') goto badhdr; @@ -843,10 +843,10 @@ hio_bch_t* parse_header_field (hio_htrd_t* htrd, hio_bch_t* line, hio_htb_t* tab htrd->errnum = HIO_HTRD_ENOERR; if (hio_htb_cbsert ( - tab, name.ptr, name.len, + tab, name.ptr, name.len, hdr_cbserter, &ctx) == HIO_NULL) { - if (htrd->errnum == HIO_HTRD_ENOERR) + if (htrd->errnum == HIO_HTRD_ENOERR) htrd->errnum = HIO_HTRD_ENOMEM; return HIO_NULL; } @@ -877,7 +877,7 @@ static HIO_INLINE int parse_initial_line_and_headers (hio_htrd_t* htrd, const hi else #endif while (is_space_octet(*p)) p++; - + HIO_ASSERT (htrd->hio, *p != '\0'); /* parse the initial line */ @@ -940,7 +940,7 @@ static const hio_bch_t* getchunklen (hio_htrd_t* htrd, const hio_bch_t* ptr, hio if (ptr < end) { - if (*ptr == '\n') + if (*ptr == '\n') { /* the chunk length line ended properly */ @@ -984,13 +984,13 @@ static const hio_bch_t* get_trailing_headers (hio_htrd_t* htrd, const hio_bch_t* switch (b) { case '\0': - /* guarantee that the request does not contain a null + /* guarantee that the request does not contain a null * character */ htrd->errnum = HIO_HTRD_EBADRE; return HIO_NULL; case '\n': - if (htrd->fed.s.crlf <= 1) + if (htrd->fed.s.crlf <= 1) { htrd->fed.s.crlf = 2; break; @@ -1025,7 +1025,7 @@ static const hio_bch_t* get_trailing_headers (hio_htrd_t* htrd, const hio_bch_t* } case '\r': - if (htrd->fed.s.crlf == 0 || htrd->fed.s.crlf == 2) + if (htrd->fed.s.crlf == 0 || htrd->fed.s.crlf == 2) htrd->fed.s.crlf++; else htrd->fed.s.crlf = 1; break; @@ -1037,7 +1037,7 @@ static const hio_bch_t* get_trailing_headers (hio_htrd_t* htrd, const hio_bch_t* } } - if (push_to_buffer (htrd, &htrd->fed.b.tra, req, ptr - req) <= -1) + if (push_to_buffer (htrd, &htrd->fed.b.tra, req, ptr - req) <= -1) return HIO_NULL; done: @@ -1049,7 +1049,9 @@ int hio_htrd_feed (hio_htrd_t* htrd, const hio_bch_t* req, hio_oow_t len, hio_oo { const hio_bch_t* end = req + len; const hio_bch_t* ptr = req; +#if 0 int header_completed_during_this_feed = 0; +#endif hio_oow_t avail; HIO_ASSERT (htrd->hio, len > 0); @@ -1069,9 +1071,9 @@ int hio_htrd_feed (hio_htrd_t* htrd, const hio_bch_t* req, hio_oow_t len, hio_oo } /* does this goto drop code maintainability? */ - if (htrd->fed.s.need > 0) + if (htrd->fed.s.need > 0) { - /* we're in need of as many octets as htrd->fed.s.need + /* we're in need of as many octets as htrd->fed.s.need * for contents body. make a proper jump to resume * content handling */ goto content_resume; @@ -1083,7 +1085,7 @@ int hio_htrd_feed (hio_htrd_t* htrd, const hio_bch_t* req, hio_oow_t len, hio_oo goto dechunk_resume; case GET_CHUNK_DATA: - /* this won't be reached as htrd->fed.s.need + /* this won't be reached as htrd->fed.s.need * is greater than 0 if GET_CHUNK_DATA is true */ goto content_resume; @@ -1102,7 +1104,7 @@ int hio_htrd_feed (hio_htrd_t* htrd, const hio_bch_t* req, hio_oow_t len, hio_oo #if 0 if (htrd->option & HIO_HTRD_SKIP_EMPTY_LINES && - htrd->fed.s.plen <= 0 && is_whspace_octet(b)) + htrd->fed.s.plen <= 0 && is_whspace_octet(b)) { /* let's drop leading whitespaces across multiple * lines */ @@ -1121,13 +1123,13 @@ int hio_htrd_feed (hio_htrd_t* htrd, const hio_bch_t* req, hio_oow_t len, hio_oo case '\n': { - if (htrd->fed.s.crlf <= 1) + if (htrd->fed.s.crlf <= 1) { /* htrd->fed.s.crlf == 0 * => CR was not seen * htrd->fed.s.crlf == 1 - * => CR was seen - * whatever the current case is, + * => CR was seen + * whatever the current case is, * mark the first LF is seen here. */ htrd->fed.s.crlf = 2; @@ -1148,18 +1150,20 @@ int hio_htrd_feed (hio_htrd_t* htrd, const hio_bch_t* req, hio_oow_t len, hio_oo /* reset the raw request length */ htrd->fed.s.plen = 0; - if (parse_initial_line_and_headers(htrd, req, ptr - req) <= -1) + if (parse_initial_line_and_headers(htrd, req, ptr - req) <= -1) { return -1; } +#if 0 /* compelete request header is received */ header_completed_during_this_feed = 1; +#endif //////////////////////////////////////////////////////////////////////////////////////////////////// if (htrd->recbs.peek(htrd, &htrd->re) <= -1) { - /* need to clear request on error? + /* need to clear request on error? clear_feed (htrd); */ return -1; } @@ -1190,7 +1194,7 @@ int hio_htrd_feed (hio_htrd_t* htrd, const hio_bch_t* req, hio_oow_t len, hio_oo { /* this state is reached after the * last chunk length 0 is read. The next - * empty line immediately completes + * empty line immediately completes * a content body. so i need to adjust * this crlf status to 2 as if a trailing * header line has been read. */ @@ -1212,32 +1216,32 @@ int hio_htrd_feed (hio_htrd_t* htrd, const hio_bch_t* req, hio_oow_t len, hio_oo { /* we need to read as many octets as * Content-Length */ - if ((htrd->option & HIO_HTRD_RESPONSE) && + if ((htrd->option & HIO_HTRD_RESPONSE) && !(htrd->re.flags & HIO_HTRE_ATTR_LENGTH) && !(htrd->re.flags & HIO_HTRE_ATTR_KEEPALIVE)) { - /* for a response, no content-length and - * no chunk are specified and 'connection' - * is to close. i must read until the - * connection is closed. however, there isn't - * any good way to know when to stop from + /* for a response, no content-length and + * no chunk are specified and 'connection' + * is to close. i must read until the + * connection is closed. however, there isn't + * any good way to know when to stop from * within this function. so the caller * can call hio_htrd_halt() for this. */ /* set this to the maximum in a type safe way * assuming it's unsigned. the problem of - * the current implementation is that + * the current implementation is that * it can't receive more than */ htrd->fed.s.need = 0; - htrd->fed.s.need = ~htrd->fed.s.need; + htrd->fed.s.need = ~htrd->fed.s.need; htrd->fed.s.flags |= CONSUME_UNTIL_CLOSE; } else if ((htrd->option & HIO_HTRD_RESPONSE) && !(htrd->re.flags & HIO_HTRE_ATTR_LENGTH) && (htrd->re.flags & HIO_HTRE_ATTR_KEEPALIVE)) { - /* - * what the hell! + /* + * what the hell! * no content-length, but keep-alive and not chunked. * there's no way to know how large the contents is. * @@ -1246,7 +1250,7 @@ int hio_htrd_feed (hio_htrd_t* htrd, const hio_bch_t* req, hio_oow_t len, hio_oo * Accept-Encoding: gzip, deflate * * the service gave this response as of this writing: - * + * HTTP/1.1 304 Not Modified Server: nginx/1.6.2 Date: Tue, 04 Nov 2014 15:45:46 GMT @@ -1256,7 +1260,7 @@ Set-Cookie: LAST_LANG=en; expires=Wed, 04-Nov-2015 15:45:46 GMT; Max-Age=3153600 Set-Cookie: COUNTRY=KOR%2C220.121.110.171; expires=Tue, 11-Nov-2014 15:45:46 GMT; Max-Age=604800; path=/; domain=.php.net XXXXXXXX - * + * * XXXXXXX is some compressed garbage included in the contents-body. * why does the service behave this way? is it a server bug or am i doing anything wrong? * @@ -1276,7 +1280,7 @@ XXXXXXXX if (htrd->fed.s.need > 0) { - /* content-length or chunked data length + /* content-length or chunked data length * specified */ content_resume: avail = end - ptr; @@ -1285,41 +1289,41 @@ XXXXXXXX /* we didn't get a complete content yet */ /* avail can be 0 if data fed ends with - * a chunk length withtout actual data. + * a chunk length withtout actual data. * so i check if avail is greater than 0 * in order not to push empty content. */ - goto feedme_more; + goto feedme_more; } else if (avail < htrd->fed.s.need) { /* the data is not as large as needed */ - if (push_content(htrd, ptr, avail) <= -1) + if (push_content(htrd, ptr, avail) <= -1) { return -1; } - if (!(htrd->fed.s.flags & CONSUME_UNTIL_CLOSE)) + if (!(htrd->fed.s.flags & CONSUME_UNTIL_CLOSE)) { /* i don't decrement htrd->fed.s.need * if i should read until connection is closed. * well, unless set your own callback, - * push_content() above will fail + * push_content() above will fail * if too much has been received already */ htrd->fed.s.need -= avail; } /* we didn't get a complete content yet */ - goto feedme_more; + goto feedme_more; } - else + else { /* we got all or more than needed */ - if (push_content (htrd, ptr, htrd->fed.s.need) <= -1) + if (push_content (htrd, ptr, htrd->fed.s.need) <= -1) { return -1; } ptr += htrd->fed.s.need; - if (!(htrd->fed.s.flags & CONSUME_UNTIL_CLOSE)) + if (!(htrd->fed.s.flags & CONSUME_UNTIL_CLOSE)) htrd->fed.s.need = 0; } } @@ -1333,17 +1337,17 @@ XXXXXXXX while (ptr < end && is_space_octet(*ptr)) ptr++; if (ptr < end) { - if (*ptr == '\n') + if (*ptr == '\n') { /* end of chunk data. */ ptr++; - /* more octets still available. - * let it decode the next chunk + /* more octets still available. + * let it decode the next chunk */ - if (ptr < end) goto dechunk_start; + if (ptr < end) goto dechunk_start; - /* no more octets available after + /* no more octets available after * chunk data. the chunk state variables * need to be reset when a jump is made * to dechunk_resume upon the next call @@ -1377,13 +1381,13 @@ XXXXXXXX /* the peek handler has not been executed. * this can happen if this function is fed with * at least the ending part of a complete header - * plus complete content body and the header + * plus complete content body and the header * of the next request. */ int n; n = htrd->recbs.peek(htrd, &htrd->re); if (n <= -1) { - /* need to clear request on error? + /* need to clear request on error? clear_feed (htrd); */ return -1; } @@ -1398,20 +1402,20 @@ XXXXXXXX n = htrd->recbs.poke(htrd, &htrd->re); if (n <= -1) { - /* need to clear request on error? + /* need to clear request on error? clear_feed (htrd); */ return -1; } } #if 0 -hio_printf (HIO_T("CONTENT_LENGTH %d, RAW HEADER LENGTH %d\n"), +hio_printf (HIO_T("CONTENT_LENGTH %d, RAW HEADER LENGTH %d\n"), (int)HIO_BECS_LEN(&htrd->re.content), (int)HIO_BECS_LEN(&htrd->fed.b.raw)); #endif clear_feed (htrd); - if (rem) + if (rem) { /* stop even if there are fed data left */ *rem = end - ptr; /* remaining feeds */ @@ -1440,29 +1444,29 @@ hio_printf (HIO_T("CONTENT_LENGTH %d, RAW HEADER LENGTH %d\n"), return 0; } - /* let ptr point to the next character to LF or + /* let ptr point to the next character to LF or * the optional contents */ - req = ptr; + req = ptr; /* since there are more to handle, i mark that * htrd is in need of some data. this may - * not be really compatible with SKIP_EMPTY_LINES. + * not be really compatible with SKIP_EMPTY_LINES. * SHOULD I simply remove the option? */ - htrd->clean = 0; + htrd->clean = 0; } break; } case '\r': - if (htrd->fed.s.crlf == 0 || htrd->fed.s.crlf == 2) + if (htrd->fed.s.crlf == 0 || htrd->fed.s.crlf == 2) htrd->fed.s.crlf++; else htrd->fed.s.crlf = 1; break; default: - /* increment length of a request in raw + /* increment length of a request in raw * excluding crlf */ - htrd->fed.s.plen++; + htrd->fed.s.plen++; /* mark that neither CR nor LF was seen */ htrd->fed.s.crlf = 0; } @@ -1471,7 +1475,7 @@ hio_printf (HIO_T("CONTENT_LENGTH %d, RAW HEADER LENGTH %d\n"), if (ptr > req) { /* enbuffer the incomplete request */ - if (push_to_buffer(htrd, &htrd->fed.b.raw, req, ptr - req) <= -1) + if (push_to_buffer(htrd, &htrd->fed.b.raw, req, ptr - req) <= -1) { return -1; } @@ -1485,7 +1489,7 @@ feedme_more: n = htrd->recbs.peek(htrd, &htrd->re); if (n <= -1) { - /* need to clear request on error? + /* need to clear request on error? clear_feed (htrd); */ return -1; } @@ -1508,7 +1512,7 @@ int hio_htrd_halt (hio_htrd_t* htrd) n = htrd->recbs.poke(htrd, &htrd->re); if (n <= -1) { - /* need to clear request on error? + /* need to clear request on error? clear_feed (htrd); */ return -1; } diff --git a/lib/htre.c b/lib/htre.c index a97b0bc..006b757 100644 --- a/lib/htre.c +++ b/lib/htre.c @@ -84,7 +84,7 @@ void hio_htre_fini (hio_htre_t* re) hio_htb_fini (&re->trailers); hio_htb_fini (&re->hdrtab); - if (re->orgqpath.buf) + if (re->orgqpath.buf) { hio_freemem (re->hio, re->orgqpath.buf); re->orgqpath.buf = HIO_NULL; @@ -96,7 +96,7 @@ void hio_htre_fini (hio_htre_t* re) void hio_htre_clear (hio_htre_t* re) { - if (!(re->state & HIO_HTRE_COMPLETED) && + if (!(re->state & HIO_HTRE_COMPLETED) && !(re->state & HIO_HTRE_DISCARDED)) { if (re->concb) @@ -119,7 +119,7 @@ void hio_htre_clear (hio_htre_t* re) hio_htb_clear (&re->trailers); hio_becs_clear (&re->content); -#if 0 +#if 0 hio_becs_clear (&re->iniline); #endif } @@ -151,7 +151,7 @@ struct header_walker_ctx_t static hio_htb_walk_t walk_headers (hio_htb_t* htb, hio_htb_pair_t* pair, void* ctx) { struct header_walker_ctx_t* hwctx = (struct header_walker_ctx_t*)ctx; - if (hwctx->walker (hwctx->re, HIO_HTB_KPTR(pair), HIO_HTB_VPTR(pair), hwctx->ctx) <= -1) + if (hwctx->walker (hwctx->re, HIO_HTB_KPTR(pair), HIO_HTB_VPTR(pair), hwctx->ctx) <= -1) { hwctx->ret = -1; return HIO_HTB_WALK_STOP; @@ -187,7 +187,7 @@ int hio_htre_addcontent (hio_htre_t* re, const hio_bch_t* ptr, hio_oow_t len) if (re->state & (HIO_HTRE_COMPLETED | HIO_HTRE_DISCARDED)) return 0; /* skipped */ - if (re->concb) + if (re->concb) { /* if the callback is set, the content goes to the callback. */ if (re->concb(re, ptr, len, re->concb_ctx) <= -1) return -1; @@ -205,14 +205,14 @@ void hio_htre_completecontent (hio_htre_t* re) { /* see comments in hio_htre_discardcontent() */ - if (!(re->state & HIO_HTRE_COMPLETED) && + if (!(re->state & HIO_HTRE_COMPLETED) && !(re->state & HIO_HTRE_DISCARDED)) { re->state |= HIO_HTRE_COMPLETED; if (re->concb) { /* indicate end of content */ - re->concb (re, HIO_NULL, 0, re->concb_ctx); + re->concb (re, HIO_NULL, 0, re->concb_ctx); } } } @@ -220,7 +220,7 @@ void hio_htre_completecontent (hio_htre_t* re) void hio_htre_discardcontent (hio_htre_t* re) { /* you can't discard this if it's completed. - * you can't complete this if it's discarded + * you can't complete this if it's discarded * you can't add contents to this if it's completed or discarded */ @@ -233,8 +233,8 @@ void hio_htre_discardcontent (hio_htre_t* re) * hio_htre_discardcontent()... <-- POINT A. * * at point A, the content must contain something - * and concb is also set. for simplicity, - * clear the content buffer and invoke the callback + * and concb is also set. for simplicity, + * clear the content buffer and invoke the callback * * likewise, you may produce many weird combinations * of these functions. however, these functions are @@ -245,7 +245,7 @@ void hio_htre_discardcontent (hio_htre_t* re) if (re->concb) { /* indicate end of content */ - re->concb (re, HIO_NULL, 0, re->concb_ctx); + re->concb (re, HIO_NULL, 0, re->concb_ctx); } } } @@ -307,9 +307,9 @@ int hio_htre_perdecqpath (hio_htre_t* re) } re->u.q.path.len = hio_perdec_http_bcstr(re->u.q.path.ptr, re->u.q.path.ptr, &dec_count); - if (dec_count > 0) + if (dec_count > 0) { - /* this assertion is to ensure that hio_is_perenced_http_bstr() + /* this assertion is to ensure that hio_is_perenced_http_bstr() * returned true when dec_count is greater than 0 */ HIO_ASSERT (re->hio, re->orgqpath.buf != HIO_NULL); HIO_ASSERT (re->hio, re->orgqpath.ptr != HIO_NULL); @@ -325,7 +325,7 @@ int hio_htre_getreqcontentlen (hio_htre_t* req, hio_oow_t* len) if (req->flags & HIO_HTRE_ATTR_CHUNKED) { - /* "Transfer-Encoding: chunked" take precedence over "Content-Length: XXX". + /* "Transfer-Encoding: chunked" take precedence over "Content-Length: XXX". * * [RFC7230] * If a message is received with both a Transfer-Encoding and a diff --git a/lib/http-cgi.c b/lib/http-cgi.c index af5c03e..652bdea 100644 --- a/lib/http-cgi.c +++ b/lib/http-cgi.c @@ -107,7 +107,7 @@ static int cgi_write_to_client (cgi_t* cgi, const void* data, hio_iolen_t dlen) cgi->ever_attempted_to_write_to_client = 1; cgi->num_pending_writes_to_client++; - if (hio_dev_sck_write(cgi->client->sck, data, dlen, HIO_NULL, HIO_NULL) <= -1) + if (hio_dev_sck_write(cgi->client->sck, data, dlen, HIO_NULL, HIO_NULL) <= -1) { cgi->num_pending_writes_to_client--; return -1; @@ -126,7 +126,7 @@ static int cgi_writev_to_client (cgi_t* cgi, hio_iovec_t* iov, hio_iolen_t iovcn cgi->ever_attempted_to_write_to_client = 1; cgi->num_pending_writes_to_client++; - if (hio_dev_sck_writev(cgi->client->sck, iov, iovcnt, HIO_NULL, HIO_NULL) <= -1) + if (hio_dev_sck_writev(cgi->client->sck, iov, iovcnt, HIO_NULL, HIO_NULL) <= -1) { cgi->num_pending_writes_to_client--; return -1; @@ -177,7 +177,7 @@ static int cgi_write_last_chunk_to_client (cgi_t* cgi) static int cgi_write_to_peer (cgi_t* cgi, const void* data, hio_iolen_t dlen) { cgi->num_pending_writes_to_peer++; - if (hio_dev_pro_write(cgi->peer, data, dlen, HIO_NULL) <= -1) + if (hio_dev_pro_write(cgi->peer, data, dlen, HIO_NULL) <= -1) { cgi->num_pending_writes_to_peer--; return -1; @@ -203,7 +203,7 @@ static HIO_INLINE void cgi_mark_over (cgi_t* cgi, int over_bits) if (!(old_over & CGI_OVER_READ_FROM_CLIENT) && (cgi->over & CGI_OVER_READ_FROM_CLIENT)) { - if (hio_dev_sck_read(cgi->client->sck, 0) <= -1) + if (hio_dev_sck_read(cgi->client->sck, 0) <= -1) { HIO_DEBUG2 (cgi->htts->hio, "HTTS(%p) - halting client(%p) for failure to disable input watching\n", cgi->htts, cgi->client->sck); hio_dev_sck_halt (cgi->client->sck); @@ -212,7 +212,7 @@ static HIO_INLINE void cgi_mark_over (cgi_t* cgi, int over_bits) if (!(old_over & CGI_OVER_READ_FROM_PEER) && (cgi->over & CGI_OVER_READ_FROM_PEER)) { - if (cgi->peer && hio_dev_pro_read(cgi->peer, HIO_DEV_PRO_OUT, 0) <= -1) + if (cgi->peer && hio_dev_pro_read(cgi->peer, HIO_DEV_PRO_OUT, 0) <= -1) { HIO_DEBUG2 (cgi->htts->hio, "HTTS(%p) - halting peer(%p) for failure to disable input watching\n", cgi->htts, cgi->peer); hio_dev_pro_halt (cgi->peer); @@ -222,7 +222,7 @@ static HIO_INLINE void cgi_mark_over (cgi_t* cgi, int over_bits) if (old_over != CGI_OVER_ALL && cgi->over == CGI_OVER_ALL) { /* ready to stop */ - if (cgi->peer) + if (cgi->peer) { HIO_DEBUG2 (cgi->htts->hio, "HTTS(%p) - halting peer(%p) as it is unneeded\n", cgi->htts, cgi->peer); hio_dev_pro_halt (cgi->peer); @@ -234,7 +234,7 @@ static HIO_INLINE void cgi_mark_over (cgi_t* cgi, int over_bits) HIO_ASSERT (cgi->htts->hio, cgi->client->rsrc == (hio_svc_htts_rsrc_t*)cgi); /*printf ("DETACHING FROM THE MAIN CLIENT RSRC... state -> %p\n", cgi->client->rsrc);*/ - HIO_SVC_HTTS_RSRC_DETACH (cgi->client->rsrc); + HIO_SVC_HTTS_RSRC_UNREF (cgi->client->rsrc); /* cgi must not be accessed from here down as it could have been destroyed */ } else @@ -246,8 +246,9 @@ static HIO_INLINE void cgi_mark_over (cgi_t* cgi, int over_bits) } } -static void cgi_on_kill (cgi_t* cgi) +static void cgi_on_kill (hio_svc_htts_rsrc_t* rsrc) { + cgi_t* cgi = (cgi_t*)rsrc; hio_t* hio = cgi->htts->hio; HIO_DEBUG2 (hio, "HTTS(%p) - killing cgi client(%p)\n", cgi->htts, cgi->client->sck); @@ -323,7 +324,7 @@ static void peer_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid) HIO_ASSERT (hio, peer_xtn->cgi != HIO_NULL); /*printf ("DETACHING FROM CGI PEER DEVICE.....................%p %d\n", peer->cgi, (int)peer->cgi->rsrc_refcnt);*/ - HIO_SVC_HTTS_RSRC_DETACH (peer_xtn->cgi); + HIO_SVC_HTTS_RSRC_UNREF (peer_xtn->cgi); if (cgi->peer_htrd) { @@ -332,7 +333,7 @@ static void peer_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid) peer_xtn = hio_htrd_getxtn(cgi->peer_htrd); HIO_ASSERT (hio, peer_xtn->cgi != HIO_NULL); /*printf ("DETACHING FROM CGI PEER HTRD.....................%p %d\n", peer->cgi, (int)peer->cgi->rsrc_refcnt);*/ - HIO_SVC_HTTS_RSRC_DETACH (peer_xtn->cgi); + HIO_SVC_HTTS_RSRC_UNREF (peer_xtn->cgi); } break; @@ -343,7 +344,7 @@ static void peer_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid) if (!(cgi->over & CGI_OVER_READ_FROM_PEER)) { - if (cgi_write_last_chunk_to_client(cgi) <= -1) + if (cgi_write_last_chunk_to_client(cgi) <= -1) cgi_halt_participating_devices (cgi); else cgi_mark_over (cgi, CGI_OVER_READ_FROM_PEER); @@ -400,7 +401,7 @@ static int peer_on_read (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid, const void* HIO_ASSERT (hio, !(cgi->over & CGI_OVER_READ_FROM_PEER)); - if (hio_htrd_feed(cgi->peer_htrd, data, dlen, &rem) <= -1) + if (hio_htrd_feed(cgi->peer_htrd, data, dlen, &rem) <= -1) { HIO_DEBUG3 (hio, "HTTS(%p) - unable to feed peer htrd - peer %p(pid=%u)\n", cgi->htts, pro, (unsigned int)pro->child_pid); @@ -410,10 +411,10 @@ static int peer_on_read (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid, const void* cgi_send_final_status_to_client (cgi, HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR, 1); /* don't care about error because it jumps to oops below anyway */ } - goto oops; + goto oops; } - if (rem > 0) + if (rem > 0) { /* If the script specifies Content-Length and produces longer data, it will come here */ /*printf ("AAAAAAAAAAAAAAAAAa EEEEEXcessive DATA..................\n");*/ @@ -541,7 +542,7 @@ static int peer_htrd_push_content (hio_htrd_t* htrd, hio_htre_t* req, const hio_ hio_oow_t llen; /* hio_fmt_uintmax_to_bcstr() null-terminates the output. only HIO_COUNTOF(lbuf) - 1 - * is enough to hold '\r' and '\n' at the back without '\0'. */ + * is enough to hold '\r' and '\n' at the back without '\0'. */ llen = hio_fmt_uintmax_to_bcstr(lbuf, HIO_COUNTOF(lbuf) - 1, dlen, 16 | HIO_FMT_UINTMAX_UPPERCASE, 0, '\0', HIO_NULL); lbuf[llen++] = '\r'; lbuf[llen++] = '\n'; @@ -861,13 +862,13 @@ static int peer_on_fork (hio_dev_pro_t* pro, void* fork_ctx) if (environ) environ[0] = '\0'; } #endif - if (path) + if (path) { setenv ("PATH", path, 1); hio_freemem (hio, path); } - if (lang) + if (lang) { setenv ("LANG", lang, 1); hio_freemem (hio, lang); @@ -973,7 +974,7 @@ int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r csck->on_disconnect = cgi_client_on_disconnect; HIO_ASSERT (hio, cli->rsrc == HIO_NULL); - HIO_SVC_HTTS_RSRC_ATTACH ((hio_svc_htts_rsrc_t*)cgi, cli->rsrc); /* cli->rsrc = cgi */ + HIO_SVC_HTTS_RSRC_REF ((hio_svc_htts_rsrc_t*)cgi, cli->rsrc); /* cli->rsrc = cgi */ if (access(mi.cmd, X_OK) == -1) { @@ -984,7 +985,7 @@ int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r cgi->peer = hio_dev_pro_make(hio, HIO_SIZEOF(*peer_xtn), &mi); if (HIO_UNLIKELY(!cgi->peer)) goto oops; peer_xtn = hio_dev_pro_getxtn(cgi->peer); - HIO_SVC_HTTS_RSRC_ATTACH ((hio_svc_htts_rsrc_t*)cgi, peer_xtn->cgi); /* peer->cgi in pro = cgi */ + HIO_SVC_HTTS_RSRC_REF ((hio_svc_htts_rsrc_t*)cgi, peer_xtn->cgi); /* peer->cgi in pro = cgi */ cgi->peer_htrd = hio_htrd_open(hio, HIO_SIZEOF(*peer_xtn)); if (HIO_UNLIKELY(!cgi->peer_htrd)) goto oops; @@ -992,13 +993,13 @@ int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r hio_htrd_setrecbs (cgi->peer_htrd, &peer_htrd_recbs); peer_xtn = hio_htrd_getxtn(cgi->peer_htrd); - HIO_SVC_HTTS_RSRC_ATTACH ((hio_svc_htts_rsrc_t*)cgi, peer_xtn->cgi); /* peer->cgi in htrd = cgi */ + HIO_SVC_HTTS_RSRC_REF ((hio_svc_htts_rsrc_t*)cgi, peer_xtn->cgi); /* peer->cgi in htrd = cgi */ #if !defined(CGI_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH) if (cgi->req_content_length_unlimited) { /* Transfer-Encoding is chunked. no content-length is known in advance. */ - + /* option 1. buffer contents. if it gets too large, send 413 Request Entity Too Large. * option 2. send 411 Length Required immediately * option 3. set Content-Length to -1 and use EOF to indicate the end of content [Non-Standard] */ @@ -1011,17 +1012,17 @@ int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r { /* TODO: Expect: 100-continue? who should handle this? cgi? or the http server? */ /* CAN I LET the cgi SCRIPT handle this? */ - if (!(options & HIO_SVC_HTTS_CGI_NO_100_CONTINUE) && - hio_comp_http_version_numbers(&req->version, 1, 1) >= 0 && - (cgi->req_content_length_unlimited || cgi->req_content_length > 0)) + if (!(options & HIO_SVC_HTTS_CGI_NO_100_CONTINUE) && + hio_comp_http_version_numbers(&req->version, 1, 1) >= 0 && + (cgi->req_content_length_unlimited || cgi->req_content_length > 0)) { - /* + /* * Don't send 100 Continue if http verions is lower than 1.1 - * [RFC7231] + * [RFC7231] * A server that receives a 100-continue expectation in an HTTP/1.0 * request MUST ignore that expectation. * - * Don't send 100 Continue if expected content lenth is 0. + * Don't send 100 Continue if expected content lenth is 0. * [RFC7231] * A server MAY omit sending a 100 (Continue) response if it has * already received some or all of the message body for the @@ -1078,7 +1079,7 @@ int hio_svc_htts_docgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r if (req->flags & HIO_HTRE_ATTR_KEEPALIVE) { cgi->keep_alive = 1; - cgi->res_mode_to_cli = CGI_RES_MODE_CHUNKED; + cgi->res_mode_to_cli = CGI_RES_MODE_CHUNKED; /* the mode still can get switched to CGI_RES_MODE_LENGTH if the cgi script emits Content-Length */ } else diff --git a/lib/http-fcgi.c b/lib/http-fcgi.c index a39442c..b6535f7 100644 --- a/lib/http-fcgi.c +++ b/lib/http-fcgi.c @@ -74,7 +74,7 @@ static int begin_request () /* h->type = FCGI_STDIN; */ - + } #endif @@ -100,7 +100,7 @@ static int fcgi_write_to_peer (fcgi_t* fcgi, const void* data, hio_iolen_t dlen) { #if 0 fcgi->num_pending_writes_to_peer++; - if (hio_dev_pro_write(fcgi->peer, data, dlen, HIO_NULL) <= -1) + if (hio_dev_pro_write(fcgi->peer, data, dlen, HIO_NULL) <= -1) { fcgi->num_pending_writes_to_peer--; return -1; @@ -128,7 +128,7 @@ static HIO_INLINE void fcgi_mark_over (fcgi_t* fcgi, int over_bits) if (!(old_over & FCGI_OVER_READ_FROM_CLIENT) && (fcgi->over & FCGI_OVER_READ_FROM_CLIENT)) { printf (">>>>>>>>>>>> disableing client read watching ...................\n"); - if (hio_dev_sck_read(fcgi->client->sck, 0) <= -1) + if (hio_dev_sck_read(fcgi->client->sck, 0) <= -1) { HIO_DEBUG2 (fcgi->htts->hio, "HTTS(%p) - halting client(%p) for failure to disable input watching\n", fcgi->htts, fcgi->client->sck); hio_dev_sck_halt (fcgi->client->sck); @@ -138,7 +138,7 @@ printf (">>>>>>>>>>>> disableing client read watching ...................\n"); if (!(old_over & FCGI_OVER_READ_FROM_PEER) && (fcgi->over & FCGI_OVER_READ_FROM_PEER)) { #if 0 // TODO: - if (fcgi->peer && hio_dev_pro_read(fcgi->peer, HIO_DEV_PRO_OUT, 0) <= -1) + if (fcgi->peer && hio_dev_pro_read(fcgi->peer, HIO_DEV_PRO_OUT, 0) <= -1) { HIO_DEBUG2 (fcgi->htts->hio, "HTTS(%p) - halting peer(%p) for failure to disable input watching\n", fcgi->htts, fcgi->peer); hio_dev_pro_halt (fcgi->peer); @@ -150,20 +150,20 @@ printf (">>>>>>>>>>>> disableing client read watching ...................\n"); { /* ready to stop */ #if 0 // TODO: - if (fcgi->peer) + if (fcgi->peer) { HIO_DEBUG2 (fcgi->htts->hio, "HTTS(%p) - halting peer(%p) as it is unneeded\n", fcgi->htts, fcgi->peer); hio_dev_pro_halt (fcgi->peer); } #endif - if (fcgi->keep_alive) + if (fcgi->keep_alive) { /* how to arrange to delete this fcgi object and put the socket back to the normal waiting state??? */ HIO_ASSERT (fcgi->htts->hio, fcgi->client->rsrc == (hio_svc_htts_rsrc_t*)fcgi); /*printf ("DETACHING FROM THE MAIN CLIENT RSRC... state -> %p\n", fcgi->client->rsrc);*/ - HIO_SVC_HTTS_RSRC_DETACH (fcgi->client->rsrc); + HIO_SVC_HTTS_RSRC_UNREF (fcgi->client->rsrc); /* fcgi must not be accessed from here down as it could have been destroyed */ } else @@ -180,7 +180,7 @@ static int fcgi_write_to_client (fcgi_t* fcgi, const void* data, hio_iolen_t dle fcgi->ever_attempted_to_write_to_client = 1; fcgi->num_pending_writes_to_client++; - if (hio_dev_sck_write(fcgi->client->sck, data, dlen, HIO_NULL, HIO_NULL) <= -1) + if (hio_dev_sck_write(fcgi->client->sck, data, dlen, HIO_NULL, HIO_NULL) <= -1) { fcgi->num_pending_writes_to_client--; return -1; @@ -371,8 +371,9 @@ printf ("GOT FCGI DATA.............[%.*s]\n", (int)len, buf); return 0; } -static void fcgi_on_kill (fcgi_t* fcgi) +static void fcgi_on_kill (hio_svc_htts_rsrc_t* rsrc) { + fcgi_t* fcgi = (fcgi_t*)rsrc; hio_t* hio = fcgi->htts->hio; HIO_DEBUG2 (hio, "HTTS(%p) - killing fcgi client(%p)\n", fcgi->htts, fcgi->client->sck); @@ -422,25 +423,29 @@ static void fcgi_on_kill (fcgi_t* fcgi) } } -static int write_params (fcgi_t* fcgi, hio_dev_sck_t* csck, hio_htre_t* req) +static int write_params (fcgi_t* fcgi, hio_dev_sck_t* csck, hio_htre_t* req, const hio_bch_t* docroot, const hio_bch_t* script) { hio_t* hio = fcgi->htts->hio; hio_bch_t tmp[256]; hio_oow_t len; const hio_bch_t* qparam; hio_oow_t content_length; + hio_bch_t* actual_script = HIO_NULL; HIO_ASSERT (hio, fcgi->client->sck == csck); + actual_script = hio_svc_htts_dupmergepaths(fcgi->htts, docroot, script); + if (!actual_script) goto oops; + if (hio_svc_fcgic_writeparam(fcgi->peer, "GATEWAY_INTERFACE", 17, "CGI/1.1", 7) <= -1) goto oops; len = hio_fmttobcstr(hio, tmp, HIO_COUNTOF(tmp), "HTTP/%d.%d", (int)hio_htre_getmajorversion(req), (int)hio_htre_getminorversion(req)); if (hio_svc_fcgic_writeparam(fcgi->peer, "SERVER_PROTOCOL", 15, tmp, len) <= -1) goto oops; -// TODOs: -// DOCUMENT_ROOT -// SCRIPT_NAME -// PATH_INFO + if (hio_svc_fcgic_writeparam(fcgi->peer, "DOCUMENT_ROOT", 13, docroot, hio_count_bcstr(docroot)) <= -1) goto oops; + if (hio_svc_fcgic_writeparam(fcgi->peer, "SCRIPT_NAME", 11, script, hio_count_bcstr(script)) <= -1) goto oops; + if (hio_svc_fcgic_writeparam(fcgi->peer, "SCRIPT_FILENAME", 15, actual_script, hio_count_bcstr(actual_script)) <= -1) goto oops; +// TODO: PATH_INFO if (hio_svc_fcgic_writeparam(fcgi->peer, "REQUEST_METHOD", 14, hio_htre_getqmethodname(req), hio_htre_getqmethodlen(req)) <= -1) goto oops; if (hio_svc_fcgic_writeparam(fcgi->peer, "REQUEST_URI", 11, hio_htre_getqpath(req), hio_htre_getqpathlen(req)) <= -1) goto oops; @@ -477,9 +482,10 @@ static int write_params (fcgi_t* fcgi, hio_dev_sck_t* csck, hio_htre_t* req) return 0; oops: + if (actual_script) hio_freemem (hio, actual_script); return -1; } -int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, const hio_skad_t* fcgis_addr, int options) +int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req, const hio_skad_t* fcgis_addr, const hio_bch_t* docroot, const hio_bch_t* script, int options) { hio_t* hio = htts->hio; hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck); @@ -514,7 +520,7 @@ int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* csck->on_disconnect = fcgi_client_on_disconnect; HIO_ASSERT (hio, cli->rsrc == HIO_NULL); - HIO_SVC_HTTS_RSRC_ATTACH ((hio_svc_htts_rsrc_t*)fcgi, cli->rsrc); /* cli->rsrc = fcgi */ + HIO_SVC_HTTS_RSRC_REF ((hio_svc_htts_rsrc_t*)fcgi, cli->rsrc); /* cli->rsrc = fcgi */ /* create a session in in the fcgi client service */ fcgi->peer = hio_svc_fcgic_tie(htts->fcgic, fcgis_addr, fcgi_peer_on_read); @@ -523,14 +529,14 @@ int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* /* send FCGI_BEGIN_REQUEST */ if (hio_svc_fcgic_beginrequest(fcgi->peer) <= -1) goto oops; /* write FCGI_PARAM */ - if (write_params(fcgi, csck, req) <= -1) goto oops; + if (write_params(fcgi, csck, req, docroot, script) <= -1) goto oops; if (hio_svc_fcgic_writeparam(fcgi->peer, HIO_NULL, 0, HIO_NULL, 0) <= -1) goto oops; /* end of params */ #if !defined(FCGI_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH) if (fcgi->req_content_length_unlimited) { /* Transfer-Encoding is chunked. no content-length is known in advance. */ - + /* option 1. buffer contents. if it gets too large, send 413 Request Entity Too Large. * option 2. send 411 Length Required immediately * option 3. set Content-Length to -1 and use EOF to indicate the end of content [Non-Standard] */ @@ -543,17 +549,17 @@ int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* { /* TODO: Expect: 100-continue? who should handle this? fcgi? or the http server? */ /* CAN I LET the cgi SCRIPT handle this? */ - if (!(options & HIO_SVC_HTTS_CGI_NO_100_CONTINUE) && - hio_comp_http_version_numbers(&req->version, 1, 1) >= 0 && - (fcgi->req_content_length_unlimited || fcgi->req_content_length > 0)) + if (!(options & HIO_SVC_HTTS_CGI_NO_100_CONTINUE) && + hio_comp_http_version_numbers(&req->version, 1, 1) >= 0 && + (fcgi->req_content_length_unlimited || fcgi->req_content_length > 0)) { - /* + /* * Don't send 100 Continue if http verions is lower than 1.1 - * [RFC7231] + * [RFC7231] * A server that receives a 100-continue expectation in an HTTP/1.0 * request MUST ignore that expectation. * - * Don't send 100 Continue if expected content lenth is 0. + * Don't send 100 Continue if expected content lenth is 0. * [RFC7231] * A server MAY omit sending a 100 (Continue) response if it has * already received some or all of the message body for the @@ -610,7 +616,7 @@ int hio_svc_htts_dofcgi (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* if (req->flags & HIO_HTRE_ATTR_KEEPALIVE) { fcgi->keep_alive = 1; - fcgi->res_mode_to_cli = FCGI_RES_MODE_CHUNKED; + fcgi->res_mode_to_cli = FCGI_RES_MODE_CHUNKED; /* the mode still can get switched to FCGI_RES_MODE_LENGTH if the cgi script emits Content-Length */ } else @@ -627,4 +633,4 @@ oops: HIO_DEBUG2 (hio, "HTTS(%p) - FAILURE in dofcgi - socket(%p)\n", htts, csck); if (fcgi) fcgi_halt_participating_devices (fcgi); return -1; -} \ No newline at end of file +} diff --git a/lib/http-file.c b/lib/http-file.c index fe10f2a..2e752d2 100644 --- a/lib/http-file.c +++ b/lib/http-file.c @@ -101,7 +101,7 @@ static void file_halt_participating_devices (file_t* file) HIO_DEBUG3 (file->client->htts->hio, "HTTS(%p) - file(c=%d,p=%d) Halting participating devices\n", file->client->htts, (int)file->client->sck->hnd, (int)file->peer); - /* only the client socket device. + /* only the client socket device. * the peer side is just a file descriptor - no hio-managed device */ hio_dev_sck_halt (file->client->sck); } @@ -125,7 +125,7 @@ static int file_sendfile_to_client (file_t* file, hio_foff_t foff, hio_iolen_t l file->ever_attempted_to_write_to_client = 1; file->num_pending_writes_to_client++; - if (hio_dev_sck_sendfile(file->client->sck, file->peer, foff, len, HIO_NULL) <= -1) + if (hio_dev_sck_sendfile(file->client->sck, file->peer, foff, len, HIO_NULL) <= -1) { file->num_pending_writes_to_client--; return -1; @@ -157,7 +157,7 @@ static int file_send_final_status_to_client (file_t* file, int status_code, int /* don't send content body when the status code is 3xx. include the Location header only. */ if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: 0\r\nLocation: %hs/\r\n\r\n", file->req_qpath) == (hio_oow_t)-1) return -1; } - else + else { if (hio_becs_fcat(cli->sbuf, "Content-Type: text/plain\r\nContent-Length: %zu\r\n\r\n%hs", hio_count_bcstr(status_msg), status_msg) == (hio_oow_t)-1) return -1; } @@ -194,7 +194,7 @@ static void file_mark_over (file_t* file, int over_bits) if (!(old_over & FILE_OVER_READ_FROM_CLIENT) && (file->over & FILE_OVER_READ_FROM_CLIENT)) { - if (hio_dev_sck_read(file->client->sck, 0) <= -1) + if (hio_dev_sck_read(file->client->sck, 0) <= -1) { HIO_DEBUG3 (file->htts->hio, "HTTS(%p) - file(c=%d,p=%d) halting client for failure to disable input watching\n", file->htts, (int)file->client->sck->hnd, file->peer); hio_dev_sck_halt (file->client->sck); @@ -214,7 +214,7 @@ static void file_mark_over (file_t* file, int over_bits) HIO_DEBUG3 (file->htts->hio, "HTTS(%p) - file(c=%d,p=%d) halting peer as it is unneeded\n", file->htts, (int)file->client->sck->hnd, file->peer); file_close_peer (file); - if (file->keep_alive && !file->client_eof_detected) + if (file->keep_alive && !file->client_eof_detected) { #if defined(TCP_CORK) int tcp_cork = 0; @@ -227,7 +227,7 @@ static void file_mark_over (file_t* file, int over_bits) /* how to arrange to delete this file object and put the socket back to the normal waiting state??? */ HIO_ASSERT (file->htts->hio, file->client->rsrc == (hio_svc_htts_rsrc_t*)file); - HIO_SVC_HTTS_RSRC_DETACH (file->client->rsrc); + HIO_SVC_HTTS_RSRC_UNREF (file->client->rsrc); /* the file resource must not be accessed from here down as it could have been destroyed */ } else @@ -242,7 +242,7 @@ static void file_mark_over (file_t* file, int over_bits) static int file_write_to_peer (file_t* file, const void* data, hio_iolen_t dlen) { - hio_t* hio = file->htts->hio; + /* hio_t* hio = file->htts->hio; */ if (dlen <= 0) { @@ -251,7 +251,7 @@ static int file_write_to_peer (file_t* file, const void* data, hio_iolen_t dlen) else { hio_iolen_t pos, rem, n; - if (file->req_method == HIO_HTTP_GET) return 0; + if (file->req_method == HIO_HTTP_GET) return 0; if (file->peer <= -1) return 0; /* peer open proabably failed */ /* TODO: async file io -> create a file device?? */ @@ -269,8 +269,9 @@ static int file_write_to_peer (file_t* file, const void* data, hio_iolen_t dlen) return 0; } -static void file_on_kill (file_t* file) +static void file_on_kill (hio_svc_htts_rsrc_t* rsrc) { + file_t* file = (file_t*)rsrc; hio_t* hio = file->htts->hio; HIO_DEBUG3 (hio, "HTTS(%p) - file(c=%d,p=%d) on_kill\n", file->htts, (int)file->client->sck->hnd, file->peer); @@ -331,7 +332,7 @@ static void file_client_on_disconnect (hio_dev_sck_t* sck) file->client_disconnected = 1; file->client_org_on_disconnect (sck); - /* the original disconnect handler (listener_on_disconnect in http-svr.c) + /* the original disconnect handler (listener_on_disconnect in http-svr.c) * frees the file resource attached to the client. so it must not be accessed */ HIO_ASSERT (hio, cli->rsrc == HIO_NULL); } @@ -507,7 +508,7 @@ static int file_send_header_to_client (file_t* file, int status_code, int force_ /* Content-Type is not set if mime_type is null or blank */ if (mime_type && mime_type[0] != '\0' && hio_becs_fcat(cli->sbuf, "Content-Type: %hs\r\n", mime_type) == (hio_oow_t)-1) return -1; - + if ((file->req_method == HIO_HTTP_GET || file->req_method == HIO_HTTP_HEAD) && hio_becs_fcat(cli->sbuf, "ETag: %hs\r\n", file->peer_etag) == (hio_oow_t)-1) return -1; @@ -578,7 +579,7 @@ static int file_send_contents_to_client (file_t* file) { /* no more data to read - this must not happen unless file size changed while the file is open. */ /* TODO: I probably must close the connection by force??? */ - file_mark_over (file, FILE_OVER_READ_FROM_PEER); + file_mark_over (file, FILE_OVER_READ_FROM_PEER); return -1; } @@ -604,7 +605,7 @@ static HIO_INLINE int process_range_header (file_t* file, hio_htre_t* req, int* const hio_htre_hdrval_t* tmp; hio_oow_t etag_len; - if (fstat(file->peer, &st) <= -1) + if (fstat(file->peer, &st) <= -1) { *error_status = ERRNO_TO_STATUS_CODE(errno); return -1; @@ -675,7 +676,7 @@ static HIO_INLINE int process_range_header (file_t* file, hio_htre_t* req, int* break; } - if (file->start_offset > 0) + if (file->start_offset > 0) { if (lseek(file->peer, file->start_offset, SEEK_SET) <= -1) { @@ -736,7 +737,7 @@ static int open_peer_with_mode (file_t* file, const hio_bch_t* actual_file, int } else { - if (res_mime_type && file->cbs && file->cbs->get_mime_type) + if (res_mime_type && file->cbs && file->cbs->get_mime_type) { const hio_bch_t* mime_type; mime_type = file->cbs->get_mime_type(file->htts, file->req_qpath, actual_file, file->cbs->ctx); @@ -810,14 +811,14 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* file->peer = -1; HIO_ASSERT (hio, cli->rsrc == HIO_NULL); /* you must not call this function while cli->rsrc is not HIO_NULL */ - HIO_SVC_HTTS_RSRC_ATTACH ((hio_svc_htts_rsrc_t*)file, cli->rsrc); /* cli->rsrc = file with ref-count up */ + HIO_SVC_HTTS_RSRC_REF ((hio_svc_htts_rsrc_t*)file, cli->rsrc); /* cli->rsrc = file with ref-count up */ #if !defined(FILE_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH) if (file->req_content_length_unlimited) { /* Transfer-Encoding is chunked. no content-length is known in advance. */ - + /* option 1. buffer contents. if it gets too large, send 413 Request Entity Too Large. * option 2. send 411 Length Required immediately * option 3. set Content-Length to -1 and use EOF to indicate the end of content [Non-Standard] */ @@ -830,7 +831,7 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* if (!(options & HIO_SVC_HTTS_FILE_NO_100_CONTINUE) && hio_comp_http_version_numbers(&req->version, 1, 1) >= 0 && (file->req_content_length_unlimited || file->req_content_length > 0) && - (file->req_method != HIO_HTTP_GET && file->req_method != HIO_HTTP_HEAD)) + (file->req_method != HIO_HTTP_GET && file->req_method != HIO_HTTP_HEAD)) { hio_bch_t msgbuf[64]; hio_oow_t msglen; @@ -843,7 +844,7 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* else if (req->flags & HIO_HTRE_ATTR_EXPECT) { /* 417 Expectation Failed */ - file_send_final_status_to_client (file, HIO_HTTP_STATUS_EXPECTATION_FAILED, 1, HIO_NULL); + file_send_final_status_to_client (file, HIO_HTTP_STATUS_EXPECTATION_FAILED, 1, 0); goto oops; } @@ -951,7 +952,7 @@ int hio_svc_htts_dofile (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* break; case HIO_HTTP_DELETE: - if (file->options & HIO_SVC_HTTS_FILE_READ_ONLY) + if (file->options & HIO_SVC_HTTS_FILE_READ_ONLY) { status_code = HIO_HTTP_STATUS_METHOD_NOT_ALLOWED; goto done_with_status_code; diff --git a/lib/http-prv.h b/lib/http-prv.h index c61c3a4..b9100f4 100644 --- a/lib/http-prv.h +++ b/lib/http-prv.h @@ -83,6 +83,7 @@ struct hio_svc_httc_t HIO_SVC_HEADER; }; +/* client list */ #define HIO_SVC_HTTS_CLIL_APPEND_CLI(lh,cli) do { \ (cli)->cli_next = (lh); \ (cli)->cli_prev = (lh)->cli_prev; \ diff --git a/lib/http-svr.c b/lib/http-svr.c index 5c33fc6..dd44ece 100644 --- a/lib/http-svr.c +++ b/lib/http-svr.c @@ -59,7 +59,7 @@ static int init_client (hio_svc_htts_cli_t* cli, hio_dev_sck_t* sck) cli->htrd = HIO_NULL; cli->sbuf = HIO_NULL; cli->rsrc = HIO_NULL; - /* keep this linked regardless of success or failure because the disconnect() callback + /* keep this linked regardless of success or failure because the disconnect() callback * will call fini_client(). the error handler code after 'oops:' doesn't get this unlinked */ HIO_SVC_HTTS_CLIL_APPEND_CLI (&cli->htts->cli, cli); @@ -85,7 +85,7 @@ static int init_client (hio_svc_htts_cli_t* cli, hio_dev_sck_t* sck) oops: /* since this function is called in the on_connect() callback, * fini_client() is eventually called by on_disconnect(). i don't do clean-up here. - if (cli->sbuf) + if (cli->sbuf) { hio_becs_close(cli->sbuf); cli->sbuf = HIO_NULL; @@ -100,19 +100,14 @@ oops: static void fini_client (hio_svc_htts_cli_t* cli) { - HIO_DEBUG4 (cli->sck->hio, "HTTS(%p) - finalizing client(%p,%p,%d)\n", cli->htts, cli, cli->sck, (int)cli->sck->hnd); + HIO_DEBUG5 (cli->sck->hio, "HTTS(%p) - finalizing client(c=%p,sck=%p[%d],rsrc=%p)\n", cli->htts, cli, cli->sck, (int)cli->sck->hnd, cli->rsrc); if (cli->rsrc) { - #if 0 - hio_svc_htts_rsrc_kill (cli->rsrc); - cli->rsrc = HIO_NULL; - #else - HIO_SVC_HTTS_RSRC_DETACH (cli->rsrc); - #endif + HIO_SVC_HTTS_RSRC_UNREF (cli->rsrc); } - if (cli->sbuf) + if (cli->sbuf) { hio_becs_close (cli->sbuf); cli->sbuf = HIO_NULL; @@ -126,11 +121,11 @@ static void fini_client (hio_svc_htts_cli_t* cli) HIO_SVC_HTTS_CLIL_UNLINK_CLI_CLEAN (cli); - /* are these needed? not symmetrical if done here. + /* are these needed? not symmetrical if done here. * these fields are copied from the listener socket upon accept. * init_client() doesn't fill in these fields. let's comment out these lines cli->sck = HIO_NULL; - cli->htts = HIO_NULL; + cli->htts = HIO_NULL; */ } @@ -151,7 +146,7 @@ static int listener_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t le /* if a resource has been set(cli->rsrc not NULL), the resource must take over * this handler. this handler is never called unless the the overriding handler * call this. */ - HIO_ASSERT (hio, cli->rsrc == HIO_NULL); + HIO_ASSERT (hio, cli->rsrc == HIO_NULL); if (len <= -1) { @@ -159,14 +154,14 @@ static int listener_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t le goto oops; } - if (len == 0) + if (len == 0) { HIO_DEBUG3 (hio, "HTTS(%p) - EOF on client %p(%d)\n", cli->htts, sck, (int)sck->hnd); goto oops; } hio_gettime (hio, &cli->last_active); - if ((x = hio_htrd_feed(cli->htrd, buf, len, &rem)) <= -1) + if ((x = hio_htrd_feed(cli->htrd, buf, len, &rem)) <= -1) { HIO_DEBUG3 (hio, "HTTS(%p) - feed error onto client htrd %p(%d)\n", cli->htts, sck, (int)sck->hnd); goto oops; @@ -236,45 +231,46 @@ static void listener_on_disconnect (hio_dev_sck_t* sck) { hio_t* hio = sck->hio; hio_svc_htts_cli_t* xtn = hio_dev_sck_getxtn(sck); + hio_svc_htts_t* htts = xtn->htts; switch (HIO_DEV_SCK_GET_PROGRESS(sck)) { case HIO_DEV_SCK_CONNECTING: /* only for connecting sockets */ - HIO_DEBUG1 (hio, "OUTGOING SESSION DISCONNECTED - FAILED TO CONNECT (%d) TO REMOTE SERVER\n", (int)sck->hnd); + HIO_DEBUG3 (hio, "HTTS(%p) - OUTGOING SESSION DISCONNECTED - FAILED TO CONNECT %p[%d] TO REMOTE SERVER\n", htts, sck, (int)sck->hnd); break; case HIO_DEV_SCK_CONNECTING_SSL: /* only for connecting sockets */ - HIO_DEBUG1 (hio, "OUTGOING SESSION DISCONNECTED - FAILED TO SSL-CONNECT (%d) TO REMOTE SERVER\n", (int)sck->hnd); + HIO_DEBUG3 (hio, "HTTS(%p) - OUTGOING SESSION DISCONNECTED - FAILED TO SSL-CONNECT %p[%d] TO REMOTE SERVER\n", htts, sck, (int)sck->hnd); break; case HIO_DEV_SCK_CONNECTED: /* only for connecting sockets */ - HIO_DEBUG1 (hio, "OUTGOING CLIENT CONNECTION GOT TORN DOWN %p(%d).......\n", (int)sck->hnd); + HIO_DEBUG3 (hio, "HTTS(%p) - OUTGOING CLIENT CONNECTION GOT TORN DOWN %p[%d].......\n", htts, sck, (int)sck->hnd); break; case HIO_DEV_SCK_LISTENING: - HIO_DEBUG2 (hio, "LISTNER SOCKET %p(%d) - SHUTTUING DOWN\n", sck, (int)sck->hnd); + HIO_DEBUG3 (hio, "HTTS(%p) - LISTNER SOCKET %p[%d] - SHUTTUING DOWN\n", htts, sck, (int)sck->hnd); break; case HIO_DEV_SCK_ACCEPTING_SSL: /* special case. */ /* this progress code indicates that the ssl-level accept failed. - * on_disconnected() with this code is called without corresponding on_connect(). + * on_disconnected() with this code is called without corresponding on_connect(). * the cli extension are is not initialized yet */ HIO_ASSERT (hio, sck != xtn->sck); //HIO_ASSERT (hio, cli->sck == cli->htts->lsck); /* the field is a copy of the extension are of the listener socket. so it should point to the listner socket */ - HIO_DEBUG2 (hio, "LISTENER UNABLE TO SSL-ACCEPT CLIENT %p(%d) ....%p\n", sck, (int)sck->hnd); + HIO_DEBUG3 (hio, "HTTS(%p) - LISTENER UNABLE TO SSL-ACCEPT CLIENT %p[%d]\n", htts, sck, (int)sck->hnd); return; case HIO_DEV_SCK_ACCEPTED: /* only for sockets accepted by the listeners. will never come here because * the disconnect call for such sockets have been changed in listener_on_connect() */ - HIO_DEBUG2 (hio, "ACCEPTED CLIENT SOCKET %p(%d) GOT DISCONNECTED.......\n", sck, (int)sck->hnd); + HIO_DEBUG3 (hio, "HTTS(%p) - ACCEPTED CLIENT SOCKET %p[%d] GOT DISCONNECTED\n", htts, sck, (int)sck->hnd); break; default: - HIO_DEBUG2 (hio, "SOCKET %p(%d) DISCONNECTED AFTER ALL.......\n", sck, (int)sck->hnd); + HIO_DEBUG3 (hio, "HTTS(%p) - SOCKET %p[%d] DISCONNECTED AFTER ALL\n", htts, sck, (int)sck->hnd); break; } @@ -284,13 +280,13 @@ static void listener_on_disconnect (hio_dev_sck_t* sck) HIO_ASSERT (hio, xtn->htrd == HIO_NULL); HIO_ASSERT (hio, xtn->sbuf == HIO_NULL); - HIO_DEBUG2 (hio, "HTTS(%p) - listener socket disconnect %p\n", xtn->htts, sck); + HIO_DEBUG3 (hio, "HTTS(%p) - listener socket disconnect %p[%d]\n", xtn->htts, sck, (int)sck->hnd); xtn->htts->l.sck[xtn->l_idx] = HIO_NULL; /* let the htts service forget about this listening socket */ } else { /* client socket */ - HIO_DEBUG2 (hio, "HTTS(%p) - client socket disconnect %p\n", xtn->htts, sck); + HIO_DEBUG3 (hio, "HTTS(%p) - client socket disconnect %p[%d]\n", xtn->htts, sck, (int)sck->hnd); HIO_ASSERT (hio, xtn->sck == sck); fini_client (xtn); } @@ -299,11 +295,10 @@ static void listener_on_disconnect (hio_dev_sck_t* sck) /* ------------------------------------------------------------------------ */ #define MAX_CLIENT_IDLE 10 - static void halt_idle_clients (hio_t* hio, const hio_ntime_t* now, hio_tmrjob_t* job) { /* TODO: this idle client detector is far away from being accurate. - * enhance htrd to specify timeout on feed() and utilize it... + * enhance htrd to specify timeout on feed() and utilize it... * and remove this timer job */ hio_svc_htts_t* htts = (hio_svc_htts_t*)job->ctx; hio_svc_htts_cli_t* cli; @@ -318,7 +313,7 @@ static void halt_idle_clients (hio_t* hio, const hio_ntime_t* now, hio_tmrjob_t* hio_ntime_t t; HIO_SUB_NTIME(&t, now, &cli->last_active); - if (HIO_CMP_NTIME(&t, &max_client_idle) >= 0) + if (HIO_CMP_NTIME(&t, &max_client_idle) >= 0) { HIO_DEBUG4 (hio, "HTTS(%p) - Halting idle client(%p,%p,%d)\n", htts, cli, cli->sck, (int)cli->sck->hnd); hio_dev_sck_halt (cli->sck); @@ -346,7 +341,7 @@ hio_svc_htts_t* hio_svc_htts_start (hio_t* hio, hio_oow_t xtnsize, hio_dev_sck_b hio_svc_htts_cli_t* cli; hio_oow_t i, noks; - if (HIO_UNLIKELY(nbinds <= 0)) + if (HIO_UNLIKELY(nbinds <= 0)) { hio_seterrnum (hio, HIO_EINVAL); goto oops; @@ -376,11 +371,11 @@ hio_svc_htts_t* hio_svc_htts_start (hio_t* hio, hio_oow_t xtnsize, hio_dev_sck_b case HIO_AF_INET: info.m.type = HIO_DEV_SCK_TCP4; break; - + case HIO_AF_INET6: info.m.type = HIO_DEV_SCK_TCP6; break; - + #if defined(HIO_AF_UNIX) case HIO_AF_UNIX: info.m.type = HIO_DEV_SCK_UNIX; @@ -396,6 +391,10 @@ hio_svc_htts_t* hio_svc_htts_start (hio_t* hio, hio_oow_t xtnsize, hio_dev_sck_b HIO_DEBUG3 (hio, "HTTS(%p) - [%zu] unsupported bind address type %d\n", htts, i, (int)hio_skad_get_family(&binds[i].localaddr)); continue; } + + /* the callback names(prefixed with listener_on_) are somewhat misleading because + * they are triggered on the client sockets because the accepted client sockets + * inherit them */ info.m.options = HIO_DEV_SCK_MAKE_LENIENT; info.m.on_write = listener_on_write; info.m.on_read = listener_on_read; @@ -403,7 +402,7 @@ hio_svc_htts_t* hio_svc_htts_start (hio_t* hio, hio_oow_t xtnsize, hio_dev_sck_b info.m.on_disconnect = listener_on_disconnect; sck = hio_dev_sck_make(hio, HIO_SIZEOF(*cli), &info.m); if (HIO_UNLIKELY(!sck)) continue; - + /* the name 'cli' for the listening socket is awkward. * the listening socket will use the htts, sck, and listening fields for tracking only. * each accepted client socket gets the extension size for this size as well. @@ -432,7 +431,7 @@ hio_svc_htts_t* hio_svc_htts_start (hio_t* hio, hio_oow_t xtnsize, hio_dev_sck_b HIO_MEMSET (&info, 0, HIO_SIZEOF(info)); info.l.backlogs = 4096; /* TODO: use configuration? */ HIO_INIT_NTIME (&info.l.accept_tmout, 5, 1); /* usedd for ssl accept */ - if (hio_dev_sck_listen(sck, &info.l) <= -1) + if (hio_dev_sck_listen(sck, &info.l) <= -1) { if (HIO_LOG_ENABLED(hio, HIO_LOG_DEBUG)) { @@ -461,7 +460,7 @@ hio_svc_htts_t* hio_svc_htts_start (hio_t* hio, hio_oow_t xtnsize, hio_dev_sck_b if (noks <= 0) goto oops; - hio_fmttobcstr (htts->hio, htts->server_name_buf, HIO_COUNTOF(htts->server_name_buf), "%s-%d.%d.%d", + hio_fmttobcstr (htts->hio, htts->server_name_buf, HIO_COUNTOF(htts->server_name_buf), "%s-%d.%d.%d", HIO_PACKAGE_NAME, (int)HIO_PACKAGE_VERSION_MAJOR, (int)HIO_PACKAGE_VERSION_MINOR, (int)HIO_PACKAGE_VERSION_PATCH); htts->server_name = htts->server_name_buf; @@ -498,7 +497,7 @@ oops: htts->fcgic = HIO_NULL; } - if (htts->l.sck) + if (htts->l.sck) { for (i = 0; i < htts->l.count; i++) { @@ -524,7 +523,7 @@ void hio_svc_htts_stop (hio_svc_htts_t* htts) hio_svc_fcgic_stop (htts->fcgic); htts->fcgic = HIO_NULL; } - + for (i = 0; i < htts->l.count; i++) { /* the socket may be null: @@ -539,13 +538,23 @@ void hio_svc_htts_stop (hio_svc_htts_t* htts) hio_dev_sck_kill (cli->sck); } +/* + while (!HIO_SVC_HTTS_TASKL_IS_EMPTY(&htts->task)) + { + + } +*/ + HIO_SVCL_UNLINK_SVC (htts); if (htts->server_name && htts->server_name != htts->server_name_buf) hio_freemem (hio, htts->server_name); if (htts->idle_tmridx != HIO_TMRIDX_INVALID) hio_deltmrjob (hio, htts->idle_tmridx); if (htts->l.sck) hio_freemem (hio, htts->l.sck); + hio_freemem (hio, htts); + + HIO_DEBUG1 (hio, "HTTS - STOPPED SERVICE %p\n", htts); } void* hio_svc_htts_getxtn (hio_svc_htts_t* htts) @@ -629,29 +638,42 @@ int hio_svc_htts_getsockaddr (hio_svc_htts_t* htts, hio_oow_t idx, hio_skad_t* s * int b; * }; * - * you can pass sizeof(my_rsrc_t) to hio_svc_htts_rsrc_make() + * you can pass sizeof(my_rsrc_t) to hio_svc_htts_rsrc_make() */ hio_svc_htts_rsrc_t* hio_svc_htts_rsrc_make (hio_svc_htts_t* htts, hio_oow_t rsrc_size, hio_svc_htts_rsrc_on_kill_t on_kill) { hio_t* hio = htts->hio; hio_svc_htts_rsrc_t* rsrc; + HIO_DEBUG1 (hio, "HTTS(%p) - allocating resource\n", htts); + rsrc = hio_callocmem(hio, rsrc_size); - if (HIO_UNLIKELY(!rsrc)) return HIO_NULL; + if (HIO_UNLIKELY(!rsrc)) + { + HIO_DEBUG1 (hio, "HTTS(%p) - failed to allocate resource\n", htts); + return HIO_NULL; + } rsrc->htts = htts; rsrc->rsrc_size = rsrc_size; rsrc->rsrc_refcnt = 0; rsrc->rsrc_on_kill = on_kill; + HIO_DEBUG2 (hio, "HTTS(%p) - allocated resource %p\n", htts, rsrc); return rsrc; } void hio_svc_htts_rsrc_kill (hio_svc_htts_rsrc_t* rsrc) { - hio_t* hio = rsrc->htts->hio; + hio_svc_htts_t* htts = rsrc->htts; + hio_t* hio = htts->hio; + + HIO_DEBUG2 (hio, "HTTS(%p) - destroying resource %p\n", htts, rsrc); + if (rsrc->rsrc_on_kill) rsrc->rsrc_on_kill (rsrc); hio_freemem (hio, rsrc); + + HIO_DEBUG2 (hio, "HTTS(%p) - destroyed resource %p\n", htts, rsrc); } /* ----------------------------------------------------------------- */ @@ -663,7 +685,7 @@ int hio_svc_htts_doproxy (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* { #if 0 1. attempt to connect to the proxy target... - 2. in the mean time, + 2. in the mean time, hio_dev_watch (csck, HIO_DEV_WATCH_UPDATE, 0); /* no input, no output watching */ 3. once connected, @@ -685,7 +707,7 @@ void hio_svc_htts_fmtgmtime (hio_svc_htts_t* htts, const hio_ntime_t* nt, hio_bc { hio_ntime_t now; - if (!nt) + if (!nt) { hio_sys_getrealtime(htts->hio, &now); nt = &now; @@ -719,14 +741,14 @@ int hio_svc_htts_writetosidechan (hio_svc_htts_t* htts, hio_oow_t idx, const voi if (idx >= htts->l.count) { /* don't set the error information - TODO: change hio_seterrbfmt thread-safe? - *hio_seterrbfmt (htts->hio, HIO_EINVAL, "index out of range");*/ + *hio_seterrbfmt (htts->hio, HIO_EINVAL, "index out of range");*/ errno = EINVAL; return -1; } if (!htts->l.sck[idx]) { - /* don't set the error information + /* don't set the error information *hio_seterrbfmt (htts->hio, HIO_EINVAL, "no listener at the given index"); */ errno = EINVAL; return -1; diff --git a/lib/http-thr.c b/lib/http-thr.c index dee709f..89d2334 100644 --- a/lib/http-thr.c +++ b/lib/http-thr.c @@ -21,6 +21,7 @@ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + #include "http-prv.h" #include #include @@ -30,32 +31,32 @@ #define THR_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH -enum thr_state_res_mode_t +enum THR_TASK_RES_mode_t { - THR_STATE_RES_MODE_CHUNKED, - THR_STATE_RES_MODE_CLOSE, - THR_STATE_RES_MODE_LENGTH + THR_TASK_RES_MODE_CHUNKED, + THR_TASK_RES_MODE_CLOSE, + THR_TASK_RES_MODE_LENGTH }; -typedef enum thr_state_res_mode_t thr_state_res_mode_t; +typedef enum THR_TASK_RES_mode_t THR_TASK_RES_mode_t; -#define THR_STATE_PENDING_IO_THRESHOLD 5 +#define THR_TASK_PENDING_IO_THRESHOLD 5 -#define THR_STATE_OVER_READ_FROM_CLIENT (1 << 0) -#define THR_STATE_OVER_READ_FROM_PEER (1 << 1) -#define THR_STATE_OVER_WRITE_TO_CLIENT (1 << 2) -#define THR_STATE_OVER_WRITE_TO_PEER (1 << 3) -#define THR_STATE_OVER_ALL (THR_STATE_OVER_READ_FROM_CLIENT | THR_STATE_OVER_READ_FROM_PEER | THR_STATE_OVER_WRITE_TO_CLIENT | THR_STATE_OVER_WRITE_TO_PEER) +#define THR_TASK_OVER_READ_FROM_CLIENT (1 << 0) +#define THR_TASK_OVER_READ_FROM_PEER (1 << 1) +#define THR_TASK_OVER_WRITE_TO_CLIENT (1 << 2) +#define THR_TASK_OVER_WRITE_TO_PEER (1 << 3) +#define THR_TASK_OVER_ALL (THR_TASK_OVER_READ_FROM_CLIENT | THR_TASK_OVER_READ_FROM_PEER | THR_TASK_OVER_WRITE_TO_CLIENT | THR_TASK_OVER_WRITE_TO_PEER) struct thr_func_start_t { - hio_t* hio; + hio_svc_htts_t* htts; hio_svc_htts_thr_func_t thr_func; void* thr_ctx; hio_svc_htts_thr_func_info_t tfi; }; typedef struct thr_func_start_t thr_func_start_t; -struct thr_state_t +struct thr_task_t { HIO_SVC_HTTS_RSRC_HEADER; @@ -64,10 +65,11 @@ struct thr_state_t hio_oow_t num_pending_writes_to_peer; hio_dev_thr_t* peer; hio_htrd_t* peer_htrd; + hio_dev_sck_t* csck; hio_svc_htts_cli_t* client; hio_http_version_t req_version; /* client request */ - unsigned int over: 4; /* must be large enough to accomodate THR_STATE_OVER_ALL */ + unsigned int over: 4; /* must be large enough to accomodate THR_TASK_OVER_ALL */ unsigned int keep_alive: 1; unsigned int req_content_length_unlimited: 1; unsigned int ever_attempted_to_write_to_client: 1; @@ -75,7 +77,7 @@ struct thr_state_t unsigned int client_disconnected: 1; unsigned int client_htrd_recbs_changed: 1; hio_oow_t req_content_length; /* client request content length */ - thr_state_res_mode_t res_mode_to_cli; + THR_TASK_RES_mode_t res_mode_to_cli; hio_dev_sck_on_read_t client_org_on_read; hio_dev_sck_on_write_t client_org_on_write; @@ -83,276 +85,282 @@ struct thr_state_t hio_htrd_recbs_t client_htrd_org_recbs; }; -typedef struct thr_state_t thr_state_t; +typedef struct thr_task_t thr_task_t; struct thr_peer_xtn_t { - thr_state_t* state; + thr_task_t* task; }; typedef struct thr_peer_xtn_t thr_peer_xtn_t; -static void thr_state_halt_participating_devices (thr_state_t* thr_state) +static void thr_task_halt_participating_devices (thr_task_t* thr_task) { - HIO_ASSERT (thr_state->client->htts->hio, thr_state->client != HIO_NULL); - HIO_ASSERT (thr_state->client->htts->hio, thr_state->client->sck != HIO_NULL); + HIO_ASSERT (thr_task->htts->hio, thr_task->client != HIO_NULL); + HIO_ASSERT (thr_task->htts->hio, thr_task->csck != HIO_NULL); - HIO_DEBUG4 (thr_state->client->htts->hio, "HTTS(%p) - Halting participating devices in thr state %p(client=%p,peer=%p)\n", thr_state->client->htts, thr_state, thr_state->client->sck, thr_state->peer); + HIO_DEBUG4 (thr_task->htts->hio, "HTTS(%p) - Halting participating devices in thr task %p(csck=%p,peer=%p)\n", thr_task->htts, thr_task, thr_task->csck, thr_task->peer); - hio_dev_sck_halt (thr_state->client->sck); + if (thr_task->csck) hio_dev_sck_halt (thr_task->csck); /* check for peer as it may not have been started */ - if (thr_state->peer) hio_dev_thr_halt (thr_state->peer); + if (thr_task->peer) hio_dev_thr_halt (thr_task->peer); } -static int thr_state_write_to_client (thr_state_t* thr_state, const void* data, hio_iolen_t dlen) +static int thr_task_write_to_client (thr_task_t* thr_task, const void* data, hio_iolen_t dlen) { - thr_state->ever_attempted_to_write_to_client = 1; - - thr_state->num_pending_writes_to_client++; - if (hio_dev_sck_write(thr_state->client->sck, data, dlen, HIO_NULL, HIO_NULL) <= -1) + if (thr_task->csck) { - thr_state->num_pending_writes_to_client--; - return -1; - } + thr_task->ever_attempted_to_write_to_client = 1; - if (thr_state->num_pending_writes_to_client > THR_STATE_PENDING_IO_THRESHOLD) - { - if (hio_dev_thr_read(thr_state->peer, 0) <= -1) return -1; + thr_task->num_pending_writes_to_client++; + if (hio_dev_sck_write(thr_task->csck, data, dlen, HIO_NULL, HIO_NULL) <= -1) + { + thr_task->num_pending_writes_to_client--; + return -1; + } + + if (thr_task->num_pending_writes_to_client > THR_TASK_PENDING_IO_THRESHOLD) + { + if (hio_dev_thr_read(thr_task->peer, 0) <= -1) return -1; + } } return 0; } -static int thr_state_writev_to_client (thr_state_t* thr_state, hio_iovec_t* iov, hio_iolen_t iovcnt) +static int thr_task_writev_to_client (thr_task_t* thr_task, hio_iovec_t* iov, hio_iolen_t iovcnt) { - thr_state->ever_attempted_to_write_to_client = 1; - - thr_state->num_pending_writes_to_client++; - if (hio_dev_sck_writev(thr_state->client->sck, iov, iovcnt, HIO_NULL, HIO_NULL) <= -1) + if (thr_task->csck) { - thr_state->num_pending_writes_to_client--; - return -1; - } + thr_task->ever_attempted_to_write_to_client = 1; - if (thr_state->num_pending_writes_to_client > THR_STATE_PENDING_IO_THRESHOLD) - { - if (hio_dev_thr_read(thr_state->peer, 0) <= -1) return -1; + thr_task->num_pending_writes_to_client++; + if (hio_dev_sck_writev(thr_task->csck, iov, iovcnt, HIO_NULL, HIO_NULL) <= -1) + { + thr_task->num_pending_writes_to_client--; + return -1; + } + + if (thr_task->num_pending_writes_to_client > THR_TASK_PENDING_IO_THRESHOLD) + { + if (hio_dev_thr_read(thr_task->peer, 0) <= -1) return -1; + } } return 0; } -static int thr_state_send_final_status_to_client (thr_state_t* thr_state, int status_code, int force_close) +static int thr_task_send_final_status_to_client (thr_task_t* thr_task, int status_code, int force_close) { - hio_svc_htts_cli_t* cli = thr_state->client; + hio_svc_htts_cli_t* cli = thr_task->client; hio_bch_t dtbuf[64]; hio_svc_htts_fmtgmtime (cli->htts, HIO_NULL, dtbuf, HIO_COUNTOF(dtbuf)); - if (!force_close) force_close = !thr_state->keep_alive; + if (!force_close) force_close = !thr_task->keep_alive; if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %s\r\nConnection: %hs\r\nContent-Length: 0\r\n\r\n", - thr_state->req_version.major, thr_state->req_version.minor, + thr_task->req_version.major, thr_task->req_version.minor, status_code, hio_http_status_to_bcstr(status_code), cli->htts->server_name, dtbuf, (force_close? "close": "keep-alive")) == (hio_oow_t)-1) return -1; - return (thr_state_write_to_client(thr_state, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)) <= -1 || - (force_close && thr_state_write_to_client(thr_state, HIO_NULL, 0) <= -1))? -1: 0; + return (thr_task_write_to_client(thr_task, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)) <= -1 || + (force_close && thr_task_write_to_client(thr_task, HIO_NULL, 0) <= -1))? -1: 0; } - -static int thr_state_write_last_chunk_to_client (thr_state_t* thr_state) +static int thr_task_write_last_chunk_to_client (thr_task_t* thr_task) { - if (!thr_state->ever_attempted_to_write_to_client) + if (!thr_task->ever_attempted_to_write_to_client) { - if (thr_state_send_final_status_to_client(thr_state, HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR, 0) <= -1) return -1; + if (thr_task_send_final_status_to_client(thr_task, HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR, 0) <= -1) return -1; } else { - if (thr_state->res_mode_to_cli == THR_STATE_RES_MODE_CHUNKED && - thr_state_write_to_client(thr_state, "0\r\n\r\n", 5) <= -1) return -1; + if (thr_task->res_mode_to_cli == THR_TASK_RES_MODE_CHUNKED && + thr_task_write_to_client(thr_task, "0\r\n\r\n", 5) <= -1) return -1; } - if (!thr_state->keep_alive && thr_state_write_to_client(thr_state, HIO_NULL, 0) <= -1) return -1; + if (!thr_task->keep_alive && thr_task_write_to_client(thr_task, HIO_NULL, 0) <= -1) return -1; return 0; } -static int thr_state_write_to_peer (thr_state_t* thr_state, const void* data, hio_iolen_t dlen) +static int thr_task_write_to_peer (thr_task_t* thr_task, const void* data, hio_iolen_t dlen) { - thr_state->num_pending_writes_to_peer++; - if (hio_dev_thr_write(thr_state->peer, data, dlen, HIO_NULL) <= -1) + thr_task->num_pending_writes_to_peer++; + if (hio_dev_thr_write(thr_task->peer, data, dlen, HIO_NULL) <= -1) { - thr_state->num_pending_writes_to_peer--; + thr_task->num_pending_writes_to_peer--; return -1; } /* TODO: check if it's already finished or something.. */ - if (thr_state->num_pending_writes_to_peer > THR_STATE_PENDING_IO_THRESHOLD) + if (thr_task->num_pending_writes_to_peer > THR_TASK_PENDING_IO_THRESHOLD) { - if (hio_dev_sck_read(thr_state->client->sck, 0) <= -1) return -1; + if (hio_dev_sck_read(thr_task->csck, 0) <= -1) return -1; } return 0; } -static HIO_INLINE void thr_state_mark_over (thr_state_t* thr_state, int over_bits) +static HIO_INLINE void thr_task_mark_over (thr_task_t* thr_task, int over_bits) { + hio_svc_htts_t* htts = thr_task->htts; + hio_t* hio = htts->hio; unsigned int old_over; - old_over = thr_state->over; - thr_state->over |= over_bits; + old_over = thr_task->over; + thr_task->over |= over_bits; - HIO_DEBUG5 (thr_state->htts->hio, "HTTS(%p) - client=%p peer=%p new-bits=%x over=%x\n", thr_state->htts, thr_state->client->sck, thr_state->peer, (int)over_bits, (int)thr_state->over); + HIO_DEBUG8 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - old_over=%x | new-bits=%x => over=%x\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer, (int)old_over, (int)over_bits, (int)thr_task->over); - if (!(old_over & THR_STATE_OVER_READ_FROM_CLIENT) && (thr_state->over & THR_STATE_OVER_READ_FROM_CLIENT)) + if (!(old_over & THR_TASK_OVER_READ_FROM_CLIENT) && (thr_task->over & THR_TASK_OVER_READ_FROM_CLIENT)) { - if (hio_dev_sck_read(thr_state->client->sck, 0) <= -1) + if (thr_task->csck && hio_dev_sck_read(thr_task->csck, 0) <= -1) { - HIO_DEBUG2 (thr_state->htts->hio, "HTTS(%p) - halting client(%p) for failure to disable input watching\n", thr_state->htts, thr_state->client->sck); - hio_dev_sck_halt (thr_state->client->sck); + HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting client for failure to disable input watching\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); + hio_dev_sck_halt (thr_task->csck); } } - if (!(old_over & THR_STATE_OVER_READ_FROM_PEER) && (thr_state->over & THR_STATE_OVER_READ_FROM_PEER)) + if (!(old_over & THR_TASK_OVER_READ_FROM_PEER) && (thr_task->over & THR_TASK_OVER_READ_FROM_PEER)) { - if (thr_state->peer && hio_dev_thr_read(thr_state->peer, 0) <= -1) + if (thr_task->peer && hio_dev_thr_read(thr_task->peer, 0) <= -1) { - HIO_DEBUG2 (thr_state->htts->hio, "HTTS(%p) - halting peer(%p) for failure to disable input watching\n", thr_state->htts, thr_state->peer); - hio_dev_thr_halt (thr_state->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting peer for failure to disable input watching\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); + hio_dev_thr_halt (thr_task->peer); } } - if (old_over != THR_STATE_OVER_ALL && thr_state->over == THR_STATE_OVER_ALL) + if (old_over != THR_TASK_OVER_ALL && thr_task->over == THR_TASK_OVER_ALL) { /* ready to stop */ - if (thr_state->peer) + if (thr_task->peer) { - HIO_DEBUG2 (thr_state->htts->hio, "HTTS(%p) - halting peer(%p) as it is unneeded\n", thr_state->htts, thr_state->peer); - hio_dev_thr_halt (thr_state->peer); + HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting peer as it is unneeded\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); + hio_dev_thr_halt (thr_task->peer); } - if (thr_state->keep_alive && !thr_state->client_eof_detected) + if (thr_task->csck) { - /* how to arrange to delete this thr_state object and put the socket back to the normal waiting state??? */ - HIO_ASSERT (thr_state->htts->hio, thr_state->client->rsrc == (hio_svc_htts_rsrc_t*)thr_state); + HIO_ASSERT (hio, thr_task->client != HIO_NULL); - HIO_SVC_HTTS_RSRC_DETACH (thr_state->client->rsrc); - /* thr_state must not be access from here down as it could have been destroyed */ - } - else - { - HIO_DEBUG2 (thr_state->htts->hio, "HTTS(%p) - halting client(%p) for no keep-alive\n", thr_state->htts, thr_state->client->sck); - hio_dev_sck_shutdown (thr_state->client->sck, HIO_DEV_SCK_SHUTDOWN_WRITE); - hio_dev_sck_halt (thr_state->client->sck); + if (thr_task->keep_alive && !thr_task->client_eof_detected) + { + /* how to arrange to delete this thr_task object and put the socket back to the normal waiting state??? */ + HIO_ASSERT (thr_task->htts->hio, thr_task->client->rsrc == (hio_svc_htts_rsrc_t*)thr_task); + HIO_SVC_HTTS_RSRC_UNREF (thr_task->client->rsrc); + /* IMPORTANT: thr_task must not be accessed from here down as it could have been destroyed */ + } + else + { + HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - halting client for no keep-alive\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); + hio_dev_sck_shutdown (thr_task->csck, HIO_DEV_SCK_SHUTDOWN_WRITE); + hio_dev_sck_halt (thr_task->csck); + } } } } -static void thr_state_on_kill (thr_state_t* thr_state) +static void thr_task_on_kill (hio_svc_htts_rsrc_t* rsrc) { - hio_t* hio = thr_state->htts->hio; + thr_task_t* thr_task = (thr_task_t*)rsrc; + hio_t* hio = thr_task->htts->hio; - HIO_DEBUG2 (hio, "HTTS(%p) - killing thr_state client(%p)\n", thr_state->htts, thr_state->client->sck); + HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - killing the task\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); - if (thr_state->peer) + if (thr_task->peer) { - thr_peer_xtn_t* thr_peer = hio_dev_thr_getxtn(thr_state->peer); - thr_peer->state = HIO_NULL; /* thr_peer->state many not be NULL if the resource is killed regardless of the reference count */ - - hio_dev_thr_kill (thr_state->peer); - thr_state->peer = HIO_NULL; - } - - if (thr_state->peer_htrd) - { - thr_peer_xtn_t* thr_peer = hio_htrd_getxtn(thr_state->peer_htrd); - thr_peer->state = HIO_NULL; /* thr_peer->state many not be NULL if the resource is killed regardless of the reference count */ - - hio_htrd_close (thr_state->peer_htrd); - thr_state->peer_htrd = HIO_NULL; - } - - if (thr_state->client_org_on_read) - { - thr_state->client->sck->on_read = thr_state->client_org_on_read; - thr_state->client_org_on_read = HIO_NULL; - } - - if (thr_state->client_org_on_write) - { - thr_state->client->sck->on_write = thr_state->client_org_on_write; - thr_state->client_org_on_write = HIO_NULL; - } - - - if (thr_state->client_org_on_disconnect) - { - thr_state->client->sck->on_disconnect = thr_state->client_org_on_disconnect; - thr_state->client_org_on_disconnect = HIO_NULL; - } - - if (thr_state->client_htrd_recbs_changed) - { - /* restore the callbacks */ - hio_htrd_setrecbs (thr_state->client->htrd, &thr_state->client_htrd_org_recbs); - } - - if (!thr_state->client_disconnected) - { -/*printf ("ENABLING INPUT WATCHING on CLIENT %p. \n", thr_state->client->sck);*/ - if (!thr_state->keep_alive || hio_dev_sck_read(thr_state->client->sck, 1) <= -1) + thr_peer_xtn_t* thr_peer = hio_dev_thr_getxtn(thr_task->peer); + if (thr_peer->task) { - HIO_DEBUG2 (hio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", thr_state->htts, thr_state->client->sck); - hio_dev_sck_halt (thr_state->client->sck); + /* thr_peer->task may not be NULL if the resource is killed regardless of the reference count. + * anyway, don't use HIO_SVC_HTTS_RSRC_UNREF (thr_peer->task) because the resource itself + * is already being killed. */ + thr_peer->task = HIO_NULL; + } + + hio_dev_thr_kill (thr_task->peer); + thr_task->peer = HIO_NULL; + } + + if (thr_task->peer_htrd) + { + thr_peer_xtn_t* thr_peer = hio_htrd_getxtn(thr_task->peer_htrd); + if (thr_peer->task) thr_peer->task = HIO_NULL; // no HIO_SVC_HTTS_RSRC_UNREF() for the same reason above + + hio_htrd_close (thr_task->peer_htrd); + thr_task->peer_htrd = HIO_NULL; + } + + if (thr_task->csck) + { + HIO_ASSERT (hio, thr_task->client != HIO_NULL); + + /* restore callbacks */ + if (thr_task->client_org_on_read) thr_task->csck->on_read = thr_task->client_org_on_read; + if (thr_task->client_org_on_write) thr_task->csck->on_write = thr_task->client_org_on_write; + if (thr_task->client_org_on_disconnect) thr_task->csck->on_disconnect = thr_task->client_org_on_disconnect; + if (thr_task->client_htrd_recbs_changed) hio_htrd_setrecbs (thr_task->client->htrd, &thr_task->client_htrd_org_recbs); + + if (!thr_task->keep_alive || hio_dev_sck_read(thr_task->csck, 1) <= -1) + { + HIO_DEBUG2 (hio, "HTTS(%p) - halting client(%p) for failure to enable input watching\n", thr_task->htts, thr_task->csck); + hio_dev_sck_halt (thr_task->csck); } } -/*printf ("**** THR_STATE_ON_KILL DONE\n");*/ + thr_task->client_org_on_read = HIO_NULL; + thr_task->client_org_on_write = HIO_NULL; + thr_task->client_org_on_disconnect = HIO_NULL; + thr_task->client_htrd_recbs_changed = 0; + +/////HIO_SVC_HTTS_TASKL_UNLINK_CLI (thr_task); /* detached from the htts service */ + HIO_DEBUG5 (hio, "HTTS(%p) - thr(t=%p,c=%p[%d],p=%p) - killed the task\n", thr_task->htts, thr_task, thr_task->client, (thr_task->csck? thr_task->csck->hnd: -1), thr_task->peer); } static void thr_peer_on_close (hio_dev_thr_t* thr, hio_dev_thr_sid_t sid) { hio_t* hio = thr->hio; thr_peer_xtn_t* thr_peer = (thr_peer_xtn_t*)hio_dev_thr_getxtn(thr); - thr_state_t* thr_state = thr_peer->state; + thr_task_t* thr_task = thr_peer->task; - if (!thr_state) return; /* thr state already gone */ + if (!thr_task) return; /* thr task already gone */ switch (sid) { case HIO_DEV_THR_MASTER: - HIO_DEBUG2 (hio, "HTTS(%p) - peer %p closing master\n", thr_state->client->htts, thr); - thr_state->peer = HIO_NULL; /* clear this peer from the state */ + HIO_DEBUG2 (hio, "HTTS(%p) - peer %p closing master\n", thr_task->htts, thr); + thr_task->peer = HIO_NULL; /* clear this peer from the state */ - HIO_ASSERT (hio, thr_peer->state != HIO_NULL); - HIO_SVC_HTTS_RSRC_DETACH (thr_peer->state); + HIO_ASSERT (hio, thr_peer->task != HIO_NULL); + HIO_SVC_HTTS_RSRC_UNREF (thr_peer->task); - if (thr_state->peer_htrd) + if (thr_task->peer_htrd) { /* once this peer device is closed, peer's htrd is also never used. * it's safe to detach the extra information attached on the htrd object. */ - thr_peer = hio_htrd_getxtn(thr_state->peer_htrd); - HIO_ASSERT (hio, thr_peer->state != HIO_NULL); - HIO_SVC_HTTS_RSRC_DETACH (thr_peer->state); + thr_peer = hio_htrd_getxtn(thr_task->peer_htrd); + HIO_ASSERT (hio, thr_peer->task != HIO_NULL); + HIO_SVC_HTTS_RSRC_UNREF (thr_peer->task); } break; case HIO_DEV_THR_OUT: - HIO_ASSERT (hio, thr_state->peer == thr); - HIO_DEBUG3 (hio, "HTTS(%p) - peer %p closing slave[%d]\n", thr_state->client->htts, thr, sid); + HIO_ASSERT (hio, thr_task->peer == thr); + HIO_DEBUG3 (hio, "HTTS(%p) - peer %p closing slave[%d]\n", thr_task->htts, thr, sid); - if (!(thr_state->over & THR_STATE_OVER_READ_FROM_PEER)) + if (!(thr_task->over & THR_TASK_OVER_READ_FROM_PEER)) { - if (thr_state_write_last_chunk_to_client(thr_state) <= -1) - thr_state_halt_participating_devices (thr_state); + if (thr_task_write_last_chunk_to_client(thr_task) <= -1) + thr_task_halt_participating_devices (thr_task); else - thr_state_mark_over (thr_state, THR_STATE_OVER_READ_FROM_PEER); + thr_task_mark_over (thr_task, THR_TASK_OVER_READ_FROM_PEER); } break; case HIO_DEV_THR_IN: - thr_state_mark_over (thr_state, THR_STATE_OVER_WRITE_TO_PEER); + thr_task_mark_over (thr_task, THR_TASK_OVER_WRITE_TO_PEER); break; default: - HIO_DEBUG3 (hio, "HTTS(%p) - peer %p closing slave[%d]\n", thr_state->client->htts, thr, sid); + HIO_DEBUG3 (hio, "HTTS(%p) - peer %p closing slave[%d]\n", thr_task->htts, thr, sid); /* do nothing */ break; } @@ -362,49 +370,49 @@ static int thr_peer_on_read (hio_dev_thr_t* thr, const void* data, hio_iolen_t d { hio_t* hio = thr->hio; thr_peer_xtn_t* thr_peer = (thr_peer_xtn_t*)hio_dev_thr_getxtn(thr); - thr_state_t* thr_state = thr_peer->state; + thr_task_t* thr_task = thr_peer->task; - HIO_ASSERT (hio, thr_state != HIO_NULL); + HIO_ASSERT (hio, thr_task != HIO_NULL); if (dlen <= -1) { - HIO_DEBUG2 (hio, "HTTPS(%p) - read error from peer %p\n", thr_state->client->htts, thr); + HIO_DEBUG2 (hio, "HTTPS(%p) - read error from peer %p\n", thr_task->htts, thr); goto oops; } if (dlen == 0) { - HIO_DEBUG2 (hio, "HTTPS(%p) - EOF from peer %p\n", thr_state->client->htts, thr); + HIO_DEBUG2 (hio, "HTTPS(%p) - EOF from peer %p\n", thr_task->htts, thr); - if (!(thr_state->over & THR_STATE_OVER_READ_FROM_PEER)) + if (!(thr_task->over & THR_TASK_OVER_READ_FROM_PEER)) { /* the thr script could be misbehaviing. * it still has to read more but EOF is read. * otherwise client_peer_htrd_poke() should have been called */ - if (thr_state_write_last_chunk_to_client(thr_state) <= -1) goto oops; - thr_state_mark_over (thr_state, THR_STATE_OVER_READ_FROM_PEER); + if (thr_task_write_last_chunk_to_client(thr_task) <= -1) goto oops; + thr_task_mark_over (thr_task, THR_TASK_OVER_READ_FROM_PEER); } } else { hio_oow_t rem; - HIO_ASSERT (hio, !(thr_state->over & THR_STATE_OVER_READ_FROM_PEER)); + HIO_ASSERT (hio, !(thr_task->over & THR_TASK_OVER_READ_FROM_PEER)); - if (hio_htrd_feed(thr_state->peer_htrd, data, dlen, &rem) <= -1) + if (hio_htrd_feed(thr_task->peer_htrd, data, dlen, &rem) <= -1) { - HIO_DEBUG2 (hio, "HTTPS(%p) - unable to feed peer htrd - peer %p\n", thr_state->htts, thr); + HIO_DEBUG2 (hio, "HTTPS(%p) - unable to feed peer htrd - peer %p\n", thr_task->htts, thr); - if (!thr_state->ever_attempted_to_write_to_client && - !(thr_state->over & THR_STATE_OVER_WRITE_TO_CLIENT)) + if (!thr_task->ever_attempted_to_write_to_client && + !(thr_task->over & THR_TASK_OVER_WRITE_TO_CLIENT)) { - thr_state_send_final_status_to_client (thr_state, HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR, 1); /* don't care about error because it jumps to oops below anyway */ + thr_task_send_final_status_to_client (thr_task, HIO_HTTP_STATUS_INTERNAL_SERVER_ERROR, 1); /* don't care about error because it jumps to oops below anyway */ } - goto oops; + goto oops; } - if (rem > 0) + if (rem > 0) { /* If the script specifies Content-Length and produces longer data, it will come here */ /*printf ("AAAAAAAAAAAAAAAAAa EEEEEXcessive DATA..................\n");*/ @@ -415,7 +423,7 @@ static int thr_peer_on_read (hio_dev_thr_t* thr, const void* data, hio_iolen_t d return 0; oops: - thr_state_halt_participating_devices (thr_state); + thr_task_halt_participating_devices (thr_task); return 0; } @@ -451,15 +459,15 @@ static int thr_peer_capture_response_header (hio_htre_t* req, const hio_bch_t* k static int thr_peer_htrd_peek (hio_htrd_t* htrd, hio_htre_t* req) { thr_peer_xtn_t* thr_peer = hio_htrd_getxtn(htrd); - thr_state_t* thr_state = thr_peer->state; - hio_svc_htts_cli_t* cli = thr_state->client; + thr_task_t* thr_task = thr_peer->task; + hio_svc_htts_cli_t* cli = thr_task->client; hio_bch_t dtbuf[64]; int status_code = HIO_HTTP_STATUS_OK; if (req->attr.content_length) { // TOOD: remove content_length if content_length is negative or not numeric. - thr_state->res_mode_to_cli = THR_STATE_RES_MODE_LENGTH; + thr_task->res_mode_to_cli = THR_TASK_RES_MODE_LENGTH; } if (req->attr.status) @@ -475,61 +483,61 @@ static int thr_peer_htrd_peek (hio_htrd_t* htrd, hio_htre_t* req) hio_svc_htts_fmtgmtime (cli->htts, HIO_NULL, dtbuf, HIO_COUNTOF(dtbuf)); if (hio_becs_fmt(cli->sbuf, "HTTP/%d.%d %d %hs\r\nServer: %hs\r\nDate: %hs\r\n", - thr_state->req_version.major, thr_state->req_version.minor, + thr_task->req_version.major, thr_task->req_version.minor, status_code, hio_http_status_to_bcstr(status_code), cli->htts->server_name, dtbuf) == (hio_oow_t)-1) return -1; if (hio_htre_walkheaders(req, thr_peer_capture_response_header, cli) <= -1) return -1; - switch (thr_state->res_mode_to_cli) + switch (thr_task->res_mode_to_cli) { - case THR_STATE_RES_MODE_CHUNKED: + case THR_TASK_RES_MODE_CHUNKED: if (hio_becs_cat(cli->sbuf, "Transfer-Encoding: chunked\r\n") == (hio_oow_t)-1) return -1; /*if (hio_becs_cat(cli->sbuf, "Connection: keep-alive\r\n") == (hio_oow_t)-1) return -1;*/ break; - case THR_STATE_RES_MODE_CLOSE: + case THR_TASK_RES_MODE_CLOSE: if (hio_becs_cat(cli->sbuf, "Connection: close\r\n") == (hio_oow_t)-1) return -1; break; - case THR_STATE_RES_MODE_LENGTH: - if (hio_becs_cat(cli->sbuf, (thr_state->keep_alive? "Connection: keep-alive\r\n": "Connection: close\r\n")) == (hio_oow_t)-1) return -1; + case THR_TASK_RES_MODE_LENGTH: + if (hio_becs_cat(cli->sbuf, (thr_task->keep_alive? "Connection: keep-alive\r\n": "Connection: close\r\n")) == (hio_oow_t)-1) return -1; } if (hio_becs_cat(cli->sbuf, "\r\n") == (hio_oow_t)-1) return -1; - return thr_state_write_to_client(thr_state, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)); + return thr_task_write_to_client(thr_task, HIO_BECS_PTR(cli->sbuf), HIO_BECS_LEN(cli->sbuf)); } static int thr_peer_htrd_poke (hio_htrd_t* htrd, hio_htre_t* req) { /* client request got completed */ thr_peer_xtn_t* thr_peer = hio_htrd_getxtn(htrd); - thr_state_t* thr_state = thr_peer->state; + thr_task_t* thr_task = thr_peer->task; - if (thr_state_write_last_chunk_to_client(thr_state) <= -1) return -1; + if (thr_task_write_last_chunk_to_client(thr_task) <= -1) return -1; - thr_state_mark_over (thr_state, THR_STATE_OVER_READ_FROM_PEER); + thr_task_mark_over (thr_task, THR_TASK_OVER_READ_FROM_PEER); return 0; } static int thr_peer_htrd_push_content (hio_htrd_t* htrd, hio_htre_t* req, const hio_bch_t* data, hio_oow_t dlen) { thr_peer_xtn_t* thr_peer = hio_htrd_getxtn(htrd); - thr_state_t* thr_state = thr_peer->state; + thr_task_t* thr_task = thr_peer->task; - HIO_ASSERT (thr_state->client->htts->hio, htrd == thr_state->peer_htrd); + HIO_ASSERT (thr_task->htts->hio, htrd == thr_task->peer_htrd); - switch (thr_state->res_mode_to_cli) + switch (thr_task->res_mode_to_cli) { - case THR_STATE_RES_MODE_CHUNKED: + case THR_TASK_RES_MODE_CHUNKED: { hio_iovec_t iov[3]; hio_bch_t lbuf[16]; hio_oow_t llen; /* hio_fmt_uintmax_to_bcstr() null-terminates the output. only HIO_COUNTOF(lbuf) - 1 - * is enough to hold '\r' and '\n' at the back without '\0'. */ + * is enough to hold '\r' and '\n' at the back without '\0'. */ llen = hio_fmt_uintmax_to_bcstr(lbuf, HIO_COUNTOF(lbuf) - 1, dlen, 16 | HIO_FMT_UINTMAX_UPPERCASE, 0, '\0', HIO_NULL); lbuf[llen++] = '\r'; lbuf[llen++] = '\n'; @@ -541,25 +549,25 @@ static int thr_peer_htrd_push_content (hio_htrd_t* htrd, hio_htre_t* req, const iov[2].iov_ptr = "\r\n"; iov[2].iov_len = 2; - if (thr_state_writev_to_client(thr_state, iov, HIO_COUNTOF(iov)) <= -1) + if (thr_task_writev_to_client(thr_task, iov, HIO_COUNTOF(iov)) <= -1) { goto oops; } break; } - case THR_STATE_RES_MODE_CLOSE: - case THR_STATE_RES_MODE_LENGTH: - if (thr_state_write_to_client(thr_state, data, dlen) <= -1) + case THR_TASK_RES_MODE_CLOSE: + case THR_TASK_RES_MODE_LENGTH: + if (thr_task_write_to_client(thr_task, data, dlen) <= -1) { goto oops; } break; } - if (thr_state->num_pending_writes_to_client > THR_STATE_PENDING_IO_THRESHOLD) + if (thr_task->num_pending_writes_to_client > THR_TASK_PENDING_IO_THRESHOLD) { - if (hio_dev_thr_read(thr_state->peer, 0) <= -1) + if (hio_dev_thr_read(thr_task->peer, 0) <= -1) { goto oops; } @@ -584,12 +592,12 @@ static int thr_client_htrd_poke (hio_htrd_t* htrd, hio_htre_t* req) hio_svc_htts_cli_htrd_xtn_t* htrdxtn = (hio_svc_htts_cli_htrd_xtn_t*)hio_htrd_getxtn(htrd); hio_dev_sck_t* sck = htrdxtn->sck; hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck); - thr_state_t* thr_state = (thr_state_t*)cli->rsrc; + thr_task_t* thr_task = (thr_task_t*)cli->rsrc; /* indicate EOF to the client peer */ - if (thr_state_write_to_peer(thr_state, HIO_NULL, 0) <= -1) return -1; + if (thr_task_write_to_peer(thr_task, HIO_NULL, 0) <= -1) return -1; - thr_state_mark_over (thr_state, THR_STATE_OVER_READ_FROM_CLIENT); + thr_task_mark_over (thr_task, THR_TASK_OVER_READ_FROM_CLIENT); return 0; } @@ -598,10 +606,10 @@ static int thr_client_htrd_push_content (hio_htrd_t* htrd, hio_htre_t* req, cons hio_svc_htts_cli_htrd_xtn_t* htrdxtn = (hio_svc_htts_cli_htrd_xtn_t*)hio_htrd_getxtn(htrd); hio_dev_sck_t* sck = htrdxtn->sck; hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck); - thr_state_t* thr_state = (thr_state_t*)cli->rsrc; + thr_task_t* thr_task = (thr_task_t*)cli->rsrc; HIO_ASSERT (sck->hio, cli->sck == sck); - return thr_state_write_to_peer(thr_state, data, dlen); + return thr_task_write_to_peer(thr_task, data, dlen); } static hio_htrd_recbs_t thr_client_htrd_recbs = @@ -615,15 +623,15 @@ static int thr_peer_on_write (hio_dev_thr_t* thr, hio_iolen_t wrlen, void* wrctx { hio_t* hio = thr->hio; thr_peer_xtn_t* thr_peer = (thr_peer_xtn_t*)hio_dev_thr_getxtn(thr); - thr_state_t* thr_state = thr_peer->state; + thr_task_t* thr_task = thr_peer->task; - if (thr_state == HIO_NULL) return 0; /* there is nothing i can do. the thr_state is being cleared or has been cleared already. */ + if (!thr_task) return 0; /* there is nothing i can do. the thr_task is being cleared or has been cleared already. */ - HIO_ASSERT (hio, thr_state->peer == thr); + HIO_ASSERT (hio, thr_task->peer == thr); if (wrlen <= -1) { - HIO_DEBUG2 (hio, "HTTS(%p) - unable to write to peer %p\n", thr_state->client->htts, thr); + HIO_DEBUG2 (hio, "HTTS(%p) - unable to write to peer %p\n", thr_task->htts, thr); goto oops; } else if (wrlen == 0) @@ -631,50 +639,66 @@ static int thr_peer_on_write (hio_dev_thr_t* thr, hio_iolen_t wrlen, void* wrctx /* indicated EOF */ /* do nothing here as i didn't incremented num_pending_writes_to_peer when making the write request */ - thr_state->num_pending_writes_to_peer--; - HIO_ASSERT (hio, thr_state->num_pending_writes_to_peer == 0); - HIO_DEBUG2 (hio, "HTTS(%p) - indicated EOF to peer %p\n", thr_state->client->htts, thr); + thr_task->num_pending_writes_to_peer--; + HIO_ASSERT (hio, thr_task->num_pending_writes_to_peer == 0); + HIO_DEBUG2 (hio, "HTTS(%p) - indicated EOF to peer %p\n", thr_task->htts, thr); /* indicated EOF to the peer side. i need no more data from the client side. * i don't need to enable input watching in the client side either */ - thr_state_mark_over (thr_state, THR_STATE_OVER_WRITE_TO_PEER); + thr_task_mark_over (thr_task, THR_TASK_OVER_WRITE_TO_PEER); } else { - HIO_ASSERT (hio, thr_state->num_pending_writes_to_peer > 0); + HIO_ASSERT (hio, thr_task->num_pending_writes_to_peer > 0); - thr_state->num_pending_writes_to_peer--; - if (thr_state->num_pending_writes_to_peer == THR_STATE_PENDING_IO_THRESHOLD) + thr_task->num_pending_writes_to_peer--; + if (thr_task->num_pending_writes_to_peer == THR_TASK_PENDING_IO_THRESHOLD) { - if (!(thr_state->over & THR_STATE_OVER_READ_FROM_CLIENT) && - hio_dev_sck_read(thr_state->client->sck, 1) <= -1) goto oops; + if (!(thr_task->over & THR_TASK_OVER_READ_FROM_CLIENT) && + hio_dev_sck_read(thr_task->csck, 1) <= -1) goto oops; } - if ((thr_state->over & THR_STATE_OVER_READ_FROM_CLIENT) && thr_state->num_pending_writes_to_peer <= 0) + if ((thr_task->over & THR_TASK_OVER_READ_FROM_CLIENT) && thr_task->num_pending_writes_to_peer <= 0) { - thr_state_mark_over (thr_state, THR_STATE_OVER_WRITE_TO_PEER); + thr_task_mark_over (thr_task, THR_TASK_OVER_WRITE_TO_PEER); } } return 0; oops: - thr_state_halt_participating_devices (thr_state); + thr_task_halt_participating_devices (thr_task); return 0; } static void thr_client_on_disconnect (hio_dev_sck_t* sck) { hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck); - thr_state_t* thr_state = (thr_state_t*)cli->rsrc; - thr_state->client_disconnected = 1; - thr_state->client_org_on_disconnect (sck); + thr_task_t* thr_task = (thr_task_t*)cli->rsrc; + hio_svc_htts_t* htts = thr_task->htts; + hio_t* hio = sck->hio; + + HIO_ASSERT (hio, sck = thr_task->csck); + HIO_DEBUG4 (hio, "HTTS(%p) - thr(t=%p,c=%p,csck=%p) - client socket disconnect notified\n", htts, thr_task, sck, cli); + + thr_task->client_disconnected = 1; + thr_task->csck = HIO_NULL; + thr_task->client = HIO_NULL; + if (thr_task->client_org_on_disconnect) + { + thr_task->client_org_on_disconnect (sck); + /* this original callback destroys the associated resource. + * thr_task must not be accessed from here down */ + } + + HIO_DEBUG4 (hio, "HTTS(%p) - thr(t=%p,c=%p,csck=%p) - client socket disconnect handled\n", htts, thr_task, sck, cli); + /* Note: after this callback, the actual device pointed to by 'sck' will be freed in the main loop. */ } static int thr_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t len, const hio_skad_t* srcaddr) { hio_t* hio = sck->hio; hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck); - thr_state_t* thr_state = (thr_state_t*)cli->rsrc; + thr_task_t* thr_task = (thr_task_t*)cli->rsrc; HIO_ASSERT (hio, sck == cli->sck); @@ -685,7 +709,7 @@ static int thr_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t goto oops; } - if (!thr_state->peer) + if (!thr_task->peer) { /* the peer is gone */ goto oops; /* do what? just return 0? */ @@ -694,14 +718,14 @@ static int thr_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t if (len == 0) { /* EOF on the client side. arrange to close */ - HIO_DEBUG3 (hio, "HTTPS(%p) - EOF from client %p(hnd=%d)\n", thr_state->client->htts, sck, (int)sck->hnd); - thr_state->client_eof_detected = 1; + HIO_DEBUG3 (hio, "HTTPS(%p) - EOF from client %p(hnd=%d)\n", thr_task->htts, sck, (int)sck->hnd); + thr_task->client_eof_detected = 1; - if (!(thr_state->over & THR_STATE_OVER_READ_FROM_CLIENT)) /* if this is true, EOF is received without thr_client_htrd_poke() */ + if (!(thr_task->over & THR_TASK_OVER_READ_FROM_CLIENT)) /* if this is true, EOF is received without thr_client_htrd_poke() */ { int n; - n = thr_state_write_to_peer(thr_state, HIO_NULL, 0); - thr_state_mark_over (thr_state, THR_STATE_OVER_READ_FROM_CLIENT); + n = thr_task_write_to_peer(thr_task, HIO_NULL, 0); + thr_task_mark_over (thr_task, THR_TASK_OVER_READ_FROM_CLIENT); if (n <= -1) goto oops; } } @@ -709,7 +733,7 @@ static int thr_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t { hio_oow_t rem; - HIO_ASSERT (hio, !(thr_state->over & THR_STATE_OVER_READ_FROM_CLIENT)); + HIO_ASSERT (hio, !(thr_task->over & THR_TASK_OVER_READ_FROM_CLIENT)); if (hio_htrd_feed(cli->htrd, buf, len, &rem) <= -1) goto oops; @@ -723,7 +747,7 @@ static int thr_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t return 0; oops: - thr_state_halt_participating_devices (thr_state); + thr_task_halt_participating_devices (thr_task); return 0; } @@ -731,7 +755,7 @@ static int thr_client_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wrc { hio_t* hio = sck->hio; hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(sck); - thr_state_t* thr_state = (thr_state_t*)cli->rsrc; + thr_task_t* thr_task = (thr_task_t*)cli->rsrc; if (wrlen <= -1) { @@ -742,51 +766,52 @@ static int thr_client_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wrc if (wrlen == 0) { /* if the connect is keep-alive, this part may not be called */ - thr_state->num_pending_writes_to_client--; - HIO_ASSERT (hio, thr_state->num_pending_writes_to_client == 0); - HIO_DEBUG3 (hio, "HTTS(%p) - indicated EOF to client %p(%d)\n", thr_state->client->htts, sck, (int)sck->hnd); + thr_task->num_pending_writes_to_client--; + HIO_ASSERT (hio, thr_task->num_pending_writes_to_client == 0); + HIO_DEBUG3 (hio, "HTTS(%p) - indicated EOF to client %p(%d)\n", thr_task->htts, sck, (int)sck->hnd); /* since EOF has been indicated to the client, it must not write to the client any further. * this also means that i don't need any data from the peer side either. * i don't need to enable input watching on the peer side */ - thr_state_mark_over (thr_state, THR_STATE_OVER_WRITE_TO_CLIENT); + thr_task_mark_over (thr_task, THR_TASK_OVER_WRITE_TO_CLIENT); } else { - HIO_ASSERT (hio, thr_state->num_pending_writes_to_client > 0); + HIO_ASSERT (hio, thr_task->num_pending_writes_to_client > 0); - thr_state->num_pending_writes_to_client--; - if (thr_state->peer && thr_state->num_pending_writes_to_client == THR_STATE_PENDING_IO_THRESHOLD) + thr_task->num_pending_writes_to_client--; + if (thr_task->peer && thr_task->num_pending_writes_to_client == THR_TASK_PENDING_IO_THRESHOLD) { - if (!(thr_state->over & THR_STATE_OVER_READ_FROM_PEER) && - hio_dev_thr_read(thr_state->peer, 1) <= -1) goto oops; + if (!(thr_task->over & THR_TASK_OVER_READ_FROM_PEER) && + hio_dev_thr_read(thr_task->peer, 1) <= -1) goto oops; } - if ((thr_state->over & THR_STATE_OVER_READ_FROM_PEER) && thr_state->num_pending_writes_to_client <= 0) + if ((thr_task->over & THR_TASK_OVER_READ_FROM_PEER) && thr_task->num_pending_writes_to_client <= 0) { - thr_state_mark_over (thr_state, THR_STATE_OVER_WRITE_TO_CLIENT); + thr_task_mark_over (thr_task, THR_TASK_OVER_WRITE_TO_CLIENT); } } return 0; oops: - thr_state_halt_participating_devices (thr_state); + thr_task_halt_participating_devices (thr_task); return 0; } static void free_thr_start_info (void* ctx) { thr_func_start_t* tfs = (thr_func_start_t*)ctx; - if (tfs->tfi.req_path) hio_freemem (tfs->hio, tfs->tfi.req_path); - if (tfs->tfi.req_param) hio_freemem (tfs->hio, tfs->tfi.req_param); - hio_freemem (tfs->hio, tfs); + hio_t* hio = tfs->htts->hio; + if (tfs->tfi.req_path) hio_freemem (hio, tfs->tfi.req_path); + if (tfs->tfi.req_param) hio_freemem (hio, tfs->tfi.req_param); + hio_freemem (hio, tfs); } static void thr_func (hio_t* hio, hio_dev_thr_iopair_t* iop, void* ctx) { thr_func_start_t* tfs = (thr_func_start_t*)ctx; pthread_cleanup_push (free_thr_start_info, tfs); - tfs->thr_func (hio, iop, &tfs->tfi, tfs->thr_ctx); + tfs->thr_func (tfs->htts, iop, &tfs->tfi, tfs->thr_ctx); pthread_cleanup_pop (1); } @@ -821,10 +846,11 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r { hio_t* hio = htts->hio; hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck); - thr_state_t* thr_state = HIO_NULL; + thr_task_t* thr_task = HIO_NULL; thr_peer_xtn_t* thr_peer; hio_dev_thr_make_t mi; thr_func_start_t* tfs; + int have_content; /* ensure that you call this function before any contents is received */ HIO_ASSERT (hio, hio_htre_getcontentlen(req) == 0); @@ -832,11 +858,10 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r tfs = hio_callocmem(hio, HIO_SIZEOF(*tfs)); if (!tfs) goto oops; - tfs->hio = hio; + tfs->htts = htts; tfs->thr_func = func; tfs->thr_ctx = ctx; - tfs->tfi.htts = htts; tfs->tfi.req_method = hio_htre_getqmethodtype(req); tfs->tfi.req_version = *hio_htre_getversion(req); tfs->tfi.req_path = hio_dupbcstr(hio, hio_htre_getqpath(req), HIO_NULL); @@ -860,56 +885,63 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r mi.on_write = thr_peer_on_write; mi.on_close = thr_peer_on_close; - thr_state = (thr_state_t*)hio_svc_htts_rsrc_make(htts, HIO_SIZEOF(*thr_state), thr_state_on_kill); - if (HIO_UNLIKELY(!thr_state)) goto oops; + thr_task = (thr_task_t*)hio_svc_htts_rsrc_make(htts, HIO_SIZEOF(*thr_task), thr_task_on_kill); + if (HIO_UNLIKELY(!thr_task)) goto oops; - thr_state->options = options; - thr_state->client = cli; - /*thr_state->num_pending_writes_to_client = 0; - thr_state->num_pending_writes_to_peer = 0;*/ - thr_state->req_version = *hio_htre_getversion(req); - thr_state->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &thr_state->req_content_length); + thr_task->options = options; + thr_task->csck = csck; + thr_task->client = cli; /* for faster access without going through csck. */ - thr_state->client_org_on_read = csck->on_read; - thr_state->client_org_on_write = csck->on_write; - thr_state->client_org_on_disconnect = csck->on_disconnect; + /*thr_task->num_pending_writes_to_client = 0; + thr_task->num_pending_writes_to_peer = 0;*/ + thr_task->req_version = *hio_htre_getversion(req); + thr_task->req_content_length_unlimited = hio_htre_getreqcontentlen(req, &thr_task->req_content_length); + + thr_task->client_org_on_read = csck->on_read; + thr_task->client_org_on_write = csck->on_write; + thr_task->client_org_on_disconnect = csck->on_disconnect; csck->on_read = thr_client_on_read; csck->on_write = thr_client_on_write; csck->on_disconnect = thr_client_on_disconnect; + /* attach the thr task to the client socket via the rsrc field in the extended space of the socket */ HIO_ASSERT (hio, cli->rsrc == HIO_NULL); - HIO_SVC_HTTS_RSRC_ATTACH ((hio_svc_htts_rsrc_t*)thr_state, cli->rsrc); + HIO_SVC_HTTS_RSRC_REF ((hio_svc_htts_rsrc_t*)thr_task, cli->rsrc); - thr_state->peer = hio_dev_thr_make(hio, HIO_SIZEOF(*thr_peer), &mi); - if (HIO_UNLIKELY(!thr_state->peer)) - { + thr_task->peer = hio_dev_thr_make(hio, HIO_SIZEOF(*thr_peer), &mi); + if (HIO_UNLIKELY(!thr_task->peer)) + { + /* no need to detach the attached rsrc here because that is handled + * in the kill/disconnect callbacks of relevant devices */ HIO_DEBUG3 (hio, "HTTS(%p) - failed to create thread for %p(%d)\n", htts, csck, (int)csck->hnd); - goto oops; + goto oops; } tfs = HIO_NULL; /* mark that tfs is delegated to the thread */ - thr_peer = hio_dev_thr_getxtn(thr_state->peer); - HIO_SVC_HTTS_RSRC_ATTACH ((hio_svc_htts_rsrc_t*)thr_state, thr_peer->state); + /* attach the thr task to the peer thread device */ + thr_peer = hio_dev_thr_getxtn(thr_task->peer); + HIO_SVC_HTTS_RSRC_REF ((hio_svc_htts_rsrc_t*)thr_task, thr_peer->task); - thr_state->peer_htrd = hio_htrd_open(hio, HIO_SIZEOF(*thr_peer)); - if (HIO_UNLIKELY(!thr_state->peer_htrd)) goto oops; - hio_htrd_setoption (thr_state->peer_htrd, HIO_HTRD_SKIP_INITIAL_LINE | HIO_HTRD_RESPONSE); - hio_htrd_setrecbs (thr_state->peer_htrd, &thr_peer_htrd_recbs); + thr_task->peer_htrd = hio_htrd_open(hio, HIO_SIZEOF(*thr_peer)); + if (HIO_UNLIKELY(!thr_task->peer_htrd)) goto oops; + hio_htrd_setoption (thr_task->peer_htrd, HIO_HTRD_SKIP_INITIAL_LINE | HIO_HTRD_RESPONSE); + hio_htrd_setrecbs (thr_task->peer_htrd, &thr_peer_htrd_recbs); - thr_peer = hio_htrd_getxtn(thr_state->peer_htrd); - HIO_SVC_HTTS_RSRC_ATTACH ((hio_svc_htts_rsrc_t*)thr_state, thr_peer->state); + /* attach the thr task to the htrd parser set on the peer thread device */ + thr_peer = hio_htrd_getxtn(thr_task->peer_htrd); + HIO_SVC_HTTS_RSRC_REF ((hio_svc_htts_rsrc_t*)thr_task, thr_peer->task); #if !defined(THR_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH) - if (thr_state->req_content_length_unlimited) + if (thr_task->req_content_length_unlimited) { /* Transfer-Encoding is chunked. no content-length is known in advance. */ - + /* option 1. buffer contents. if it gets too large, send 413 Request Entity Too Large. * option 2. send 411 Length Required immediately * option 3. set Content-Length to -1 and use EOF to indicate the end of content [Non-Standard] */ - if (thr_state_send_final_status_to_client(thr_state, HIO_HTTP_STATUS_LENGTH_REQUIRED, 1) <= -1) goto oops; + if (thr_task_send_final_status_to_client(thr_task, HIO_HTTP_STATUS_LENGTH_REQUIRED, 1) <= -1) goto oops; } #endif @@ -919,15 +951,15 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r /* CAN I LET the thr SCRIPT handle this? */ if (!(options & HIO_SVC_HTTS_THR_NO_100_CONTINUE) && hio_comp_http_version_numbers(&req->version, 1, 1) >= 0 && - (thr_state->req_content_length_unlimited || thr_state->req_content_length > 0)) + (thr_task->req_content_length_unlimited || thr_task->req_content_length > 0)) { - /* + /* * Don't send 100 Continue if http verions is lower than 1.1 - * [RFC7231] + * [RFC7231] * A server that receives a 100-continue expectation in an HTTP/1.0 * request MUST ignore that expectation. * - * Don't send 100 Continue if expected content lenth is 0. + * Don't send 100 Continue if expected content lenth is 0. * [RFC7231] * A server MAY omit sending a 100 (Continue) response if it has * already received some or all of the message body for the @@ -937,69 +969,61 @@ int hio_svc_htts_dothr (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r hio_bch_t msgbuf[64]; hio_oow_t msglen; - msglen = hio_fmttobcstr(hio, msgbuf, HIO_COUNTOF(msgbuf), "HTTP/%d.%d %d %hs\r\n\r\n", thr_state->req_version.major, thr_state->req_version.minor, HIO_HTTP_STATUS_CONTINUE, hio_http_status_to_bcstr(HIO_HTTP_STATUS_CONTINUE)); - if (thr_state_write_to_client(thr_state, msgbuf, msglen) <= -1) goto oops; - thr_state->ever_attempted_to_write_to_client = 0; /* reset this as it's polluted for 100 continue */ + msglen = hio_fmttobcstr(hio, msgbuf, HIO_COUNTOF(msgbuf), "HTTP/%d.%d %d %hs\r\n\r\n", thr_task->req_version.major, thr_task->req_version.minor, HIO_HTTP_STATUS_CONTINUE, hio_http_status_to_bcstr(HIO_HTTP_STATUS_CONTINUE)); + if (thr_task_write_to_client(thr_task, msgbuf, msglen) <= -1) goto oops; + thr_task->ever_attempted_to_write_to_client = 0; /* reset this as it's polluted for 100 continue */ } } else if (req->flags & HIO_HTRE_ATTR_EXPECT) { /* 417 Expectation Failed */ - thr_state_send_final_status_to_client(thr_state, HIO_HTTP_STATUS_EXPECTATION_FAILED, 1); + thr_task_send_final_status_to_client(thr_task, HIO_HTTP_STATUS_EXPECTATION_FAILED, 1); goto oops; } #if defined(THR_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH) - if (thr_state->req_content_length_unlimited) + have_content = thr_task->req_content_length > 0 || thr_task->req_content_length_unlimited; +#else + have_content = thr_task->req_content_length > 0; +#endif + if (have_content) { /* change the callbacks to subscribe to contents to be uploaded */ - thr_state->client_htrd_org_recbs = *hio_htrd_getrecbs(thr_state->client->htrd); - thr_client_htrd_recbs.peek = thr_state->client_htrd_org_recbs.peek; - hio_htrd_setrecbs (thr_state->client->htrd, &thr_client_htrd_recbs); - thr_state->client_htrd_recbs_changed = 1; + thr_task->client_htrd_org_recbs = *hio_htrd_getrecbs(thr_task->client->htrd); + thr_client_htrd_recbs.peek = thr_task->client_htrd_org_recbs.peek; + hio_htrd_setrecbs (thr_task->client->htrd, &thr_client_htrd_recbs); + thr_task->client_htrd_recbs_changed = 1; } else { -#endif - if (thr_state->req_content_length > 0) - { - /* change the callbacks to subscribe to contents to be uploaded */ - thr_state->client_htrd_org_recbs = *hio_htrd_getrecbs(thr_state->client->htrd); - thr_client_htrd_recbs.peek = thr_state->client_htrd_org_recbs.peek; - hio_htrd_setrecbs (thr_state->client->htrd, &thr_client_htrd_recbs); - thr_state->client_htrd_recbs_changed = 1; - } - else - { - /* no content to be uploaded from the client */ - /* indicate EOF to the peer and disable input wathching from the client */ - if (thr_state_write_to_peer(thr_state, HIO_NULL, 0) <= -1) goto oops; - thr_state_mark_over (thr_state, THR_STATE_OVER_READ_FROM_CLIENT | THR_STATE_OVER_WRITE_TO_PEER); - } -#if defined(THR_ALLOW_UNLIMITED_REQ_CONTENT_LENGTH) + /* no content to be uploaded from the client */ + /* indicate EOF to the peer and disable input wathching from the client */ + if (thr_task_write_to_peer(thr_task, HIO_NULL, 0) <= -1) goto oops; + thr_task_mark_over (thr_task, THR_TASK_OVER_READ_FROM_CLIENT | THR_TASK_OVER_WRITE_TO_PEER); } -#endif /* this may change later if Content-Length is included in the thr output */ if (req->flags & HIO_HTRE_ATTR_KEEPALIVE) { - thr_state->keep_alive = 1; - thr_state->res_mode_to_cli = THR_STATE_RES_MODE_CHUNKED; - /* the mode still can get switched to THR_STATE_RES_MODE_LENGTH if the thr script emits Content-Length */ + thr_task->keep_alive = 1; + thr_task->res_mode_to_cli = THR_TASK_RES_MODE_CHUNKED; + /* the mode still can get switched to THR_TASK_RES_MODE_LENGTH if the thr script emits Content-Length */ } else { - thr_state->keep_alive = 0; - thr_state->res_mode_to_cli = THR_STATE_RES_MODE_CLOSE; + thr_task->keep_alive = 0; + thr_task->res_mode_to_cli = THR_TASK_RES_MODE_CLOSE; } - /* TODO: store current input watching state and use it when destroying the thr_state data */ - if (hio_dev_sck_read(csck, !(thr_state->over & THR_STATE_OVER_READ_FROM_CLIENT)) <= -1) goto oops; + /* TODO: store current input watching state and use it when destroying the thr_task data */ + if (hio_dev_sck_read(csck, !(thr_task->over & THR_TASK_OVER_READ_FROM_CLIENT)) <= -1) goto oops; + +// HIO_SVC_HTTS_TASKL_APPEND_TASK(htts->task, thr_task); return 0; oops: HIO_DEBUG2 (hio, "HTTS(%p) - FAILURE in dothr - socket(%p)\n", htts, csck); if (tfs) free_thr_start_info (tfs); - if (thr_state) thr_state_halt_participating_devices (thr_state); + if (thr_task) thr_task_halt_participating_devices (thr_task); return -1; } diff --git a/lib/http-txt.c b/lib/http-txt.c index 47a893a..80c2239 100644 --- a/lib/http-txt.c +++ b/lib/http-txt.c @@ -64,7 +64,7 @@ static void txt_halt_participating_devices (txt_t* txt) static int txt_write_to_client (txt_t* txt, const void* data, hio_iolen_t dlen) { txt->num_pending_writes_to_client++; - if (hio_dev_sck_write(txt->client->sck, data, dlen, HIO_NULL, HIO_NULL) <= -1) + if (hio_dev_sck_write(txt->client->sck, data, dlen, HIO_NULL, HIO_NULL) <= -1) { txt->num_pending_writes_to_client--; return -1; @@ -76,7 +76,7 @@ static int txt_write_to_client (txt_t* txt, const void* data, hio_iolen_t dlen) static int txt_writev_to_client (txt_t* txt, hio_iovec_t* iov, hio_iolen_t iovcnt) { txt->num_pending_writes_to_client++; - if (hio_dev_sck_writev(txt->client->sck, iov, iovcnt, HIO_NULL, HIO_NULL) <= -1) + if (hio_dev_sck_writev(txt->client->sck, iov, iovcnt, HIO_NULL, HIO_NULL) <= -1) { txt->num_pending_writes_to_client--; return -1; @@ -125,7 +125,7 @@ static HIO_INLINE void txt_mark_over (txt_t* txt, int over_bits) if (!(old_over & TXT_OVER_READ_FROM_CLIENT) && (txt->over & TXT_OVER_READ_FROM_CLIENT)) { - if (hio_dev_sck_read(txt->client->sck, 0) <= -1) + if (hio_dev_sck_read(txt->client->sck, 0) <= -1) { HIO_DEBUG2 (txt->htts->hio, "HTTS(%p) - halting client(%p) for failure to disable input watching\n", txt->htts, txt->client->sck); hio_dev_sck_halt (txt->client->sck); @@ -141,7 +141,7 @@ static HIO_INLINE void txt_mark_over (txt_t* txt, int over_bits) HIO_ASSERT (txt->htts->hio, txt->client->rsrc == (hio_svc_htts_rsrc_t*)txt); /*printf ("DETACHING FROM THE MAIN CLIENT RSRC... state -> %p\n", txt->client->rsrc);*/ - HIO_SVC_HTTS_RSRC_DETACH (txt->client->rsrc); + HIO_SVC_HTTS_RSRC_UNREF (txt->client->rsrc); /* txt must not be access from here down as it could have been destroyed */ } else @@ -153,8 +153,9 @@ static HIO_INLINE void txt_mark_over (txt_t* txt, int over_bits) } } -static void txt_on_kill (txt_t* txt) +static void txt_on_kill (hio_svc_htts_rsrc_t* rsrc) { + txt_t* txt = (txt_t*)rsrc; hio_t* hio = txt->htts->hio; HIO_DEBUG2 (hio, "HTTS(%p) - killing txt client(%p)\n", txt->htts, txt->client->sck); @@ -180,7 +181,7 @@ static void txt_on_kill (txt_t* txt) if (txt->client_htrd_recbs_changed) { /* restore the callbacks */ - hio_htrd_setrecbs (txt->client->htrd, &txt->client_htrd_org_recbs); + hio_htrd_setrecbs (txt->client->htrd, &txt->client_htrd_org_recbs); } if (!txt->client_disconnected) @@ -251,7 +252,7 @@ static int txt_client_on_read (hio_dev_sck_t* sck, const void* buf, hio_iolen_t /* EOF on the client side. arrange to close */ HIO_DEBUG3 (hio, "HTTPS(%p) - EOF from client %p(hnd=%d)\n", txt->client->htts, sck, (int)sck->hnd); txt->client_eof_detected = 1; - + if (!(txt->over & TXT_OVER_READ_FROM_CLIENT)) /* if this is true, EOF is received without txt_client_htrd_poke() */ { txt_mark_over (txt, TXT_OVER_READ_FROM_CLIENT); @@ -345,7 +346,7 @@ int hio_svc_htts_dotxt (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* r csck->on_disconnect = txt_client_on_disconnect; HIO_ASSERT (hio, cli->rsrc == HIO_NULL); - HIO_SVC_HTTS_RSRC_ATTACH ((hio_svc_htts_rsrc_t*)txt, cli->rsrc); + HIO_SVC_HTTS_RSRC_REF ((hio_svc_htts_rsrc_t*)txt, cli->rsrc); if (req->flags & HIO_HTRE_ATTR_EXPECT100) { diff --git a/lib/http.c b/lib/http.c index a93419c..f828a1d 100644 --- a/lib/http.c +++ b/lib/http.c @@ -56,7 +56,7 @@ const hio_bch_t* hio_http_status_to_bcstr (int code) case HIO_HTTP_STATUS_NO_CONTENT: msg = "No Content"; break; case HIO_HTTP_STATUS_RESET_CONTENT: msg = "Reset Content"; break; case HIO_HTTP_STATUS_PARTIAL_CONTENT: msg = "Partial Content"; break; - + case 300: msg = "Multiple Choices"; break; case HIO_HTTP_STATUS_MOVED_PERMANENTLY: msg = "Moved Permanently"; break; case 302: msg = "Found"; break; @@ -118,7 +118,7 @@ const hio_bch_t* hio_http_method_to_bcstr (hio_http_method_t type) "OPTIONS", "TRACE", "CONNECT" - }; + }; return (type < 0 || type >= HIO_COUNTOF(names))? HIO_NULL: names[type]; } @@ -161,10 +161,10 @@ hio_http_method_t hio_bcstr_to_http_method (const hio_bch_t* name) entry = &mtab[mid]; n = hio_comp_bcstr(name, entry->name, 1); - if (n < 0) + if (n < 0) { /* if left, right, mid were of hio_oow_t, - * you would need the following line. + * you would need the following line. if (mid == 0) break; */ right = mid - 1; @@ -194,10 +194,10 @@ hio_http_method_t hio_bchars_to_http_method (const hio_bch_t* nameptr, hio_oow_t entry = &mtab[mid]; n = hio_comp_bchars_bcstr(nameptr, namelen, entry->name, 1); - if (n < 0) + if (n < 0) { /* if left, right, mid were of hio_oow_t, - * you would need the following line. + * you would need the following line. if (mid == 0) break; */ right = mid - 1; @@ -211,7 +211,7 @@ hio_http_method_t hio_bchars_to_http_method (const hio_bch_t* nameptr, hio_oow_t int hio_parse_http_range_bcstr (const hio_bch_t* str, hio_http_range_t* range) { - /* NOTE: this function does not support a range set + /* NOTE: this function does not support a range set * like bytes=1-20,30-50 */ hio_foff_t from, to; @@ -264,7 +264,7 @@ struct mname_t const hio_bch_t* s; const hio_bch_t* l; }; - + static mname_t wday_name[] = { { "Sun", "Sunday" }, @@ -387,7 +387,7 @@ hio_bch_t* hio_fmt_http_time_to_bcstr (const hio_ntime_t* nt, hio_bch_t* buf, hi t = nt->sec; gmtime_r (&t, &bt); /* TODO: create hio_sys_gmtime() and make it system dependent */ - hio_fmttobcstr (HIO_NULL, buf, bufsz, + hio_fmttobcstr (HIO_NULL, buf, bufsz, "%hs, %d %hs %d %02d:%02d:%02d GMT", wday_name[bt.tm_wday].s, bt.tm_mday, @@ -412,7 +412,7 @@ int hio_is_perenced_http_bcstr (const hio_bch_t* str) { /* return true if the first valid percent-encoded sequence is found */ int w = HIO_XDIGIT_TO_NUM(*(p + 2)); - if (w >= 0) return 1; + if (w >= 0) return 1; } } @@ -628,7 +628,8 @@ int hio_scan_http_qparam (hio_bch_t* qparam, int (*qparamcb) (hio_bcs_t* key, hi hio_oow_t hio_escape_html_bchars (const hio_bch_t* str, hio_oow_t sln, hio_bch_t* buf, hio_oow_t len) { - hio_bch_t* ptr, * end = str + sln; + hio_bch_t* ptr; + const hio_bch_t* end = str + sln; hio_oow_t reqlen = 0; for (ptr = (hio_bch_t*)str; ptr < end; ptr++) diff --git a/lib/json.c b/lib/json.c index fc3b486..24ba5bd 100644 --- a/lib/json.c +++ b/lib/json.c @@ -57,7 +57,7 @@ static int add_char_to_token (hio_json_t* json, hio_ooch_t ch, int handle_surrog json->tok.ptr = tmp; } -#if (HIO_SIZEOF_OOCH_T >= 4) +#if (HIO_SIZEOF_OOCH_T >= 4) if (handle_surrogate_pair && ch >= 0xDC00 && ch <= 0xDFFF && json->tok.len > 0) { /* RFC7159 @@ -87,7 +87,7 @@ static int add_char_to_token (hio_json_t* json, hio_ooch_t ch, int handle_surrog static int add_chars_to_token (hio_json_t* json, const hio_ooch_t* ptr, hio_oow_t len) { hio_oow_t i; - + if (json->tok_capa - json->tok.len > len) { hio_ooch_t* tmp; @@ -135,7 +135,7 @@ static int push_read_state (hio_json_t* json, hio_json_state_t state) ss->index = 0; ss->in_comment = 0; ss->next = json->state_stack; - + json->state_stack = ss; return 0; } @@ -177,7 +177,7 @@ static int invoke_data_inst (hio_json_t* json, hio_json_inst_t inst) if (ss->state == HIO_JSON_STATE_IN_OBJECT) { - if (ss->u.io.state == 1) + if (ss->u.io.state == 1) { /* just got the key part. the colon has not been seen. */ @@ -194,7 +194,7 @@ static int invoke_data_inst (hio_json_t* json, hio_json_inst_t inst) inst = HIO_JSON_INST_KEY; } - else + else { /* if this variable is non-zero, level is set to 0 regardless of actual level. * this helps the callback to print the value without indentation immediately @@ -204,7 +204,7 @@ static int invoke_data_inst (hio_json_t* json, hio_json_inst_t inst) } } - if (inst == __INST_WORD_STRING) + if (inst == __INST_WORD_STRING) { hio_seterrbfmt (json->hio, HIO_EINVAL, "invalid word value - %.*js at line %zu:%zu", json->tok.len, json->tok.ptr, json->tok_line, json->tok_col); return -1; @@ -324,7 +324,7 @@ static int handle_string_value_char (hio_json_t* json, hio_ooci_t c) } else if (json->state_stack->u.sv.escaped == 1) { - if (c >= '0' && c <= '8') + if (c >= '0' && c <= '8') { json->state_stack->u.sv.escaped = 3; json->state_stack->u.sv.digit_count = 0; @@ -787,7 +787,7 @@ static int feed_json_data (hio_json_t* json, const hio_bch_t* data, hio_oow_t le else if (n > bcslen) { /* incomplete sequence */ - *xlen = ptr - data; + *xlen = ptr - data; return 0; /* feed more for incomplete sequence */ } @@ -808,14 +808,14 @@ static int feed_json_data (hio_json_t* json, const hio_bch_t* data, hio_oow_t le json->c_col++; } - if (json->state_stack->in_comment) + if (json->state_stack->in_comment) { if (c == HIO_EOL) json->state_stack->in_comment = 0; continue; } if (json->state_stack->state == HIO_JSON_STATE_START && hio_is_ooch_space(c)) continue; /* skip white space */ - if (stop_if_ever_completed && ever_completed) + if (stop_if_ever_completed && ever_completed) { *xlen = optr - data; return 2; @@ -889,7 +889,7 @@ int hio_json_init (hio_json_t* json, hio_t* hio) void hio_json_fini (hio_json_t* json) { pop_all_read_states (json); - if (json->tok.ptr) + if (json->tok.ptr) { hio_freemem (json->hio, json->tok.ptr); json->tok.ptr = HIO_NULL; @@ -947,7 +947,7 @@ int hio_json_feed (hio_json_t* json, const void* ptr, hio_oow_t len, hio_oow_t* total += ylen; if (x == 0) break; /* incomplete sequence encountered */ - if (stop_if_ever_completed && x >= 2) + if (stop_if_ever_completed && x >= 2) { if (rem) *rem = len - total; return 1; @@ -1116,7 +1116,7 @@ static int write_bytes_noesc (hio_jsonwr_t* jsonwr, const hio_bch_t* dptr, hio_o { rem = HIO_COUNTOF(jsonwr->wbuf) - jsonwr->wbuf_len; - if (dlen <= rem) + if (dlen <= rem) { HIO_MEMCPY (&jsonwr->wbuf[jsonwr->wbuf_len], dptr, dlen); jsonwr->wbuf_len += dlen; @@ -1205,7 +1205,7 @@ static int write_uchars (hio_jsonwr_t* jsonwr, int escape, const hio_uch_t* ptr, { no_escape: n = hio->_cmgr->uctobc(*ptr, bcsbuf, HIO_COUNTOF(bcsbuf)); - if (n == 0) + if (n == 0) { hio_seterrnum (hio, HIO_EECERR); return -1; @@ -1256,7 +1256,7 @@ int hio_jsonwr_write (hio_jsonwr_t* jsonwr, hio_json_inst_t inst, int is_uchars, WRITE_INDENT (jsonwr); } sn->obj_awaiting_val = 0; - WRITE_BYTES_NOESC (jsonwr, "[", 1); + WRITE_BYTES_NOESC (jsonwr, "[", 1); if (jsonwr->flags & HIO_JSONWR_FLAG_PRETTY) WRITE_LINE_BREAK (jsonwr); if (push_write_state(jsonwr, HIO_JSON_STATE_IN_ARRAY) <= -1) return -1; jsonwr->state_stack->level++; @@ -1264,7 +1264,7 @@ int hio_jsonwr_write (hio_jsonwr_t* jsonwr, hio_json_inst_t inst, int is_uchars, case HIO_JSON_INST_START_OBJECT: if (sn->state != HIO_JSON_STATE_START && sn->state != HIO_JSON_STATE_IN_ARRAY && - !(sn->state == HIO_JSON_STATE_IN_OBJECT && sn->obj_awaiting_val)) goto incompatible_inst; + !(sn->state == HIO_JSON_STATE_IN_OBJECT && sn->obj_awaiting_val)) goto incompatible_inst; if (sn->index > 0 && sn->state == HIO_JSON_STATE_IN_ARRAY) WRITE_COMMA (jsonwr); sn->index++; if ((jsonwr->flags & HIO_JSONWR_FLAG_PRETTY) && @@ -1273,7 +1273,7 @@ int hio_jsonwr_write (hio_jsonwr_t* jsonwr, hio_json_inst_t inst, int is_uchars, WRITE_INDENT (jsonwr); } sn->obj_awaiting_val = 0; - WRITE_BYTES_NOESC (jsonwr, "{", 1); + WRITE_BYTES_NOESC (jsonwr, "{", 1); if (jsonwr->flags & HIO_JSONWR_FLAG_PRETTY) WRITE_LINE_BREAK (jsonwr); if (push_write_state (jsonwr, HIO_JSON_STATE_IN_OBJECT) <= -1) return -1; jsonwr->state_stack->level++; @@ -1282,13 +1282,13 @@ int hio_jsonwr_write (hio_jsonwr_t* jsonwr, hio_json_inst_t inst, int is_uchars, case HIO_JSON_INST_END_ARRAY: if (sn->state != HIO_JSON_STATE_IN_ARRAY) goto incompatible_inst; pop_write_state (jsonwr); - if (jsonwr->flags & HIO_JSONWR_FLAG_PRETTY) + if (jsonwr->flags & HIO_JSONWR_FLAG_PRETTY) { WRITE_LINE_BREAK (jsonwr); WRITE_INDENT (jsonwr); } - WRITE_BYTES_NOESC (jsonwr, "]", 1); - if (jsonwr->state_stack->state == HIO_JSON_STATE_START) + WRITE_BYTES_NOESC (jsonwr, "]", 1); + if (jsonwr->state_stack->state == HIO_JSON_STATE_START) { /* end of json */ if (jsonwr->flags & HIO_JSONWR_FLAG_PRETTY) WRITE_LINE_BREAK (jsonwr); @@ -1299,13 +1299,13 @@ int hio_jsonwr_write (hio_jsonwr_t* jsonwr, hio_json_inst_t inst, int is_uchars, case HIO_JSON_INST_END_OBJECT: if (sn->state != HIO_JSON_STATE_IN_OBJECT || sn->obj_awaiting_val) goto incompatible_inst; pop_write_state (jsonwr); - if (jsonwr->flags & HIO_JSONWR_FLAG_PRETTY) + if (jsonwr->flags & HIO_JSONWR_FLAG_PRETTY) { WRITE_LINE_BREAK (jsonwr); WRITE_INDENT (jsonwr); } - WRITE_BYTES_NOESC (jsonwr, "}", 1); - if (jsonwr->state_stack->state == HIO_JSON_STATE_START) + WRITE_BYTES_NOESC (jsonwr, "}", 1); + if (jsonwr->state_stack->state == HIO_JSON_STATE_START) { /* end of json */ if (jsonwr->flags & HIO_JSONWR_FLAG_PRETTY) WRITE_LINE_BREAK (jsonwr); @@ -1333,7 +1333,7 @@ int hio_jsonwr_write (hio_jsonwr_t* jsonwr, hio_json_inst_t inst, int is_uchars, PREACTION_FOR_VALUE (jsonwr, sn); WRITE_BYTES_NOESC (jsonwr, "true", 4); break; - + case HIO_JSON_INST_FALSE: PREACTION_FOR_VALUE (jsonwr, sn); WRITE_BYTES_NOESC (jsonwr, "false", 5); diff --git a/lib/mar-cli.c b/lib/mar-cli.c index d101ef7..98fee9c 100644 --- a/lib/mar-cli.c +++ b/lib/mar-cli.c @@ -106,7 +106,7 @@ hio_svc_marc_t* hio_svc_marc_start (hio_t* hio, const hio_svc_marc_connect_t* ci marc->hio = hio; marc->svc_stop = hio_svc_marc_stop; marc->ci = *ci; - if (tmout) + if (tmout) { marc->tmout = *tmout; marc->tmout_set = 1; @@ -131,7 +131,7 @@ void hio_svc_marc_stop (hio_svc_marc_t* marc) for (i = 0; i < marc->sess.capa; i++) { - if (marc->sess.ptr[i].dev) + if (marc->sess.ptr[i].dev) { hio_dev_mar_kill (marc->sess.ptr[i].dev); } @@ -205,7 +205,7 @@ static int send_pending_query_if_any (sess_t* sess) { sq->sent = 1; /*printf ("sending... %.*s\n", (int)sq->qlen, sq->qptr);*/ - if (hio_dev_mar_querywithbchars(sess->dev, sq->qptr, sq->qlen) <= -1) + if (hio_dev_mar_querywithbchars(sess->dev, sq->qptr, sq->qlen) <= -1) { HIO_DEBUG2 (sess->svc->hio, "MARC(%p) - SEND FAIL %js\n", sess->dev, hio_geterrmsg(sess->svc->hio)); sq->sent = 0; @@ -232,7 +232,7 @@ static void mar_on_disconnect (hio_dev_mar_t* dev) if (xtn->sid == INVALID_SID) return; /* this session data is not set if there's failure in alloc_device() */ sess = &xtn->svc->sess.ptr[xtn->sid]; - HIO_DEBUG6 (hio, "MARC(%p) - device disconnected - sid %lu session %p session-connected %d device %p device-broken %d\n", sess->svc, (unsigned long int)sess->sid, sess, (int)sess->connected, dev, (int)dev->broken); + HIO_DEBUG6 (hio, "MARC(%p) - device disconnected - sid %lu session %p session-connected %d device %p device-broken %d\n", sess->svc, (unsigned long int)sess->sid, sess, (int)sess->connected, dev, (int)dev->broken); HIO_ASSERT (hio, dev == sess->dev); if (HIO_UNLIKELY(!sess->svc->stopping && hio->stopreq == HIO_STOPREQ_NONE)) @@ -291,7 +291,7 @@ static void mar_on_connect (hio_dev_mar_t* dev) HIO_ASSERT (hio, xtn->sid != INVALID_SID); sess = &xtn->svc->sess.ptr[xtn->sid]; - HIO_DEBUG5 (hio, "MARC(%p) - device connected - sid %lu session %p device %p device-broken %d\n", sess->svc, (unsigned long int)sess->sid, sess, dev, dev->broken); + HIO_DEBUG5 (hio, "MARC(%p) - device connected - sid %lu session %p device %p device-broken %d\n", sess->svc, (unsigned long int)sess->sid, sess, dev, dev->broken); sess->connected = 1; send_pending_query_if_any (sess); @@ -353,7 +353,7 @@ static void mar_on_row_fetched (hio_dev_mar_t* dev, void* data) sq->on_result (sess->svc, sess->sid, (data? HIO_SVC_MARC_RCODE_ROW: HIO_SVC_MARC_RCODE_DONE), data, sq->qctx); - if (!data) + if (!data) { dequeue_session_query (sess->svc->hio, sess); send_pending_query_if_any (sess); @@ -387,7 +387,7 @@ static hio_dev_mar_t* alloc_device (hio_svc_marc_t* marc, hio_oow_t sid) xtn->svc = marc; xtn->sid = sid; - if (hio_dev_mar_connect(mar, &marc->ci) <= -1) + if (hio_dev_mar_connect(mar, &marc->ci) <= -1) { /* connection failed immediately */ xtn->sid = INVALID_SID; @@ -424,7 +424,7 @@ static sess_t* get_session (hio_svc_marc_t* marc, hio_oow_t flagged_sid) sess = &marc->sess.ptr[i]; if (sess->dev) { - if (!get_first_session_query(sess)) + if (!get_first_session_query(sess)) { marc->autoi = i; sid = marc->autoi; @@ -438,7 +438,7 @@ static sess_t* get_session (hio_svc_marc_t* marc, hio_oow_t flagged_sid) sess = &marc->sess.ptr[i]; if (sess->dev) { - if (!get_first_session_query(sess)) + if (!get_first_session_query(sess)) { marc->autoi = i; sid = marc->autoi; @@ -505,16 +505,16 @@ got_sid: if (HIO_UNLIKELY(!sq)) return HIO_NULL; sess->dev = alloc_device(marc, sess->sid); - if (HIO_UNLIKELY(!sess->dev)) + if (HIO_UNLIKELY(!sess->dev)) { free_session_query (hio, sq); return HIO_NULL; } - /* queue initialization with a place holder. the queue maintains a placeholder node. + /* queue initialization with a place holder. the queue maintains a placeholder node. * the first actual data node enqueued is inserted at the back and becomes the second - * node in terms of the entire queue. - * + * node in terms of the entire queue. + * * PH -> DN1 -> DN2 -> ... -> DNX * ^ ^ * q_head q_tail @@ -522,7 +522,7 @@ got_sid: * get_first_session_query() returns the data of DN1, not the data held in PH. * * the first dequeing operations kills PH. - * + * * DN1 -> DN2 -> ... -> DNX * ^ ^ * q_head q_tail @@ -565,7 +565,7 @@ int hio_svc_marc_querywithbchars (hio_svc_marc_t* marc, hio_oow_t flagged_sid, h HIO_ASSERT (hio, sq->sent == 0); sq->sent = 1; - if (hio_dev_mar_querywithbchars(sess->dev, sq->qptr, sq->qlen) <= -1) + if (hio_dev_mar_querywithbchars(sess->dev, sq->qptr, sq->qlen) <= -1) { sq->sent = 0; if (!sess->dev->broken) diff --git a/lib/mar.c b/lib/mar.c index f4e5cae..4d0a67f 100644 --- a/lib/mar.c +++ b/lib/mar.c @@ -79,7 +79,7 @@ static void desched_connect_timeout (hio_dev_t* dev) hio_t* hio = dev->hio; hio_dev_mar_t* rdev = (hio_dev_mar_t*)dev; - if (rdev->ctmridx != HIO_TMRIDX_INVALID) + if (rdev->ctmridx != HIO_TMRIDX_INVALID) { hio_deltmrjob (hio, rdev->ctmridx); HIO_ASSERT (hio, rdev->ctmridx == HIO_TMRIDX_INVALID); @@ -93,7 +93,7 @@ static int dev_mar_make (hio_dev_t* dev, void* ctx) hio_dev_mar_make_t* mi = (hio_dev_mar_make_t*)ctx; rdev->hnd = mysql_init(HIO_NULL); - if (HIO_UNLIKELY(!rdev->hnd)) + if (HIO_UNLIKELY(!rdev->hnd)) { hio_seterrnum (hio, HIO_ESYSMEM); return -1; @@ -118,10 +118,10 @@ static int dev_mar_make (hio_dev_t* dev, void* ctx) /* remember the timeout settings. use a negative second to indicate no timeout. * the saved values will be used in scheduling a timer job for each relevant operation. - * Timing out can't be implemented with the standard MYSQL TIMEOUT options in + * Timing out can't be implemented with the standard MYSQL TIMEOUT options in * the asynchronous mode. that is, this sample code doesn't work. unsigned int tmout; - tmout = mi->tmout.c.sec; // mysql supports the granularity of seconds only + tmout = mi->tmout.c.sec; // mysql supports the granularity of seconds only if (tmout >= 0) mysql_options(rdev->hnd, MYSQL_OPT_CONNECT_TIMEOUT, &tmout); tmout = mi->tmout.r.sec; if (tmout >= 0) mysql_options(rdev->hnd, MYSQL_OPT_READ_TIMEOUT, &tmout); @@ -157,16 +157,16 @@ static int dev_mar_kill (hio_dev_t* dev, int force) desched_connect_timeout (dev); - /* if rdev->connected is 0 at this point, + /* if rdev->connected is 0 at this point, * the underlying socket of this device is down */ if (HIO_LIKELY(rdev->on_disconnect)) rdev->on_disconnect (rdev); /* hack */ - if (!rdev->broken) + if (!rdev->broken) { /* mysql_free_result() blocks if not all rows have been read. * mysql_close() also blocks to transmit COM_QUIT, - * in this context, it is not appropriate to call + * in this context, it is not appropriate to call * mysql_free_result_start()/mysql_free_result_cont() and * mysql_close_start()/mysql_close_cont(). * let me just call shutdown on the underlying socket to work around this issue. @@ -186,7 +186,7 @@ static int dev_mar_kill (hio_dev_t* dev, int force) if (rdev->hnd) { - mysql_close (rdev->hnd); + mysql_close (rdev->hnd); rdev->hnd = HIO_NULL; } @@ -297,7 +297,7 @@ static int dev_mar_ioctl (hio_dev_t* dev, int cmd, void* arg) { if (HIO_UNLIKELY(!tmp)) /* connection attempt failed immediately */ { - /* immediate failure doesn't invoke on_discoonect(). + /* immediate failure doesn't invoke on_discoonect(). * the caller must check the return code of this function. */ rdev->connected = 0; @@ -348,7 +348,7 @@ static int dev_mar_ioctl (hio_dev_t* dev, int cmd, void* arg) else { /* query sent immediately */ - if (err) + if (err) { /* but there is an error */ if (err == 1 || err == -1) err = mysql_errno(rdev->hnd); @@ -365,7 +365,7 @@ static int dev_mar_ioctl (hio_dev_t* dev, int cmd, void* arg) rdev->connected = 0; rdev->broken = 1; /* remember the previous handle - this may be needed by the poll/select based multiplexer */ - rdev->broken_syshnd = syshnd; + rdev->broken_syshnd = syshnd; watch_mysql (rdev, 0); hio_dev_mar_halt (rdev); /* i can't keep this device alive regardless of the caller's post-action */ @@ -403,7 +403,7 @@ static int dev_mar_ioctl (hio_dev_t* dev, int cmd, void* arg) } } -static hio_dev_mth_t dev_mar_methods = +static hio_dev_mth_t dev_mar_methods = { dev_mar_make, dev_mar_kill, @@ -491,8 +491,8 @@ static int dev_evcb_mar_ready (hio_dev_t* dev, int events) { /* connection attempt failed */ - /* the mysql client library closes the underlying socket handle - * whenever the connection attempt fails. this prevents hio from + /* the mysql client library closes the underlying socket handle + * whenever the connection attempt fails. this prevents hio from * managing the the mysql connections properly. this also causes * race condition if this library is used in multi-threaded programs. */ @@ -500,21 +500,21 @@ static int dev_evcb_mar_ready (hio_dev_t* dev, int events) rdev->broken = 1; /* trick dev_mar_getsyshnd() to return rdev->broken_syshnd. */ /* remember the previous handle - this may be needed by the poll/select based multiplexer * mysql_get_socket() over a failed mariadb handle ends up with segfault */ - rdev->broken_syshnd = syshnd; + rdev->broken_syshnd = syshnd; /* this attempts to trigger the low-level multiplxer to delete 'syshnd' closed by mysql_real_connect_cont(). - * the underlying low-level operation may fail. but i don't care. the best is not to open + * the underlying low-level operation may fail. but i don't care. the best is not to open * new file descriptor between mysql_real_connect_cont() and watch_mysql(rdev, 0). - * + * * close(6); <- mysql_real_connect_cont(); * epoll_ctl(4, EPOLL_CTL_DEL, 6, 0x7ffc785e7154) = -1 EBADF (Bad file descriptor) <- by hio_dev_watch() in watch_mysql */ watch_mysql (rdev, 0); - /* on_disconnect() will be called without on_connect(). - * you may assume that the initial connection attempt failed. + /* on_disconnect() will be called without on_connect(). + * you may assume that the initial connection attempt failed. * reconnection doesn't apply in this context. */ - hio_dev_mar_halt (rdev); + hio_dev_mar_halt (rdev); } } } @@ -549,7 +549,7 @@ static int dev_evcb_mar_ready (hio_dev_t* dev, int events) if (err == CR_SERVER_LOST || err == CR_SERVER_GONE_ERROR || err == CR_COMMANDS_OUT_OF_SYNC || err == ER_CONNECTION_KILLED) { /* - preserving the error information here isn't very useful because + preserving the error information here isn't very useful because the info won't survive until on_disconnect() is called... hio_seterrbfmt (hio, HIO_ECONLOST, "%hs", mysql_error(rdev->hnd)); */ @@ -610,7 +610,7 @@ static int dev_evcb_mar_ready (hio_dev_t* dev, int events) if (!status) { /* row is available */ - if (!row) + if (!row) { /* the last row has been received - cleanup before invoking the callback */ watch_mysql (rdev, status); diff --git a/lib/md5.c b/lib/md5.c index 9d87140..fcb4194 100644 --- a/lib/md5.c +++ b/lib/md5.c @@ -25,7 +25,7 @@ #include #include "hio-prv.h" -/* +/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved. @@ -33,12 +33,12 @@ is identified as the "RSA Data Security, Inc. MD5 Message-Digest Algorithm" in all material mentioning or referencing this software or this function. - + License is also granted to make and use derivative works provided that such works are identified as "derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm" in all material mentioning or referencing the derived work. - + RSA Data Security, Inc. makes no representations concerning either the merchantability of this software or the suitability of this software for any particular purpose. It is provided "as is" @@ -136,12 +136,12 @@ void hio_md5_update (hio_md5_t* md5, const void* data, hio_uint32_t len) part_len = 64 - index; /* transform as many times as possible */ - if (len >= part_len) + if (len >= part_len) { HIO_MEMCPY (&md5->buffer[index], input, part_len); __transform (md5->state, md5->buffer); - for (i = part_len; i + 63 < len; i += 64) + for (i = part_len; i + 63 < len; i += 64) __transform (md5->state, &input[i]); index = 0; } @@ -286,8 +286,8 @@ static void __transform (hio_uint32_t state[4], hio_uint8_t block[64]) static void __encode (hio_uint8_t* output, hio_uint32_t* input, hio_oow_t len) { hio_oow_t i, j; - - for (i = 0, j = 0; j < len; i++, j += 4) + + for (i = 0, j = 0; j < len; i++, j += 4) { output[j + 0] = (hio_uint8_t)((input[i] >> 0) & 0xFF); output[j + 1] = (hio_uint8_t)((input[i] >> 8) & 0xFF); @@ -300,7 +300,7 @@ static void __decode (hio_uint32_t* output, hio_uint8_t* input, hio_oow_t len) { hio_oow_t i, j; - for (i = 0, j = 0; j < len; i++, j += 4) + for (i = 0, j = 0; j < len; i++, j += 4) { output[i] = input[j] | (input[j + 1] << 8) | (input[j + 2] << 16) | (input[j + 3] << 24); } diff --git a/lib/nwif.c b/lib/nwif.c index 01c50fb..f90faa4 100644 --- a/lib/nwif.c +++ b/lib/nwif.c @@ -57,8 +57,8 @@ static int get_sco_ifconf (hio_t* hio, struct ifconf* ifc) int h, num; struct ifreq* ifr; - h = socket(AF_INET, SOCK_DGRAM, 0); - if (h <= -1) + h = socket(AF_INET, SOCK_DGRAM, 0); + if (h <= -1) { hio_seterrwithsyserr (hio, 0, errno); return -1; @@ -83,7 +83,7 @@ static int get_sco_ifconf (hio_t* hio, struct ifconf* ifc) /* sco needs reboot when you add an network interface. * it should be safe not to consider the case when the interface - * is added after SIOCGIFANUM above. + * is added after SIOCGIFANUM above. * another thing to note is that SIOCGIFCONF ends with segfault * if the buffer is not large enough unlike some other OSes * like opensolaris which truncates the configuration. */ @@ -92,7 +92,7 @@ static int get_sco_ifconf (hio_t* hio, struct ifconf* ifc) ifc->ifc_buf = hio_allocmem(hio, ifc->ifc_len); if (ifc->ifc_buf == HIO_NULL) goto oops; - if (ioctl(h, SIOCGIFCONF, ifc) <= -1) + if (ioctl(h, SIOCGIFCONF, ifc) <= -1) { hio_seterrwithsyserr (hio, 0, errno); goto oops; @@ -134,7 +134,7 @@ int hio_bcstrtoifindex (hio_t* hio, const hio_bch_t* ptr, unsigned int* index) hio_oow_t len; struct ifreq ifr; - h = socket(AF_INET, SOCK_DGRAM, 0); + h = socket(AF_INET, SOCK_DGRAM, 0); if (h <= -1) { hio_seterrwithsyserr (hio, 0, errno); @@ -186,7 +186,7 @@ int hio_bcstrtoifindex (hio_t* hio, const hio_bch_t* ptr, unsigned int* index) num = ifc.ifc_len / HIO_SIZEOF(struct ifreq); for (i = 0; i < num; i++) { - if (hio_comp_bcstr(ptr, ifc.ifc_req[i].ifr_name, 0) == 0) + if (hio_comp_bcstr(ptr, ifc.ifc_req[i].ifr_name, 0) == 0) { free_sco_ifconf (hio, &ifc); *index = i + 1; @@ -221,8 +221,8 @@ int hio_bcharstoifindex (hio_t* hio, const hio_bch_t* ptr, hio_oow_t len, unsign int h, x; struct ifreq ifr; - h = socket(AF_INET, SOCK_DGRAM, 0); - if (h <= -1) + h = socket(AF_INET, SOCK_DGRAM, 0); + if (h <= -1) { hio_seterrwithsyserr (hio, 0, errno); return -1; @@ -269,7 +269,7 @@ int hio_bcharstoifindex (hio_t* hio, const hio_bch_t* ptr, hio_oow_t len, unsign num = ifc.ifc_len / HIO_SIZEOF(struct ifreq); for (i = 0; i < num; i++) { - if (hio_comp_bchars_bcstr(ptr, len, ifc.ifc_req[i].ifr_name) == 0) + if (hio_comp_bchars_bcstr(ptr, len, ifc.ifc_req[i].ifr_name) == 0) { free_sco_ifconf (hio, &ifc); *index = i + 1; @@ -305,8 +305,8 @@ int hio_ucstrtoifindex (hio_t* hio, const hio_uch_t* ptr, unsigned int* index) struct ifreq ifr; hio_oow_t wl, ml; - h = socket(AF_INET, SOCK_DGRAM, 0); - if (h <= -1) + h = socket(AF_INET, SOCK_DGRAM, 0); + if (h <= -1) { hio_seterrwithsyserr (hio, 0, errno); return -1; @@ -338,7 +338,7 @@ int hio_ucstrtoifindex (hio_t* hio, const hio_uch_t* ptr, unsigned int* index) if (hio_convutobcstr(hio, ptr, &wl, tmp, &ml) <= -1) return -1; tmpidx = if_nametoindex(tmp); - if (tmpidx == 0) + if (tmpidx == 0) { hio_seterrwithsyserr (hio, 0, errno); return -1; @@ -361,7 +361,7 @@ int hio_ucstrtoifindex (hio_t* hio, const hio_uch_t* ptr, unsigned int* index) num = ifc.ifc_len / HIO_SIZEOF(struct ifreq); for (i = 0; i < num; i++) { - if (hio_comp_bcstr(tmp, ifc.ifc_req[i].ifr_name, 0) == 0) + if (hio_comp_bcstr(tmp, ifc.ifc_req[i].ifr_name, 0) == 0) { free_sco_ifconf (hio, &ifc); *index = i + 1; @@ -397,8 +397,8 @@ int hio_ucharstoifindex (hio_t* hio, const hio_uch_t* ptr, hio_oow_t len, unsign struct ifreq ifr; hio_oow_t wl, ml; - h = socket(AF_INET, SOCK_DGRAM, 0); - if (h <= -1) + h = socket(AF_INET, SOCK_DGRAM, 0); + if (h <= -1) { hio_seterrwithsyserr (hio, 0, errno); return -1; @@ -432,7 +432,7 @@ int hio_ucharstoifindex (hio_t* hio, const hio_uch_t* ptr, hio_oow_t len, unsign tmp[ml] = '\0'; tmpidx = if_nametoindex(tmp); - if (tmpidx == 0) + if (tmpidx == 0) { hio_seterrwithsyserr (hio, 0, errno); return -1; @@ -455,7 +455,7 @@ int hio_ucharstoifindex (hio_t* hio, const hio_uch_t* ptr, hio_oow_t len, unsign num = ifc.ifc_len / HIO_SIZEOF(struct ifreq); for (i = 0; i < num; i++) { - if (hio_comp_bcstr(tmp, ifc.ifc_req[i].ifr_name, 0) == 0) + if (hio_comp_bcstr(tmp, ifc.ifc_req[i].ifr_name, 0) == 0) { free_sco_ifconf (hio, &ifc); *index = i + 1; @@ -492,8 +492,8 @@ int hio_ifindextobcstr (hio_t* hio, unsigned int index, hio_bch_t* buf, hio_oow_ int h, x; struct ifreq ifr; - h = socket(AF_INET, SOCK_DGRAM, 0); - if (h <= -1) + h = socket(AF_INET, SOCK_DGRAM, 0); + if (h <= -1) { hio_seterrwithsyserr (hio, 0, errno); return -1; @@ -505,7 +505,7 @@ int hio_ifindextobcstr (hio_t* hio, unsigned int index, hio_bch_t* buf, hio_oow_ #else ifr.ifr_index = index; #endif - + x = ioctl(h, SIOCGIFNAME, &ifr); close (h); @@ -519,7 +519,7 @@ int hio_ifindextobcstr (hio_t* hio, unsigned int index, hio_bch_t* buf, hio_oow_ #elif defined(HAVE_IF_INDEXTONAME) hio_bch_t tmp[IF_NAMESIZE + 1]; - if (if_indextoname (index, tmp) == HIO_NULL) + if (if_indextoname (index, tmp) == HIO_NULL) { hio_seterrwithsyserr (hio, 0, errno); return -1; @@ -536,7 +536,7 @@ int hio_ifindextobcstr (hio_t* hio, unsigned int index, hio_bch_t* buf, hio_oow_ if (get_sco_ifconf(hio, &ifc) <= -1) return -1; num = ifc.ifc_len / HIO_SIZEOF(struct ifreq); - if (index > num) + if (index > num) { hio_seterrnum (hio, HIO_ENOENT); free_sco_ifconf (hio, &ifc); @@ -573,7 +573,7 @@ int hio_ifindextoucstr (hio_t* hio, unsigned int index, hio_uch_t* buf, hio_oow_ struct ifreq ifr; hio_oow_t wl, ml; - h = socket(AF_INET, SOCK_DGRAM, 0); + h = socket(AF_INET, SOCK_DGRAM, 0); if (h <= -1) { hio_seterrwithsyserr (hio, 0, errno); @@ -607,7 +607,7 @@ int hio_ifindextoucstr (hio_t* hio, unsigned int index, hio_uch_t* buf, hio_oow_ hio_oow_t ml, wl; int x; - if (if_indextoname(index, tmp) == HIO_NULL) + if (if_indextoname(index, tmp) == HIO_NULL) { hio_seterrwithsyserr (hio, 0, errno); return -1; @@ -628,7 +628,7 @@ int hio_ifindextoucstr (hio_t* hio, unsigned int index, hio_uch_t* buf, hio_oow_ if (get_sco_ifconf(hio, &ifc) <= -1) return -1; num = ifc.ifc_len / HIO_SIZEOF(struct ifreq); - if (index > num) + if (index > num) { free_sco_ifconf (hio, &ifc); return -1; diff --git a/lib/opt-imp.h b/lib/opt-imp.h index de93f2e..18bd4c4 100644 --- a/lib/opt-imp.h +++ b/lib/opt-imp.h @@ -29,7 +29,7 @@ #include "hio-opt.h" #include "hio-utl.h" -/* +/* * hio_getopt is based on BSD getopt. * -------------------------------------------------------------------------- * @@ -73,18 +73,18 @@ xci_t xgetopt (int argc, xch_t* const* argv, xopt_t* opt) opt->arg = HIO_NULL; opt->lngopt = HIO_NULL; - if (opt->cur == HIO_NULL) + if (opt->cur == HIO_NULL) { opt->cur = XEMSG; opt->ind = 1; } - if (*opt->cur == '\0') + if (*opt->cur == '\0') { /* update scanning pointer */ - if (opt->ind >= argc || *(opt->cur = argv[opt->ind]) != '-') + if (opt->ind >= argc || *(opt->cur = argv[opt->ind]) != '-') { - /* All arguments have been processed or the current + /* All arguments have been processed or the current * argument doesn't start with a dash */ opt->cur = XEMSG; return XCI_EOF; @@ -125,7 +125,7 @@ xci_t xgetopt (int argc, xch_t* const* argv, xopt_t* opt) while (*end != '\0' && *end != '=') end++; - for (o = opt->lng; o->str; o++) + for (o = opt->lng; o->str; o++) { const xch_t* str = o->str; @@ -149,10 +149,10 @@ xci_t xgetopt (int argc, xch_t* const* argv, xopt_t* opt) } else if (opt->arg == HIO_NULL) { - /* check if it has a remaining argument + /* check if it has a remaining argument * available */ - if (argc <= ++opt->ind) return BADARG; - /* If so, the next available argument is + if (argc <= ++opt->ind) return BADARG; + /* If so, the next available argument is * taken to be an option argument */ opt->arg = argv[opt->ind]; } @@ -162,12 +162,12 @@ xci_t xgetopt (int argc, xch_t* const* argv, xopt_t* opt) } /*if (*end == HIO_T('=')) *end = HIO_T('\0');*/ - opt->lngopt = opt->cur; + opt->lngopt = opt->cur; return BADCH; } if ((opt->opt = *opt->cur++) == ':' || - (oli = xfindcharincstr(opt->str, opt->opt)) == HIO_NULL) + (oli = xfindcharincstr(opt->str, opt->opt)) == HIO_NULL) { /* * if the user didn't specify '-' as an option, @@ -178,21 +178,21 @@ xci_t xgetopt (int argc, xch_t* const* argv, xopt_t* opt) return BADCH; } - if (*++oli != ':') + if (*++oli != ':') { /* don't need argument */ if (*opt->cur == '\0') opt->ind++; } - else + else { /* need an argument */ - if (*opt->cur != '\0') + if (*opt->cur != '\0') { /* no white space */ opt->arg = opt->cur; } - else if (argc <= ++opt->ind) + else if (argc <= ++opt->ind) { /* no arg */ opt->cur = XEMSG; diff --git a/lib/path.c b/lib/path.c index 8ddd7a9..2f4765b 100644 --- a/lib/path.c +++ b/lib/path.c @@ -25,8 +25,8 @@ #include #include "hio-prv.h" -/* TODO: support the \\?\ prefix and the \\.\ prefix on windows - * support \\?\UNC\server\path which is equivalent to \\server\path. +/* TODO: support the \\?\ prefix and the \\.\ prefix on windows + * support \\?\UNC\server\path which is equivalent to \\server\path. * */ /* ------------------------------------------------------------------ */ /* UCH IMPLEMENTATION */ @@ -80,7 +80,7 @@ hio_oow_t hio_canon_ucstr_path (const hio_uch_t* path, hio_uch_t* canon, int fla #endif hio_oow_t canon_len; - if (path[0] == '\0') + if (path[0] == '\0') { /* if the source is empty, no translation is needed */ canon[0] = '\0'; @@ -98,20 +98,20 @@ hio_oow_t hio_canon_ucstr_path (const hio_uch_t* path, hio_uch_t* canon, int fla *dst++ = *ptr++; /* colon */ is_drive = 1; - if (HIO_IS_PATH_SEP(*ptr)) + if (HIO_IS_PATH_SEP(*ptr)) { *dst++ = *ptr++; /* root directory */ has_root = 1; } } - else if (HIO_IS_PATH_SEP(*ptr)) + else if (HIO_IS_PATH_SEP(*ptr)) { *dst++ = *ptr++; /* root directory */ has_root = 1; #if defined(_WIN32) /* handle UNC path for Windows */ - if (HIO_IS_PATH_SEP(*ptr)) + if (HIO_IS_PATH_SEP(*ptr)) { *dst++ = *ptr++; @@ -131,14 +131,14 @@ hio_oow_t hio_canon_ucstr_path (const hio_uch_t* path, hio_uch_t* canon, int fla #endif } #else - if (HIO_IS_PATH_SEP(*ptr)) + if (HIO_IS_PATH_SEP(*ptr)) { *dst++ = *ptr++; /* root directory */ has_root = 1; } #endif - /* non_root_start points to the beginning of the canonicalized + /* non_root_start points to the beginning of the canonicalized * path excluding the root directory part. */ non_root_start = dst; @@ -170,7 +170,7 @@ hio_oow_t hio_canon_ucstr_path (const hio_uch_t* path, hio_uch_t* canon, int fla hio_uch_t* tmp; tmp = dst; - if (tmp > non_root_start) + if (tmp > non_root_start) { /* there is a previous segment. */ @@ -180,10 +180,10 @@ hio_oow_t hio_canon_ucstr_path (const hio_uch_t* path, hio_uch_t* canon, int fla while (tmp > non_root_start) { tmp--; - if (HIO_IS_PATH_SEP(*tmp)) + if (HIO_IS_PATH_SEP(*tmp)) { tmp++; /* position it next to the separator */ - break; + break; } } } @@ -194,7 +194,7 @@ hio_oow_t hio_canon_ucstr_path (const hio_uch_t* path, hio_uch_t* canon, int fla * Eat up the previous segment if it exists. * * If it doesn't exist, tmp == dst so dst = tmp - * keeps dst unchanged. If it exists, + * keeps dst unchanged. If it exists, * tmp != dst. so dst = tmp changes dst. * * path /abc/def/.. @@ -202,7 +202,7 @@ hio_oow_t hio_canon_ucstr_path (const hio_uch_t* path, hio_uch_t* canon, int fla * seg ptr * * canon /abc/def/ - * ^ ^ + * ^ ^ * tmp dst */ dst = tmp; @@ -219,7 +219,7 @@ hio_oow_t hio_canon_ucstr_path (const hio_uch_t* path, hio_uch_t* canon, int fla goto normal; } - if (prevlen == 3 && tmp[0] == '.' && tmp[1] == '.') + if (prevlen == 3 && tmp[0] == '.' && tmp[1] == '.') { /* nothing to eat away because the previous segment is ../ * @@ -241,7 +241,7 @@ hio_oow_t hio_canon_ucstr_path (const hio_uch_t* path, hio_uch_t* canon, int fla { normal: while (seg < ptr) *dst++ = *seg++; - if (HIO_IS_PATH_SEP(*ptr)) + if (HIO_IS_PATH_SEP(*ptr)) { /* this segment ended with a separator */ *dst++ = *seg++; /* copy the separator */ @@ -251,16 +251,16 @@ hio_oow_t hio_canon_ucstr_path (const hio_uch_t* path, hio_uch_t* canon, int fla } while (1); - if (dst > non_root_start && HIO_IS_PATH_SEP(dst[-1]) && - ((flags & HIO_CANON_BCSTR_PATH_DROP_TRAILING_SEP) || !HIO_IS_PATH_SEP(ptr[-1]))) + if (dst > non_root_start && HIO_IS_PATH_SEP(dst[-1]) && + ((flags & HIO_CANON_BCSTR_PATH_DROP_TRAILING_SEP) || !HIO_IS_PATH_SEP(ptr[-1]))) { /* if the canoncal path composed so far ends with a separator * and the original path didn't end with the separator, delete - * the ending separator. + * the ending separator. * also delete it if HIO_CANON_BCSTR_PATH_DROP_TRAILING_SEP is set. * * dst > non_root_start: - * there is at least 1 character after the root directory + * there is at least 1 character after the root directory * part. * HIO_IS_PATH_SEP(dst[-1]): * the canonical path ends with a separator. @@ -270,14 +270,14 @@ hio_oow_t hio_canon_ucstr_path (const hio_uch_t* path, hio_uch_t* canon, int fla dst[-1] = '\0'; canon_len = dst - canon - 1; } - else + else { /* just null-terminate the canonical path normally */ - dst[0] = '\0'; + dst[0] = '\0'; canon_len = dst - canon; } - if (canon_len <= 0) + if (canon_len <= 0) { if (!(flags & HIO_CANON_UCSTR_PATH_EMPTY_SINGLE_DOT)) { @@ -288,15 +288,15 @@ hio_oow_t hio_canon_ucstr_path (const hio_uch_t* path, hio_uch_t* canon, int fla canon_len = 1; } } - else + else { - /* drop a traling separator if the last segment is + /* drop a traling separator if the last segment is * double slashes */ int adj_base_len = 3; #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) - if (is_drive && !has_root) + if (is_drive && !has_root) { /* A path like A:..\\\ need some adjustment for * finalization below. */ @@ -307,7 +307,7 @@ hio_oow_t hio_canon_ucstr_path (const hio_uch_t* path, hio_uch_t* canon, int fla if (canon_len == adj_base_len) { /* i don't have to retain a trailing separator - * if the last segment is double slashes because + * if the last segment is double slashes because * the double slahses indicate a directory obviously */ if (canon[canon_len-3] == '.' && canon[canon_len-2] == '.' && @@ -385,7 +385,7 @@ hio_oow_t hio_canon_bcstr_path (const hio_bch_t* path, hio_bch_t* canon, int fla #endif hio_oow_t canon_len; - if (path[0] == '\0') + if (path[0] == '\0') { /* if the source is empty, no translation is needed */ canon[0] = '\0'; @@ -403,20 +403,20 @@ hio_oow_t hio_canon_bcstr_path (const hio_bch_t* path, hio_bch_t* canon, int fla *dst++ = *ptr++; /* colon */ is_drive = 1; - if (HIO_IS_PATH_SEP(*ptr)) + if (HIO_IS_PATH_SEP(*ptr)) { *dst++ = *ptr++; /* root directory */ has_root = 1; } } - else if (HIO_IS_PATH_SEP(*ptr)) + else if (HIO_IS_PATH_SEP(*ptr)) { *dst++ = *ptr++; /* root directory */ has_root = 1; #if defined(_WIN32) /* handle UNC path for Windows */ - if (HIO_IS_PATH_SEP(*ptr)) + if (HIO_IS_PATH_SEP(*ptr)) { *dst++ = *ptr++; @@ -436,14 +436,14 @@ hio_oow_t hio_canon_bcstr_path (const hio_bch_t* path, hio_bch_t* canon, int fla #endif } #else - if (HIO_IS_PATH_SEP(*ptr)) + if (HIO_IS_PATH_SEP(*ptr)) { *dst++ = *ptr++; /* root directory */ has_root = 1; } #endif - /* non_root_start points to the beginning of the canonicalized + /* non_root_start points to the beginning of the canonicalized * path excluding the root directory part. */ non_root_start = dst; @@ -475,7 +475,7 @@ hio_oow_t hio_canon_bcstr_path (const hio_bch_t* path, hio_bch_t* canon, int fla hio_bch_t* tmp; tmp = dst; - if (tmp > non_root_start) + if (tmp > non_root_start) { /* there is a previous segment. */ @@ -485,10 +485,10 @@ hio_oow_t hio_canon_bcstr_path (const hio_bch_t* path, hio_bch_t* canon, int fla while (tmp > non_root_start) { tmp--; - if (HIO_IS_PATH_SEP(*tmp)) + if (HIO_IS_PATH_SEP(*tmp)) { tmp++; /* position it next to the separator */ - break; + break; } } } @@ -499,7 +499,7 @@ hio_oow_t hio_canon_bcstr_path (const hio_bch_t* path, hio_bch_t* canon, int fla * Eat up the previous segment if it exists. * * If it doesn't exist, tmp == dst so dst = tmp - * keeps dst unchanged. If it exists, + * keeps dst unchanged. If it exists, * tmp != dst. so dst = tmp changes dst. * * path /abc/def/.. @@ -507,7 +507,7 @@ hio_oow_t hio_canon_bcstr_path (const hio_bch_t* path, hio_bch_t* canon, int fla * seg ptr * * canon /abc/def/ - * ^ ^ + * ^ ^ * tmp dst */ dst = tmp; @@ -524,7 +524,7 @@ hio_oow_t hio_canon_bcstr_path (const hio_bch_t* path, hio_bch_t* canon, int fla goto normal; } - if (prevlen == 3 && tmp[0] == '.' && tmp[1] == '.') + if (prevlen == 3 && tmp[0] == '.' && tmp[1] == '.') { /* nothing to eat away because the previous segment is ../ * @@ -546,7 +546,7 @@ hio_oow_t hio_canon_bcstr_path (const hio_bch_t* path, hio_bch_t* canon, int fla { normal: while (seg < ptr) *dst++ = *seg++; - if (HIO_IS_PATH_SEP(*ptr)) + if (HIO_IS_PATH_SEP(*ptr)) { /* this segment ended with a separator */ *dst++ = *seg++; /* copy the separator */ @@ -556,16 +556,16 @@ hio_oow_t hio_canon_bcstr_path (const hio_bch_t* path, hio_bch_t* canon, int fla } while (1); - if (dst > non_root_start && HIO_IS_PATH_SEP(dst[-1]) && - ((flags & HIO_CANON_BCSTR_PATH_DROP_TRAILING_SEP) || !HIO_IS_PATH_SEP(ptr[-1]))) + if (dst > non_root_start && HIO_IS_PATH_SEP(dst[-1]) && + ((flags & HIO_CANON_BCSTR_PATH_DROP_TRAILING_SEP) || !HIO_IS_PATH_SEP(ptr[-1]))) { /* if the canoncal path composed so far ends with a separator * and the original path didn't end with the separator, delete - * the ending separator. + * the ending separator. * also delete it if HIO_CANON_BCSTR_PATH_DROP_TRAILING_SEP is set. * * dst > non_root_start: - * there is at least 1 character after the root directory + * there is at least 1 character after the root directory * part. * HIO_IS_PATH_SEP(dst[-1]): * the canonical path ends with a separator. @@ -575,14 +575,14 @@ hio_oow_t hio_canon_bcstr_path (const hio_bch_t* path, hio_bch_t* canon, int fla dst[-1] = '\0'; canon_len = dst - canon - 1; } - else + else { /* just null-terminate the canonical path normally */ dst[0] = '\0'; canon_len = dst - canon; } - if (canon_len <= 0) + if (canon_len <= 0) { if (!(flags & HIO_CANON_BCSTR_PATH_EMPTY_SINGLE_DOT)) { @@ -593,15 +593,15 @@ hio_oow_t hio_canon_bcstr_path (const hio_bch_t* path, hio_bch_t* canon, int fla canon_len = 1; } } - else + else { - /* drop a traling separator if the last segment is + /* drop a traling separator if the last segment is * double slashes */ int adj_base_len = 3; #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) - if (is_drive && !has_root) + if (is_drive && !has_root) { /* A path like A:..\\\ need some adjustment for * finalization below. */ @@ -612,7 +612,7 @@ hio_oow_t hio_canon_bcstr_path (const hio_bch_t* path, hio_bch_t* canon, int fla if (canon_len == adj_base_len) { /* i don't have to retain a trailing separator - * if the last segment is double slashes because + * if the last segment is double slashes because * the double slahses indicate a directory obviously */ if (canon[canon_len-3] == '.' && canon[canon_len-2] == '.' && diff --git a/lib/pipe.c b/lib/pipe.c index add4cb9..2321952 100644 --- a/lib/pipe.c +++ b/lib/pipe.c @@ -100,7 +100,7 @@ pipe_done: if (!rdev->slave[HIO_DEV_PIPE_OUT]) goto oops; rdev->slave_count++; - for (i = 0; i < HIO_COUNTOF(rdev->slave); i++) + for (i = 0; i < HIO_COUNTOF(rdev->slave); i++) { if (rdev->slave[i]) rdev->slave[i]->master = rdev; } @@ -195,7 +195,7 @@ static int dev_pipe_kill_slave (hio_dev_t* dev, int force) { /* this call is started by the slave device itself. * if this is the last slave, kill the master also */ - if (master->slave_count <= 0) + if (master->slave_count <= 0) { hio_dev_kill ((hio_dev_t*)master); /* the master pointer is not valid from this point onwards @@ -355,7 +355,7 @@ static int dev_pipe_ioctl (hio_dev_t* dev, int cmd, void* arg) if (rdev->slave[sid]) { /* unlike dev_pipe_kill_master(), i don't nullify rdev->slave[sid]. - * so i treat the closing ioctl as if it's a kill request + * so i treat the closing ioctl as if it's a kill request * initiated by the slave device itself. */ hio_dev_kill ((hio_dev_t*)rdev->slave[sid]); } @@ -368,7 +368,7 @@ static int dev_pipe_ioctl (hio_dev_t* dev, int cmd, void* arg) } } -static hio_dev_mth_t dev_pipe_methods = +static hio_dev_mth_t dev_pipe_methods = { dev_pipe_make_master, dev_pipe_kill_master, @@ -443,7 +443,7 @@ static int pipe_ready_slave (hio_dev_t* dev, int events) if (events & HIO_DEV_EVENT_HUP) { - if (events & (HIO_DEV_EVENT_PRI | HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT)) + if (events & (HIO_DEV_EVENT_PRI | HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT)) { /* pipebably half-open? */ return 1; @@ -490,12 +490,12 @@ static hio_dev_pipe_slave_t* make_slave (hio_t* hio, slave_info_t* si) { case HIO_DEV_PIPE_IN: return (hio_dev_pipe_slave_t*)hio_dev_make( - hio, HIO_SIZEOF(hio_dev_pipe_t), + hio, HIO_SIZEOF(hio_dev_pipe_t), &dev_pipe_methods_slave, &dev_pipe_event_callbacks_slave_in, si); case HIO_DEV_PIPE_OUT: return (hio_dev_pipe_slave_t*)hio_dev_make( - hio, HIO_SIZEOF(hio_dev_pipe_t), + hio, HIO_SIZEOF(hio_dev_pipe_t), &dev_pipe_methods_slave, &dev_pipe_event_callbacks_slave_out, si); default: @@ -507,7 +507,7 @@ static hio_dev_pipe_slave_t* make_slave (hio_t* hio, slave_info_t* si) hio_dev_pipe_t* hio_dev_pipe_make (hio_t* hio, hio_oow_t xtnsize, const hio_dev_pipe_make_t* info) { return (hio_dev_pipe_t*)hio_dev_make( - hio, HIO_SIZEOF(hio_dev_pipe_t) + xtnsize, + hio, HIO_SIZEOF(hio_dev_pipe_t) + xtnsize, &dev_pipe_methods, &dev_pipe_event_callbacks, (void*)info); } diff --git a/lib/pro.c b/lib/pro.c index a7e6df1..6864f8a 100644 --- a/lib/pro.c +++ b/lib/pro.c @@ -58,7 +58,7 @@ typedef struct param_t param_t; static void free_param (hio_t* hio, param_t* param) { - if (param->argv && param->argv != param->fixed_argv) + if (param->argv && param->argv != param->fixed_argv) hio_freemem (hio, param->argv); if (param->mcmd) hio_freemem (hio, param->mcmd); HIO_MEMSET (param, 0, HIO_SIZEOF(*param)); @@ -100,8 +100,8 @@ static int make_param (hio_t* hio, const void* cmd, int flags, param_t* param) hio_dupbcstr(hio, cmd, HIO_NULL); if (HIO_UNLIKELY(!mcmd)) goto oops; - fcnt = hio_split_bcstr(mcmd, "", '\"', '\"', '\\'); - if (fcnt <= 0) + fcnt = hio_split_bcstr(mcmd, "", '\"', '\"', '\\'); + if (fcnt <= 0) { /* no field or an error */ hio_seterrnum (hio, HIO_EINVAL); @@ -142,7 +142,7 @@ static pid_t standard_fork_and_exec (hio_dev_pro_t* dev, int pfds[], hio_dev_pro pid_t pid; pid = fork(); - if (pid == -1) + if (pid == -1) { hio_seterrwithsyserr (hio, 0, errno); return -1; @@ -218,7 +218,7 @@ static pid_t standard_fork_and_exec (hio_dev_pro_t* dev, int pfds[], hio_dev_pro if ((mi->flags & HIO_DEV_PRO_OUTTONUL) && dup2(devnull, 1) == -1) goto slave_oops; if ((mi->flags & HIO_DEV_PRO_ERRTONUL) && dup2(devnull, 2) == -1) goto slave_oops; - close (devnull); + close (devnull); devnull = HIO_SYSHND_INVALID; } @@ -229,7 +229,7 @@ static pid_t standard_fork_and_exec (hio_dev_pro_t* dev, int pfds[], hio_dev_pro execv (param->argv[0], param->argv); /* if exec fails, free 'param' parameter which is an inherited pointer */ - free_param (hio, param); + free_param (hio, param); slave_oops: if (devnull != HIO_SYSHND_INVALID) close(devnull); @@ -390,7 +390,7 @@ static int dev_pro_make_master (hio_dev_t* dev, void* ctx) rdev->slave_count++; } - for (i = 0; i < HIO_COUNTOF(rdev->slave); i++) + for (i = 0; i < HIO_COUNTOF(rdev->slave); i++) { if (rdev->slave[i]) rdev->slave[i]->master = rdev; } @@ -401,8 +401,8 @@ static int dev_pro_make_master (hio_dev_t* dev, void* ctx) rdev->on_write = info->on_write; rdev->on_close = info->on_close; - HIO_DEBUG7 (hio, "PRO(%p) - slave[%d] %p slave[%d] %p slave[%d] %p\n", dev, - HIO_DEV_PRO_IN, rdev->slave[HIO_DEV_PRO_IN], + HIO_DEBUG7 (hio, "PRO(%p) - slave[%d] %p slave[%d] %p slave[%d] %p\n", dev, + HIO_DEV_PRO_IN, rdev->slave[HIO_DEV_PRO_IN], HIO_DEV_PRO_OUT, rdev->slave[HIO_DEV_PRO_OUT], HIO_DEV_PRO_ERR, rdev->slave[HIO_DEV_PRO_ERR]); return 0; @@ -413,7 +413,7 @@ oops: if (pfds[i] != HIO_SYSHND_INVALID) close (pfds[i]); } - if (rdev->mcmd) + if (rdev->mcmd) { hio_freemem (hio, rdev->mcmd); free_param (hio, ¶m); @@ -538,7 +538,7 @@ static int dev_pro_kill_slave (hio_dev_t* dev, int force) if (master->slave[rdev->id]) { /* this call is started by the slave device itself. */ - if (master->slave_count <= 0) + if (master->slave_count <= 0) { /* if this is the last slave, kill the master also */ hio_dev_kill ((hio_dev_t*)master); @@ -576,7 +576,7 @@ static int dev_pro_read_slave (hio_dev_t* dev, void* buf, hio_iolen_t* len, hio_ ssize_t x; /* the read and write operation happens on different slave devices. - * the write EOF indication doesn't affect this device + * the write EOF indication doesn't affect this device if (HIO_UNLIKELY(pro->pfd == HIO_SYSHND_INVALID)) { hio_seterrnum (pro->hio, HIO_EBADHND); @@ -604,7 +604,7 @@ static int dev_pro_write_slave (hio_dev_t* dev, const void* data, hio_iolen_t* l /* this check is not needed because HIO_DEV_CAP_OUT_CLOSED is set on the device by the core * when EOF indication is successful(return value 1 and *iovcnt 0). - * If HIO_DEV_CAP_OUT_CLOSED, the core doesn't invoke the write method + * If HIO_DEV_CAP_OUT_CLOSED, the core doesn't invoke the write method if (HIO_UNLIKELY(pro->pfd == HIO_SYSHND_INVALID)) { hio_seterrnum (pro->hio, HIO_EBADHND); @@ -646,7 +646,7 @@ static int dev_pro_writev_slave (hio_dev_t* dev, const hio_iovec_t* iov, hio_iol /* this check is not needed because HIO_DEV_CAP_OUT_CLOSED is set on the device by the core * when EOF indication is successful(return value 1 and *iovcnt 0). - * If HIO_DEV_CAP_OUT_CLOSED, the core doesn't invoke the write method + * If HIO_DEV_CAP_OUT_CLOSED, the core doesn't invoke the write method if (HIO_UNLIKELY(pro->pfd == HIO_SYSHND_INVALID)) { hio_seterrnum (pro->hio, HIO_EBADHND); @@ -712,11 +712,11 @@ static int dev_pro_ioctl (hio_dev_t* dev, int cmd, void* arg) if (rdev->slave[sid]) { /* unlike dev_pro_kill_master(), i don't nullify rdev->slave[sid]. - * so i treat the closing ioctl as if it's a kill request + * so i treat the closing ioctl as if it's a kill request * initiated by the slave device itself. */ hio_dev_kill ((hio_dev_t*)rdev->slave[sid]); - /* if this is the last slave, the master is destroyed as well. + /* if this is the last slave, the master is destroyed as well. * therefore, using rdev is unsafe in the assertion below is unsafe. *HIO_ASSERT (hio, rdev->slave[sid] == HIO_NULL); */ } @@ -742,7 +742,7 @@ static int dev_pro_ioctl (hio_dev_t* dev, int cmd, void* arg) } } -static hio_dev_mth_t dev_pro_methods = +static hio_dev_mth_t dev_pro_methods = { dev_pro_make_master, dev_pro_kill_master, @@ -817,7 +817,7 @@ static int pro_ready_slave (hio_dev_t* dev, int events) if (events & HIO_DEV_EVENT_HUP) { - if (events & (HIO_DEV_EVENT_PRI | HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT)) + if (events & (HIO_DEV_EVENT_PRI | HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT)) { /* probably half-open? */ return 1; @@ -878,17 +878,17 @@ static hio_dev_pro_slave_t* make_slave (hio_t* hio, slave_info_t* si) { case HIO_DEV_PRO_IN: return (hio_dev_pro_slave_t*)hio_dev_make( - hio, HIO_SIZEOF(hio_dev_pro_t), + hio, HIO_SIZEOF(hio_dev_pro_t), &dev_pro_methods_slave, &dev_pro_event_callbacks_slave_in, si); case HIO_DEV_PRO_OUT: return (hio_dev_pro_slave_t*)hio_dev_make( - hio, HIO_SIZEOF(hio_dev_pro_t), + hio, HIO_SIZEOF(hio_dev_pro_t), &dev_pro_methods_slave, &dev_pro_event_callbacks_slave_out, si); case HIO_DEV_PRO_ERR: return (hio_dev_pro_slave_t*)hio_dev_make( - hio, HIO_SIZEOF(hio_dev_pro_t), + hio, HIO_SIZEOF(hio_dev_pro_t), &dev_pro_methods_slave, &dev_pro_event_callbacks_slave_err, si); default: @@ -900,7 +900,7 @@ static hio_dev_pro_slave_t* make_slave (hio_t* hio, slave_info_t* si) hio_dev_pro_t* hio_dev_pro_make (hio_t* hio, hio_oow_t xtnsize, const hio_dev_pro_make_t* info) { return (hio_dev_pro_t*)hio_dev_make( - hio, HIO_SIZEOF(hio_dev_pro_t) + xtnsize, + hio, HIO_SIZEOF(hio_dev_pro_t) + xtnsize, &dev_pro_methods, &dev_pro_event_callbacks, (void*)info); } diff --git a/lib/pty.c b/lib/pty.c index c0022fd..2d52dff 100644 --- a/lib/pty.c +++ b/lib/pty.c @@ -60,7 +60,7 @@ typedef struct param_t param_t; static void free_param (hio_t* hio, param_t* param) { - if (param->argv && param->argv != param->fixed_argv) + if (param->argv && param->argv != param->fixed_argv) hio_freemem (hio, param->argv); if (param->mcmd) hio_freemem (hio, param->mcmd); HIO_MEMSET (param, 0, HIO_SIZEOF(*param)); @@ -106,8 +106,8 @@ static int make_param (hio_t* hio, const void* cmd, int flags, param_t* param) hio_dupbcstr(hio, cmd, HIO_NULL); if (HIO_UNLIKELY(!mcmd)) goto oops; - fcnt = hio_split_bcstr(mcmd, "", '\"', '\"', '\\'); - if (fcnt <= 0) + fcnt = hio_split_bcstr(mcmd, "", '\"', '\"', '\\'); + if (fcnt <= 0) { /* no field or an error */ hio_seterrnum (hio, HIO_EINVAL); @@ -148,7 +148,7 @@ static pid_t standard_fork_and_exec (hio_dev_pty_t* dev, int pfds[], hio_dev_pty pid_t pid; pid = fork(); - if (pid == -1) + if (pid == -1) { hio_seterrwithsyserr (hio, 0, errno); return -1; @@ -176,7 +176,7 @@ static pid_t standard_fork_and_exec (hio_dev_pty_t* dev, int pfds[], hio_dev_pty execve (param->argv[0], param->argv, param->fixed_env); /* if exec fails, free 'param' parameter which is an inherited pointer */ - free_param (hio, param); + free_param (hio, param); slave_oops: if (pfds[1] != HIO_SYSHND_INVALID) close(pfds[1]); @@ -228,7 +228,7 @@ static int dev_pty_make (hio_dev_t* dev, void* ctx) if (ttyname_r(pfds[0], ptr, capa) != 0) #endif { - if (errno == ERANGE) + if (errno == ERANGE) { char* tmp; tmp = hio_reallocmem(hio, (ptr == pts_name_buf? HIO_NULL: ptr), capa + 128); @@ -486,7 +486,7 @@ static int dev_pty_ioctl (hio_dev_t* dev, int cmd, void* arg) } } -static hio_dev_mth_t dev_pty_methods = +static hio_dev_mth_t dev_pty_methods = { dev_pty_make, dev_pty_kill, @@ -516,7 +516,7 @@ static int pty_ready (hio_dev_t* dev, int events) if (events & HIO_DEV_EVENT_HUP) { - if (events & (HIO_DEV_EVENT_PRI | HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT)) + if (events & (HIO_DEV_EVENT_PRI | HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT)) { /* ptybably half-open? */ return 1; @@ -554,7 +554,7 @@ static hio_dev_evcb_t dev_pty_event_callbacks = hio_dev_pty_t* hio_dev_pty_make (hio_t* hio, hio_oow_t xtnsize, const hio_dev_pty_make_t* info) { return (hio_dev_pty_t*)hio_dev_make( - hio, HIO_SIZEOF(hio_dev_pty_t) + xtnsize, + hio, HIO_SIZEOF(hio_dev_pty_t) + xtnsize, &dev_pty_methods, &dev_pty_event_callbacks, (void*)info); } diff --git a/lib/rad-msg.c b/lib/rad-msg.c index 73c5947..3488238 100644 --- a/lib/rad-msg.c +++ b/lib/rad-msg.c @@ -33,8 +33,8 @@ /* TODO: tlv and various value encoding. radius dictionary support - change hio_rad_walk_attributes() to hio_rad_walk_attrs() with enhancement - finish long attribute insertion in hio_rad_insert_attr... break long data to multiple attrs + change hio_rad_walk_attributes() to hio_rad_walk_attrs() with enhancement + finish long attribute insertion in hio_rad_insert_attr... break long data to multiple attrs */ void hio_rad_initialize (hio_rad_hdr_t* hdr, hio_rad_code_t code, hio_uint8_t id) @@ -60,9 +60,9 @@ static void fill_authenticator_randomly (void* authenticator, int length) int fd; fd = open("/dev/urandom", O_RDONLY, 0); - if (fd >= 0) + if (fd >= 0) { - while (total < length) + while (total < length) { int bytes = read(fd, &v[total], length - total); if (bytes <= 0) break; @@ -71,7 +71,7 @@ static void fill_authenticator_randomly (void* authenticator, int length) close (fd); } - if (total < length) + if (total < length) { struct timeval now; unsigned int seed; @@ -80,7 +80,7 @@ static void fill_authenticator_randomly (void* authenticator, int length) seed = getpid() + now.tv_sec + now.tv_usec; srandom (seed); - while (total < length) + while (total < length) { seed = random(); v[total] = seed % HIO_TYPE_MAX(hio_uint8_t); @@ -103,7 +103,7 @@ int hio_rad_walk_attributes (const hio_rad_hdr_t* hdr, hio_rad_attr_walker_t wal { /* sanity checks */ if (rem < attr->length) return -1; - if (attr->length < HIO_SIZEOF(*attr)) + if (attr->length < HIO_SIZEOF(*attr)) { /* attribute length cannot be less than the header size. * the packet could be corrupted... */ @@ -125,7 +125,7 @@ int hio_rad_walk_attributes (const hio_rad_hdr_t* hdr, hio_rad_attr_walker_t wal /* if this vendor happens to be 0, walker can't tell * if it is vendor specific or not because 0 is passed in - * for non-VSAs. but i don't care. in reality, + * for non-VSAs. but i don't care. in reality, * 0 is reserved in IANA enterpirse number assignments. * (http://www.iana.org/assignments/enterprise-numbers) */ if (walker(hdr, hio_ntoh32(vsattr->vendor), &vsattr->vs, ctx) <= -1) return -1; @@ -151,15 +151,15 @@ static hio_rad_attr_hdr_t* find_attribute (hio_rad_attr_hdr_t* attr, int* len, h { /* sanity checks */ if (rem < attr->length) return HIO_NULL; - if (attr->length < HIO_SIZEOF(*attr)) + if (attr->length < HIO_SIZEOF(*attr)) { /* attribute length cannot be less than the header size. * the packet could be corrupted... */ - return HIO_NULL; + return HIO_NULL; } rem -= attr->length; - if (attr->type == attrtype) + if (attr->type == attrtype) { *len = rem; /* remaining length */ return attr; @@ -194,7 +194,7 @@ static hio_rad_attr_hdr_t* find_extended_attribute (hio_rad_attr_hdr_t* attr, in if (attr->length < HIO_SIZEOF(*attr)) goto oops; rem -= attr->length; - if (attr->type == xtype) + if (attr->type == xtype) { hio_uint8_t xattrtype; @@ -277,25 +277,25 @@ hio_rad_vsattr_hdr_t* hio_rad_find_vendor_specific_attribute (hio_rad_hdr_t* hdr while (attr) { hio_rad_vsattr_hdr_t* vsattr; - + if (attr->length >= HIO_SIZEOF(*vsattr)) /* sanity check */ { vsattr = (hio_rad_vsattr_hdr_t*)attr; - + if (hio_ntoh32(vsattr->vendor) == vendor && vsattr->vs.type == attrtype) { int val_len; - + val_len = (int)vsattr->length - HIO_SIZEOF(*vsattr); - - if ((int)vsattr->vs.length == val_len + HIO_SIZEOF(vsattr->vs)) + + if ((int)vsattr->vs.length == val_len + HIO_SIZEOF(vsattr->vs)) { if (index <= 0) return vsattr; index--; } } } - + attr = find_attribute((hio_rad_attr_hdr_t*)((hio_uint8_t*)attr + attr->length), &len, HIO_RAD_ATTR_VENDOR_SPECIFIC); } } @@ -321,16 +321,16 @@ hio_rad_xvsattr_hdr_t* hio_rad_find_extended_vendor_specific_attribute (hio_rad_ if (attr->length >= HIO_SIZEOF(*lxvsattr)) /* sanity check */ { lxvsattr = (hio_rad_lxvsattr_hdr_t*)attr; - + if (hio_ntoh32(lxvsattr->vendor) == vendor && lxvsattr->lxvs.type == attrtype) { int val_len; - + val_len = (int)lxvsattr->length - HIO_SIZEOF(*lxvsattr); - - if ((int)lxvsattr->lxvs.length == val_len + HIO_SIZEOF(lxvsattr->lxvs)) + + if ((int)lxvsattr->lxvs.length == val_len + HIO_SIZEOF(lxvsattr->lxvs)) { - /* the caller must check if the extended type is long. + /* the caller must check if the extended type is long. * if long, it must cast back to hio_rad_lxvsattr_hdr_t* */ if (index <= 0) return (hio_rad_xvsattr_hdr_t*)lxvsattr; index--; @@ -339,19 +339,19 @@ hio_rad_xvsattr_hdr_t* hio_rad_find_extended_vendor_specific_attribute (hio_rad_ } } else - { + { hio_rad_xvsattr_hdr_t* xvsattr; if (attr->length >= HIO_SIZEOF(*xvsattr)) /* sanity check */ { xvsattr = (hio_rad_xvsattr_hdr_t*)attr; - + if (hio_ntoh32(xvsattr->vendor) == vendor && xvsattr->xvs.type == attrtype) { int val_len; - + val_len = (int)xvsattr->length - HIO_SIZEOF(*xvsattr); - - if ((int)xvsattr->xvs.length == val_len + HIO_SIZEOF(xvsattr->xvs)) + + if ((int)xvsattr->xvs.length == val_len + HIO_SIZEOF(xvsattr->xvs)) { if (index <= 0) return xvsattr; index--; @@ -359,7 +359,7 @@ hio_rad_xvsattr_hdr_t* hio_rad_find_extended_vendor_specific_attribute (hio_rad_ } } } - + attr = find_extended_attribute((hio_rad_attr_hdr_t*)((hio_uint8_t*)attr + attr->length), &len, xtype, HIO_RAD_ATTR_VENDOR_SPECIFIC); } } @@ -458,12 +458,12 @@ hio_rad_xattr_hdr_t* hio_rad_insert_extended_attribute ( int auth_len = hio_ntoh16(auth->length); int new_auth_len, maxvallen, hdrlen; - if (HIO_RAD_ATTR_IS_SHORT_EXTENDED(xtype)) + if (HIO_RAD_ATTR_IS_SHORT_EXTENDED(xtype)) { maxvallen = HIO_RAD_MAX_XATTR_VALUE_LEN; hdrlen = HIO_SIZEOF(hio_rad_xattr_hdr_t); } - else if (HIO_RAD_ATTR_IS_LONG_EXTENDED(xtype)) + else if (HIO_RAD_ATTR_IS_LONG_EXTENDED(xtype)) { maxvallen = HIO_RAD_MAX_LXATTR_VALUE_LEN; hdrlen = HIO_SIZEOF(hio_rad_lxattr_hdr_t); @@ -479,7 +479,7 @@ hio_rad_xattr_hdr_t* hio_rad_insert_extended_attribute ( xattr = (hio_rad_xattr_hdr_t*)((hio_uint8_t*)auth + auth_len); xattr->type = xtype; xattr->length = new_auth_len - auth_len; - if (HIO_RAD_ATTR_IS_LONG_EXTENDED(xtype)) + if (HIO_RAD_ATTR_IS_LONG_EXTENDED(xtype)) { hio_rad_lxattr_hdr_t* lxattr; lxattr = (hio_rad_lxattr_hdr_t*)xattr; @@ -533,12 +533,12 @@ hio_rad_xvsattr_hdr_t* hio_rad_insert_extended_vendor_specific_attribute ( int auth_len = hio_ntoh16(auth->length); int new_auth_len, maxvallen, hdrlen; - if (HIO_RAD_ATTR_IS_SHORT_EXTENDED(xtype)) + if (HIO_RAD_ATTR_IS_SHORT_EXTENDED(xtype)) { maxvallen = HIO_RAD_MAX_XVSATTR_VALUE_LEN; hdrlen = HIO_SIZEOF(hio_rad_xvsattr_hdr_t); } - else if (HIO_RAD_ATTR_IS_LONG_EXTENDED(xtype)) + else if (HIO_RAD_ATTR_IS_LONG_EXTENDED(xtype)) { maxvallen = HIO_RAD_MAX_LXVSATTR_VALUE_LEN; hdrlen = HIO_SIZEOF(hio_rad_lxvsattr_hdr_t); @@ -557,7 +557,7 @@ hio_rad_xvsattr_hdr_t* hio_rad_insert_extended_vendor_specific_attribute ( xvsattr->xtype = HIO_RAD_ATTR_VENDOR_SPECIFIC; xvsattr->vendor = hio_hton32(vendor); - if (HIO_RAD_ATTR_IS_LONG_EXTENDED(xtype)) + if (HIO_RAD_ATTR_IS_LONG_EXTENDED(xtype)) { /* this function is still low-level. it doesn't handle continuation of big data */ hio_rad_lxvsattr_hdr_t* lxvsattr; @@ -572,7 +572,7 @@ hio_rad_xvsattr_hdr_t* hio_rad_insert_extended_vendor_specific_attribute ( xvsattr->xvs.type = attrtype; xvsattr->xvs.length = len + HIO_SIZEOF(xvsattr->xvs); HIO_MEMCPY (xvsattr + 1, ptr, len); - } + } auth->length = hio_hton16(new_auth_len); return xvsattr; @@ -581,7 +581,7 @@ hio_rad_xvsattr_hdr_t* hio_rad_insert_extended_vendor_specific_attribute ( /* ---------------------------------------------------------------- */ hio_rad_attr_hdr_t* hio_rad_insert_attribute_with_bcstr ( - hio_rad_hdr_t* auth, int max, hio_uint32_t vendor, + hio_rad_hdr_t* auth, int max, hio_uint32_t vendor, hio_uint8_t id, const hio_bch_t* value) { return (vendor == 0)? @@ -590,7 +590,7 @@ hio_rad_attr_hdr_t* hio_rad_insert_attribute_with_bcstr ( } hio_rad_attr_hdr_t* hio_rad_insert_attribute_with_ucstr ( - hio_rad_hdr_t* auth, int max, hio_uint32_t vendor, + hio_rad_hdr_t* auth, int max, hio_uint32_t vendor, hio_uint8_t id, const hio_uch_t* value) { hio_oow_t bcslen, ucslen; @@ -604,7 +604,7 @@ hio_rad_attr_hdr_t* hio_rad_insert_attribute_with_ucstr ( } hio_rad_attr_hdr_t* hio_rad_insert_attribute_with_bchars ( - hio_rad_hdr_t* auth, int max, hio_uint32_t vendor, + hio_rad_hdr_t* auth, int max, hio_uint32_t vendor, hio_uint8_t id, const hio_bch_t* value, hio_uint8_t length) { return (vendor == 0)? @@ -613,7 +613,7 @@ hio_rad_attr_hdr_t* hio_rad_insert_attribute_with_bchars ( } hio_rad_attr_hdr_t* hio_rad_insert_attribute_with_uchars ( - hio_rad_hdr_t* auth, int max, hio_uint32_t vendor, + hio_rad_hdr_t* auth, int max, hio_uint32_t vendor, hio_uint8_t id, const hio_uch_t* value, hio_uint8_t length) { hio_oow_t bcslen, ucslen; @@ -621,7 +621,7 @@ hio_rad_attr_hdr_t* hio_rad_insert_attribute_with_uchars ( ucslen = length; bcslen = HIO_COUNTOF(bcsval); - if (hio_conv_uchars_to_utf8(value, &ucslen, bcsval, &bcslen) <= -1) return HIO_NULL; + if (hio_conv_uchars_to_utf8(value, &ucslen, bcsval, &bcslen) <= -1) return HIO_NULL; return (vendor == 0)? hio_rad_insert_attribute(auth, max, id, bcsval, bcslen): @@ -648,7 +648,7 @@ hio_rad_attr_hdr_t* hio_rad_insert_ipv6prefix_attribute ( hio_ip6ad_t value; } __attribute__((__packed__)); - struct ipv6prefix_t ipv6prefix; + struct ipv6prefix_t ipv6prefix; hio_uint8_t i, j; if (prefix_bits > 128) prefix_bits = 128; @@ -677,7 +677,7 @@ hio_rad_attr_hdr_t* hio_rad_insert_ipv6prefix_attribute ( ipv6prefix.value.v[j] = value->v[j] & (0xFF << (8 - bits)); } } - + return (vendor == 0)? hio_rad_insert_attribute(auth, max, id, &ipv6prefix, j + 2): (hio_rad_attr_hdr_t*)hio_rad_insert_vendor_specific_attribute(auth, max, vendor, id, &ipv6prefix, j + 2); @@ -834,11 +834,11 @@ hio_rad_attr_hdr_t* hio_rad_insert_attr (hio_rad_hdr_t* auth, int max, hio_uint1 { hio_rad_xattr_hdr_t* tmp, * ret = HIO_NULL; hio_uint16_t orglen = auth->length; - + while (len > HIO_RAD_MAX_LXATTR_VALUE_LEN) { tmp = hio_rad_insert_extended_attribute(auth, max, hi, lo, ptr, HIO_RAD_MAX_LXATTR_VALUE_LEN, (1 << 7)); - if (!tmp) + if (!tmp) { auth->length = orglen; return HIO_NULL; @@ -848,7 +848,7 @@ hio_rad_attr_hdr_t* hio_rad_insert_attr (hio_rad_hdr_t* auth, int max, hio_uint1 ptr = (const hio_uint8_t*)ptr + HIO_RAD_MAX_LXATTR_VALUE_LEN; } tmp = hio_rad_insert_extended_attribute(auth, max, hi, lo, ptr, len, 0); - if (!tmp) + if (!tmp) { auth->length = orglen; return HIO_NULL; @@ -883,11 +883,11 @@ hio_rad_vsattr_hdr_t* hio_rad_insert_vsattr (hio_rad_hdr_t* auth, int max, hio_u { hio_rad_xvsattr_hdr_t* tmp, * ret = HIO_NULL; hio_uint16_t orglen = auth->length; - + while (len > HIO_RAD_MAX_LXVSATTR_VALUE_LEN) { tmp = hio_rad_insert_extended_vendor_specific_attribute(auth, max, vendor, hi, lo, ptr, HIO_RAD_MAX_LXVSATTR_VALUE_LEN, (1 << 7)); - if (!tmp) + if (!tmp) { auth->length = orglen; return HIO_NULL; @@ -897,7 +897,7 @@ hio_rad_vsattr_hdr_t* hio_rad_insert_vsattr (hio_rad_hdr_t* auth, int max, hio_u ptr = (const hio_uint8_t*)ptr + HIO_RAD_MAX_LXVSATTR_VALUE_LEN; } tmp = hio_rad_insert_extended_vendor_specific_attribute(auth, max, vendor, hi, lo, ptr, len, 0); - if (!tmp) + if (!tmp) { auth->length = orglen; return HIO_NULL; @@ -958,7 +958,7 @@ int hio_rad_set_user_password (hio_rad_hdr_t* auth, int max, const hio_bch_t* pa xor (&hashed[0], tmp, HIO_SIZEOF(tmp)); - for (i = 1; i < (padlen >> 4); i++) + for (i = 1; i < (padlen >> 4); i++) { hio_md5_initialize (&md5); hio_md5_update (&md5, secret, hio_count_bcstr(secret)); @@ -973,7 +973,7 @@ int hio_rad_set_user_password (hio_rad_hdr_t* auth, int max, const hio_bch_t* pa int n; n = hio_rad_delete_attribute(auth, HIO_RAD_ATTR_USER_PASSWORD, 0); if (n <= -1) goto oops; - if (n == 0) break; + if (n == 0) break; } if (!hio_rad_insert_attribute(auth, max, HIO_RAD_ATTR_USER_PASSWORD, hashed, padlen)) goto oops; @@ -1041,7 +1041,7 @@ int hio_rad_verify_response (hio_rad_hdr_t* res, const hio_rad_hdr_t* req, const /* * We could dispense with the HIO_MEMCPY, and do MD5's of the packet - * + authenticator piece by piece. This is easier understand, + * + authenticator piece by piece. This is easier understand, * and maybe faster. */ HIO_MEMCPY(reply, res->authenticator, HIO_SIZEOF(res->authenticator)); /* save the reply */ @@ -1051,9 +1051,9 @@ int hio_rad_verify_response (hio_rad_hdr_t* res, const hio_rad_hdr_t* req, const hio_md5_initialize (&md5); hio_md5_update (&md5, res, hio_ntoh16(res->length)); - /* + /* * This next bit is necessary because of a bug in the original Livingston - * RADIUS server. The authentication authenticator is *supposed* to be + * RADIUS server. The authentication authenticator is *supposed* to be * MD5'd with the old password (as the secret) for password changes. * However, the old password isn't used. The "authentication" authenticator * for the server reply packet is simply the MD5 of the reply packet. diff --git a/lib/sck.c b/lib/sck.c index 38c5e1f..07856ff 100644 --- a/lib/sck.c +++ b/lib/sck.c @@ -101,8 +101,8 @@ static hio_syshnd_t open_async_socket (hio_t* hio, int domain, int type, int pro type |= SOCK_NONBLOCK | SOCK_CLOEXEC; open_socket: #endif - sck = socket(domain, type, proto); - if (sck == HIO_SYSHND_INVALID) + sck = socket(domain, type, proto); + if (sck == HIO_SYSHND_INVALID) { #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC) if (errno == EINVAL && (type & (SOCK_NONBLOCK | SOCK_CLOEXEC))) @@ -163,7 +163,7 @@ open_socket: if (hio_makesyshndasync(hio, fd[0]) <= -1 || hio_makesyshndasync(hio, fd[1]) <= -1 || hio_makesyshndcloexec(hio, fd[0]) <= -1 || - hio_makesyshndcloexec(hio, fd[1]) <= -1) + hio_makesyshndcloexec(hio, fd[1]) <= -1) { hio_seterrwithsyserr (hio, 0, errno); close (fd[0]); @@ -253,7 +253,7 @@ static struct sck_type_map_t sck_type_map[] = /* HIO_DEV_SCK_TCP6 */ { AF_INET6, SOCK_STREAM, 0, 1, 1, HIO_DEV_CAP_STREAM }, - /* HIO_DEV_SCK_UPD4 */ /* TODO: the socket api allows connect() on UDP sockets. should i mark it connectable? */ + /* HIO_DEV_SCK_UPD4 */ /* TODO: the socket api allows connect() on UDP sockets. should i mark it connectable? */ { AF_INET, SOCK_DGRAM, 0, 0, 0, 0 }, /* HIO_DEV_SCK_UDP6 */ @@ -330,9 +330,9 @@ static void connect_timedout (hio_t* hio, const hio_ntime_t* now, hio_tmrjob_t* if (rdev->state & HIO_DEV_SCK_CONNECTING) { /* the state check for HIO_DEV_TCP_CONNECTING is actually redundant - * as it must not be fired after it gets connected. the timer job - * doesn't need to be deleted when it gets connected for this check - * here. this libarary, however, deletes the job when it gets + * as it must not be fired after it gets connected. the timer job + * doesn't need to be deleted when it gets connected for this check + * here. this libarary, however, deletes the job when it gets * connected. */ HIO_DEBUG1 (hio, "SCK(%p) - connect timed out. halting\n", rdev); hio_dev_sck_halt (rdev); @@ -417,7 +417,7 @@ static int dev_sck_make (hio_dev_t* dev, void* ctx) if (hnd == HIO_SYSHND_INVALID) goto oops; #if defined(ENABLE_SCTP) - if (sck_type_map[arg->type].type == SOCK_SEQPACKET && + if (sck_type_map[arg->type].type == SOCK_SEQPACKET && sck_type_map[arg->type].proto == IPPROTO_SCTP) { struct sctp_event_subscribe sctp_ev_s; @@ -489,15 +489,16 @@ static int dev_sck_kill (hio_dev_t* dev, int force) { hio_t* hio = dev->hio; hio_dev_sck_t* rdev = (hio_dev_sck_t*)dev; + int hnd = rdev->hnd; - HIO_DEBUG1 (hio, "SCK(%p) - being killed\n", rdev); + HIO_DEBUG2 (hio, "SCK(%p) - being killed [%d]\n", rdev, hnd); #if 0 if (IS_STREAM(rdev)) { /*if (HIO_DEV_SCK_GET_PROGRESS(rdev)) {*/ /* for HIO_DEV_SCK_CONNECTING, HIO_DEV_SCK_CONNECTING_SSL, and HIO_DEV_SCK_ACCEPTING_SSL - * on_disconnect() is called without corresponding on_connect(). + * on_disconnect() is called without corresponding on_connect(). * it is the same if connect or accept has not been called. */ if (rdev->on_disconnect) rdev->on_disconnect (rdev); /*}*/ @@ -532,7 +533,7 @@ static int dev_sck_kill (hio_dev_t* dev, int force) } #endif - if (rdev->hnd != HIO_SYSHND_INVALID) + if (rdev->hnd != HIO_SYSHND_INVALID) { close (rdev->hnd); rdev->hnd = HIO_SYSHND_INVALID; @@ -543,6 +544,8 @@ static int dev_sck_kill (hio_dev_t* dev, int force) close (rdev->side_chan); rdev->side_chan = HIO_SYSHND_INVALID; } + + HIO_DEBUG2 (hio, "SCK(%p) - killed [%d]\n", rdev, (int)hnd); return 0; } @@ -612,7 +615,7 @@ static int dev_sck_read_stateless (hio_dev_t* dev, void* buf, hio_iolen_t* len, hio_seterrwithsyserr (hio, 0, eno); - HIO_DEBUG2 (hio, "SCK(%p) - recvfrom failure - %hs", rdev, strerror(eno)); + HIO_DEBUG2 (hio, "SCK(%p) - recvfrom failure - %hs", rdev, strerror(eno)); return -1; } @@ -634,7 +637,7 @@ static int dev_sck_read_bpf (hio_dev_t* dev, void* buf, hio_iolen_t* len, hio_de #if defined(ENABLE_SCTP) static int recvmsg_sctp( - int s, void* ptr, hio_oow_t len, struct sockaddr* srcaddr, hio_scklen_t* srcaddrlen, + int s, void* ptr, hio_oow_t len, struct sockaddr* srcaddr, hio_scklen_t* srcaddrlen, struct sctp_sndrcvinfo* sinfo, int* msg_flags) { int n; @@ -662,7 +665,7 @@ static int recvmsg_sctp( for (cmsg = CMSG_FIRSTHDR(&msg); cmsg ; cmsg = CMSG_NXTHDR(&msg, cmsg)) { - if (cmsg->cmsg_level == IPPROTO_SCTP && cmsg->cmsg_type == SCTP_SNDRCV) + if (cmsg->cmsg_level == IPPROTO_SCTP && cmsg->cmsg_type == SCTP_SNDRCV) { HIO_MEMCPY(sinfo, CMSG_DATA(cmsg), HIO_SIZEOF(*sinfo)); break; @@ -673,7 +676,7 @@ static int recvmsg_sctp( } static int sendmsg_sctp( - int s, const hio_iovec_t* iov, hio_oow_t iovcnt, struct sockaddr* dstaddr, hio_scklen_t dstaddrlen, + int s, const hio_iovec_t* iov, hio_oow_t iovcnt, struct sockaddr* dstaddr, hio_scklen_t dstaddrlen, hio_uint32_t ppid, hio_uint32_t flags, hio_uint16_t stream_no, hio_uint32_t ttl, hio_uint32_t context) { struct sctp_sndrcvinfo* sinfo; @@ -729,7 +732,7 @@ static int dev_sck_read_sctp_sp (hio_dev_t* dev, void* buf, hio_iolen_t* len, hi hio_seterrwithsyserr (hio, 0, eno); - HIO_DEBUG2 (hio, "SCK(%p) - recvfrom failure - %hs", rdev, strerror(eno)); + HIO_DEBUG2 (hio, "SCK(%p) - recvfrom failure - %hs", rdev, strerror(eno)); return -1; } @@ -790,7 +793,7 @@ static int dev_sck_write_stream (hio_dev_t* dev, const void* data, hio_iolen_t* if (*len <= 0) { - /* the write handler for a stream device must handle a zero-length + /* the write handler for a stream device must handle a zero-length * writing request specially. it's a writing finish indicator. close * the writing end of the socket, probably leaving it in the half-closed state */ if (shutdown(rdev->hnd, SHUT_WR) <= -1) @@ -799,7 +802,7 @@ static int dev_sck_write_stream (hio_dev_t* dev, const void* data, hio_iolen_t* return -1; } - /* it must return a non-zero positive value. if it returns 0, this request + /* it must return a non-zero positive value. if it returns 0, this request * gets enqueued by the core. we must aovid it */ return 1; } @@ -809,7 +812,7 @@ static int dev_sck_write_stream (hio_dev_t* dev, const void* data, hio_iolen_t* flags |= MSG_NOSIGNAL; #endif x = send(rdev->hnd, data, *len, flags); - if (x <= -1) + if (x <= -1) { if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */ if (errno == EINTR) return 0; @@ -851,7 +854,7 @@ static int dev_sck_writev_stream (hio_dev_t* dev, const hio_iovec_t* iov, hio_io nwritten = 0; for (i = 0; i < *iovcnt; i++) { - /* no SSL_writev. invoke multiple calls to SSL_write(). + /* no SSL_writev. invoke multiple calls to SSL_write(). * since the write function is for the stream connection, * mutiple calls shouldn't really matter */ x = SSL_write((SSL*)rdev->ssl, iov[i].iov_ptr, iov[i].iov_len); @@ -903,7 +906,7 @@ static int dev_sck_writev_stream (hio_dev_t* dev, const hio_iovec_t* iov, hio_io #else x = writev(rdev->hnd, (const struct iovec*)iov, *iovcnt); #endif - if (x <= -1) + if (x <= -1) { if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */ if (errno == EINTR) return 0; @@ -927,7 +930,7 @@ static int dev_sck_write_stateless (hio_dev_t* dev, const void* data, hio_iolen_ ssize_t x; x = sendto(rdev->hnd, data, *len, 0, dstaddr->ptr, dstaddr->len); - if (x <= -1) + if (x <= -1) { if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */ if (errno == EINTR) return 0; @@ -965,7 +968,7 @@ static int dev_sck_writev_stateless (hio_dev_t* dev, const hio_iovec_t* iov, hio #endif x = sendmsg(rdev->hnd, &msg, flags); - if (x <= -1) + if (x <= -1) { if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */ if (errno == EINTR) return 0; @@ -1006,14 +1009,14 @@ static int dev_sck_write_sctp_sp (hio_dev_t* dev, const void* data, hio_iolen_t* iov.iov_ptr = (void*)data; iov.iov_len = *len; - x = sendmsg_sctp(rdev->hnd, - &iov, 1, dstaddr->ptr, dstaddr->len, + x = sendmsg_sctp(rdev->hnd, + &iov, 1, dstaddr->ptr, dstaddr->len, 0, /* ppid - opaque */ 0, /* flags (e.g. SCTP_UNORDERED, SCTP_EOF, SCT_ABORT, ...) */ hio_skad_get_chan(dstaddr->ptr), /* stream number */ 0, /* ttl */ 0 /* context*/); - if (x <= -1) + if (x <= -1) { if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */ if (errno == EINTR) return 0; @@ -1031,14 +1034,14 @@ static int dev_sck_writev_sctp_sp (hio_dev_t* dev, const hio_iovec_t* iov, hio_i hio_dev_sck_t* rdev = (hio_dev_sck_t*)dev; ssize_t x; - x = sendmsg_sctp(rdev->hnd, - iov, *iovcnt, dstaddr->ptr, dstaddr->len, + x = sendmsg_sctp(rdev->hnd, + iov, *iovcnt, dstaddr->ptr, dstaddr->len, 0, /* ppid - opaque */ 0, /* flags (e.g. SCTP_UNORDERED, SCTP_EOF, SCT_ABORT, ...) */ hio_skad_get_chan(dstaddr->ptr), /* stream number */ 0, /* ttl */ 0 /* context*/); - if (x <= -1) + if (x <= -1) { if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */ if (errno == EINTR) return 0; @@ -1094,7 +1097,7 @@ static int dev_sck_sendfile_stream (hio_dev_t* dev, hio_syshnd_t in_fd, hio_foff if (*len <= 0) { - /* the write handler for a stream device must handle a zero-length + /* the write handler for a stream device must handle a zero-length * writing request specially. it's a writing finish indicator. close * the writing end of the socket, probably leaving it in the half-closed state */ if (shutdown(rdev->hnd, SHUT_WR) <= -1) @@ -1103,7 +1106,7 @@ static int dev_sck_sendfile_stream (hio_dev_t* dev, hio_syshnd_t in_fd, hio_foff return -1; } - /* it must return a non-zero positive value. if it returns 0, this request + /* it must return a non-zero positive value. if it returns 0, this request * gets enqueued by the core. we must aovid it */ return 1; } @@ -1111,7 +1114,7 @@ static int dev_sck_sendfile_stream (hio_dev_t* dev, hio_syshnd_t in_fd, hio_foff #if defined(HAVE_SENDFILE) /* TODO: cater for other systems */ x = sendfile(rdev->hnd, in_fd, &foff, *len); - if (x <= -1) + if (x <= -1) { if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */ if (errno == EINTR) return 0; @@ -1124,7 +1127,7 @@ static int dev_sck_sendfile_stream (hio_dev_t* dev, hio_syshnd_t in_fd, hio_foff return -1; #endif - + #if 0 && defined(USE_SSL) } #endif @@ -1362,7 +1365,7 @@ static int dev_sck_ioctl (hio_dev_t* dev, int cmd, void* arg) } SSL_CTX_set_read_ahead (ssl_ctx, 0); - SSL_CTX_set_mode (ssl_ctx, SSL_CTX_get_mode(ssl_ctx) | + SSL_CTX_set_mode (ssl_ctx, SSL_CTX_get_mode(ssl_ctx) | /*SSL_MODE_ENABLE_PARTIAL_WRITE |*/ SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); @@ -1417,7 +1420,7 @@ static int dev_sck_ioctl (hio_dev_t* dev, int cmd, void* arg) if (sa->sa_family == AF_INET) sl = HIO_SIZEOF(struct sockaddr_in); else if (sa->sa_family == AF_INET6) sl = HIO_SIZEOF(struct sockaddr_in6); - else + else { hio_seterrbfmt (hio, HIO_EINVAL, "unknown address family %d", sa->sa_family); return -1; @@ -1446,7 +1449,7 @@ static int dev_sck_ioctl (hio_dev_t* dev, int cmd, void* arg) } SSL_CTX_set_read_ahead (ssl_ctx, 0); - SSL_CTX_set_mode (ssl_ctx, SSL_CTX_get_mode(ssl_ctx) | + SSL_CTX_set_mode (ssl_ctx, SSL_CTX_get_mode(ssl_ctx) | /* SSL_MODE_ENABLE_PARTIAL_WRITE | */ SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); } @@ -1477,7 +1480,7 @@ fcntl (rdev->hnd, F_SETFL, flags | O_NONBLOCK); if (HIO_IS_POS_NTIME(&conn->connect_tmout)) { - if (schedule_timer_job_after(rdev, &conn->connect_tmout, connect_timedout) <= -1) + if (schedule_timer_job_after(rdev, &conn->connect_tmout, connect_timedout) <= -1) { goto oops_connect; } @@ -1558,7 +1561,7 @@ fcntl (rdev->hnd, F_SETFL, flags | O_NONBLOCK); } x = listen(rdev->hnd, lstn->backlogs); - if (x <= -1) + if (x <= -1) { hio_seterrwithsyserr (hio, 0, errno); return -1; @@ -1590,7 +1593,7 @@ fcntl (rdev->hnd, F_SETFL, flags | O_NONBLOCK); return 0; } -static hio_dev_mth_t dev_mth_sck_stateless = +static hio_dev_mth_t dev_mth_sck_stateless = { dev_sck_make, dev_sck_kill, @@ -1606,7 +1609,7 @@ static hio_dev_mth_t dev_mth_sck_stateless = }; -static hio_dev_mth_t dev_mth_sck_stream = +static hio_dev_mth_t dev_mth_sck_stream = { dev_sck_make, dev_sck_kill, @@ -1622,7 +1625,7 @@ static hio_dev_mth_t dev_mth_sck_stream = }; #if defined(ENABLE_SCTP) -static hio_dev_mth_t dev_mth_sck_sctp_sp = +static hio_dev_mth_t dev_mth_sck_sctp_sp = { dev_sck_make, dev_sck_kill, @@ -1685,7 +1688,7 @@ static hio_dev_mth_t dev_mth_clisck_sctp_sp = }; #endif -static hio_dev_mth_t dev_mth_sck_bpf = +static hio_dev_mth_t dev_mth_sck_bpf = { dev_sck_make, dev_sck_kill, @@ -1732,7 +1735,7 @@ static int harvest_outgoing_connection (hio_dev_sck_t* rdev) addrlen = HIO_SIZEOF(localaddr); if (getsockname(rdev->hnd, (struct sockaddr*)&localaddr, &addrlen) == 0) rdev->localaddr = localaddr; - if (hio_dev_watch((hio_dev_t*)rdev, HIO_DEV_WATCH_RENEW, HIO_DEV_EVENT_IN) <= -1) + if (hio_dev_watch((hio_dev_t*)rdev, HIO_DEV_WATCH_RENEW, HIO_DEV_EVENT_IN) <= -1) { /* watcher update failure. it's critical */ hio_stop (hio, HIO_STOPREQ_WATCHER_ERROR); @@ -1755,7 +1758,7 @@ static int harvest_outgoing_connection (hio_dev_sck_t* rdev) HIO_ASSERT (hio, rdev->tmrjob_index == HIO_TMRIDX_INVALID); /* rdev->tmout has been set to the deadline of the connect task - * when the CONNECT IOCTL command has been executed. use the + * when the CONNECT IOCTL command has been executed. use the * same deadline here */ if (HIO_IS_POS_NTIME(&rdev->tmout) && schedule_timer_job_at(rdev, &rdev->tmout, ssl_connect_timedout) <= -1) @@ -1804,20 +1807,20 @@ static int make_accepted_client_connection (hio_dev_sck_t* rdev, hio_syshnd_t cl if (rdev->on_raw_accept) { - /* this is a special optional callback. If you don't want a client socket device - * to be created upon accept, you may implement the on_raw_accept() handler. + /* this is a special optional callback. If you don't want a client socket device + * to be created upon accept, you may implement the on_raw_accept() handler. * the socket handle is delegated to the callback. */ rdev->on_raw_accept (rdev, clisck, remoteaddr); return 0; } - /* rdev->dev_size: + /* rdev->dev_size: * use rdev->dev_size when instantiating a client sck device - * instead of HIO_SIZEOF(hio_dev_sck_t). therefore, the + * instead of HIO_SIZEOF(hio_dev_sck_t). therefore, the * extension area as big as that of the master socket device - * is created in the client sck device + * is created in the client sck device * dev_mth: - * choose the client socket method base on the master socket + * choose the client socket method base on the master socket * device capability. currently, stream or non-stream is supported. */ #if defined(ENABLE_SCTP) @@ -1826,7 +1829,7 @@ static int make_accepted_client_connection (hio_dev_sck_t* rdev, hio_syshnd_t cl #else dev_mth = (sck_type_map[clisck_type].extra_dev_cap & HIO_DEV_CAP_STREAM)? &dev_mth_clisck_stream: &dev_mth_clisck_stateless; #endif - clidev = (hio_dev_sck_t*)hio_dev_make(hio, rdev->dev_size, dev_mth, rdev->dev_evcb, &clisck); + clidev = (hio_dev_sck_t*)hio_dev_make(hio, rdev->dev_size, dev_mth, rdev->dev_evcb, &clisck); if (HIO_UNLIKELY(!clidev)) { /* [NOTE] 'clisck' is closed by callback methods called by hio_dev_make() upon failure */ @@ -1883,7 +1886,7 @@ static int make_accepted_client_connection (hio_dev_sck_t* rdev, hio_syshnd_t cl /* inherit some event handlers from the parent. * you can still change them inside the on_connect handler */ clidev->on_connect = rdev->on_connect; - clidev->on_disconnect = rdev->on_disconnect; + clidev->on_disconnect = rdev->on_disconnect; clidev->on_raw_accept = HIO_NULL; /* don't inherit this */ clidev->on_write = rdev->on_write; clidev->on_read = rdev->on_read; @@ -2030,7 +2033,7 @@ static int dev_evcb_sck_ready_stream (hio_dev_t* dev, int events) } else { - return 0; /* success but don't invoke on_read() */ + return 0; /* success but don't invoke on_read() */ } case HIO_DEV_SCK_CONNECTING_SSL: @@ -2102,7 +2105,7 @@ static int dev_evcb_sck_ready_stream (hio_dev_t* dev, int events) } else { - return 0; /* success but don't invoke on_read() */ + return 0; /* success but don't invoke on_read() */ } case HIO_DEV_SCK_ACCEPTING_SSL: @@ -2151,7 +2154,7 @@ static int dev_evcb_sck_ready_stream (hio_dev_t* dev, int events) default: if (events & HIO_DEV_EVENT_HUP) { - if (events & (HIO_DEV_EVENT_PRI | HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT)) + if (events & (HIO_DEV_EVENT_PRI | HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT)) { /* probably half-open? */ return 1; @@ -2329,7 +2332,7 @@ static int dev_evcb_sck_on_write_qx (hio_dev_t* dev, hio_iolen_t wrlen, void* wr /* this should not be called */ return 0; } - + return rdev->on_write(rdev, wrlen, wrctx, HIO_NULL); } @@ -2545,7 +2548,7 @@ static int update_mcast_group (hio_dev_sck_t* dev, int join, const hio_skad_t* m return 0; } } - + hio_seterrbfmt (hio_dev_sck_gethio(dev), HIO_EINVAL, "invalid multicast address family"); return -1; } @@ -2613,11 +2616,11 @@ int hio_dev_sck_sendfileok (hio_dev_sck_t* dev) int hio_dev_sck_writetosidechan (hio_dev_sck_t* dev, const void* dptr, hio_oow_t dlen) { - if (write(dev->side_chan, dptr, dlen) <= -1) + if (write(dev->side_chan, dptr, dlen) <= -1) { /* this doesn't set the error information on the main socket. if you may check errno, though */ /* TODO: make hio_seterrbfmt() thread safe and set the error information properly. still the caller may be in the thread-unsafe context */ - return -1; + return -1; } return 0; } @@ -2636,7 +2639,7 @@ hio_uint16_t hio_checksum_ip (const void* hdr, hio_oow_t len) sum = (sum & 0xFFFF) + (sum >> 16); len -= 2; } - + while (sum >> 16) sum = (sum & 0xFFFF) + (sum >> 16); return (hio_uint16_t)~sum; diff --git a/lib/shw.c b/lib/shw.c index 21e3c4e..18fbb00 100644 --- a/lib/shw.c +++ b/lib/shw.c @@ -183,7 +183,7 @@ static hio_syshnd_t dev_shw_getsyshnd (hio_dev_t* dev) return rdev->hnd; } -static hio_dev_mth_t dev_shw_methods = +static hio_dev_mth_t dev_shw_methods = { dev_shw_make, dev_shw_kill, @@ -213,10 +213,10 @@ static int shw_ready (hio_dev_t* dev, int events) if (events & HIO_DEV_EVENT_HUP) { - if (events & (HIO_DEV_EVENT_PRI | HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT)) + if (events & (HIO_DEV_EVENT_PRI | HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT)) { /* probably half-open? */ - return shw->on_ready? shw->on_ready(shw, events): 1; + return shw->on_ready? shw->on_ready(shw, events): 1; } hio_seterrnum (hio, HIO_EDEVHUP); @@ -252,7 +252,7 @@ static hio_dev_evcb_t dev_shw_event_callbacks = hio_dev_shw_t* hio_dev_shw_make (hio_t* hio, hio_oow_t xtnsize, const hio_dev_shw_make_t* info) { return (hio_dev_shw_t*)hio_dev_make( - hio, HIO_SIZEOF(hio_dev_shw_t) + xtnsize, + hio, HIO_SIZEOF(hio_dev_shw_t) + xtnsize, &dev_shw_methods, &dev_shw_event_callbacks, (void*)info); } diff --git a/lib/skad.c b/lib/skad.c index c7e376c..5dc2c1e 100644 --- a/lib/skad.c +++ b/lib/skad.c @@ -55,10 +55,10 @@ typedef struct sockaddr_extra_t sockaddr_extra_t; * you must update the corresponding checks in configure.ac. * * extra fields: - * chan - used as a stream number for SCTP PACKETSEQ sockets. + * chan - used as a stream number for SCTP PACKETSEQ sockets. * use hio_skad_get_chan() and hio_skad_set_chan() for safe access. */ -struct sockaddr_in_x +struct sockaddr_in_x { struct sockaddr_in a; sockaddr_extra_t x; @@ -115,14 +115,14 @@ static int uchars_to_ipv4 (const hio_uch_t* str, hio_oow_t len, struct in_addr* c = *str++; - if (c >= '0' && c <= '9') + if (c >= '0' && c <= '9') { if (digits > 0 && acc == 0) return -1; acc = acc * 10 + (c - '0'); if (acc > 255) return -1; digits++; } - else if (c == '.') + else if (c == '.') { if (dots >= 3 || digits == 0) return -1; addr = (addr << 8) | acc; @@ -157,14 +157,14 @@ static int bchars_to_ipv4 (const hio_bch_t* str, hio_oow_t len, struct in_addr* c = *str++; - if (c >= '0' && c <= '9') + if (c >= '0' && c <= '9') { if (digits > 0 && acc == 0) return -1; acc = acc * 10 + (c - '0'); if (acc > 255) return -1; digits++; } - else if (c == '.') + else if (c == '.') { if (dots >= 3 || digits == 0) return -1; addr = (addr << 8) | acc; @@ -223,10 +223,10 @@ static int uchars_to_ipv6 (const hio_uch_t* src, hio_oow_t len, struct in6_addr* continue; } - if (ch == ':') + if (ch == ':') { curtok = src; - if (!saw_xdigit) + if (!saw_xdigit) { if (colonp) return -1; colonp = tp; @@ -246,23 +246,23 @@ static int uchars_to_ipv6 (const hio_uch_t* src, hio_oow_t len, struct in6_addr* } if (ch == '.' && ((tp + HIO_SIZEOF(struct in_addr)) <= endp) && - uchars_to_ipv4(curtok, src_end - curtok, (struct in_addr*)tp) == 0) + uchars_to_ipv4(curtok, src_end - curtok, (struct in_addr*)tp) == 0) { tp += HIO_SIZEOF(struct in_addr*); saw_xdigit = 0; - break; + break; } return -1; } - if (saw_xdigit) + if (saw_xdigit) { if (tp + HIO_SIZEOF(hio_uint16_t) > endp) return -1; *tp++ = (hio_uint8_t)(val >> 8) & 0xff; *tp++ = (hio_uint8_t)val & 0xff; } - if (colonp != HIO_NULL) + if (colonp != HIO_NULL) { /* * Since some memmove()'s erroneously fail to handle @@ -270,8 +270,8 @@ static int uchars_to_ipv6 (const hio_uch_t* src, hio_oow_t len, struct in6_addr* */ hio_oow_t n = tp - colonp; hio_oow_t i; - - for (i = 1; i <= n; i++) + + for (i = 1; i <= n; i++) { endp[-i] = colonp[n - i]; colonp[n - i] = 0; @@ -327,10 +327,10 @@ static int bchars_to_ipv6 (const hio_bch_t* src, hio_oow_t len, struct in6_addr* continue; } - if (ch == ':') + if (ch == ':') { curtok = src; - if (!saw_xdigit) + if (!saw_xdigit) { if (colonp) return -1; colonp = tp; @@ -350,23 +350,23 @@ static int bchars_to_ipv6 (const hio_bch_t* src, hio_oow_t len, struct in6_addr* } if (ch == '.' && ((tp + HIO_SIZEOF(struct in_addr)) <= endp) && - bchars_to_ipv4(curtok, src_end - curtok, (struct in_addr*)tp) == 0) + bchars_to_ipv4(curtok, src_end - curtok, (struct in_addr*)tp) == 0) { tp += HIO_SIZEOF(struct in_addr*); saw_xdigit = 0; - break; + break; } return -1; } - if (saw_xdigit) + if (saw_xdigit) { if (tp + HIO_SIZEOF(hio_uint16_t) > endp) return -1; *tp++ = (hio_uint8_t)(val >> 8) & 0xff; *tp++ = (hio_uint8_t)val & 0xff; } - if (colonp != HIO_NULL) + if (colonp != HIO_NULL) { /* * Since some memmove()'s erroneously fail to handle @@ -374,8 +374,8 @@ static int bchars_to_ipv6 (const hio_bch_t* src, hio_oow_t len, struct in6_addr* */ hio_oow_t n = tp - colonp; hio_oow_t i; - - for (i = 1; i <= n; i++) + + for (i = 1; i <= n; i++) { endp[-i] = colonp[n - i]; colonp[n - i] = 0; @@ -401,14 +401,14 @@ int hio_ucharstoskad (hio_t* hio, const hio_uch_t* str, hio_oow_t len, hio_skad_ p = str; end = str + len; - if (p >= end) + if (p >= end) { hio_seterrbfmt (hio, HIO_EINVAL, "blank address"); return -1; } /* use HIO_SIZEOF(*_skad) instead of HIO_SIZEOF(*skad) in case they are different */ - HIO_MEMSET (skad, 0, HIO_SIZEOF(*_skad)); + HIO_MEMSET (skad, 0, HIO_SIZEOF(*_skad)); if (p[0] == '<' && p[1] == 'q' && p[2] == 'x' && p[3] == '>' && p[4] == '\0') { @@ -430,7 +430,7 @@ int hio_ucharstoskad (hio_t* hio, const hio_uch_t* str, hio_oow_t len, hio_skad_ return 0; #else hio_seterrbfmt (hio, HIO_ENOIMPL, "unix address not supported"); - return -1; + return -1; #endif } @@ -459,14 +459,14 @@ int hio_ucharstoskad (hio_t* hio, const hio_uch_t* str, hio_oow_t len, hio_skad_ return -1; } - if (*p >= '0' && *p <= '9') + if (*p >= '0' && *p <= '9') { /* numeric scope id */ skad->in6.a.sin6_scope_id = 0; do { x = skad->in6.a.sin6_scope_id * 10 + (*p - '0'); - if (x < skad->in6.a.sin6_scope_id) + if (x < skad->in6.a.sin6_scope_id) { hio_seterrbfmt (hio, HIO_EINVAL, "scope id too large"); return -1; /* overflow */ @@ -504,9 +504,9 @@ int hio_ucharstoskad (hio_t* hio, const hio_uch_t* str, hio_oow_t len, hio_skad_ if (uchars_to_ipv4(tmp.ptr, tmp.len, &skad->in4.a.sin_addr) <= -1) { #if (HIO_SIZEOF_STRUCT_SOCKADDR_IN6 > 0) - /* check if it is an IPv6 address not enclosed in []. + /* check if it is an IPv6 address not enclosed in []. * the port number can't be specified in this format. */ - if (p >= end || *p != ':') + if (p >= end || *p != ':') { /* without :, it can't be an ipv6 address */ goto unrecog; @@ -531,14 +531,14 @@ int hio_ucharstoskad (hio_t* hio, const hio_uch_t* str, hio_oow_t len, hio_skad_ return -1; } - if (*p >= '0' && *p <= '9') + if (*p >= '0' && *p <= '9') { /* numeric scope id */ skad->in6.a.sin6_scope_id = 0; do { x = skad->in6.a.sin6_scope_id * 10 + (*p - '0'); - if (x < skad->in6.a.sin6_scope_id) + if (x < skad->in6.a.sin6_scope_id) { hio_seterrbfmt (hio, HIO_EINVAL, "scope id too large"); return -1; /* overflow */ @@ -573,7 +573,7 @@ int hio_ucharstoskad (hio_t* hio, const hio_uch_t* str, hio_oow_t len, hio_skad_ } #endif - if (p < end && *p == ':') + if (p < end && *p == ':') { /* port number */ hio_uint32_t port = 0; @@ -588,8 +588,8 @@ int hio_ucharstoskad (hio_t* hio, const hio_uch_t* str, hio_oow_t len, hio_skad_ } tmp.len = p - tmp.ptr; - if (tmp.len <= 0 || tmp.len >= 6 || - port > HIO_TYPE_MAX(hio_uint16_t)) + if (tmp.len <= 0 || tmp.len >= 6 || + port > HIO_TYPE_MAX(hio_uint16_t)) { hio_seterrbfmt (hio, HIO_EINVAL, "port number blank or too large"); return -1; @@ -610,7 +610,7 @@ int hio_ucharstoskad (hio_t* hio, const hio_uch_t* str, hio_oow_t len, hio_skad_ unrecog: hio_seterrbfmt (hio, HIO_EINVAL, "unrecognized address"); return -1; - + no_rbrack: hio_seterrbfmt (hio, HIO_EINVAL, "missing right bracket"); return -1; @@ -628,7 +628,7 @@ int hio_bcharstoskad (hio_t* hio, const hio_bch_t* str, hio_oow_t len, hio_skad_ p = str; end = str + len; - if (p >= end) + if (p >= end) { hio_seterrbfmt (hio, HIO_EINVAL, "blank address"); return -1; @@ -653,7 +653,7 @@ int hio_bcharstoskad (hio_t* hio, const hio_bch_t* str, hio_oow_t len, hio_skad_ return 0; #else hio_seterrbfmt (hio, HIO_ENOIMPL, "unix address not supported"); - return -1; + return -1; #endif } @@ -681,14 +681,14 @@ int hio_bcharstoskad (hio_t* hio, const hio_bch_t* str, hio_oow_t len, hio_skad_ return -1; } - if (*p >= '0' && *p <= '9') + if (*p >= '0' && *p <= '9') { /* numeric scope id */ skad->in6.a.sin6_scope_id = 0; do { x = skad->in6.a.sin6_scope_id * 10 + (*p - '0'); - if (x < skad->in6.a.sin6_scope_id) + if (x < skad->in6.a.sin6_scope_id) { hio_seterrbfmt (hio, HIO_EINVAL, "scope id too large"); return -1; /* overflow */ @@ -726,9 +726,9 @@ int hio_bcharstoskad (hio_t* hio, const hio_bch_t* str, hio_oow_t len, hio_skad_ if (bchars_to_ipv4(tmp.ptr, tmp.len, &skad->in4.a.sin_addr) <= -1) { #if (HIO_SIZEOF_STRUCT_SOCKADDR_IN6 > 0) - /* check if it is an IPv6 address not enclosed in []. + /* check if it is an IPv6 address not enclosed in []. * the port number can't be specified in this format. */ - if (p >= end || *p != ':') + if (p >= end || *p != ':') { /* without :, it can't be an ipv6 address */ goto unrecog; @@ -754,14 +754,14 @@ int hio_bcharstoskad (hio_t* hio, const hio_bch_t* str, hio_oow_t len, hio_skad_ return -1; } - if (*p >= '0' && *p <= '9') + if (*p >= '0' && *p <= '9') { /* numeric scope id */ skad->in6.a.sin6_scope_id = 0; do { x = skad->in6.a.sin6_scope_id * 10 + (*p - '0'); - if (x < skad->in6.a.sin6_scope_id) + if (x < skad->in6.a.sin6_scope_id) { hio_seterrbfmt (hio, HIO_EINVAL, "scope id too large"); return -1; /* overflow */ @@ -796,7 +796,7 @@ int hio_bcharstoskad (hio_t* hio, const hio_bch_t* str, hio_oow_t len, hio_skad_ } #endif - if (p < end && *p == ':') + if (p < end && *p == ':') { /* port number */ hio_uint32_t port = 0; @@ -811,8 +811,8 @@ int hio_bcharstoskad (hio_t* hio, const hio_bch_t* str, hio_oow_t len, hio_skad_ } tmp.len = p - tmp.ptr; - if (tmp.len <= 0 || tmp.len >= 6 || - port > HIO_TYPE_MAX(hio_uint16_t)) + if (tmp.len <= 0 || tmp.len >= 6 || + port > HIO_TYPE_MAX(hio_uint16_t)) { hio_seterrbfmt (hio, HIO_EINVAL, "port number blank or too large"); return -1; @@ -933,9 +933,9 @@ static hio_oow_t ip6ad_to_ucstr (const struct in6_addr* ipad, hio_uch_t* buf, hi cur.base = -1; cur.len = 0; - for (i = 0; i < IP6AD_NWORDS; i++) + for (i = 0; i < IP6AD_NWORDS; i++) { - if (words[i] == 0) + if (words[i] == 0) { if (cur.base == -1) { @@ -947,16 +947,16 @@ static hio_oow_t ip6ad_to_ucstr (const struct in6_addr* ipad, hio_uch_t* buf, hi cur.len++; } } - else + else { - if (cur.base != -1) + if (cur.base != -1) { if (best.base == -1 || cur.len > best.len) best = cur; cur.base = -1; } } } - if (cur.base != -1) + if (cur.base != -1) { if (best.base == -1 || cur.len > best.len) best = cur; } @@ -966,11 +966,11 @@ static hio_oow_t ip6ad_to_ucstr (const struct in6_addr* ipad, hio_uch_t* buf, hi * Format the result. */ tp = tmp; - for (i = 0; i < IP6AD_NWORDS; i++) + for (i = 0; i < IP6AD_NWORDS; i++) { /* Are we inside the best run of 0x00's? */ if (best.base != -1 && i >= best.base && - i < (best.base + best.len)) + i < (best.base + best.len)) { if (i == best.base) *tp++ = ':'; continue; @@ -980,7 +980,7 @@ static hio_oow_t ip6ad_to_ucstr (const struct in6_addr* ipad, hio_uch_t* buf, hi if (i != 0) *tp++ = ':'; /* Is this address an encapsulated IPv4? ipv4-compatible or ipv4-mapped */ - if (i == 6 && best.base == 0 && (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) + if (i == 6 && best.base == 0 && (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) { struct in_addr ip4ad; HIO_MEMCPY (&ip4ad.s_addr, ipad->s6_addr + 12, HIO_SIZEOF(ip4ad.s_addr)); @@ -1071,7 +1071,7 @@ hio_oow_t hio_skadtoucstr (hio_t* hio, const hio_skad_t* _skad, hio_uch_t* buf, if (flags & HIO_SKAD_TO_UCSTR_PORT) { - if (!(flags & HIO_SKAD_TO_UCSTR_ADDR) || skad->in6.a.sin6_port != 0) + if (!(flags & HIO_SKAD_TO_UCSTR_ADDR) || skad->in6.a.sin6_port != 0) { if (flags & HIO_SKAD_TO_UCSTR_ADDR) { @@ -1194,9 +1194,9 @@ static hio_oow_t ip6ad_to_bcstr (const struct in6_addr* ipad, hio_bch_t* buf, hi cur.base = -1; cur.len = 0; - for (i = 0; i < IP6AD_NWORDS; i++) + for (i = 0; i < IP6AD_NWORDS; i++) { - if (words[i] == 0) + if (words[i] == 0) { if (cur.base == -1) { @@ -1208,16 +1208,16 @@ static hio_oow_t ip6ad_to_bcstr (const struct in6_addr* ipad, hio_bch_t* buf, hi cur.len++; } } - else + else { - if (cur.base != -1) + if (cur.base != -1) { if (best.base == -1 || cur.len > best.len) best = cur; cur.base = -1; } } } - if (cur.base != -1) + if (cur.base != -1) { if (best.base == -1 || cur.len > best.len) best = cur; } @@ -1227,11 +1227,11 @@ static hio_oow_t ip6ad_to_bcstr (const struct in6_addr* ipad, hio_bch_t* buf, hi * Format the result. */ tp = tmp; - for (i = 0; i < IP6AD_NWORDS; i++) + for (i = 0; i < IP6AD_NWORDS; i++) { /* Are we inside the best run of 0x00's? */ if (best.base != -1 && i >= best.base && - i < (best.base + best.len)) + i < (best.base + best.len)) { if (i == best.base) *tp++ = ':'; continue; @@ -1241,7 +1241,7 @@ static hio_oow_t ip6ad_to_bcstr (const struct in6_addr* ipad, hio_bch_t* buf, hi if (i != 0) *tp++ = ':'; /* Is this address an encapsulated IPv4? ipv4-compatible or ipv4-mapped */ - if (i == 6 && best.base == 0 && (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) + if (i == 6 && best.base == 0 && (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) { struct in_addr ip4ad; HIO_MEMCPY (&ip4ad.s_addr, ipad->s6_addr + 12, HIO_SIZEOF(ip4ad.s_addr)); @@ -1333,7 +1333,7 @@ hio_oow_t hio_skadtobcstr (hio_t* hio, const hio_skad_t* _skad, hio_bch_t* buf, if (flags & HIO_SKAD_TO_BCSTR_PORT) { - if (!(flags & HIO_SKAD_TO_BCSTR_ADDR) || skad->in6.a.sin6_port != 0) + if (!(flags & HIO_SKAD_TO_BCSTR_ADDR) || skad->in6.a.sin6_port != 0) { if (flags & HIO_SKAD_TO_BCSTR_ADDR) { @@ -1646,7 +1646,7 @@ int hio_equal_skads (const hio_skad_t* addr1, const hio_skad_t* addr2, int stric #if defined(AF_INET6) && (HIO_SIZEOF_STRUCT_SOCKADDR_IN6 > 0) case AF_INET6: - + if (strict) { return HIO_MEMCMP(&((struct sockaddr_in6*)addr1)->sin6_addr, &((struct sockaddr_in6*)addr2)->sin6_addr, HIO_SIZEOF(((struct sockaddr_in6*)addr2)->sin6_addr)) == 0 && @@ -1787,7 +1787,7 @@ int hio_ipad_bytes_is_loop_back (const hio_uint8_t* iptr, hio_oow_t ilen) { hio_uint32_t* x = (hio_uint32_t*)iptr; return (x[0] == 0 && x[1] == 0 && x[2] == 0 && x[3] == HIO_CONST_HTON32(1)) || /* TODO: is this alignment safe? */ - (hio_ipad_bytes_is_v4_mapped(iptr, ilen) && (x[3] & HIO_CONST_HTON32(0xFF000000u)) == HIO_CONST_HTON32(0x7F000000u)); + (hio_ipad_bytes_is_v4_mapped(iptr, ilen) && (x[3] & HIO_CONST_HTON32(0xFF000000u)) == HIO_CONST_HTON32(0x7F000000u)); } default: diff --git a/lib/sys-ass.c b/lib/sys-ass.c index e1ca3c0..a0a680b 100644 --- a/lib/sys-ass.c +++ b/lib/sys-ass.c @@ -94,7 +94,7 @@ static void backtrace_stack_frames (hio_t* hio) unw_init_local(&cursor, &context); hio_logbfmt (hio, HIO_LOG_UNTYPED | HIO_LOG_DEBUG, "[BACKTRACE]\n"); - for (n = 0; unw_step(&cursor) > 0; n++) + for (n = 0; unw_step(&cursor) > 0; n++) { unw_word_t ip, sp, off; char symbol[256]; @@ -102,13 +102,13 @@ static void backtrace_stack_frames (hio_t* hio) unw_get_reg (&cursor, UNW_REG_IP, &ip); unw_get_reg (&cursor, UNW_REG_SP, &sp); - if (unw_get_proc_name(&cursor, symbol, HIO_COUNTOF(symbol), &off)) + if (unw_get_proc_name(&cursor, symbol, HIO_COUNTOF(symbol), &off)) { hio_copy_bcstr (symbol, HIO_COUNTOF(symbol), ""); } - hio_logbfmt (hio, HIO_LOG_UNTYPED | HIO_LOG_DEBUG, - "#%02d ip=0x%*p sp=0x%*p %s+0x%zu\n", + hio_logbfmt (hio, HIO_LOG_UNTYPED | HIO_LOG_DEBUG, + "#%02d ip=0x%*p sp=0x%*p %s+0x%zu\n", n, HIO_SIZEOF(void*) * 2, (void*)ip, HIO_SIZEOF(void*) * 2, (void*)sp, symbol, (hio_oow_t)off); } } diff --git a/lib/sys-err.c b/lib/sys-err.c index 53449f9..227a356 100644 --- a/lib/sys-err.c +++ b/lib/sys-err.c @@ -60,7 +60,7 @@ static hio_errnum_t errno_to_errnum (int errcode) #endif #if defined(EAGAIN) && defined(EWOULDBLOCK) && (EAGAIN != EWOULDBLOCK) - case EAGAIN: + case EAGAIN: case EWOULDBLOCK: return HIO_EAGAIN; #elif defined(EAGAIN) case EAGAIN: return HIO_EAGAIN; @@ -152,14 +152,14 @@ static hio_errnum_t os2err_to_errnum (APIRET errcode) case ERROR_NOT_ENOUGH_MEMORY: return HIO_ESYSMEM; - case ERROR_INVALID_PARAMETER: + case ERROR_INVALID_PARAMETER: case ERROR_INVALID_NAME: return HIO_EINVAL; - case ERROR_INVALID_HANDLE: + case ERROR_INVALID_HANDLE: return HIO_EBADHND; - case ERROR_ACCESS_DENIED: + case ERROR_ACCESS_DENIED: case ERROR_SHARING_VIOLATION: return HIO_EACCES; @@ -196,7 +196,7 @@ static hio_errnum_t macerr_to_errnum (int errcode) return HIO_ENOENT; /*TODO: add more mappings */ - default: + default: return HIO_ESYSERR; } } @@ -206,14 +206,14 @@ hio_errnum_t hio_sys_syserrstrb (hio_t* hio, int syserr_type, int syserr_code, h { switch (syserr_type) { - case 1: + case 1: #if defined(_WIN32) if (buf) { DWORD rc; rc = FormatMessageA ( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, syserr_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + NULL, syserr_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, len, HIO_NULL ); while (rc > 0 && buf[rc - 1] == '\r' || buf[rc - 1] == '\n') buf[--rc] = '\0'; diff --git a/lib/sys-log.c b/lib/sys-log.c index 21fc6f4..34f1b33 100644 --- a/lib/sys-log.c +++ b/lib/sys-log.c @@ -167,7 +167,7 @@ static int write_log (hio_t* hio, int fd, hio_bitmask_t mask, const hio_bch_t* p log->out.len += len; ptr += len; len -= len; - + } } } @@ -222,7 +222,7 @@ void hio_sys_writelog (hio_t* hio, hio_bitmask_t mask, const hio_ooch_t* msg, hi #if defined(_WIN32) tmp = localtime(&now); tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp); - if (tslen == 0) + if (tslen == 0) { tslen = sprintf(ts, "%04d-%02d-%02d %02d:%02d:%02d ", tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); } @@ -239,7 +239,7 @@ void hio_sys_writelog (hio_t* hio, hio_bitmask_t mask, const hio_ooch_t* msg, hi #else tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp); #endif - if (tslen == 0) + if (tslen == 0) { tslen = sprintf(ts, "%04d-%02d-%02d %02d:%02d:%02d ", tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); } @@ -258,9 +258,9 @@ void hio_sys_writelog (hio_t* hio, hio_bitmask_t mask, const hio_ooch_t* msg, hi #if defined(HAVE_STRFTIME_SMALL_Z) tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp); #else - tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %Z ", tmp); + tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %Z ", tmp); #endif - if (tslen == 0) + if (tslen == 0) { tslen = sprintf(ts, "%04d-%02d-%02d %02d:%02d:%02d ", tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); } @@ -285,9 +285,9 @@ void hio_sys_writelog (hio_t* hio, hio_bitmask_t mask, const hio_ooch_t* msg, hi n = hio_convootobchars(hio, &msg[msgidx], &ucslen, buf, &bcslen); if (n == 0 || n == -2) { - /* n = 0: - * converted all successfully - * n == -2: + /* n = 0: + * converted all successfully + * n == -2: * buffer not sufficient. not all got converted yet. * write what have been converted this round. */ @@ -347,7 +347,7 @@ void hio_sys_finilog (hio_t* hio) if (hio->_features & HIO_FEATURE_LOG_WRITER) { - if ((log->fd_flag & LOGFD_OPENED_HERE) && log->fd >= 0) + if ((log->fd_flag & LOGFD_OPENED_HERE) && log->fd >= 0) { close (log->fd); log->fd = -1; diff --git a/lib/sys-mux.c b/lib/sys-mux.c index 2f87f3a..06f45af 100644 --- a/lib/sys-mux.c +++ b/lib/sys-mux.c @@ -69,7 +69,7 @@ int hio_sys_initmux (hio_t* hio) secure_poll_data_slot_for_insert(hio) <= -1) { /* no control pipes if registration fails */ - close (mux->ctrlp[1]); + close (mux->ctrlp[1]); mux->ctrlp[1] = HIO_SYSHND_INVALID; close (mux->ctrlp[0]); mux->ctrlp[0] = HIO_SYSHND_INVALID; @@ -154,7 +154,7 @@ epoll_create_done: if (epoll_ctl(mux->hnd, EPOLL_CTL_ADD, mux->ctrlp[0], &ev) == -1) { /* if ADD fails, close the control pipes and forget them */ - close (mux->ctrlp[1]); + close (mux->ctrlp[1]); mux->ctrlp[1] = HIO_SYSHND_INVALID; close (mux->ctrlp[0]); mux->ctrlp[0] = HIO_SYSHND_INVALID; @@ -169,19 +169,19 @@ void hio_sys_finimux (hio_t* hio) { hio_sys_mux_t* mux = &hio->sysdep->mux; #if defined(USE_POLL) - if (mux->map.ptr) + if (mux->map.ptr) { hio_freemem (hio, mux->map.ptr); mux->map.ptr = HIO_NULL; mux->map.capa = 0; } - if (mux->pd.pfd) + if (mux->pd.pfd) { hio_freemem (hio, mux->pd.pfd); mux->pd.pfd = HIO_NULL; } - if (mux->pd.dptr) + if (mux->pd.dptr) { hio_freemem (hio, mux->pd.dptr); mux->pd.dptr = HIO_NULL; @@ -215,7 +215,7 @@ void hio_sys_finimux (hio_t* hio) if (mux->ctrlp[1] != HIO_SYSHND_INVALID) { - close (mux->ctrlp[1]); + close (mux->ctrlp[1]); mux->ctrlp[1] = HIO_SYSHND_INVALID; } @@ -347,7 +347,7 @@ int hio_sys_ctrlmux (hio_t* hio, hio_sys_mux_cmd_t cmd, hio_dev_t* dev, int dev_ hio_seterrnum (hio, HIO_ENOENT); return -1; } - else if (idx == MUX_INDEX_SUSPENDED) + else if (idx == MUX_INDEX_SUSPENDED) { if (!events) return 0; /* no change. keep suspended */ goto do_insert; @@ -384,7 +384,7 @@ int hio_sys_ctrlmux (hio_t* hio, hio_sys_mux_cmd_t cmd, hio_dev_t* dev, int dev_ /* TODO: speed up deletion. allow a hole in the array. * delay array compaction if there is a hole. * set fd for the hole to -1 such that poll() - * ignores it. compact the array if another deletion + * ignores it. compact the array if another deletion * is requested when there is an existing hole. */ idx++; while (idx < mux->pd.size) @@ -419,7 +419,7 @@ int hio_sys_ctrlmux (hio_t* hio, hio_sys_mux_cmd_t cmd, hio_dev_t* dev, int dev_ HIO_ASSERT (hio, hio == dev->hio); /* no operation over a broken(closed) handle to prevent multiplexer from failing. - * close of the handle leads to auto-deletion from the kqueue multiplexer. + * close of the handle leads to auto-deletion from the kqueue multiplexer. * the closed handle must not be fed to the multiplexer */ if (dev->dev_mth->issyshndbroken && dev->dev_mth->issyshndbroken(dev)) return 0; hnd = dev->dev_mth->getsyshnd(dev); @@ -438,15 +438,15 @@ int hio_sys_ctrlmux (hio_t* hio, hio_sys_mux_cmd_t cmd, hio_dev_t* dev, int dev_ i_flag = (dev_cap & HIO_DEV_CAP_IN_WATCHED)? EV_ENABLE: EV_DISABLE; o_flag = (dev_cap & HIO_DEV_CAP_OUT_WATCHED)? EV_ENABLE: EV_DISABLE; - EV_SET (&chlist[1], hnd, EVFILT_READ, EV_ADD | i_flag, 0, 0, dev); + EV_SET (&chlist[1], hnd, EVFILT_READ, EV_ADD | i_flag, 0, 0, dev); EV_SET (&chlist[0], hnd, EVFILT_WRITE, EV_ADD | o_flag, 0, 0, dev); x = kevent(mux->kq, chlist, 2, HIO_NULL, 0, HIO_NULL); - if (x >= 0) dev->dev_cap |= HIO_DEV_CAP_WATCH_REREG_REQUIRED; /* ugly hack for the listening sockets in NetBSD */ + if (x >= 0) dev->dev_cap |= HIO_DEV_CAP_WATCH_REREG_REQUIRED; /* ugly hack for the listening sockets in NetBSD */ - /* the CMD_INSERT comes with at MIO_DEV_CAP_IN_WATCHD set. + /* the CMD_INSERT comes with at MIO_DEV_CAP_IN_WATCHD set. * skip checking to set WATCH_SUSPENDED */ - + break; } @@ -456,11 +456,11 @@ int hio_sys_ctrlmux (hio_t* hio, hio_sys_mux_cmd_t cmd, hio_dev_t* dev, int dev_ i_flag = (dev_cap & HIO_DEV_CAP_IN_WATCHED)? EV_ENABLE: EV_DISABLE; o_flag = (dev_cap & HIO_DEV_CAP_OUT_WATCHED)? EV_ENABLE: EV_DISABLE; - EV_SET (&chlist[0], hnd, EVFILT_READ, EV_ADD | i_flag, 0, 0, dev); + EV_SET (&chlist[0], hnd, EVFILT_READ, EV_ADD | i_flag, 0, 0, dev); EV_SET (&chlist[1], hnd, EVFILT_WRITE, EV_ADD | o_flag, 0, 0, dev); x = kevent(mux->kq, chlist, 2, HIO_NULL, 0, HIO_NULL); - if (x >= 0) + if (x >= 0) { if (i_flag == EV_DISABLE && o_flag == EV_DISABLE) dev->dev_cap &= ~HIO_DEV_CAP_WATCH_SUSPENDED; @@ -500,13 +500,13 @@ int hio_sys_ctrlmux (hio_t* hio, hio_sys_mux_cmd_t cmd, hio_dev_t* dev, int dev_ HIO_ASSERT (hio, hio == dev->hio); /* no operation over a broken(closed) handle to prevent multiplexer from failing. - * close of the handle leads to auto-deletion from the epoll multiplexer. + * close() of the handle leads to auto-deletion from the epoll multiplexer. * the closed handle must not be fed to the multiplexer */ if (dev->dev_mth->issyshndbroken && dev->dev_mth->issyshndbroken(dev)) return 0; hnd = dev->dev_mth->getsyshnd(dev); events = 0; - if (dev_cap & HIO_DEV_CAP_IN_WATCHED) + if (dev_cap & HIO_DEV_CAP_IN_WATCHED) { events |= EPOLLIN; #if defined(EPOLLRDHUP) @@ -560,7 +560,7 @@ int hio_sys_ctrlmux (hio_t* hio, hio_sys_mux_cmd_t cmd, hio_dev_t* dev, int dev_ break; case HIO_SYS_MUX_CMD_DELETE: - if (dev->dev_cap & HIO_DEV_CAP_WATCH_SUSPENDED) + if (dev->dev_cap & HIO_DEV_CAP_WATCH_SUSPENDED) { /* clear the SUSPENDED bit because it's a normal deletion */ dev->dev_cap &= ~HIO_DEV_CAP_WATCH_SUSPENDED; @@ -651,7 +651,7 @@ int hio_sys_waitmux (hio_t* hio, const hio_ntime_t* tmout, hio_sys_mux_evtcb_t e int events = 0; hio_dev_t* dev; - dev = mux->revs[i].udata; + dev = mux->revs[i].udata; if (HIO_LIKELY(dev)) { @@ -690,7 +690,7 @@ int hio_sys_waitmux (hio_t* hio, const hio_ntime_t* tmout, hio_sys_mux_evtcb_t e } /* TODO: merge events??? for the same descriptor */ - + for (i = 0; i < nentries; i++) { int events = 0, rdhup = 0; diff --git a/lib/sys-prv.h b/lib/sys-prv.h index 74964cf..876a9fc 100644 --- a/lib/sys-prv.h +++ b/lib/sys-prv.h @@ -76,7 +76,7 @@ struct hio_sys_mux_t int kq; struct kevent revs[1024]; /* TODO: is it a good size? */ - + int ctrlp[2]; }; diff --git a/lib/sys.c b/lib/sys.c index 7c19acb..213e34c 100644 --- a/lib/sys.c +++ b/lib/sys.c @@ -54,7 +54,7 @@ oops: if (time_inited) hio_sys_finitime (hio); if (mux_inited) hio_sys_finimux (hio); if (log_inited) hio_sys_finilog (hio); - if (hio->sysdep) + if (hio->sysdep) { hio_freemem (hio, hio->sysdep); hio->sysdep = HIO_NULL; diff --git a/lib/tar.c b/lib/tar.c index c2db731..f3e4699 100644 --- a/lib/tar.c +++ b/lib/tar.c @@ -16,7 +16,7 @@ static int verify_checksum(const char *p) { int n, u = 0; - for (n = 0; n < 512; n++) + for (n = 0; n < 512; n++) { if (n < 148 || n > 155) /* Standard tar checksum adds unsigned bytes. */ @@ -37,16 +37,16 @@ static int create_dir (hio_bch_t *pathname, int mode) pathlen = hio_count_bcstr(pathname); - /* Strip trailing '/' TODO: improve ...*/ + /* Strip trailing '/' TODO: improve ...*/ if (pathname[pathlen - 1] == '/') pathname[pathlen - 1] = '\0'; n = mkdir(pathname, mode); - if (n <= -1) + if (n <= -1) { /* On failure, try creating parent directory. */ p = hio_rfind_bchar_in_bcstr(pathname, '/'); - if (p) + if (p) { *p = '\0'; create_dir(pathname, 0755); @@ -123,7 +123,7 @@ static int x_process_header (hio_tar_t* tar) hdr = (hio_tar_hdr_t*)tar->x.blk.buf; /* all-zero byte block ends the archive */ - if (HIO_MEMCMP(hdr, _end_block, HIO_TAR_BLKSIZE) == 0) + if (HIO_MEMCMP(hdr, _end_block, HIO_TAR_BLKSIZE) == 0) { /* two all-zero blocks are expected as the EOF indicator */ tar->x.state = HIO_TAR_STATE_END; @@ -156,7 +156,7 @@ static int x_process_header (hio_tar_t* tar) hio_becs_cat(&tar->x.hi.filename, hdr->name) == (hio_oow_t)-1) return -1; filename = HIO_BECS_PTR(&tar->x.hi.filename); - switch (hdr->typeflag) + switch (hdr->typeflag) { case HIO_TAR_LNKTYPE: link (hdr->linkname, filename); @@ -164,7 +164,7 @@ static int x_process_header (hio_tar_t* tar) case HIO_TAR_SYMTYPE: symlink (hdr->linkname, filename); /* TODO: error check */ break; - case HIO_TAR_CHRTYPE: + case HIO_TAR_CHRTYPE: mknod (filename, S_IFCHR | tar->x.hi.filemode, ((tar->x.hi.devmajor << 8) | tar->x.hi.devminor)); break; case HIO_TAR_BLKTYPE: diff --git a/lib/thr.c b/lib/thr.c index 033eeac..b63c082 100644 --- a/lib/thr.c +++ b/lib/thr.c @@ -61,19 +61,19 @@ static hio_dev_thr_slave_t* make_slave (hio_t* hio, slave_info_t* si); static void free_thr_info_resources (hio_t* hio, hio_dev_thr_info_t* ti) { - if (ti->thr_iop.rfd != HIO_SYSHND_INVALID) + if (ti->thr_iop.rfd != HIO_SYSHND_INVALID) { /* this function is called at the end of run_thr_func() and * close() can be a thread cancellation point. * - * i must invalidate ti->thr_iop.rfd calling close() with it. - * if resetting is done after close() and close() becomes a cancellation point, + * i must invalidate ti->thr_iop.rfd calling close() with it. + * if resetting is done after close() and close() becomes a cancellation point, * the invalidation operation gets skipped. */ hio_syshnd_t tmp = ti->thr_iop.rfd; - ti->thr_iop.rfd = HIO_SYSHND_INVALID; + ti->thr_iop.rfd = HIO_SYSHND_INVALID; close (tmp); } - if (ti->thr_iop.wfd != HIO_SYSHND_INVALID) + if (ti->thr_iop.wfd != HIO_SYSHND_INVALID) { hio_syshnd_t tmp = ti->thr_iop.wfd; ti->thr_iop.wfd = HIO_SYSHND_INVALID; @@ -126,7 +126,7 @@ static void* run_thr_func (void* ctx) ti->thr_func (ti->hio, &ti->thr_iop, ti->thr_ctx); - free_thr_info_resources (ti->hio, ti); + free_thr_info_resources (ti->hio, ti); pthread_cleanup_pop (1); pthread_exit (HIO_NULL); @@ -176,10 +176,10 @@ pipe_done: si.dev_cap = HIO_DEV_CAP_OUT | HIO_DEV_CAP_STREAM; si.id = HIO_DEV_THR_IN; - /* invalidate pfds[1] before calling make_slave() because when it fails, the + /* invalidate pfds[1] before calling make_slave() because when it fails, the * fail_before_make(dev_thr_fail_before_make_slave) and kill(dev_thr_kill_slave) callbacks close si.pfd */ pfds[1] = HIO_SYSHND_INVALID; - + rdev->slave[HIO_DEV_THR_IN] = make_slave(hio, &si); if (!rdev->slave[HIO_DEV_THR_IN]) goto oops; rdev->slave_count++; @@ -188,14 +188,14 @@ pipe_done: si.pfd = pfds[2]; si.dev_cap = HIO_DEV_CAP_IN | HIO_DEV_CAP_STREAM; si.id = HIO_DEV_THR_OUT; - /* invalidate pfds[2] before calling make_slave() because when it fails, the + /* invalidate pfds[2] before calling make_slave() because when it fails, the * fail_before_make(dev_thr_fail_before_make_slave) and kill(dev_thr_kill_slave) callbacks close si.pfd */ pfds[2] = HIO_SYSHND_INVALID; rdev->slave[HIO_DEV_THR_OUT] = make_slave(hio, &si); if (!rdev->slave[HIO_DEV_THR_OUT]) goto oops; rdev->slave_count++; - for (i = 0; i < HIO_COUNTOF(rdev->slave); i++) + for (i = 0; i < HIO_COUNTOF(rdev->slave); i++) { if (rdev->slave[i]) rdev->slave[i]->master = rdev; } @@ -204,7 +204,7 @@ pipe_done: rdev->on_read = info->on_read; rdev->on_write = info->on_write; rdev->on_close = info->on_close; - + /* ---------------------------------------------------------- */ { int n; @@ -221,7 +221,7 @@ pipe_done: rdev->thr_info = ti; n = pthread_create(&ti->thr_hnd, HIO_NULL, run_thr_func, ti); - if (n != 0) + if (n != 0) { rdev->thr_info = HIO_NULL; hio_freemem (hio, ti); @@ -240,7 +240,7 @@ pipe_done: oops: for (i = 0; i < HIO_COUNTOF(pfds); i++) { - if (pfds[i] != HIO_SYSHND_INVALID) + if (pfds[i] != HIO_SYSHND_INVALID) { close (pfds[i]); } @@ -307,7 +307,7 @@ static int dev_thr_kill_master (hio_dev_t* dev, int force) } rdev->thr_info = HIO_NULL; - if (ti->thr_done) + if (ti->thr_done) { pthread_detach (ti->thr_hnd); /* pthread_join() may be blocking. detach the thread instead */ free_thr_info_resources (hio, ti); @@ -316,7 +316,7 @@ static int dev_thr_kill_master (hio_dev_t* dev, int force) else { #if 0 - /* since pthread_join can be blocking, i'd schedule a resource destroyer with hio_addcfmb(). + /* since pthread_join can be blocking, i'd schedule a resource destroyer with hio_addcfmb(). * see after #else */ pthread_join (ti->thr_hnd, HIO_NULL); free_thr_info_resources (hio, ti); @@ -352,7 +352,7 @@ static int dev_thr_kill_slave (hio_dev_t* dev, int force) if (master->slave[rdev->id]) { /* this call is started by the slave device itself. */ - if (master->slave_count <= 0) + if (master->slave_count <= 0) { /* if this is the last slave, kill the master also */ hio_dev_kill ((hio_dev_t*)master); @@ -392,7 +392,7 @@ static int dev_thr_read_slave (hio_dev_t* dev, void* buf, hio_iolen_t* len, hio_ ssize_t x; /* the read and write operation happens on different slave devices. - * the write EOF indication doesn't affect this device + * the write EOF indication doesn't affect this device if (HIO_UNLIKELY(thr->pfd == HIO_SYSHND_INVALID)) { hio_seterrnum (thr->hio, HIO_EBADHND); @@ -420,7 +420,7 @@ static int dev_thr_write_slave (hio_dev_t* dev, const void* data, hio_iolen_t* l /* this check is not needed because HIO_DEV_CAP_OUT_CLOSED is set on the device by the core * when EOF indication is successful(return value 1 and *iovcnt 0). - * If HIO_DEV_CAP_OUT_CLOSED, the core doesn't invoke the write method + * If HIO_DEV_CAP_OUT_CLOSED, the core doesn't invoke the write method if (HIO_UNLIKELY(thr->pfd == HIO_SYSHND_INVALID)) { hio_seterrnum (thr->hio, HIO_EBADHND); @@ -462,7 +462,7 @@ static int dev_thr_writev_slave (hio_dev_t* dev, const hio_iovec_t* iov, hio_iol /* this check is not needed because HIO_DEV_CAP_OUT_CLOSED is set on the device by the core * when EOF indication is successful(return value 1 and *iovcnt 0). - * If HIO_DEV_CAP_OUT_CLOSED, the core doesn't invoke the write method + * If HIO_DEV_CAP_OUT_CLOSED, the core doesn't invoke the write method if (HIO_UNLIKELY(thr->pfd == HIO_SYSHND_INVALID)) { hio_seterrnum (thr->hio, HIO_EBADHND); @@ -528,11 +528,11 @@ static int dev_thr_ioctl (hio_dev_t* dev, int cmd, void* arg) if (rdev->slave[sid]) { /* unlike dev_thr_kill_master(), i don't nullify rdev->slave[sid]. - * so i treat the closing ioctl as if it's a kill request + * so i treat the closing ioctl as if it's a kill request * initiated by the slave device itself. */ hio_dev_kill ((hio_dev_t*)rdev->slave[sid]); - /* if this is the last slave, the master is destroyed as well. + /* if this is the last slave, the master is destroyed as well. * therefore, using rdev is unsafe in the assertion below is unsafe. *HIO_ASSERT (hio, rdev->slave[sid] == HIO_NULL); */ } @@ -560,7 +560,7 @@ static int dev_thr_ioctl (hio_dev_t* dev, int cmd, void* arg) } } -static hio_dev_mth_t dev_thr_methods = +static hio_dev_mth_t dev_thr_methods = { dev_thr_make_master, dev_thr_kill_master, @@ -635,7 +635,7 @@ static int thr_ready_slave (hio_dev_t* dev, int events) if (events & HIO_DEV_EVENT_HUP) { - if (events & (HIO_DEV_EVENT_PRI | HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT)) + if (events & (HIO_DEV_EVENT_PRI | HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT)) { /* thrbably half-open? */ return 1; @@ -683,12 +683,12 @@ static hio_dev_thr_slave_t* make_slave (hio_t* hio, slave_info_t* si) { case HIO_DEV_THR_IN: return (hio_dev_thr_slave_t*)hio_dev_make( - hio, HIO_SIZEOF(hio_dev_thr_t), + hio, HIO_SIZEOF(hio_dev_thr_t), &dev_thr_methods_slave, &dev_thr_event_callbacks_slave_in, si); case HIO_DEV_THR_OUT: return (hio_dev_thr_slave_t*)hio_dev_make( - hio, HIO_SIZEOF(hio_dev_thr_t), + hio, HIO_SIZEOF(hio_dev_thr_t), &dev_thr_methods_slave, &dev_thr_event_callbacks_slave_out, si); default: @@ -700,7 +700,7 @@ static hio_dev_thr_slave_t* make_slave (hio_t* hio, slave_info_t* si) hio_dev_thr_t* hio_dev_thr_make (hio_t* hio, hio_oow_t xtnsize, const hio_dev_thr_make_t* info) { return (hio_dev_thr_t*)hio_dev_make( - hio, HIO_SIZEOF(hio_dev_thr_t) + xtnsize, + hio, HIO_SIZEOF(hio_dev_thr_t) + xtnsize, &dev_thr_methods, &dev_thr_event_callbacks, (void*)info); } diff --git a/lib/tmr.c b/lib/tmr.c index da639d6..c6b7805 100644 --- a/lib/tmr.c +++ b/lib/tmr.c @@ -45,7 +45,7 @@ static hio_tmridx_t sift_up (hio_t* hio, hio_tmridx_t index) { hio_tmrjob_t item; - item = hio->tmr.jobs[index]; + item = hio->tmr.jobs[index]; do { @@ -100,7 +100,7 @@ static hio_tmridx_t sift_down (hio_t* hio, hio_tmridx_t index) index = younger; } while (index < base); - + hio->tmr.jobs[index] = item; if (hio->tmr.jobs[index].idxptr) *hio->tmr.jobs[index].idxptr = index; } diff --git a/lib/uch-case.h b/lib/uch-case.h index f39d1bd..e6b78dd 100644 --- a/lib/uch-case.h +++ b/lib/uch-case.h @@ -4,7 +4,7 @@ typedef struct uch_case_page_t uch_case_page_t; -struct uch_case_page_t +struct uch_case_page_t { hio_int32_t upper; hio_int32_t lower; diff --git a/lib/utf8.c b/lib/utf8.c index 8e97c2a..50d5339 100644 --- a/lib/utf8.c +++ b/lib/utf8.c @@ -56,7 +56,7 @@ struct __utf8_t typedef struct __utf8_t __utf8_t; -static __utf8_t utf8_table[] = +static __utf8_t utf8_table[] = { {0x00000000ul, 0x0000007Ful, 0x00, 0x80, 0x7F, 1}, {0x00000080ul, 0x000007FFul, 0xC0, 0xE0, 0x1F, 2}, @@ -80,7 +80,7 @@ static HIO_INLINE __utf8_t* get_utf8_slot (hio_uch_t uc) end = utf8_table + HIO_COUNTOF(utf8_table); cur = utf8_table; - while (cur < end) + while (cur < end) { if (uc >= cur->lower && uc <= cur->upper) return cur; cur++; @@ -98,7 +98,7 @@ hio_oow_t hio_uc_to_utf8 (hio_uch_t uc, hio_bch_t* utf8, hio_oow_t size) if (utf8 && cur->length <= size) { int index = cur->length; - while (index > 1) + while (index > 1) { /* * 0x3F: 00111111 @@ -128,16 +128,16 @@ hio_oow_t hio_utf8_to_uc (const hio_bch_t* utf8, hio_oow_t size, hio_uch_t* uc) end = utf8_table + HIO_COUNTOF(utf8_table); cur = utf8_table; - while (cur < end) + while (cur < end) { - if ((utf8[0] & cur->mask) == cur->fbyte) + if ((utf8[0] & cur->mask) == cur->fbyte) { - /* if size is less that cur->length, the incomplete-seqeunce + /* if size is less that cur->length, the incomplete-seqeunce * error is naturally indicated. so validate the string * only if size is as large as cur->length. */ - if (size >= cur->length) + if (size >= cur->length) { int i; @@ -149,12 +149,12 @@ hio_oow_t hio_utf8_to_uc (const hio_bch_t* utf8, hio_oow_t size, hio_uch_t* uc) for (i = 1; i < cur->length; i++) { /* in utf8, trailing bytes are all - * set with 0x80. + * set with 0x80. * * 10XXXXXX & 11000000 => 10000000 * * if not, invalid. */ - if ((utf8[i] & 0xC0) != 0x80) return 0; + if ((utf8[i] & 0xC0) != 0x80) return 0; w = (w << 6) | (utf8[i] & 0x3F); } *uc = w; @@ -164,19 +164,19 @@ hio_oow_t hio_utf8_to_uc (const hio_bch_t* utf8, hio_oow_t size, hio_uch_t* uc) for (i = 1; i < cur->length; i++) { /* in utf8, trailing bytes are all - * set with 0x80. + * set with 0x80. * * 10XXXXXX & 11000000 => 10000000 * * if not, invalid. */ - if ((utf8[i] & 0xC0) != 0x80) return 0; + if ((utf8[i] & 0xC0) != 0x80) return 0; } } } - /* this return value can indicate both - * the correct length (size >= cur->length) - * and + /* this return value can indicate both + * the correct length (size >= cur->length) + * and * the incomplete seqeunce error (size < cur->length). */ return (hio_oow_t)cur->length; diff --git a/lib/utl-mime.c b/lib/utl-mime.c index 6bf1348..21525e6 100644 --- a/lib/utl-mime.c +++ b/lib/utl-mime.c @@ -123,10 +123,10 @@ const hio_bch_t* hio_get_mime_type_by_ext(const hio_bch_t* ext) entry = &mime_type_tab[mid]; n = hio_comp_bcstr(ext, entry->ext, 1); - if (n < 0) + if (n < 0) { /* if left, right, mid were of hio_oow_t, - * you would need the following line. + * you would need the following line. if (mid == 0) break; */ right = mid - 1; diff --git a/lib/utl-siph.c b/lib/utl-siph.c index 1791155..2b4c930 100644 --- a/lib/utl-siph.c +++ b/lib/utl-siph.c @@ -24,7 +24,7 @@ #include -/* +/* * This code is based on https://github.com/emboss/siphash-c/blob/master/src/siphash.c * * See https://131002.net/siphash/siphash24.c for a reference implementation by the inventor. @@ -62,7 +62,7 @@ typedef hio_uint64_t sip_uint64_t; struct sip_uint64_t { hio_uint32_t _u32[2]; -}; +}; typedef struct sip_uint64_t sip_uint64_t; #if defined(HIO_ENDIAN_LITTLE) @@ -133,10 +133,10 @@ static HIO_INLINE sip_uint64_t* xor64_to (sip_uint64_t* v, sip_uint64_t s) #endif /* (HIO_SIZEOF_UINT64_T > 0) */ -static const hio_uint8_t sip_init_v_bin[] = +static const hio_uint8_t sip_init_v_bin[] = { #if defined(HIO_ENDIAN_LITTLE) - 0x75, 0x65, 0x73, 0x70, 0x65, 0x6d, 0x6f, 0x73, + 0x75, 0x65, 0x73, 0x70, 0x65, 0x6d, 0x6f, 0x73, 0x6d, 0x6f, 0x64, 0x6e, 0x61, 0x72, 0x6f, 0x64, 0x61, 0x72, 0x65, 0x6e, 0x65, 0x67, 0x79, 0x6c, 0x73, 0x65, 0x74, 0x79, 0x62, 0x64, 0x65, 0x74 @@ -195,7 +195,7 @@ void hio_sip_hash_24 (const hio_uint8_t key[16], const void* dptr, hio_oow_t dle v2 = k0; XOR64_TO(v2, sip_init_v[2]); v3 = k1; XOR64_TO(v3, sip_init_v[3]); - for (; ptr != end; ptr += 8) + for (; ptr != end; ptr += 8) { m = U8TO64_LE(ptr); SIP_2_ROUND (m, v0, v1, v2, v3); diff --git a/lib/utl-str.c b/lib/utl-str.c index e31a082..760df79 100644 --- a/lib/utl-str.c +++ b/lib/utl-str.c @@ -22,11 +22,11 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* +/* * Do NOT edit utl-str.c. Edit utl-str.c.m4 instead. - * + * * Generate utl-str.c with m4 - * $ m4 utl-str.c.m4 > utl-str.c + * $ m4 utl-str.c.m4 > utl-str.c */ #include "hio-prv.h" @@ -34,57 +34,57 @@ static int match_uch_class (const hio_uch_t* pp, hio_uch_t sc, int* matched) { - if (hio_comp_ucstr_bcstr_limited(pp, "[:upper:]", 9, 0) == 0) + if (hio_comp_ucstr_bcstr_limited(pp, "[:upper:]", 9, 0) == 0) { *matched = hio_is_uch_upper(sc); return 9; } - else if (hio_comp_ucstr_bcstr_limited(pp, "[:lower:]", 9, 0) == 0) + else if (hio_comp_ucstr_bcstr_limited(pp, "[:lower:]", 9, 0) == 0) { *matched = hio_is_uch_lower(sc); return 9; } - else if (hio_comp_ucstr_bcstr_limited(pp, "[:alpha:]", 9, 0) == 0) + else if (hio_comp_ucstr_bcstr_limited(pp, "[:alpha:]", 9, 0) == 0) { *matched = hio_is_uch_alpha(sc); return 9; } - else if (hio_comp_ucstr_bcstr_limited(pp, "[:digit:]", 9, 0) == 0) + else if (hio_comp_ucstr_bcstr_limited(pp, "[:digit:]", 9, 0) == 0) { *matched = hio_is_uch_digit(sc); return 9; } - else if (hio_comp_ucstr_bcstr_limited(pp, "[:xdigit:]", 10, 0) == 0) + else if (hio_comp_ucstr_bcstr_limited(pp, "[:xdigit:]", 10, 0) == 0) { *matched = hio_is_uch_xdigit(sc); return 10; } - else if (hio_comp_ucstr_bcstr_limited(pp, "[:alnum:]", 9, 0) == 0) + else if (hio_comp_ucstr_bcstr_limited(pp, "[:alnum:]", 9, 0) == 0) { *matched = hio_is_uch_alnum(sc); return 9; } - else if (hio_comp_ucstr_bcstr_limited(pp, "[:space:]", 9, 0) == 0) + else if (hio_comp_ucstr_bcstr_limited(pp, "[:space:]", 9, 0) == 0) { *matched = hio_is_uch_space(sc); return 9; } - else if (hio_comp_ucstr_bcstr_limited(pp, "[:print:]", 9, 0) == 0) + else if (hio_comp_ucstr_bcstr_limited(pp, "[:print:]", 9, 0) == 0) { *matched = hio_is_uch_print(sc); return 9; } - else if (hio_comp_ucstr_bcstr_limited(pp, "[:graph:]", 9, 0) == 0) + else if (hio_comp_ucstr_bcstr_limited(pp, "[:graph:]", 9, 0) == 0) { *matched = hio_is_uch_graph(sc); return 9; } - else if (hio_comp_ucstr_bcstr_limited(pp, "[:cntrl:]", 9, 0) == 0) + else if (hio_comp_ucstr_bcstr_limited(pp, "[:cntrl:]", 9, 0) == 0) { *matched = hio_is_uch_cntrl(sc); return 9; } - else if (hio_comp_ucstr_bcstr_limited(pp, "[:punct:]", 9, 0) == 0) + else if (hio_comp_ucstr_bcstr_limited(pp, "[:punct:]", 9, 0) == 0) { *matched = hio_is_uch_punct(sc); return 9; @@ -95,57 +95,57 @@ static int match_uch_class (const hio_uch_t* pp, hio_uch_t sc, int* matched) static int match_bch_class (const hio_bch_t* pp, hio_bch_t sc, int* matched) { - if (hio_comp_bcstr_limited(pp, "[:upper:]", 9, 0) == 0) + if (hio_comp_bcstr_limited(pp, "[:upper:]", 9, 0) == 0) { *matched = hio_is_bch_upper(sc); return 9; } - else if (hio_comp_bcstr_limited(pp, "[:lower:]", 9, 0) == 0) + else if (hio_comp_bcstr_limited(pp, "[:lower:]", 9, 0) == 0) { *matched = hio_is_bch_lower(sc); return 9; } - else if (hio_comp_bcstr_limited(pp, "[:alpha:]", 9, 0) == 0) + else if (hio_comp_bcstr_limited(pp, "[:alpha:]", 9, 0) == 0) { *matched = hio_is_bch_alpha(sc); return 9; } - else if (hio_comp_bcstr_limited(pp, "[:digit:]", 9, 0) == 0) + else if (hio_comp_bcstr_limited(pp, "[:digit:]", 9, 0) == 0) { *matched = hio_is_bch_digit(sc); return 9; } - else if (hio_comp_bcstr_limited(pp, "[:xdigit:]", 10, 0) == 0) + else if (hio_comp_bcstr_limited(pp, "[:xdigit:]", 10, 0) == 0) { *matched = hio_is_bch_xdigit(sc); return 10; } - else if (hio_comp_bcstr_limited(pp, "[:alnum:]", 9, 0) == 0) + else if (hio_comp_bcstr_limited(pp, "[:alnum:]", 9, 0) == 0) { *matched = hio_is_bch_alnum(sc); return 9; } - else if (hio_comp_bcstr_limited(pp, "[:space:]", 9, 0) == 0) + else if (hio_comp_bcstr_limited(pp, "[:space:]", 9, 0) == 0) { *matched = hio_is_bch_space(sc); return 9; } - else if (hio_comp_bcstr_limited(pp, "[:print:]", 9, 0) == 0) + else if (hio_comp_bcstr_limited(pp, "[:print:]", 9, 0) == 0) { *matched = hio_is_bch_print(sc); return 9; } - else if (hio_comp_bcstr_limited(pp, "[:graph:]", 9, 0) == 0) + else if (hio_comp_bcstr_limited(pp, "[:graph:]", 9, 0) == 0) { *matched = hio_is_bch_graph(sc); return 9; } - else if (hio_comp_bcstr_limited(pp, "[:cntrl:]", 9, 0) == 0) + else if (hio_comp_bcstr_limited(pp, "[:cntrl:]", 9, 0) == 0) { *matched = hio_is_bch_cntrl(sc); return 9; } - else if (hio_comp_bcstr_limited(pp, "[:punct:]", 9, 0) == 0) + else if (hio_comp_bcstr_limited(pp, "[:punct:]", 9, 0) == 0) { *matched = hio_is_bch_punct(sc); return 9; @@ -169,7 +169,7 @@ int hio_comp_uchars (const hio_uch_t* str1, hio_oow_t len1, const hio_uch_t* str while (str1 < end1) { c1 = hio_to_uch_lower(*str1); - if (str2 < end2) + if (str2 < end2) { c2 = hio_to_uch_lower(*str2); if (c1 > c2) return 1; @@ -184,7 +184,7 @@ int hio_comp_uchars (const hio_uch_t* str1, hio_oow_t len1, const hio_uch_t* str while (str1 < end1) { c1 = *str1; - if (str2 < end2) + if (str2 < end2) { c2 = *str2; if (c1 > c2) return 1; @@ -209,7 +209,7 @@ int hio_comp_bchars (const hio_bch_t* str1, hio_oow_t len1, const hio_bch_t* str while (str1 < end1) { c1 = hio_to_bch_lower(*str1); - if (str2 < end2) + if (str2 < end2) { c2 = hio_to_bch_lower(*str2); if (c1 > c2) return 1; @@ -224,7 +224,7 @@ int hio_comp_bchars (const hio_bch_t* str1, hio_oow_t len1, const hio_bch_t* str while (str1 < end1) { c1 = *str1; - if (str2 < end2) + if (str2 < end2) { c2 = *str2; if (c1 > c2) return 1; @@ -342,14 +342,14 @@ int hio_comp_uchars_ucstr (const hio_uch_t* str1, hio_oow_t len, const hio_uch_t { /* for "abc\0" of length 4 vs "abc", the fourth character * of the first string is equal to the terminating null of - * the second string. the first string is still considered + * the second string. the first string is still considered * bigger */ if (ignorecase) { const hio_uch_t* end = str1 + len; hio_uch_t c1; hio_uch_t c2; - while (str1 < end && *str2 != '\0') + while (str1 < end && *str2 != '\0') { c1 = hio_to_uch_lower(*str1); c2 = hio_to_uch_lower(*str2); @@ -361,7 +361,7 @@ int hio_comp_uchars_ucstr (const hio_uch_t* str1, hio_oow_t len, const hio_uch_t else { const hio_uch_t* end = str1 + len; - while (str1 < end && *str2 != '\0') + while (str1 < end && *str2 != '\0') { if (*str1 != *str2) return ((hio_uchu_t)*str1 > (hio_uchu_t)*str2)? 1: -1; str1++; str2++; @@ -374,14 +374,14 @@ int hio_comp_bchars_bcstr (const hio_bch_t* str1, hio_oow_t len, const hio_bch_t { /* for "abc\0" of length 4 vs "abc", the fourth character * of the first string is equal to the terminating null of - * the second string. the first string is still considered + * the second string. the first string is still considered * bigger */ if (ignorecase) { const hio_bch_t* end = str1 + len; hio_bch_t c1; hio_bch_t c2; - while (str1 < end && *str2 != '\0') + while (str1 < end && *str2 != '\0') { c1 = hio_to_bch_lower(*str1); c2 = hio_to_bch_lower(*str2); @@ -393,7 +393,7 @@ int hio_comp_bchars_bcstr (const hio_bch_t* str1, hio_oow_t len, const hio_bch_t else { const hio_bch_t* end = str1 + len; - while (str1 < end && *str2 != '\0') + while (str1 < end && *str2 != '\0') { if (*str1 != *str2) return ((hio_bchu_t)*str1 > (hio_bchu_t)*str2)? 1: -1; str1++; str2++; @@ -416,7 +416,7 @@ hio_oow_t hio_concat_uchars_to_ucstr (hio_uch_t* buf, hio_oow_t bsz, const hio_u end = str + len; - while (p < p2) + while (p < p2) { if (str >= end) break; *p++ = *str++; @@ -440,7 +440,7 @@ hio_oow_t hio_concat_bchars_to_bcstr (hio_bch_t* buf, hio_oow_t bsz, const hio_b end = str + len; - while (p < p2) + while (p < p2) { if (str >= end) break; *p++ = *str++; @@ -461,7 +461,7 @@ hio_oow_t hio_concat_ucstr (hio_uch_t* buf, hio_oow_t bsz, const hio_uch_t* str) p = buf + blen; p2 = buf + bsz - 1; - while (p < p2) + while (p < p2) { if (*str == '\0') break; *p++ = *str++; @@ -482,7 +482,7 @@ hio_oow_t hio_concat_bcstr (hio_bch_t* buf, hio_oow_t bsz, const hio_bch_t* str) p = buf + blen; p2 = buf + bsz - 1; - while (p < p2) + while (p < p2) { if (*str == '\0') break; *p++ = *str++; @@ -743,15 +743,15 @@ hio_oow_t hio_copy_fmt_ucses_to_ucstr (hio_uch_t* buf, hio_oow_t bsz, const hio_ hio_uch_t* b = buf; hio_uch_t* end = buf + bsz - 1; const hio_uch_t* f = fmt; - + if (bsz <= 0) return 0; - + while (*f != '\0') { if (*f == '\\') { /* get the escaped character and treat it normally. - * if the escaper is the last character, treat it + * if the escaper is the last character, treat it * normally also. */ if (f[1] != '\0') f++; } @@ -761,24 +761,24 @@ hio_oow_t hio_copy_fmt_ucses_to_ucstr (hio_uch_t* buf, hio_oow_t bsz, const hio_ { const hio_uch_t* tmp, * tmpend; hio_oow_t idx = 0; - + tmp = f; f += 2; - + do idx = idx * 10 + (*f++ - '0'); while (*f >= '0' && *f <= '9'); - + if (*f != '}') { f = tmp; goto normal; } - + f++; - + tmp = str[idx].ptr; tmpend = tmp + str[idx].len; - + while (tmp < tmpend) { if (b >= end) goto fini; @@ -788,12 +788,12 @@ hio_oow_t hio_copy_fmt_ucses_to_ucstr (hio_uch_t* buf, hio_oow_t bsz, const hio_ } else if (f[1] == '$') f++; } - + normal: if (b >= end) break; *b++ = *f++; } - + fini: *b = '\0'; return b - buf; @@ -804,15 +804,15 @@ hio_oow_t hio_copy_fmt_bcses_to_bcstr (hio_bch_t* buf, hio_oow_t bsz, const hio_ hio_bch_t* b = buf; hio_bch_t* end = buf + bsz - 1; const hio_bch_t* f = fmt; - + if (bsz <= 0) return 0; - + while (*f != '\0') { if (*f == '\\') { /* get the escaped character and treat it normally. - * if the escaper is the last character, treat it + * if the escaper is the last character, treat it * normally also. */ if (f[1] != '\0') f++; } @@ -822,24 +822,24 @@ hio_oow_t hio_copy_fmt_bcses_to_bcstr (hio_bch_t* buf, hio_oow_t bsz, const hio_ { const hio_bch_t* tmp, * tmpend; hio_oow_t idx = 0; - + tmp = f; f += 2; - + do idx = idx * 10 + (*f++ - '0'); while (*f >= '0' && *f <= '9'); - + if (*f != '}') { f = tmp; goto normal; } - + f++; - + tmp = str[idx].ptr; tmpend = tmp + str[idx].len; - + while (tmp < tmpend) { if (b >= end) goto fini; @@ -849,12 +849,12 @@ hio_oow_t hio_copy_fmt_bcses_to_bcstr (hio_bch_t* buf, hio_oow_t bsz, const hio_ } else if (f[1] == '$') f++; } - + normal: if (b >= end) break; *b++ = *f++; } - + fini: *b = '\0'; return b - buf; @@ -865,14 +865,14 @@ hio_oow_t hio_count_ucstr (const hio_uch_t* str) const hio_uch_t* ptr = str; while (*ptr != '\0') ptr++; return ptr - str; -} +} hio_oow_t hio_count_bcstr (const hio_bch_t* str) { const hio_bch_t* ptr = str; while (*ptr != '\0') ptr++; return ptr - str; -} +} hio_oow_t hio_count_ucstr_limited (const hio_uch_t* str, hio_oow_t maxlen) { @@ -1048,13 +1048,13 @@ hio_uch_t* hio_find_uchars_in_uchars (const hio_uch_t* str, hio_oow_t strsz, con if (subsz == 0) return (hio_uch_t*)str; if (strsz < subsz) return HIO_NULL; - + end = str + strsz - subsz; subp = sub + subsz; if (HIO_UNLIKELY(ignorecase)) { - while (str <= end) + while (str <= end) { const hio_uch_t* x = str; const hio_uch_t* y = sub; @@ -1071,7 +1071,7 @@ hio_uch_t* hio_find_uchars_in_uchars (const hio_uch_t* str, hio_oow_t strsz, con } else { - while (str <= end) + while (str <= end) { const hio_uch_t* x = str; const hio_uch_t* y = sub; @@ -1096,13 +1096,13 @@ hio_bch_t* hio_find_bchars_in_bchars (const hio_bch_t* str, hio_oow_t strsz, con if (subsz == 0) return (hio_bch_t*)str; if (strsz < subsz) return HIO_NULL; - + end = str + strsz - subsz; subp = sub + subsz; if (HIO_UNLIKELY(ignorecase)) { - while (str <= end) + while (str <= end) { const hio_bch_t* x = str; const hio_bch_t* y = sub; @@ -1119,7 +1119,7 @@ hio_bch_t* hio_find_bchars_in_bchars (const hio_bch_t* str, hio_oow_t strsz, con } else { - while (str <= end) + while (str <= end) { const hio_bch_t* x = str; const hio_bch_t* y = sub; @@ -1150,12 +1150,12 @@ hio_uch_t* hio_rfind_uchars_in_uchars (const hio_uch_t* str, hio_oow_t strsz, co if (HIO_UNLIKELY(ignorecase)) { - while (p >= str) + while (p >= str) { const hio_uch_t* x = p; const hio_uch_t* y = sub; - while (1) + while (1) { if (y >= subp) return (hio_uch_t*)p; if (hio_to_uch_lower(*x) != hio_to_uch_lower(*y)) break; @@ -1167,17 +1167,17 @@ hio_uch_t* hio_rfind_uchars_in_uchars (const hio_uch_t* str, hio_oow_t strsz, co } else { - while (p >= str) + while (p >= str) { const hio_uch_t* x = p; const hio_uch_t* y = sub; - while (1) + while (1) { if (y >= subp) return (hio_uch_t*)p; if (*x != *y) break; x++; y++; - } + } p--; } @@ -1198,12 +1198,12 @@ hio_bch_t* hio_rfind_bchars_in_bchars (const hio_bch_t* str, hio_oow_t strsz, co if (HIO_UNLIKELY(ignorecase)) { - while (p >= str) + while (p >= str) { const hio_bch_t* x = p; const hio_bch_t* y = sub; - while (1) + while (1) { if (y >= subp) return (hio_bch_t*)p; if (hio_to_bch_lower(*x) != hio_to_bch_lower(*y)) break; @@ -1215,17 +1215,17 @@ hio_bch_t* hio_rfind_bchars_in_bchars (const hio_bch_t* str, hio_oow_t strsz, co } else { - while (p >= str) + while (p >= str) { const hio_bch_t* x = p; const hio_bch_t* y = sub; - while (1) + while (1) { if (y >= subp) return (hio_bch_t*)p; if (*x != *y) break; x++; y++; - } + } p--; } @@ -1240,30 +1240,30 @@ hio_oow_t hio_compact_uchars (hio_uch_t* str, hio_oow_t len) int followed_by_space = 0; int state = 0; - while (p < end) + while (p < end) { - if (state == 0) + if (state == 0) { - if (!(*p)) + if (!(*p)) { *q++ = *p; state = 1; } } - else if (state == 1) + else if (state == 1) { - if ((*p)) + if ((*p)) { - if (!followed_by_space) + if (!followed_by_space) { followed_by_space = 1; *q++ = *p; } } - else + else { followed_by_space = 0; - *q++ = *p; + *q++ = *p; } } @@ -1279,30 +1279,30 @@ hio_oow_t hio_compact_bchars (hio_bch_t* str, hio_oow_t len) int followed_by_space = 0; int state = 0; - while (p < end) + while (p < end) { - if (state == 0) + if (state == 0) { - if (!(*p)) + if (!(*p)) { *q++ = *p; state = 1; } } - else if (state == 1) + else if (state == 1) { - if ((*p)) + if ((*p)) { - if (!followed_by_space) + if (!followed_by_space) { followed_by_space = 1; *q++ = *p; } } - else + else { followed_by_space = 0; - *q++ = *p; + *q++ = *p; } } @@ -1401,11 +1401,11 @@ hio_uch_t* hio_trim_uchars (const hio_uch_t* str, hio_oow_t* len, int flags) if (e) { - if (flags & HIO_TRIM_UCHARS_RIGHT) + if (flags & HIO_TRIM_UCHARS_RIGHT) { *len -= end - e - 1; } - if (flags & HIO_TRIM_UCHARS_LEFT) + if (flags & HIO_TRIM_UCHARS_LEFT) { *len -= s - str; str = s; @@ -1414,7 +1414,7 @@ hio_uch_t* hio_trim_uchars (const hio_uch_t* str, hio_oow_t* len, int flags) else { /* the entire string need to be deleted */ - if ((flags & HIO_TRIM_UCHARS_RIGHT) || + if ((flags & HIO_TRIM_UCHARS_RIGHT) || (flags & HIO_TRIM_UCHARS_LEFT)) *len = 0; } } @@ -1443,11 +1443,11 @@ hio_bch_t* hio_trim_bchars (const hio_bch_t* str, hio_oow_t* len, int flags) if (e) { - if (flags & HIO_TRIM_BCHARS_RIGHT) + if (flags & HIO_TRIM_BCHARS_RIGHT) { *len -= end - e - 1; } - if (flags & HIO_TRIM_BCHARS_LEFT) + if (flags & HIO_TRIM_BCHARS_LEFT) { *len -= s - str; str = s; @@ -1456,7 +1456,7 @@ hio_bch_t* hio_trim_bchars (const hio_bch_t* str, hio_oow_t* len, int flags) else { /* the entire string need to be deleted */ - if ((flags & HIO_TRIM_BCHARS_RIGHT) || + if ((flags & HIO_TRIM_BCHARS_RIGHT) || (flags & HIO_TRIM_BCHARS_LEFT)) *len = 0; } } @@ -1472,35 +1472,35 @@ int hio_split_ucstr (hio_uch_t* s, const hio_uch_t* delim, hio_uch_t lquote, hio int cnt = 0; if (delim == HIO_NULL) delim_mode = 0; - else + else { delim_mode = 1; for (d = (hio_uch_t*)delim; *d != '\0'; d++) if (!hio_is_uch_space(*d)) delim_mode = 2; } - if (delim_mode == 0) + if (delim_mode == 0) { /* skip preceding space characters */ while (hio_is_uch_space(*p)) p++; /* when 0 is given as "delim", it has an effect of cutting preceding and trailing space characters off "s". */ - if (lquote != '\0' && *p == lquote) + if (lquote != '\0' && *p == lquote) { hio_copy_ucstr_unlimited (p, p + 1); - for (;;) + for (;;) { if (*p == '\0') return -1; - if (escape != '\0' && *p == escape) + if (escape != '\0' && *p == escape) { hio_copy_ucstr_unlimited (p, p + 1); } - else + else { - if (*p == rquote) + if (*p == rquote) { p++; break; @@ -1515,18 +1515,18 @@ int hio_split_ucstr (hio_uch_t* s, const hio_uch_t* delim, hio_uch_t lquote, hio if (*p != '\0') return -1; if (sp == 0 && ep == 0) s[0] = '\0'; - else + else { ep[1] = '\0'; if (s != (hio_uch_t*)sp) hio_copy_ucstr_unlimited (s, sp); cnt++; } } - else + else { - while (*p) + while (*p) { - if (!hio_is_uch_space(*p)) + if (!hio_is_uch_space(*p)) { if (sp == 0) sp = p; ep = p; @@ -1535,7 +1535,7 @@ int hio_split_ucstr (hio_uch_t* s, const hio_uch_t* delim, hio_uch_t lquote, hio } if (sp == 0 && ep == 0) s[0] = '\0'; - else + else { ep[1] = '\0'; if (s != (hio_uch_t*)sp) hio_copy_ucstr_unlimited (s, sp); @@ -1543,31 +1543,31 @@ int hio_split_ucstr (hio_uch_t* s, const hio_uch_t* delim, hio_uch_t lquote, hio } } } - else if (delim_mode == 1) + else if (delim_mode == 1) { hio_uch_t* o; - while (*p) + while (*p) { o = p; while (hio_is_uch_space(*p)) p++; if (o != p) { hio_copy_ucstr_unlimited (o, p); p = o; } - if (lquote != '\0' && *p == lquote) + if (lquote != '\0' && *p == lquote) { hio_copy_ucstr_unlimited (p, p + 1); - for (;;) + for (;;) { if (*p == '\0') return -1; - if (escape != '\0' && *p == escape) + if (escape != '\0' && *p == escape) { hio_copy_ucstr_unlimited (p, p + 1); } - else + else { - if (*p == rquote) + if (*p == rquote) { *p++ = '\0'; cnt++; @@ -1577,17 +1577,17 @@ int hio_split_ucstr (hio_uch_t* s, const hio_uch_t* delim, hio_uch_t lquote, hio p++; } } - else + else { o = p; - for (;;) + for (;;) { - if (*p == '\0') + if (*p == '\0') { if (o != p) cnt++; break; } - if (hio_is_uch_space(*p)) + if (hio_is_uch_space(*p)) { *p++ = '\0'; cnt++; @@ -1603,27 +1603,27 @@ int hio_split_ucstr (hio_uch_t* s, const hio_uch_t* delim, hio_uch_t lquote, hio hio_uch_t* o; int ok; - while (*p != '\0') + while (*p != '\0') { o = p; while (hio_is_uch_space(*p)) p++; if (o != p) { hio_copy_ucstr_unlimited (o, p); p = o; } - if (lquote != '\0' && *p == lquote) + if (lquote != '\0' && *p == lquote) { hio_copy_ucstr_unlimited (p, p + 1); - for (;;) + for (;;) { if (*p == '\0') return -1; - if (escape != '\0' && *p == escape) + if (escape != '\0' && *p == escape) { hio_copy_ucstr_unlimited (p, p + 1); } - else + else { - if (*p == rquote) + if (*p == rquote) { *p++ = '\0'; cnt++; @@ -1636,9 +1636,9 @@ int hio_split_ucstr (hio_uch_t* s, const hio_uch_t* delim, hio_uch_t lquote, hio ok = 0; while (hio_is_uch_space(*p)) p++; if (*p == '\0') ok = 1; - for (d = (hio_uch_t*)delim; *d != '\0'; d++) + for (d = (hio_uch_t*)delim; *d != '\0'; d++) { - if (*p == *d) + if (*p == *d) { ok = 1; hio_copy_ucstr_unlimited (p, p + 1); @@ -1647,15 +1647,15 @@ int hio_split_ucstr (hio_uch_t* s, const hio_uch_t* delim, hio_uch_t lquote, hio } if (ok == 0) return -1; } - else + else { o = p; sp = ep = 0; - for (;;) + for (;;) { - if (*p == '\0') + if (*p == '\0') { - if (ep) + if (ep) { ep[1] = '\0'; p = &ep[1]; @@ -1663,16 +1663,16 @@ int hio_split_ucstr (hio_uch_t* s, const hio_uch_t* delim, hio_uch_t lquote, hio cnt++; break; } - for (d = (hio_uch_t*)delim; *d != '\0'; d++) + for (d = (hio_uch_t*)delim; *d != '\0'; d++) { - if (*p == *d) + if (*p == *d) { - if (sp == HIO_NULL) + if (sp == HIO_NULL) { hio_copy_ucstr_unlimited (o, p); p = o; *p++ = '\0'; } - else + else { hio_copy_ucstr_unlimited (&ep[1], p); hio_copy_ucstr_unlimited (o, sp); @@ -1686,7 +1686,7 @@ int hio_split_ucstr (hio_uch_t* s, const hio_uch_t* delim, hio_uch_t lquote, hio } } - if (!hio_is_uch_space(*p)) + if (!hio_is_uch_space(*p)) { if (sp == HIO_NULL) sp = p; ep = p; @@ -1710,35 +1710,35 @@ int hio_split_bcstr (hio_bch_t* s, const hio_bch_t* delim, hio_bch_t lquote, hio int cnt = 0; if (delim == HIO_NULL) delim_mode = 0; - else + else { delim_mode = 1; for (d = (hio_bch_t*)delim; *d != '\0'; d++) if (!hio_is_bch_space(*d)) delim_mode = 2; } - if (delim_mode == 0) + if (delim_mode == 0) { /* skip preceding space characters */ while (hio_is_bch_space(*p)) p++; /* when 0 is given as "delim", it has an effect of cutting preceding and trailing space characters off "s". */ - if (lquote != '\0' && *p == lquote) + if (lquote != '\0' && *p == lquote) { hio_copy_bcstr_unlimited (p, p + 1); - for (;;) + for (;;) { if (*p == '\0') return -1; - if (escape != '\0' && *p == escape) + if (escape != '\0' && *p == escape) { hio_copy_bcstr_unlimited (p, p + 1); } - else + else { - if (*p == rquote) + if (*p == rquote) { p++; break; @@ -1753,18 +1753,18 @@ int hio_split_bcstr (hio_bch_t* s, const hio_bch_t* delim, hio_bch_t lquote, hio if (*p != '\0') return -1; if (sp == 0 && ep == 0) s[0] = '\0'; - else + else { ep[1] = '\0'; if (s != (hio_bch_t*)sp) hio_copy_bcstr_unlimited (s, sp); cnt++; } } - else + else { - while (*p) + while (*p) { - if (!hio_is_bch_space(*p)) + if (!hio_is_bch_space(*p)) { if (sp == 0) sp = p; ep = p; @@ -1773,7 +1773,7 @@ int hio_split_bcstr (hio_bch_t* s, const hio_bch_t* delim, hio_bch_t lquote, hio } if (sp == 0 && ep == 0) s[0] = '\0'; - else + else { ep[1] = '\0'; if (s != (hio_bch_t*)sp) hio_copy_bcstr_unlimited (s, sp); @@ -1781,31 +1781,31 @@ int hio_split_bcstr (hio_bch_t* s, const hio_bch_t* delim, hio_bch_t lquote, hio } } } - else if (delim_mode == 1) + else if (delim_mode == 1) { hio_bch_t* o; - while (*p) + while (*p) { o = p; while (hio_is_bch_space(*p)) p++; if (o != p) { hio_copy_bcstr_unlimited (o, p); p = o; } - if (lquote != '\0' && *p == lquote) + if (lquote != '\0' && *p == lquote) { hio_copy_bcstr_unlimited (p, p + 1); - for (;;) + for (;;) { if (*p == '\0') return -1; - if (escape != '\0' && *p == escape) + if (escape != '\0' && *p == escape) { hio_copy_bcstr_unlimited (p, p + 1); } - else + else { - if (*p == rquote) + if (*p == rquote) { *p++ = '\0'; cnt++; @@ -1815,17 +1815,17 @@ int hio_split_bcstr (hio_bch_t* s, const hio_bch_t* delim, hio_bch_t lquote, hio p++; } } - else + else { o = p; - for (;;) + for (;;) { - if (*p == '\0') + if (*p == '\0') { if (o != p) cnt++; break; } - if (hio_is_bch_space(*p)) + if (hio_is_bch_space(*p)) { *p++ = '\0'; cnt++; @@ -1841,27 +1841,27 @@ int hio_split_bcstr (hio_bch_t* s, const hio_bch_t* delim, hio_bch_t lquote, hio hio_bch_t* o; int ok; - while (*p != '\0') + while (*p != '\0') { o = p; while (hio_is_bch_space(*p)) p++; if (o != p) { hio_copy_bcstr_unlimited (o, p); p = o; } - if (lquote != '\0' && *p == lquote) + if (lquote != '\0' && *p == lquote) { hio_copy_bcstr_unlimited (p, p + 1); - for (;;) + for (;;) { if (*p == '\0') return -1; - if (escape != '\0' && *p == escape) + if (escape != '\0' && *p == escape) { hio_copy_bcstr_unlimited (p, p + 1); } - else + else { - if (*p == rquote) + if (*p == rquote) { *p++ = '\0'; cnt++; @@ -1874,9 +1874,9 @@ int hio_split_bcstr (hio_bch_t* s, const hio_bch_t* delim, hio_bch_t lquote, hio ok = 0; while (hio_is_bch_space(*p)) p++; if (*p == '\0') ok = 1; - for (d = (hio_bch_t*)delim; *d != '\0'; d++) + for (d = (hio_bch_t*)delim; *d != '\0'; d++) { - if (*p == *d) + if (*p == *d) { ok = 1; hio_copy_bcstr_unlimited (p, p + 1); @@ -1885,15 +1885,15 @@ int hio_split_bcstr (hio_bch_t* s, const hio_bch_t* delim, hio_bch_t lquote, hio } if (ok == 0) return -1; } - else + else { o = p; sp = ep = 0; - for (;;) + for (;;) { - if (*p == '\0') + if (*p == '\0') { - if (ep) + if (ep) { ep[1] = '\0'; p = &ep[1]; @@ -1901,16 +1901,16 @@ int hio_split_bcstr (hio_bch_t* s, const hio_bch_t* delim, hio_bch_t lquote, hio cnt++; break; } - for (d = (hio_bch_t*)delim; *d != '\0'; d++) + for (d = (hio_bch_t*)delim; *d != '\0'; d++) { - if (*p == *d) + if (*p == *d) { - if (sp == HIO_NULL) + if (sp == HIO_NULL) { hio_copy_bcstr_unlimited (o, p); p = o; *p++ = '\0'; } - else + else { hio_copy_bcstr_unlimited (&ep[1], p); hio_copy_bcstr_unlimited (o, sp); @@ -1924,7 +1924,7 @@ int hio_split_bcstr (hio_bch_t* s, const hio_bch_t* delim, hio_bch_t lquote, hio } } - if (!hio_is_bch_space(*p)) + if (!hio_is_bch_space(*p)) { if (sp == HIO_NULL) sp = p; ep = p; @@ -1946,7 +1946,7 @@ hio_uch_t* hio_tokenize_uchars (const hio_uch_t* s, hio_oow_t len, const hio_uch const hio_uch_t* end = s + len; const hio_uch_t* sp = HIO_NULL, * ep = HIO_NULL; const hio_uch_t* delim_end = delim + delim_len; - hio_uch_t c; + hio_uch_t c; int delim_mode; #define __DELIM_NULL 0 @@ -1955,13 +1955,13 @@ hio_uch_t* hio_tokenize_uchars (const hio_uch_t* s, hio_oow_t len, const hio_uch #define __DELIM_NOSPACES 3 #define __DELIM_COMPOSITE 4 if (delim == HIO_NULL) delim_mode = __DELIM_NULL; - else + else { delim_mode = __DELIM_EMPTY; - for (d = delim; d < delim_end; d++) + for (d = delim; d < delim_end; d++) { - if (hio_is_uch_space(*d)) + if (hio_is_uch_space(*d)) { if (delim_mode == __DELIM_EMPTY) delim_mode = __DELIM_SPACES; @@ -1985,20 +1985,20 @@ hio_uch_t* hio_tokenize_uchars (const hio_uch_t* s, hio_oow_t len, const hio_uch /* TODO: verify the following statement... */ if (delim_mode == __DELIM_SPACES && delim_len == 1 && delim[0] != ' ') delim_mode = __DELIM_NOSPACES; - } - - if (delim_mode == __DELIM_NULL) - { - /* when HIO_NULL is given as "delim", it trims off the + } + + if (delim_mode == __DELIM_NULL) + { + /* when HIO_NULL is given as "delim", it trims off the * leading and trailing spaces characters off the source * string "s" eventually. */ while (p < end && hio_is_uch_space(*p)) p++; - while (p < end) + while (p < end) { c = *p; - if (!hio_is_uch_space(c)) + if (!hio_is_uch_space(c)) { if (sp == HIO_NULL) sp = p; ep = p; @@ -2016,13 +2016,13 @@ hio_uch_t* hio_tokenize_uchars (const hio_uch_t* s, hio_oow_t len, const hio_uch ep = p++; } } - else if (delim_mode == __DELIM_SPACES) + else if (delim_mode == __DELIM_SPACES) { /* each token is delimited by space characters. all leading * and trailing spaces are removed. */ while (p < end && hio_is_uch_space(*p)) p++; - while (p < end) + while (p < end) { c = *p; if (hio_is_uch_space(c)) break; @@ -2033,14 +2033,14 @@ hio_uch_t* hio_tokenize_uchars (const hio_uch_t* s, hio_oow_t len, const hio_uch } else if (delim_mode == __DELIM_NOSPACES) { - /* each token is delimited by one of charaters + /* each token is delimited by one of charaters * in the delimeter set "delim". */ if (ignorecase) { - while (p < end) + while (p < end) { c = hio_to_uch_lower(*p); - for (d = delim; d < delim_end; d++) + for (d = delim; d < delim_end; d++) { if (c == hio_to_uch_lower(*d)) goto exit_loop; } @@ -2051,10 +2051,10 @@ hio_uch_t* hio_tokenize_uchars (const hio_uch_t* s, hio_oow_t len, const hio_uch } else { - while (p < end) + while (p < end) { c = *p; - for (d = delim; d < delim_end; d++) + for (d = delim; d < delim_end; d++) { if (c == *d) goto exit_loop; } @@ -2064,7 +2064,7 @@ hio_uch_t* hio_tokenize_uchars (const hio_uch_t* s, hio_oow_t len, const hio_uch } } } - else /* if (delim_mode == __DELIM_COMPOSITE) */ + else /* if (delim_mode == __DELIM_COMPOSITE) */ { /* each token is delimited by one of non-space charaters * in the delimeter set "delim". however, all space characters @@ -2072,15 +2072,15 @@ hio_uch_t* hio_tokenize_uchars (const hio_uch_t* s, hio_oow_t len, const hio_uch while (p < end && hio_is_uch_space(*p)) p++; if (ignorecase) { - while (p < end) + while (p < end) { c = hio_to_uch_lower(*p); - if (hio_is_uch_space(c)) + if (hio_is_uch_space(c)) { p++; continue; } - for (d = delim; d < delim_end; d++) + for (d = delim; d < delim_end; d++) { if (c == hio_to_uch_lower(*d)) goto exit_loop; } @@ -2090,15 +2090,15 @@ hio_uch_t* hio_tokenize_uchars (const hio_uch_t* s, hio_oow_t len, const hio_uch } else { - while (p < end) + while (p < end) { c = *p; - if (hio_is_uch_space(c)) + if (hio_is_uch_space(c)) { p++; continue; } - for (d = delim; d < delim_end; d++) + for (d = delim; d < delim_end; d++) { if (c == *d) goto exit_loop; } @@ -2109,12 +2109,12 @@ hio_uch_t* hio_tokenize_uchars (const hio_uch_t* s, hio_oow_t len, const hio_uch } exit_loop: - if (sp == HIO_NULL) + if (sp == HIO_NULL) { tok->ptr = HIO_NULL; tok->len = (hio_oow_t)0; } - else + else { tok->ptr = (hio_uch_t*)sp; tok->len = ep - sp + 1; @@ -2132,7 +2132,7 @@ hio_bch_t* hio_tokenize_bchars (const hio_bch_t* s, hio_oow_t len, const hio_bch const hio_bch_t* end = s + len; const hio_bch_t* sp = HIO_NULL, * ep = HIO_NULL; const hio_bch_t* delim_end = delim + delim_len; - hio_bch_t c; + hio_bch_t c; int delim_mode; #define __DELIM_NULL 0 @@ -2141,13 +2141,13 @@ hio_bch_t* hio_tokenize_bchars (const hio_bch_t* s, hio_oow_t len, const hio_bch #define __DELIM_NOSPACES 3 #define __DELIM_COMPOSITE 4 if (delim == HIO_NULL) delim_mode = __DELIM_NULL; - else + else { delim_mode = __DELIM_EMPTY; - for (d = delim; d < delim_end; d++) + for (d = delim; d < delim_end; d++) { - if (hio_is_bch_space(*d)) + if (hio_is_bch_space(*d)) { if (delim_mode == __DELIM_EMPTY) delim_mode = __DELIM_SPACES; @@ -2171,20 +2171,20 @@ hio_bch_t* hio_tokenize_bchars (const hio_bch_t* s, hio_oow_t len, const hio_bch /* TODO: verify the following statement... */ if (delim_mode == __DELIM_SPACES && delim_len == 1 && delim[0] != ' ') delim_mode = __DELIM_NOSPACES; - } - - if (delim_mode == __DELIM_NULL) - { - /* when HIO_NULL is given as "delim", it trims off the + } + + if (delim_mode == __DELIM_NULL) + { + /* when HIO_NULL is given as "delim", it trims off the * leading and trailing spaces characters off the source * string "s" eventually. */ while (p < end && hio_is_bch_space(*p)) p++; - while (p < end) + while (p < end) { c = *p; - if (!hio_is_bch_space(c)) + if (!hio_is_bch_space(c)) { if (sp == HIO_NULL) sp = p; ep = p; @@ -2202,13 +2202,13 @@ hio_bch_t* hio_tokenize_bchars (const hio_bch_t* s, hio_oow_t len, const hio_bch ep = p++; } } - else if (delim_mode == __DELIM_SPACES) + else if (delim_mode == __DELIM_SPACES) { /* each token is delimited by space characters. all leading * and trailing spaces are removed. */ while (p < end && hio_is_bch_space(*p)) p++; - while (p < end) + while (p < end) { c = *p; if (hio_is_bch_space(c)) break; @@ -2219,14 +2219,14 @@ hio_bch_t* hio_tokenize_bchars (const hio_bch_t* s, hio_oow_t len, const hio_bch } else if (delim_mode == __DELIM_NOSPACES) { - /* each token is delimited by one of charaters + /* each token is delimited by one of charaters * in the delimeter set "delim". */ if (ignorecase) { - while (p < end) + while (p < end) { c = hio_to_bch_lower(*p); - for (d = delim; d < delim_end; d++) + for (d = delim; d < delim_end; d++) { if (c == hio_to_bch_lower(*d)) goto exit_loop; } @@ -2237,10 +2237,10 @@ hio_bch_t* hio_tokenize_bchars (const hio_bch_t* s, hio_oow_t len, const hio_bch } else { - while (p < end) + while (p < end) { c = *p; - for (d = delim; d < delim_end; d++) + for (d = delim; d < delim_end; d++) { if (c == *d) goto exit_loop; } @@ -2250,7 +2250,7 @@ hio_bch_t* hio_tokenize_bchars (const hio_bch_t* s, hio_oow_t len, const hio_bch } } } - else /* if (delim_mode == __DELIM_COMPOSITE) */ + else /* if (delim_mode == __DELIM_COMPOSITE) */ { /* each token is delimited by one of non-space charaters * in the delimeter set "delim". however, all space characters @@ -2258,15 +2258,15 @@ hio_bch_t* hio_tokenize_bchars (const hio_bch_t* s, hio_oow_t len, const hio_bch while (p < end && hio_is_bch_space(*p)) p++; if (ignorecase) { - while (p < end) + while (p < end) { c = hio_to_bch_lower(*p); - if (hio_is_bch_space(c)) + if (hio_is_bch_space(c)) { p++; continue; } - for (d = delim; d < delim_end; d++) + for (d = delim; d < delim_end; d++) { if (c == hio_to_bch_lower(*d)) goto exit_loop; } @@ -2276,15 +2276,15 @@ hio_bch_t* hio_tokenize_bchars (const hio_bch_t* s, hio_oow_t len, const hio_bch } else { - while (p < end) + while (p < end) { c = *p; - if (hio_is_bch_space(c)) + if (hio_is_bch_space(c)) { p++; continue; } - for (d = delim; d < delim_end; d++) + for (d = delim; d < delim_end; d++) { if (c == *d) goto exit_loop; } @@ -2295,12 +2295,12 @@ hio_bch_t* hio_tokenize_bchars (const hio_bch_t* s, hio_oow_t len, const hio_bch } exit_loop: - if (sp == HIO_NULL) + if (sp == HIO_NULL) { tok->ptr = HIO_NULL; tok->len = (hio_oow_t)0; } - else + else { tok->ptr = (hio_bch_t*)sp; tok->len = ep - sp + 1; @@ -2323,7 +2323,7 @@ hio_oow_t hio_byte_to_ucstr (hio_uint8_t byte, hio_uch_t* buf, hio_oow_t size, i radix_char = (flagged_radix & HIO_BYTE_TO_UCSTR_LOWERCASE)? 'a': 'A'; if (radix < 2 || radix > 36 || size <= 0) return 0; - do + do { hio_uint8_t digit = byte % radix; if (digit < 10) *p++ = digit + '0'; @@ -2332,9 +2332,9 @@ hio_oow_t hio_byte_to_ucstr (hio_uint8_t byte, hio_uch_t* buf, hio_oow_t size, i } while (byte > 0); - if (fill != '\0') + if (fill != '\0') { - while (size - 1 > p - tmp) + while (size - 1 > p - tmp) { *bp++ = fill; size--; @@ -2357,7 +2357,7 @@ hio_oow_t hio_byte_to_bcstr (hio_uint8_t byte, hio_bch_t* buf, hio_oow_t size, i radix_char = (flagged_radix & HIO_BYTE_TO_BCSTR_LOWERCASE)? 'a': 'A'; if (radix < 2 || radix > 36 || size <= 0) return 0; - do + do { hio_uint8_t digit = byte % radix; if (digit < 10) *p++ = digit + '0'; @@ -2366,9 +2366,9 @@ hio_oow_t hio_byte_to_bcstr (hio_uint8_t byte, hio_bch_t* buf, hio_oow_t size, i } while (byte > 0); - if (fill != '\0') + if (fill != '\0') { - while (size - 1 > p - tmp) + while (size - 1 > p - tmp) { *bp++ = fill; size--; @@ -2392,14 +2392,14 @@ hio_oow_t hio_intmax_to_ucstr (hio_intmax_t value, int radix, const hio_uch_t* p if (t == 0) { /* zero */ - if (buf == HIO_NULL) + if (buf == HIO_NULL) { - /* if buf is not given, + /* if buf is not given, * return the number of bytes required */ return prefix_len + 1; } - if (size < prefix_len+1) + if (size < prefix_len+1) { /* buffer too small */ return (hio_oow_t)-1; @@ -2429,7 +2429,7 @@ hio_oow_t hio_intmax_to_ucstr (hio_intmax_t value, int radix, const hio_uch_t* p t = value; if (t < 0) t = -t; - while (t > 0) + while (t > 0) { rem = t % radix; if (rem >= 10) @@ -2439,9 +2439,9 @@ hio_oow_t hio_intmax_to_ucstr (hio_intmax_t value, int radix, const hio_uch_t* p t /= radix; } - if (value < 0) + if (value < 0) { - for (i = 1; i <= prefix_len; i++) + for (i = 1; i <= prefix_len; i++) { buf[i] = prefix[i-1]; len--; @@ -2468,14 +2468,14 @@ hio_oow_t hio_intmax_to_bcstr (hio_intmax_t value, int radix, const hio_bch_t* p if (t == 0) { /* zero */ - if (buf == HIO_NULL) + if (buf == HIO_NULL) { - /* if buf is not given, + /* if buf is not given, * return the number of bytes required */ return prefix_len + 1; } - if (size < prefix_len+1) + if (size < prefix_len+1) { /* buffer too small */ return (hio_oow_t)-1; @@ -2505,7 +2505,7 @@ hio_oow_t hio_intmax_to_bcstr (hio_intmax_t value, int radix, const hio_bch_t* p t = value; if (t < 0) t = -t; - while (t > 0) + while (t > 0) { rem = t % radix; if (rem >= 10) @@ -2515,9 +2515,9 @@ hio_oow_t hio_intmax_to_bcstr (hio_intmax_t value, int radix, const hio_bch_t* p t /= radix; } - if (value < 0) + if (value < 0) { - for (i = 1; i <= prefix_len; i++) + for (i = 1; i <= prefix_len; i++) { buf[i] = prefix[i-1]; len--; @@ -2544,14 +2544,14 @@ hio_oow_t hio_uintmax_to_ucstr (hio_uintmax_t value, int radix, const hio_uch_t* if (t == 0) { /* zero */ - if (buf == HIO_NULL) + if (buf == HIO_NULL) { - /* if buf is not given, + /* if buf is not given, * return the number of bytes required */ return prefix_len + 1; } - if (size < prefix_len+1) + if (size < prefix_len+1) { /* buffer too small */ return (hio_oow_t)-1; @@ -2581,7 +2581,7 @@ hio_oow_t hio_uintmax_to_ucstr (hio_uintmax_t value, int radix, const hio_uch_t* t = value; if (t < 0) t = -t; - while (t > 0) + while (t > 0) { rem = t % radix; if (rem >= 10) @@ -2591,9 +2591,9 @@ hio_oow_t hio_uintmax_to_ucstr (hio_uintmax_t value, int radix, const hio_uch_t* t /= radix; } - if (value < 0) + if (value < 0) { - for (i = 1; i <= prefix_len; i++) + for (i = 1; i <= prefix_len; i++) { buf[i] = prefix[i-1]; len--; @@ -2620,14 +2620,14 @@ hio_oow_t hio_uintmax_to_bcstr (hio_uintmax_t value, int radix, const hio_bch_t* if (t == 0) { /* zero */ - if (buf == HIO_NULL) + if (buf == HIO_NULL) { - /* if buf is not given, + /* if buf is not given, * return the number of bytes required */ return prefix_len + 1; } - if (size < prefix_len+1) + if (size < prefix_len+1) { /* buffer too small */ return (hio_oow_t)-1; @@ -2657,7 +2657,7 @@ hio_oow_t hio_uintmax_to_bcstr (hio_uintmax_t value, int radix, const hio_bch_t* t = value; if (t < 0) t = -t; - while (t > 0) + while (t > 0) { rem = t % radix; if (rem >= 10) @@ -2667,9 +2667,9 @@ hio_oow_t hio_uintmax_to_bcstr (hio_uintmax_t value, int radix, const hio_bch_t* t /= radix; } - if (value < 0) + if (value < 0) { - for (i = 1; i <= prefix_len; i++) + for (i = 1; i <= prefix_len; i++) { buf[i] = prefix[i-1]; len--; @@ -2693,7 +2693,7 @@ hio_intmax_t hio_uchars_to_intmax (const hio_uch_t* str, hio_oow_t len, int opti int digit, negative = 0; int base = HIO_UCHARS_TO_INTMAX_GET_OPTION_BASE(option); - p = str; + p = str; end = str + len; if (HIO_UCHARS_TO_INTMAX_GET_OPTION_LTRIM(option)) @@ -2705,7 +2705,7 @@ hio_intmax_t hio_uchars_to_intmax (const hio_uch_t* str, hio_oow_t len, int opti /* check for a sign */ while (p < end) { - if (*p == '-') + if (*p == '-') { negative = ~negative; p++; @@ -2716,9 +2716,9 @@ hio_intmax_t hio_uchars_to_intmax (const hio_uch_t* str, hio_oow_t len, int opti /* check for a binary/octal/hexadecimal notation */ rem = end - p; - if (base == 0) + if (base == 0) { - if (rem >= 1 && *p == '0') + if (rem >= 1 && *p == '0') { p++; @@ -2726,7 +2726,7 @@ hio_intmax_t hio_uchars_to_intmax (const hio_uch_t* str, hio_oow_t len, int opti else if (*p == 'x' || *p == 'X') { p++; base = 16; - } + } else if (*p == 'b' || *p == 'B') { p++; base = 2; @@ -2734,14 +2734,14 @@ hio_intmax_t hio_uchars_to_intmax (const hio_uch_t* str, hio_oow_t len, int opti else base = 8; } else base = 10; - } + } else if (rem >= 2 && base == 16) { - if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ @@ -2785,7 +2785,7 @@ hio_intmax_t hio_uchars_to_intmax (const hio_uch_t* str, hio_oow_t len, int opti /* base 8: at least a zero digit has been seen. * other case: p > pp to be able to have at least 1 meaningful digit. */ - if (is_sober) *is_sober = (base == 8 || p > pp); + if (is_sober) *is_sober = (base == 8 || p > pp); if (HIO_UCHARS_TO_INTMAX_GET_OPTION_RTRIM(option)) { @@ -2806,7 +2806,7 @@ hio_intmax_t hio_bchars_to_intmax (const hio_bch_t* str, hio_oow_t len, int opti int digit, negative = 0; int base = HIO_BCHARS_TO_INTMAX_GET_OPTION_BASE(option); - p = str; + p = str; end = str + len; if (HIO_BCHARS_TO_INTMAX_GET_OPTION_LTRIM(option)) @@ -2818,7 +2818,7 @@ hio_intmax_t hio_bchars_to_intmax (const hio_bch_t* str, hio_oow_t len, int opti /* check for a sign */ while (p < end) { - if (*p == '-') + if (*p == '-') { negative = ~negative; p++; @@ -2829,9 +2829,9 @@ hio_intmax_t hio_bchars_to_intmax (const hio_bch_t* str, hio_oow_t len, int opti /* check for a binary/octal/hexadecimal notation */ rem = end - p; - if (base == 0) + if (base == 0) { - if (rem >= 1 && *p == '0') + if (rem >= 1 && *p == '0') { p++; @@ -2839,7 +2839,7 @@ hio_intmax_t hio_bchars_to_intmax (const hio_bch_t* str, hio_oow_t len, int opti else if (*p == 'x' || *p == 'X') { p++; base = 16; - } + } else if (*p == 'b' || *p == 'B') { p++; base = 2; @@ -2847,14 +2847,14 @@ hio_intmax_t hio_bchars_to_intmax (const hio_bch_t* str, hio_oow_t len, int opti else base = 8; } else base = 10; - } + } else if (rem >= 2 && base == 16) { - if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ @@ -2898,7 +2898,7 @@ hio_intmax_t hio_bchars_to_intmax (const hio_bch_t* str, hio_oow_t len, int opti /* base 8: at least a zero digit has been seen. * other case: p > pp to be able to have at least 1 meaningful digit. */ - if (is_sober) *is_sober = (base == 8 || p > pp); + if (is_sober) *is_sober = (base == 8 || p > pp); if (HIO_BCHARS_TO_INTMAX_GET_OPTION_RTRIM(option)) { @@ -2919,7 +2919,7 @@ hio_uintmax_t hio_uchars_to_uintmax (const hio_uch_t* str, hio_oow_t len, int op int digit; int base = HIO_UCHARS_TO_UINTMAX_GET_OPTION_BASE(option); - p = str; + p = str; end = str + len; if (HIO_UCHARS_TO_UINTMAX_GET_OPTION_LTRIM(option)) @@ -2937,9 +2937,9 @@ hio_uintmax_t hio_uchars_to_uintmax (const hio_uch_t* str, hio_oow_t len, int op /* check for a binary/octal/hexadecimal notation */ rem = end - p; - if (base == 0) + if (base == 0) { - if (rem >= 1 && *p == '0') + if (rem >= 1 && *p == '0') { p++; @@ -2947,7 +2947,7 @@ hio_uintmax_t hio_uchars_to_uintmax (const hio_uch_t* str, hio_oow_t len, int op else if (*p == 'x' || *p == 'X') { p++; base = 16; - } + } else if (*p == 'b' || *p == 'B') { p++; base = 2; @@ -2955,14 +2955,14 @@ hio_uintmax_t hio_uchars_to_uintmax (const hio_uch_t* str, hio_oow_t len, int op else base = 8; } else base = 10; - } + } else if (rem >= 2 && base == 16) { - if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ @@ -3006,7 +3006,7 @@ hio_uintmax_t hio_uchars_to_uintmax (const hio_uch_t* str, hio_oow_t len, int op /* base 8: at least a zero digit has been seen. * other case: p > pp to be able to have at least 1 meaningful digit. */ - if (is_sober) *is_sober = (base == 8 || p > pp); + if (is_sober) *is_sober = (base == 8 || p > pp); if (HIO_UCHARS_TO_UINTMAX_GET_OPTION_RTRIM(option)) { @@ -3027,7 +3027,7 @@ hio_uintmax_t hio_bchars_to_uintmax (const hio_bch_t* str, hio_oow_t len, int op int digit; int base = HIO_BCHARS_TO_UINTMAX_GET_OPTION_BASE(option); - p = str; + p = str; end = str + len; if (HIO_BCHARS_TO_UINTMAX_GET_OPTION_LTRIM(option)) @@ -3045,9 +3045,9 @@ hio_uintmax_t hio_bchars_to_uintmax (const hio_bch_t* str, hio_oow_t len, int op /* check for a binary/octal/hexadecimal notation */ rem = end - p; - if (base == 0) + if (base == 0) { - if (rem >= 1 && *p == '0') + if (rem >= 1 && *p == '0') { p++; @@ -3055,7 +3055,7 @@ hio_uintmax_t hio_bchars_to_uintmax (const hio_bch_t* str, hio_oow_t len, int op else if (*p == 'x' || *p == 'X') { p++; base = 16; - } + } else if (*p == 'b' || *p == 'B') { p++; base = 2; @@ -3063,14 +3063,14 @@ hio_uintmax_t hio_bchars_to_uintmax (const hio_bch_t* str, hio_oow_t len, int op else base = 8; } else base = 10; - } + } else if (rem >= 2 && base == 16) { - if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; + if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) p += 2; } else if (rem >= 2 && base == 2) { - if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; + if (*p == '0' && (*(p + 1) == 'b' || *(p + 1) == 'B')) p += 2; } /* process the digits */ @@ -3114,7 +3114,7 @@ hio_uintmax_t hio_bchars_to_uintmax (const hio_bch_t* str, hio_oow_t len, int op /* base 8: at least a zero digit has been seen. * other case: p > pp to be able to have at least 1 meaningful digit. */ - if (is_sober) *is_sober = (base == 8 || p > pp); + if (is_sober) *is_sober = (base == 8 || p > pp); if (HIO_BCHARS_TO_UINTMAX_GET_OPTION_RTRIM(option)) { @@ -3134,16 +3134,16 @@ int hio_fnmat_uchars_i (const hio_uch_t* str, hio_oow_t slen, const hio_uch_t* p const hio_uch_t* pe = ptn + plen; hio_uch_t sc, pc, pc2; - while (1) + while (1) { - if (pp < pe && HIO_FNMAT_IS_ESC(*pp) && !(flags & HIO_FNMAT_NOESCAPE)) + if (pp < pe && HIO_FNMAT_IS_ESC(*pp) && !(flags & HIO_FNMAT_NOESCAPE)) { /* pattern is escaped and escaping is allowed. */ - if ((++pp) >= pe) + if ((++pp) >= pe) { - /* - * the last character of the pattern is an WCS_ESC. + /* + * the last character of the pattern is an WCS_ESC. * matching is performed as if the end of the pattern is * reached just without an WCS_ESC. */ @@ -3154,27 +3154,27 @@ int hio_fnmat_uchars_i (const hio_uch_t* str, hio_oow_t slen, const hio_uch_t* p if (sp >= se) return 0; /* premature string termination */ sc = *sp; pc = *pp; /* pc is just a normal character */ - if ((flags & HIO_FNMAT_IGNORECASE) != 0) + if ((flags & HIO_FNMAT_IGNORECASE) != 0) { /* make characters to lower-case */ sc = hio_to_uch_lower(sc); - pc = hio_to_uch_lower(pc); + pc = hio_to_uch_lower(pc); } if (sc != pc) return 0; - sp++; pp++; + sp++; pp++; continue; } - if (pp >= pe) + if (pp >= pe) { - /* - * the end of the pattern has been reached. + /* + * the end of the pattern has been reached. * the string must terminate too. */ return sp >= se; } - if (sp >= se) + if (sp >= se) { /* the string terminats prematurely */ while (pp < pe && *pp == '*') pp++; @@ -3183,23 +3183,23 @@ int hio_fnmat_uchars_i (const hio_uch_t* str, hio_oow_t slen, const hio_uch_t* p sc = *sp; pc = *pp; - if (sc == '.' && (flags & HIO_FNMAT_PERIOD)) + if (sc == '.' && (flags & HIO_FNMAT_PERIOD)) { - /* - * a leading period in the staring must match - * a period in the pattern explicitly + /* + * a leading period in the staring must match + * a period in the pattern explicitly */ - if ((!no_first_period && sp == str) || - (HIO_FNMAT_IS_SEP(sp[-1]) && (flags & HIO_FNMAT_PATHNAME))) + if ((!no_first_period && sp == str) || + (HIO_FNMAT_IS_SEP(sp[-1]) && (flags & HIO_FNMAT_PATHNAME))) { if (pc != '.') return 0; sp++; pp++; continue; } } - else if (HIO_FNMAT_IS_SEP(sc) && (flags & HIO_FNMAT_PATHNAME)) + else if (HIO_FNMAT_IS_SEP(sc) && (flags & HIO_FNMAT_PATHNAME)) { - while (pc == '*') + while (pc == '*') { if ((++pp) >= pe) return 0; pc = *pp; @@ -3212,21 +3212,21 @@ int hio_fnmat_uchars_i (const hio_uch_t* str, hio_oow_t slen, const hio_uch_t* p } /* the handling of special pattern characters begins here */ - if (pc == '?') + if (pc == '?') { /* match any single character */ - sp++; pp++; - } - else if (pc == '*') - { + sp++; pp++; + } + else if (pc == '*') + { /* match zero or more characters */ /* compact asterisks */ do { pp++; } while (pp < pe && *pp == '*'); - if (pp >= pe) + if (pp >= pe) { - /* + /* * if the last character in the pattern is an asterisk, * the string should not have any directory separators * when HIO_FNMAT_PATHNAME is set. @@ -3241,31 +3241,31 @@ int hio_fnmat_uchars_i (const hio_uch_t* str, hio_oow_t slen, const hio_uch_t* p } return 1; } - else + else { - do + do { if (hio_fnmat_uchars_i(sp, se - sp, pp, pe - pp, flags, 1)) return 1; if (HIO_FNMAT_IS_SEP(*sp) && (flags & HIO_FNMAT_PATHNAME)) break; sp++; - } + } while (sp < se); return 0; } } - else if (pc == '[') + else if (pc == '[') { /* match range */ int negate = 0; int matched = 0; if ((++pp) >= pe) return 0; - if (*pp == '!') { negate = 1; pp++; } + if (*pp == '!') { negate = 1; pp++; } - while (pp < pe && *pp != ']') + while (pp < pe && *pp != ']') { - if (*pp == '[') + if (*pp == '[') { hio_oow_t pl = pe - pp; @@ -3279,9 +3279,9 @@ int hio_fnmat_uchars_i (const hio_uch_t* str, hio_oow_t slen, const hio_uch_t* p } } - /* - * characters in an invalid class name are - * just treated as normal characters + /* + * characters in an invalid class name are + * just treated as normal characters */ } @@ -3291,44 +3291,44 @@ int hio_fnmat_uchars_i (const hio_uch_t* str, hio_oow_t slen, const hio_uch_t* p if (pp >= pe) break; pc = *pp; - if ((flags & HIO_FNMAT_IGNORECASE) != 0) + if ((flags & HIO_FNMAT_IGNORECASE) != 0) { - sc = hio_to_uch_lower(sc); - pc = hio_to_uch_lower(pc); + sc = hio_to_uch_lower(sc); + pc = hio_to_uch_lower(pc); } - if (pp + 1 < pe && pp[1] == '-') + if (pp + 1 < pe && pp[1] == '-') { pp += 2; /* move the a character next to a dash */ - if (pp >= pe) + if (pp >= pe) { if (sc >= pc) matched = 1; break; } - if (HIO_FNMAT_IS_ESC(*pp) && !(flags & HIO_FNMAT_NOESCAPE)) + if (HIO_FNMAT_IS_ESC(*pp) && !(flags & HIO_FNMAT_NOESCAPE)) { - if ((++pp) >= pe) + if ((++pp) >= pe) { if (sc >= pc) matched = 1; break; } } - else if (*pp == ']') + else if (*pp == ']') { if (sc >= pc) matched = 1; break; } pc2 = *pp; - if ((flags & HIO_FNMAT_IGNORECASE) != 0) - pc2 = hio_to_uch_lower(pc2); + if ((flags & HIO_FNMAT_IGNORECASE) != 0) + pc2 = hio_to_uch_lower(pc2); if (sc >= pc && sc <= pc2) matched = 1; pp++; } - else + else { if (sc == pc) matched = 1; pp++; @@ -3339,13 +3339,13 @@ int hio_fnmat_uchars_i (const hio_uch_t* str, hio_oow_t slen, const hio_uch_t* p if (!matched) return 0; sp++; if (pp < pe) pp++; } - else + else { /* a normal character */ - if ((flags & HIO_FNMAT_IGNORECASE) != 0) + if ((flags & HIO_FNMAT_IGNORECASE) != 0) { - sc = hio_to_uch_lower(sc); - pc = hio_to_uch_lower(pc); + sc = hio_to_uch_lower(sc); + pc = hio_to_uch_lower(pc); } if (sc != pc) return 0; @@ -3365,16 +3365,16 @@ int hio_fnmat_bchars_i (const hio_bch_t* str, hio_oow_t slen, const hio_bch_t* p const hio_bch_t* pe = ptn + plen; hio_bch_t sc, pc, pc2; - while (1) + while (1) { - if (pp < pe && HIO_FNMAT_IS_ESC(*pp) && !(flags & HIO_FNMAT_NOESCAPE)) + if (pp < pe && HIO_FNMAT_IS_ESC(*pp) && !(flags & HIO_FNMAT_NOESCAPE)) { /* pattern is escaped and escaping is allowed. */ - if ((++pp) >= pe) + if ((++pp) >= pe) { - /* - * the last character of the pattern is an WCS_ESC. + /* + * the last character of the pattern is an WCS_ESC. * matching is performed as if the end of the pattern is * reached just without an WCS_ESC. */ @@ -3385,27 +3385,27 @@ int hio_fnmat_bchars_i (const hio_bch_t* str, hio_oow_t slen, const hio_bch_t* p if (sp >= se) return 0; /* premature string termination */ sc = *sp; pc = *pp; /* pc is just a normal character */ - if ((flags & HIO_FNMAT_IGNORECASE) != 0) + if ((flags & HIO_FNMAT_IGNORECASE) != 0) { /* make characters to lower-case */ sc = hio_to_bch_lower(sc); - pc = hio_to_bch_lower(pc); + pc = hio_to_bch_lower(pc); } if (sc != pc) return 0; - sp++; pp++; + sp++; pp++; continue; } - if (pp >= pe) + if (pp >= pe) { - /* - * the end of the pattern has been reached. + /* + * the end of the pattern has been reached. * the string must terminate too. */ return sp >= se; } - if (sp >= se) + if (sp >= se) { /* the string terminats prematurely */ while (pp < pe && *pp == '*') pp++; @@ -3414,23 +3414,23 @@ int hio_fnmat_bchars_i (const hio_bch_t* str, hio_oow_t slen, const hio_bch_t* p sc = *sp; pc = *pp; - if (sc == '.' && (flags & HIO_FNMAT_PERIOD)) + if (sc == '.' && (flags & HIO_FNMAT_PERIOD)) { - /* - * a leading period in the staring must match - * a period in the pattern explicitly + /* + * a leading period in the staring must match + * a period in the pattern explicitly */ - if ((!no_first_period && sp == str) || - (HIO_FNMAT_IS_SEP(sp[-1]) && (flags & HIO_FNMAT_PATHNAME))) + if ((!no_first_period && sp == str) || + (HIO_FNMAT_IS_SEP(sp[-1]) && (flags & HIO_FNMAT_PATHNAME))) { if (pc != '.') return 0; sp++; pp++; continue; } } - else if (HIO_FNMAT_IS_SEP(sc) && (flags & HIO_FNMAT_PATHNAME)) + else if (HIO_FNMAT_IS_SEP(sc) && (flags & HIO_FNMAT_PATHNAME)) { - while (pc == '*') + while (pc == '*') { if ((++pp) >= pe) return 0; pc = *pp; @@ -3443,21 +3443,21 @@ int hio_fnmat_bchars_i (const hio_bch_t* str, hio_oow_t slen, const hio_bch_t* p } /* the handling of special pattern characters begins here */ - if (pc == '?') + if (pc == '?') { /* match any single character */ - sp++; pp++; - } - else if (pc == '*') - { + sp++; pp++; + } + else if (pc == '*') + { /* match zero or more characters */ /* compact asterisks */ do { pp++; } while (pp < pe && *pp == '*'); - if (pp >= pe) + if (pp >= pe) { - /* + /* * if the last character in the pattern is an asterisk, * the string should not have any directory separators * when HIO_FNMAT_PATHNAME is set. @@ -3472,31 +3472,31 @@ int hio_fnmat_bchars_i (const hio_bch_t* str, hio_oow_t slen, const hio_bch_t* p } return 1; } - else + else { - do + do { if (hio_fnmat_bchars_i(sp, se - sp, pp, pe - pp, flags, 1)) return 1; if (HIO_FNMAT_IS_SEP(*sp) && (flags & HIO_FNMAT_PATHNAME)) break; sp++; - } + } while (sp < se); return 0; } } - else if (pc == '[') + else if (pc == '[') { /* match range */ int negate = 0; int matched = 0; if ((++pp) >= pe) return 0; - if (*pp == '!') { negate = 1; pp++; } + if (*pp == '!') { negate = 1; pp++; } - while (pp < pe && *pp != ']') + while (pp < pe && *pp != ']') { - if (*pp == '[') + if (*pp == '[') { hio_oow_t pl = pe - pp; @@ -3510,9 +3510,9 @@ int hio_fnmat_bchars_i (const hio_bch_t* str, hio_oow_t slen, const hio_bch_t* p } } - /* - * characters in an invalid class name are - * just treated as normal characters + /* + * characters in an invalid class name are + * just treated as normal characters */ } @@ -3522,44 +3522,44 @@ int hio_fnmat_bchars_i (const hio_bch_t* str, hio_oow_t slen, const hio_bch_t* p if (pp >= pe) break; pc = *pp; - if ((flags & HIO_FNMAT_IGNORECASE) != 0) + if ((flags & HIO_FNMAT_IGNORECASE) != 0) { - sc = hio_to_bch_lower(sc); - pc = hio_to_bch_lower(pc); + sc = hio_to_bch_lower(sc); + pc = hio_to_bch_lower(pc); } - if (pp + 1 < pe && pp[1] == '-') + if (pp + 1 < pe && pp[1] == '-') { pp += 2; /* move the a character next to a dash */ - if (pp >= pe) + if (pp >= pe) { if (sc >= pc) matched = 1; break; } - if (HIO_FNMAT_IS_ESC(*pp) && !(flags & HIO_FNMAT_NOESCAPE)) + if (HIO_FNMAT_IS_ESC(*pp) && !(flags & HIO_FNMAT_NOESCAPE)) { - if ((++pp) >= pe) + if ((++pp) >= pe) { if (sc >= pc) matched = 1; break; } } - else if (*pp == ']') + else if (*pp == ']') { if (sc >= pc) matched = 1; break; } pc2 = *pp; - if ((flags & HIO_FNMAT_IGNORECASE) != 0) - pc2 = hio_to_bch_lower(pc2); + if ((flags & HIO_FNMAT_IGNORECASE) != 0) + pc2 = hio_to_bch_lower(pc2); if (sc >= pc && sc <= pc2) matched = 1; pp++; } - else + else { if (sc == pc) matched = 1; pp++; @@ -3570,13 +3570,13 @@ int hio_fnmat_bchars_i (const hio_bch_t* str, hio_oow_t slen, const hio_bch_t* p if (!matched) return 0; sp++; if (pp < pe) pp++; } - else + else { /* a normal character */ - if ((flags & HIO_FNMAT_IGNORECASE) != 0) + if ((flags & HIO_FNMAT_IGNORECASE) != 0) { - sc = hio_to_bch_lower(sc); - pc = hio_to_bch_lower(pc); + sc = hio_to_bch_lower(sc); + pc = hio_to_bch_lower(pc); } if (sc != pc) return 0; diff --git a/lib/utl.c b/lib/utl.c index 73189d2..1e28af7 100644 --- a/lib/utl.c +++ b/lib/utl.c @@ -85,7 +85,7 @@ int hio_comp_uchars_bcstr (const hio_uch_t* str1, hio_oow_t len, const hio_bch_t const hio_uch_t* end = str1 + len; hio_uch_t c1; hio_bch_t c2; - while (str1 < end && *str2 != '\0') + while (str1 < end && *str2 != '\0') { c1 = hio_to_uch_lower(*str1); c2 = hio_to_bch_lower(*str2); @@ -97,7 +97,7 @@ int hio_comp_uchars_bcstr (const hio_uch_t* str1, hio_oow_t len, const hio_bch_t else { const hio_uch_t* end = str1 + len; - while (str1 < end && *str2 != '\0') + while (str1 < end && *str2 != '\0') { if (*str1 != *str2) return ((hio_uchu_t)*str1 > (hio_bchu_t)*str2)? 1: -1; str1++; str2++; @@ -113,7 +113,7 @@ int hio_comp_bchars_ucstr (const hio_bch_t* str1, hio_oow_t len, const hio_uch_t const hio_bch_t* end = str1 + len; hio_bch_t c1; hio_uch_t c2; - while (str1 < end && *str2 != '\0') + while (str1 < end && *str2 != '\0') { c1 = hio_to_bch_lower(*str1); c2 = hio_to_uch_lower(*str2); @@ -125,7 +125,7 @@ int hio_comp_bchars_ucstr (const hio_bch_t* str1, hio_oow_t len, const hio_uch_t else { const hio_bch_t* end = str1 + len; - while (str1 < end && *str2 != '\0') + while (str1 < end && *str2 != '\0') { if (*str1 != *str2) return ((hio_bchu_t)*str1 > (hio_uchu_t)*str2)? 1: -1; str1++; str2++; @@ -217,7 +217,7 @@ HIO_INLINE int hio_conv_bchars_to_uchars_with_cmgr ( if (ucs) { - /* destination buffer is specified. + /* destination buffer is specified. * copy the conversion result to the buffer */ hio_uch_t* q, * qend; @@ -281,7 +281,7 @@ HIO_INLINE int hio_conv_bchars_to_uchars_with_cmgr ( /* no destination buffer is specified. perform conversion * but don't copy the result. the caller can call this function * without a buffer to find the required buffer size, allocate - * a buffer with the size and call this function again with + * a buffer with the size and call this function again with * the buffer. */ hio_uch_t w; @@ -357,13 +357,13 @@ HIO_INLINE int hio_conv_uchars_to_bchars_with_cmgr ( { const hio_uch_t* p = ucs; const hio_uch_t* end = ucs + *ucslen; - int ret = 0; + int ret = 0; if (bcs) { hio_oow_t rem = *bcslen; - while (p < end) + while (p < end) { hio_oow_t n; @@ -374,12 +374,12 @@ HIO_INLINE int hio_conv_uchars_to_bchars_with_cmgr ( } n = cmgr->uctobc(*p, bcs, rem); - if (n == 0) + if (n == 0) { ret = -1; break; /* illegal character */ } - if (n > rem) + if (n > rem) { ret = -2; /* buffer too small */ break; @@ -387,7 +387,7 @@ HIO_INLINE int hio_conv_uchars_to_bchars_with_cmgr ( bcs += n; rem -= n; p++; } - *bcslen -= rem; + *bcslen -= rem; } else { @@ -399,7 +399,7 @@ HIO_INLINE int hio_conv_uchars_to_bchars_with_cmgr ( hio_oow_t n; n = cmgr->uctobc(*p, bcsbuf, HIO_COUNTOF(bcsbuf)); - if (n == 0) + if (n == 0) { ret = -1; break; /* illegal character */ @@ -411,7 +411,7 @@ HIO_INLINE int hio_conv_uchars_to_bchars_with_cmgr ( p++; mlen += n; } - /* this length excludes the terminating null character. + /* this length excludes the terminating null character. * this function doesn't even null-terminate the result. */ *bcslen = mlen; } @@ -440,14 +440,14 @@ HIO_INLINE int hio_conv_ucstr_to_bcstr_with_cmgr ( ret = -2; break; } - + n = cmgr->uctobc(*p, bcs, rem); - if (n == 0) + if (n == 0) { ret = -1; break; /* illegal character */ } - if (n > rem) + if (n > rem) { ret = -2; break; /* buffer too small */ @@ -458,13 +458,13 @@ HIO_INLINE int hio_conv_ucstr_to_bcstr_with_cmgr ( /* update bcslen to the length of the bcs string converted excluding * terminating null */ - *bcslen -= rem; + *bcslen -= rem; /* null-terminate the multibyte sequence if it has sufficient space */ if (rem > 0) *bcs = '\0'; - else + else { - /* if ret is -2 and cs[cslen] == '\0', + /* if ret is -2 and cs[cslen] == '\0', * this means that the bcs buffer was lacking one * slot for the terminating null */ ret = -2; /* buffer too small */ @@ -480,7 +480,7 @@ HIO_INLINE int hio_conv_ucstr_to_bcstr_with_cmgr ( hio_oow_t n; n = cmgr->uctobc(*p, bcsbuf, HIO_COUNTOF(bcsbuf)); - if (n == 0) + if (n == 0) { ret = -1; break; /* illegal character */ @@ -492,7 +492,7 @@ HIO_INLINE int hio_conv_ucstr_to_bcstr_with_cmgr ( p++; mlen += n; } - /* this length holds the number of resulting multi-byte characters + /* this length holds the number of resulting multi-byte characters * excluding the terminating null character */ *bcslen = mlen; } @@ -609,7 +609,7 @@ HIO_INLINE hio_uch_t* hio_dupbtoucharswithheadroom (hio_t* hio, hio_oow_t headro hio_uch_t* ptr; inlen = bcslen; - if (hio_convbtouchars(hio, bcs, &inlen, HIO_NULL, &outlen, all) <= -1) + if (hio_convbtouchars(hio, bcs, &inlen, HIO_NULL, &outlen, all) <= -1) { /* note it's also an error if no full conversion is made in this function */ return HIO_NULL; @@ -623,10 +623,10 @@ HIO_INLINE hio_uch_t* hio_dupbtoucharswithheadroom (hio_t* hio, hio_oow_t headro ptr = (hio_uch_t*)((hio_oob_t*)ptr + headroom_bytes); hio_convbtouchars (hio, bcs, &inlen, ptr, &outlen, all); - /* hio_convbtouchars() doesn't null-terminate the target. + /* hio_convbtouchars() doesn't null-terminate the target. * but in hio_dupbtouchars(), i allocate space. so i don't mind * null-terminating it with 1 extra character overhead */ - ptr[outlen] = '\0'; + ptr[outlen] = '\0'; if (ucslen) *ucslen = outlen; return ptr; } @@ -642,7 +642,7 @@ HIO_INLINE hio_bch_t* hio_duputobcharswithheadroom (hio_t* hio, hio_oow_t headro hio_bch_t* ptr; inlen = ucslen; - if (hio_convutobchars(hio, ucs, &inlen, HIO_NULL, &outlen) <= -1) + if (hio_convutobchars(hio, ucs, &inlen, HIO_NULL, &outlen) <= -1) { /* note it's also an error if no full conversion is made in this function */ return HIO_NULL; @@ -673,7 +673,7 @@ HIO_INLINE hio_uch_t* hio_dupbtoucstrwithheadroom (hio_t* hio, hio_oow_t headroo hio_oow_t inlen, outlen; hio_uch_t* ptr; - if (hio_convbtoucstr(hio, bcs, &inlen, HIO_NULL, &outlen, all) <= -1) + if (hio_convbtoucstr(hio, bcs, &inlen, HIO_NULL, &outlen, all) <= -1) { /* note it's also an error if no full conversion is made in this function */ return HIO_NULL; @@ -698,7 +698,7 @@ HIO_INLINE hio_bch_t* hio_duputobcstrwithheadroom (hio_t* hio, hio_oow_t headroo hio_oow_t inlen, outlen; hio_bch_t* ptr; - if (hio_convutobcstr(hio, ucs, &inlen, HIO_NULL, &outlen) <= -1) + if (hio_convutobcstr(hio, ucs, &inlen, HIO_NULL, &outlen) <= -1) { /* note it's also an error if no full conversion is made in this function */ return HIO_NULL; @@ -791,7 +791,7 @@ hio_uch_t* hio_dupucstrs (hio_t* hio, const hio_uch_t* ucs[], hio_oow_t* ucslen) ptr = (hio_uch_t*)hio_allocmem(hio, (len + 1) * HIO_SIZEOF(hio_uch_t)); if (!ptr) return HIO_NULL; - for (i = 0, len = 0; ucs[i]; i++) + for (i = 0, len = 0; ucs[i]; i++) len += hio_copy_ucstr_unlimited(&ptr[len], ucs[i]); ptr[len] = '\0'; @@ -809,7 +809,7 @@ hio_bch_t* hio_dupbcstrs (hio_t* hio, const hio_bch_t* bcs[], hio_oow_t* bcslen) ptr = (hio_bch_t*)hio_allocmem(hio, (len + 1) * HIO_SIZEOF(hio_bch_t)); if (!ptr) return HIO_NULL; - for (i = 0, len = 0; bcs[i]; i++) + for (i = 0, len = 0; bcs[i]; i++) len += hio_copy_bcstr_unlimited(&ptr[len], bcs[i]); ptr[len] = '\0'; @@ -916,7 +916,7 @@ void hio_sub_ntime (hio_ntime_t* z, const hio_ntime_t* x, const hio_ntime_t* y) xs = HIO_TYPE_MIN(hio_ntime_sec_t); ns = 0; } - } + } else { xs = xs - ys;