added ase_awk_runsimple()

This commit is contained in:
2008-12-12 04:05:28 +00:00
parent 6a3e652db3
commit cd1dd05766
6 changed files with 630 additions and 641 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: Awk.cpp 468 2008-12-10 10:19:59Z baconevi $
* $Id: Awk.cpp 469 2008-12-11 10:05:28Z baconevi $
*
* {License}
*/
@ -1673,7 +1673,7 @@ Awk::ssize_t Awk::consoleHandler (
int Awk::functionHandler (
run_t* run, const char_t* name, size_t len)
{
Run* ctx = (Run*) ase_awk_getruncustomdata (run);
Run* ctx = (Run*) ase_awk_getrundata (run);
Awk* awk = ctx->awk;
return awk->dispatchFunction (ctx, name, len);
}

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c 466 2008-12-09 09:50:40Z baconevi $
* $Id: run.c 469 2008-12-11 10:05:28Z baconevi $
*
* {License}
*/
@ -599,12 +599,17 @@ ase_awk_t* ase_awk_getrunawk (ase_awk_run_t* run)
return run->awk;
}
void* ase_awk_getruncustomdata (ase_awk_run_t* run)
ase_mmgr_t* ase_awk_getrunmmgr (ase_awk_run_t* run)
{
return run->awk->mmgr;
}
void* ase_awk_getrundata (ase_awk_run_t* run)
{
return run->data;
}
ase_map_t* ase_awk_getrunnamedvarmap (ase_awk_run_t* run)
ase_map_t* ase_awk_getrunnvmap (ase_awk_run_t* run)
{
return run->named;
}

View File

