diff --git a/bin/Makefile.am b/bin/Makefile.am index b5b8db7d..74b231bd 100644 --- a/bin/Makefile.am +++ b/bin/Makefile.am @@ -18,7 +18,7 @@ bin_PROGRAMS = hawk ################################################## # hawk ################################################## -hawk_SOURCES = hawk.c sed.c main.c main.h +hawk_SOURCES = cut.c hawk.c sed.c main.c main.h hawk_CPPFLAGS = $(CPPFLAGS_COMMON) hawk_CFLAGS = $(CFLAGS_COMMON) hawk_LDFLAGS = $(LDFLAGS_COMMON) diff --git a/bin/Makefile.in b/bin/Makefile.in index 4a4b32e9..ac8f8dc8 100644 --- a/bin/Makefile.in +++ b/bin/Makefile.in @@ -111,8 +111,8 @@ CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = am__installdirs = "$(DESTDIR)$(bindir)" PROGRAMS = $(bin_PROGRAMS) -am_hawk_OBJECTS = hawk-hawk.$(OBJEXT) hawk-sed.$(OBJEXT) \ - hawk-main.$(OBJEXT) +am_hawk_OBJECTS = hawk-cut.$(OBJEXT) hawk-hawk.$(OBJEXT) \ + hawk-sed.$(OBJEXT) hawk-main.$(OBJEXT) hawk_OBJECTS = $(am_hawk_OBJECTS) am__DEPENDENCIES_1 = hawk_DEPENDENCIES = $(LIBADD_COMMON) $(am__DEPENDENCIES_1) @@ -138,7 +138,7 @@ am__v_at_1 = DEFAULT_INCLUDES = depcomp = $(SHELL) $(top_srcdir)/ac/depcomp am__maybe_remake_depfiles = depfiles -am__depfiles_remade = ./$(DEPDIR)/hawk-hawk.Po \ +am__depfiles_remade = ./$(DEPDIR)/hawk-cut.Po ./$(DEPDIR)/hawk-hawk.Po \ ./$(DEPDIR)/hawk-main.Po ./$(DEPDIR)/hawk-sed.Po am__mv = mv -f COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ @@ -356,7 +356,7 @@ LIBADD_COMMON = ../lib/libhawk.la ################################################## # hawk ################################################## -hawk_SOURCES = hawk.c sed.c main.c main.h +hawk_SOURCES = cut.c hawk.c sed.c main.c main.h hawk_CPPFLAGS = $(CPPFLAGS_COMMON) hawk_CFLAGS = $(CFLAGS_COMMON) hawk_LDFLAGS = $(LDFLAGS_COMMON) @@ -449,6 +449,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hawk-cut.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hawk-hawk.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hawk-main.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hawk-sed.Po@am__quote@ # am--include-marker @@ -483,6 +484,20 @@ 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-cut.o: cut.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hawk_CPPFLAGS) $(CPPFLAGS) $(hawk_CFLAGS) $(CFLAGS) -MT hawk-cut.o -MD -MP -MF $(DEPDIR)/hawk-cut.Tpo -c -o hawk-cut.o `test -f 'cut.c' || echo '$(srcdir)/'`cut.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/hawk-cut.Tpo $(DEPDIR)/hawk-cut.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='cut.c' object='hawk-cut.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-cut.o `test -f 'cut.c' || echo '$(srcdir)/'`cut.c + +hawk-cut.obj: cut.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hawk_CPPFLAGS) $(CPPFLAGS) $(hawk_CFLAGS) $(CFLAGS) -MT hawk-cut.obj -MD -MP -MF $(DEPDIR)/hawk-cut.Tpo -c -o hawk-cut.obj `if test -f 'cut.c'; then $(CYGPATH_W) 'cut.c'; else $(CYGPATH_W) '$(srcdir)/cut.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/hawk-cut.Tpo $(DEPDIR)/hawk-cut.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='cut.c' object='hawk-cut.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-cut.obj `if test -f 'cut.c'; then $(CYGPATH_W) 'cut.c'; else $(CYGPATH_W) '$(srcdir)/cut.c'; fi` + hawk-hawk.o: hawk.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hawk_CPPFLAGS) $(CPPFLAGS) $(hawk_CFLAGS) $(CFLAGS) -MT hawk-hawk.o -MD -MP -MF $(DEPDIR)/hawk-hawk.Tpo -c -o hawk-hawk.o `test -f 'hawk.c' || echo '$(srcdir)/'`hawk.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/hawk-hawk.Tpo $(DEPDIR)/hawk-hawk.Po @@ -657,6 +672,7 @@ clean: clean-am clean-am: clean-binPROGRAMS clean-generic clean-libtool mostlyclean-am distclean: distclean-am + -rm -f ./$(DEPDIR)/hawk-cut.Po -rm -f ./$(DEPDIR)/hawk-hawk.Po -rm -f ./$(DEPDIR)/hawk-main.Po -rm -f ./$(DEPDIR)/hawk-sed.Po @@ -705,6 +721,7 @@ install-ps-am: installcheck-am: maintainer-clean: maintainer-clean-am + -rm -f ./$(DEPDIR)/hawk-cut.Po -rm -f ./$(DEPDIR)/hawk-hawk.Po -rm -f ./$(DEPDIR)/hawk-main.Po -rm -f ./$(DEPDIR)/hawk-sed.Po diff --git a/bin/cut.c b/bin/cut.c new file mode 100644 index 00000000..22d92cfa --- /dev/null +++ b/bin/cut.c @@ -0,0 +1,924 @@ +/* + * $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 "main.h" + +#include +#include +#include +#include +#include +#include + +#if !defined(_GNU_SOURCE) +# define _GNU_SOURCE +#endif +#if !defined(_XOPEN_SOURCE) +# define _XOPEN_SOURCE 700 +#endif + +#include +#include +#include +#include + +#if defined(_WIN32) +# include +#elif defined(__OS2__) +# define INCL_ERRORS +# include +# include +#elif defined(__DOS__) +# include +# include +#else +# include +# include +# include +#endif + +static struct +{ + hawk_cut_iostd_t* io; + hawk_oow_t capa; + hawk_oow_t size; +} g_script = +{ + HAWK_NULL, + 0, + 0 +}; + + +static hawk_cut_t* g_cut = HAWK_NULL; +static hawk_cmgr_t* g_script_cmgr = HAWK_NULL; +static hawk_cmgr_t* g_infile_cmgr = HAWK_NULL; +static hawk_cmgr_t* g_outfile_cmgr = HAWK_NULL; + +/* ------------------------------------------------------------------- */ + +struct arg_t +{ + hawk_bch_t* output_file; + int infile_pos; + int option; + int separate; + int inplace; + int wildcard; + int debug; + hawk_uintptr_t memlimit; +}; + +/* ------------------------------------------------------------------- */ + +static void print_usage (FILE* out, const hawk_bch_t* argv0, const hawk_bch_t* real_argv0) +{ + const hawk_bch_t* b1 = hawk_get_base_name_bcstr(real_argv0? real_argv0: argv0); + const hawk_bch_t* b2 = (real_argv0? " ": ""); + const hawk_bch_t* b3 = (real_argv0? argv0: ""); + + fprintf (out, "USAGE: %s%s%s [options] script [file]\n", b1, b2, b3); + fprintf (out, " %s%s%s [options] -f script-file [file]\n", b1, b2, b3); + fprintf (out, " %s%s%s [options] -e script [file]\n", b1, b2, b3); + + fprintf (out, "Options as follows:\n"); + fprintf (out, " -h/--help show this message\n"); + fprintf (out, " -D show extra information\n"); + fprintf (out, " -n disable auto-print\n"); + fprintf (out, " -e script specify a script\n"); + fprintf (out, " -f file specify a script file\n"); + fprintf (out, " -o file specify an output file\n"); + fprintf (out, " -r use the extended regular expression\n"); + fprintf (out, " -R enable non-standard extensions to the regular\n"); + fprintf (out, " expression\n"); + fprintf (out, " -i perform in-place editing. imply -s\n"); + fprintf (out, " -s process input files separately\n"); + fprintf (out, " -a perform strict address and label check\n"); + fprintf (out, " -b allow extended address formats\n"); + fprintf (out, " ,,,<0,/regex/>\n"); + fprintf (out, " -x allow text on the same line as c, a, i\n"); + fprintf (out, " -y ensure a newline at text end\n"); + fprintf (out, " -m/--memory-limit number specify the maximum amount of memory to use in bytes\n"); + fprintf (out, " -w expand file wildcards\n"); +#if defined(HAWK_OOCH_IS_UCH) + fprintf (out, " --script-encoding string specify script file encoding name\n"); + fprintf (out, " --infile-encoding string specify input file encoding name\n"); + fprintf (out, " --outfile-encoding string specify output file encoding name\n"); +#endif +} + +static int add_script (const hawk_bch_t* str, int mem) +{ + if (g_script.size >= g_script.capa) + { + hawk_cut_iostd_t* tmp; + + tmp = (hawk_cut_iostd_t*)realloc(g_script.io, HAWK_SIZEOF(*g_script.io) * (g_script.capa + 16 + 1)); + if (tmp == HAWK_NULL) + { + hawk_main_print_error("out of memory while processing %s\n", str); + return -1; + } + + g_script.io = tmp; + g_script.capa += 16; + } + + if (mem) + { + /* string */ + g_script.io[g_script.size].type = HAWK_CUT_IOSTD_BCS; + /* though its type is not qualified to be const, + * u.mem.ptr is actually const when ucut for input */ + g_script.io[g_script.size].u.bcs.ptr = (hawk_bch_t*)str; + g_script.io[g_script.size].u.bcs.len = hawk_count_bcstr(str); + } + else + { + /* file name */ + g_script.io[g_script.size].type = HAWK_CUT_IOSTD_FILEB; + g_script.io[g_script.size].u.fileb.path = str; + g_script.io[g_script.size].u.fileb.cmgr = g_script_cmgr; + } + g_script.size++; + return 0; +} + +static void free_scripts (void) +{ + if (g_script.io) + { + free (g_script.io); + g_script.io = HAWK_NULL; + g_script.capa = 0; + g_script.size = 0; + } +} + +static int handle_args (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0, struct arg_t* arg) +{ + static hawk_bcli_lng_t lng[] = + { +#if defined(HAWK_OOCH_IS_UCH) + { ":script-encoding", '\0' }, + { ":infile-encoding", '\0' }, + { ":outfile-encoding", '\0' }, +#endif + { ":memory-limit", 'm' }, + { "help", 'h' }, + { HAWK_NULL, '\0' } + }; + static hawk_bcli_t opt = + { + "hDe:f:o:rRisabxym:w", + lng + }; + hawk_bci_t c; + + while ((c = hawk_get_bcli(argc, argv, &opt)) != HAWK_BCI_EOF) + { + switch (c) + { + default: + print_usage (stderr, argv[0], real_argv0); + goto oops; + + case '?': + hawk_main_print_error("bad option - %c\n", opt.opt); + print_usage (stderr, argv[0], real_argv0); + goto oops; + + case ':': + hawk_main_print_error("bad parameter for %c\n", opt.opt); + print_usage (stderr, argv[0], real_argv0); + goto oops; + + case 'h': + print_usage (stdout, argv[0], real_argv0); + goto done; + + case 'D': + arg->debug = 1; + break; + + case 'e': + if (add_script(opt.arg, 1) <= -1) goto oops; + break; + + case 'f': + if (add_script(opt.arg, 0) <= -1) goto oops; + break; + + case 'o': + arg->output_file = opt.arg; + break; + + case 'i': + /* 'i' implies 's'. */ + arg->inplace = 1; + + case 's': + arg->separate = 1; + break; + +#if 0 + case 'a': + arg->option |= HAWK_CUT_STRICT; + break; + + case 'b': + arg->option |= HAWK_CUT_EXTENDEDADR; + break; + + case 'x': + arg->option |= HAWK_CUT_SAMELINE; + break; + + case 'y': + arg->option |= HAWK_CUT_ENSURENL; + break; +#endif + + case 'm': + arg->memlimit = strtoul(opt.arg, HAWK_NULL, 10); + break; + + case 'w': + arg->wildcard = 1; + break; + + case '\0': + { + if (hawk_comp_bcstr(opt.lngopt, "script-encoding", 0) == 0) + { + g_script_cmgr = hawk_get_cmgr_by_bcstr(opt.arg); + if (g_script_cmgr == HAWK_NULL) + { + hawk_main_print_error("unknown script encoding - %s\n", opt.arg); + goto oops; + } + } + else if (hawk_comp_bcstr(opt.lngopt, "infile-encoding", 0) == 0) + { + g_infile_cmgr = hawk_get_cmgr_by_bcstr(opt.arg); + if (g_infile_cmgr == HAWK_NULL) + { + hawk_main_print_error("unknown input file encoding - %s\n", opt.arg); + goto oops; + } + } + else if (hawk_comp_bcstr(opt.lngopt, "outfile-encoding", 0) == 0) + { + g_outfile_cmgr = hawk_get_cmgr_by_bcstr(opt.arg); + if (g_outfile_cmgr == HAWK_NULL) + { + hawk_main_print_error("unknown output file encoding - %s\n", opt.arg); + goto oops; + } + } + break; + } + + } + } + + if (opt.ind < argc && g_script.size <= 0) + { + if (add_script(argv[opt.ind++], 1) <= -1) goto oops; + } + if (opt.ind < argc) arg->infile_pos = opt.ind; + + if (g_script.size <= 0) + { + print_usage (stderr, argv[0], real_argv0); + goto oops; + } + + g_script.io[g_script.size].type = HAWK_CUT_IOSTD_NULL; + return 1; + +oops: + free_scripts (); + return -1; + +done: + free_scripts (); + return 0; +} + +static void print_exec_error (hawk_cut_t* cut) +{ + const hawk_loc_t* errloc = hawk_cut_geterrloc(cut); + if (errloc->line > 0 || errloc->colm > 0) + { + hawk_main_print_error("cannot execute - %s at line %lu column %lu\n", + hawk_cut_geterrbmsg(cut), (unsigned long)errloc->line, (unsigned long)errloc->colm); + } + else + { + hawk_main_print_error("cannot execute - %s\n", hawk_cut_geterrbmsg(cut)); + } +} + +/* ---------------------------------------------------------------------- */ + +typedef struct sig_state_t sig_state_t; +struct sig_state_t +{ + hawk_uintptr_t handler; + hawk_uintptr_t old_handler; +#if defined(HAVE_SIGACTION) + sigset_t old_sa_mask; + int old_sa_flags; +#endif +}; + +typedef void (*sig_handler_t) (int sig); + +static sig_state_t g_sig_state[HAWK_NSIG]; + +static int is_signal_handler_set (int sig) +{ + return !!g_sig_state[sig].handler; +} + +#if defined(HAVE_SIGACTION) +static void dispatch_siginfo (int sig, siginfo_t* si, void* ctx) +{ + if (g_sig_state[sig].handler != (hawk_uintptr_t)SIG_IGN && + g_sig_state[sig].handler != (hawk_uintptr_t)SIG_DFL) + { + ((sig_handler_t)g_sig_state[sig].handler) (sig); + } + + if (g_sig_state[sig].old_handler && + g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_IGN && + g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_DFL) + { + ((void(*)(int, siginfo_t*, void*))g_sig_state[sig].old_handler) (sig, si, ctx); + } +} +#endif + +static void dispatch_signal (int sig) +{ + if (g_sig_state[sig].handler != (hawk_uintptr_t)SIG_IGN && + g_sig_state[sig].handler != (hawk_uintptr_t)SIG_DFL) + { + ((sig_handler_t)g_sig_state[sig].handler) (sig); + } + + if (g_sig_state[sig].old_handler && + g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_IGN && + g_sig_state[sig].old_handler != (hawk_uintptr_t)SIG_DFL) + { + ((sig_handler_t)g_sig_state[sig].old_handler) (sig); + } +} + +static int set_signal_handler (int sig, sig_handler_t handler, int extra_flags) +{ + if (g_sig_state[sig].handler) + { + /* already set - allow handler change. ignore extra_flags. */ + if (g_sig_state[sig].handler == (hawk_uintptr_t)handler) return -1; + g_sig_state[sig].handler = (hawk_uintptr_t)handler; + } + else + { + #if defined(HAVE_SIGACTION) + struct sigaction sa, oldsa; + + if (sigaction(sig, HAWK_NULL, &oldsa) == -1) return -1; + + memset (&sa, 0, HAWK_SIZEOF(sa)); + if (oldsa.sa_flags & SA_SIGINFO) + { + sa.sa_sigaction = dispatch_siginfo; + sa.sa_flags = SA_SIGINFO; + } + else + { + sa.sa_handler = dispatch_signal; + sa.sa_flags = 0; + } + sa.sa_flags |= extra_flags; + /*sa.sa_flags |= SA_INTERUPT; + sa.sa_flags |= SA_RESTART;*/ + sigfillset (&sa.sa_mask); /* block all signals while the handler is being executed */ + + if (sigaction(sig, &sa, HAWK_NULL) == -1) return -1; + + g_sig_state[sig].handler = (hawk_uintptr_t)handler; + if (oldsa.sa_flags & SA_SIGINFO) + g_sig_state[sig].old_handler = (hawk_uintptr_t)oldsa.sa_sigaction; + else + g_sig_state[sig].old_handler = (hawk_uintptr_t)oldsa.sa_handler; + + g_sig_state[sig].old_sa_mask = oldsa.sa_mask; + g_sig_state[sig].old_sa_flags = oldsa.sa_flags; + #else + g_sig_state[sig].old_handler = (hawk_uintptr_t)signal(sig, handler); + g_sig_state[sig].handler = (hawk_uintptr_t)dispatch_signal; + #endif + } + + return 0; +} + +static int unset_signal_handler (int sig) +{ +#if defined(HAVE_SIGACTION) + struct sigaction sa; +#endif + + if (!g_sig_state[sig].handler) return -1; /* not set */ + +#if defined(HAVE_SIGACTION) + memset (&sa, 0, HAWK_SIZEOF(sa)); + sa.sa_mask = g_sig_state[sig].old_sa_mask; + sa.sa_flags = g_sig_state[sig].old_sa_flags; + + if (sa.sa_flags & SA_SIGINFO) + { + sa.sa_sigaction = (void(*)(int,siginfo_t*,void*))g_sig_state[sig].old_handler; + } + else + { + sa.sa_handler = (sig_handler_t)g_sig_state[sig].old_handler; + } + + if (sigaction(sig, &sa, HAWK_NULL) == -1) return -1; +#else + signal (sig, (sig_handler_t)g_sig_state[sig].old_handler); +#endif + + g_sig_state[sig].handler = 0; + /* keep other fields untouched */ + + return 0; +} + +/* ---------------------------------------------------------------------- */ + + +static void stop_run (int signo) +{ +#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__) + int e = errno; +#endif + + // TODO: hawk_cut_halt(g_cut); + +#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__) + errno = e; +#endif +} + +static void do_nothing (int unucut) +{ +} + +static void set_intr_run (void) +{ +#if defined(SIGTERM) + set_signal_handler (SIGTERM, stop_run, 0); +#endif +#if defined(SIGHUP) + set_signal_handler (SIGHUP, stop_run, 0); +#endif +#if defined(SIGINT) + set_signal_handler (SIGINT, stop_run, 0); +#endif +#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__) && defined(SIGPIPE) + set_signal_handler (SIGPIPE, do_nothing, 0); +#endif +} + +static void unset_intr_run (void) +{ +#if defined(SIGTERM) + unset_signal_handler (SIGTERM); +#endif +#if defined(SIGHUP) + unset_signal_handler (SIGHUP); +#endif +#if defined(SIGINT) + unset_signal_handler (SIGINT); +#endif +#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__) && defined(SIGPIPE) + unset_signal_handler (SIGPIPE); +#endif +} + + +struct xarg_t +{ + hawk_mmgr_t* mmgr; + hawk_bch_t** ptr; + hawk_oow_t size; + hawk_oow_t capa; +}; + +typedef struct xarg_t xarg_t; + +static int collect_into_xarg (const hawk_bcs_t* path, void* ctx) +{ + xarg_t* xarg = (xarg_t*)ctx; + + if (xarg->size <= xarg->capa) + { + hawk_bch_t** tmp; + + tmp = realloc(xarg->ptr, HAWK_SIZEOF(*tmp) * (xarg->capa + 128)); + if (tmp == HAWK_NULL) return -1; + + xarg->ptr = tmp; + xarg->capa += 128; + } + + xarg->ptr[xarg->size] = strdup(path->ptr); + if (xarg->ptr[xarg->size] == HAWK_NULL) return -1; + xarg->size++; + + return 0; +} + +static void purge_xarg (xarg_t* xarg) +{ + if (xarg->ptr) + { + hawk_oow_t i; + + for (i = 0; i < xarg->size; i++) free (xarg->ptr[i]); + free (xarg->ptr); + + xarg->size = 0; + xarg->capa = 0; + xarg->ptr = HAWK_NULL; + } +} + +static int expand_wildcards (int argc, hawk_bch_t* argv[], int glob, xarg_t* xarg) +{ + int i; + hawk_bcs_t tmp; + + for (i = 0; i < argc; i++) + { + int x; + +#if 0 + if (glob) + { + #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) + int glob_flags = HAWK_GLOB_TOLERANT | HAWK_GLOB_PERIOD | HAWK_GLOB_NOESCAPE | HAWK_GLOB_IGNORECASE; + #else + int glob_flags = HAWK_GLOB_TOLERANT | HAWK_GLOB_PERIOD; + #endif + + x = hawk_glob(argv[i], collect_into_xarg, xarg, glob_flags, xarg->mmgr, hawk_getdflcmgr()); + + if (x <= -1) return -1; + } + else x = 0; +#else + x = 0; +#endif + + if (x == 0) + { + /* not expanded. just use it as is */ + tmp.ptr = argv[i]; + tmp.len = hawk_count_bcstr(argv[i]); + if (collect_into_xarg(&tmp, xarg) <= -1) return -1; + } + } + + return 0; +} + +int main_cut(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) +{ + hawk_cut_t* cut = HAWK_NULL; + hawk_oow_t script_count; + int ret = -1; + struct arg_t arg; + xarg_t xarg; + int xarg_inited = 0; + hawk_mmgr_t* mmgr = hawk_get_sys_mmgr(); + hawk_cmgr_t* cmgr = hawk_get_cmgr_by_id(HAWK_CMGR_UTF8); + hawk_mmgr_t xma_mmgr; + + memset (&arg, 0, HAWK_SIZEOF(arg)); + ret = handle_args(argc, argv, real_argv0, &arg); + if (ret <= -1) return -1; + if (ret == 0) return 0; + + ret = -1; + + if (arg.memlimit > 0) + { + if (hawk_init_xma_mmgr(&xma_mmgr, arg.memlimit) <= -1) + { + hawk_main_print_error("cannot open memory heap\n"); + goto oops; + } + mmgr = &xma_mmgr; + } + + + cut = hawk_cut_openstdwithmmgr(mmgr, 0, cmgr, HAWK_NULL); + if (!cut) + { + hawk_main_print_error("cannot open stream editor\n"); + goto oops; + } + +//TODO hawk_cut_setopt(cut, HAWK_CUT_TRAIT, &arg.option); + + if (hawk_cut_compstd(cut, g_script.io, &script_count) <= -1) + { + const hawk_loc_t* errloc; + const hawk_bch_t* target; + hawk_bch_t exprbuf[128]; + + errloc = hawk_cut_geterrloc(cut); + + if (g_script.io[script_count].type == HAWK_CUT_IOSTD_FILEB) + { + target = g_script.io[script_count].u.fileb.path; + } + else + { + /* i dont' use HAWK_CUT_IOSTD_SIO for input */ + HAWK_ASSERT(g_script.io[script_count].type == HAWK_CUT_IOSTD_BCS); + hawk_fmt_uintmax_to_bcstr(exprbuf, HAWK_COUNTOF(exprbuf), + script_count, 10, -1, '\0', "expression #"); + target = exprbuf; + } + + if (errloc->line > 0 || errloc->colm > 0) + { + hawk_main_print_error("cannot compile %s - %s at line %lu column %lu\n", + target, hawk_cut_geterrbmsg(cut), (unsigned long)errloc->line, (unsigned long)errloc->colm + ); + } + else + { + hawk_main_print_error("cannot compile %s - %s\n", target, hawk_cut_geterrbmsg(cut)); + } + goto oops; + } + + memset(&xarg, 0, HAWK_SIZEOF(xarg)); + xarg.mmgr = hawk_cut_getmmgr(cut); + xarg_inited = 1; + + if (arg.separate && arg.infile_pos > 0) + { + /* 's' and input files are specified on the command line */ + hawk_cut_iostd_t out_file; + hawk_cut_iostd_t out_inplace; + hawk_cut_iostd_t* output_file = HAWK_NULL; + hawk_cut_iostd_t* output = HAWK_NULL; + int inpos; + + /* a dash is treated specially for HAWK_CUT_IOSTD_FILE in + * hawk_cut_execstd(). so make an exception here */ + if (arg.output_file && hawk_comp_bcstr(arg.output_file, "-", 0) != 0) + { + out_file.type = HAWK_CUT_IOSTD_SIO; + out_file.u.sio = hawk_sio_open( + hawk_cut_getgem(cut), + 0, + (const hawk_ooch_t*)arg.output_file, /* fake type casting - note HAWK_SIO_BCSTRPATH below */ + HAWK_SIO_WRITE | + HAWK_SIO_CREATE | + HAWK_SIO_TRUNCATE | + HAWK_SIO_IGNOREECERR | + HAWK_SIO_BCSTRPATH + ); + if (out_file.u.sio == HAWK_NULL) + { + hawk_main_print_error("cannot open %s\n", arg.output_file); + goto oops; + } + + output_file = &out_file; + output = output_file; + } + + /* perform wild-card expansions for non-unix platforms */ + if (expand_wildcards(argc - arg.infile_pos, &argv[arg.infile_pos], arg.wildcard, &xarg) <= -1) + { + hawk_main_print_error("out of memory\n"); + goto oops; + } + + for (inpos = 0; inpos < xarg.size; inpos++) + { + hawk_cut_iostd_t in[2]; + hawk_bch_t* tmpl_tmpfile; + + in[0].type = HAWK_CUT_IOSTD_FILEB; + in[0].u.fileb.path = xarg.ptr[inpos]; + in[0].u.fileb.cmgr = g_infile_cmgr; + in[1].type = HAWK_CUT_IOSTD_NULL; + + tmpl_tmpfile = HAWK_NULL; + if (arg.inplace && in[0].u.file.path) + { + int retried = 0; + const hawk_bch_t* f[3]; + + f[0] = in[0].u.fileb.path; + f[1] = ".XXXX"; + f[2] = HAWK_NULL; + + tmpl_tmpfile = hawk_gem_dupbcstrarr(hawk_cut_getgem(cut), f, HAWK_NULL); + if (tmpl_tmpfile == HAWK_NULL) + { + hawk_main_print_error("out of memory\n"); + goto oops; + } + + open_temp: + out_inplace.type = HAWK_CUT_IOSTD_SIO; + out_inplace.u.sio = hawk_sio_open( + hawk_cut_getgem(cut), + 0, + (const hawk_ooch_t*)tmpl_tmpfile, /* fake type casting - note HAWK_SIO_BCSTRPATH below */ + HAWK_SIO_WRITE | + HAWK_SIO_CREATE | + HAWK_SIO_IGNOREECERR | + HAWK_SIO_EXCLUSIVE | + HAWK_SIO_TEMPORARY | + HAWK_SIO_BCSTRPATH + ); + if (out_inplace.u.sio == HAWK_NULL) + { + if (retried) + { + hawk_main_print_error("cannot open %s\n", tmpl_tmpfile); + hawk_cut_freemem(cut, tmpl_tmpfile); + goto oops; + } + else + { + /* retry to open the file with shorter names */ + hawk_cut_freemem(cut, tmpl_tmpfile); + tmpl_tmpfile = hawk_gem_dupbcstr(hawk_cut_getgem(cut), "TMP-XXXX", HAWK_NULL); + if (tmpl_tmpfile == HAWK_NULL) + { + hawk_main_print_error("out of memory\n"); + goto oops; + } + retried = 1; + goto open_temp; + } + } + + output = &out_inplace; + } + + if (hawk_cut_execstd(cut, in, output) <= -1) + { + if (output) hawk_sio_close(output->u.sio); + + if (tmpl_tmpfile) + { + unlink(tmpl_tmpfile); + hawk_cut_freemem(cut, tmpl_tmpfile); + } + print_exec_error(cut); + goto oops; + } + + if (tmpl_tmpfile) + { + HAWK_ASSERT(output == &out_inplace); + hawk_sio_close(output->u.sio); + output = output_file; + + if (rename(tmpl_tmpfile, in[0].u.fileb.path) <= -1) + { + hawk_main_print_error("cannot rename %s to %s. not deleting %s\n", + tmpl_tmpfile, in[0].u.fileb.path, tmpl_tmpfile); + hawk_cut_freemem(cut, tmpl_tmpfile); + goto oops; + } + + hawk_cut_freemem(cut, tmpl_tmpfile); + } + + // TODO: if (hawk_cut_ishalt(cut)) break; + } + + if (output) hawk_sio_close(output->u.sio); + } + else + { + int xx; + hawk_cut_iostd_t* in = HAWK_NULL; + hawk_cut_iostd_t out; + + if (arg.infile_pos > 0) + { + int i; + const hawk_bch_t* tmp; + + /* input files are specified on the command line */ + + /* perform wild-card expansions for non-unix platforms */ + if (expand_wildcards(argc - arg.infile_pos, &argv[arg.infile_pos], arg.wildcard, &xarg) <= -1) + { + hawk_main_print_error("out of memory\n"); + goto oops; + } + + in = (hawk_cut_iostd_t*)hawk_cut_allocmem(cut, HAWK_SIZEOF(*in) * (xarg.size + 1)); + if (in == HAWK_NULL) + { + hawk_main_print_error("out of memory\n"); + goto oops; + } + + for (i = 0; i < xarg.size; i++) + { + in[i].type = HAWK_CUT_IOSTD_FILEB; + tmp = xarg.ptr[i]; + in[i].u.fileb.path = tmp; + in[i].u.fileb.cmgr = g_infile_cmgr; + } + + in[i].type = HAWK_CUT_IOSTD_NULL; + } + + if (arg.output_file) + { + out.type = HAWK_CUT_IOSTD_FILEB; + out.u.fileb.path = arg.output_file; + out.u.fileb.cmgr = g_outfile_cmgr; + } + else + { + /* arrange to be able to specify cmgr. + * if not for cmgr, i could simply pass HAWK_NULL + * to hawk_cut_execstd() below like + * xx = hawk_cut_execstd (cut, in, HAWK_NULL); */ + out.type = HAWK_CUT_IOSTD_FILEB; + out.u.fileb.path = HAWK_NULL; + out.u.fileb.cmgr = g_outfile_cmgr; + } + + g_cut = cut; + set_intr_run(); + xx = hawk_cut_execstd(cut, in, &out); + if (in) hawk_cut_freemem(cut, in); + unset_intr_run(); + g_cut = HAWK_NULL; + + if (xx <= -1) + { + print_exec_error(cut); + goto oops; + } + } + + ret = 0; + +oops: + if (xarg_inited) purge_xarg(&xarg); + if (cut) hawk_cut_close(cut); + if (arg.memlimit > 0) + { + if (arg.debug) hawk_xma_dump(xma_mmgr.ctx, hawk_main_print_xma, HAWK_NULL); + hawk_fini_xma_mmgr(&xma_mmgr); + } + free_scripts(); + + return ret; +} diff --git a/bin/hawk.c b/bin/hawk.c index 6a85f7d4..6621ae7e 100644 --- a/bin/hawk.c +++ b/bin/hawk.c @@ -470,29 +470,10 @@ static void dprint_return (hawk_rtx_t* rtx, hawk_val_t* ret) #if defined(ENABLE_CALLBACK) static void on_statement (hawk_rtx_t* rtx, hawk_nde_t* nde) { - hawk_logfmt (hawk_rtx_gethawk(rtx), HAWK_LOG_STDERR, HAWK_T("running %d at line %zu\n"), (int)nde->type, (hawk_oow_t)nde->loc.line); } #endif -static void print_error (const hawk_bch_t* fmt, ...) -{ - va_list va; - fprintf (stderr, "ERROR: "); - va_start (va, fmt); - vfprintf (stderr, fmt, va); - va_end (va); -} - -static void print_warning (const hawk_bch_t* fmt, ...) -{ - va_list va; - fprintf (stderr, "WARNING: "); - va_start (va, fmt); - vfprintf (stderr, fmt, va); - va_end (va); -} - struct opttab_t { const hawk_bch_t* name; @@ -702,7 +683,7 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar isf = (hawk_parsestd_t*)malloc(HAWK_SIZEOF(*isf) * isfc); if (!isf) { - print_error ("out of memory\n"); + hawk_main_print_error("out of memory\n"); goto oops; } @@ -730,7 +711,7 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar tmp = (hawk_parsestd_t*)realloc(isf, HAWK_SIZEOF(*isf) * (isfc + 16)); if (tmp == HAWK_NULL) { - print_error ("out of memory\n"); + hawk_main_print_error("out of memory\n"); goto oops; } @@ -758,7 +739,7 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar tmp.len = hawk_count_bcstr(opt.arg); if (collect_into_xarg(&tmp, &arg->ocf) <= -1) { - print_error ("out of memory\n"); + hawk_main_print_error("out of memory\n"); goto oops; } arg->ocf.ptr[arg->ocf.size] = HAWK_NULL; @@ -779,9 +760,9 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar if (eq == HAWK_NULL) { if (opt.lngopt) - print_error ("no value for '%s' in '%s'\n", opt.arg, opt.lngopt); + hawk_main_print_error("no value for '%s' in '%s'\n", opt.arg, opt.lngopt); else - print_error ("no value for '%s' in '%c'\n", opt.arg, opt.opt); + hawk_main_print_error("no value for '%s' in '%c'\n", opt.arg, opt.opt); goto oops; } @@ -796,7 +777,7 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar tmp = realloc(arg->gvm.ptr, HAWK_SIZEOF(*tmp) * newcapa); if (!tmp) { - print_error ("out of memory\n"); + hawk_main_print_error("out of memory\n"); goto oops; } @@ -842,7 +823,7 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar arg->script_cmgr = hawk_get_cmgr_by_bcstr(opt.arg); if (arg->script_cmgr == HAWK_NULL) { - print_error ("unknown script encoding - %s\n", opt.arg); + hawk_main_print_error("unknown script encoding - %s\n", opt.arg); oops_ret = 3; goto oops; } @@ -852,7 +833,7 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar arg->console_cmgr = hawk_get_cmgr_by_bcstr(opt.arg); if (arg->console_cmgr == HAWK_NULL) { - print_error ("unknown console encoding - %s\n", opt.arg); + hawk_main_print_error("unknown console encoding - %s\n", opt.arg); oops_ret = 3; goto oops; } @@ -881,7 +862,7 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar arg->opton |= opttab[i].opt; else { - print_error ("invalid value '%s' for '%s' - use 'on' or 'off'\n", opt.arg, opt.lngopt); + hawk_main_print_error("invalid value '%s' for '%s' - use 'on' or 'off'\n", opt.arg, opt.lngopt); oops_ret = 3; goto oops; } @@ -896,9 +877,9 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar case '?': { if (opt.lngopt) - print_error ("illegal option - '%s'\n", opt.lngopt); + hawk_main_print_error("illegal option - '%s'\n", opt.lngopt); else - print_error ("illegal option - '%c'\n", opt.opt); + hawk_main_print_error("illegal option - '%c'\n", opt.opt); goto oops; } @@ -906,9 +887,9 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar case ':': { if (opt.lngopt) - print_error ("bad argument for '%s'\n", opt.lngopt); + hawk_main_print_error("bad argument for '%s'\n", opt.lngopt); else - print_error ("bad argument for '%c'\n", opt.opt); + hawk_main_print_error("bad argument for '%c'\n", opt.opt); goto oops; } @@ -948,7 +929,7 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar /* the remaining arguments are input console file names */ if (expand_wildcard(argc - opt.ind, &argv[opt.ind], do_glob, &arg->icf) <= -1) { - print_error ("failed to expand wildcard\n"); + hawk_main_print_error("failed to expand wildcard\n"); goto oops; } } @@ -1057,7 +1038,7 @@ int main_hawk(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) { if (hawk_init_xma_mmgr(&xma_mmgr, arg.memlimit) <= -1) { - print_error ("cannot open memory heap\n"); + hawk_main_print_error("cannot open memory heap\n"); goto oops; } mmgr = &xma_mmgr; @@ -1068,7 +1049,7 @@ int main_hawk(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) hawk = hawk_openstdwithmmgr(mmgr, 0, hawk_get_cmgr_by_id(HAWK_CMGR_UTF8), HAWK_NULL); if (HAWK_UNLIKELY(!hawk)) { - print_error ("cannot open hawk\n"); + hawk_main_print_error("cannot open hawk\n"); goto oops; } diff --git a/bin/main.c b/bin/main.c index 82ba3756..5c9a2726 100644 --- a/bin/main.c +++ b/bin/main.c @@ -106,6 +106,7 @@ static struct { int (*main) (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0); } entry_funcs[] = { { "awk", main_hawk }, + { "cut", main_cut }, { "hawk", main_hawk }, { "sed", main_sed }, { "usage", main_usage }, @@ -150,12 +151,12 @@ int main(int argc, hawk_bch_t* argv[]) #if defined(_WIN32) if (WSAStartup(MAKEWORD(2,0), &wsadata) != 0) - print_warning("Failed to start up winsock\n"); + hawk_main_print_warning("Failed to start up winsock\n"); else sock_inited = 1; #elif defined(__DOS__) /* TODO: add an option to skip watt-32 */ _watt_do_exit = 0; /* prevent sock_init from exiting upon failure */ - if (sock_init() != 0) print_warning("Failed to initialize watt-32\n"); + if (sock_init() != 0) hawk_main_print_warning("Failed to initialize watt-32\n"); else sock_inited = 1; #endif diff --git a/bin/sed.c b/bin/sed.c index 263cecd9..29c2fcd8 100644 --- a/bin/sed.c +++ b/bin/sed.c @@ -98,24 +98,6 @@ struct arg_t /* ------------------------------------------------------------------- */ -static void print_error(const hawk_bch_t* fmt, ...) -{ - va_list va; - fprintf (stderr, "ERROR: "); - va_start (va, fmt); - vfprintf (stderr, fmt, va); - va_end (va); -} - -static void print_warning (const hawk_bch_t* fmt, ...) -{ - va_list va; - fprintf (stderr, "WARNING: "); - va_start (va, fmt); - vfprintf (stderr, fmt, va); - va_end (va); -} - static void print_usage (FILE* out, const hawk_bch_t* argv0, const hawk_bch_t* real_argv0) { const hawk_bch_t* b1 = hawk_get_base_name_bcstr(real_argv0? real_argv0: argv0); @@ -164,7 +146,7 @@ static int add_script (const hawk_bch_t* str, int mem) tmp = (hawk_sed_iostd_t*)realloc(g_script.io, HAWK_SIZEOF(*g_script.io) * (g_script.capa + 16 + 1)); if (tmp == HAWK_NULL) { - print_error("out of memory while processing %s\n", str); + hawk_main_print_error("out of memory while processing %s\n", str); return -1; } @@ -228,21 +210,21 @@ static int handle_args (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_arg switch (c) { default: - print_usage (stderr, argv[0], real_argv0); + print_usage(stderr, argv[0], real_argv0); goto oops; case '?': - print_error("bad option - %c\n", opt.opt); - print_usage (stderr, argv[0], real_argv0); + hawk_main_print_error("bad option - %c\n", opt.opt); + print_usage(stderr, argv[0], real_argv0); goto oops; case ':': - print_error("bad parameter for %c\n", opt.opt); - print_usage (stderr, argv[0], real_argv0); + hawk_main_print_error("bad parameter for %c\n", opt.opt); + print_usage(stderr, argv[0], real_argv0); goto oops; case 'h': - print_usage (stdout, argv[0], real_argv0); + print_usage(stdout, argv[0], real_argv0); goto done; case 'D': @@ -302,7 +284,7 @@ static int handle_args (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_arg arg->trace = 1; break; #else - print_usage (stderr, argv[0], real_argv0); + print_usage(stderr, argv[0], real_argv0); goto oops; #endif @@ -321,7 +303,7 @@ static int handle_args (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_arg g_script_cmgr = hawk_get_cmgr_by_bcstr(opt.arg); if (g_script_cmgr == HAWK_NULL) { - print_error("unknown script encoding - %s\n", opt.arg); + hawk_main_print_error("unknown script encoding - %s\n", opt.arg); goto oops; } } @@ -330,7 +312,7 @@ static int handle_args (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_arg g_infile_cmgr = hawk_get_cmgr_by_bcstr(opt.arg); if (g_infile_cmgr == HAWK_NULL) { - print_error("unknown input file encoding - %s\n", opt.arg); + hawk_main_print_error("unknown input file encoding - %s\n", opt.arg); goto oops; } } @@ -339,7 +321,7 @@ static int handle_args (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_arg g_outfile_cmgr = hawk_get_cmgr_by_bcstr(opt.arg); if (g_outfile_cmgr == HAWK_NULL) { - print_error("unknown output file encoding - %s\n", opt.arg); + hawk_main_print_error("unknown output file encoding - %s\n", opt.arg); goto oops; } } @@ -357,7 +339,7 @@ static int handle_args (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_arg if (g_script.size <= 0) { - print_usage (stderr, argv[0], real_argv0); + print_usage(stderr, argv[0], real_argv0); goto oops; } @@ -373,17 +355,17 @@ done: return 0; } -void print_exec_error(hawk_sed_t* sed) +static void print_exec_error(hawk_sed_t* sed) { const hawk_loc_t* errloc = hawk_sed_geterrloc(sed); if (errloc->line > 0 || errloc->colm > 0) { - print_error("cannot execute - %s at line %lu column %lu\n", + hawk_main_print_error("cannot execute - %s at line %lu column %lu\n", hawk_sed_geterrbmsg(sed), (unsigned long)errloc->line, (unsigned long)errloc->colm); } else { - print_error("cannot execute - %s\n", hawk_sed_geterrbmsg(sed)); + hawk_main_print_error("cannot execute - %s\n", hawk_sed_geterrbmsg(sed)); } } @@ -721,7 +703,7 @@ int main_sed(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) { if (hawk_init_xma_mmgr(&xma_mmgr, arg.memlimit) <= -1) { - print_error("cannot open memory heap\n"); + hawk_main_print_error("cannot open memory heap\n"); goto oops; } mmgr = &xma_mmgr; @@ -731,7 +713,7 @@ int main_sed(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) sed = hawk_sed_openstdwithmmgr(mmgr, 0, cmgr, HAWK_NULL); if (!sed) { - print_error("cannot open stream editor\n"); + hawk_main_print_error("cannot open stream editor\n"); goto oops; } @@ -760,13 +742,13 @@ int main_sed(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) if (errloc->line > 0 || errloc->colm > 0) { - print_error("cannot compile %s - %s at line %lu column %lu\n", + hawk_main_print_error("cannot compile %s - %s at line %lu column %lu\n", target, hawk_sed_geterrbmsg(sed), (unsigned long)errloc->line, (unsigned long)errloc->colm ); } else { - print_error("cannot compile %s - %s\n", target, hawk_sed_geterrbmsg(sed)); + hawk_main_print_error("cannot compile %s - %s\n", target, hawk_sed_geterrbmsg(sed)); } goto oops; } @@ -805,7 +787,7 @@ int main_sed(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) ); if (out_file.u.sio == HAWK_NULL) { - print_error("cannot open %s\n", arg.output_file); + hawk_main_print_error("cannot open %s\n", arg.output_file); goto oops; } @@ -816,7 +798,7 @@ int main_sed(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) /* perform wild-card expansions for non-unix platforms */ if (expand_wildcards(argc - arg.infile_pos, &argv[arg.infile_pos], arg.wildcard, &xarg) <= -1) { - print_error("out of memory\n"); + hawk_main_print_error("out of memory\n"); goto oops; } @@ -843,7 +825,7 @@ int main_sed(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) tmpl_tmpfile = hawk_gem_dupbcstrarr(hawk_sed_getgem(sed), f, HAWK_NULL); if (tmpl_tmpfile == HAWK_NULL) { - print_error("out of memory\n"); + hawk_main_print_error("out of memory\n"); goto oops; } @@ -864,7 +846,7 @@ int main_sed(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) { if (retried) { - print_error("cannot open %s\n", tmpl_tmpfile); + hawk_main_print_error("cannot open %s\n", tmpl_tmpfile); hawk_sed_freemem(sed, tmpl_tmpfile); goto oops; } @@ -875,7 +857,7 @@ int main_sed(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) tmpl_tmpfile = hawk_gem_dupbcstr(hawk_sed_getgem(sed), "TMP-XXXX", HAWK_NULL); if (tmpl_tmpfile == HAWK_NULL) { - print_error("out of memory\n"); + hawk_main_print_error("out of memory\n"); goto oops; } retried = 1; @@ -907,7 +889,7 @@ int main_sed(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) if (rename(tmpl_tmpfile, in[0].u.fileb.path) <= -1) { - print_error("cannot rename %s to %s. not deleting %s\n", + hawk_main_print_error("cannot rename %s to %s. not deleting %s\n", tmpl_tmpfile, in[0].u.fileb.path, tmpl_tmpfile); hawk_sed_freemem(sed, tmpl_tmpfile); goto oops; @@ -937,14 +919,14 @@ int main_sed(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0) /* perform wild-card expansions for non-unix platforms */ if (expand_wildcards(argc - arg.infile_pos, &argv[arg.infile_pos], arg.wildcard, &xarg) <= -1) { - print_error("out of memory\n"); + hawk_main_print_error("out of memory\n"); goto oops; } in = (hawk_sed_iostd_t*)hawk_sed_allocmem(sed, HAWK_SIZEOF(*in) * (xarg.size + 1)); if (in == HAWK_NULL) { - print_error("out of memory\n"); + hawk_main_print_error("out of memory\n"); goto oops; } diff --git a/lib/cut-prv.h b/lib/cut-prv.h index af18d903..046d20a6 100644 --- a/lib/cut-prv.h +++ b/lib/cut-prv.h @@ -37,8 +37,8 @@ struct hawk_cut_sel_blk_t { enum { - HAWK_SED_SEL_CHAR = HAWK_T('c'), - HAWK_SED_SEL_FIELD = HAWK_T('f') + HAWK_CUT_SEL_CHAR = HAWK_T('c'), + HAWK_CUT_SEL_FIELD = HAWK_T('f') } id; hawk_oow_t start; hawk_oow_t end; @@ -69,7 +69,25 @@ struct hawk_cut_t hawk_oow_t count; hawk_oow_t fcount; hawk_oow_t ccount; - } sel; + } sel; + + /** source text pointers */ + struct + { + hawk_cut_io_impl_t fun; /**< input stream handler */ + hawk_cut_io_arg_t arg; + hawk_ooch_t buf[1024]; + int eof; + + //hawk_cut_cid_t* cid; + //hawk_cut_unknown_cid_t unknown_cid; + + hawk_loc_t loc; /**< location */ + hawk_ooci_t cc; /**< last character read */ + const hawk_ooch_t* ptr; /**< beginning of the source text */ + const hawk_ooch_t* end; /**< end of the source text */ + const hawk_ooch_t* cur; /**< current source text pointer */ + } src; struct { diff --git a/lib/cut.c b/lib/cut.c index 5db4d5ec..b072848e 100644 --- a/lib/cut.c +++ b/lib/cut.c @@ -136,6 +136,37 @@ static void hawk_cut_fini (hawk_cut_t* cut) hawk_ooecs_fini(&cut->e.in.line); } +hawk_errstr_t hawk_cut_geterrstr (hawk_cut_t* cut) +{ + return cut->_gem.errstr; +} + +void hawk_cut_seterrbfmt (hawk_cut_t* cut, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_bch_t* fmt, ...) +{ + va_list ap; + va_start (ap, fmt); + hawk_gem_seterrbvfmt (hawk_cut_getgem(cut), errloc, errnum, fmt, ap); + va_end (ap); +} + +void hawk_cut_seterrufmt (hawk_cut_t* cut, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_uch_t* fmt, ...) +{ + va_list ap; + va_start (ap, fmt); + hawk_gem_seterruvfmt (hawk_cut_getgem(cut), errloc, errnum, fmt, ap); + va_end (ap); +} + +void hawk_cut_seterrbvfmt (hawk_cut_t* cut, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_bch_t* errfmt, va_list ap) +{ + hawk_gem_seterrbvfmt (hawk_cut_getgem(cut), errloc, errnum, errfmt, ap); +} + +void hawk_cut_seterruvfmt (hawk_cut_t* cut, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_uch_t* errfmt, va_list ap) +{ + hawk_gem_seterruvfmt (hawk_cut_getgem(cut), errloc, errnum, errfmt, ap); +} + void hawk_cut_setoption (hawk_cut_t* cut, int option) { cut->option = option; @@ -157,46 +188,149 @@ void hawk_cut_clear (hawk_cut_t* cut) hawk_ooecs_setcapa(&cut->e.in.line, DFL_LINE_CAPA); } -int hawk_cut_comp (hawk_cut_t* cut, const hawk_ooch_t* str, hawk_oow_t len) -{ - const hawk_ooch_t* p = str; - const hawk_ooch_t* xnd = str + len; - hawk_ooci_t c; - int sel = HAWK_SED_SEL_CHAR; +/* ---------------------------------------------------------------- */ + +#define CURSC(cut) ((cut)->src.cc) +#define NXTSC(cut,c,errret) \ + do { if (getnextsc(cut,&(c)) <= -1) return (errret); } while (0) +#define NXTSC_GOTO(cut,c,label) \ + do { if (getnextsc(cut,&(c)) <= -1) goto label; } while (0) +#define PEEPNXTSC(cut,c,errret) \ + do { if (peepnextsc(cut,&(c)) <= -1) return (errret); } while (0) + +static int open_script_stream (hawk_cut_t* cut) +{ + hawk_ooi_t n; + + n = cut->src.fun(cut, HAWK_CUT_IO_OPEN, &cut->src.arg, HAWK_NULL, 0); + if (n <= -1) return -1; + + cut->src.cur = cut->src.buf; + cut->src.end = cut->src.buf; + cut->src.cc = HAWK_OOCI_EOF; + cut->src.loc.line = 1; + cut->src.loc.colm = 0; + + cut->src.eof = 0; + return 0; +} + +static HAWK_INLINE int close_script_stream (hawk_cut_t* cut) +{ + return cut->src.fun(cut, HAWK_CUT_IO_CLOSE, &cut->src.arg, HAWK_NULL, 0); +} + +static int read_script_stream (hawk_cut_t* cut) +{ + hawk_ooi_t n; + + n = cut->src.fun(cut, HAWK_CUT_IO_READ, &cut->src.arg, cut->src.buf, HAWK_COUNTOF(cut->src.buf)); + if (n <= -1) return -1; /* error */ + + if (n == 0) + { + /* don't change cut->src.cur and cut->src.end. + * they remain the same on eof */ + cut->src.eof = 1; + return 0; /* eof */ + } + + cut->src.cur = cut->src.buf; + cut->src.end = cut->src.buf + n; + return 1; /* read something */ +} + +static int getnextsc (hawk_cut_t* cut, hawk_ooci_t* c) +{ + /* adjust the line and column number of the next + * character bacut on the current character */ + if (cut->src.cc == HAWK_T('\n')) + { + /* TODO: support different line end convension */ + cut->src.loc.line++; + cut->src.loc.colm = 1; + } + else + { + /* take note that if you keep on calling getnextsc() + * after HAWK_OOCI_EOF is read, this column number + * keeps increasing also. there should be a bug of + * reading more than necessary somewhere in the code + * if this happens. */ + cut->src.loc.colm++; + } + + if (cut->src.cur >= cut->src.end && !cut->src.eof) + { + /* read in more character if buffer is empty */ + if (read_script_stream(cut) <= -1) return -1; + } + + cut->src.cc = (cut->src.cur < cut->src.end)? (*cut->src.cur++): HAWK_OOCI_EOF; + + *c = cut->src.cc; + return 0; +} + +static int peepnextsc (hawk_cut_t* cut, hawk_ooci_t* c) +{ + if (cut->src.cur >= cut->src.end && !cut->src.eof) + { + /* read in more character if buffer is empty. + * it is ok to fill the buffer in the peeping + * function if it doesn't change cut->src.cc. */ + if (read_script_stream (cut) <= -1) return -1; + } + + /* no changes in line nubmers, the 'cur' pointer, and + * most importantly 'cc' unlike getnextsc(). */ + *c = (cut->src.cur < cut->src.end)? (*cut->src.cur): HAWK_OOCI_EOF; + return 0; +} + +/* ---------------------------------------------------------------- */ + +int hawk_cut_comp (hawk_cut_t* cut, hawk_cut_io_impl_t inf) +{ + hawk_ooci_t c; + hawk_ooci_t sel = HAWK_CUT_SEL_CHAR; + + if (inf == HAWK_NULL) + { + hawk_cut_seterrnum(cut, HAWK_NULL, HAWK_EINVAL); + return -1; + } -#define CC(x,y) (((x) <= (y))? ((hawk_ooci_t)*(x)): HAWK_OOCI_EOF) -#define NC(x,y) (((x) < (y))? ((hawk_ooci_t)*(++(x))): HAWK_OOCI_EOF) #define EOF(x) ((x) == HAWK_OOCI_EOF) #define MASK_START (1 << 1) #define MASK_END (1 << 2) #define MAX HAWK_TYPE_MAX(hawk_oow_t) /* free selector blocks compiled previously */ - free_all_selector_blocks (cut); + free_all_selector_blocks(cut); /* set the default delimiters */ cut->sel.din = HAWK_T(' '); cut->sel.dout = HAWK_T(' '); - /* if the selector string is empty, don't need to proceed */ - if (len <= 0) return 0; + /* open script */ + cut->src.fun = inf; + if (open_script_stream(cut) <= -1) return -1; + NXTSC_GOTO(cut, c, oops); - /* compile the selector string */ - xnd--; c = CC (p, xnd); while (1) { hawk_oow_t start = 0, end = 0; int mask = 0; - while (hawk_is_ooch_space(c)) c = NC(p, xnd); + while (hawk_is_ooch_space(c)) NXTSC_GOTO(cut, c, oops); if (EOF(c)) { if (cut->sel.count > 0) { - SETERR0 (cut, HAWK_CUT_ESELNV); + SETERR0(cut, HAWK_CUT_ESELNV); return -1; } - break; } @@ -205,46 +339,46 @@ int hawk_cut_comp (hawk_cut_t* cut, const hawk_ooch_t* str, hawk_oow_t len) /* the next character is the input delimiter. * the output delimiter defaults to the input * delimiter. */ - c = NC(p, xnd); + NXTSC_GOTO(cut, c, oops); if (EOF(c)) { - SETERR0 (cut, HAWK_CUT_ESELNV); + SETERR0(cut, HAWK_CUT_ESELNV); return -1; } cut->sel.din = c; cut->sel.dout = c; - c = NC(p, xnd); + NXTSC_GOTO(cut, c, oops); } else if (c == HAWK_T('D')) { /* the next two characters are the input and * the output delimiter each. */ - c = NC(p, xnd); + NXTSC_GOTO(cut, c, oops); if (EOF(c)) { - SETERR0 (cut, HAWK_CUT_ESELNV); + SETERR0(cut, HAWK_CUT_ESELNV); return -1; } cut->sel.din = c; - c = NC(p, xnd); + NXTSC_GOTO(cut, c, oops); if (EOF(c)) { - SETERR0 (cut, HAWK_CUT_ESELNV); + SETERR0(cut, HAWK_CUT_ESELNV); return -1; } cut->sel.dout = c; - c = NC(p, xnd); + NXTSC_GOTO(cut, c, oops); } else { if (c == HAWK_T('c') || c == HAWK_T('f')) { sel = c; - c = NC(p, xnd); - while (hawk_is_ooch_space(c)) c = NC(p, xnd); + NXTSC_GOTO(cut, c, oops); + while (hawk_is_ooch_space(c)) NXTSC_GOTO(cut, c, oops); } if (hawk_is_ooch_digit(c)) @@ -252,48 +386,45 @@ int hawk_cut_comp (hawk_cut_t* cut, const hawk_ooch_t* str, hawk_oow_t len) do { start = start * 10 + (c - HAWK_T('0')); - c = NC(p, xnd); + NXTSC_GOTO(cut, c, oops); } while (hawk_is_ooch_digit(c)); - while (hawk_is_ooch_space(c)) c = NC(p, xnd); + while (hawk_is_ooch_space(c)) NXTSC_GOTO(cut, c, oops); mask |= MASK_START; } else start++; if (c == HAWK_T('-')) { - c = NC(p, xnd); - while (hawk_is_ooch_space(c)) c = NC(p, xnd); + NXTSC_GOTO(cut, c, oops); + while (hawk_is_ooch_space(c)) NXTSC_GOTO(cut, c, oops); if (hawk_is_ooch_digit(c)) { do { end = end * 10 + (c - HAWK_T('0')); - c = NC(p, xnd); + NXTSC_GOTO(cut, c, oops); } while (hawk_is_ooch_digit(c)); mask |= MASK_END; } else end = MAX; - while (hawk_is_ooch_space(c)) c = NC(p, xnd); + while (hawk_is_ooch_space(c)) NXTSC_GOTO(cut, c, oops); } else end = start; if (!(mask & (MASK_START | MASK_END))) { - SETERR0 (cut, HAWK_CUT_ESELNV); + SETERR0(cut, HAWK_CUT_ESELNV); return -1; } if (cut->sel.lb->len >= HAWK_COUNTOF(cut->sel.lb->range)) { - if (add_selector_block (cut) <= -1) - { - return -1; - } + if (add_selector_block(cut) <= -1) return -1; } cut->sel.lb->range[cut->sel.lb->len].id = sel; @@ -301,15 +432,19 @@ int hawk_cut_comp (hawk_cut_t* cut, const hawk_ooch_t* str, hawk_oow_t len) cut->sel.lb->range[cut->sel.lb->len].end = end; cut->sel.lb->len++; cut->sel.count++; - if (sel == HAWK_SED_SEL_FIELD) cut->sel.fcount++; + if (sel == HAWK_CUT_SEL_FIELD) cut->sel.fcount++; else cut->sel.ccount++; } if (EOF(c)) break; - if (c == HAWK_T(',')) c = NC(p, xnd); + if (c == HAWK_T(',')) NXTSC_GOTO(cut, c, oops); } return 0; + +oops: + close_script_stream(cut); + return -1; } static int read_char (hawk_cut_t* cut, hawk_ooch_t* c) @@ -600,7 +735,7 @@ int hawk_cut_exec (hawk_cut_t* cut, hawk_cut_io_impl_t inf, hawk_cut_io_impl_t o goto done2; } - n = cut->e.out.fun (cut, HAWK_CUT_IO_OPEN, &cut->e.out.arg, HAWK_NULL, 0); + n = cut->e.out.fun(cut, HAWK_CUT_IO_OPEN, &cut->e.out.arg, HAWK_NULL, 0); if (n <= -1) { ret = -1; @@ -636,7 +771,7 @@ int hawk_cut_exec (hawk_cut_t* cut, hawk_cut_io_impl_t inf, hawk_cut_io_impl_t o for (i = 0; i < b->len; i++) { - if (b->range[i].id == HAWK_SED_SEL_CHAR) + if (b->range[i].id == HAWK_CUT_SEL_CHAR) { n = cut_chars ( cut, @@ -707,9 +842,9 @@ int hawk_cut_exec (hawk_cut_t* cut, hawk_cut_io_impl_t inf, hawk_cut_io_impl_t o } done: - cut->e.out.fun (cut, HAWK_CUT_IO_CLOSE, &cut->e.out.arg, HAWK_NULL, 0); + cut->e.out.fun(cut, HAWK_CUT_IO_CLOSE, &cut->e.out.arg, HAWK_NULL, 0); done2: - cut->e.in.fun (cut, HAWK_CUT_IO_CLOSE, &cut->e.in.arg, HAWK_NULL, 0); + cut->e.in.fun(cut, HAWK_CUT_IO_CLOSE, &cut->e.in.arg, HAWK_NULL, 0); done3: return ret; } diff --git a/lib/hawk-cut.h b/lib/hawk-cut.h index 1b5609ec..ed7e077c 100644 --- a/lib/hawk-cut.h +++ b/lib/hawk-cut.h @@ -372,8 +372,7 @@ HAWK_EXPORT void hawk_cut_clear ( */ HAWK_EXPORT int hawk_cut_comp ( hawk_cut_t* cut, /**< text cutter */ - const hawk_ooch_t* str, /**< selector pointer */ - hawk_oow_t len /**< selector length */ + hawk_cut_io_impl_t inf ); /** @@ -447,12 +446,12 @@ HAWK_EXPORT hawk_cut_t* hawk_cut_openstdwithmmgr ( ); /** - * The hawk_cut_compstd() function compiles a null-terminated selector. - * Call hawk_cut_comp() for a length delimited selector. + * The hawk_cut_compstd() function compiles a selector from input streams. */ HAWK_EXPORT int hawk_cut_compstd ( - hawk_cut_t* cut, - const hawk_ooch_t* str + hawk_cut_t* cut, + hawk_cut_iostd_t in[], + hawk_oow_t* count ); /** diff --git a/lib/sed.c b/lib/sed.c index 2c222883..16c6ef71 100644 --- a/lib/sed.c +++ b/lib/sed.c @@ -59,7 +59,7 @@ hawk_sed_t* hawk_sed_open (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, hawk_cmgr_t* c HAWK_MMGR_FREE (mmgr, sed); sed = HAWK_NULL; } - else HAWK_MEMSET (sed + 1, 0, xtnsize); + else HAWK_MEMSET(sed + 1, 0, xtnsize); } else if (errnum) *errnum = HAWK_ENOMEM; @@ -77,15 +77,15 @@ void hawk_sed_close (hawk_sed_t* sed) } do { ecb = hawk_sed_popecb(sed); } while (ecb); - HAWK_ASSERT (sed->ecb == (hawk_sed_ecb_t*)sed); + HAWK_ASSERT(sed->ecb == (hawk_sed_ecb_t*)sed); - hawk_sed_fini (sed); + hawk_sed_fini(sed); HAWK_MMGR_FREE (hawk_sed_getmmgr(sed), sed); } int hawk_sed_init (hawk_sed_t* sed, hawk_mmgr_t* mmgr, hawk_cmgr_t* cmgr) { - HAWK_MEMSET (sed, 0, HAWK_SIZEOF(*sed)); + HAWK_MEMSET(sed, 0, HAWK_SIZEOF(*sed)); sed->_instsize = HAWK_SIZEOF(*sed); sed->_gem.mmgr = mmgr; @@ -105,7 +105,7 @@ int hawk_sed_init (hawk_sed_t* sed, hawk_mmgr_t* mmgr, hawk_cmgr_t* cmgr) if (hawk_ooecs_init(&sed->tmp.lab, hawk_sed_getgem(sed), 0) <= -1) goto oops_2; if (hawk_map_init(&sed->tmp.labs, hawk_sed_getgem(sed), 128, 70, HAWK_SIZEOF(hawk_ooch_t), 1) <= -1) goto oops_3; - hawk_map_setstyle (&sed->tmp.labs, hawk_get_map_style(HAWK_MAP_STYLE_INLINE_KEY_COPIER)); + hawk_map_setstyle(&sed->tmp.labs, hawk_get_map_style(HAWK_MAP_STYLE_INLINE_KEY_COPIER)); /* init_append (sed); */ if (hawk_ooecs_init(&sed->e.txt.hold, hawk_sed_getgem(sed), 256) <= -1) goto oops_6; @@ -123,32 +123,32 @@ int hawk_sed_init (hawk_sed_t* sed, hawk_mmgr_t* mmgr, hawk_cmgr_t* cmgr) return 0; oops_7: - hawk_ooecs_fini (&sed->e.txt.hold); + hawk_ooecs_fini(&sed->e.txt.hold); oops_6: - hawk_map_fini (&sed->tmp.labs); + hawk_map_fini(&sed->tmp.labs); oops_3: - hawk_ooecs_fini (&sed->tmp.lab); + hawk_ooecs_fini(&sed->tmp.lab); oops_2: - hawk_ooecs_fini (&sed->tmp.rex); + hawk_ooecs_fini(&sed->tmp.rex); oops_1: return -1; } -void hawk_sed_fini (hawk_sed_t* sed) +void hawk_sed_fini(hawk_sed_t* sed) { - free_all_command_blocks (sed); - free_all_cids (sed); + free_all_command_blocks(sed); + free_all_cids(sed); if (sed->e.cutf.flds != sed->e.cutf.sflds) - hawk_sed_freemem (sed, sed->e.cutf.flds); + hawk_sed_freemem(sed, sed->e.cutf.flds); - hawk_ooecs_fini (&sed->e.txt.scratch); - hawk_ooecs_fini (&sed->e.txt.hold); + hawk_ooecs_fini(&sed->e.txt.scratch); + hawk_ooecs_fini(&sed->e.txt.hold); free_appends (sed); - hawk_map_fini (&sed->tmp.labs); - hawk_ooecs_fini (&sed->tmp.lab); - hawk_ooecs_fini (&sed->tmp.rex); + hawk_map_fini(&sed->tmp.labs); + hawk_ooecs_fini(&sed->tmp.lab); + hawk_ooecs_fini(&sed->tmp.rex); } hawk_errstr_t hawk_sed_geterrstr (hawk_sed_t* sed) @@ -160,7 +160,7 @@ void hawk_sed_seterrbfmt (hawk_sed_t* sed, const hawk_loc_t* errloc, hawk_errnum { va_list ap; va_start (ap, fmt); - hawk_gem_seterrbvfmt (hawk_sed_getgem(sed), errloc, errnum, fmt, ap); + hawk_gem_seterrbvfmt(hawk_sed_getgem(sed), errloc, errnum, fmt, ap); va_end (ap); } @@ -168,19 +168,19 @@ void hawk_sed_seterrufmt (hawk_sed_t* sed, const hawk_loc_t* errloc, hawk_errnum { va_list ap; va_start (ap, fmt); - hawk_gem_seterruvfmt (hawk_sed_getgem(sed), errloc, errnum, fmt, ap); + hawk_gem_seterruvfmt(hawk_sed_getgem(sed), errloc, errnum, fmt, ap); va_end (ap); } void hawk_sed_seterrbvfmt (hawk_sed_t* sed, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_bch_t* errfmt, va_list ap) { - hawk_gem_seterrbvfmt (hawk_sed_getgem(sed), errloc, errnum, errfmt, ap); + hawk_gem_seterrbvfmt(hawk_sed_getgem(sed), errloc, errnum, errfmt, ap); } void hawk_sed_seterruvfmt (hawk_sed_t* sed, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_uch_t* errfmt, va_list ap) { - hawk_gem_seterruvfmt (hawk_sed_getgem(sed), errloc, errnum, errfmt, ap); + hawk_gem_seterruvfmt(hawk_sed_getgem(sed), errloc, errnum, errfmt, ap); } @@ -209,7 +209,7 @@ int hawk_sed_setopt (hawk_sed_t* sed, hawk_sed_opt_t id, const void* value) return 0; } - hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_EINVAL); + hawk_sed_seterrnum(sed, HAWK_NULL, HAWK_EINVAL); return -1; } @@ -238,7 +238,7 @@ int hawk_sed_getopt (hawk_sed_t* sed, hawk_sed_opt_t id, void* value) return 0; }; - hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_EINVAL); + hawk_sed_seterrnum(sed, HAWK_NULL, HAWK_EINVAL); return -1; } @@ -283,7 +283,7 @@ static int matchtre ( int n; /*hawk_tre_match_t match[10] = { { 0, 0 }, };*/ hawk_tre_match_t match[10]; - HAWK_MEMSET (match, 0, HAWK_SIZEOF(match)); + HAWK_MEMSET(match, 0, HAWK_SIZEOF(match)); n = hawk_tre_execx(tre, str->ptr, str->len, match, HAWK_COUNTOF(match), opt, HAWK_NULL); if (n <= -1) @@ -295,7 +295,7 @@ static int matchtre ( return -1; } - HAWK_ASSERT (match[0].rm_so != -1); + HAWK_ASSERT(match[0].rm_so != -1); if (mat) { mat->ptr = &str->ptr[match[0].rm_so]; @@ -367,10 +367,7 @@ static int read_script_stream (hawk_sed_t* sed) { hawk_ooi_t n; - n = sed->src.fun ( - sed, HAWK_SED_IO_READ, &sed->src.arg, - sed->src.buf, HAWK_COUNTOF(sed->src.buf) - ); + n = sed->src.fun(sed, HAWK_SED_IO_READ, &sed->src.arg, sed->src.buf, HAWK_COUNTOF(sed->src.buf)); if (n <= -1) return -1; /* error */ if (n == 0) @@ -438,16 +435,14 @@ static void free_address (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) { if (cmd->a2.type == HAWK_SED_ADR_REX) { - HAWK_ASSERT (cmd->a2.u.rex != HAWK_NULL); - if (cmd->a2.u.rex != EMPTY_REX) - free_rex (sed, cmd->a2.u.rex); + HAWK_ASSERT(cmd->a2.u.rex != HAWK_NULL); + if (cmd->a2.u.rex != EMPTY_REX) free_rex(sed, cmd->a2.u.rex); cmd->a2.type = HAWK_SED_ADR_NONE; } if (cmd->a1.type == HAWK_SED_ADR_REX) { - HAWK_ASSERT (cmd->a1.u.rex != HAWK_NULL); - if (cmd->a1.u.rex != EMPTY_REX) - free_rex (sed, cmd->a1.u.rex); + HAWK_ASSERT(cmd->a1.u.rex != HAWK_NULL); + if (cmd->a1.u.rex != EMPTY_REX) free_rex(sed, cmd->a1.u.rex); cmd->a1.type = HAWK_SED_ADR_NONE; } } @@ -456,7 +451,7 @@ static int add_command_block (hawk_sed_t* sed) { hawk_sed_cmd_blk_t* b; - b = (hawk_sed_cmd_blk_t*) hawk_sed_callocmem (sed, HAWK_SIZEOF(*b)); + b = (hawk_sed_cmd_blk_t*)hawk_sed_callocmem(sed, HAWK_SIZEOF(*b)); if (b == HAWK_NULL) return -1; b->next = HAWK_NULL; @@ -476,13 +471,13 @@ static void free_all_command_blocks (hawk_sed_t* sed) { hawk_sed_cmd_blk_t* nxt = b->next; - while (b->len > 0) free_command (sed, &b->buf[--b->len]); - if (b != &sed->cmd.fb) hawk_sed_freemem (sed, b); + while (b->len > 0) free_command(sed, &b->buf[--b->len]); + if (b != &sed->cmd.fb) hawk_sed_freemem(sed, b); b = nxt; } - HAWK_MEMSET (&sed->cmd.fb, 0, HAWK_SIZEOF(sed->cmd.fb)); + HAWK_MEMSET(&sed->cmd.fb, 0, HAWK_SIZEOF(sed->cmd.fb)); sed->cmd.lb = &sed->cmd.fb; sed->cmd.lb->len = 0; sed->cmd.lb->next = HAWK_NULL; @@ -490,47 +485,40 @@ static void free_all_command_blocks (hawk_sed_t* sed) static void free_command (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) { - free_address (sed, cmd); + free_address(sed, cmd); switch (cmd->type) { case HAWK_SED_CMD_APPEND: case HAWK_SED_CMD_INSERT: case HAWK_SED_CMD_CHANGE: - if (cmd->u.text.ptr) - hawk_sed_freemem (sed, cmd->u.text.ptr); + if (cmd->u.text.ptr) hawk_sed_freemem(sed, cmd->u.text.ptr); break; case HAWK_SED_CMD_READ_FILE: case HAWK_SED_CMD_READ_FILELN: case HAWK_SED_CMD_WRITE_FILE: case HAWK_SED_CMD_WRITE_FILELN: - if (cmd->u.file.ptr) - hawk_sed_freemem (sed, cmd->u.file.ptr); + if (cmd->u.file.ptr) hawk_sed_freemem(sed, cmd->u.file.ptr); break; case HAWK_SED_CMD_BRANCH: case HAWK_SED_CMD_BRANCH_COND: - if (cmd->u.branch.label.ptr) - hawk_sed_freemem (sed, cmd->u.branch.label.ptr); + if (cmd->u.branch.label.ptr) hawk_sed_freemem(sed, cmd->u.branch.label.ptr); break; case HAWK_SED_CMD_SUBSTITUTE: - if (cmd->u.subst.file.ptr) - hawk_sed_freemem (sed, cmd->u.subst.file.ptr); - if (cmd->u.subst.rpl.ptr) - hawk_sed_freemem (sed, cmd->u.subst.rpl.ptr); - if (cmd->u.subst.rex && cmd->u.subst.rex != EMPTY_REX) - free_rex (sed, cmd->u.subst.rex); + if (cmd->u.subst.file.ptr) hawk_sed_freemem(sed, cmd->u.subst.file.ptr); + if (cmd->u.subst.rpl.ptr) hawk_sed_freemem(sed, cmd->u.subst.rpl.ptr); + if (cmd->u.subst.rex && cmd->u.subst.rex != EMPTY_REX) free_rex(sed, cmd->u.subst.rex); break; case HAWK_SED_CMD_TRANSLATE: - if (cmd->u.transet.ptr) - hawk_sed_freemem (sed, cmd->u.transet.ptr); + if (cmd->u.transet.ptr) hawk_sed_freemem(sed, cmd->u.transet.ptr); break; case HAWK_SED_CMD_CUT: - free_all_cut_selector_blocks (sed, cmd); + free_all_cut_selector_blocks(sed, cmd); break; default: @@ -546,7 +534,7 @@ static void free_all_cids (hawk_sed_t* sed) while (sed->src.cid) { hawk_sed_cid_t* next = sed->src.cid->next; - hawk_sed_freemem (sed, sed->src.cid); + hawk_sed_freemem(sed, sed->src.cid); sed->src.cid = next; } } @@ -651,7 +639,7 @@ static int pickup_rex ( hawk_oow_t chars_from_opening_bracket = 0; int bracket_state = 0; - hawk_ooecs_clear (buf); + hawk_ooecs_clear(buf); while (1) { @@ -856,7 +844,7 @@ static hawk_sed_adr_t* get_address (hawk_sed_t* sed, hawk_sed_adr_t* a, int exte NXTSC (sed, c, HAWK_NULL); if (!(c >= HAWK_T('0') && c <= HAWK_T('9'))) { - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EA2MOI); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EA2MOI); return HAWK_NULL; } @@ -907,7 +895,7 @@ do { \ if (sed->opt.trait & HAWK_SED_STRIPLS) { /* get the first non-space character */ - while (IS_SPACE(c)) NXTSC_GOTO (sed, c, oops); + while (IS_SPACE(c)) NXTSC_GOTO(sed, c, oops); } while (c != HAWK_OOCI_EOF) @@ -916,7 +904,7 @@ do { \ if (c == HAWK_T('\\')) { - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); if (c == HAWK_OOCI_EOF) { if (sed->opt.trait & HAWK_SED_KEEPTBS) @@ -935,16 +923,16 @@ do { \ /* if newline is not escaped, stop */ hawk_ooci_t dump; /* let's not pollute 'c' for ENSURELN check after done: */ - NXTSC_GOTO (sed, dump, oops); + NXTSC_GOTO(sed, dump, oops); goto done; } /* else carry on reading the next line */ - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); break; } - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } } while (c != HAWK_OOCI_EOF); @@ -980,16 +968,16 @@ static int get_label (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) /* label name is empty */ if (sed->opt.trait & HAWK_SED_STRICT) { - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_ELABEM); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_ELABEM); return -1; } /* empty label. noop command. don't register anything */ - hawk_ooecs_clear (&sed->tmp.lab); + hawk_ooecs_clear(&sed->tmp.lab); } else { - hawk_ooecs_clear (&sed->tmp.lab); + hawk_ooecs_clear(&sed->tmp.lab); do { if (hawk_ooecs_ccat(&sed->tmp.lab, c) == (hawk_oow_t)-1) return -1; @@ -1037,7 +1025,7 @@ static int terminate_command (hawk_sed_t* sed) while (IS_SPACE(c)) NXTSC (sed, c, -1); if (!IS_CMDTERM(c)) { - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_ESCEXP); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_ESCEXP); return -1; } @@ -1079,7 +1067,7 @@ static int get_branch_target (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) while (IS_LABCHAR(c)) { if (hawk_ooecs_ccat(t, c) == (hawk_oow_t)-1) goto oops; - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } if (terminate_command (sed) <= -1) goto oops; @@ -1118,7 +1106,7 @@ static int get_file (hawk_sed_t* sed, hawk_oocs_t* xstr) if (IS_CMDTERM(c)) { - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EFILEM); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EFILEM); goto oops; } @@ -1130,7 +1118,7 @@ static int get_file (hawk_sed_t* sed, hawk_oocs_t* xstr) if (c == HAWK_T('\0')) { /* the file name should not contain '\0' */ - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EFILIL); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EFILIL); goto oops; } @@ -1139,10 +1127,10 @@ static int get_file (hawk_sed_t* sed, hawk_oocs_t* xstr) if (c == HAWK_T('\\')) { - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); if (c == HAWK_T('\0') || c == HAWK_OOCI_EOF || IS_LINTERM(c)) { - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EFILIL); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EFILIL); goto oops; } @@ -1155,7 +1143,7 @@ static int get_file (hawk_sed_t* sed, hawk_oocs_t* xstr) goto oops; } - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } while (!IS_CMDTERM(c)); @@ -1209,12 +1197,12 @@ static int get_subst (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (delim == HAWK_T('\\')) { /* backspace is an illegal delimiter */ - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EBSDEL); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EBSDEL); goto oops; } t[0] = &sed->tmp.rex; - hawk_ooecs_clear (t[0]); + hawk_ooecs_clear(t[0]); t[1] = hawk_ooecs_open(hawk_sed_getgem(sed), 0, 32); if (t[1] == HAWK_NULL) goto oops; @@ -1223,7 +1211,7 @@ static int get_subst (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (pickup_rex(sed, delim, 1, cmd, t[1]) <= -1) goto oops; /* skip spaces before options */ - do { NXTSC_GOTO (sed, c, oops); } while (IS_SPACE(c)); + do { NXTSC_GOTO(sed, c, oops); } while (IS_SPACE(c)); /* get options */ do @@ -1231,22 +1219,22 @@ static int get_subst (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (c == HAWK_T('p')) { cmd->u.subst.p = 1; - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } else if (c == HAWK_T('i') || c == HAWK_T('I')) { cmd->u.subst.i = 1; - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } else if (c == HAWK_T('g')) { cmd->u.subst.g = 1; - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } else if (c == HAWK_T('k')) { cmd->u.subst.k = 1; - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } else if (c >= HAWK_T('0') && c <= HAWK_T('9')) { @@ -1255,7 +1243,7 @@ static int get_subst (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (cmd->u.subst.occ != 0) { /* multiple occurrence specifiers */ - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EOCSDU); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EOCSDU); goto oops; } @@ -1267,17 +1255,17 @@ static int get_subst (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (occ > HAWK_TYPE_MAX(unsigned short)) { /* occurrence specifier too large */ - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EOCSTL); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EOCSTL); goto oops; } - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } while (c >= HAWK_T('0') && c <= HAWK_T('9')); if (occ == 0) { /* zero not allowed as occurrence specifier */ - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EOCSZE); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EOCSZE); goto oops; } @@ -1285,7 +1273,7 @@ static int get_subst (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) } else if (c == HAWK_T('w')) { - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); if (get_file (sed, &cmd->u.subst.file) <= -1) goto oops; break; } @@ -1298,7 +1286,7 @@ static int get_subst (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (cmd->u.subst.file.ptr == HAWK_NULL && terminate_command (sed) <= -1) goto oops; - HAWK_ASSERT (cmd->u.subst.rex == HAWK_NULL); + HAWK_ASSERT(cmd->u.subst.rex == HAWK_NULL); if (HAWK_OOECS_LEN(t[0]) <= 0) cmd->u.subst.rex = EMPTY_REX; else @@ -1331,14 +1319,14 @@ static int get_transet (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (delim == HAWK_T('\\')) { /* backspace is an illegal delimiter */ - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EBSDEL); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EBSDEL); goto oops; } t = hawk_ooecs_open(hawk_sed_getgem(sed), 0, 32); if (t == HAWK_NULL) goto oops; - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); while (c != delim) { hawk_ooch_t b[2]; @@ -1347,7 +1335,7 @@ static int get_transet (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (c == HAWK_T('\\')) { - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); CHECK_CMDIC_ESCAPED (sed, cmd, c, goto oops); if (trans_escaped (sed, c, &c, HAWK_NULL) <= -1) goto oops; } @@ -1355,17 +1343,17 @@ static int get_transet (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) b[0] = c; if (hawk_ooecs_ncat(t, b, 2) == (hawk_oow_t)-1) goto oops; - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); for (pos = 1; c != delim; pos += 2) { CHECK_CMDIC (sed, cmd, c, goto oops); if (c == HAWK_T('\\')) { - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); CHECK_CMDIC_ESCAPED (sed, cmd, c, goto oops); if (trans_escaped (sed, c, &c, HAWK_NULL) <= -1) goto oops; } @@ -1373,22 +1361,22 @@ static int get_transet (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (pos >= HAWK_OOECS_LEN(t)) { /* source and target not the same length */ - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_ETSNSL); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_ETSNSL); goto oops; } HAWK_OOECS_CHAR(t,pos) = c; - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } if (pos < HAWK_OOECS_LEN(t)) { /* source and target not the same length */ - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_ETSNSL); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_ETSNSL); goto oops; } - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); if (terminate_command (sed) <= -1) goto oops; hawk_ooecs_yield (t, &cmd->u.transet, 0); @@ -1404,7 +1392,7 @@ static int add_cut_selector_block (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) { hawk_sed_cut_sel_t* b; - b = (hawk_sed_cut_sel_t*) hawk_sed_callocmem (sed, HAWK_SIZEOF(*b)); + b = (hawk_sed_cut_sel_t*)hawk_sed_callocmem(sed, HAWK_SIZEOF(*b)); if (b == HAWK_NULL) return -1; b->next = HAWK_NULL; @@ -1431,7 +1419,7 @@ static void free_all_cut_selector_blocks (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) for (b = cmd->u.cut.fb; b; b = next) { next = b->next; - hawk_sed_freemem (sed, b); + hawk_sed_freemem(sed, b); } cmd->u.cut.lb = HAWK_NULL; @@ -1455,7 +1443,7 @@ static int get_cut (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (delim == HAWK_T('\\')) { /* backspace is an illegal delimiter */ - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EBSDEL); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EBSDEL); goto oops; } @@ -1463,7 +1451,7 @@ static int get_cut (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) for (i = 0; i < HAWK_COUNTOF(cmd->u.cut.delim); i++) cmd->u.cut.delim[i] = HAWK_T(' '); - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); while (1) { hawk_oow_t start = 0, end = 0; @@ -1473,10 +1461,10 @@ static int get_cut (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) #define MAX HAWK_TYPE_MAX(hawk_oow_t) int mask = 0; - while (IS_SPACE(c)) NXTSC_GOTO (sed, c, oops); + while (IS_SPACE(c)) NXTSC_GOTO(sed, c, oops); if (c == HAWK_OOCI_EOF) { - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_ECSLNV); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_ECSLNV); goto oops; } @@ -1484,22 +1472,22 @@ static int get_cut (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) { int delim_idx = (c == HAWK_T('d'))? 0: 1; /* the next character is an input/output delimiter. */ - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); if (c == HAWK_OOCI_EOF) { - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_ECSLNV); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_ECSLNV); goto oops; } cmd->u.cut.delim[delim_idx] = c; - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } else { if (c == HAWK_T('c') || c == HAWK_T('f')) { sel = c; - NXTSC_GOTO (sed, c, oops); - while (IS_SPACE(c)) NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); + while (IS_SPACE(c)) NXTSC_GOTO(sed, c, oops); } if (hawk_is_ooch_digit(c)) @@ -1507,11 +1495,11 @@ static int get_cut (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) do { start = start * 10 + (c - HAWK_T('0')); - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } while (hawk_is_ooch_digit(c)); - while (IS_SPACE(c)) NXTSC_GOTO (sed, c, oops); + while (IS_SPACE(c)) NXTSC_GOTO(sed, c, oops); mask |= MASK_START; if (start >= 1) start--; /* convert it to index */ @@ -1520,22 +1508,22 @@ static int get_cut (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (c == HAWK_T('-')) { - NXTSC_GOTO (sed, c, oops); - while (IS_SPACE(c)) NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); + while (IS_SPACE(c)) NXTSC_GOTO(sed, c, oops); if (hawk_is_ooch_digit(c)) { do { end = end * 10 + (c - HAWK_T('0')); - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } while (hawk_is_ooch_digit(c)); mask |= MASK_END; } else end = MAX; - while (IS_SPACE(c)) NXTSC_GOTO (sed, c, oops); + while (IS_SPACE(c)) NXTSC_GOTO(sed, c, oops); if (end >= 1) end--; /* convert it to index */ } @@ -1544,7 +1532,7 @@ static int get_cut (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (!(mask & (MASK_START | MASK_END))) { /* invalid cut selector */ - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_ECSLNV); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_ECSLNV); goto oops; } @@ -1564,11 +1552,11 @@ static int get_cut (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) else cmd->u.cut.ccount++; } - while (IS_SPACE(c)) NXTSC_GOTO (sed, c, oops); + while (IS_SPACE(c)) NXTSC_GOTO(sed, c, oops); if (c == HAWK_OOCI_EOF) { - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_ECSLNV); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_ECSLNV); goto oops; } @@ -1576,14 +1564,14 @@ static int get_cut (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (c != HAWK_T(',')) { - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_ECSLNV); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_ECSLNV); goto oops; } - NXTSC_GOTO (sed, c, oops); /* skip a comma */ + NXTSC_GOTO(sed, c, oops); /* skip a comma */ } /* skip spaces before options */ - do { NXTSC_GOTO (sed, c, oops); } while (IS_SPACE(c)); + do { NXTSC_GOTO(sed, c, oops); } while (IS_SPACE(c)); /* get options */ do @@ -1602,7 +1590,7 @@ static int get_cut (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) } else break; - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } while (1); @@ -1634,7 +1622,7 @@ static int get_command (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) case HAWK_OOCI_EOF: case HAWK_T('\n'): - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_ECMDMS); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_ECMDMS); return -1; case HAWK_T(':'): @@ -1665,7 +1653,7 @@ static int get_command (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (sed->tmp.grp.level >= HAWK_COUNTOF(sed->tmp.grp.cmd)) { /* group nesting too deep */ - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EGRNTD); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EGRNTD); return -1; } @@ -1689,7 +1677,7 @@ static int get_command (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (sed->tmp.grp.level <= 0) { /* group not balanced */ - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EGRNBA); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EGRNBA); return -1; } @@ -1740,7 +1728,7 @@ static int get_command (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) goto sameline_ok; } - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EBSEXP); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EBSEXP); return -1; } @@ -1756,7 +1744,7 @@ static int get_command (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) goto sameline_ok; } - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EGBABS); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EGBABS); return -1; } @@ -1842,35 +1830,35 @@ int hawk_sed_comp (hawk_sed_t* sed, hawk_sed_io_impl_t inf) if (inf == HAWK_NULL) { - hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_EINVAL); + hawk_sed_seterrnum(sed, HAWK_NULL, HAWK_EINVAL); return -1; } /* free all the commands previously compiled */ - free_all_command_blocks (sed); - HAWK_ASSERT (sed->cmd.lb == &sed->cmd.fb && sed->cmd.lb->len == 0); + free_all_command_blocks(sed); + HAWK_ASSERT(sed->cmd.lb == &sed->cmd.fb && sed->cmd.lb->len == 0); /* free all the compilation identifiers */ - free_all_cids (sed); + free_all_cids(sed); /* clear the label table */ - hawk_map_clear (&sed->tmp.labs); + hawk_map_clear(&sed->tmp.labs); /* clear temporary data */ sed->tmp.grp.level = 0; - hawk_ooecs_clear (&sed->tmp.rex); + hawk_ooecs_clear(&sed->tmp.rex); /* open script */ sed->src.fun = inf; - if (open_script_stream (sed) <= -1) return -1; - NXTSC_GOTO (sed, c, oops); + if (open_script_stream(sed) <= -1) return -1; + NXTSC_GOTO(sed, c, oops); while (1) { int n; /* skip spaces including newlines */ - while (IS_WSPACE(c)) NXTSC_GOTO (sed, c, oops); + while (IS_WSPACE(c)) NXTSC_GOTO(sed, c, oops); /* check if the end has been reached */ if (c == HAWK_OOCI_EOF) break; @@ -1878,36 +1866,36 @@ int hawk_sed_comp (hawk_sed_t* sed, hawk_sed_io_impl_t inf) /* check if the line is commented out */ if (c == HAWK_T('#')) { - do NXTSC_GOTO (sed, c, oops); + do NXTSC_GOTO(sed, c, oops); while (!IS_LINTERM(c) && c != HAWK_OOCI_EOF) ; - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); continue; } if (c == HAWK_T(';')) { /* semicolon without a address-command pair */ - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); continue; } /* initialize the current command */ cmd = &sed->cmd.lb->buf[sed->cmd.lb->len]; - HAWK_MEMSET (cmd, 0, HAWK_SIZEOF(*cmd)); + HAWK_MEMSET(cmd, 0, HAWK_SIZEOF(*cmd)); /* process the first address */ a1_loc = sed->src.loc; if (get_address(sed, &cmd->a1, 0) == HAWK_NULL) { cmd = HAWK_NULL; - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EA1MOI); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EA1MOI); goto oops; } c = CURSC (sed); if (cmd->a1.type != HAWK_SED_ADR_NONE) { - while (IS_SPACE(c)) NXTSC_GOTO (sed, c, oops); + while (IS_SPACE(c)) NXTSC_GOTO(sed, c, oops); if (c == HAWK_T(',') || ((sed->opt.trait & HAWK_SED_EXTENDEDADR) && c == HAWK_T('~'))) @@ -1915,12 +1903,12 @@ int hawk_sed_comp (hawk_sed_t* sed, hawk_sed_io_impl_t inf) hawk_ooch_t delim = c; /* maybe an address range */ - do { NXTSC_GOTO (sed, c, oops); } while (IS_SPACE(c)); + do { NXTSC_GOTO(sed, c, oops); } while (IS_SPACE(c)); if (get_address (sed, &cmd->a2, (sed->opt.trait & HAWK_SED_EXTENDEDADR)) == HAWK_NULL) { - HAWK_ASSERT (cmd->a2.type == HAWK_SED_ADR_NONE); - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EA2MOI); + HAWK_ASSERT(cmd->a2.type == HAWK_SED_ADR_NONE); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EA2MOI); goto oops; } @@ -1928,7 +1916,7 @@ int hawk_sed_comp (hawk_sed_t* sed, hawk_sed_io_impl_t inf) { if (cmd->a2.type == HAWK_SED_ADR_NONE) { - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EA2MOI); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EA2MOI); goto oops; } if (cmd->a2.type == HAWK_SED_ADR_RELLINE || @@ -1947,7 +1935,7 @@ int hawk_sed_comp (hawk_sed_t* sed, hawk_sed_io_impl_t inf) if (cmd->a1.type != HAWK_SED_ADR_LINE || cmd->a2.type != HAWK_SED_ADR_LINE) { - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EA2MOI); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EA2MOI); goto oops; } @@ -1987,24 +1975,24 @@ int hawk_sed_comp (hawk_sed_t* sed, hawk_sed_io_impl_t inf) } else { - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EA1MOI); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EA1MOI); goto oops; } } /* skip white spaces */ - while (IS_SPACE(c)) NXTSC_GOTO (sed, c, oops); + while (IS_SPACE(c)) NXTSC_GOTO(sed, c, oops); if (c == HAWK_T('!')) { /* allow any number of the negation indicators */ do { cmd->negated = !cmd->negated; - NXTSC_GOTO (sed, c, oops); + NXTSC_GOTO(sed, c, oops); } while (c == HAWK_T('!')); - while (IS_SPACE(c)) NXTSC_GOTO (sed, c, oops); + while (IS_SPACE(c)) NXTSC_GOTO(sed, c, oops); } @@ -2029,16 +2017,16 @@ int hawk_sed_comp (hawk_sed_t* sed, hawk_sed_io_impl_t inf) if (sed->tmp.grp.level != 0) { /* group brackets not balanced - since it's not 0, probably no balancing closing brakcets */ - hawk_sed_seterrnum (sed, &sed->src.loc, HAWK_SED_EGRNBA); + hawk_sed_seterrnum(sed, &sed->src.loc, HAWK_SED_EGRNBA); goto oops; } - close_script_stream (sed); + close_script_stream(sed); return 0; oops: - if (cmd) free_address (sed, cmd); - close_script_stream (sed); + if (cmd) free_address(sed, cmd); + close_script_stream(sed); return -1; } @@ -2063,13 +2051,13 @@ static int read_char (hawk_sed_t* sed, hawk_ooch_t* c) } else if (sed->e.in.xbuf_len > 0) { - HAWK_ASSERT (sed->e.in.xbuf_len == 1); + HAWK_ASSERT(sed->e.in.xbuf_len == 1); *c = sed->e.in.xbuf[--sed->e.in.xbuf_len]; return 1; } else /*if (sed->e.in.xbuf_len < 0)*/ { - HAWK_ASSERT (sed->e.in.xbuf_len == -1); + HAWK_ASSERT(sed->e.in.xbuf_len == -1); return 0; } } @@ -2080,7 +2068,7 @@ static int read_line (hawk_sed_t* sed, int append) hawk_ooch_t c; int n; - if (!append) hawk_ooecs_clear (&sed->e.in.line); + if (!append) hawk_ooecs_clear(&sed->e.in.line); if (sed->e.in.eof) { #if 0 @@ -2185,7 +2173,7 @@ static int write_num (hawk_sed_t* sed, hawk_oow_t x, int base, int width) hawk_oow_t y = 0; int dig = 0; - HAWK_ASSERT (base >= 2 && base <= 36); + HAWK_ASSERT(base >= 2 && base <= 36); if (x < 0) { @@ -2320,7 +2308,7 @@ static int write_str_to_file ( { hawk_sed_io_arg_t arg; - HAWK_MEMSET (&arg, 0, HAWK_SIZEOF(arg)); + HAWK_MEMSET(&arg, 0, HAWK_SIZEOF(arg)); pair = hawk_map_insert(&sed->e.out.files, (void*)path, plen, &arg, HAWK_SIZEOF(arg)); if (pair == HAWK_NULL) @@ -2347,7 +2335,7 @@ static int write_str_to_file ( n = sed->e.out.fun(sed, HAWK_SED_IO_WRITE, ap, (hawk_ooch_t*)str, len); if (n <= -1) { - sed->e.out.fun (sed, HAWK_SED_IO_CLOSE, ap, HAWK_NULL, 0); + sed->e.out.fun(sed, HAWK_SED_IO_CLOSE, ap, HAWK_NULL, 0); ap->handle = HAWK_NULL; ADJERR_LOC (sed, &cmd->loc); return -1; @@ -2357,7 +2345,7 @@ static int write_str_to_file ( { /* eof is returned on the write stream. * it is also an error as it can't write any more */ - sed->e.out.fun (sed, HAWK_SED_IO_CLOSE, ap, HAWK_NULL, 0); + sed->e.out.fun(sed, HAWK_SED_IO_CLOSE, ap, HAWK_NULL, 0); ap->handle = HAWK_NULL; SETERR1 (sed, HAWK_SED_EIOFIL, (hawk_ooch_t*)path, plen, &cmd->loc); return -1; @@ -2419,7 +2407,7 @@ static int write_file (hawk_sed_t* sed, hawk_sed_cmd_t* cmd, int first_line) } done: - sed->e.in.fun (sed, HAWK_SED_IO_CLOSE, &arg, HAWK_NULL, 0); + sed->e.in.fun(sed, HAWK_SED_IO_CLOSE, &arg, HAWK_NULL, 0); return 0; } @@ -2463,7 +2451,7 @@ static void free_appends (hawk_sed_t* sed) while (app) { next = app->next; - hawk_sed_freemem (sed, app); + hawk_sed_freemem(sed, app); app = next; } @@ -2486,8 +2474,8 @@ static int emit_append (hawk_sed_t* sed, hawk_sed_app_t* app) return write_file(sed, app->cmd, 1); default: - HAWK_ASSERT (!"should never happen. app->cmd->type must be one of APPEND,READ_FILE,READ_FILELN"); - hawk_sed_seterrnum (sed, &app->cmd->loc, HAWK_EINTERN); + HAWK_ASSERT(!"should never happen. app->cmd->type must be one of APPEND,READ_FILE,READ_FILELN"); + hawk_sed_seterrnum(sed, &app->cmd->loc, HAWK_EINTERN); return -1; } } @@ -2548,9 +2536,9 @@ static int do_subst (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) const hawk_ooch_t* str_end; hawk_oow_t m, i, max_count, sub_count; - HAWK_ASSERT (cmd->type == HAWK_SED_CMD_SUBSTITUTE); + HAWK_ASSERT(cmd->type == HAWK_SED_CMD_SUBSTITUTE); - hawk_ooecs_clear (&sed->e.txt.scratch); + hawk_ooecs_clear(&sed->e.txt.scratch); lineterm = trim_line(sed, &str); @@ -2568,7 +2556,7 @@ static int do_subst (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) while (cur.ptr <= str_end) { hawk_oocs_t submat[9]; - HAWK_MEMSET (submat, 0, HAWK_SIZEOF(submat)); + HAWK_MEMSET(submat, 0, HAWK_SIZEOF(submat)); if (max_count == 0 || sub_count < max_count) { @@ -2580,7 +2568,7 @@ static int do_subst (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (rex == HAWK_NULL) { /* no previous regular expression */ - hawk_sed_seterrnum (sed, &cmd->loc, HAWK_SED_ENPREX); + hawk_sed_seterrnum(sed, &cmd->loc, HAWK_SED_ENPREX); return -1; } } @@ -2796,13 +2784,13 @@ static int split_into_fields_for_cut ( if (sed->e.cutf.flds == sed->e.cutf.sflds) { - tmp = hawk_sed_allocmem (sed, HAWK_SIZEOF(*tmp) * nsz); + tmp = hawk_sed_allocmem(sed, HAWK_SIZEOF(*tmp) * nsz); if (tmp == HAWK_NULL) return -1; HAWK_MEMCPY (tmp, sed->e.cutf.flds, HAWK_SIZEOF(*tmp) * sed->e.cutf.cflds); } else { - tmp = hawk_sed_reallocmem (sed, sed->e.cutf.flds, HAWK_SIZEOF(*tmp) * nsz); + tmp = hawk_sed_reallocmem(sed, sed->e.cutf.flds, HAWK_SIZEOF(*tmp) * nsz); if (tmp == HAWK_NULL) return -1; } @@ -2832,7 +2820,7 @@ static int do_cut (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) hawk_oocs_t str; int out_state; - hawk_ooecs_clear (&sed->e.txt.scratch); + hawk_ooecs_clear(&sed->e.txt.scratch); lineterm = trim_line(sed, &str); @@ -2847,7 +2835,7 @@ static int do_cut (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) /* if the 'd' option is set and the line is not * delimited by the input delimiter, delete the pattern * space and finish the current cycle */ - hawk_ooecs_clear (&sed->e.in.line); + hawk_ooecs_clear(&sed->e.in.line); return 0; } } @@ -2966,7 +2954,7 @@ static int match_a (hawk_sed_t* sed, hawk_sed_cmd_t* cmd, hawk_sed_adr_t* a) hawk_oocs_t line; void* rex; - HAWK_ASSERT (a->u.rex != HAWK_NULL); + HAWK_ASSERT(a->u.rex != HAWK_NULL); line.ptr = HAWK_OOECS_PTR(&sed->e.in.line); line.len = HAWK_OOECS_LEN(&sed->e.in.line); @@ -2983,7 +2971,7 @@ static int match_a (hawk_sed_t* sed, hawk_sed_cmd_t* cmd, hawk_sed_adr_t* a) rex = sed->e.last_rex; if (rex == HAWK_NULL) { - hawk_sed_seterrnum (sed, &cmd->loc, HAWK_SED_ENPREX); + hawk_sed_seterrnum(sed, &cmd->loc, HAWK_SED_ENPREX); return -1; } } @@ -3010,7 +2998,7 @@ static int match_a (hawk_sed_t* sed, hawk_sed_cmd_t* cmd, hawk_sed_adr_t* a) n = read_char (sed, &c); if (n <= -1) return -1; - HAWK_ASSERT (sed->e.in.xbuf_len == 0); + HAWK_ASSERT(sed->e.in.xbuf_len == 0); if (n == 0) { /* eof has been reached */ @@ -3027,7 +3015,7 @@ static int match_a (hawk_sed_t* sed, hawk_sed_cmd_t* cmd, hawk_sed_adr_t* a) case HAWK_SED_ADR_RELLINE: /* this address type should be seen only when matching * the second address */ - HAWK_ASSERT (cmd->state.a1_matched && cmd->state.a1_match_line >= 1); + HAWK_ASSERT(cmd->state.a1_matched && cmd->state.a1_match_line >= 1); return (sed->e.in.num >= cmd->state.a1_match_line + a->u.lno)? 1: 0; case HAWK_SED_ADR_RELLINEM: @@ -3036,8 +3024,8 @@ static int match_a (hawk_sed_t* sed, hawk_sed_cmd_t* cmd, hawk_sed_adr_t* a) * the second address */ hawk_oow_t tmp; - HAWK_ASSERT (cmd->state.a1_matched && cmd->state.a1_match_line >= 1); - HAWK_ASSERT (a->u.lno > 0); + HAWK_ASSERT(cmd->state.a1_matched && cmd->state.a1_match_line >= 1); + HAWK_ASSERT(a->u.lno > 0); /* TODO: is it better to store this value some in the state * not to calculate this every time?? */ @@ -3048,7 +3036,7 @@ static int match_a (hawk_sed_t* sed, hawk_sed_cmd_t* cmd, hawk_sed_adr_t* a) } default: - HAWK_ASSERT (a->type == HAWK_SED_ADR_NONE); + HAWK_ASSERT(a->type == HAWK_SED_ADR_NONE); return 1; /* match */ } } @@ -3062,18 +3050,18 @@ static int match_address (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) cmd->state.c_ready = 0; if (cmd->a1.type == HAWK_SED_ADR_NONE) { - HAWK_ASSERT (cmd->a2.type == HAWK_SED_ADR_NONE); + HAWK_ASSERT(cmd->a2.type == HAWK_SED_ADR_NONE); cmd->state.c_ready = 1; return 1; } else if (cmd->a2.type == HAWK_SED_ADR_STEP) { - HAWK_ASSERT (cmd->a1.type == HAWK_SED_ADR_LINE); + HAWK_ASSERT(cmd->a1.type == HAWK_SED_ADR_LINE); /* stepping address */ cmd->state.c_ready = 1; if (sed->e.in.num < cmd->a1.u.lno) return 0; - HAWK_ASSERT (cmd->a2.u.lno > 0); + HAWK_ASSERT(cmd->a2.u.lno > 0); if ((sed->e.in.num - cmd->a1.u.lno) % cmd->a2.u.lno == 0) return 1; return 0; } @@ -3197,7 +3185,7 @@ static hawk_sed_cmd_t* exec_cmd (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) } else { - hawk_ooecs_clear (&sed->e.in.line); + hawk_ooecs_clear(&sed->e.in.line); } /* move past the last command so as to start @@ -3238,7 +3226,7 @@ static hawk_sed_cmd_t* exec_cmd (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) } case HAWK_SED_CMD_DELETE: /* delete the pattern space */ - hawk_ooecs_clear (&sed->e.in.line); + hawk_ooecs_clear(&sed->e.in.line); /* finish the current cycle */ jumpto = &sed->cmd.over; @@ -3333,7 +3321,7 @@ static hawk_sed_cmd_t* exec_cmd (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) break; case HAWK_SED_CMD_NEXT: - if (emit_output (sed, 0) <= -1) return HAWK_NULL; + if (emit_output(sed, 0) <= -1) return HAWK_NULL; /* read the next line and fill the pattern space */ n = read_line (sed, 0); @@ -3347,7 +3335,7 @@ static hawk_sed_cmd_t* exec_cmd (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) case HAWK_SED_CMD_NEXT_APPEND: /* append the next line to the pattern space */ - if (emit_output (sed, 1) <= -1) return HAWK_NULL; + if (emit_output(sed, 1) <= -1) return HAWK_NULL; n = read_line (sed, 1); if (n <= -1) return HAWK_NULL; @@ -3404,7 +3392,7 @@ static hawk_sed_cmd_t* exec_cmd (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) if (!sed->e.subst_done) break; sed->e.subst_done = 0; case HAWK_SED_CMD_BRANCH: - HAWK_ASSERT (cmd->u.branch.target != HAWK_NULL); + HAWK_ASSERT(cmd->u.branch.target != HAWK_NULL); jumpto = cmd->u.branch.target; break; @@ -3447,7 +3435,7 @@ static hawk_sed_cmd_t* exec_cmd (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) case HAWK_SED_CMD_CLEAR_PATTERN: /* clear pattern space */ - hawk_ooecs_clear (&sed->e.in.line); + hawk_ooecs_clear(&sed->e.in.line); break; case HAWK_SED_CMD_CUT: @@ -3464,12 +3452,12 @@ static hawk_sed_cmd_t* exec_cmd (hawk_sed_t* sed, hawk_sed_cmd_t* cmd) static void close_outfile (hawk_map_t* map, void* dptr, hawk_oow_t dlen) { hawk_sed_io_arg_t* arg = dptr; - HAWK_ASSERT (dlen == HAWK_SIZEOF(*arg)); + HAWK_ASSERT(dlen == HAWK_SIZEOF(*arg)); if (arg->handle) { hawk_sed_t* sed = *(hawk_sed_t**)(map + 1); - sed->e.out.fun (sed, HAWK_SED_IO_CLOSE, arg, HAWK_NULL, 0); + sed->e.out.fun(sed, HAWK_SED_IO_CLOSE, arg, HAWK_NULL, 0); arg->handle = HAWK_NULL; } } @@ -3478,7 +3466,7 @@ static int init_command_block_for_exec (hawk_sed_t* sed, hawk_sed_cmd_blk_t* b) { hawk_oow_t i; - HAWK_ASSERT (b->len <= HAWK_COUNTOF(b->buf)); + HAWK_ASSERT(b->len <= HAWK_COUNTOF(b->buf)); for (i = 0; i < b->len; i++) { @@ -3542,7 +3530,7 @@ static int init_command_block_for_exec (hawk_sed_t* sed, hawk_sed_cmd_blk_t* b) c->u.branch.target = HAWK_MAP_VPTR(pair); /* free resolved label name */ - hawk_sed_freemem (sed, lab->ptr); + hawk_sed_freemem(sed, lab->ptr); lab->ptr = HAWK_NULL; lab->len = 0; } @@ -3642,8 +3630,8 @@ int hawk_sed_exec (hawk_sed_t* sed, hawk_sed_io_impl_t inf, hawk_sed_io_impl_t o sed->e.subst_done = 0; free_appends (sed); - hawk_ooecs_clear (&sed->e.txt.scratch); - hawk_ooecs_clear (&sed->e.txt.hold); + hawk_ooecs_clear(&sed->e.txt.scratch); + hawk_ooecs_clear(&sed->e.txt.hold); if (hawk_ooecs_ccat(&sed->e.txt.hold, HAWK_T('\n')) == (hawk_oow_t)-1) return -1; sed->e.out.fun = outf; @@ -3653,9 +3641,9 @@ int hawk_sed_exec (hawk_sed_t* sed, hawk_sed_io_impl_t inf, hawk_sed_io_impl_t o &sed->e.out.files, hawk_sed_getgem(sed), 128, 70, HAWK_SIZEOF(hawk_ooch_t), 1) <= -1) return -1; - HAWK_ASSERT ((void*)(&sed->e.out.files + 1) == (void*)&sed->e.out.files_ext); + HAWK_ASSERT((void*)(&sed->e.out.files + 1) == (void*)&sed->e.out.files_ext); *(hawk_sed_t**)(&sed->e.out.files + 1) = sed; - hawk_map_setstyle (&sed->e.out.files, &style); + hawk_map_setstyle(&sed->e.out.files, &style); sed->e.in.fun = inf; sed->e.in.eof = 0; @@ -3664,7 +3652,7 @@ int hawk_sed_exec (hawk_sed_t* sed, hawk_sed_io_impl_t inf, hawk_sed_io_impl_t o sed->e.in.num = 0; if (hawk_ooecs_init(&sed->e.in.line, hawk_sed_getgem(sed), 256) <= -1) { - hawk_map_fini (&sed->e.out.files); + hawk_map_fini(&sed->e.out.files); return -1; } @@ -3714,7 +3702,7 @@ int hawk_sed_exec (hawk_sed_t* sed, hawk_sed_io_impl_t inf, hawk_sed_io_impl_t o while (c != &sed->cmd.over) { #if defined(HAWK_ENABLE_SED_TRACER) - if (sed->opt.tracer) sed->opt.tracer (sed, HAWK_SED_TRACER_MATCH, c); + if (sed->opt.tracer) sed->opt.tracer(sed, HAWK_SED_TRACER_MATCH, c); #endif n = match_address (sed, c); @@ -3728,14 +3716,14 @@ int hawk_sed_exec (hawk_sed_t* sed, hawk_sed_io_impl_t inf, hawk_sed_io_impl_t o } #if defined(HAWK_ENABLE_SED_TRACER) - if (sed->opt.tracer) sed->opt.tracer (sed, HAWK_SED_TRACER_EXEC, c); + if (sed->opt.tracer) sed->opt.tracer(sed, HAWK_SED_TRACER_EXEC, c); #endif j = exec_cmd (sed, c); if (j == HAWK_NULL) { ret = -1; goto done; } if (j == &sed->cmd.quit_quiet) goto done; if (j == &sed->cmd.quit) { - if (emit_output (sed, 0) <= -1) ret = -1; + if (emit_output(sed, 0) <= -1) ret = -1; goto done; } if (sed->e.haltreq) goto done; @@ -3747,19 +3735,19 @@ int hawk_sed_exec (hawk_sed_t* sed, hawk_sed_io_impl_t inf, hawk_sed_io_impl_t o } #if defined(HAWK_ENABLE_SED_TRACER) - if (sed->opt.tracer) sed->opt.tracer (sed, HAWK_SED_TRACER_WRITE, HAWK_NULL); + if (sed->opt.tracer) sed->opt.tracer(sed, HAWK_SED_TRACER_WRITE, HAWK_NULL); #endif - if (emit_output (sed, 0) <= -1) { ret = -1; goto done; } + if (emit_output(sed, 0) <= -1) { ret = -1; goto done; } } done: - hawk_map_clear (&sed->e.out.files); - sed->e.out.fun (sed, HAWK_SED_IO_CLOSE, &sed->e.out.arg, HAWK_NULL, 0); + hawk_map_clear(&sed->e.out.files); + sed->e.out.fun(sed, HAWK_SED_IO_CLOSE, &sed->e.out.arg, HAWK_NULL, 0); done2: - sed->e.in.fun (sed, HAWK_SED_IO_CLOSE, &sed->e.in.arg, HAWK_NULL, 0); + sed->e.in.fun(sed, HAWK_SED_IO_CLOSE, &sed->e.in.arg, HAWK_NULL, 0); done3: - hawk_ooecs_fini (&sed->e.in.line); - hawk_map_fini (&sed->e.out.files); + hawk_ooecs_fini(&sed->e.in.line); + hawk_map_fini(&sed->e.out.files); return ret; } @@ -3803,7 +3791,7 @@ const hawk_ooch_t* hawk_sed_setcompid (hawk_sed_t* sed, const hawk_ooch_t* id) } else { - hawk_copy_oocstr_unlimited ((hawk_ooch_t*)(cid + 1), id); + hawk_copy_oocstr_unlimited((hawk_ooch_t*)(cid + 1), id); } cid->next = sed->src.cid; @@ -3844,7 +3832,7 @@ const hawk_ooch_t* hawk_sed_setcompidwithbcstr (hawk_sed_t* sed, const hawk_bch_ else { #if defined(HAWK_OOCH_IS_BCH) - hawk_copy_oocstr_unlimited ((hawk_ooch_t*)(cid + 1), id); + hawk_copy_oocstr_unlimited((hawk_ooch_t*)(cid + 1), id); #else hawk_conv_bcstr_to_ucstr_with_cmgr(id, &tmplen, (hawk_ooch_t*)(cid + 1), &len, hawk_sed_getcmgr(sed), 1); #endif @@ -3889,7 +3877,7 @@ const hawk_ooch_t* hawk_sed_setcompidwithucstr (hawk_sed_t* sed, const hawk_uch_ #if defined(HAWK_OOCH_IS_BCH) hawk_conv_ucstr_to_bcstr_with_cmgr(id, &tmplen, (hawk_ooch_t*)(cid + 1), &len, hawk_sed_getcmgr(sed)); #else - hawk_copy_oocstr_unlimited ((hawk_ooch_t*)(cid + 1), id); + hawk_copy_oocstr_unlimited((hawk_ooch_t*)(cid + 1), id); #endif } @@ -3956,26 +3944,26 @@ void hawk_sed_getspace (hawk_sed_t* sed, hawk_sed_space_t space, hawk_oocs_t* st void* hawk_sed_allocmem (hawk_sed_t* sed, hawk_oow_t size) { void* ptr = HAWK_MMGR_ALLOC(hawk_sed_getmmgr(sed), size); - if (ptr == HAWK_NULL) hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_ENOMEM); + if (ptr == HAWK_NULL) hawk_sed_seterrnum(sed, HAWK_NULL, HAWK_ENOMEM); return ptr; } void* hawk_sed_callocmem (hawk_sed_t* sed, hawk_oow_t size) { void* ptr = HAWK_MMGR_ALLOC(hawk_sed_getmmgr(sed), size); - if (ptr) HAWK_MEMSET (ptr, 0, size); - else hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_ENOMEM); + if (ptr) HAWK_MEMSET(ptr, 0, size); + else hawk_sed_seterrnum(sed, HAWK_NULL, HAWK_ENOMEM); return ptr; } void* hawk_sed_reallocmem (hawk_sed_t* sed, void* ptr, hawk_oow_t size) { void* nptr = HAWK_MMGR_REALLOC(hawk_sed_getmmgr(sed), ptr, size); - if (nptr == HAWK_NULL) hawk_sed_seterrnum (sed, HAWK_NULL, HAWK_ENOMEM); + if (nptr == HAWK_NULL) hawk_sed_seterrnum(sed, HAWK_NULL, HAWK_ENOMEM); return nptr; } void hawk_sed_freemem (hawk_sed_t* sed, void* ptr) { - HAWK_MMGR_FREE (hawk_sed_getmmgr(sed), ptr); + HAWK_MMGR_FREE(hawk_sed_getmmgr(sed), ptr); } diff --git a/lib/std-cut.c b/lib/std-cut.c index 6c10c7e2..9790b8ae 100644 --- a/lib/std-cut.c +++ b/lib/std-cut.c @@ -103,11 +103,6 @@ hawk_cut_t* hawk_cut_openstdwithmmgr (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, haw return cut; } -int hawk_cut_compstd (hawk_cut_t* cut, const hawk_ooch_t* sptr) -{ - return hawk_cut_comp(cut, sptr, hawk_count_oocstr(sptr)); -} - #if 0 static hawk_ooi_t xin (hawk_cut_t* cut, hawk_cut_io_cmd_t cmd, hawk_cut_io_arg_t* arg, hawk_ooch_t* buf, hawk_oow_t len) @@ -1117,6 +1112,61 @@ static hawk_ooi_t x_out ( /* ------------------------------------------------------------- */ +int hawk_cut_compstd (hawk_cut_t* cut, hawk_cut_iostd_t in[], hawk_oow_t* count) +{ + xtn_t* xtn = GET_XTN(cut); + int ret; + + if (in == HAWK_NULL) + { + /* it requires a valid array unlike hawk_cut_execstd(). */ + hawk_cut_seterrbfmt(cut, HAWK_NULL, HAWK_EINVAL, "no input handler provided"); + if (count) *count = 0; + return -1; + } + if (verify_iostd_in(cut, in) <= -1) + { + if (count) *count = 0; + return -1; + } + + HAWK_MEMSET(&xtn->s, 0, HAWK_SIZEOF(xtn->s)); + xtn->s.in.ptr = in; + xtn->s.in.cur = in; + + ret = hawk_cut_comp(cut, s_in); + + if (count) *count = xtn->s.in.cur - xtn->s.in.ptr; + + clear_sio_names(cut); + return ret; +} + +int hawk_cut_compstdoocstr (hawk_cut_t* cut, const hawk_ooch_t* script) +{ + hawk_cut_iostd_t in[2]; + + in[0].type = HAWK_CUT_IOSTD_OOCS; + in[0].u.oocs.ptr = (hawk_ooch_t*)script; + in[0].u.oocs.len = hawk_count_oocstr(script); + in[1].type = HAWK_CUT_IOSTD_NULL; + + return hawk_cut_compstd(cut, in, HAWK_NULL); +} + +int hawk_cut_compstdoocs (hawk_cut_t* cut, const hawk_oocs_t* script) +{ + hawk_cut_iostd_t in[2]; + + in[0].type = HAWK_CUT_IOSTD_OOCS; + in[0].u.oocs = *script; + in[1].type = HAWK_CUT_IOSTD_NULL; + + return hawk_cut_compstd(cut, in, HAWK_NULL); +} + +/* ------------------------------------------------------------- */ + int hawk_cut_execstd (hawk_cut_t* cut, hawk_cut_iostd_t in[], hawk_cut_iostd_t* out) { int n;