void* qse_awk_rtx_callocmem (qse_awk_rtx_t* rtx, qse_size_t size) { void* ptr = QSE_AWK_ALLOC (rtx->awk, size); if (ptr) QSE_MEMSET (ptr, 0, size); else qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); return ptr; }
int qse_timelocal (const qse_btime_t* bt, qse_ntime_t* nt) { /* TODO: qse_timelocal - remove dependency on timelocal */ struct tm tm; QSE_MEMSET (&tm, 0, QSE_SIZEOF(tm)); tm.tm_sec = bt->sec; tm.tm_min = bt->min; tm.tm_hour = bt->hour; tm.tm_mday = bt->mday; tm.tm_mon = bt->mon; tm.tm_year = bt->year; tm.tm_wday = bt->wday; tm.tm_yday = bt->yday; tm.tm_isdst = bt->isdst; #if defined(HAVE_TIMELOCAL) nt->sec = timelocal (&tm); #else nt->sec = mktime (&tm); #endif nt->nsec = 0; return 0; }
static qse_rex_node_t* newnode (comp_t* c, qse_rex_node_id_t id) { qse_rex_node_t* node; /* TODO: performance optimization. * preallocate a large chunk of memory and allocate a node * from the chunk. increase the chunk if it has been used up. */ node = (qse_rex_node_t*) QSE_MMGR_ALLOC (c->rex->mmgr, QSE_SIZEOF(qse_rex_node_t)); if (node == QSE_NULL) { c->rex->errnum = QSE_REX_ENOMEM; return QSE_NULL; } QSE_MEMSET (node, 0, QSE_SIZEOF(*node)); node->id = id; if (c->start != QSE_NULL) { QSE_ASSERT (c->start->id == QSE_REX_NODE_START); node->link = c->start->u.s.link; c->start->u.s.link = node; } return node; }
qse_httpd_task_t* qse_httpd_entaskformat ( qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* pred, const qse_mchar_t* fmt, ...) { qse_httpd_task_t task; task_format_t data; va_list ap; qse_mchar_t* buf; int bytes_req, l; va_start (ap, fmt); bytes_req = qse_mbsxvfmt (QSE_NULL, 0, fmt, ap); va_end (ap); buf = (qse_mchar_t*) qse_httpd_allocmem ( httpd, (bytes_req + 1) * QSE_SIZEOF(*buf)); if (buf == QSE_NULL) return QSE_NULL; va_start (ap, fmt); l = qse_mbsxvfmt (buf, bytes_req + 1, fmt, ap); va_end (ap); if (l != bytes_req) { /* something got wrong ... */ qse_httpd_freemem (httpd, buf); httpd->errnum = QSE_HTTPD_EINTERN; return QSE_NULL; } QSE_MEMSET (&data, 0, QSE_SIZEOF(data)); data.org = buf; data.ptr = buf; data.left = l; QSE_MEMSET (&task, 0, QSE_SIZEOF(task)); task.init = task_init_format; task.fini = task_fini_format; task.main = task_main_format; task.ctx = &data; return qse_httpd_entask ( httpd, client, pred, &task, QSE_SIZEOF(data)); }
void qse_xli_seterrmsg ( qse_xli_t* xli, qse_xli_errnum_t errnum, const qse_char_t* errmsg, const qse_xli_loc_t* errloc) { xli->errnum = errnum; qse_strxcpy (xli->errmsg, QSE_COUNTOF(xli->errmsg), errmsg); if (errloc != QSE_NULL) xli->errloc = *errloc; else QSE_MEMSET (&xli->errloc, 0, QSE_SIZEOF(xli->errloc)); }
qse_httpd_task_t* qse_httpd_entaskdisconnect ( qse_httpd_t* httpd, qse_httpd_client_t* client, qse_httpd_task_t* pred) { qse_httpd_task_t task; QSE_MEMSET (&task, 0, QSE_SIZEOF(task)); task.main = task_main_disconnect; return qse_httpd_entask (httpd, client, pred, &task, 0); }
int qse_rex_exec ( qse_rex_t* rex, const qse_cstr_t* str, const qse_cstr_t* substr, qse_cstr_t* matstr) { exec_t e; int n = 0; if (rex->code == QSE_NULL) { rex->errnum = QSE_REX_ENOCOMP; return -1; } QSE_MEMSET (&e, 0, QSE_SIZEOF(e)); e.rex = rex; e.str.ptr = str->ptr; e.str.end = str->ptr + str->len; e.sub.ptr = substr->ptr; e.sub.end = substr->ptr + substr->len; if (init_exec_dds (&e, rex->mmgr) <= -1) return -1; while (e.sub.ptr <= e.sub.end) { n = exec (&e); if (n <= -1) { n = -1; break; } if (n >= 1) { QSE_ASSERT (e.nmatches > 0); QSE_ASSERT (e.matchend != QSE_NULL); if (matstr) { matstr->ptr = e.sub.ptr; matstr->len = e.matchend - e.sub.ptr; } break; } e.sub.ptr++; } fini_exec_dds (&e); return n; }
int qse_rex_init (qse_rex_t* rex, qse_mmgr_t* mmgr, qse_rex_node_t* code) { QSE_MEMSET (rex, 0, QSE_SIZEOF(*rex)); rex->mmgr = mmgr; QSE_ASSERT (code == QSE_NULL || code->id == QSE_REX_NODE_START); /* note that passing a compiled expression to qse_rex_open() * is to delegate it to this rex object. when this rex object * is closed, the code delegated is destroyed. */ rex->code = code; return 0; }
int qse_rbt_init (rbt_t* rbt, mmgr_t* mmgr, int kscale, int vscale) { /* do not zero out the extension */ QSE_MEMSET (rbt, 0, SIZEOF(*rbt)); rbt->mmgr = mmgr; rbt->scale[QSE_RBT_KEY] = (kscale < 1)? 1: kscale; rbt->scale[QSE_RBT_VAL] = (vscale < 1)? 1: vscale; rbt->size = 0; rbt->style = &style[0]; /* self-initializing nil */ QSE_MEMSET(&rbt->xnil, 0, QSE_SIZEOF(rbt->xnil)); rbt->xnil.color = QSE_RBT_BLACK; rbt->xnil.left = &rbt->xnil; rbt->xnil.right = &rbt->xnil; /* root is set to nil initially */ rbt->root = &rbt->xnil; return 0; }
static int matchtre ( qse_awk_t* awk, qse_tre_t* tre, int opt, const qse_cstr_t* str, qse_cstr_t* mat, qse_cstr_t submat[9], qse_awk_errnum_t* errnum) { int n; /*qse_tre_match_t match[10] = { { 0, 0 }, };*/ qse_tre_match_t match[10]; QSE_MEMSET (match, 0, QSE_SIZEOF(match)); n = qse_tre_execx(tre, str->ptr, str->len, match, QSE_COUNTOF(match), opt); if (n <= -1) { if (QSE_TRE_ERRNUM(tre) == QSE_TRE_ENOMATCH) return 0; #if 0 /* TODO: */ *errnum = (QSE_TRE_ERRNUM(tre) == QSE_TRE_ENOMEM)? QSE_AWK_ENOMEM: QSE_AWK_EREXMA; SETERR0 (sed, errnum, loc); #endif *errnum = (QSE_TRE_ERRNUM(tre) == QSE_TRE_ENOMEM)? QSE_AWK_ENOMEM: QSE_AWK_EREXMA; return -1; } QSE_ASSERT (match[0].rm_so != -1); if (mat) { mat->ptr = &str->ptr[match[0].rm_so]; mat->len = match[0].rm_eo - match[0].rm_so; } if (submat) { int i; /* you must intialize submat before you pass into this * function because it can abort filling */ for (i = 1; i < QSE_COUNTOF(match); i++) { if (match[i].rm_so != -1) { submat[i-1].ptr = &str->ptr[match[i].rm_so]; submat[i-1].len = match[i].rm_eo - match[i].rm_so; } } } return 1; }
void qse_xli_seterror ( qse_xli_t* xli, qse_xli_errnum_t errnum, const qse_cstr_t* errarg, const qse_xli_loc_t* errloc) { const qse_char_t* errfmt; xli->errnum = errnum; errfmt = qse_xli_geterrstr(xli)(xli,xli->errnum); QSE_ASSERT (errfmt != QSE_NULL); qse_strxfncpy (xli->errmsg, QSE_COUNTOF(xli->errmsg), errfmt, errarg); if (errloc != QSE_NULL) xli->errloc = *errloc; else QSE_MEMSET (&xli->errloc, 0, QSE_SIZEOF(xli->errloc)); }
rbt_t* qse_rbt_open (mmgr_t* mmgr, size_t xtnsize, int kscale, int vscale) { rbt_t* rbt; rbt = (rbt_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(rbt_t) + xtnsize); if (rbt == QSE_NULL) return QSE_NULL; if (qse_rbt_init (rbt, mmgr, kscale, vscale) <= -1) { QSE_MMGR_FREE (mmgr, rbt); return QSE_NULL; } QSE_MEMSET (rbt + 1, 0, xtnsize); return rbt; }
qse_rex_t* qse_rex_open (qse_mmgr_t* mmgr, qse_size_t xtnsize, qse_rex_node_t* code) { qse_rex_t* rex; rex = (qse_rex_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_rex_t) + xtnsize); if (rex == QSE_NULL) return QSE_NULL; if (qse_rex_init (rex, mmgr, code) <= -1) { QSE_MMGR_FREE (mmgr, rex); return QSE_NULL; } QSE_MEMSET (QSE_XTN(rex), 0, xtnsize); return rex; }
qse_tio_t* qse_tio_open (qse_mmgr_t* mmgr, qse_size_t xtnsize, int flags) { qse_tio_t* tio; tio = QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_tio_t) + xtnsize); if (tio) { if (qse_tio_init (tio, mmgr, flags) <= -1) { QSE_MMGR_FREE (mmgr, tio); return QSE_NULL; } else QSE_MEMSET (QSE_XTN(tio), 0, xtnsize); } return tio; }
int qse_localtime (const qse_ntime_t* nt, qse_btime_t* bt) { struct tm* tm; time_t t = nt->sec; /* TODO: remove dependency on localtime/localtime_r */ #if defined(_WIN32) tm = localtime (&t); #elif defined(__OS2__) # if defined(__WATCOMC__) struct tm btm; tm = _localtime (&t, &btm); # else # error Please support other compilers # endif #elif defined(__DOS__) # if defined(__WATCOMC__) struct tm btm; tm = _localtime (&t, &btm); # else # error Please support other compilers # endif #elif defined(HAVE_LOCALTIME_R) struct tm btm; tm = localtime_r (&t, &btm); #else /* thread unsafe */ tm = localtime (&t); #endif if (tm == QSE_NULL) return -1; QSE_MEMSET (bt, 0, QSE_SIZEOF(*bt)); bt->sec = tm->tm_sec; bt->min = tm->tm_min; bt->hour = tm->tm_hour; bt->mday = tm->tm_mday; bt->mon = tm->tm_mon; bt->year = tm->tm_year; bt->wday = tm->tm_wday; bt->yday = tm->tm_yday; bt->isdst = tm->tm_isdst; /*bt->offset = tm->tm_offset;*/ return 0; }
qse_nwio_t* qse_nwio_open ( qse_mmgr_t* mmgr, qse_size_t xtnsize, const qse_nwad_t* nwad, int flags, const qse_nwio_tmout_t* tmout) { qse_nwio_t* nwio; nwio = QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_nwio_t) + xtnsize); if (nwio == QSE_NULL) return QSE_NULL; if (qse_nwio_init (nwio, mmgr, nwad, flags, tmout) <= -1) { QSE_MMGR_FREE (mmgr, nwio); return QSE_NULL; } QSE_MEMSET (nwio + 1, 0, xtnsize); return nwio; }
int qse_tio_init (qse_tio_t* tio, qse_mmgr_t* mmgr, int flags) { QSE_MEMSET (tio, 0, QSE_SIZEOF(*tio)); tio->mmgr = mmgr; tio->cmgr = qse_getdflcmgr(); tio->flags = flags; /* tio->input_func = QSE_NULL; tio->input_arg = QSE_NULL; tio->output_func = QSE_NULL; tio->output_arg = QSE_NULL; tio->status = 0; tio->inbuf_cur = 0; tio->inbuf_len = 0; tio->outbuf_len = 0; */ tio->errnum = QSE_TIO_ENOERR; return 0; }
int qse_nwio_init ( qse_nwio_t* nwio, qse_mmgr_t* mmgr, const qse_nwad_t* nwad, int flags, const qse_nwio_tmout_t* tmout) { qse_skad_t addr; qse_sck_len_t addrlen; int family, type, tmp; QSE_MEMSET (nwio, 0, QSE_SIZEOF(*nwio)); nwio->mmgr = mmgr; nwio->flags = flags; nwio->errnum = QSE_NWIO_ENOERR; if (tmout) nwio->tmout = *tmout; else { nwio->tmout.r.sec = -1; nwio->tmout.w.sec = -1; nwio->tmout.c.sec = -1; nwio->tmout.a.sec = -1; } tmp = qse_nwadtoskad (nwad, &addr); if (tmp <= -1) { nwio->errnum = QSE_NWIO_EINVAL; return -1; } addrlen = tmp; #if defined(SOCK_STREAM) && defined(SOCK_DGRAM) if (flags & QSE_NWIO_TCP) type = SOCK_STREAM; else if (flags & QSE_NWIO_UDP) type = SOCK_DGRAM; else #endif { nwio->errnum = QSE_NWIO_EINVAL; return -1; } family = qse_skadfamily (&addr); #if defined(_WIN32) nwio->handle = socket (family, type, 0); if (nwio->handle == INVALID_SOCKET) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } if ((flags & QSE_NWIO_TCP) && (flags & QSE_NWIO_KEEPALIVE)) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_KEEPALIVE, (void*)&optval, QSE_SIZEOF(optval)); } if (flags & QSE_NWIO_PASSIVE) { qse_nwio_hnd_t handle; if (flags & QSE_NWIO_REUSEADDR) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, QSE_SIZEOF(optval)); } if (bind (nwio->handle, (struct sockaddr*)&addr, addrlen) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } if (flags & QSE_NWIO_TCP) { if (listen (nwio->handle, 10) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } if (TMOUT_ENABLED(nwio->tmout.a) && wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) goto oops; handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen); if (handle == INVALID_SOCKET) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } closesocket (nwio->handle); nwio->handle = handle; } else if (flags & QSE_NWIO_UDP) { nwio->status |= STATUS_UDP_CONNECT; } } else { int xret; if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { unsigned long cmd = 1; if (ioctlsocket(nwio->handle, FIONBIO, &cmd) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } } xret = connect (nwio->handle, (struct sockaddr*)&addr, addrlen); if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { unsigned long cmd = 0; if ((xret == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) || ioctlsocket (nwio->handle, FIONBIO, &cmd) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } if (wait_for_data (nwio, &nwio->tmout.c, 1) <= -1) goto oops; else { int xlen; DWORD xerr; xlen = QSE_SIZEOF(xerr); if (getsockopt (nwio->handle, SOL_SOCKET, SO_ERROR, (char*)&xerr, &xlen) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } else if (xerr != 0) { nwio->errnum = skerr_to_errnum (xerr); goto oops; } } } else { if (xret == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } } } #elif defined(__OS2__) nwio->handle = socket (family, type, 0); if (nwio->handle <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } if ((flags & QSE_NWIO_TCP) && (flags & QSE_NWIO_KEEPALIVE)) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_KEEPALIVE, (void*)&optval, QSE_SIZEOF(optval)); } if (flags & QSE_NWIO_PASSIVE) { qse_nwio_hnd_t handle; if (flags & QSE_NWIO_REUSEADDR) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, QSE_SIZEOF(optval)); } if (bind (nwio->handle, (struct sockaddr*)&addr, addrlen) <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } if (flags & QSE_NWIO_TCP) { if (listen (nwio->handle, 10) <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } if (TMOUT_ENABLED(nwio->tmout.a) && wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) goto oops; handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen); if (handle <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } soclose (nwio->handle); nwio->handle = handle; } else if (flags & QSE_NWIO_UDP) { nwio->status |= STATUS_UDP_CONNECT; } } else { int xret; if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { int noblk = 1; if (ioctl (nwio->handle, FIONBIO, (void*)&noblk, QSE_SIZEOF(noblk)) <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } } xret = connect (nwio->handle, (struct sockaddr*)&addr, addrlen); if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { int noblk = 0; if ((xret <= -1 && sock_errno() != SOCEINPROGRESS) || ioctl (nwio->handle, FIONBIO, (void*)&noblk, QSE_SIZEOF(noblk)) <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } if (wait_for_data (nwio, &nwio->tmout.c, 1) <= -1) goto oops; else { int xlen, xerr; xlen = QSE_SIZEOF(xerr); if (getsockopt (nwio->handle, SOL_SOCKET, SO_ERROR, (char*)&xerr, &xlen) <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } else if (xerr != 0) { nwio->errnum = skerr_to_errnum (xerr); goto oops; } } } else { if (xret <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } } } #elif defined(__DOS__) nwio->handle = socket (family, type, 0); if (nwio->handle <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if ((flags & QSE_NWIO_TCP) && (flags & QSE_NWIO_KEEPALIVE)) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_KEEPALIVE, (void*)&optval, QSE_SIZEOF(optval)); } if (flags & QSE_NWIO_PASSIVE) { qse_nwio_hnd_t handle; #if defined(SO_REUSEADDR) if (flags & QSE_NWIO_REUSEADDR) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, QSE_SIZEOF(optval)); } #endif if (bind (nwio->handle, (struct sockaddr*)&addr, addrlen) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (flags & QSE_NWIO_TCP) { if (listen (nwio->handle, 10) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (TMOUT_ENABLED(nwio->tmout.a) && wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) goto oops; handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen); if (handle <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } close_s (nwio->handle); nwio->handle = handle; } else if (flags & QSE_NWIO_UDP) { nwio->status |= STATUS_UDP_CONNECT; } } else { int xret; if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { int cmd = 1; if (ioctlsocket(nwio->handle, FIONBIO, (char*)&cmd) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (errno); goto oops; } } xret = connect (nwio->handle, (struct sockaddr*)&addr, addrlen); if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { int cmd = 0; if ((xret == SOCKET_ERROR && errno != EWOULDBLOCK) || ioctlsocket (nwio->handle, FIONBIO, (char*)&cmd) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (wait_for_data (nwio, &nwio->tmout.c, 1) <= -1) goto oops; else { int xlen, xerr; xlen = QSE_SIZEOF(xerr); if (getsockopt (nwio->handle, SOL_SOCKET, SO_ERROR, (char*)&xerr, &xlen) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } else if (xerr != 0) { nwio->errnum = skerr_to_errnum (xerr); goto oops; } } } else { if (xret <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } } } #elif defined(USE_TLI) { static const qse_mchar_t* dev_path[2][2] = { { "/dev/tcp", "/dev/inet/tcp" }, { "/dev/udp", "/dev/inet/tcp" } }; int dev_id; if (flags & QSE_NWIO_TCP) dev_id = 0; else { QSE_ASSERT (flags & QSE_NWIO_UDP); dev_id = 1; } nwio->handle = t_open (dev_path[dev_id][0], O_RDWR, QSE_NULL); if (nwio->handle <= -1) { nwio->handle = t_open (dev_path[dev_id][1], O_RDWR, QSE_NULL); if (nwio->handle <= -1) { nwio->errnum = tlierr_to_errnum (t_errno, errno); goto oops; } } if (flags & QSE_NWIO_PASSIVE) { /* TODO: */ nwio->errnum = QSE_NWIO_ENOIMPL; goto oops; } else { struct t_call call; /* for connecting */ struct t_bind req, ret; /* for binding */ qse_skad_t reqaddr, retaddr; qse_nwad_t reqnwad; /* call = t_alloc (nwio->handle, T_CALL, T_ADDR); if (!call) { nwio->errnum = tlierr_to_errnum (t_errno, errno); goto oops; }*/ qse_clearnwad (&reqnwad, nwad->type); qse_nwadtoskad (&reqnwad, &reqaddr); QSE_MEMSET (&ret, 0, QSE_SIZEOF(req)); req.addr.maxlen = addrlen; req.addr.len = addrlen; req.addr.buf = &reqaddr; QSE_MEMSET (&ret, 0, QSE_SIZEOF(ret)); ret.addr.maxlen = addrlen; ret.addr.len = addrlen; ret.addr.buf = &retaddr; if (t_bind (nwio->handle, &req, &ret) <= -1) { nwio->errnum = tlierr_to_errnum (t_errno, errno); goto oops; } /* TODO: should i use t_alloc() and t_free for call, ret, req? */ QSE_MEMSET (&call, 0, QSE_SIZEOF(call)); call.addr.maxlen = addrlen; call.addr.len = addrlen; call.addr.buf = &addr; if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { int orgfl; orgfl = fcntl (nwio->handle, F_GETFL, 0); if (orgfl <= -1 || fcntl (nwio->handle, F_SETFL, orgfl | O_NONBLOCK) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (t_connect (nwio->handle, &call, 0) <= -1) { if (t_errno != TNODATA) { nwio->errnum = tlierr_to_errnum (t_errno, errno); goto oops; } /* TODO: this doesn't seem to work wel... REDO THE WORK */ if (wait_for_data (nwio, &nwio->tmout.c, 0) <= -1) goto oops; if (t_rcvconnect (nwio->handle, QSE_NULL) <= -1) { nwio->errnum = tlierr_to_errnum (t_errno, errno); goto oops; } } if (fcntl (nwio->handle, F_SETFL, orgfl) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } } else { if (t_connect (nwio->handle, &call, 0) <= -1) { nwio->errnum = tlierr_to_errnum (t_errno, errno); goto oops; } } } } #else #if defined(SOCK_CLOEXEC) nwio->handle = socket (family, type | SOCK_CLOEXEC, 0); #else nwio->handle = socket (family, type, 0); #endif if (nwio->handle <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } #if !defined(SOCK_CLOEXEC) && defined(FD_CLOEXEC) { int tmp = fcntl (nwio->handle, F_GETFD); if (tmp >= 0) fcntl (nwio->handle, F_SETFD, tmp | FD_CLOEXEC); } #endif if ((flags & QSE_NWIO_TCP) && (flags & QSE_NWIO_KEEPALIVE)) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_KEEPALIVE, (void*)&optval, QSE_SIZEOF(optval)); } if (flags & QSE_NWIO_PASSIVE) { qse_nwio_hnd_t handle; #if defined(SO_REUSEADDR) if (flags & QSE_NWIO_REUSEADDR) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, QSE_SIZEOF(optval)); } #endif if (bind (nwio->handle, (struct sockaddr*)&addr, addrlen) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (flags & QSE_NWIO_TCP) { if (listen (nwio->handle, 10) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (TMOUT_ENABLED(nwio->tmout.a) && wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) goto oops; handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen); if (handle <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } qse_closesckhnd (nwio->handle); /* close the listening socket */ nwio->handle = handle; /* set the handle to the accepted socket */ } else if (flags & QSE_NWIO_UDP) { nwio->status |= STATUS_UDP_CONNECT; } } else { int xret; if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { int orgfl; orgfl = fcntl (nwio->handle, F_GETFL, 0); if (orgfl <= -1 || fcntl (nwio->handle, F_SETFL, orgfl | O_NONBLOCK) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } xret = connect (nwio->handle, (struct sockaddr*)&addr, addrlen); if ((xret <= -1 && errno != EINPROGRESS) || fcntl (nwio->handle, F_SETFL, orgfl) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (wait_for_data (nwio, &nwio->tmout.c, 1) <= -1) goto oops; else { qse_sck_len_t xlen; xlen = QSE_SIZEOF(xret); if (getsockopt (nwio->handle, SOL_SOCKET, SO_ERROR, (char*)&xret, &xlen) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } else if (xret != 0) { nwio->errnum = skerr_to_errnum (xret); goto oops; } } } else { xret = connect (nwio->handle, (struct sockaddr*)&addr, addrlen); if (xret <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } } } #endif if (flags & QSE_NWIO_TEXT) { int topt = 0; if (flags & QSE_NWIO_IGNOREMBWCERR) topt |= QSE_TIO_IGNOREMBWCERR; if (flags & QSE_NWIO_NOAUTOFLUSH) topt |= QSE_TIO_NOAUTOFLUSH; nwio->tio = qse_tio_open (mmgr, QSE_SIZEOF(qse_nwio_t*), topt); if (nwio->tio == QSE_NULL) { nwio->errnum = QSE_NWIO_ENOMEM; goto oops; } /* store the back-reference to nwio in the extension area.*/ *(qse_nwio_t**)QSE_XTN(nwio->tio) = nwio; if (qse_tio_attachin (nwio->tio, socket_input, QSE_NULL, 4096) <= -1 || qse_tio_attachout (nwio->tio, socket_output, QSE_NULL, 4096) <= -1) { if (nwio->errnum == QSE_NWIO_ENOERR) nwio->errnum = tio_errnum_to_nwio_errnum (nwio->tio); goto oops; } }
int qse_mbstouri (const qse_mchar_t* str, qse_muri_t* uri, int flags) { const qse_mchar_t* ptr, * colon; qse_muri_t xuri; QSE_MEMSET (&xuri, 0, QSE_SIZEOF(xuri)); /* scheme */ xuri.scheme.ptr = str; while (*str != QSE_MT(':')) { if (*str == QSE_MT('\0')) return -1; str++; } xuri.scheme.len = str - (const qse_mchar_t*)xuri.scheme.ptr; str++; /* skip : */ if (*str != QSE_MT('/')) return -1; str++; /* skip / */ if (*str != QSE_MT('/')) return -1; str++; /* skip / */ /* username, password, host, port */ for (colon = QSE_NULL, ptr = str; ; str++) { if (flags & QSE_MBSTOURI_NOAUTH) { if (colon == QSE_NULL && *str == QSE_MT(':')) colon = str; else if (*str == QSE_MT('/') || *str == QSE_MT('\0')) { if (colon) { xuri.host.ptr = ptr; xuri.host.len = colon - ptr; xuri.port.ptr = colon + 1; xuri.port.len = str - colon - 1; } else { xuri.host.ptr = ptr; xuri.host.len = str - ptr; } break; } } else { if (colon == QSE_NULL && *str == QSE_MT(':')) colon = str; else if (xuri.auth.user.ptr == QSE_NULL && *str == QSE_MT('@')) { if (colon) { xuri.auth.user.ptr = ptr; xuri.auth.user.len = colon - ptr; xuri.auth.pass.ptr = colon + 1; xuri.auth.pass.len = str - colon - 1; colon = QSE_NULL; } else { xuri.auth.user.ptr = ptr; xuri.auth.user.len = str - ptr; } ptr = str + 1; } else if (*str == QSE_MT('/') || *str == QSE_MT('\0')) { if (colon) { xuri.host.ptr = ptr; xuri.host.len = colon - ptr; xuri.port.ptr = colon + 1; xuri.port.len = str - colon - 1; } else { xuri.host.ptr = ptr; xuri.host.len = str - ptr; } break; } } } if (*str == QSE_MT('/')) { xuri.path.ptr = str; while (*str != QSE_MT('\0')) { if ((!(flags & QSE_MBSTOURI_NOQUERY) && *str == QSE_MT('?')) || (!(flags & QSE_MBSTOURI_NOFRAG) && *str == QSE_MT('#'))) break; str++; } xuri.path.len = str - (const qse_mchar_t*)xuri.path.ptr; if (!(flags & QSE_MBSTOURI_NOQUERY) && *str == QSE_MT('?')) { xuri.query.ptr = ++str; while (*str != QSE_MT('\0')) { if (!(flags & QSE_MBSTOURI_NOFRAG) && *str == QSE_MT('#')) break; str++; } xuri.query.len = str - (const qse_mchar_t*)xuri.query.ptr; } if (!(flags & QSE_MBSTOURI_NOFRAG) && *str == QSE_MT('#')) { xuri.frag.ptr = ++str; while (*str != QSE_MT('\0')) str++; xuri.frag.len = str - (const qse_mchar_t*)xuri.frag.ptr; } } QSE_ASSERT (*str == QSE_MT('\0')); *uri = xuri; return 0; }
int qse_settime (const qse_ntime_t* t) { #if defined(_WIN32) FILETIME ft; SYSTEMTIME st; /**((qse_int64_t*)&ft) = ((t + EPOCH_DIFF_MSECS) * (10 * 1000));*/ *((qse_int64_t*)&ft) = (QSE_SEC_TO_NSEC(t->sec + EPOCH_DIFF_SECS) / 100) + (t->nsec / 100); if (FileTimeToSystemTime (&ft, &st) == FALSE) return -1; if (SetSystemTime(&st) == FALSE) return -1; return 0; #elif defined(__OS2__) APIRET rc; DATETIME dt; qse_btime_t bt; if (qse_localtime (t, &bt) <= -1) return -1; QSE_MEMSET (&dt, 0, QSE_SIZEOF(dt)); dt.year = bt.year + QSE_BTIME_YEAR_BASE; dt.month = bt.mon + 1; dt.day = bt.mday; dt.hours = bt.hour; dt.minutes = bt.min; dt.seconds = bt.sec; dt.hundredths = QSE_NSEC_TO_MSEC(t->nsec) / 10; rc = DosSetDateTime (&dt); return (rc != NO_ERROR)? -1: 0; #elif defined(__DOS__) struct dostime_t dt; struct dosdate_t dd; qse_btime_t bt; if (qse_localtime (t, &bt) <= -1) return -1; dd.year = bt.year + QSE_BTIME_YEAR_BASE; dd.month = bt.mon + 1; dd.day = bt.mday; dt.hour = bt.hour; dt.minute = bt.min; dt.second = bt.sec; dt.hsecond = QSE_NSEC_TO_MSEC(t->nsec) / 10; if (_dos_settime (&dt) != 0) return -1; if (_dos_setdate (&dd) != 0) return -1; return 0; #elif defined(HAVE_SETTIMEOFDAY) struct timeval tv; int n; tv.tv_sec = t->sec; tv.tv_usec = QSE_NSEC_TO_USEC(t->nsec); /* #if defined(CLOCK_REALTIME) && defined(HAVE_CLOCK_SETTIME) { int r = clock_settime (CLOCK_REALTIME, ts); if (r == 0 || errno == EPERM) return r; } #elif defined(HAVE_STIME) return stime (&ts->tv_sec); #else */ n = QSE_SETTIMEOFDAY (&tv, QSE_NULL); if (n == -1) return -1; return 0; #else time_t tv; tv = t->sec; return (QSE_STIME (&tv) == -1)? -1: 0; #endif }