From a604d40e6e0a722f6fa71f0b32214a929cf2409c Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Thu, 31 Mar 2022 17:14:52 +0000 Subject: [PATCH] importing sed code --- hawk/bin/Makefile.am | 9 +- hawk/bin/Makefile.in | 38 +- hawk/bin/{main.c => main-hawk.c} | 2 +- hawk/configure | 15 + hawk/configure.ac | 5 + hawk/lib/Makefile.am | 4 +- hawk/lib/Makefile.in | 51 +- hawk/lib/err.c | 24 +- hawk/lib/fio.c | 118 +++- hawk/lib/hawk-cfg.h.in | 3 + hawk/lib/hawk-fio.h | 4 +- hawk/lib/hawk-gem.h | 11 +- hawk/lib/hawk-sed.h | 286 ++++++-- hawk/lib/hawk-sio.h | 2 + hawk/lib/hawk-utl.h | 25 +- hawk/lib/hawk.h | 46 +- hawk/lib/sed-prv.h | 5 +- hawk/lib/sed.c | 10 +- hawk/lib/sio.c | 21 +- hawk/lib/std-sed.c | 1059 ++++++++++++++++++++++++++++++ hawk/lib/utl-rnd.c | 49 ++ hawk/lib/utl-str.c | 12 +- hawk/mod/mod-ffi.c | 18 +- 23 files changed, 1682 insertions(+), 135 deletions(-) rename hawk/bin/{main.c => main-hawk.c} (99%) create mode 100644 hawk/lib/std-sed.c create mode 100644 hawk/lib/utl-rnd.c diff --git a/hawk/bin/Makefile.am b/hawk/bin/Makefile.am index 95b9b454..a12a3160 100644 --- a/hawk/bin/Makefile.am +++ b/hawk/bin/Makefile.am @@ -13,12 +13,12 @@ LDFLAGS_COMMON = -L$(abs_builddir)/../lib -L$(libdir) ##Use -l for third party libraries for automatic inclusion to DEPENDENCIES . LIBADD_COMMON = ../lib/libhawk.la -bin_PROGRAMS = hawk +bin_PROGRAMS = hawk #hsed ################################################## # hawk ################################################## -hawk_SOURCES = main.c +hawk_SOURCES = main-hawk.c hawk_CPPFLAGS = $(CPPFLAGS_COMMON) hawk_CFLAGS = $(CFLAGS_COMMON) hawk_LDFLAGS = $(LDFLAGS_COMMON) @@ -38,3 +38,8 @@ hawk_LDFLAGS = $(LDFLAGS_COMMON) ## hawk_LDADD = $(LIBADD_COMMON) $(LIBM) +#hsed_SOURCES = main-hsed.c +#hsed_CPPFLAGS = $(CPPFLAGS_COMMON) +#hsed_CFLAGS = $(CFLAGS_COMMON) +#hsed_LDFLAGS = $(LDFLAGS_COMMON) +#hsed_LDADD = $(LIBADD_COMMON) $(LIBM) diff --git a/hawk/bin/Makefile.in b/hawk/bin/Makefile.in index 41c3cf3b..0bf7dfaa 100644 --- a/hawk/bin/Makefile.in +++ b/hawk/bin/Makefile.in @@ -109,7 +109,7 @@ CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = am__installdirs = "$(DESTDIR)$(bindir)" PROGRAMS = $(bin_PROGRAMS) -am_hawk_OBJECTS = hawk-main.$(OBJEXT) +am_hawk_OBJECTS = hawk-main-hawk.$(OBJEXT) hawk_OBJECTS = $(am_hawk_OBJECTS) am__DEPENDENCIES_1 = hawk_DEPENDENCIES = $(LIBADD_COMMON) $(am__DEPENDENCIES_1) @@ -135,7 +135,7 @@ am__v_at_1 = DEFAULT_INCLUDES = depcomp = $(SHELL) $(top_srcdir)/ac/depcomp am__maybe_remake_depfiles = depfiles -am__depfiles_remade = ./$(DEPDIR)/hawk-main.Po +am__depfiles_remade = ./$(DEPDIR)/hawk-main-hawk.Po am__mv = mv -f COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) @@ -346,7 +346,7 @@ LIBADD_COMMON = ../lib/libhawk.la ################################################## # hawk ################################################## -hawk_SOURCES = main.c +hawk_SOURCES = main-hawk.c hawk_CPPFLAGS = $(CPPFLAGS_COMMON) hawk_CFLAGS = $(CFLAGS_COMMON) hawk_LDFLAGS = $(LDFLAGS_COMMON) @@ -444,7 +444,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hawk-main.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hawk-main-hawk.Po@am__quote@ # am--include-marker $(am__depfiles_remade): @$(MKDIR_P) $(@D) @@ -476,19 +476,19 @@ am--depfiles: $(am__depfiles_remade) @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $< -hawk-main.o: main.c -@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hawk_CPPFLAGS) $(CPPFLAGS) $(hawk_CFLAGS) $(CFLAGS) -MT hawk-main.o -MD -MP -MF $(DEPDIR)/hawk-main.Tpo -c -o hawk-main.o `test -f 'main.c' || echo '$(srcdir)/'`main.c -@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/hawk-main.Tpo $(DEPDIR)/hawk-main.Po -@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='main.c' object='hawk-main.o' libtool=no @AMDEPBACKSLASH@ +hawk-main-hawk.o: main-hawk.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hawk_CPPFLAGS) $(CPPFLAGS) $(hawk_CFLAGS) $(CFLAGS) -MT hawk-main-hawk.o -MD -MP -MF $(DEPDIR)/hawk-main-hawk.Tpo -c -o hawk-main-hawk.o `test -f 'main-hawk.c' || echo '$(srcdir)/'`main-hawk.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/hawk-main-hawk.Tpo $(DEPDIR)/hawk-main-hawk.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='main-hawk.c' object='hawk-main-hawk.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) $(hawk_CPPFLAGS) $(CPPFLAGS) $(hawk_CFLAGS) $(CFLAGS) -c -o hawk-main.o `test -f 'main.c' || echo '$(srcdir)/'`main.c +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hawk_CPPFLAGS) $(CPPFLAGS) $(hawk_CFLAGS) $(CFLAGS) -c -o hawk-main-hawk.o `test -f 'main-hawk.c' || echo '$(srcdir)/'`main-hawk.c -hawk-main.obj: main.c -@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hawk_CPPFLAGS) $(CPPFLAGS) $(hawk_CFLAGS) $(CFLAGS) -MT hawk-main.obj -MD -MP -MF $(DEPDIR)/hawk-main.Tpo -c -o hawk-main.obj `if test -f 'main.c'; then $(CYGPATH_W) 'main.c'; else $(CYGPATH_W) '$(srcdir)/main.c'; fi` -@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/hawk-main.Tpo $(DEPDIR)/hawk-main.Po -@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='main.c' object='hawk-main.obj' libtool=no @AMDEPBACKSLASH@ +hawk-main-hawk.obj: main-hawk.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hawk_CPPFLAGS) $(CPPFLAGS) $(hawk_CFLAGS) $(CFLAGS) -MT hawk-main-hawk.obj -MD -MP -MF $(DEPDIR)/hawk-main-hawk.Tpo -c -o hawk-main-hawk.obj `if test -f 'main-hawk.c'; then $(CYGPATH_W) 'main-hawk.c'; else $(CYGPATH_W) '$(srcdir)/main-hawk.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/hawk-main-hawk.Tpo $(DEPDIR)/hawk-main-hawk.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='main-hawk.c' object='hawk-main-hawk.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) $(hawk_CPPFLAGS) $(CPPFLAGS) $(hawk_CFLAGS) $(CFLAGS) -c -o hawk-main.obj `if test -f 'main.c'; then $(CYGPATH_W) 'main.c'; else $(CYGPATH_W) '$(srcdir)/main.c'; fi` +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hawk_CPPFLAGS) $(CPPFLAGS) $(hawk_CFLAGS) $(CFLAGS) -c -o hawk-main-hawk.obj `if test -f 'main-hawk.c'; then $(CYGPATH_W) 'main-hawk.c'; else $(CYGPATH_W) '$(srcdir)/main-hawk.c'; fi` mostlyclean-libtool: -rm -f *.lo @@ -623,7 +623,7 @@ clean: clean-am clean-am: clean-binPROGRAMS clean-generic clean-libtool mostlyclean-am distclean: distclean-am - -rm -f ./$(DEPDIR)/hawk-main.Po + -rm -f ./$(DEPDIR)/hawk-main-hawk.Po -rm -f Makefile distclean-am: clean-am distclean-compile distclean-generic \ distclean-tags @@ -669,7 +669,7 @@ install-ps-am: installcheck-am: maintainer-clean: maintainer-clean-am - -rm -f ./$(DEPDIR)/hawk-main.Po + -rm -f ./$(DEPDIR)/hawk-main-hawk.Po -rm -f Makefile maintainer-clean-am: distclean-am maintainer-clean-generic @@ -707,6 +707,12 @@ uninstall-am: uninstall-binPROGRAMS .PRECIOUS: Makefile +#hsed_SOURCES = main-hsed.c +#hsed_CPPFLAGS = $(CPPFLAGS_COMMON) +#hsed_CFLAGS = $(CFLAGS_COMMON) +#hsed_LDFLAGS = $(LDFLAGS_COMMON) +#hsed_LDADD = $(LIBADD_COMMON) $(LIBM) + # Tell versions [3.59,3.63) of GNU make to not export all variables. # Otherwise a system limit (for SysV at least) may be exceeded. .NOEXPORT: diff --git a/hawk/bin/main.c b/hawk/bin/main-hawk.c similarity index 99% rename from hawk/bin/main.c rename to hawk/bin/main-hawk.c index 9dd3f105..0832d764 100644 --- a/hawk/bin/main.c +++ b/hawk/bin/main-hawk.c @@ -465,7 +465,7 @@ static void on_statement (hawk_rtx_t* rtx, hawk_nde_t* nde) static void print_version (const hawk_bch_t* argv0) { printf ("%s %s\n", hawk_get_base_name_bcstr(argv0), HAWK_PACKAGE_VERSION); - printf ("Copyright 2006-2020 Chung, Hyung-Hwan\n"); + printf ("Copyright 2006-2022 Chung, Hyung-Hwan\n"); } static void print_error (const hawk_bch_t* fmt, ...) diff --git a/hawk/configure b/hawk/configure index 247483eb..ad548e46 100755 --- a/hawk/configure +++ b/hawk/configure @@ -19303,6 +19303,21 @@ fi +OLDLIBS="$LIBS" +LIBS="$FFI_LIBS $LIBS" +for ac_func in ffi_prep_cif_var +do : + ac_fn_c_check_func "$LINENO" "ffi_prep_cif_var" "ac_cv_func_ffi_prep_cif_var" +if test "x$ac_cv_func_ffi_prep_cif_var" = xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_FFI_PREP_CIF_VAR 1 +_ACEOF + +fi +done + +LIBS="$OLDLIBS" + if test "x${ac_cv_header_libunwind_h}" = "xyes" then { $as_echo "$as_me:${as_lineno-$LINENO}: checking for unw_backtrace in -lunwind" >&5 diff --git a/hawk/configure.ac b/hawk/configure.ac index 1aae3ba1..c12f61e7 100644 --- a/hawk/configure.ac +++ b/hawk/configure.ac @@ -311,6 +311,11 @@ AC_CHECK_LIB([ffi], [ffi_call], ) AC_SUBST(FFI_LIBS) +OLDLIBS="$LIBS" +LIBS="$FFI_LIBS $LIBS" +AC_CHECK_FUNCS([ffi_prep_cif_var]) +LIBS="$OLDLIBS" + if test "x${ac_cv_header_libunwind_h}" = "xyes" then AC_CHECK_LIB([unwind], [unw_backtrace], diff --git a/hawk/lib/Makefile.am b/hawk/lib/Makefile.am index 1c98cae3..9fa346dc 100644 --- a/hawk/lib/Makefile.am +++ b/hawk/lib/Makefile.am @@ -124,6 +124,7 @@ libhawk_la_SOURCES = \ utf16.c \ utf8.c \ utl-ass.c \ + utl-rnd.c\ utl-sort.c \ utl-str.c \ utl-sys.c \ @@ -148,7 +149,8 @@ libhawk_la_SOURCES += \ syscall.h \ tio.c \ std-prv.h \ - std.c + std.c \ + std-sed.c libhawk_la_CPPFLAGS = $(CPPFLAGS_ALL_COMMON) $(CPPFLAGS_PFMOD) libhawk_la_CFLAGS = $(CFLAGS_ALL_COMMON) diff --git a/hawk/lib/Makefile.in b/hawk/lib/Makefile.in index 79af4376..d1ace1f4 100644 --- a/hawk/lib/Makefile.in +++ b/hawk/lib/Makefile.in @@ -240,12 +240,13 @@ am__libhawk_la_SOURCES_DIST = hawk.h hawk-arr.h hawk-chr.h hawk-cmn.h \ tre-compile.c tre-compile.h tre-match-bt.c tre-match-pa.c \ tre-match-ut.h tre-mem.c tre-mem.h tre-parse.c tre-parse.h \ tre-stack.h tre-stack.c tre.c tree-prv.h tree.c uch-prop.h \ - uch-case.h utf16.c utf8.c utl-ass.c utl-sort.c utl-str.c \ - utl-sys.c utl.c val-prv.h val.c xma.c hawk-cli.h hawk-fio.h \ - hawk-mtx.h hawk-pio.h hawk-sio.h hawk-tio.h cli-imp.h cli.c \ - fio.c mtx.c pio.c sio.c syscall.h tio.c std-prv.h std.c \ - Hawk.cpp HawkStd.cpp mod-hawk.c mod-hawk.h mod-math.c \ - mod-math.h mod-str.c mod-str.h mod-sys.c mod-sys.h + uch-case.h utf16.c utf8.c utl-ass.c utl-rnd.c utl-sort.c \ + utl-str.c utl-sys.c utl.c val-prv.h val.c xma.c hawk-cli.h \ + hawk-fio.h hawk-mtx.h hawk-pio.h hawk-sio.h hawk-tio.h \ + cli-imp.h cli.c fio.c mtx.c pio.c sio.c syscall.h tio.c \ + std-prv.h std.c std-sed.c Hawk.cpp HawkStd.cpp mod-hawk.c \ + mod-hawk.h mod-math.c mod-math.h mod-str.c mod-str.h mod-sys.c \ + mod-sys.h am__objects_1 = am__objects_2 = $(am__objects_1) @ENABLE_CXX_TRUE@am__objects_3 = libhawk_la-Hawk.lo \ @@ -267,13 +268,13 @@ am_libhawk_la_OBJECTS = $(am__objects_2) libhawk_la-arr.lo \ libhawk_la-tre-match-pa.lo libhawk_la-tre-mem.lo \ libhawk_la-tre-parse.lo libhawk_la-tre-stack.lo \ libhawk_la-tre.lo libhawk_la-tree.lo libhawk_la-utf16.lo \ - libhawk_la-utf8.lo libhawk_la-utl-ass.lo \ + libhawk_la-utf8.lo libhawk_la-utl-ass.lo libhawk_la-utl-rnd.lo \ libhawk_la-utl-sort.lo libhawk_la-utl-str.lo \ libhawk_la-utl-sys.lo libhawk_la-utl.lo libhawk_la-val.lo \ libhawk_la-xma.lo libhawk_la-cli.lo libhawk_la-fio.lo \ libhawk_la-mtx.lo libhawk_la-pio.lo libhawk_la-sio.lo \ - libhawk_la-tio.lo libhawk_la-std.lo $(am__objects_3) \ - $(am__objects_4) + libhawk_la-tio.lo libhawk_la-std.lo libhawk_la-std-sed.lo \ + $(am__objects_3) $(am__objects_4) libhawk_la_OBJECTS = $(am_libhawk_la_OBJECTS) AM_V_P = $(am__v_P_@AM_V@) am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) @@ -314,6 +315,7 @@ am__depfiles_remade = ./$(DEPDIR)/libhawk_hawk_la-mod-hawk.Plo \ ./$(DEPDIR)/libhawk_la-rec.Plo ./$(DEPDIR)/libhawk_la-rio.Plo \ ./$(DEPDIR)/libhawk_la-run.Plo ./$(DEPDIR)/libhawk_la-sed.Plo \ ./$(DEPDIR)/libhawk_la-sio.Plo ./$(DEPDIR)/libhawk_la-skad.Plo \ + ./$(DEPDIR)/libhawk_la-std-sed.Plo \ ./$(DEPDIR)/libhawk_la-std.Plo ./$(DEPDIR)/libhawk_la-tio.Plo \ ./$(DEPDIR)/libhawk_la-tre-ast.Plo \ ./$(DEPDIR)/libhawk_la-tre-compile.Plo \ @@ -326,6 +328,7 @@ am__depfiles_remade = ./$(DEPDIR)/libhawk_hawk_la-mod-hawk.Plo \ ./$(DEPDIR)/libhawk_la-utf16.Plo \ ./$(DEPDIR)/libhawk_la-utf8.Plo \ ./$(DEPDIR)/libhawk_la-utl-ass.Plo \ + ./$(DEPDIR)/libhawk_la-utl-rnd.Plo \ ./$(DEPDIR)/libhawk_la-utl-sort.Plo \ ./$(DEPDIR)/libhawk_la-utl-str.Plo \ ./$(DEPDIR)/libhawk_la-utl-sys.Plo \ @@ -610,11 +613,11 @@ libhawk_la_SOURCES = $(pkginclude_HEADERS) arr.c chr.c dir.c ecs-imp.h \ tre-compile.c tre-compile.h tre-match-bt.c tre-match-pa.c \ tre-match-ut.h tre-mem.c tre-mem.h tre-parse.c tre-parse.h \ tre-stack.h tre-stack.c tre.c tree-prv.h tree.c uch-prop.h \ - uch-case.h utf16.c utf8.c utl-ass.c utl-sort.c utl-str.c \ - utl-sys.c utl.c val-prv.h val.c xma.c hawk-cli.h hawk-fio.h \ - hawk-mtx.h hawk-pio.h hawk-sio.h hawk-tio.h cli-imp.h cli.c \ - fio.c mtx.c pio.c sio.c syscall.h tio.c std-prv.h std.c \ - $(am__append_8) $(am__append_9) + uch-case.h utf16.c utf8.c utl-ass.c utl-rnd.c utl-sort.c \ + utl-str.c utl-sys.c utl.c val-prv.h val.c xma.c hawk-cli.h \ + hawk-fio.h hawk-mtx.h hawk-pio.h hawk-sio.h hawk-tio.h \ + cli-imp.h cli.c fio.c mtx.c pio.c sio.c syscall.h tio.c \ + std-prv.h std.c std-sed.c $(am__append_8) $(am__append_9) libhawk_la_CPPFLAGS = $(CPPFLAGS_ALL_COMMON) $(CPPFLAGS_PFMOD) \ $(am__append_3) libhawk_la_CFLAGS = $(CFLAGS_ALL_COMMON) @@ -806,6 +809,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-sed.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-sio.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-skad.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-std-sed.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-std.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-tio.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-tre-ast.Plo@am__quote@ # am--include-marker @@ -820,6 +824,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-utf16.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-utf8.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-utl-ass.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-utl-rnd.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-utl-sort.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-utl-str.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-utl-sys.Plo@am__quote@ # am--include-marker @@ -1126,6 +1131,13 @@ libhawk_la-utl-ass.lo: utl-ass.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CFLAGS) $(CFLAGS) -c -o libhawk_la-utl-ass.lo `test -f 'utl-ass.c' || echo '$(srcdir)/'`utl-ass.c +libhawk_la-utl-rnd.lo: utl-rnd.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CFLAGS) $(CFLAGS) -MT libhawk_la-utl-rnd.lo -MD -MP -MF $(DEPDIR)/libhawk_la-utl-rnd.Tpo -c -o libhawk_la-utl-rnd.lo `test -f 'utl-rnd.c' || echo '$(srcdir)/'`utl-rnd.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libhawk_la-utl-rnd.Tpo $(DEPDIR)/libhawk_la-utl-rnd.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='utl-rnd.c' object='libhawk_la-utl-rnd.lo' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CFLAGS) $(CFLAGS) -c -o libhawk_la-utl-rnd.lo `test -f 'utl-rnd.c' || echo '$(srcdir)/'`utl-rnd.c + libhawk_la-utl-sort.lo: utl-sort.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CFLAGS) $(CFLAGS) -MT libhawk_la-utl-sort.lo -MD -MP -MF $(DEPDIR)/libhawk_la-utl-sort.Tpo -c -o libhawk_la-utl-sort.lo `test -f 'utl-sort.c' || echo '$(srcdir)/'`utl-sort.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libhawk_la-utl-sort.Tpo $(DEPDIR)/libhawk_la-utl-sort.Plo @@ -1217,6 +1229,13 @@ libhawk_la-std.lo: std.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CFLAGS) $(CFLAGS) -c -o libhawk_la-std.lo `test -f 'std.c' || echo '$(srcdir)/'`std.c +libhawk_la-std-sed.lo: std-sed.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CFLAGS) $(CFLAGS) -MT libhawk_la-std-sed.lo -MD -MP -MF $(DEPDIR)/libhawk_la-std-sed.Tpo -c -o libhawk_la-std-sed.lo `test -f 'std-sed.c' || echo '$(srcdir)/'`std-sed.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libhawk_la-std-sed.Tpo $(DEPDIR)/libhawk_la-std-sed.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='std-sed.c' object='libhawk_la-std-sed.lo' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CFLAGS) $(CFLAGS) -c -o libhawk_la-std-sed.lo `test -f 'std-sed.c' || echo '$(srcdir)/'`std-sed.c + libhawk_la-mod-hawk.lo: mod-hawk.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CFLAGS) $(CFLAGS) -MT libhawk_la-mod-hawk.lo -MD -MP -MF $(DEPDIR)/libhawk_la-mod-hawk.Tpo -c -o libhawk_la-mod-hawk.lo `test -f 'mod-hawk.c' || echo '$(srcdir)/'`mod-hawk.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libhawk_la-mod-hawk.Tpo $(DEPDIR)/libhawk_la-mod-hawk.Plo @@ -1472,6 +1491,7 @@ distclean: distclean-am -rm -f ./$(DEPDIR)/libhawk_la-sed.Plo -rm -f ./$(DEPDIR)/libhawk_la-sio.Plo -rm -f ./$(DEPDIR)/libhawk_la-skad.Plo + -rm -f ./$(DEPDIR)/libhawk_la-std-sed.Plo -rm -f ./$(DEPDIR)/libhawk_la-std.Plo -rm -f ./$(DEPDIR)/libhawk_la-tio.Plo -rm -f ./$(DEPDIR)/libhawk_la-tre-ast.Plo @@ -1486,6 +1506,7 @@ distclean: distclean-am -rm -f ./$(DEPDIR)/libhawk_la-utf16.Plo -rm -f ./$(DEPDIR)/libhawk_la-utf8.Plo -rm -f ./$(DEPDIR)/libhawk_la-utl-ass.Plo + -rm -f ./$(DEPDIR)/libhawk_la-utl-rnd.Plo -rm -f ./$(DEPDIR)/libhawk_la-utl-sort.Plo -rm -f ./$(DEPDIR)/libhawk_la-utl-str.Plo -rm -f ./$(DEPDIR)/libhawk_la-utl-sys.Plo @@ -1575,6 +1596,7 @@ maintainer-clean: maintainer-clean-am -rm -f ./$(DEPDIR)/libhawk_la-sed.Plo -rm -f ./$(DEPDIR)/libhawk_la-sio.Plo -rm -f ./$(DEPDIR)/libhawk_la-skad.Plo + -rm -f ./$(DEPDIR)/libhawk_la-std-sed.Plo -rm -f ./$(DEPDIR)/libhawk_la-std.Plo -rm -f ./$(DEPDIR)/libhawk_la-tio.Plo -rm -f ./$(DEPDIR)/libhawk_la-tre-ast.Plo @@ -1589,6 +1611,7 @@ maintainer-clean: maintainer-clean-am -rm -f ./$(DEPDIR)/libhawk_la-utf16.Plo -rm -f ./$(DEPDIR)/libhawk_la-utf8.Plo -rm -f ./$(DEPDIR)/libhawk_la-utl-ass.Plo + -rm -f ./$(DEPDIR)/libhawk_la-utl-rnd.Plo -rm -f ./$(DEPDIR)/libhawk_la-utl-sort.Plo -rm -f ./$(DEPDIR)/libhawk_la-utl-str.Plo -rm -f ./$(DEPDIR)/libhawk_la-utl-sys.Plo diff --git a/hawk/lib/err.c b/hawk/lib/err.c index bc382caa..af17a949 100644 --- a/hawk/lib/err.c +++ b/hawk/lib/err.c @@ -224,11 +224,6 @@ hawk_errstr_t hawk_geterrstr (hawk_t* hawk) /* ------------------------------------------------------------------------- */ -const hawk_loc_t* hawk_geterrloc (hawk_t* hawk) -{ - return &hawk->_gem.errloc; -} - const hawk_bch_t* hawk_geterrbmsg (hawk_t* hawk) { return hawk_gem_geterrbmsg(hawk_getgem(hawk)); @@ -286,13 +281,13 @@ void hawk_seterruvfmt (hawk_t* hawk, const hawk_loc_t* errloc, hawk_errnum_t err hawk_gem_seterruvfmt (hawk_getgem(hawk), errloc, errnum, errfmt, ap); } -/* ------------------------------------------------------------------------- */ - -const hawk_loc_t* hawk_rtx_geterrloc (hawk_rtx_t* rtx) +void hawk_seterror (hawk_t* hawk, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_oocs_t* errarg) { - return &rtx->_gem.errloc; + hawk_gem_seterror (hawk_getgem(hawk), errloc, errnum, errarg); } +/* ------------------------------------------------------------------------- */ + const hawk_bch_t* hawk_rtx_geterrbmsg (hawk_rtx_t* rtx) { return hawk_gem_geterrbmsg(hawk_rtx_getgem(rtx)); @@ -350,6 +345,11 @@ void hawk_rtx_seterruvfmt (hawk_rtx_t* rtx, const hawk_loc_t* errloc, hawk_errnu hawk_gem_seterruvfmt (hawk_rtx_getgem(rtx), errloc, errnum, errfmt, ap); } +void hawk_rtx_seterror (hawk_rtx_t* rtx, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_oocs_t* errarg) +{ + hawk_gem_seterror (hawk_rtx_getgem(rtx), errloc, errnum, errarg); +} + void hawk_rtx_errortohawk (hawk_rtx_t* rtx, hawk_t* hawk) { /* copy error information in 'rtx' to the 'hawk' object */ @@ -408,8 +408,6 @@ const hawk_uch_t* hawk_gem_geterrumsg (hawk_gem_t* gem) #endif } - - void hawk_gem_seterrnum (hawk_gem_t* gem, const hawk_loc_t* errloc, hawk_errnum_t errnum) { gem->errnum = errnum; @@ -550,7 +548,7 @@ void hawk_gem_seterruvfmt (hawk_gem_t* gem, const hawk_loc_t* errloc, hawk_errnu gem->errloc = (errloc? *errloc: _nullloc); } -void hawk_gem_seterror (hawk_gem_t* gem, hawk_errnum_t errnum, const hawk_oocs_t* errarg, const hawk_loc_t* errloc) +void hawk_gem_seterror (hawk_gem_t* gem, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_oocs_t* errarg) { const hawk_ooch_t* errfmt; @@ -559,7 +557,7 @@ void hawk_gem_seterror (hawk_gem_t* gem, hawk_errnum_t errnum, const hawk_oocs_t errfmt = gem->errstr(gem->errnum); HAWK_ASSERT (errfmt != HAWK_NULL); - hawk_copy_oocses_to_oochars (gem->errmsg, HAWK_COUNTOF(gem->errmsg), errfmt, errarg); + hawk_copy_oofcs_to_oochars (gem->errmsg, HAWK_COUNTOF(gem->errmsg), errfmt, errarg); if (errloc != HAWK_NULL) gem->errloc = *errloc; else HAWK_MEMSET (&gem->errloc, 0, HAWK_SIZEOF(gem->errloc)); diff --git a/hawk/lib/fio.c b/hawk/lib/fio.c index cc9fea32..a9d59acc 100644 --- a/hawk/lib/fio.c +++ b/hawk/lib/fio.c @@ -140,6 +140,11 @@ int hawk_fio_init (hawk_fio_t* fio, hawk_gem_t* gem, const hawk_ooch_t* path, in { hawk_fio_hnd_t handle; + hawk_uint32_t temp_no; + hawk_ooch_t* temp_ptr; + hawk_bch_t* temp_ptr_b; + hawk_oow_t temp_tries; + #if defined(_WIN32) int fellback = 0; #endif @@ -180,6 +185,102 @@ int hawk_fio_init (hawk_fio_t* fio, hawk_gem_t* gem, const hawk_ooch_t* path, in if (flags & HAWK_FIO_NOCLOSE) fio->status |= STATUS_NOCLOSE; + if (flags & HAWK_FIO_TEMPORARY) + { + hawk_ntime_t now; + + /*if (flags & (HAWK_FIO_HANDLE | HAWK_FIO_BCSTRPATH))*/ + if (flags & HAWK_FIO_HANDLE) + { + /* HAWK_FIO_TEMPORARY and HAWK_FIO_HANDLE/HAWK_FIO_BCSTRPATH + * are mutually exclusive */ + hawk_gem_seterrnum (fio->gem, HAWK_NULL, HAWK_EINVAL); + return -1; + } + + temp_no = 0; + + if (flags & HAWK_FIO_BCSTRPATH) + { + + for (temp_ptr_b = (hawk_bch_t*)path; *temp_ptr_b; temp_ptr_b++) + temp_no += *temp_ptr_b; + + if (temp_ptr_b - (hawk_bch_t*)path < 4) + { + hawk_gem_seterrnum (fio->gem, HAWK_NULL, HAWK_EINVAL); + return -1; + } + + temp_ptr_b -= 4; + } + else + { + /* if HAWK_FIO_TEMPORARY is used, the path name must be writable. */ + for (temp_ptr = (hawk_ooch_t*)path; *temp_ptr; temp_ptr++) + temp_no += *temp_ptr; + + /* The path name template must be at least 4 characters long + * excluding the terminating null. this function fails if not */ + if (temp_ptr - path < 4) + { + hawk_gem_seterrnum (fio->gem, HAWK_NULL, HAWK_EINVAL); + return -1; + } + + temp_ptr -= 4; + } + + hawk_get_ntime (&now); + temp_no += (now.sec & 0xFFFFFFFFlu); + + temp_tries = 0; + + retry_temporary: + temp_tries++; + + /* Fails after 5000 tries. 5000 randomly chosen */ + if (temp_tries > 5000) + { + hawk_gem_seterrnum (fio->gem, HAWK_NULL, HAWK_EINVAL); + return -1; + } + + /* Generate the next random number to use to make a + * new path name */ + temp_no = hawk_rand31(temp_no); + + /* + * You must not pass a constant string for a path name + * when HAWK_FIO_TEMPORARY is set, because it changes + * the path name with a random number generated + */ + if (flags & HAWK_FIO_BCSTRPATH) + { + hawk_fmt_uintmax_to_bcstr ( + temp_ptr_b, + 4, + temp_no % 0x10000, + 16 | HAWK_FMT_UINTMAX_NOTRUNC | HAWK_FMT_UINTMAX_NONULL, + 4, + '\0', + HAWK_NULL + ); + } + else + { + hawk_fmt_uintmax_to_oocstr ( + temp_ptr, + 4, + temp_no % 0x10000, + 16 | HAWK_FMT_UINTMAX_NOTRUNC | HAWK_FMT_UINTMAX_NONULL, + 4, + HAWK_T('\0'), + HAWK_NULL + ); + } + } + #if defined(_WIN32) if (flags & HAWK_FIO_HANDLE) { @@ -256,7 +357,7 @@ int hawk_fio_init (hawk_fio_t* fio, hawk_gem_t* gem, const hawk_ooch_t* path, in if (flags & HAWK_FIO_SEQUENTIAL) flag_and_attr |= FILE_FLAG_SEQUENTIAL_SCAN; - if (flags & HAWK_FIO_MBSPATH) + if (flags & HAWK_FIO_BCSTRPATH) { handle = CreateFileA( (const hawk_bch_t*)path, desired_access, share_mode, @@ -291,7 +392,7 @@ int hawk_fio_init (hawk_fio_t* fio, hawk_gem_t* gem, const hawk_ooch_t* path, in desired_access |= GENERIC_WRITE; } - if (flags & HAWK_FIO_MBSPATH) + if (flags & HAWK_FIO_BCSTRPATH) { handle = CreateFileA( (const hawk_bch_t*)path, desired_access, share_mode, @@ -309,12 +410,14 @@ int hawk_fio_init (hawk_fio_t* fio, hawk_gem_t* gem, const hawk_ooch_t* path, in } if (handle == INVALID_HANDLE_VALUE) { + i if (flags & HAWK_FIO_TEMPORARY) goto retry_temporary; hawk_gem_seterrnum (fio->gem, HAWK_NULL, hawk_syserr_to_errnum(GetLastError())); return -1; } } else { + if (flags & HAWK_FIO_TEMPORARY) goto retry_temporary; hawk_gem_seterrnum (fio->gem, HAWK_NULL, hawk_syserr_to_errnum(e)); return -1; } @@ -353,7 +456,7 @@ int hawk_fio_init (hawk_fio_t* fio, hawk_gem_t* gem, const hawk_ooch_t* path, in hawk_oow_t wl, ml; int px; - if (flags & HAWK_FIO_MBSPATH) + if (flags & HAWK_FIO_BCSTRPATH) { path_mb = (hawk_bch_t*)path; } @@ -460,6 +563,7 @@ int hawk_fio_init (hawk_fio_t* fio, hawk_gem_t* gem, const hawk_ooch_t* path, in if (ret != NO_ERROR) { + if (flags & HAWK_FIO_TEMPORARY) goto retry_temporary; hawk_gem_seterrnum (fio->gem, HAWK_NULL, hawk_syserr_to_errnum(ret)); return -1; } @@ -486,7 +590,7 @@ int hawk_fio_init (hawk_fio_t* fio, hawk_gem_t* gem, const hawk_ooch_t* path, in hawk_oow_t wl, ml; int px; - if (flags & HAWK_FIO_MBSPATH) + if (flags & HAWK_FIO_BCSTRPATH) { path_mb = (hawk_bch_t*)path; } @@ -546,6 +650,7 @@ int hawk_fio_init (hawk_fio_t* fio, hawk_gem_t* gem, const hawk_ooch_t* path, in if (handle <= -1) { + if (flags & HAWK_FIO_TEMPORARY) goto retry_temporary; hawk_gem_seterrnum (fio->gem, HAWK_NULL, hawk_syserr_to_errnum(errno)); return -1; } @@ -573,7 +678,7 @@ int hawk_fio_init (hawk_fio_t* fio, hawk_gem_t* gem, const hawk_ooch_t* path, in hawk_oow_t wl, ml; int px; - if (flags & HAWK_FIO_MBSPATH) + if (flags & HAWK_FIO_BCSTRPATH) { path_mb = (hawk_bch_t*)path; } @@ -695,7 +800,7 @@ int hawk_fio_init (hawk_fio_t* fio, hawk_gem_t* gem, const hawk_ooch_t* path, in hawk_oow_t wl, ml; int px; - if (flags & HAWK_FIO_MBSPATH) + if (flags & HAWK_FIO_BCSTRPATH) { path_mb = (hawk_bch_t*)path; } @@ -768,6 +873,7 @@ int hawk_fio_init (hawk_fio_t* fio, hawk_gem_t* gem, const hawk_ooch_t* path, in #endif if (handle == -1) { + if (flags & HAWK_FIO_TEMPORARY) goto retry_temporary; hawk_gem_seterrnum (fio->gem, HAWK_NULL, hawk_syserr_to_errnum(errno)); return -1; } diff --git a/hawk/lib/hawk-cfg.h.in b/hawk/lib/hawk-cfg.h.in index 47e62651..938ca2b6 100644 --- a/hawk/lib/hawk-cfg.h.in +++ b/hawk/lib/hawk-cfg.h.in @@ -169,6 +169,9 @@ /* libffi library is available */ #undef HAVE_FFI_LIB +/* Define to 1 if you have the `ffi_prep_cif_var' function. */ +#undef HAVE_FFI_PREP_CIF_VAR + /* Define to 1 if you have the `floor' function. */ #undef HAVE_FLOOR diff --git a/hawk/lib/hawk-fio.h b/hawk/lib/hawk-fio.h index 34519895..455f65a2 100644 --- a/hawk/lib/hawk-fio.h +++ b/hawk/lib/hawk-fio.h @@ -38,17 +38,15 @@ enum hawk_fio_flag_t /** treat the file name pointer as a handle pointer */ HAWK_FIO_HANDLE = (1 << 8), -#if 0 /** treat the file name pointer as a pointer to file name * template to use when making a temporary file name */ HAWK_FIO_TEMPORARY = (1 << 9), -#endif /** don't close an I/O handle in hawk_fio_fini() and hawk_fio_close() */ HAWK_FIO_NOCLOSE = (1 << 10), /** treat the path name as a multi-byte string */ - HAWK_FIO_MBSPATH = (1 << 11), + HAWK_FIO_BCSTRPATH = (1 << 11), /* normal open flags */ HAWK_FIO_READ = (1 << 14), diff --git a/hawk/lib/hawk-gem.h b/hawk/lib/hawk-gem.h index 53d568a3..aa79fa42 100644 --- a/hawk/lib/hawk-gem.h +++ b/hawk/lib/hawk-gem.h @@ -164,7 +164,7 @@ HAWK_EXPORT hawk_uch_t* hawk_gem_dupucstrarr ( HAWK_EXPORT hawk_bch_t* hawk_gem_dupbcstrarr ( hawk_gem_t* gem, const hawk_bch_t* str[], - hawk_oow_t* len + hawk_oow_t* len ); #if defined(HAWK_OOCH_IS_UCH) @@ -435,8 +435,10 @@ HAWK_EXPORT int hawk_gem_getifcfg ( #if defined(HAWK_HAVE_INLINE) static HAWK_INLINE hawk_errnum_t hawk_gem_geterrnum (hawk_gem_t* gem) { return gem->errnum; } +static HAWK_INLINE const hawk_loc_t* hawk_gem_geterrloc (hawk_gem_t* gem) { return &gem->errloc; } #else #define hawk_gem_geterrnum(gem) (((hawk_gem_t*)(gem))->errnum) +#define hawk_gem_geterrloc(gem) (&((hawk_gem_t*)(gem))->errloc) #endif HAWK_EXPORT void hawk_gem_geterrinf ( @@ -464,6 +466,13 @@ HAWK_EXPORT void hawk_gem_seterrinf ( const hawk_errinf_t* errinf ); +HAWK_EXPORT void hawk_gem_seterror ( + hawk_gem_t* gem, + const hawk_loc_t* errloc, + hawk_errnum_t errnum, + const hawk_oocs_t* errarg +); + HAWK_EXPORT void hawk_gem_seterrnum ( hawk_gem_t* gem, const hawk_loc_t* errloc, diff --git a/hawk/lib/hawk-sed.h b/hawk/lib/hawk-sed.h index 9767ad09..31f78b8f 100644 --- a/hawk/lib/hawk-sed.h +++ b/hawk/lib/hawk-sed.h @@ -29,6 +29,7 @@ #include #include +#include /** @file * This file defines data types and functions to use for creating a custom @@ -353,6 +354,92 @@ enum hawk_sed_space_t }; typedef enum hawk_sed_space_t hawk_sed_space_t; + +/* ------------------------------------------------------------------------ */ + +/** + * This section defines easier-to-use helper interface for a stream editor. + * If you don't care about the details of memory management and I/O handling, + * you can choose to use the helper functions provided here. It is + * a higher-level interface that is easier to use as it implements + * default handlers for I/O and memory management. + */ + +/** + * The hawk_sed_iostd_type_t type defines types of standard + * I/O resources. + */ +enum hawk_sed_iostd_type_t +{ + HAWK_SED_IOSTD_NULL, /**< null resource type */ + HAWK_SED_IOSTD_FILE, /**< file */ + HAWK_SED_IOSTD_FILEB, /**< file */ + HAWK_SED_IOSTD_FILEU, /**< file */ + HAWK_SED_IOSTD_STR, /**< string */ + HAWK_SED_IOSTD_SIO /**< sio */ +}; +typedef enum hawk_sed_iostd_type_t hawk_sed_iostd_type_t; + +/** + * The hawk_sed_iostd_t type defines a standard I/O resource. + */ +struct hawk_sed_iostd_t +{ + /** resource type */ + hawk_sed_iostd_type_t type; + + /** union describing the resource of the specified type */ + union + { + /** file path with character encoding */ + struct + { + /** file path to open. #HAWK_NULL or '-' for stdin/stdout. */ + const hawk_ooch_t* path; + /** a stream created with the file path is set with this + * cmgr if it is not #HAWK_NULL. */ + hawk_cmgr_t* cmgr; + } file; + + struct + { + const hawk_bch_t* path; + hawk_cmgr_t* cmgr; + } fileb; + + struct + { + const hawk_uch_t* path; + hawk_cmgr_t* cmgr; + } fileu; + + /** + * input string or dynamically allocated output string + * + * For input, the ptr and the len field of str indicates the + * pointer and the length of a string to read. You must set + * these two fields before calling hawk_sed_execstd(). + * + * For output, the ptr and the len field of str indicates the + * pointer and the length of produced output. The output + * string is dynamically allocated. You don't need to set these + * fields before calling hawk_sed_execstd() because they are + * set by hawk_sed_execstd() and valid while the relevant sed + * object is alive. You must free the memory chunk pointed to by + * the ptr field with hawk_sed_freemem() once you're done with it + * to avoid memory leaks. + */ + hawk_oocs_t str; + + /** pre-opened sio stream */ + hawk_sio_t* sio; + } u; +}; + +typedef struct hawk_sed_iostd_t hawk_sed_iostd_t; + +/* ------------------------------------------------------------------------ */ + #if defined(__cplusplus) extern "C" { #endif @@ -370,6 +457,7 @@ extern "C" { HAWK_EXPORT hawk_sed_t* hawk_sed_open ( hawk_mmgr_t* mmgr, /**< memory manager */ hawk_oow_t xtnsize, /**< extension size in bytes */ + hawk_cmgr_t* cmgr, hawk_errnum_t* errnum ); @@ -448,27 +536,17 @@ HAWK_EXPORT int hawk_sed_setopt ( */ #if defined(HAWK_HAVE_INLINE) -static HAWK_INLINE hawk_errnum_t hawk_sed_geterrnum (hawk_sed_t* sed) { return ((hawk_sed_alt_t*)sed)->_gem.errnum; } +static HAWK_INLINE hawk_errnum_t hawk_sed_geterrnum (hawk_sed_t* sed) { return hawk_gem_geterrnum(hawk_sed_getgem(sed)); } +static HAWK_INLINE const hawk_loc_t* hawk_sed_geterrloc (hawk_sed_t* sed) { return hawk_gem_geterrloc(hawk_sed_getgem(sed)); } +static HAWK_INLINE const hawk_bch_t* hawk_sed_geterrbmsg (hawk_sed_t* sed) { return hawk_gem_geterrbmsg(hawk_sed_getgem(sed)); } +static HAWK_INLINE const hawk_uch_t* hawk_sed_geterrumsg (hawk_sed_t* sed) { return hawk_gem_geterrumsg(hawk_sed_getgem(sed)); } #else -# define hawk_sed_geterrnum(sed) (((hawk_sed_alt_t*)(sed))->_gem.errnum) +#define hawk_sed_geterrnum(sed) hawk_gem_geterrnum(hawk_sed_getgem(sed)) +#define hawk_sed_geterrloc(sed) hawk_gem_geterrloc(hawk_sed_getgem(sed)) +#define hawk_sed_geterrbmsg(sed) hawk_gem_geterrbmsg(hawk_sed_getgem(sed)) +#define hawk_sed_geterrumsg(sed) hawk_gem_geterrumsg(hawk_sed_getgem(sed)) #endif -/** - * The hawk_sed_geterrloc() function gets the location where the last error - * has occurred. - * @return error location - */ -HAWK_EXPORT const hawk_loc_t* hawk_sed_geterrloc ( - hawk_sed_t* sed /**< stream editor */ -); - -/** - * The hawk_sed_geterrmsg() function gets a string describing the last error. - * @return error message pointer - */ -HAWK_EXPORT const hawk_ooch_t* hawk_sed_geterrmsg ( - hawk_sed_t* sed /**< stream editor */ -); /** * The hawk_sed_geterror() function gets an error number, an error location, @@ -490,33 +568,12 @@ HAWK_EXPORT void hawk_sed_geterror ( */ #if defined(HAWK_HAVE_INLINE) static HAWK_INLINE void hawk_sed_seterrnum (hawk_sed_t* sed, const hawk_loc_t* errloc, hawk_errnum_t errnum) { hawk_gem_seterrnum (hawk_sed_getgem(sed), errloc, errnum); } +static HAWK_INLINE void hawk_sed_seterror (hawk_sed_t* sed, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_oocs_t* errarg) { hawk_gem_seterror(hawk_sed_getgem(sed), errloc, errnum, errarg); } #else -#define hawk_sed_seterrnum(sed, errloc, errnum) hawk_sed_gem_seterrnum(hawk_sed_getgem(sed), errloc, errnum) +#define hawk_sed_seterrnum(sed, errloc, errnum) hawk_gem_seterrnum(hawk_sed_getgem(sed), errloc, errnum) +#define hawk_set_seterror(sed, errloc, errnum, errarg) hawk_sed_seterror(hawk_sed_getgem(sed), errloc, errnum, errarg) #endif -/** - * The hawk_sed_seterrmsg() function sets error information with a customized - * message for a given error number. - */ -HAWK_EXPORT void hawk_sed_seterrmsg ( - hawk_sed_t* sed, /**< stream editor */ - hawk_errnum_t errnum, /**< error number */ - const hawk_ooch_t* errmsg, /**< error message */ - const hawk_loc_t* errloc /**< error location */ -); - -/** - * The hawk_sed_seterror() function sets an error number, an error location, and - * an error message. An error string is composed of a formatting string - * and an array of formatting parameters. - */ -HAWK_EXPORT void hawk_sed_seterror ( - hawk_sed_t* sed, /**< stream editor */ - hawk_errnum_t errnum, /**< error number */ - const hawk_oocs_t* errarg, /**< array of arguments for formatting an error message */ - const hawk_loc_t* errloc /**< error location */ -); - /** * The hawk_sed_popecb() function pops an sed event callback set * and returns the pointer to it. If no callback set can be popped, @@ -654,6 +711,151 @@ HAWK_EXPORT void hawk_sed_getspace ( hawk_oocs_t* str ); +/* ------------------------------------------------------------------------ */ + +/** + * The hawk_sed_openstd() function creates a stream editor with the default + * memory manager and initializes it. + * \return pointer to a stream editor on success, #HAWK_NULL on failure. + */ +HAWK_EXPORT hawk_sed_t* hawk_sed_openstd ( + hawk_oow_t xtnsize, /**< extension size in bytes */ + hawk_errnum_t* errnum +); + +/** + * The hawk_sed_openstdwithmmgr() function creates a stream editor with a + * user-defined memory manager. It is equivalent to hawk_sed_openstd(), + * except that you can specify your own memory manager. + * \return pointer to a stream editor on success, #HAWK_NULL on failure. + */ +HAWK_EXPORT hawk_sed_t* hawk_sed_openstdwithmmgr ( + hawk_mmgr_t* mmgr, /**< memory manager */ + hawk_oow_t xtnsize, /**< extension size in bytes */ + hawk_cmgr_t* cmgr, + hawk_errnum_t* errnum +); + +/** + * The hawk_sed_compstd() function compiles sed scripts specified in + * an array of stream resources. The end of the array is indicated + * by an element whose type is #HAWK_SED_IOSTD_NULL. However, the type + * of the first element shall not be #HAWK_SED_IOSTD_NULL. The output + * parameter \a count is set to the count of stream resources + * opened on both success and failure. You can pass #HAWK_NULL to \a + * count if the count is not needed. + * + * \return 0 on success, -1 on failure + */ +HAWK_EXPORT int hawk_sed_compstd ( + hawk_sed_t* sed, /**< stream editor */ + hawk_sed_iostd_t in[], /**< input scripts */ + hawk_oow_t* count /**< number of input scripts opened */ +); + +/** + * The hawk_sed_compstdfile() function compiles a sed script from + * a single file \a infile. If \a infile is #HAWK_NULL, it reads + * the script from the standard input. + * When #HAWK_OOCH_IS_UCH is defined, it converts the multibyte + * sequences in the file \a infile to wide characters via the + * #hawk_cmgr_t interface \a cmgr. If \a cmgr is #HAWK_NULL, it uses + * the default interface. It calls cmgr->mbtowc() for conversion. + * \return 0 on success, -1 on failure + */ +HAWK_EXPORT int hawk_sed_compstdfile ( + hawk_sed_t* sed, + const hawk_ooch_t* infile, + hawk_cmgr_t* cmgr +); + +HAWK_EXPORT int hawk_sed_compstdfileb ( + hawk_sed_t* sed, + const hawk_bch_t* infile, + hawk_cmgr_t* cmgr +); + +HAWK_EXPORT int hawk_sed_compstdfileu ( + hawk_sed_t* sed, + const hawk_uch_t* infile, + hawk_cmgr_t* cmgr +); + +/** + * The hawk_sed_compstdstr() function compiles a sed script stored + * in a null-terminated string pointed to by \a script. + * \return 0 on success, -1 on failure + */ +HAWK_EXPORT int hawk_sed_compstdstr ( + hawk_sed_t* sed, + const hawk_ooch_t* script +); + +/** + * The hawk_sed_compstdxstr() function compiles a sed script of the + * length \a script->len pointed to by \a script->ptr. + * \return 0 on success, -1 on failure + */ +HAWK_EXPORT int hawk_sed_compstdxstr ( + hawk_sed_t* sed, + const hawk_oocs_t* script +); + +/** + * The hawk_sed_execstd() function executes a compiled script + * over input streams \a in and an output stream \a out. + * + * If \a in is not #HAWK_NULL, it must point to an array of stream + * resources whose end is indicated by an element with #HAWK_SED_IOSTD_NULL + * type. However, the type of the first element \a in[0].type show not + * be #HAWK_SED_IOSTD_NULL. It requires at least 1 valid resource to be + * included in the array. + * + * If \a in is #HAWK_NULL, the standard console input is used. + * If \a out is #HAWK_NULL, the standard console output is used. + * + * \return 0 on success, -1 on failure + */ +HAWK_EXPORT int hawk_sed_execstd ( + hawk_sed_t* sed, + hawk_sed_iostd_t in[], + hawk_sed_iostd_t* out +); + +/** + * The hawk_sed_execstdfile() function executes a compiled script + * over a single input file \a infile and a single output file \a + * outfile. + * + * If \a infile is #HAWK_NULL, the standard console input is used. + * If \a outfile is #HAWK_NULL, the standard console output is used. + * + * \return 0 on success, -1 on failure + */ +HAWK_EXPORT int hawk_sed_execstdfile ( + hawk_sed_t* sed, + const hawk_ooch_t* infile, + const hawk_ooch_t* outfile, + hawk_cmgr_t* cmgr +); + + +/** + * The hawk_sed_execstdfile() function executes a compiled script + * over a single input string \a instr and a dynamically allocated buffer. + * It copies the buffer pointer and length to the location pointed to + * by \a outstr on success. The buffer pointer copied to \a outstr + * must be released with hawk_sed_freemem(). + * + * \return 0 on success, -1 on failure + */ +HAWK_EXPORT int hawk_sed_execstdxstr ( + hawk_sed_t* sed, + const hawk_oocs_t* instr, + hawk_oocs_t* outstr, + hawk_cmgr_t* cmgr +); + #if defined(__cplusplus) } #endif diff --git a/hawk/lib/hawk-sio.h b/hawk/lib/hawk-sio.h index eae0e09a..6b3d35b6 100644 --- a/hawk/lib/hawk-sio.h +++ b/hawk/lib/hawk-sio.h @@ -48,7 +48,9 @@ enum hawk_sio_flag_t /* ensure that the following enumerators are one of * hawk_fio_flags_t enumerators */ HAWK_SIO_HANDLE = HAWK_FIO_HANDLE, + HAWK_SIO_TEMPORARY = HAWK_FIO_TEMPORARY, HAWK_SIO_NOCLOSE = HAWK_FIO_NOCLOSE, + HAWK_SIO_BCSTRPATH = HAWK_FIO_BCSTRPATH, HAWK_SIO_READ = HAWK_FIO_READ, HAWK_SIO_WRITE = HAWK_FIO_WRITE, HAWK_SIO_APPEND = HAWK_FIO_APPEND, diff --git a/hawk/lib/hawk-utl.h b/hawk/lib/hawk-utl.h index 7f197d78..a64926b0 100644 --- a/hawk/lib/hawk-utl.h +++ b/hawk/lib/hawk-utl.h @@ -567,6 +567,20 @@ HAWK_EXPORT hawk_oow_t hawk_copy_bcstr_unlimited ( const hawk_bch_t* src ); +HAWK_EXPORT hawk_oow_t hawk_copy_ufcs_to_uchars ( + hawk_uch_t* buf, + hawk_oow_t bsz, + const hawk_uch_t* fmt, + const hawk_ucs_t str[] +); + +HAWK_EXPORT hawk_oow_t hawk_copy_bfcs_to_bchars ( + hawk_bch_t* buf, + hawk_oow_t bsz, + const hawk_bch_t* fmt, + const hawk_bcs_t str[] +); + HAWK_EXPORT hawk_oow_t hawk_count_ucstr ( const hawk_uch_t* str ); @@ -585,7 +599,6 @@ HAWK_EXPORT hawk_oow_t hawk_count_bcstr_limited ( hawk_oow_t maxlen ); - HAWK_EXPORT void hawk_fill_uchars ( hawk_uch_t* dst, const hawk_uch_t ch, @@ -810,6 +823,8 @@ HAWK_EXPORT hawk_oow_t hawk_subst_for_bcstr_to_bcstr ( # define hawk_copy_oocstr hawk_copy_ucstr # define hawk_copy_oocstr_unlimited hawk_copy_ucstr_unlimited +# define hawk_copy_oofcs_to_oochars hawk_copy_ufcs_to_uchars + # define hawk_count_oocstr hawk_count_ucstr # define hawk_count_oocstr_limited hawk_count_ucstr_limited @@ -853,6 +868,8 @@ HAWK_EXPORT hawk_oow_t hawk_subst_for_bcstr_to_bcstr ( # define hawk_copy_oocstr hawk_copy_bcstr # define hawk_copy_oocstr_unlimited hawk_copy_bcstr_unlimited +# define hawk_copy_oofcs_to_oochars hawk_copy_bfcs_to_bchars + # define hawk_count_oocstr hawk_count_bcstr # define hawk_count_oocstr_limited hawk_count_bcstr_limited @@ -1651,6 +1668,12 @@ HAWK_EXPORT void hawk_sub_ntime ( const hawk_ntime_t* y ); +/* ========================================================================= + * RANDOM NUMBER GENERATOR + * ========================================================================= */ +HAWK_EXPORT hawk_uint32_t hawk_rand31 ( + hawk_uint32_t seed +); /* ========================================================================= * ASSERTION diff --git a/hawk/lib/hawk.h b/hawk/lib/hawk.h index 10a083d3..d3fca711 100644 --- a/hawk/lib/hawk.h +++ b/hawk/lib/hawk.h @@ -1584,19 +1584,18 @@ HAWK_EXPORT hawk_errstr_t hawk_geterrstr ( * \return error number */ -#if defined(HAWK_HAVE_INLINE) -static HAWK_INLINE hawk_errnum_t hawk_geterrnum (hawk_t* hawk) { return ((hawk_alt_t*)hawk)->_gem.errnum; } -#else -# define hawk_geterrnum(hawk) (((hawk_alt_t*)(hawk))->_gem.errnum) -#endif - /** * The hawk_geterrloc() function returns the location where the * last error has occurred. */ -HAWK_EXPORT const hawk_loc_t* hawk_geterrloc ( - hawk_t* hawk /**< hawk */ -); + +#if defined(HAWK_HAVE_INLINE) +static HAWK_INLINE hawk_errnum_t hawk_geterrnum (hawk_t* hawk) { return ((hawk_alt_t*)hawk)->_gem.errnum; } +static HAWK_INLINE const hawk_loc_t* hawk_geterrloc (hawk_t* hawk) { return hawk_gem_geterrloc(hawk_getgem(hawk)); } +#else +# define hawk_geterrnum(hawk) (((hawk_alt_t*)(hawk))->_gem.errnum) +# define hawk_geterrloc(hawk) (hawk_gem_geterrloc(hawk_getgem(hawk))) +#endif /** * The hawk_geterrbmsg() function returns the error message describing @@ -1703,6 +1702,13 @@ HAWK_EXPORT void hawk_seterrinf ( ); +HAWK_EXPORT void hawk_seterror ( + hawk_t* hawk, + const hawk_loc_t* errloc, + hawk_errnum_t errnum, + const hawk_oocs_t* errarg +); + /** * The hawk_geterror() function gets error information via parameters. */ @@ -2566,20 +2572,19 @@ HAWK_EXPORT hawk_htb_t* hawk_rtx_getnvmap ( * occurred during runtime. * \return error number */ -#if defined(HAWK_HAVE_INLINE) -static HAWK_INLINE hawk_errnum_t hawk_rtx_geterrnum (hawk_rtx_t* rtx) { return ((hawk_rtx_alt_t*)rtx)->_gem.errnum; } -#else -# define hawk_rtx_geterrnum(hawk) (((hawk_rtx_alt_t*)(rtx))->_gem.errnum) -#endif /** * The hawk_rtx_geterrloc() function gets the location of the last error * occurred during runtime. The * \return error location */ -HAWK_EXPORT const hawk_loc_t* hawk_rtx_geterrloc ( - hawk_rtx_t* rtx /**< runtime context */ -); +#if defined(HAWK_HAVE_INLINE) +static HAWK_INLINE hawk_errnum_t hawk_rtx_geterrnum (hawk_rtx_t* rtx) { return ((hawk_rtx_alt_t*)rtx)->_gem.errnum; } +static HAWK_INLINE const hawk_loc_t* hawk_rtx_geterrloc (hawk_rtx_t* rtx) { return hawk_gem_geterrloc(hawk_rtx_getgem(rtx)); } +#else +# define hawk_rtx_geterrnum(rtx) (((hawk_rtx_alt_t*)(rtx))->_gem.errnum) +# define hawk_rtx_geterrloc(rtx) (hawk_gem_geterrloc(hawk_rtx_getgem(rtx))) +#endif /** * The hawk_rtx_geterrbmsg() function gets the string describing the last @@ -2645,6 +2650,13 @@ HAWK_EXPORT void hawk_rtx_seterrinf ( const hawk_errinf_t* errinf /**< error information */ ); +HAWK_EXPORT void hawk_rtx_seterror ( + hawk_rtx_t* rtx, + const hawk_loc_t* errloc, + hawk_errnum_t errnum, + const hawk_oocs_t* errarg +); + HAWK_EXPORT void hawk_rtx_seterrbfmt ( hawk_rtx_t* rtx, const hawk_loc_t* errloc, diff --git a/hawk/lib/sed-prv.h b/hawk/lib/sed-prv.h index 71d09f20..d86a3615 100644 --- a/hawk/lib/sed-prv.h +++ b/hawk/lib/sed-prv.h @@ -222,8 +222,9 @@ extern "C" { #endif int hawk_sed_init ( - hawk_sed_t* sed, - hawk_mmgr_t* mmgr + hawk_sed_t* sed, + hawk_mmgr_t* mmgr, + hawk_cmgr_t* cmgr ); void hawk_sed_fini ( diff --git a/hawk/lib/sed.c b/hawk/lib/sed.c index b89a5e21..edb4f0a5 100644 --- a/hawk/lib/sed.c +++ b/hawk/lib/sed.c @@ -51,14 +51,14 @@ do { \ static void free_all_cut_selector_blocks (hawk_sed_t* sed, hawk_sed_cmd_t* cmd); -hawk_sed_t* hawk_sed_open (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, hawk_errnum_t* errnum) +hawk_sed_t* hawk_sed_open (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, hawk_cmgr_t* cmgr, hawk_errnum_t* errnum) { hawk_sed_t* sed; sed = (hawk_sed_t*)HAWK_MMGR_ALLOC(mmgr, HAWK_SIZEOF(hawk_sed_t) + xtnsize); if (HAWK_LIKELY(sed)) { - if (hawk_sed_init(sed, mmgr) <= -1) + if (hawk_sed_init(sed, mmgr, cmgr) <= -1) { if (errnum) *errnum = hawk_sed_geterrnum(sed); HAWK_MMGR_FREE (mmgr, sed); @@ -82,13 +82,13 @@ void hawk_sed_close (hawk_sed_t* sed) HAWK_MMGR_FREE (hawk_sed_getmmgr(sed), sed); } -int hawk_sed_init (hawk_sed_t* sed, hawk_mmgr_t* mmgr) +int hawk_sed_init (hawk_sed_t* sed, hawk_mmgr_t* mmgr, hawk_cmgr_t* cmgr) { HAWK_MEMSET (sed, 0, HAWK_SIZEOF(*sed)); sed->_instsize = HAWK_SIZEOF(*sed); sed->_gem.mmgr = mmgr; - sed->_gem.cmgr = HAWK_NULL; /* no cmgr used */ + sed->_gem.cmgr = cmgr; /* initialize error handling fields */ sed->_gem.errnum = HAWK_ENOERR; @@ -1889,7 +1889,7 @@ int hawk_sed_comp (hawk_sed_t* sed, hawk_sed_io_impl_t inf) /* process the first address */ a1_loc = sed->src.loc; - if (get_address (sed, &cmd->a1, 0) == HAWK_NULL) + if (get_address(sed, &cmd->a1, 0) == HAWK_NULL) { cmd = HAWK_NULL; hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EA1MOI); diff --git a/hawk/lib/sio.c b/hawk/lib/sio.c index 1e92e775..d1e1bb2c 100644 --- a/hawk/lib/sio.c +++ b/hawk/lib/sio.c @@ -135,7 +135,22 @@ int hawk_sio_init (hawk_sio_t* sio, hawk_gem_t* gem, const hawk_ooch_t* path, in if ((flags & HAWK_SIO_KEEPPATH) && !(flags & HAWK_SIO_HANDLE)) { + #if defined(HAWK_OOCH_IS_BCH) sio->path = hawk_gem_dupoocstr(gem, path, HAWK_NULL); + #else + if (flags & HAWK_SIO_BCSTRPATH) + { + /* the stored path is always of the hawk_ooch_t type. + * and the conversion uses the cmgr set on the gem object. + * note that cmgr for the actual file content can be set + * with hawk_sio_setcmgr(). */ + sio->path = hawk_gem_dupbtoucstr(gem, (const hawk_bch_t*)path, HAWK_NULL, 1); + } + else + { + sio->path = hawk_gem_dupoocstr(gem, path, HAWK_NULL); + } + #endif if (sio->path == HAWK_NULL) goto oops02; } @@ -173,7 +188,7 @@ int hawk_sio_initstd (hawk_sio_t* sio, hawk_gem_t* gem, hawk_sio_std_t std, int int n; hawk_fio_hnd_t hnd; - if (hawk_get_std_fio_handle (std, &hnd) <= -1) return -1; + if (hawk_get_std_fio_handle(std, &hnd) <= -1) return -1; n = hawk_sio_init(sio, gem, (const hawk_ooch_t*)&hnd, flags | HAWK_SIO_HANDLE | HAWK_SIO_NOCLOSE); @@ -219,7 +234,9 @@ hawk_sio_hnd_t hawk_sio_gethnd (const hawk_sio_t* sio) const hawk_ooch_t* hawk_sio_getpath (hawk_sio_t* sio) { - /* this path is valid if HAWK_SIO_HANDLE is off and HAWK_SIO_KEEPPATH is on */ + /* this path is valid if HAWK_SIO_HANDLE is off and HAWK_SIO_KEEPPATH is on. + * HAWK_SIO_BCSTRPATH doesn't affect this value. The opening side ensures it + * to be in the hawk_ooch_type. */ return sio->path; } diff --git a/hawk/lib/std-sed.c b/hawk/lib/std-sed.c new file mode 100644 index 00000000..d83e3dd8 --- /dev/null +++ b/hawk/lib/std-sed.c @@ -0,0 +1,1059 @@ +/* + * $Id$ + * + Copyright (c) 2006-2020 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 "sed-prv.h" +#include "hawk-prv.h" +#include "hawk-std.h" + +typedef struct xtn_in_t xtn_in_t; +struct xtn_in_t +{ + hawk_sed_iostd_t* ptr; + hawk_sed_iostd_t* cur; + hawk_oow_t mempos; +}; + +typedef struct xtn_out_t xtn_out_t; +struct xtn_out_t +{ + hawk_sed_iostd_t* ptr; + hawk_ooecs_t* memstr; +}; + +typedef struct xtn_t xtn_t; +struct xtn_t +{ + struct + { + xtn_in_t in; + hawk_ooch_t last; + int newline_squeezed; + } s; + struct + { + xtn_in_t in; + xtn_out_t out; + } e; + + hawk_link_t* sio_names; +}; + +#if defined(HAWK_HAVE_INLINE) +static HAWK_INLINE xtn_t* GET_XTN(hawk_sed_t* sed) { return (xtn_t*)((hawk_uint8_t*)hawk_sed_getxtn(sed) - HAWK_SIZEOF(xtn_t)); } +#else +#define GET_XTN(sed) ((xtn_t*)((hawk_uint8_t*)hawk_sed_getxtn(sed) - HAWK_SIZEOF(xtn_t))) +#endif + +static int int_to_str (hawk_oow_t val, hawk_ooch_t* buf, hawk_oow_t buflen) +{ + hawk_oow_t t; + hawk_oow_t rlen = 0; + + t = val; + if (t == 0) rlen++; + else + { + /* non-zero values */ + if (t < 0) { t = -t; rlen++; } + while (t > 0) { rlen++; t /= 10; } + } + + if (rlen >= buflen) return -1; /* buffer too small */ + + buf[rlen] = HAWK_T('\0'); + + t = val; + if (t == 0) buf[0] = HAWK_T('0'); + else + { + if (t < 0) t = -t; + + /* fill in the buffer with digits */ + while (t > 0) + { + buf[--rlen] = (hawk_ooch_t)(t % 10) + HAWK_T('0'); + t /= 10; + } + + /* insert the negative sign if necessary */ + if (val < 0) buf[--rlen] = HAWK_T('-'); + } + + return 0; +} + +hawk_sed_t* hawk_sed_openstd (hawk_oow_t xtnsize, hawk_errnum_t* errnum) +{ + return hawk_sed_openstdwithmmgr (hawk_get_sys_mmgr(), xtnsize, hawk_get_cmgr_by_id(HAWK_CMGR_UTF8), errnum); +} + +hawk_sed_t* hawk_sed_openstdwithmmgr (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, hawk_cmgr_t* cmgr, hawk_errnum_t* errnum) +{ + hawk_sed_t* sed; + + if (!mmgr) mmgr = hawk_get_sys_mmgr(); + if (!cmgr) cmgr = hawk_get_cmgr_by_id(HAWK_CMGR_UTF8); + + sed = hawk_sed_open(mmgr, HAWK_SIZEOF(xtn_t) + xtnsize, cmgr, errnum); + if (!sed) return HAWK_NULL; + + sed->_instsize += HAWK_SIZEOF(xtn_t); + + return sed; +} + +static int verify_iostd_in (hawk_sed_t* sed, hawk_sed_iostd_t in[]) +{ + hawk_oow_t i; + + if (in[0].type == HAWK_SED_IOSTD_NULL) + { + /* if 'in' is specified, it must contains at least one + * valid entry */ + hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_EINVAL); + return -1; + } + + for (i = 0; in[i].type != HAWK_SED_IOSTD_NULL; i++) + { + if (in[i].type != HAWK_SED_IOSTD_FILE && + in[i].type != HAWK_SED_IOSTD_FILEB && + in[i].type != HAWK_SED_IOSTD_FILEU && + in[i].type != HAWK_SED_IOSTD_STR && + in[i].type != HAWK_SED_IOSTD_SIO) + { + hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_EINVAL); + return -1; + } + } + + return 0; +} + +static hawk_sio_t* open_sio_file (hawk_sed_t* sed, const hawk_ooch_t* file, int flags) +{ + hawk_sio_t* sio; + + sio = hawk_sio_open(hawk_sed_getgem(sed), 0, file, flags); + if (sio == HAWK_NULL) + { + hawk_oocs_t ea; + ea.ptr = (hawk_ooch_t*)file; + ea.len = hawk_count_oocstr(file); + hawk_sed_seterror (sed, HAWK_NULL, HAWK_SED_EIOFIL, &ea); + } + return sio; +} + +static hawk_oocs_t sio_std_names[] = +{ + { (hawk_ooch_t*)HAWK_T("stdin"), 5 }, + { (hawk_ooch_t*)HAWK_T("stdout"), 6 }, + { (hawk_ooch_t*)HAWK_T("stderr"), 6 } +}; + +static hawk_ooch_t* add_sio_name_with_uchars (hawk_sed_t* sed, const hawk_uch_t* ptr, hawk_oow_t len) +{ + xtn_t* xtn = GET_XTN(sed); + hawk_link_t* link; + + /* TODO: duplication check? */ + +#if defined(HAWK_OOCH_IS_UCH) + link = (hawk_link_t*)hawk_sed_callocmem(sed, HAWK_SIZEOF(*link) + HAWK_SIZEOF(hawk_uch_t) * (len + 1)); + if (!link) return HAWK_NULL; + + hawk_copy_uchars_to_ucstr_unlimited ((hawk_uch_t*)(link + 1), ptr, len); +#else + hawk_oow_t bcslen, ucslen; + + ucslen = len; + if (hawk_gem_convutobchars(hawk_sed_getgem(sed), ptr, &ucslen, HAWK_NULL, &bcslen) <= -1) return HAWK_NULL; + + link = (hawk_link_t*)hawk_sed_callocmem(sed, HAWK_SIZEOF(*link) + HAWK_SIZEOF(hawk_bch_t) * (bcslen + 1)); + if (!link) return HAWK_NULL; + + ucslen = len; + bcslen = bcslen + 1; + hawk_gem_convutobchars (hawk_sed_getgem(sed), ptr, &ucslen, (hawk_bch_t*)(link + 1), &bcslen); + ((hawk_bch_t*)(link + 1))[bcslen] = '\0'; +#endif + + link->link = xtn->sio_names; + xtn->sio_names = link; + + return (hawk_ooch_t*)(link + 1); +} + +static hawk_ooch_t* add_sio_name_with_bchars (hawk_sed_t* sed, const hawk_bch_t* ptr, hawk_oow_t len) +{ + xtn_t* xtn = GET_XTN(sed); + hawk_link_t* link; + + /* TODO: duplication check? */ + +#if defined(HAWK_OOCH_IS_UCH) + hawk_oow_t bcslen, ucslen; + + bcslen = len; + if (hawk_gem_convbtouchars(hawk_sed_getgem(sed), ptr, &bcslen, HAWK_NULL, &ucslen, 0) <= -1) return HAWK_NULL; + + link = (hawk_link_t*)hawk_sed_callocmem(sed, HAWK_SIZEOF(*link) + HAWK_SIZEOF(hawk_uch_t) * (ucslen + 1)); + if (!link) return HAWK_NULL; + + bcslen = len; + ucslen = ucslen + 1; + hawk_gem_convbtouchars (hawk_sed_getgem(sed), ptr, &bcslen, (hawk_uch_t*)(link + 1), &ucslen, 0); + ((hawk_uch_t*)(link + 1))[ucslen] = '\0'; + +#else + link = (hawk_link_t*)hawk_sed_callocmem(sed, HAWK_SIZEOF(*link) + HAWK_SIZEOF(hawk_bch_t) * (len + 1)); + if (!link) return HAWK_NULL; + + hawk_copy_bchars_to_bcstr_unlimited ((hawk_bch_t*)(link + 1), ptr, len); +#endif + + link->link = xtn->sio_names; + xtn->sio_names = link; + + return (hawk_ooch_t*)(link + 1); +} + +static void clear_sio_names (hawk_sed_t* sed) +{ + xtn_t* xtn = GET_XTN(sed); + hawk_link_t* cur; + + while (xtn->sio_names) + { + cur = xtn->sio_names; + xtn->sio_names = cur->link; + hawk_sed_freemem (sed, cur); + } +} + +static hawk_sio_t* open_sio_std (hawk_sed_t* sed, hawk_sio_std_t std, int flags) +{ + hawk_sio_t* sio; + + sio = hawk_sio_openstd(hawk_sed_getgem(sed), 0, std, flags); + if (sio == HAWK_NULL) hawk_sed_seterror (sed, HAWK_NULL, HAWK_SED_EIOFIL, &sio_std_names[std]); + return sio; +} + +static void close_main_stream (hawk_sed_t* sed, hawk_sed_io_arg_t* arg, hawk_sed_iostd_t* io) +{ + switch (io->type) + { + case HAWK_SED_IOSTD_FILE: + case HAWK_SED_IOSTD_FILEB: + case HAWK_SED_IOSTD_FILEU: + hawk_sio_close (arg->handle); + break; + + case HAWK_SED_IOSTD_STR: + /* nothing to do for input. + * i don't close xtn->e.out.memstr intentionally. + * i close this in hawk_awk_execstd() + */ + break; + + case HAWK_SED_IOSTD_SIO: + /* nothing to do */ + break; + + default: + /* do nothing */ + break; + } + +} + +static int open_input_stream (hawk_sed_t* sed, hawk_sed_io_arg_t* arg, hawk_sed_iostd_t* io, xtn_in_t* base) +{ + xtn_t* xtn = GET_XTN(sed); + + HAWK_ASSERT (io != HAWK_NULL); + switch (io->type) + { + #if defined(HAWK_OOCH_IS_BCH) + case HAWK_SED_IOSTD_FILE: + HAWK_ASSERT (&io->u.fileb.path == &io->u.file.path); + HAWK_ASSERT (&io->u.fileb.cmgr == &io->u.file.cmgr); + #endif + case HAWK_SED_IOSTD_FILEB: + { + hawk_sio_t* sio; + hawk_ooch_t* path; + + if (io->u.fileb.path == HAWK_NULL || + (io->u.fileb.path[0] == '-' && io->u.fileb.path[1] == '\0')) + { + sio = open_sio_std(sed, HAWK_SIO_STDIN, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR); + } + else + { + path = add_sio_name_with_bchars(sed, io->u.fileb.path, hawk_count_bcstr(io->u.fileb.path)); + if (path == HAWK_NULL) return -1; + sio = open_sio_file(sed, path, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR); + } + if (sio == HAWK_NULL) return -1; + + if (io->u.fileb.cmgr) hawk_sio_setcmgr (sio, io->u.fileb.cmgr); + arg->handle = sio; + break; + } + + #if defined(HAWK_OOCH_IS_UCH) + case HAWK_SED_IOSTD_FILE: + HAWK_ASSERT (&io->u.fileu.path == &io->u.file.path); + HAWK_ASSERT (&io->u.fileu.cmgr == &io->u.file.cmgr); + #endif + case HAWK_SED_IOSTD_FILEU: + { + hawk_sio_t* sio; + hawk_ooch_t* path; + + if (io->u.fileu.path == HAWK_NULL || + (io->u.fileu.path[0] == '-' && io->u.fileu.path[1] == '\0')) + { + sio = open_sio_std(sed, HAWK_SIO_STDIN, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR); + } + else + { + path = add_sio_name_with_uchars(sed, io->u.fileu.path, hawk_count_ucstr(io->u.fileu.path)); + if (path == HAWK_NULL) return -1; + sio = open_sio_file(sed, path, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR); + } + if (sio == HAWK_NULL) return -1; + + if (io->u.fileu.cmgr) hawk_sio_setcmgr (sio, io->u.fileu.cmgr); + arg->handle = sio; + break; + } + + case HAWK_SED_IOSTD_STR: + /* don't store anything to arg->handle */ + base->mempos = 0; + break; + + case HAWK_SED_IOSTD_SIO: + arg->handle = io->u.sio; + break; + + default: + HAWK_ASSERT (!"should never happen - io-type must be one of SIO,FILE,FILEB,FILEU,STR"); + hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_EINTERN); + return -1; + } + + + if (base == &xtn->s.in) + { + /* reset script location */ + if (io->type == HAWK_SED_IOSTD_FILE) + { + hawk_sed_setcompid ( + sed, + ((io->u.file.path == HAWK_NULL)? sio_std_names[HAWK_SIO_STDIN].ptr: io->u.file.path) + ); + } + else + { + hawk_ooch_t buf[64]; + + /* format an identifier to be something like M#1, S#5 */ + buf[0] = (io->type == HAWK_SED_IOSTD_STR)? HAWK_T('M'): HAWK_T('S'); + buf[1] = HAWK_T('#'); + int_to_str (io - xtn->s.in.ptr, &buf[2], HAWK_COUNTOF(buf) - 2); + + /* don't care about failure int_to_str() though it's not + * likely to happen */ + hawk_sed_setcompid (sed, buf); + } + sed->src.loc.line = 1; + sed->src.loc.colm = 1; + } + return 0; +} + +static int open_output_stream (hawk_sed_t* sed, hawk_sed_io_arg_t* arg, hawk_sed_iostd_t* io) +{ + xtn_t* xtn = GET_XTN(sed); + + HAWK_ASSERT (io != HAWK_NULL); + switch (io->type) + { + #if defined(HAWK_OOCH_IS_BCH) + case HAWK_SED_IOSTD_FILE: + HAWK_ASSERT (&io->u.fileb.path == &io->u.file.path); + HAWK_ASSERT (&io->u.fileb.cmgr == &io->u.file.cmgr); + #endif + case HAWK_SED_IOSTD_FILEB: + { + hawk_sio_t* sio; + hawk_ooch_t* path; + + if (io->u.fileb.path == HAWK_NULL || + (io->u.fileb.path[0] == HAWK_T('-') && io->u.fileb.path[1] == HAWK_T('\0'))) + { + sio = open_sio_std( + sed, HAWK_SIO_STDOUT, + HAWK_SIO_WRITE | + HAWK_SIO_CREATE | + HAWK_SIO_TRUNCATE | + HAWK_SIO_IGNOREECERR | + HAWK_SIO_LINEBREAK + ); + } + else + { + path = add_sio_name_with_bchars(sed, io->u.fileb.path, hawk_count_bcstr(io->u.fileb.path)); + if (path == HAWK_NULL) return -1; + sio = open_sio_file( + sed, path, + HAWK_SIO_WRITE | + HAWK_SIO_CREATE | + HAWK_SIO_TRUNCATE | + HAWK_SIO_IGNOREECERR + ); + } + if (sio == HAWK_NULL) return -1; + if (io->u.fileb.cmgr) hawk_sio_setcmgr (sio, io->u.fileb.cmgr); + arg->handle = sio; + break; + } + + #if defined(HAWK_OOCH_IS_UCH) + case HAWK_SED_IOSTD_FILE: + HAWK_ASSERT (&io->u.fileu.path == &io->u.file.path); + HAWK_ASSERT (&io->u.fileu.cmgr == &io->u.file.cmgr); + #endif + case HAWK_SED_IOSTD_FILEU: + { + hawk_sio_t* sio; + hawk_ooch_t* path; + + if (io->u.fileu.path == HAWK_NULL || + (io->u.fileu.path[0] == HAWK_T('-') && io->u.fileu.path[1] == HAWK_T('\0'))) + { + sio = open_sio_std( + sed, HAWK_SIO_STDOUT, + HAWK_SIO_WRITE | + HAWK_SIO_CREATE | + HAWK_SIO_TRUNCATE | + HAWK_SIO_IGNOREECERR | + HAWK_SIO_LINEBREAK + ); + } + else + { + path = add_sio_name_with_uchars(sed, io->u.fileu.path, hawk_count_ucstr(io->u.fileu.path)); + if (path == HAWK_NULL) return -1; + sio = open_sio_file( + sed, path, + HAWK_SIO_WRITE | + HAWK_SIO_CREATE | + HAWK_SIO_TRUNCATE | + HAWK_SIO_IGNOREECERR + ); + } + if (sio == HAWK_NULL) return -1; + if (io->u.fileu.cmgr) hawk_sio_setcmgr (sio, io->u.fileu.cmgr); + arg->handle = sio; + break; + } + + case HAWK_SED_IOSTD_STR: + /* don't store anything to arg->handle */ + xtn->e.out.memstr = hawk_ooecs_open(hawk_sed_getgem(sed), 0, 512); + if (xtn->e.out.memstr == HAWK_NULL) + { + hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_ENOMEM); + return -1; + } + break; + + case HAWK_SED_IOSTD_SIO: + arg->handle = io->u.sio; + break; + + default: + HAWK_ASSERT (!"should never happen - io-type must be one of SIO,FILE,FILEB,FILEU,STR"); + hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_EINTERN); + return -1; + } + + return 0; +} + +static hawk_ooi_t read_input_stream (hawk_sed_t* sed, hawk_sed_io_arg_t* arg, hawk_ooch_t* buf, hawk_oow_t len, xtn_in_t* base) +{ + xtn_t* xtn = GET_XTN(sed); + hawk_sed_iostd_t* io, * next; + void* old, * new; + hawk_ooi_t n = 0; + + if (len > HAWK_TYPE_MAX(hawk_ooi_t)) len = HAWK_TYPE_MAX(hawk_ooi_t); + + do + { + io = base->cur; + + if (base == &xtn->s.in && xtn->s.newline_squeezed) + { + xtn->s.newline_squeezed = 0; + goto open_next; + } + + HAWK_ASSERT (io != HAWK_NULL); + + if (io->type == HAWK_SED_IOSTD_STR) + { + n = 0; + while (base->mempos < io->u.str.len && n < len) + buf[n++] = io->u.str.ptr[base->mempos++]; + } + else n = hawk_sio_getoochars(arg->handle, buf, len); + + if (n != 0) + { + if (n <= -1) + { + if (io->type == HAWK_SED_IOSTD_FILE) + { + hawk_oocs_t ea; + if (io->u.file.path) + { + ea.ptr = io->u.file.path; + ea.len = hawk_count_oocstr(io->u.file.path); + } + else + { + ea = sio_std_names[HAWK_SIO_STDIN]; + } + hawk_sed_seterror (sed, HAWK_NULL, HAWK_SED_EIOFIL, &ea); + } + } + else if (base == &xtn->s.in) + { + xtn->s.last = buf[n-1]; + } + + break; + } + + /* ============================================= */ + /* == end of file on the current input stream == */ + /* ============================================= */ + + if (base == &xtn->s.in && xtn->s.last != HAWK_T('\n')) + { + /* TODO: different line termination convension */ + buf[0] = HAWK_T('\n'); + n = 1; + xtn->s.newline_squeezed = 1; + break; + } + + open_next: + next = base->cur + 1; + if (next->type == HAWK_SED_IOSTD_NULL) + { + /* no next stream available - return 0 */ + break; + } + + old = arg->handle; + + /* try to open the next input stream */ + if (open_input_stream(sed, arg, next, base) <= -1) + { + /* failed to open the next input stream */ + if (next->type == HAWK_SED_IOSTD_FILE) + { + hawk_oocs_t ea; + if (next->u.file.path) + { + ea.ptr = next->u.file.path; + ea.len = hawk_count_oocstr (next->u.file.path); + } + else + { + ea = sio_std_names[HAWK_SIO_STDIN]; + } + hawk_sed_seterror (sed, HAWK_NULL, HAWK_SED_EIOFIL, &ea); + } + + n = -1; + break; + } + + /* successfuly opened the next input stream */ + new = arg->handle; + + arg->handle = old; + + /* close the previous stream */ + close_main_stream (sed, arg, io); + + arg->handle = new; + + base->cur++; + } + while (1); + + return n; +} + +static hawk_ooi_t s_in (hawk_sed_t* sed, hawk_sed_io_cmd_t cmd, hawk_sed_io_arg_t* arg, hawk_ooch_t* buf, hawk_oow_t len) +{ + xtn_t* xtn = GET_XTN(sed); + + switch (cmd) + { + case HAWK_SED_IO_OPEN: + { + if (open_input_stream(sed, arg, xtn->s.in.cur, &xtn->s.in) <= -1) return -1; + return 1; + } + + case HAWK_SED_IO_CLOSE: + { + close_main_stream (sed, arg, xtn->s.in.cur); + return 0; + } + + case HAWK_SED_IO_READ: + { + return read_input_stream(sed, arg, buf, len, &xtn->s.in); + } + + default: + { + HAWK_ASSERT (!"should never happen - cmd must be one of OPEN,CLOSE,READ"); + hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_EINTERN); + return -1; + } + } +} + +static hawk_ooi_t x_in (hawk_sed_t* sed, hawk_sed_io_cmd_t cmd, hawk_sed_io_arg_t* arg, hawk_ooch_t* buf, hawk_oow_t len) +{ + hawk_sio_t* sio; + xtn_t* xtn = GET_XTN(sed); + + switch (cmd) + { + case HAWK_SED_IO_OPEN: + { + if (arg->path == HAWK_NULL) + { + /* no file specified. console stream */ + if (xtn->e.in.ptr == HAWK_NULL) + { + /* HAWK_NULL passed into hawk_sed_exec() for input */ + sio = open_sio_std(sed, HAWK_SIO_STDIN, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR); + if (sio == HAWK_NULL) return -1; + arg->handle = sio; + } + else + { + if (open_input_stream(sed, arg, xtn->e.in.cur, &xtn->e.in) <= -1) return -1; + } + } + else + { + sio = open_sio_file(sed, arg->path, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR); + if (sio == HAWK_NULL) return -1; + arg->handle = sio; + } + + return 1; + } + + case HAWK_SED_IO_CLOSE: + { + if (arg->path == HAWK_NULL) + { + /* main data stream */ + if (xtn->e.in.ptr == HAWK_NULL) + hawk_sio_close (arg->handle); + else + close_main_stream (sed, arg, xtn->e.in.cur); + } + else + { + hawk_sio_close (arg->handle); + } + + return 0; + } + + case HAWK_SED_IO_READ: + { + if (arg->path == HAWK_NULL) + { + /* main data stream */ + if (xtn->e.in.ptr == HAWK_NULL) + { + hawk_ooi_t n; + n = hawk_sio_getoochars(arg->handle, buf, len); + if (n <= -1) hawk_sed_seterror (sed, HAWK_NULL, HAWK_SED_EIOFIL, &sio_std_names[HAWK_SIO_STDIN]); + return n; + } + else + return read_input_stream(sed, arg, buf, len, &xtn->e.in); + } + else + { + hawk_ooi_t n; + n = hawk_sio_getoochars(arg->handle, buf, len); + if (n <= -1) + { + hawk_oocs_t ea; + ea.ptr = arg->path; + ea.len = hawk_count_oocstr(arg->path); + hawk_sed_seterror (sed, HAWK_NULL, HAWK_SED_EIOFIL, &ea); + } + return n; + } + } + + default: + HAWK_ASSERT (!"should never happen - cmd must be one of OPEN,CLOSE,READ"); + hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_EINTERN); + return -1; + } +} + +static hawk_ooi_t x_out ( + hawk_sed_t* sed, hawk_sed_io_cmd_t cmd, hawk_sed_io_arg_t* arg, + hawk_ooch_t* dat, hawk_oow_t len) +{ + xtn_t* xtn = GET_XTN(sed); + hawk_sio_t* sio; + + switch (cmd) + { + case HAWK_SED_IO_OPEN: + { + if (arg->path == HAWK_NULL) + { + /* main data stream */ + + if (xtn->e.out.ptr == HAWK_NULL) + { + /* HAWK_NULL passed into hawk_sed_execstd() for output */ + sio = open_sio_std( + sed, HAWK_SIO_STDOUT, + HAWK_SIO_WRITE | + HAWK_SIO_CREATE | + HAWK_SIO_TRUNCATE | + HAWK_SIO_IGNOREECERR | + HAWK_SIO_LINEBREAK + ); + if (sio == HAWK_NULL) return -1; + arg->handle = sio; + } + else + { + if (open_output_stream(sed, arg, xtn->e.out.ptr) <= -1) return -1; + } + } + else + { + + sio = open_sio_file( + sed, arg->path, + HAWK_SIO_WRITE | + HAWK_SIO_CREATE | + HAWK_SIO_TRUNCATE | + HAWK_SIO_IGNOREECERR + ); + if (sio == HAWK_NULL) return -1; + arg->handle = sio; + } + + return 1; + } + + case HAWK_SED_IO_CLOSE: + { + if (arg->path == HAWK_NULL) + { + if (xtn->e.out.ptr == HAWK_NULL) + hawk_sio_close (arg->handle); + else + close_main_stream (sed, arg, xtn->e.out.ptr); + } + else + { + hawk_sio_close (arg->handle); + } + return 0; + } + + case HAWK_SED_IO_WRITE: + { + if (arg->path == HAWK_NULL) + { + /* main data stream */ + if (xtn->e.out.ptr == HAWK_NULL) + { + hawk_ooi_t n; + n = hawk_sio_putoochars(arg->handle, dat, len); + if (n <= -1) hawk_sed_seterror (sed, HAWK_NULL, HAWK_SED_EIOFIL, &sio_std_names[HAWK_SIO_STDOUT]); + return n; + } + else + { + hawk_sed_iostd_t* io = xtn->e.out.ptr; + if (io->type == HAWK_SED_IOSTD_STR) + { + if (len > HAWK_TYPE_MAX(hawk_ooi_t)) len = HAWK_TYPE_MAX(hawk_ooi_t); + + if (hawk_ooecs_ncat(xtn->e.out.memstr, dat, len) == (hawk_oow_t)-1) + { + hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_ENOMEM); + return -1; + } + + return len; + } + else + { + hawk_ooi_t n; + n = hawk_sio_putoochars(arg->handle, dat, len); + if (n <= -1) + { + hawk_oocs_t ea; + if (io->u.file.path) + { + ea.ptr = io->u.file.path; + ea.len = hawk_count_oocstr(io->u.file.path); + } + else + { + ea = sio_std_names[HAWK_SIO_STDOUT]; + } + hawk_sed_seterror (sed, HAWK_NULL, HAWK_SED_EIOFIL, &ea); + } + return n; + } + } + } + else + { + hawk_ooi_t n; + n = hawk_sio_putoochars(arg->handle, dat, len); + if (n <= -1) + { + hawk_oocs_t ea; + ea.ptr = arg->path; + ea.len = hawk_count_oocstr(arg->path); + hawk_sed_seterror (sed, HAWK_NULL, HAWK_SED_EIOFIL, &ea); + } + return n; + } + } + + default: + HAWK_ASSERT (!"should never happen - cmd must be one of OPEN,CLOSE,WRITE"); + hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_EINTERN); + return -1; + } +} + +int hawk_sed_compstd (hawk_sed_t* sed, hawk_sed_iostd_t in[], hawk_oow_t* count) +{ + xtn_t* xtn = GET_XTN(sed); + int ret; + + if (in == HAWK_NULL) + { + /* it requires a valid array unlike hawk_sed_execstd(). */ + hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_EINVAL); + return -1; + } + if (verify_iostd_in(sed, in) <= -1) return -1; + + HAWK_MEMSET (&xtn->s, 0, HAWK_SIZEOF(xtn->s)); + xtn->s.in.ptr = in; + xtn->s.in.cur = in; + + ret = hawk_sed_comp(sed, s_in); + + if (count) *count = xtn->s.in.cur - xtn->s.in.ptr; + + clear_sio_names (sed); + return ret; +} + +int hawk_sed_execstd (hawk_sed_t* sed, hawk_sed_iostd_t in[], hawk_sed_iostd_t* out) +{ + int n; + xtn_t* xtn = GET_XTN(sed); + + if (in && verify_iostd_in(sed, in) <= -1) return -1; + + if (out) + { + if (out->type != HAWK_SED_IOSTD_FILE && + out->type != HAWK_SED_IOSTD_FILEB && + out->type != HAWK_SED_IOSTD_FILEU && + out->type != HAWK_SED_IOSTD_STR && + out->type != HAWK_SED_IOSTD_SIO) + { + hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_EINVAL); + return -1; + } + } + + HAWK_MEMSET (&xtn->e, 0, HAWK_SIZEOF(xtn->e)); + xtn->e.in.ptr = in; + xtn->e.in.cur = in; + xtn->e.out.ptr = out; + + n = hawk_sed_exec (sed, x_in, x_out); + + if (out && out->type == HAWK_SED_IOSTD_STR) + { + if (n >= 0) + { + HAWK_ASSERT (xtn->e.out.memstr != HAWK_NULL); + hawk_ooecs_yield (xtn->e.out.memstr, &out->u.str, 0); + } + if (xtn->e.out.memstr) hawk_ooecs_close (xtn->e.out.memstr); + } + + clear_sio_names (sed); + return n; +} + +int hawk_sed_compstdfile (hawk_sed_t* sed, const hawk_ooch_t* file, hawk_cmgr_t* cmgr) +{ + hawk_sed_iostd_t in[2]; + + in[0].type = HAWK_SED_IOSTD_FILE; + in[0].u.file.path = file; + in[0].u.file.cmgr = cmgr; + in[1].type = HAWK_SED_IOSTD_NULL; + + return hawk_sed_compstd(sed, in, HAWK_NULL); +} + +int hawk_sed_compstdfileb (hawk_sed_t* sed, const hawk_bch_t* file, hawk_cmgr_t* cmgr) +{ + hawk_sed_iostd_t in[2]; + + in[0].type = HAWK_SED_IOSTD_FILEB; + in[0].u.fileb.path = file; + in[0].u.fileb.cmgr = cmgr; + in[1].type = HAWK_SED_IOSTD_NULL; + + return hawk_sed_compstd(sed, in, HAWK_NULL); +} + +int hawk_sed_compstdfileu (hawk_sed_t* sed, const hawk_uch_t* file, hawk_cmgr_t* cmgr) +{ + hawk_sed_iostd_t in[2]; + + in[0].type = HAWK_SED_IOSTD_FILEU; + in[0].u.fileu.path = file; + in[0].u.fileu.cmgr = cmgr; + in[1].type = HAWK_SED_IOSTD_NULL; + + return hawk_sed_compstd(sed, in, HAWK_NULL); +} + +int hawk_sed_compstdstr (hawk_sed_t* sed, const hawk_ooch_t* script) +{ + hawk_sed_iostd_t in[2]; + + in[0].type = HAWK_SED_IOSTD_STR; + in[0].u.str.ptr = (hawk_ooch_t*)script; + in[0].u.str.len = hawk_count_oocstr(script); + in[1].type = HAWK_SED_IOSTD_NULL; + + return hawk_sed_compstd (sed, in, HAWK_NULL); +} + +int hawk_sed_compstdxstr (hawk_sed_t* sed, const hawk_oocs_t* script) +{ + hawk_sed_iostd_t in[2]; + + in[0].type = HAWK_SED_IOSTD_STR; + in[0].u.str = *script; + in[1].type = HAWK_SED_IOSTD_NULL; + + return hawk_sed_compstd (sed, in, HAWK_NULL); +} + +int hawk_sed_execstdfile (hawk_sed_t* sed, const hawk_ooch_t* infile, const hawk_ooch_t* outfile, hawk_cmgr_t* cmgr) +{ + hawk_sed_iostd_t in[2]; + hawk_sed_iostd_t out; + hawk_sed_iostd_t* pin = HAWK_NULL, * pout = HAWK_NULL; + + if (infile) + { + in[0].type = HAWK_SED_IOSTD_FILE; + in[0].u.file.path = infile; + in[0].u.file.cmgr = cmgr; + in[1].type = HAWK_SED_IOSTD_NULL; + pin = in; + } + + if (outfile) + { + out.type = HAWK_SED_IOSTD_FILE; + out.u.file.path = outfile; + out.u.file.cmgr = cmgr; + pout = &out; + } + + return hawk_sed_execstd(sed, pin, pout); +} + +int hawk_sed_execstdxstr (hawk_sed_t* sed, const hawk_oocs_t* instr, hawk_oocs_t* outstr, hawk_cmgr_t* cmgr) +{ + hawk_sed_iostd_t in[2]; + hawk_sed_iostd_t out; + int n; + + in[0].type = HAWK_SED_IOSTD_STR; + in[0].u.str = *instr; + in[1].type = HAWK_SED_IOSTD_NULL; + + out.type = HAWK_SED_IOSTD_STR; + + n = hawk_sed_execstd(sed, in, &out); + + if (n >= 0) *outstr = out.u.str; + + return n; +} diff --git a/hawk/lib/utl-rnd.c b/hawk/lib/utl-rnd.c new file mode 100644 index 00000000..d0125087 --- /dev/null +++ b/hawk/lib/utl-rnd.c @@ -0,0 +1,49 @@ +/* + * $Id$ + * + Copyright (c) 2006-2020 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 + +/* Park-Miller "minimal standard" 31 bit + * pseudo-random number generator, implemented + * with David G. Carta's optimisation: with + * 32 bit math and without division. + */ +hawk_uint32_t hawk_rand31 (hawk_uint32_t seed) +{ + hawk_uint32_t hi, lo; + + if (seed == 0) seed++; + + lo = 16807 * (seed & 0xFFFF); + hi = 16807 * (seed >> 16); + + lo += (hi & 0x7FFF) << 16; + lo += hi >> 15; + + if (lo > 0x7FFFFFFFul) lo -= 0x7FFFFFFFul; + + return lo; +} diff --git a/hawk/lib/utl-str.c b/hawk/lib/utl-str.c index c50bef24..ce0ceeda 100644 --- a/hawk/lib/utl-str.c +++ b/hawk/lib/utl-str.c @@ -660,7 +660,7 @@ fini: -hawk_oow_t hawk_copy_ucses_to_uchars (hawk_uch_t* buf, hawk_oow_t bsz, const hawk_uch_t* fmt, const hawk_ucs_t str[]) +hawk_oow_t hawk_copy_ufcs_to_uchars (hawk_uch_t* buf, hawk_oow_t bsz, const hawk_uch_t* fmt, const hawk_ucs_t str[]) { hawk_uch_t* b = buf; hawk_uch_t* end = buf + bsz - 1; @@ -722,11 +722,11 @@ fini: return b - buf; } -hawk_oow_t hawk_copy_bcses_to_bchars (hawk_bch* buf, hawk_oow_t bsz, const hawk_bch* fmt, const hawk_bcs_t str[]) +hawk_oow_t hawk_copy_bfcs_to_bchars (hawk_bch_t* buf, hawk_oow_t bsz, const hawk_bch_t* fmt, const hawk_bcs_t str[]) { - hawk_bch* b = buf; - hawk_bch* end = buf + bsz - 1; - const hawk_bch* f = fmt; + hawk_bch_t* b = buf; + hawk_bch_t* end = buf + bsz - 1; + const hawk_bch_t* f = fmt; if (bsz <= 0) return 0; @@ -744,7 +744,7 @@ hawk_oow_t hawk_copy_bcses_to_bchars (hawk_bch* buf, hawk_oow_t bsz, const hawk_ if (f[1] == HAWK_BT('{') && (f[2] >= HAWK_BT('0') && f[2] <= HAWK_BT('9'))) { - const hawk_bch* tmp, * tmpend; + const hawk_bch_t* tmp, * tmpend; hawk_oow_t idx = 0; tmp = f; diff --git a/hawk/mod/mod-ffi.c b/hawk/mod/mod-ffi.c index 93ff9706..c00148a7 100644 --- a/hawk/mod/mod-ffi.c +++ b/hawk/mod/mod-ffi.c @@ -847,7 +847,7 @@ static int fnc_call (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) if (add_ffi_arg(rtx, ffi_list, ffi_node, fmtc, _unsigned, hawk_rtx_getarg(rtx, j + FNC_CALL_ARG_BASE), &ret) <= -1) goto done; _unsigned = 0; - j++; + j++; /* increment the total number of arguments */ } while (i < sig.len && sig.ptr[i] == ' ') i++; /* skip all spaces after > */ @@ -868,8 +868,20 @@ static int fnc_call (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) goto done; } - fs = (nfixedargs == j)? ffi_prep_cif(&ffi->cif, FFI_DEFAULT_ABI, j, ffi->fmtc_to_type[0][fmtc], ffi->arg_types): - ffi_prep_cif_var(&ffi->cif, FFI_DEFAULT_ABI, nfixedargs, j, ffi->fmtc_to_type[0][fmtc], ffi->arg_types); + if (nfixedargs == j) + { + fs = ffi_prep_cif(&ffi->cif, FFI_DEFAULT_ABI, j, ffi->fmtc_to_type[0][fmtc], ffi->arg_types); + } + else + { + #if defined(HAVE_FFI_PREP_CIF_VAR) + fs = ffi_prep_cif_var(&ffi->cif, FFI_DEFAULT_ABI, nfixedargs, j, ffi->fmtc_to_type[0][fmtc], ffi->arg_types); + #else + ret = set_error_on_ffi_list(rtx, ffi_list, HAWK_ENOIMPL, HAWK_T("variadic arguments not supported")); + goto done; + #endif + } + if (fs != FFI_OK) { ret = set_error_on_ffi_list(rtx, ffi_list, HAWK_ESYSERR, HAWK_T("unable to prepare the ffi_cif structure"));