removed duplicate code
This commit is contained in:
parent
cbbc06179a
commit
d235fa023d
@ -18,7 +18,7 @@ bin_PROGRAMS = hawk
|
|||||||
##################################################
|
##################################################
|
||||||
# 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_CPPFLAGS = $(CPPFLAGS_COMMON)
|
||||||
hawk_CFLAGS = $(CFLAGS_COMMON)
|
hawk_CFLAGS = $(CFLAGS_COMMON)
|
||||||
hawk_LDFLAGS = $(LDFLAGS_COMMON)
|
hawk_LDFLAGS = $(LDFLAGS_COMMON)
|
||||||
|
@ -111,8 +111,8 @@ CONFIG_CLEAN_FILES =
|
|||||||
CONFIG_CLEAN_VPATH_FILES =
|
CONFIG_CLEAN_VPATH_FILES =
|
||||||
am__installdirs = "$(DESTDIR)$(bindir)"
|
am__installdirs = "$(DESTDIR)$(bindir)"
|
||||||
PROGRAMS = $(bin_PROGRAMS)
|
PROGRAMS = $(bin_PROGRAMS)
|
||||||
am_hawk_OBJECTS = hawk-hawk.$(OBJEXT) hawk-sed.$(OBJEXT) \
|
am_hawk_OBJECTS = hawk-cut.$(OBJEXT) hawk-hawk.$(OBJEXT) \
|
||||||
hawk-main.$(OBJEXT)
|
hawk-sed.$(OBJEXT) hawk-main.$(OBJEXT)
|
||||||
hawk_OBJECTS = $(am_hawk_OBJECTS)
|
hawk_OBJECTS = $(am_hawk_OBJECTS)
|
||||||
am__DEPENDENCIES_1 =
|
am__DEPENDENCIES_1 =
|
||||||
hawk_DEPENDENCIES = $(LIBADD_COMMON) $(am__DEPENDENCIES_1)
|
hawk_DEPENDENCIES = $(LIBADD_COMMON) $(am__DEPENDENCIES_1)
|
||||||
@ -138,7 +138,7 @@ am__v_at_1 =
|
|||||||
DEFAULT_INCLUDES =
|
DEFAULT_INCLUDES =
|
||||||
depcomp = $(SHELL) $(top_srcdir)/ac/depcomp
|
depcomp = $(SHELL) $(top_srcdir)/ac/depcomp
|
||||||
am__maybe_remake_depfiles = depfiles
|
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
|
./$(DEPDIR)/hawk-main.Po ./$(DEPDIR)/hawk-sed.Po
|
||||||
am__mv = mv -f
|
am__mv = mv -f
|
||||||
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
|
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
|
||||||
@ -356,7 +356,7 @@ LIBADD_COMMON = ../lib/libhawk.la
|
|||||||
##################################################
|
##################################################
|
||||||
# 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_CPPFLAGS = $(CPPFLAGS_COMMON)
|
||||||
hawk_CFLAGS = $(CFLAGS_COMMON)
|
hawk_CFLAGS = $(CFLAGS_COMMON)
|
||||||
hawk_LDFLAGS = $(LDFLAGS_COMMON)
|
hawk_LDFLAGS = $(LDFLAGS_COMMON)
|
||||||
@ -449,6 +449,7 @@ mostlyclean-compile:
|
|||||||
distclean-compile:
|
distclean-compile:
|
||||||
-rm -f *.tab.c
|
-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-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-main.Po@am__quote@ # am--include-marker
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hawk-sed.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@
|
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||||
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $<
|
@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
|
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_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
|
@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
|
clean-am: clean-binPROGRAMS clean-generic clean-libtool mostlyclean-am
|
||||||
|
|
||||||
distclean: distclean-am
|
distclean: distclean-am
|
||||||
|
-rm -f ./$(DEPDIR)/hawk-cut.Po
|
||||||
-rm -f ./$(DEPDIR)/hawk-hawk.Po
|
-rm -f ./$(DEPDIR)/hawk-hawk.Po
|
||||||
-rm -f ./$(DEPDIR)/hawk-main.Po
|
-rm -f ./$(DEPDIR)/hawk-main.Po
|
||||||
-rm -f ./$(DEPDIR)/hawk-sed.Po
|
-rm -f ./$(DEPDIR)/hawk-sed.Po
|
||||||
@ -705,6 +721,7 @@ install-ps-am:
|
|||||||
installcheck-am:
|
installcheck-am:
|
||||||
|
|
||||||
maintainer-clean: maintainer-clean-am
|
maintainer-clean: maintainer-clean-am
|
||||||
|
-rm -f ./$(DEPDIR)/hawk-cut.Po
|
||||||
-rm -f ./$(DEPDIR)/hawk-hawk.Po
|
-rm -f ./$(DEPDIR)/hawk-hawk.Po
|
||||||
-rm -f ./$(DEPDIR)/hawk-main.Po
|
-rm -f ./$(DEPDIR)/hawk-main.Po
|
||||||
-rm -f ./$(DEPDIR)/hawk-sed.Po
|
-rm -f ./$(DEPDIR)/hawk-sed.Po
|
||||||
|
924
bin/cut.c
Normal file
924
bin/cut.c
Normal file
@ -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 <hawk-cut.h>
|
||||||
|
#include <hawk-cli.h>
|
||||||
|
#include <hawk-fmt.h>
|
||||||
|
#include <hawk-utl.h>
|
||||||
|
#include <hawk-std.h>
|
||||||
|
#include <hawk-xma.h>
|
||||||
|
|
||||||
|
#if !defined(_GNU_SOURCE)
|
||||||
|
# define _GNU_SOURCE
|
||||||
|
#endif
|
||||||
|
#if !defined(_XOPEN_SOURCE)
|
||||||
|
# define _XOPEN_SOURCE 700
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <locale.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
# include <windows.h>
|
||||||
|
#elif defined(__OS2__)
|
||||||
|
# define INCL_ERRORS
|
||||||
|
# include <os2.h>
|
||||||
|
# include <signal.h>
|
||||||
|
#elif defined(__DOS__)
|
||||||
|
# include <dos.h>
|
||||||
|
# include <signal.h>
|
||||||
|
#else
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <errno.h>
|
||||||
|
# include <signal.h>
|
||||||
|
#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, " <start~step>,<start,+line>,<start,~line>,<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;
|
||||||
|
}
|
51
bin/hawk.c
51
bin/hawk.c
@ -470,29 +470,10 @@ static void dprint_return (hawk_rtx_t* rtx, hawk_val_t* ret)
|
|||||||
#if defined(ENABLE_CALLBACK)
|
#if defined(ENABLE_CALLBACK)
|
||||||
static void on_statement (hawk_rtx_t* rtx, hawk_nde_t* nde)
|
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);
|
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
|
#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
|
struct opttab_t
|
||||||
{
|
{
|
||||||
const hawk_bch_t* name;
|
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);
|
isf = (hawk_parsestd_t*)malloc(HAWK_SIZEOF(*isf) * isfc);
|
||||||
if (!isf)
|
if (!isf)
|
||||||
{
|
{
|
||||||
print_error ("out of memory\n");
|
hawk_main_print_error("out of memory\n");
|
||||||
goto oops;
|
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));
|
tmp = (hawk_parsestd_t*)realloc(isf, HAWK_SIZEOF(*isf) * (isfc + 16));
|
||||||
if (tmp == HAWK_NULL)
|
if (tmp == HAWK_NULL)
|
||||||
{
|
{
|
||||||
print_error ("out of memory\n");
|
hawk_main_print_error("out of memory\n");
|
||||||
goto oops;
|
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);
|
tmp.len = hawk_count_bcstr(opt.arg);
|
||||||
if (collect_into_xarg(&tmp, &arg->ocf) <= -1)
|
if (collect_into_xarg(&tmp, &arg->ocf) <= -1)
|
||||||
{
|
{
|
||||||
print_error ("out of memory\n");
|
hawk_main_print_error("out of memory\n");
|
||||||
goto oops;
|
goto oops;
|
||||||
}
|
}
|
||||||
arg->ocf.ptr[arg->ocf.size] = HAWK_NULL;
|
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 (eq == HAWK_NULL)
|
||||||
{
|
{
|
||||||
if (opt.lngopt)
|
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
|
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;
|
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);
|
tmp = realloc(arg->gvm.ptr, HAWK_SIZEOF(*tmp) * newcapa);
|
||||||
if (!tmp)
|
if (!tmp)
|
||||||
{
|
{
|
||||||
print_error ("out of memory\n");
|
hawk_main_print_error("out of memory\n");
|
||||||
goto oops;
|
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);
|
arg->script_cmgr = hawk_get_cmgr_by_bcstr(opt.arg);
|
||||||
if (arg->script_cmgr == HAWK_NULL)
|
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;
|
oops_ret = 3;
|
||||||
goto oops;
|
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);
|
arg->console_cmgr = hawk_get_cmgr_by_bcstr(opt.arg);
|
||||||
if (arg->console_cmgr == HAWK_NULL)
|
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;
|
oops_ret = 3;
|
||||||
goto oops;
|
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;
|
arg->opton |= opttab[i].opt;
|
||||||
else
|
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;
|
oops_ret = 3;
|
||||||
goto oops;
|
goto oops;
|
||||||
}
|
}
|
||||||
@ -896,9 +877,9 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar
|
|||||||
case '?':
|
case '?':
|
||||||
{
|
{
|
||||||
if (opt.lngopt)
|
if (opt.lngopt)
|
||||||
print_error ("illegal option - '%s'\n", opt.lngopt);
|
hawk_main_print_error("illegal option - '%s'\n", opt.lngopt);
|
||||||
else
|
else
|
||||||
print_error ("illegal option - '%c'\n", opt.opt);
|
hawk_main_print_error("illegal option - '%c'\n", opt.opt);
|
||||||
|
|
||||||
goto oops;
|
goto oops;
|
||||||
}
|
}
|
||||||
@ -906,9 +887,9 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar
|
|||||||
case ':':
|
case ':':
|
||||||
{
|
{
|
||||||
if (opt.lngopt)
|
if (opt.lngopt)
|
||||||
print_error ("bad argument for '%s'\n", opt.lngopt);
|
hawk_main_print_error("bad argument for '%s'\n", opt.lngopt);
|
||||||
else
|
else
|
||||||
print_error ("bad argument for '%c'\n", opt.opt);
|
hawk_main_print_error("bad argument for '%c'\n", opt.opt);
|
||||||
|
|
||||||
goto oops;
|
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 */
|
/* the remaining arguments are input console file names */
|
||||||
if (expand_wildcard(argc - opt.ind, &argv[opt.ind], do_glob, &arg->icf) <= -1)
|
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;
|
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)
|
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;
|
goto oops;
|
||||||
}
|
}
|
||||||
mmgr = &xma_mmgr;
|
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);
|
hawk = hawk_openstdwithmmgr(mmgr, 0, hawk_get_cmgr_by_id(HAWK_CMGR_UTF8), HAWK_NULL);
|
||||||
if (HAWK_UNLIKELY(!hawk))
|
if (HAWK_UNLIKELY(!hawk))
|
||||||
{
|
{
|
||||||
print_error ("cannot open hawk\n");
|
hawk_main_print_error("cannot open hawk\n");
|
||||||
goto oops;
|
goto oops;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,6 +106,7 @@ static struct {
|
|||||||
int (*main) (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0);
|
int (*main) (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0);
|
||||||
} entry_funcs[] = {
|
} entry_funcs[] = {
|
||||||
{ "awk", main_hawk },
|
{ "awk", main_hawk },
|
||||||
|
{ "cut", main_cut },
|
||||||
{ "hawk", main_hawk },
|
{ "hawk", main_hawk },
|
||||||
{ "sed", main_sed },
|
{ "sed", main_sed },
|
||||||
{ "usage", main_usage },
|
{ "usage", main_usage },
|
||||||
@ -150,12 +151,12 @@ int main(int argc, hawk_bch_t* argv[])
|
|||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
if (WSAStartup(MAKEWORD(2,0), &wsadata) != 0)
|
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;
|
else sock_inited = 1;
|
||||||
#elif defined(__DOS__)
|
#elif defined(__DOS__)
|
||||||
/* TODO: add an option to skip watt-32 */
|
/* TODO: add an option to skip watt-32 */
|
||||||
_watt_do_exit = 0; /* prevent sock_init from exiting upon failure */
|
_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;
|
else sock_inited = 1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
72
bin/sed.c
72
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)
|
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* 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));
|
tmp = (hawk_sed_iostd_t*)realloc(g_script.io, HAWK_SIZEOF(*g_script.io) * (g_script.capa + 16 + 1));
|
||||||
if (tmp == HAWK_NULL)
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,21 +210,21 @@ static int handle_args (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_arg
|
|||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
default:
|
default:
|
||||||
print_usage (stderr, argv[0], real_argv0);
|
print_usage(stderr, argv[0], real_argv0);
|
||||||
goto oops;
|
goto oops;
|
||||||
|
|
||||||
case '?':
|
case '?':
|
||||||
print_error("bad option - %c\n", opt.opt);
|
hawk_main_print_error("bad option - %c\n", opt.opt);
|
||||||
print_usage (stderr, argv[0], real_argv0);
|
print_usage(stderr, argv[0], real_argv0);
|
||||||
goto oops;
|
goto oops;
|
||||||
|
|
||||||
case ':':
|
case ':':
|
||||||
print_error("bad parameter for %c\n", opt.opt);
|
hawk_main_print_error("bad parameter for %c\n", opt.opt);
|
||||||
print_usage (stderr, argv[0], real_argv0);
|
print_usage(stderr, argv[0], real_argv0);
|
||||||
goto oops;
|
goto oops;
|
||||||
|
|
||||||
case 'h':
|
case 'h':
|
||||||
print_usage (stdout, argv[0], real_argv0);
|
print_usage(stdout, argv[0], real_argv0);
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
case 'D':
|
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;
|
arg->trace = 1;
|
||||||
break;
|
break;
|
||||||
#else
|
#else
|
||||||
print_usage (stderr, argv[0], real_argv0);
|
print_usage(stderr, argv[0], real_argv0);
|
||||||
goto oops;
|
goto oops;
|
||||||
#endif
|
#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);
|
g_script_cmgr = hawk_get_cmgr_by_bcstr(opt.arg);
|
||||||
if (g_script_cmgr == HAWK_NULL)
|
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;
|
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);
|
g_infile_cmgr = hawk_get_cmgr_by_bcstr(opt.arg);
|
||||||
if (g_infile_cmgr == HAWK_NULL)
|
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;
|
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);
|
g_outfile_cmgr = hawk_get_cmgr_by_bcstr(opt.arg);
|
||||||
if (g_outfile_cmgr == HAWK_NULL)
|
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;
|
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)
|
if (g_script.size <= 0)
|
||||||
{
|
{
|
||||||
print_usage (stderr, argv[0], real_argv0);
|
print_usage(stderr, argv[0], real_argv0);
|
||||||
goto oops;
|
goto oops;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -373,17 +355,17 @@ done:
|
|||||||
return 0;
|
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);
|
const hawk_loc_t* errloc = hawk_sed_geterrloc(sed);
|
||||||
if (errloc->line > 0 || errloc->colm > 0)
|
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);
|
hawk_sed_geterrbmsg(sed), (unsigned long)errloc->line, (unsigned long)errloc->colm);
|
||||||
}
|
}
|
||||||
else
|
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)
|
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;
|
goto oops;
|
||||||
}
|
}
|
||||||
mmgr = &xma_mmgr;
|
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);
|
sed = hawk_sed_openstdwithmmgr(mmgr, 0, cmgr, HAWK_NULL);
|
||||||
if (!sed)
|
if (!sed)
|
||||||
{
|
{
|
||||||
print_error("cannot open stream editor\n");
|
hawk_main_print_error("cannot open stream editor\n");
|
||||||
goto oops;
|
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)
|
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
|
target, hawk_sed_geterrbmsg(sed), (unsigned long)errloc->line, (unsigned long)errloc->colm
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else
|
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;
|
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)
|
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;
|
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 */
|
/* perform wild-card expansions for non-unix platforms */
|
||||||
if (expand_wildcards(argc - arg.infile_pos, &argv[arg.infile_pos], arg.wildcard, &xarg) <= -1)
|
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;
|
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);
|
tmpl_tmpfile = hawk_gem_dupbcstrarr(hawk_sed_getgem(sed), f, HAWK_NULL);
|
||||||
if (tmpl_tmpfile == HAWK_NULL)
|
if (tmpl_tmpfile == HAWK_NULL)
|
||||||
{
|
{
|
||||||
print_error("out of memory\n");
|
hawk_main_print_error("out of memory\n");
|
||||||
goto oops;
|
goto oops;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -864,7 +846,7 @@ int main_sed(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0)
|
|||||||
{
|
{
|
||||||
if (retried)
|
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);
|
hawk_sed_freemem(sed, tmpl_tmpfile);
|
||||||
goto oops;
|
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);
|
tmpl_tmpfile = hawk_gem_dupbcstr(hawk_sed_getgem(sed), "TMP-XXXX", HAWK_NULL);
|
||||||
if (tmpl_tmpfile == HAWK_NULL)
|
if (tmpl_tmpfile == HAWK_NULL)
|
||||||
{
|
{
|
||||||
print_error("out of memory\n");
|
hawk_main_print_error("out of memory\n");
|
||||||
goto oops;
|
goto oops;
|
||||||
}
|
}
|
||||||
retried = 1;
|
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)
|
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);
|
tmpl_tmpfile, in[0].u.fileb.path, tmpl_tmpfile);
|
||||||
hawk_sed_freemem(sed, tmpl_tmpfile);
|
hawk_sed_freemem(sed, tmpl_tmpfile);
|
||||||
goto oops;
|
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 */
|
/* perform wild-card expansions for non-unix platforms */
|
||||||
if (expand_wildcards(argc - arg.infile_pos, &argv[arg.infile_pos], arg.wildcard, &xarg) <= -1)
|
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;
|
goto oops;
|
||||||
}
|
}
|
||||||
|
|
||||||
in = (hawk_sed_iostd_t*)hawk_sed_allocmem(sed, HAWK_SIZEOF(*in) * (xarg.size + 1));
|
in = (hawk_sed_iostd_t*)hawk_sed_allocmem(sed, HAWK_SIZEOF(*in) * (xarg.size + 1));
|
||||||
if (in == HAWK_NULL)
|
if (in == HAWK_NULL)
|
||||||
{
|
{
|
||||||
print_error("out of memory\n");
|
hawk_main_print_error("out of memory\n");
|
||||||
goto oops;
|
goto oops;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,8 +37,8 @@ struct hawk_cut_sel_blk_t
|
|||||||
{
|
{
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
HAWK_SED_SEL_CHAR = HAWK_T('c'),
|
HAWK_CUT_SEL_CHAR = HAWK_T('c'),
|
||||||
HAWK_SED_SEL_FIELD = HAWK_T('f')
|
HAWK_CUT_SEL_FIELD = HAWK_T('f')
|
||||||
} id;
|
} id;
|
||||||
hawk_oow_t start;
|
hawk_oow_t start;
|
||||||
hawk_oow_t end;
|
hawk_oow_t end;
|
||||||
@ -69,7 +69,25 @@ struct hawk_cut_t
|
|||||||
hawk_oow_t count;
|
hawk_oow_t count;
|
||||||
hawk_oow_t fcount;
|
hawk_oow_t fcount;
|
||||||
hawk_oow_t ccount;
|
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
|
struct
|
||||||
{
|
{
|
||||||
|
221
lib/cut.c
221
lib/cut.c
@ -136,6 +136,37 @@ static void hawk_cut_fini (hawk_cut_t* cut)
|
|||||||
hawk_ooecs_fini(&cut->e.in.line);
|
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)
|
void hawk_cut_setoption (hawk_cut_t* cut, int option)
|
||||||
{
|
{
|
||||||
cut->option = 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);
|
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;
|
#define CURSC(cut) ((cut)->src.cc)
|
||||||
const hawk_ooch_t* xnd = str + len;
|
#define NXTSC(cut,c,errret) \
|
||||||
hawk_ooci_t c;
|
do { if (getnextsc(cut,&(c)) <= -1) return (errret); } while (0)
|
||||||
int sel = HAWK_SED_SEL_CHAR;
|
#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 EOF(x) ((x) == HAWK_OOCI_EOF)
|
||||||
#define MASK_START (1 << 1)
|
#define MASK_START (1 << 1)
|
||||||
#define MASK_END (1 << 2)
|
#define MASK_END (1 << 2)
|
||||||
#define MAX HAWK_TYPE_MAX(hawk_oow_t)
|
#define MAX HAWK_TYPE_MAX(hawk_oow_t)
|
||||||
|
|
||||||
/* free selector blocks compiled previously */
|
/* free selector blocks compiled previously */
|
||||||
free_all_selector_blocks (cut);
|
free_all_selector_blocks(cut);
|
||||||
|
|
||||||
/* set the default delimiters */
|
/* set the default delimiters */
|
||||||
cut->sel.din = HAWK_T(' ');
|
cut->sel.din = HAWK_T(' ');
|
||||||
cut->sel.dout = HAWK_T(' ');
|
cut->sel.dout = HAWK_T(' ');
|
||||||
|
|
||||||
/* if the selector string is empty, don't need to proceed */
|
/* open script */
|
||||||
if (len <= 0) return 0;
|
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)
|
while (1)
|
||||||
{
|
{
|
||||||
hawk_oow_t start = 0, end = 0;
|
hawk_oow_t start = 0, end = 0;
|
||||||
int mask = 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 (EOF(c))
|
||||||
{
|
{
|
||||||
if (cut->sel.count > 0)
|
if (cut->sel.count > 0)
|
||||||
{
|
{
|
||||||
SETERR0 (cut, HAWK_CUT_ESELNV);
|
SETERR0(cut, HAWK_CUT_ESELNV);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
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 next character is the input delimiter.
|
||||||
* the output delimiter defaults to the input
|
* the output delimiter defaults to the input
|
||||||
* delimiter. */
|
* delimiter. */
|
||||||
c = NC(p, xnd);
|
NXTSC_GOTO(cut, c, oops);
|
||||||
if (EOF(c))
|
if (EOF(c))
|
||||||
{
|
{
|
||||||
SETERR0 (cut, HAWK_CUT_ESELNV);
|
SETERR0(cut, HAWK_CUT_ESELNV);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
cut->sel.din = c;
|
cut->sel.din = c;
|
||||||
cut->sel.dout = c;
|
cut->sel.dout = c;
|
||||||
|
|
||||||
c = NC(p, xnd);
|
NXTSC_GOTO(cut, c, oops);
|
||||||
}
|
}
|
||||||
else if (c == HAWK_T('D'))
|
else if (c == HAWK_T('D'))
|
||||||
{
|
{
|
||||||
/* the next two characters are the input and
|
/* the next two characters are the input and
|
||||||
* the output delimiter each. */
|
* the output delimiter each. */
|
||||||
c = NC(p, xnd);
|
NXTSC_GOTO(cut, c, oops);
|
||||||
if (EOF(c))
|
if (EOF(c))
|
||||||
{
|
{
|
||||||
SETERR0 (cut, HAWK_CUT_ESELNV);
|
SETERR0(cut, HAWK_CUT_ESELNV);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
cut->sel.din = c;
|
cut->sel.din = c;
|
||||||
|
|
||||||
c = NC(p, xnd);
|
NXTSC_GOTO(cut, c, oops);
|
||||||
if (EOF(c))
|
if (EOF(c))
|
||||||
{
|
{
|
||||||
SETERR0 (cut, HAWK_CUT_ESELNV);
|
SETERR0(cut, HAWK_CUT_ESELNV);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
cut->sel.dout = c;
|
cut->sel.dout = c;
|
||||||
|
|
||||||
c = NC(p, xnd);
|
NXTSC_GOTO(cut, c, oops);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (c == HAWK_T('c') || c == HAWK_T('f'))
|
if (c == HAWK_T('c') || c == HAWK_T('f'))
|
||||||
{
|
{
|
||||||
sel = c;
|
sel = c;
|
||||||
c = NC(p, xnd);
|
NXTSC_GOTO(cut, c, oops);
|
||||||
while (hawk_is_ooch_space(c)) c = NC(p, xnd);
|
while (hawk_is_ooch_space(c)) NXTSC_GOTO(cut, c, oops);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hawk_is_ooch_digit(c))
|
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
|
do
|
||||||
{
|
{
|
||||||
start = start * 10 + (c - HAWK_T('0'));
|
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_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;
|
mask |= MASK_START;
|
||||||
}
|
}
|
||||||
else start++;
|
else start++;
|
||||||
|
|
||||||
if (c == HAWK_T('-'))
|
if (c == HAWK_T('-'))
|
||||||
{
|
{
|
||||||
c = NC(p, xnd);
|
NXTSC_GOTO(cut, c, oops);
|
||||||
while (hawk_is_ooch_space(c)) c = NC(p, xnd);
|
while (hawk_is_ooch_space(c)) NXTSC_GOTO(cut, c, oops);
|
||||||
|
|
||||||
if (hawk_is_ooch_digit(c))
|
if (hawk_is_ooch_digit(c))
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
end = end * 10 + (c - HAWK_T('0'));
|
end = end * 10 + (c - HAWK_T('0'));
|
||||||
c = NC(p, xnd);
|
NXTSC_GOTO(cut, c, oops);
|
||||||
}
|
}
|
||||||
while (hawk_is_ooch_digit(c));
|
while (hawk_is_ooch_digit(c));
|
||||||
mask |= MASK_END;
|
mask |= MASK_END;
|
||||||
}
|
}
|
||||||
else end = MAX;
|
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;
|
else end = start;
|
||||||
|
|
||||||
if (!(mask & (MASK_START | MASK_END)))
|
if (!(mask & (MASK_START | MASK_END)))
|
||||||
{
|
{
|
||||||
SETERR0 (cut, HAWK_CUT_ESELNV);
|
SETERR0(cut, HAWK_CUT_ESELNV);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cut->sel.lb->len >= HAWK_COUNTOF(cut->sel.lb->range))
|
if (cut->sel.lb->len >= HAWK_COUNTOF(cut->sel.lb->range))
|
||||||
{
|
{
|
||||||
if (add_selector_block (cut) <= -1)
|
if (add_selector_block(cut) <= -1) return -1;
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cut->sel.lb->range[cut->sel.lb->len].id = sel;
|
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->range[cut->sel.lb->len].end = end;
|
||||||
cut->sel.lb->len++;
|
cut->sel.lb->len++;
|
||||||
cut->sel.count++;
|
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++;
|
else cut->sel.ccount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (EOF(c)) break;
|
if (EOF(c)) break;
|
||||||
if (c == HAWK_T(',')) c = NC(p, xnd);
|
if (c == HAWK_T(',')) NXTSC_GOTO(cut, c, oops);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
oops:
|
||||||
|
close_script_stream(cut);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int read_char (hawk_cut_t* cut, hawk_ooch_t* c)
|
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;
|
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)
|
if (n <= -1)
|
||||||
{
|
{
|
||||||
ret = -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++)
|
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 (
|
n = cut_chars (
|
||||||
cut,
|
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:
|
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:
|
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:
|
done3:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -372,8 +372,7 @@ HAWK_EXPORT void hawk_cut_clear (
|
|||||||
*/
|
*/
|
||||||
HAWK_EXPORT int hawk_cut_comp (
|
HAWK_EXPORT int hawk_cut_comp (
|
||||||
hawk_cut_t* cut, /**< text cutter */
|
hawk_cut_t* cut, /**< text cutter */
|
||||||
const hawk_ooch_t* str, /**< selector pointer */
|
hawk_cut_io_impl_t inf
|
||||||
hawk_oow_t len /**< selector length */
|
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -447,12 +446,12 @@ HAWK_EXPORT hawk_cut_t* hawk_cut_openstdwithmmgr (
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The hawk_cut_compstd() function compiles a null-terminated selector.
|
* The hawk_cut_compstd() function compiles a selector from input streams.
|
||||||
* Call hawk_cut_comp() for a length delimited selector.
|
|
||||||
*/
|
*/
|
||||||
HAWK_EXPORT int hawk_cut_compstd (
|
HAWK_EXPORT int hawk_cut_compstd (
|
||||||
hawk_cut_t* cut,
|
hawk_cut_t* cut,
|
||||||
const hawk_ooch_t* str
|
hawk_cut_iostd_t in[],
|
||||||
|
hawk_oow_t* count
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -103,11 +103,6 @@ hawk_cut_t* hawk_cut_openstdwithmmgr (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, haw
|
|||||||
return cut;
|
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
|
#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)
|
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 hawk_cut_execstd (hawk_cut_t* cut, hawk_cut_iostd_t in[], hawk_cut_iostd_t* out)
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user