From 06a135f85212a524a117a7623d15a0725fb2fffa Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 27 Apr 2024 21:33:34 +0900 Subject: [PATCH] merged hclj to hclx --- bin/Makefile.am | 5 - bin/Makefile.in | 45 +--- bin/hclj.c | 337 --------------------------- bin/hclx.c | 113 ++++++++- bin/main-c.c | 601 ------------------------------------------------ 5 files changed, 111 insertions(+), 990 deletions(-) delete mode 100644 bin/hclj.c delete mode 100644 bin/main-c.c diff --git a/bin/Makefile.am b/bin/Makefile.am index 115ef03..54a6ae9 100644 --- a/bin/Makefile.am +++ b/bin/Makefile.am @@ -37,9 +37,4 @@ hclx_CPPFLAGS = $(CPPFLAGS_COMMON) hclx_LDFLAGS = $(LDFLAGS_COMMON) hclx_LDADD = ../lib/libhclx.la $(LIBADD_COMMON) -bin_PROGRAMS += hclj -hclj_SOURCES = hclj.c -hclj_CPPFLAGS = $(CPPFLAGS_COMMON) -hclj_LDFLAGS = $(LDFLAGS_COMMON) -hclj_LDADD = ../lib/libhclx.la $(LIBADD_COMMON) endif diff --git a/bin/Makefile.in b/bin/Makefile.in index 3ad25de..e7fb433 100644 --- a/bin/Makefile.in +++ b/bin/Makefile.in @@ -95,7 +95,7 @@ host_triplet = @host@ @ENABLE_LIBLTDL_TRUE@am__append_1 = $(LTDL_LIBS) @ENABLE_LIBLTDL_FALSE@am__append_2 = $(DL_LIBS) bin_PROGRAMS = hcl$(EXEEXT) $(am__EXEEXT_1) -@ENABLE_HCLX_TRUE@am__append_3 = hclx hclj +@ENABLE_HCLX_TRUE@am__append_3 = hclx subdir = bin ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/m4/ax_check_sign.m4 \ @@ -110,7 +110,7 @@ mkinstalldirs = $(install_sh) -d CONFIG_HEADER = $(top_builddir)/lib/hcl-cfg.h CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = -@ENABLE_HCLX_TRUE@am__EXEEXT_1 = hclx$(EXEEXT) hclj$(EXEEXT) +@ENABLE_HCLX_TRUE@am__EXEEXT_1 = hclx$(EXEEXT) am__installdirs = "$(DESTDIR)$(bindir)" PROGRAMS = $(bin_PROGRAMS) am_hcl_OBJECTS = hcl-hcl.$(OBJEXT) @@ -127,14 +127,6 @@ am__v_lt_1 = hcl_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ $(hcl_LDFLAGS) $(LDFLAGS) -o $@ -am__hclj_SOURCES_DIST = hclj.c -@ENABLE_HCLX_TRUE@am_hclj_OBJECTS = hclj-hclj.$(OBJEXT) -hclj_OBJECTS = $(am_hclj_OBJECTS) -@ENABLE_HCLX_TRUE@hclj_DEPENDENCIES = ../lib/libhclx.la \ -@ENABLE_HCLX_TRUE@ $(am__DEPENDENCIES_4) -hclj_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ - $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ - $(hclj_LDFLAGS) $(LDFLAGS) -o $@ am__hclx_SOURCES_DIST = hclx.c @ENABLE_HCLX_TRUE@am_hclx_OBJECTS = hclx-hclx.$(OBJEXT) hclx_OBJECTS = $(am_hclx_OBJECTS) @@ -158,8 +150,7 @@ am__v_at_1 = DEFAULT_INCLUDES = depcomp = $(SHELL) $(top_srcdir)/ac/depcomp am__maybe_remake_depfiles = depfiles -am__depfiles_remade = ./$(DEPDIR)/hcl-hcl.Po ./$(DEPDIR)/hclj-hclj.Po \ - ./$(DEPDIR)/hclx-hclx.Po +am__depfiles_remade = ./$(DEPDIR)/hcl-hcl.Po ./$(DEPDIR)/hclx-hclx.Po am__mv = mv -f COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) @@ -179,9 +170,8 @@ AM_V_CCLD = $(am__v_CCLD_@AM_V@) am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) am__v_CCLD_0 = @echo " CCLD " $@; am__v_CCLD_1 = -SOURCES = $(hcl_SOURCES) $(hclj_SOURCES) $(hclx_SOURCES) -DIST_SOURCES = $(hcl_SOURCES) $(am__hclj_SOURCES_DIST) \ - $(am__hclx_SOURCES_DIST) +SOURCES = $(hcl_SOURCES) $(hclx_SOURCES) +DIST_SOURCES = $(hcl_SOURCES) $(am__hclx_SOURCES_DIST) am__can_run_installinfo = \ case $$AM_UPDATE_INFO_DIR in \ n|no|NO) false;; \ @@ -366,10 +356,6 @@ hcl_LDADD = ../lib/libhcl.la $(LIBADD_COMMON) @ENABLE_HCLX_TRUE@hclx_CPPFLAGS = $(CPPFLAGS_COMMON) @ENABLE_HCLX_TRUE@hclx_LDFLAGS = $(LDFLAGS_COMMON) @ENABLE_HCLX_TRUE@hclx_LDADD = ../lib/libhclx.la $(LIBADD_COMMON) -@ENABLE_HCLX_TRUE@hclj_SOURCES = hclj.c -@ENABLE_HCLX_TRUE@hclj_CPPFLAGS = $(CPPFLAGS_COMMON) -@ENABLE_HCLX_TRUE@hclj_LDFLAGS = $(LDFLAGS_COMMON) -@ENABLE_HCLX_TRUE@hclj_LDADD = ../lib/libhclx.la $(LIBADD_COMMON) all: all-am .SUFFIXES: @@ -457,10 +443,6 @@ hcl$(EXEEXT): $(hcl_OBJECTS) $(hcl_DEPENDENCIES) $(EXTRA_hcl_DEPENDENCIES) @rm -f hcl$(EXEEXT) $(AM_V_CCLD)$(hcl_LINK) $(hcl_OBJECTS) $(hcl_LDADD) $(LIBS) -hclj$(EXEEXT): $(hclj_OBJECTS) $(hclj_DEPENDENCIES) $(EXTRA_hclj_DEPENDENCIES) - @rm -f hclj$(EXEEXT) - $(AM_V_CCLD)$(hclj_LINK) $(hclj_OBJECTS) $(hclj_LDADD) $(LIBS) - hclx$(EXEEXT): $(hclx_OBJECTS) $(hclx_DEPENDENCIES) $(EXTRA_hclx_DEPENDENCIES) @rm -f hclx$(EXEEXT) $(AM_V_CCLD)$(hclx_LINK) $(hclx_OBJECTS) $(hclx_LDADD) $(LIBS) @@ -472,7 +454,6 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hcl-hcl.Po@am__quote@ # am--include-marker -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hclj-hclj.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hclx-hclx.Po@am__quote@ # am--include-marker $(am__depfiles_remade): @@ -519,20 +500,6 @@ hcl-hcl.obj: hcl.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hcl_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o hcl-hcl.obj `if test -f 'hcl.c'; then $(CYGPATH_W) 'hcl.c'; else $(CYGPATH_W) '$(srcdir)/hcl.c'; fi` -hclj-hclj.o: hclj.c -@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hclj_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT hclj-hclj.o -MD -MP -MF $(DEPDIR)/hclj-hclj.Tpo -c -o hclj-hclj.o `test -f 'hclj.c' || echo '$(srcdir)/'`hclj.c -@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/hclj-hclj.Tpo $(DEPDIR)/hclj-hclj.Po -@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='hclj.c' object='hclj-hclj.o' libtool=no @AMDEPBACKSLASH@ -@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hclj_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o hclj-hclj.o `test -f 'hclj.c' || echo '$(srcdir)/'`hclj.c - -hclj-hclj.obj: hclj.c -@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hclj_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT hclj-hclj.obj -MD -MP -MF $(DEPDIR)/hclj-hclj.Tpo -c -o hclj-hclj.obj `if test -f 'hclj.c'; then $(CYGPATH_W) 'hclj.c'; else $(CYGPATH_W) '$(srcdir)/hclj.c'; fi` -@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/hclj-hclj.Tpo $(DEPDIR)/hclj-hclj.Po -@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='hclj.c' object='hclj-hclj.obj' libtool=no @AMDEPBACKSLASH@ -@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hclj_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o hclj-hclj.obj `if test -f 'hclj.c'; then $(CYGPATH_W) 'hclj.c'; else $(CYGPATH_W) '$(srcdir)/hclj.c'; fi` - hclx-hclx.o: hclx.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hclx_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT hclx-hclx.o -MD -MP -MF $(DEPDIR)/hclx-hclx.Tpo -c -o hclx-hclx.o `test -f 'hclx.c' || echo '$(srcdir)/'`hclx.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/hclx-hclx.Tpo $(DEPDIR)/hclx-hclx.Po @@ -680,7 +647,6 @@ clean-am: clean-binPROGRAMS clean-generic clean-libtool mostlyclean-am distclean: distclean-am -rm -f ./$(DEPDIR)/hcl-hcl.Po - -rm -f ./$(DEPDIR)/hclj-hclj.Po -rm -f ./$(DEPDIR)/hclx-hclx.Po -rm -f Makefile distclean-am: clean-am distclean-compile distclean-generic \ @@ -728,7 +694,6 @@ installcheck-am: maintainer-clean: maintainer-clean-am -rm -f ./$(DEPDIR)/hcl-hcl.Po - -rm -f ./$(DEPDIR)/hclj-hclj.Po -rm -f ./$(DEPDIR)/hclx-hclx.Po -rm -f Makefile maintainer-clean-am: distclean-am maintainer-clean-generic diff --git a/bin/hclj.c b/bin/hclj.c deleted file mode 100644 index 04a05d1..0000000 --- a/bin/hclj.c +++ /dev/null @@ -1,337 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -/* ========================================================================= */ - -typedef struct json_xtn_t json_xtn_t; -struct json_xtn_t -{ - int logfd; - hcl_bitmask_t logmask; - int logfd_istty; - - struct - { - hcl_bch_t buf[4096]; - hcl_oow_t len; - } logbuf; - - int depth; -}; -/* ========================================================================= */ - -static void* sys_alloc (hcl_mmgr_t* mmgr, hcl_oow_t size) -{ - return malloc(size); -} - -static void* sys_realloc (hcl_mmgr_t* mmgr, void* ptr, hcl_oow_t size) -{ - return realloc(ptr, size); -} - -static void sys_free (hcl_mmgr_t* mmgr, void* ptr) -{ - free (ptr); -} - -static hcl_mmgr_t sys_mmgr = -{ - sys_alloc, - sys_realloc, - sys_free, - HCL_NULL -}; - -/* ========================================================================= */ - -static int write_all (int fd, const hcl_bch_t* ptr, hcl_oow_t len) -{ - while (len > 0) - { - hcl_ooi_t wr; - - wr = write(fd, ptr, len); - - if (wr <= -1) - { - #if defined(EAGAIN) && defined(EWOULDBLOCK) && (EAGAIN == EWOULDBLOCK) - if (errno == EAGAIN) continue; - #else - #if defined(EAGAIN) - if (errno == EAGAIN) continue; - #elif defined(EWOULDBLOCK) - if (errno == EWOULDBLOCK) continue; - #endif - #endif - - #if defined(EINTR) - /* TODO: would this interfere with non-blocking nature of this VM? */ - if (errno == EINTR) continue; - #endif - return -1; - } - - ptr += wr; - len -= wr; - } - - return 0; -} - - -static int write_log (hcl_json_t* json, int fd, const hcl_bch_t* ptr, hcl_oow_t len) -{ - json_xtn_t* xtn = (json_xtn_t*)hcl_json_getxtn(json); - - while (len > 0) - { - if (xtn->logbuf.len > 0) - { - hcl_oow_t rcapa, cplen; - - rcapa = HCL_COUNTOF(xtn->logbuf.buf) - xtn->logbuf.len; - cplen = (len >= rcapa)? rcapa: len; - - memcpy (&xtn->logbuf.buf[xtn->logbuf.len], ptr, cplen); - xtn->logbuf.len += cplen; - ptr += cplen; - len -= cplen; - - if (xtn->logbuf.len >= HCL_COUNTOF(xtn->logbuf.buf)) - { - write_all(fd, xtn->logbuf.buf, xtn->logbuf.len); - xtn->logbuf.len = 0; - } - } - else - { - hcl_oow_t rcapa; - - rcapa = HCL_COUNTOF(xtn->logbuf.buf); - if (len >= rcapa) - { - write_all (fd, ptr, rcapa); - ptr += rcapa; - len -= rcapa; - } - else - { - memcpy (xtn->logbuf.buf, ptr, len); - xtn->logbuf.len += len; - ptr += len; - len -= len; - - } - } - } - - return 0; -} - -static void flush_log (hcl_json_t* json, int fd) -{ - json_xtn_t* xtn = (json_xtn_t*)hcl_json_getxtn(json); - if (xtn->logbuf.len > 0) - { - write_all (fd, xtn->logbuf.buf, xtn->logbuf.len); - xtn->logbuf.len = 0; - } -} - -static void log_write (hcl_json_t* json, hcl_bitmask_t mask, const hcl_ooch_t* msg, hcl_oow_t len) -{ - json_xtn_t* xtn = (json_xtn_t*)hcl_json_getxtn(json); - hcl_bch_t buf[256]; - hcl_oow_t ucslen, bcslen; - - hcl_oow_t msgidx; - int n, logfd; - - if (mask & HCL_LOG_STDERR) - { - /* the messages that go to STDERR don't get masked out */ - logfd = 2; - } - else - { - if (!(xtn->logmask & mask & ~HCL_LOG_ALL_LEVELS)) return; /* check log types */ - if (!(xtn->logmask & mask & ~HCL_LOG_ALL_TYPES)) return; /* check log levels */ - - if (mask & HCL_LOG_STDOUT) logfd = 1; - else - { - logfd = xtn->logfd; - if (logfd <= -1) return; - } - } - -/* TODO: beautify the log message. - * do classification based on mask. */ - if (!(mask & (HCL_LOG_STDOUT | HCL_LOG_STDERR))) - { - time_t now; - char ts[32]; - size_t tslen; - struct tm tm, *tmp; - - now = time(NULL); - - #if defined(__OS2__) - tmp = _localtime(&now, &tm); - #elif defined(HAVE_LOCALTIME_R) - tmp = localtime_r(&now, &tm); - #else - tmp = localtime(&now); - #endif - #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); - #endif - if (tslen == 0) - { - strcpy (ts, "0000-00-00 00:00:00 +0000"); - tslen = 25; - } - - write_log (json, logfd, ts, tslen); - } - - if (logfd == xtn->logfd && xtn->logfd_istty) - { - if (mask & HCL_LOG_FATAL) write_log (json, logfd, "\x1B[1;31m", 7); - else if (mask & HCL_LOG_ERROR) write_log (json, logfd, "\x1B[1;32m", 7); - else if (mask & HCL_LOG_WARN) write_log (json, logfd, "\x1B[1;33m", 7); - } - -#if defined(HCL_OOCH_IS_UCH) - msgidx = 0; - while (len > 0) - { - ucslen = len; - bcslen = HCL_COUNTOF(buf); - - n = hcl_conv_oochars_to_bchars_with_cmgr(&msg[msgidx], &ucslen, buf, &bcslen, hcl_get_utf8_cmgr()); - if (n == 0 || n == -2) - { - /* n = 0: - * converted all successfully - * n == -2: - * buffer not sufficient. not all got converted yet. - * write what have been converted this round. */ - - /*HCL_ASSERT (hcl, ucslen > 0); */ /* if this fails, the buffer size must be increased */ - /*assert (ucslen > 0);*/ - - /* attempt to write all converted characters */ - if (write_log(json, logfd, buf, bcslen) <= -1) break; - - if (n == 0) break; - else - { - msgidx += ucslen; - len -= ucslen; - } - } - else if (n <= -1) - { - /* conversion error */ - if (bcslen <= 0) break; - if (write_log(json, logfd, buf, bcslen) <= -1) break; - msgidx += ucslen; - len -= ucslen; - } - } -#else - write_log (json, logfd, msg, len); -#endif - - if (logfd == xtn->logfd && xtn->logfd_istty) - { - if (mask & (HCL_LOG_FATAL | HCL_LOG_ERROR | HCL_LOG_WARN)) write_log (json, logfd, "\x1B[0m", 4); - } - - flush_log (json, logfd); -} - -static int instcb (hcl_json_t* json, hcl_json_inst_t it, const hcl_oocs_t* str) -{ - json_xtn_t* json_xtn = (json_xtn_t*)hcl_json_getxtn(json); - - switch (it) - { - case HCL_JSON_INST_START_ARRAY: - json_xtn->depth++; - hcl_json_logbfmt (json, HCL_LOG_INFO | HCL_LOG_APP, "[\n"); - break; - case HCL_JSON_INST_END_ARRAY: - json_xtn->depth--; - hcl_json_logbfmt (json, HCL_LOG_INFO | HCL_LOG_APP, "]\n"); - break; - case HCL_JSON_INST_START_DIC: - json_xtn->depth++; - hcl_json_logbfmt (json, HCL_LOG_INFO | HCL_LOG_APP, "{\n"); - break; - case HCL_JSON_INST_END_DIC: - json_xtn->depth--; - hcl_json_logbfmt (json, HCL_LOG_INFO | HCL_LOG_APP, "}\n"); - break; - - case HCL_JSON_INST_KEY: - hcl_json_logbfmt (json, HCL_LOG_INFO | HCL_LOG_APP, "%.*js: ", str->len, str->ptr); - break; - - case HCL_JSON_INST_CHARACTER: - case HCL_JSON_INST_STRING: - case HCL_JSON_INST_NUMBER: - case HCL_JSON_INST_TRUE: - case HCL_JSON_INST_FALSE: - case HCL_JSON_INST_NIL: - hcl_json_logbfmt (json, HCL_LOG_INFO | HCL_LOG_APP, "%.*js\n", str->len, str->ptr); - break; - } - - return 0; -} -/* ========================================================================= */ - -int main (int argc, char* argv[]) -{ - - hcl_json_t* json; - hcl_json_prim_t json_prim; - json_xtn_t* json_xtn; - hcl_oow_t xlen; - const char* p; - - memset (&json_prim, 0, HCL_SIZEOF(json_prim)); - json_prim.log_write = log_write; - json_prim.instcb = instcb; - - json = hcl_json_open (&sys_mmgr, HCL_SIZEOF(json_xtn_t), &json_prim, NULL); - - json_xtn = (json_xtn_t*)hcl_json_getxtn(json); - json_xtn->logmask = HCL_LOG_ALL_LEVELS | HCL_LOG_ALL_TYPES; - - p = "[ \"ab\\xab\\uC88B\\uC544\\uC6A9c\", \"kaden\", \"iron\", true, { \"null\": \"a\\1bc\", \"123\": \"AA20AA\", \"10\": -0.123, \"way\": '\\uC88A' } ]"; - /*p = "{ \"result\": \"SUCCESS\", \"message\": \"1 clients\", \"sessions\": [] }";*/ - - if (hcl_json_feed(json, p, strlen(p), &xlen) <= -1) - { - hcl_json_logbfmt (json, HCL_LOG_FATAL | HCL_LOG_APP, "ERROR: unable to process - %js\n", hcl_json_geterrmsg(json)); - } - else if (json_xtn->depth != 0) - { - hcl_json_logbfmt (json, HCL_LOG_FATAL | HCL_LOG_APP, "ERROR: incomplete input\n"); - } - - hcl_json_close (json); - return 0; -} diff --git a/bin/hclx.c b/bin/hclx.c index ae55d27..3b569a0 100644 --- a/bin/hclx.c +++ b/bin/hclx.c @@ -25,6 +25,7 @@ */ #include "hcl-x.h" +#include "hcl-json.h" #include "hcl-opt.h" #include "hcl-utl.h" #include "hcl-xutl.h" @@ -65,10 +66,15 @@ struct server_xtn_t hcl_bch_t buf[4096]; hcl_oow_t len; } logbuf; + + + /* used by the json submodule */ + int json_depth; }; typedef server_xtn_t client_xtn_t; +typedef server_xtn_t json_xtn_t; /* ========================================================================= */ @@ -320,6 +326,11 @@ static void client_log_write (hcl_client_t* client, hcl_bitmask_t mask, const hc log_write ((client_xtn_t*)hcl_client_getxtn(client), HCL_SERVER_WID_INVALID, mask, msg, len); } +static void json_log_write (hcl_json_t* json, hcl_bitmask_t mask, const hcl_ooch_t* msg, hcl_oow_t len) +{ + log_write ((json_xtn_t*)hcl_json_getxtn(json), HCL_SERVER_WID_INVALID, mask, msg, len); +} + /* ========================================================================= */ static hcl_server_t* g_server = HCL_NULL; @@ -374,14 +385,11 @@ static void set_signal_to_default (int sig) /* ========================================================================= */ -static int handle_logopt (hcl_server_t* server, const hcl_bch_t* str) +static int handle_logopt (server_xtn_t* xtn, const hcl_bch_t* str) { hcl_bch_t* xstr = (hcl_bch_t*)str; hcl_bch_t* cm, * flt; hcl_bitmask_t logmask; - server_xtn_t* xtn; - - xtn = (server_xtn_t*)hcl_server_getxtn(server); cm = hcl_find_bchar_in_bcstr(xstr, ','); if (cm) @@ -460,6 +468,16 @@ static int handle_logopt (hcl_server_t* server, const hcl_bch_t* str) return 0; } +static int server_handle_logopt (hcl_server_t* server, const hcl_bch_t* str) +{ + return handle_logopt((server_xtn_t*)hcl_server_getxtn(server), str); +} + +static int client_handle_logopt (hcl_client_t* client, const hcl_bch_t* str) +{ + return handle_logopt((client_xtn_t*)hcl_client_getxtn(client), str); +} + #if defined(HCL_BUILD_DEBUG) static int handle_dbgopt (hcl_server_t* server, const char* str) { @@ -631,7 +649,7 @@ static int server_main (const char* outer, int argc, char* argv[]) if (logopt) { - if (handle_logopt(server, logopt) <= -1) goto oops; + if (handle_logopt(xtn, logopt) <= -1) goto oops; } else { @@ -799,7 +817,7 @@ static int client_main (const char* outer, int argc, char* argv[]) if (logopt) { - if (handle_logopt(client, logopt) <= -1) goto oops; + if (handle_logopt(xtn, logopt) <= -1) goto oops; } else { @@ -836,11 +854,88 @@ oops: if (client) hcl_client_close (client); return -1; } + +/* -------------------------------------------------------------- */ + +static int json_inst_cb (hcl_json_t* json, hcl_json_inst_t it, const hcl_oocs_t* str) +{ + json_xtn_t* json_xtn = (json_xtn_t*)hcl_json_getxtn(json); + + switch (it) + { + case HCL_JSON_INST_START_ARRAY: + json_xtn->json_depth++; + hcl_json_logbfmt (json, HCL_LOG_INFO | HCL_LOG_APP, "[\n"); + break; + case HCL_JSON_INST_END_ARRAY: + json_xtn->json_depth--; + hcl_json_logbfmt (json, HCL_LOG_INFO | HCL_LOG_APP, "]\n"); + break; + case HCL_JSON_INST_START_DIC: + json_xtn->json_depth++; + hcl_json_logbfmt (json, HCL_LOG_INFO | HCL_LOG_APP, "{\n"); + break; + case HCL_JSON_INST_END_DIC: + json_xtn->json_depth--; + hcl_json_logbfmt (json, HCL_LOG_INFO | HCL_LOG_APP, "}\n"); + break; + + case HCL_JSON_INST_KEY: + hcl_json_logbfmt (json, HCL_LOG_INFO | HCL_LOG_APP, "%.*js: ", str->len, str->ptr); + break; + + case HCL_JSON_INST_CHARACTER: + case HCL_JSON_INST_STRING: + case HCL_JSON_INST_NUMBER: + case HCL_JSON_INST_TRUE: + case HCL_JSON_INST_FALSE: + case HCL_JSON_INST_NIL: + hcl_json_logbfmt (json, HCL_LOG_INFO | HCL_LOG_APP, "%.*js\n", str->len, str->ptr); + break; + } + + return 0; +} +int json_main (const char* outer, int argc, char* argv[]) +{ + hcl_json_t* json; + hcl_json_prim_t json_prim; + json_xtn_t* json_xtn; + hcl_oow_t xlen; + const char* p; + +/* TODO: enhance this to accept parameters from command line */ + + memset (&json_prim, 0, HCL_SIZEOF(json_prim)); + json_prim.log_write = json_log_write; + json_prim.instcb = json_inst_cb; + + json = hcl_json_open (&sys_mmgr, HCL_SIZEOF(json_xtn_t), &json_prim, NULL); + + json_xtn = (json_xtn_t*)hcl_json_getxtn(json); + json_xtn->logmask = HCL_LOG_ALL_LEVELS | HCL_LOG_ALL_TYPES; + + p = "[ \"ab\\xab\\uC88B\\uC544\\uC6A9c\", \"kaden\", \"iron\", true, { \"null\": \"a\\1bc\", \"123\": \"AA20AA\", \"10\": -0.123, \"way\": '\\uC88A' } ]"; + /*p = "{ \"result\": \"SUCCESS\", \"message\": \"1 clients\", \"sessions\": [] }";*/ + + if (hcl_json_feed(json, p, strlen(p), &xlen) <= -1) + { + hcl_json_logbfmt (json, HCL_LOG_FATAL | HCL_LOG_APP, "ERROR: unable to process - %js\n", hcl_json_geterrmsg(json)); + } + else if (json_xtn->json_depth != 0) + { + hcl_json_logbfmt (json, HCL_LOG_FATAL | HCL_LOG_APP, "ERROR: incomplete input\n"); + } + + hcl_json_close (json); + return 0; +} + /* -------------------------------------------------------------- */ static void print_main_usage (const char* argv0) { - fprintf (stderr, "Usage: %s server|client\n", argv0); + fprintf (stderr, "Usage: %s server|client|json\n", argv0); } int main (int argc, char* argv[]) @@ -863,6 +958,10 @@ int main (int argc, char* argv[]) { n = client_main(argv[0], argc -1, &argv[1]); } + else if (strcmp(argv[1], "json") == 0) + { + n = json_main(argv[0], argc -1, &argv[1]); + } else { print_main_usage (argv[0]); diff --git a/bin/main-c.c b/bin/main-c.c deleted file mode 100644 index 036c7c3..0000000 --- a/bin/main-c.c +++ /dev/null @@ -1,601 +0,0 @@ -/* - * $Id$ - * - Copyright (c) 2016-2018 Chung, Hyung-Hwan. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (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 "hcl-x.h" -#include "hcl-opt.h" -#include "hcl-utl.h" -#include "hcl-xutl.h" - -#include -#include -#include -#include -#include - -#if defined(HAVE_TIME_H) -# include -#endif -#if defined(HAVE_SYS_TIME_H) -# include -#endif -#if defined(HAVE_SIGNAL_H) -# include -#endif -#if defined(HAVE_SYS_UIO_H) -# include -#endif - -#include -#include -#include -#include -#include -#include - -/* ========================================================================= */ - -typedef struct client_xtn_t client_xtn_t; -struct client_xtn_t -{ - int logfd; - hcl_bitmask_t logmask; - int logfd_istty; - - struct - { - hcl_bch_t buf[4096]; - hcl_oow_t len; - } logbuf; -}; - -/* ========================================================================= */ - -static void* sys_alloc (hcl_mmgr_t* mmgr, hcl_oow_t size) -{ - return malloc(size); -} - -static void* sys_realloc (hcl_mmgr_t* mmgr, void* ptr, hcl_oow_t size) -{ - return realloc(ptr, size); -} - -static void sys_free (hcl_mmgr_t* mmgr, void* ptr) -{ - free (ptr); -} - -static hcl_mmgr_t sys_mmgr = -{ - sys_alloc, - sys_realloc, - sys_free, - HCL_NULL -}; -/* ========================================================================= */ - -static int write_all (int fd, const hcl_bch_t* ptr, hcl_oow_t len) -{ - while (len > 0) - { - hcl_ooi_t wr; - - wr = write(fd, ptr, len); - - if (wr <= -1) - { - #if defined(EAGAIN) && defined(EWOULDBLOCK) && (EAGAIN == EWOULDBLOCK) - if (errno == EAGAIN) continue; - #else - #if defined(EAGAIN) - if (errno == EAGAIN) continue; - #elif defined(EWOULDBLOCK) - if (errno == EWOULDBLOCK) continue; - #endif - #endif - - #if defined(EINTR) - /* TODO: would this interfere with non-blocking nature of this VM? */ - if (errno == EINTR) continue; - #endif - return -1; - } - - ptr += wr; - len -= wr; - } - - return 0; -} - - -static int write_log (hcl_client_t* client, int fd, const hcl_bch_t* ptr, hcl_oow_t len) -{ - client_xtn_t* xtn; - - xtn = hcl_client_getxtn(client); - - while (len > 0) - { - if (xtn->logbuf.len > 0) - { - hcl_oow_t rcapa, cplen; - - rcapa = HCL_COUNTOF(xtn->logbuf.buf) - xtn->logbuf.len; - cplen = (len >= rcapa)? rcapa: len; - - memcpy (&xtn->logbuf.buf[xtn->logbuf.len], ptr, cplen); - xtn->logbuf.len += cplen; - ptr += cplen; - len -= cplen; - - if (xtn->logbuf.len >= HCL_COUNTOF(xtn->logbuf.buf)) - { - write_all(fd, xtn->logbuf.buf, xtn->logbuf.len); - xtn->logbuf.len = 0; - } - } - else - { - hcl_oow_t rcapa; - - rcapa = HCL_COUNTOF(xtn->logbuf.buf); - if (len >= rcapa) - { - write_all (fd, ptr, rcapa); - ptr += rcapa; - len -= rcapa; - } - else - { - memcpy (xtn->logbuf.buf, ptr, len); - xtn->logbuf.len += len; - ptr += len; - len -= len; - - } - } - } - - return 0; -} - -static void flush_log (hcl_client_t* client, int fd) -{ - client_xtn_t* xtn; - xtn = hcl_client_getxtn(client); - if (xtn->logbuf.len > 0) - { - write_all (fd, xtn->logbuf.buf, xtn->logbuf.len); - xtn->logbuf.len = 0; - } -} - -static void log_write (hcl_client_t* client, hcl_bitmask_t mask, const hcl_ooch_t* msg, hcl_oow_t len) -{ - hcl_bch_t buf[256]; - hcl_oow_t ucslen, bcslen; - client_xtn_t* xtn; - hcl_oow_t msgidx; - int n, logfd; - - xtn = hcl_client_getxtn(client); - - if (mask & HCL_LOG_STDERR) - { - /* the messages that go to STDERR don't get masked out */ - logfd = 2; - } - else - { - if (!(xtn->logmask & mask & ~HCL_LOG_ALL_LEVELS)) return; /* check log types */ - if (!(xtn->logmask & mask & ~HCL_LOG_ALL_TYPES)) return; /* check log levels */ - - if (mask & HCL_LOG_STDOUT) logfd = 1; - else - { - logfd = xtn->logfd; - if (logfd <= -1) return; - } - } - -/* TODO: beautify the log message. - * do classification based on mask. */ - if (!(mask & (HCL_LOG_STDOUT | HCL_LOG_STDERR))) - { - time_t now; - char ts[32]; - size_t tslen; - struct tm tm, *tmp; - - now = time(NULL); - - #if defined(__OS2__) - tmp = _localtime(&now, &tm); - #elif defined(HAVE_LOCALTIME_R) - tmp = localtime_r(&now, &tm); - #else - tmp = localtime(&now); - #endif - - #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); - #endif - if (tslen == 0) - { - strcpy (ts, "0000-00-00 00:00:00 +0000"); - tslen = 25; - } - - write_log (client, logfd, ts, tslen); - } - - if (logfd == xtn->logfd && xtn->logfd_istty) - { - if (mask & HCL_LOG_FATAL) write_log (client, logfd, "\x1B[1;31m", 7); - else if (mask & HCL_LOG_ERROR) write_log (client, logfd, "\x1B[1;32m", 7); - else if (mask & HCL_LOG_WARN) write_log (client, logfd, "\x1B[1;33m", 7); - } - -#if defined(HCL_OOCH_IS_UCH) - msgidx = 0; - while (len > 0) - { - ucslen = len; - bcslen = HCL_COUNTOF(buf); - - n = hcl_conv_oochars_to_bchars_with_cmgr(&msg[msgidx], &ucslen, buf, &bcslen, hcl_get_utf8_cmgr()); - if (n == 0 || n == -2) - { - /* n = 0: - * converted all successfully - * n == -2: - * buffer not sufficient. not all got converted yet. - * write what have been converted this round. */ - - /*HCL_ASSERT (hcl, ucslen > 0); */ /* if this fails, the buffer size must be increased */ - /*assert (ucslen > 0);*/ - - /* attempt to write all converted characters */ - if (write_log(client, logfd, buf, bcslen) <= -1) break; - - if (n == 0) break; - else - { - msgidx += ucslen; - len -= ucslen; - } - } - else if (n <= -1) - { - /* conversion error */ - if (bcslen <= 0) break; - if (write_log(client, logfd, buf, bcslen) <= -1) break; - msgidx += ucslen; - len -= ucslen; - } - } -#else - write_log (client, logfd, msg, len); -#endif - - if (logfd == xtn->logfd && xtn->logfd_istty) - { - if (mask & (HCL_LOG_FATAL | HCL_LOG_ERROR | HCL_LOG_WARN)) write_log (client, logfd, "\x1B[0m", 4); - } - - flush_log (client, logfd); -} - -/* ========================================================================= */ - -static hcl_client_t* g_client = HCL_NULL; - -/* ========================================================================= */ - -typedef void (*signal_handler_t) (int, siginfo_t*, void*); - -static void handle_sigint (int sig, siginfo_t* siginfo, void* ctx) -{ - /*if (g_client) hcl_client_stop (g_client);*/ -} - -static void set_signal (int sig, signal_handler_t handler) -{ - struct sigaction sa; - - memset (&sa, 0, sizeof(sa)); - /*sa.sa_handler = handler;*/ - sa.sa_flags = SA_SIGINFO; - sa.sa_sigaction = handler; - sigemptyset (&sa.sa_mask); - - sigaction (sig, &sa, NULL); -} - -static void set_signal_to_ignore (int sig) -{ - struct sigaction sa; - - memset (&sa, 0, sizeof(sa)); - sa.sa_handler = SIG_IGN; - sa.sa_flags = 0; - sigemptyset (&sa.sa_mask); - - sigaction (sig, &sa, NULL); -} - -static void set_signal_to_default (int sig) -{ - struct sigaction sa; - - memset (&sa, 0, sizeof(sa)); - sa.sa_handler = SIG_DFL; - sa.sa_flags = 0; - sigemptyset (&sa.sa_mask); - - sigaction (sig, &sa, NULL); -} - -/* ========================================================================= */ - -static int handle_logopt (hcl_client_t* client, const hcl_bch_t* str) -{ - hcl_bch_t* xstr = (hcl_bch_t*)str; - hcl_bch_t* cm, * flt; - hcl_bitmask_t logmask; - client_xtn_t* xtn; - - xtn = (client_xtn_t*)hcl_client_getxtn(client); - - cm = hcl_find_bchar_in_bcstr(xstr, ','); - if (cm) - { - /* i duplicate this string for open() below as open() doesn't - * accept a length-bounded string */ - xstr = strdup(str); - if (!xstr) - { - fprintf (stderr, "ERROR: out of memory in duplicating %s\n", str); - return -1; - } - - cm = hcl_find_bchar_in_bcstr(xstr, ','); - *cm = '\0'; - - logmask = xtn->logmask; - do - { - flt = cm + 1; - - cm = hcl_find_bchar_in_bcstr(flt, ','); - if (cm) *cm = '\0'; - - if (hcl_comp_bcstr(flt, "app") == 0) logmask |= HCL_LOG_APP; - else if (hcl_comp_bcstr(flt, "compiler") == 0) logmask |= HCL_LOG_COMPILER; - else if (hcl_comp_bcstr(flt, "vm") == 0) logmask |= HCL_LOG_VM; - else if (hcl_comp_bcstr(flt, "mnemonic") == 0) logmask |= HCL_LOG_MNEMONIC; - else if (hcl_comp_bcstr(flt, "gc") == 0) logmask |= HCL_LOG_GC; - else if (hcl_comp_bcstr(flt, "ic") == 0) logmask |= HCL_LOG_IC; - else if (hcl_comp_bcstr(flt, "primitive") == 0) logmask |= HCL_LOG_PRIMITIVE; - - else if (hcl_comp_bcstr(flt, "fatal") == 0) logmask |= HCL_LOG_FATAL; - else if (hcl_comp_bcstr(flt, "error") == 0) logmask |= HCL_LOG_ERROR; - else if (hcl_comp_bcstr(flt, "warn") == 0) logmask |= HCL_LOG_WARN; - else if (hcl_comp_bcstr(flt, "info") == 0) logmask |= HCL_LOG_INFO; - else if (hcl_comp_bcstr(flt, "debug") == 0) logmask |= HCL_LOG_DEBUG; - - else if (hcl_comp_bcstr(flt, "fatal+") == 0) logmask |= HCL_LOG_FATAL; - else if (hcl_comp_bcstr(flt, "error+") == 0) logmask |= HCL_LOG_FATAL | HCL_LOG_ERROR; - else if (hcl_comp_bcstr(flt, "warn+") == 0) logmask |= HCL_LOG_FATAL | HCL_LOG_ERROR | HCL_LOG_WARN; - else if (hcl_comp_bcstr(flt, "info+") == 0) logmask |= HCL_LOG_FATAL | HCL_LOG_ERROR | HCL_LOG_WARN | HCL_LOG_INFO; - else if (hcl_comp_bcstr(flt, "debug+") == 0) logmask |= HCL_LOG_FATAL | HCL_LOG_ERROR | HCL_LOG_WARN | HCL_LOG_INFO | HCL_LOG_DEBUG; - - else - { - fprintf (stderr, "ERROR: unknown log option value - %s\n", flt); - if (str != xstr) free (xstr); - return -1; - } - } - while (cm); - - if (!(logmask & HCL_LOG_ALL_TYPES)) logmask |= HCL_LOG_ALL_TYPES; /* no types specified. force to all types */ - if (!(logmask & HCL_LOG_ALL_LEVELS)) logmask |= HCL_LOG_ALL_LEVELS; /* no levels specified. force to all levels */ - } - else - { - logmask = HCL_LOG_ALL_LEVELS | HCL_LOG_ALL_TYPES; - } - - xtn->logfd = open(xstr, O_CREAT | O_WRONLY | O_APPEND , 0644); - if (xtn->logfd == -1) - { - fprintf (stderr, "ERROR: cannot open a log file %s\n", xstr); - if (str != xstr) free (xstr); - return -1; - } - - xtn->logmask = logmask; -#if defined(HAVE_ISATTY) - xtn->logfd_istty = isatty(xtn->logfd); -#endif - - if (str != xstr) free (xstr); - return 0; -} - -/* ========================================================================= */ - -static int client_on_packet (hcl_xproto_t* proto, hcl_xpkt_type_t type, const void* data, hcl_oow_t len) -{ - if (type == HCL_XPKT_STDOUT) - { - /*if (len > 0) fwrite (data, 1, len, stdout); */ - if (len > 0) fprintf (stdout, "%.*s", (int)len, data); - } - else if (type == HCL_XPKT_STDERR) - { - if (len > 0) fprintf (stderr, "%.*s", (int)len, data); - } - else if (type == HCL_XPKT_ERROR) - { - /* error notification */ - if (len > 0) fprintf (stderr, "ERROR: %.*s\n", (int)len, data); - } - return 1; -} - -/* ========================================================================= */ - -int main (int argc, char* argv[]) -{ - hcl_bci_t c; - static hcl_bopt_lng_t lopt[] = - { - { ":log", 'l' }, - { "reuseaddr", '\0' }, - { "shutwr", '\0' }, - { HCL_NULL, '\0' } - }; - static hcl_bopt_t opt = - { - "l:", - lopt - }; - - hcl_client_t* client; - client_xtn_t* xtn; - hcl_client_prim_t client_prim; - int n; - const char* logopt = HCL_NULL; - int reuse_addr = 0; - int shut_wr_after_req = 0; - - setlocale (LC_ALL, ""); - - if (argc < 2) - { - print_usage: - fprintf (stderr, "Usage: %s [-l/--log log-options] [--reuseaddr] [--shutwr] bind-address:port script-to-run\n", argv[0]); - return -1; - } - - while ((c = hcl_getbopt (argc, argv, &opt)) != HCL_BCI_EOF) - { - switch (c) - { - case 'l': - logopt = opt.arg; - break; - - case '\0': - if (hcl_comp_bcstr(opt.lngopt, "reuseaddr") == 0) - { - reuse_addr = 1; - } - else if (hcl_comp_bcstr(opt.lngopt, "shutwr") == 0) - { - shut_wr_after_req = 1; - } - else - { - goto print_usage; - } - break; - - case ':': - if (opt.lngopt) - fprintf (stderr, "bad argument for '%s'\n", opt.lngopt); - else - fprintf (stderr, "bad argument for '%c'\n", opt.opt); - - return -1; - - default: - goto print_usage; - } - } - - /* needs 2 fixed arguments */ - if (opt.ind + 1 >= argc) goto print_usage; - - memset (&client_prim, 0, HCL_SIZEOF(client_prim)); - client_prim.log_write = log_write; - client_prim.on_packet = client_on_packet; - - client = hcl_client_open(&sys_mmgr, HCL_SIZEOF(client_xtn_t), &client_prim, HCL_NULL); - if (!client) - { - fprintf (stderr, "cannot open client\n"); - return -1; - } - - xtn = (client_xtn_t*)hcl_client_getxtn(client); - xtn->logfd = -1; - xtn->logfd_istty = 0; - - if (logopt) - { - if (handle_logopt(client, logopt) <= -1) goto oops; - } - else - { - /* default logging mask when no logging option is set */ - xtn->logmask = HCL_LOG_ALL_TYPES | HCL_LOG_ERROR | HCL_LOG_FATAL; - } - - g_client = client; - set_signal (SIGINT, handle_sigint); - set_signal_to_ignore (SIGPIPE); - - n = hcl_client_start(client, argv[opt.ind], argv[opt.ind + 1], reuse_addr, shut_wr_after_req); - if (n <= -1) - { - fprintf (stderr, "ERROR: %s\n", hcl_client_geterrbmsg(client)); - goto oops; - } - - set_signal_to_default (SIGINT); - set_signal_to_default (SIGPIPE); - g_client = NULL; - - if (xtn->logfd >= 0) - { - close (xtn->logfd); - xtn->logfd = -1; - xtn->logfd_istty = 0; - } - - hcl_client_close (client); - return n; - -oops: - if (client) hcl_client_close (client); - return -1; -}