@ -87,6 +87,9 @@ static ase_ssize_t sf_in (int cmd, void* arg, ase_char_t* data, ase_size_t size)
if (cmd == ASE_AWK_IO_OPEN)
{
if (sf->in.index >= sf->in.count) return 0;
/*
if (sf->in.files[sf->in.index] == ASE_NULL) return 0;
*/
if (sf->in.files[sf->in.index][0] == ASE_T('\0'))
{
@ -111,8 +114,14 @@ static ase_ssize_t sf_in (int cmd, void* arg, ase_char_t* data, ase_size_t size)
}
else if (cmd == ASE_AWK_IO_CLOSE)
{
if (sf->in.index >= sf->in.count) return 0;
if (sf->in.handle != ase_sio_in) ase_sio_close (sf->in.handle);
if (sf->in.handle != ASE_NULL &&
sf->in.handle != ase_sio_in &&
sf->in.handle != ase_sio_out &&
sf->in.handle != ase_sio_err)
{
ase_sio_close (sf->in.handle);
}
return 0;
}
else if (cmd == ASE_AWK_IO_READ)
@ -141,49 +150,15 @@ static ase_ssize_t sf_in (int cmd, void* arg, ase_char_t* data, ase_size_t size)
);
if (sf->in.handle == ASE_NULL) return -1;
}
/* TODO: reset internal line counters...
set new source name....
ase_awk_setsinname ();
*/
goto retry;
}
#if 0
while (!ase_feof(fp) && n < size)
{
ase_cint_t c = ase_fgetc (fp);
if (c == ASE_CHAR_EOF)
{
if (ase_ferror(fp)) n = -1;
break;
}
data[n++] = c;
}
if (n == 0)
{
sf->in.index++;
if (sf->in.index < sf->in.count)
{
if (fp != ase_sio_in) ase_sio_close (fp);
if (sf->in.files[sf->in.index][0] == ASE_T('\0'))
{
sf->in.handle = ase_sio_in;
}
else
{
sf->in.handle = ase_sio_open (
ase_awk_getmmgr(sf->awk), 0,
sf->in.files[sf->in.index], ASE_SIO_READ);
if (sf->in.handle == ASE_NULL) return -1;
}
/* TODO: reset internal line counters...
ase_awk_setsinname ();
*/
goto retry;
}
}
#endif
return n;
}
@ -215,9 +190,17 @@ static ase_ssize_t sf_out (int cmd, void* arg, ase_char_t* data, ase_size_t size
}
else if (cmd == ASE_AWK_IO_CLOSE)
{
ase_sio_flush (sf->out.handle);
if (sf->out.handle != ase_sio_out &&
sf->out.handle != ase_sio_err) ase_sio_close (sf->out.handle);
if (sf->out.handle != ASE_NULL)
{
ase_sio_flush (sf->out.handle);
if (sf->out.handle != ase_sio_in &&
sf->out.handle != ase_sio_out &&
sf->out.handle != ase_sio_err)
{
ase_sio_close (sf->out.handle);
}
}
return 0;
}
else if (cmd == ASE_AWK_IO_WRITE)
@ -280,6 +263,7 @@ int ase_awk_parsesimple (
}
sf.out.file = osf;
sf.out.handle = ASE_NULL;
sf.awk = awk;
sio.in = sf_in;
@ -291,8 +275,443 @@ int ase_awk_parsesimple (
/*** RUNSIMPLE ***/
ase_awk_run_t* ase_awk_runsimple (
ase_awk_t* awk, ase_size_t argc, const char* args[])
typedef struct runio_data_t
{
return ASE_NULL;
ase_char_t** icf; /* input console files */
ase_size_t icf_cur;
} runio_data_t;
static ase_ssize_t awk_extio_pipe (
int cmd, void* arg, ase_char_t* data, ase_size_t size)
{
ase_awk_extio_t* epa = (ase_awk_extio_t*)arg;
switch (cmd)
{
case ASE_AWK_IO_OPEN:
{
ase_sio_t* handle;
int mode;
if (epa->mode == ASE_AWK_EXTIO_PIPE_READ)
{
mode = ASE_SIO_READ;
}
else if (epa->mode == ASE_AWK_EXTIO_PIPE_WRITE)
{
mode = ASE_SIO_WRITE |
ASE_SIO_TRUNCATE |
ASE_SIO_CREATE;
}
else return -1; /* TODO: any way to set the error number? */
//dprint (ASE_T("opening %s of type %d (pipe)\n"), epa->name, epa->type);
// TOOD: pipe open....
handle = ase_sio_open (ase_awk_getrunmmgr(epa->run), 0, epa->name, mode);
if (handle == ASE_NULL) return -1;
epa->handle = (void*)handle;
return 1;
}
case ASE_AWK_IO_CLOSE:
{
//dprint (ASE_T("closing %s of type (pipe) %d\n"), epa->name, epa->type);
ase_sio_close ((ase_sio_t*)epa->handle);
epa->handle = ASE_NULL;
return 0;
}
case ASE_AWK_IO_READ:
{
return ase_sio_getsx (
(ase_sio_t*)epa->handle,
data,
size
);
}
case ASE_AWK_IO_WRITE:
{
return ase_sio_putsx (
(ase_sio_t*)epa->handle,
data,
size
);
}
case ASE_AWK_IO_FLUSH:
{
/*if (epa->mode == ASE_AWK_EXTIO_PIPE_READ) return -1;*/
return ase_sio_flush ((ase_sio_t*)epa->handle);
}
case ASE_AWK_IO_NEXT:
{
return -1;
}
}
return -1;
}
static ase_ssize_t awk_extio_file (
int cmd, void* arg, ase_char_t* data, ase_size_t size)
{
ase_awk_extio_t* epa = (ase_awk_extio_t*)arg;
switch (cmd)
{
case ASE_AWK_IO_OPEN:
{
ase_sio_t* handle;
int mode;
if (epa->mode == ASE_AWK_EXTIO_FILE_READ)
{
mode = ASE_SIO_READ;
}
else if (epa->mode == ASE_AWK_EXTIO_FILE_WRITE)
{
mode = ASE_SIO_WRITE |
ASE_SIO_CREATE |
ASE_SIO_TRUNCATE;
}
else if (epa->mode == ASE_AWK_EXTIO_FILE_APPEND)
{
mode = ASE_SIO_APPEND |
ASE_SIO_CREATE |
ASE_SIO_TRUNCATE;
}
else return -1; /* TODO: any way to set the error number? */
//dprint (ASE_T("opening %s of type %d (file)\n"), epa->name, epa->type);
handle = ase_sio_open (
ase_awk_getrunmmgr(epa->run),
0,
epa->name,
mode
);
if (handle == ASE_NULL)
{
ase_cstr_t errarg;
errarg.ptr = epa->name;
errarg.len = ase_strlen(epa->name);
ase_awk_setrunerror (epa->run, ASE_AWK_EOPEN, 0, &errarg, 1);
return -1;
}
epa->handle = (void*)handle;
return 1;
}
case ASE_AWK_IO_CLOSE:
{
//dprint (ASE_T("closing %s of type %d (file)\n"), epa->name, epa->type);
ase_sio_close ((ase_sio_t*)epa->handle);
epa->handle = ASE_NULL;
return 0;
}
case ASE_AWK_IO_READ:
{
return ase_sio_getsx (
(ase_sio_t*)epa->handle,
data,
size
);
}
case ASE_AWK_IO_WRITE:
{
return ase_sio_putsx (
(ase_sio_t*)epa->handle,
data,
size
);
}
case ASE_AWK_IO_FLUSH:
{
return ase_sio_flush ((ase_sio_t*)epa->handle);
}
case ASE_AWK_IO_NEXT:
{
return -1;
}
}
return -1;
}
static int open_extio_console (ase_awk_extio_t* epa);
static int close_extio_console (ase_awk_extio_t* epa);
static int next_extio_console (ase_awk_extio_t* epa);
static ase_ssize_t awk_extio_console (
int cmd, void* arg, ase_char_t* data, ase_size_t size)
{
ase_awk_extio_t* epa = (ase_awk_extio_t*)arg;
runio_data_t* rd = (runio_data_t*)epa->data;
if (cmd == ASE_AWK_IO_OPEN)
{
return open_extio_console (epa);
}
else if (cmd == ASE_AWK_IO_CLOSE)
{
return close_extio_console (epa);
}
else if (cmd == ASE_AWK_IO_READ)
{
ase_ssize_t n;
while ((n = ase_sio_getsx((ase_sio_t*)epa->handle,data,size)) == 0)
{
/* it has reached the end of the current file.
* open the next file if available */
if (rd->icf[rd->icf_cur] == ASE_NULL)
{
/* no more input console */
return 0;
}
if (rd->icf[rd->icf_cur][0] == ASE_T('\0'))
{
if (epa->handle != ASE_NULL &&
epa->handle != ase_sio_in &&
epa->handle != ase_sio_out &&
epa->handle != ase_sio_err)
{
ase_sio_close ((ase_sio_t*)epa->handle);
}
epa->handle = ase_sio_in;
}
else
{
ase_sio_t* fp;
fp = ase_sio_open (
ase_awk_getrunmmgr(epa->run),
0,
rd->icf[rd->icf_cur],
ASE_SIO_READ
);
if (fp == ASE_NULL)
{
ase_cstr_t errarg;
errarg.ptr = rd->icf[rd->icf_cur];
errarg.len = ase_strlen(rd->icf[rd->icf_cur]);
ase_awk_setrunerror (epa->run, ASE_AWK_EOPEN, 0, &errarg, 1);
return -1;
}
if (ase_awk_setfilename (
epa->run, rd->icf[rd->icf_cur],
ase_strlen(rd->icf[rd->icf_cur])) == -1)
{
ase_sio_close (fp);
return -1;
}
if (ase_awk_setglobal (
epa->run, ASE_AWK_GLOBAL_FNR, ase_awk_val_zero) == -1)
{
/* need to reset FNR */
ase_sio_close (fp);
return -1;
}
if (epa->handle != ASE_NULL &&
epa->handle != ase_sio_in &&
epa->handle != ase_sio_out &&
epa->handle != ase_sio_err)
{
ase_sio_close ((ase_sio_t*)epa->handle);
}
//dprint (ASE_T("open the next console [%s]\n"), rd->icf[rd->icf_cur]);
epa->handle = fp;
}
rd->icf_cur++;
}
return n;
}
else if (cmd == ASE_AWK_IO_WRITE)
{
return ase_sio_putsx (
(ase_sio_t*)epa->handle,
data,
size
);
}
else if (cmd == ASE_AWK_IO_FLUSH)
{
return ase_sio_flush ((ase_sio_t*)epa->handle);
}
else if (cmd == ASE_AWK_IO_NEXT)
{
return next_extio_console (epa);
}
return -1;
}
static int open_extio_console (ase_awk_extio_t* epa)
{
runio_data_t* rd = (runio_data_t*)epa->data;
//dprint (ASE_T("opening console[%s] of type %x\n"), epa->name, epa->type);
if (epa->mode == ASE_AWK_EXTIO_CONSOLE_READ)
{
if (rd->icf[rd->icf_cur] == ASE_NULL)
{
/* no more input file */
//dprint (ASE_T("console - no more file\n"));;
return 0;
}
if (rd->icf[rd->icf_cur][0] == ASE_T('\0'))
{
//dprint (ASE_T(" console(r) - <standard input>\n"));
epa->handle = ase_sio_in;
}
else
{
/* a temporary variable fp is used here not to change
* any fields of epa when the open operation fails */
ase_sio_t* fp;
fp = ase_sio_open (
ase_awk_getrunmmgr(epa->run),
0,
rd->icf[rd->icf_cur],
ASE_SIO_READ
);
if (fp == ASE_NULL)
{
ase_cstr_t errarg;
errarg.ptr = rd->icf[rd->icf_cur];
errarg.len = ase_strlen(rd->icf[rd->icf_cur]);
ase_awk_setrunerror (epa->run, ASE_AWK_EOPEN, 0, &errarg, 1);
return -1;
}
//dprint (ASE_T(" console(r) - %s\n"), rd->icf[rd->icf_cur]);
if (ase_awk_setfilename (
epa->run, rd->icf[rd->icf_cur],
ase_strlen(rd->icf[rd->icf_cur])) == -1)
{
ase_sio_close (fp);
return -1;
}
epa->handle = fp;
}
rd->icf_cur++;
return 1;
}
else if (epa->mode == ASE_AWK_EXTIO_CONSOLE_WRITE)
{
//dprint (ASE_T(" console(w) - <standard output>\n"));
if (ase_awk_setofilename (epa->run, ASE_T(""), 0) == -1)
{
return -1;
}
epa->handle = ase_sio_out;
return 1;
}
return -1;
}
static int close_extio_console (ase_awk_extio_t* epa)
{
//dprint (ASE_T("closing console of type %x\n"), epa->type);
if (epa->handle != ASE_NULL &&
epa->handle != ase_sio_in &&
epa->handle != ase_sio_out &&
epa->handle != ase_sio_err)
{
ase_sio_close ((ase_sio_t*)epa->handle);
}
return 0;
}
static int next_extio_console (ase_awk_extio_t* epa)
{
int n;
ase_sio_t* fp = (ase_sio_t*)epa->handle;
//dprint (ASE_T("switching console[%s] of type %x\n"), epa->name, epa->type);
n = open_extio_console(epa);
if (n == -1) return -1;
if (n == 0)
{
/* if there is no more file, keep the previous handle */
return 0;
}
if (fp != ASE_NULL &&
fp != ase_sio_in &&
fp != ase_sio_out &&
fp != ase_sio_err)
{
ase_sio_close (fp);
}
return n;
}
int ase_awk_runsimple (ase_awk_t* awk, ase_char_t** icf)
{
ase_awk_runcbs_t runcbs;
ase_awk_runios_t runios;
runio_data_t rd;
rd.icf = icf;
rd.icf_cur = 0;
runios.pipe = awk_extio_pipe;
runios.file = awk_extio_file;
runios.console = awk_extio_console;
runios.data = &rd;
/*
runcbs.on_start = on_run_start;
runcbs.on_statement = on_run_statement;
runcbs.on_return = on_run_return;
runcbs.on_end = on_run_end;
runcbs.data = ASE_NULL;
*/
return ase_awk_run (
awk,
ASE_NULL/*mfn*/,
&runios,
ASE_NULL/*&runcbs*/,
ASE_NULL/*runarg*/,
ASE_NULL
);
}

View File

@ -315,8 +315,24 @@ static ase_ssize_t __sio_input (int cmd, void* arg, void* buf, ase_size_t size)
ase_sio_t* sio = (ase_sio_t*)arg;
ASE_ASSERT (sio != ASE_NULL);
if (cmd == ASE_TIO_IO_DATA)
{
#ifdef _WIN32
/* TODO: I hate this way of adjusting the handle value
* Is there any good ways to do it statically? */
HANDLE h = sio->fio.handle;
if (h == (HANDLE)STD_INPUT_HANDLE ||
h == (HANDLE)STD_OUTPUT_HANDLE ||
h == (HANDLE)STD_ERROR_HANDLE)
{
h = GetStdHandle((DWORD)h);
if (h != INVALID_HANDLE_VALUE && h != NULL)
{
sio->fio.handle = h;
}
}
#endif
return ase_fio_read (&sio->fio, buf, size);
}
@ -331,6 +347,21 @@ static ase_ssize_t __sio_output (int cmd, void* arg, void* buf, ase_size_t size)
if (cmd == ASE_TIO_IO_DATA)
{
#ifdef _WIN32
/* TODO: I hate this way of adjusting the handle value
* Is there any good ways to do it statically? */
HANDLE h = sio->fio.handle;
if (h == (HANDLE)STD_INPUT_HANDLE ||
h == (HANDLE)STD_OUTPUT_HANDLE ||
h == (HANDLE)STD_ERROR_HANDLE)
{
h = GetStdHandle((DWORD)h);
if (h != INVALID_HANDLE_VALUE && h != NULL)
{
sio->fio.handle = h;
}
}
#endif
return ase_fio_write (&sio->fio, buf, size);
}