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.c 468 2008-12-10 10:19:59Z baconevi $
* $Id: awk.c 469 2008-12-11 10:05:28Z baconevi $
*/
#include <ase/awk/awk.h>
@ -122,527 +122,6 @@ static int custom_awk_sprintf (
return n;
}
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:
{
FILE* handle;
const ase_char_t* mode;
if (epa->mode == ASE_AWK_EXTIO_PIPE_READ)
mode = ASE_T("r");
else if (epa->mode == ASE_AWK_EXTIO_PIPE_WRITE)
mode = ASE_T("w");
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);
handle = ase_popen (epa->name, mode);
if (handle == 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);
fclose ((FILE*)epa->handle);
epa->handle = NULL;
return 0;
}
case ASE_AWK_IO_READ:
{
/*
int chunk = (size > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)size;
if (ase_fgets (data, chunk, (FILE*)epa->handle) == ASE_NULL)
{
if (ferror((FILE*)epa->handle)) return -1;
return 0;
}
return ase_strlen(data);
*/
ase_ssize_t n = 0;
FILE* fp = (FILE*)epa->handle;
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;
}
return n;
}
case ASE_AWK_IO_WRITE:
{
FILE* fp = (FILE*)epa->handle;
size_t left = size;
while (left > 0)
{
if (*data == ASE_T('\0'))
{
#if defined(ASE_CHAR_IS_WCHAR) && defined(__linux)
if (fputc ('\0', fp) == EOF)
#else
if (ase_fputc (*data, fp) == ASE_CHAR_EOF)
#endif
{
return -1;
}
left -= 1; data += 1;
}
else
{
#if defined(ASE_CHAR_IS_WCHAR) && defined(__linux)
/* fwprintf seems to return an error with the file
* pointer opened by popen, as of this writing.
* anyway, hopefully the following replacement
* will work all the way. */
int chunk = (left > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)left;
int n = fprintf (fp, "%.*ls", chunk, data);
if (n >= 0)
{
size_t x;
for (x = 0; x < chunk; x++)
{
if (data[x] == ASE_T('\0')) break;
}
n = x;
}
#else
int chunk = (left > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)left;
int n = ase_fprintf (fp, ASE_T("%.*s"), chunk, data);
#endif
if (n < 0 || n > chunk) return -1;
left -= n; data += n;
}
}
return size;
}
case ASE_AWK_IO_FLUSH:
{
if (epa->mode == ASE_AWK_EXTIO_PIPE_READ) return -1;
return fflush ((FILE*)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:
{
FILE* handle;
const ase_char_t* mode;
if (epa->mode == ASE_AWK_EXTIO_FILE_READ)
mode = ASE_T("r");
else if (epa->mode == ASE_AWK_EXTIO_FILE_WRITE)
mode = ASE_T("w");
else if (epa->mode == ASE_AWK_EXTIO_FILE_APPEND)
mode = ASE_T("a");
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_fopen (epa->name, mode);
if (handle == 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);
fclose ((FILE*)epa->handle);
epa->handle = NULL;
return 0;
}
case ASE_AWK_IO_READ:
{
/*
int chunk = (size > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)size;
if (ase_fgets (data, chunk, (FILE*)epa->handle) == ASE_NULL)
{
if (ferror((FILE*)epa->handle)) return -1;
return 0;
}
return ase_strlen(data);
*/
ase_ssize_t n = 0;
FILE* fp = (FILE*)epa->handle;
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;
}
return n;
}
case ASE_AWK_IO_WRITE:
{
FILE* fp = (FILE*)epa->handle;
ase_ssize_t left = size;
while (left > 0)
{
if (*data == ASE_T('\0'))
{
if (ase_fputc (*data, fp) == ASE_CHAR_EOF) return -1;
left -= 1; data += 1;
}
else
{
int chunk = (left > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)left;
int n = ase_fprintf (fp, ASE_T("%.*s"), chunk, data);
if (n < 0) return -1;
left -= n; data += n;
}
}
return size;
}
case ASE_AWK_IO_FLUSH:
{
if (fflush ((FILE*)epa->handle) == EOF) return -1;
return 0;
}
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 getdata (ase_char_t* data, ase_size_t size, FILE* fp)
{
ase_ssize_t n = 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 (c == ASE_T('\n')) break;
}
return n;
}
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;
/*int chunk = (size > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)size;
while (ase_fgets (data, chunk, (FILE*)epa->handle) == ASE_NULL)
{
if (ferror((FILE*)epa->handle)) return -1;*/
while ((n = getdata(data,size,(FILE*)epa->handle)) == 0)
{
/* it has reached the end of the current file.
* open the next file if available */
if (rd->icf[rd->icf_no] == ASE_NULL)
{
/* no more input console */
return 0;
}
if (rd->icf[rd->icf_no][0] == ASE_T('\0'))
{
if (epa->handle != ASE_NULL &&
epa->handle != stdin &&
epa->handle != stdout &&
epa->handle != stderr)
{
/* TODO: ................................ */
if (fclose ((FILE*)epa->handle) == EOF)
{
ase_cstr_t errarg;
errarg.ptr = ASE_T("console");
errarg.len = 7;
ase_awk_setrunerror (epa->run, ASE_AWK_ECLOSE, 0, &errarg, 1);
return -1;
}
}
epa->handle = stdin;
}
else
{
FILE* fp = ase_fopen (rd->icf[rd->icf_no], ASE_T("r"));
if (fp == ASE_NULL)
{
ase_cstr_t errarg;
errarg.ptr = rd->icf[rd->icf_no];
errarg.len = ase_strlen(rd->icf[rd->icf_no]);
ase_awk_setrunerror (epa->run, ASE_AWK_EOPEN, 0, &errarg, 1);
return -1;
}
if (ase_awk_setfilename (
epa->run, rd->icf[rd->icf_no],
ase_strlen(rd->icf[rd->icf_no])) == -1)
{
fclose (fp);
return -1;
}
if (ase_awk_setglobal (
epa->run, ASE_AWK_GLOBAL_FNR, ase_awk_val_zero) == -1)
{
/* need to reset FNR */
fclose (fp);
return -1;
}
if (epa->handle != ASE_NULL &&
epa->handle != stdin &&
epa->handle != stdout &&
epa->handle != stderr)
{
/* TODO: ................................ */
if (fclose ((FILE*)epa->handle) == EOF)
{
ase_cstr_t errarg;
errarg.ptr = ASE_T("console");
errarg.len = 7;
ase_awk_setrunerror (epa->run, ASE_AWK_ECLOSE, 0, &errarg, 1);
fclose (fp);
return -1;
}
}
dprint (ASE_T("open the next console [%s]\n"), rd->icf[rd->icf_no]);
epa->handle = fp;
}
rd->icf_no++;
}
/*return ase_strlen(data);*/
return n;
}
else if (cmd == ASE_AWK_IO_WRITE)
{
FILE* fp = (FILE*)epa->handle;
ase_ssize_t left = size;
while (left > 0)
{
if (*data == ASE_T('\0'))
{
if (ase_fputc (*data, fp) == ASE_CHAR_EOF) return -1;
left -= 1; data += 1;
}
else
{
int chunk = (left > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)left;
int n = ase_fprintf (fp, ASE_T("%.*s"), chunk, data);
if (n < 0) return -1;
left -= n; data += n;
}
}
return size;
}
else if (cmd == ASE_AWK_IO_FLUSH)
{
if (fflush ((FILE*)epa->handle) == EOF) return -1;
return 0;
}
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;
/* TODO: OpenConsole in GUI APPLICATION */
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_no] == ASE_NULL)
{
/* no more input file */
dprint (ASE_T("console - no more file\n"));;
return 0;
}
if (rd->icf[rd->icf_no][0] == ASE_T('\0'))
{
dprint (ASE_T(" console(r) - <standard input>\n"));
epa->handle = stdin;
}
else
{
/* a temporary variable fp is used here not to change
* any fields of epa when the open operation fails */
FILE* fp = ase_fopen (rd->icf[rd->icf_no], ASE_T("r"));
if (fp == ASE_NULL)
{
ase_cstr_t errarg;
errarg.ptr = rd->icf[rd->icf_no];
errarg.len = ase_strlen(rd->icf[rd->icf_no]);
ase_awk_setrunerror (epa->run, ASE_AWK_EOPEN, 0, &errarg, 1);
return -1;
}
dprint (ASE_T(" console(r) - %s\n"), rd->icf[rd->icf_no]);
if (ase_awk_setfilename (
epa->run, rd->icf[rd->icf_no],
ase_strlen(rd->icf[rd->icf_no])) == -1)
{
fclose (fp);
return -1;
}
epa->handle = fp;
}
rd->icf_no++;
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 = stdout;
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 != stdin &&
epa->handle != stdout &&
epa->handle != stderr)
{
if (fclose ((FILE*)epa->handle) == EOF)
{
ase_cstr_t errarg;
errarg.ptr = epa->name;
errarg.len = ase_strlen(epa->name);
ase_awk_setrunerror (epa->run, ASE_AWK_ECLOSE, 0, &errarg, 1);
return -1;
}
}
return 0;
}
static int next_extio_console (ase_awk_extio_t* epa)
{
int n;
FILE* fp = (FILE*)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 != stdin &&
fp != stdout && fp != stderr) fclose (fp);
return n;
}
ase_awk_t* app_awk = NULL;
ase_awk_run_t* app_run = NULL;
@ -720,7 +199,7 @@ static void on_run_return (
}
dprint (ASE_T("[NAMED VARIABLES]\n"));
ase_map_walk (ase_awk_getrunnamedvarmap(run), print_awk_value, run);
ase_map_walk (ase_awk_getrunnvmap(run), print_awk_value, run);
dprint (ASE_T("[END NAMED VARIABLES]\n"));
}
@ -781,42 +260,6 @@ static void print_usage (const ase_char_t* argv0)
}
}
static int run_awk (
ase_awk_t* awk, const ase_char_t* mfn,
ase_awk_runarg_t* runarg, ase_char_t** icf)
{
ase_awk_runcbs_t runcbs;
ase_awk_runios_t runios;
runio_data_t rd;
rd.icf = icf;
rd.icf_no = 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;
if (ase_awk_run (awk, mfn, &runios, &runcbs, runarg, ASE_NULL) == -1)
{
ase_printf (
ASE_T("RUN ERROR: CODE [%d] LINE [%u] %s\n"),
ase_awk_geterrnum(awk),
(unsigned int)ase_awk_geterrlin(awk),
ase_awk_geterrmsg(awk));
return -1;
}
return 0;
}
static int bfn_sleep (
ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
{
@ -1231,7 +674,9 @@ static int awk_main (int argc, ase_char_t* argv[])
ASE_T("PARSE ERROR: CODE [%d] LINE [%u] %s\n"),
ase_awk_geterrnum(awk),
(unsigned int)ase_awk_geterrlin(awk),
ase_awk_geterrmsg(awk));
ase_awk_geterrmsg(awk)
);
close_awk (awk);
return -1;
}
@ -1243,8 +688,15 @@ static int awk_main (int argc, ase_char_t* argv[])
signal (SIGINT, stop_run);
#endif
if (run_awk (awk, mfn, runarg, ao.icf) == -1)
if (ase_awk_runsimple (awk, ao.icf) == -1)
{
ase_printf (
ASE_T("RUN ERROR: CODE [%d] LINE [%u] %s\n"),
ase_awk_geterrnum(awk),
(unsigned int)ase_awk_geterrlin(awk),
ase_awk_geterrmsg(awk)
);
close_awk (awk);
return -1;
}