added support for source string

This commit is contained in:
hyung-hwan 2008-12-13 03:42:32 +00:00
parent cdbb0b419d
commit 2aa9f8321b
3 changed files with 116 additions and 107 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.c 469 2008-12-11 10:05:28Z baconevi $ * $Id: awk.c 478 2008-12-12 09:42:32Z baconevi $
*/ */
#include <ase/awk/awk.h> #include <ase/awk/awk.h>
@ -304,9 +304,9 @@ static void out_of_memory (void)
struct argout_t struct argout_t
{ {
ase_char_t* iss; /* input source string */ void* isp; /* input source files or string */
ase_char_t** isf; /* input source files */ int ist; /* input source type */
ase_size_t isfl; /* the number of input source filse */ ase_size_t isfl; /* the number of input source files */
ase_char_t* osf; /* output source file */ ase_char_t* osf; /* output source file */
ase_char_t** icf; /* input console files */ ase_char_t** icf; /* input console files */
ase_size_t icfl; /* the number of input console files */ ase_size_t icfl; /* the number of input console files */
@ -396,10 +396,10 @@ static int handle_args (int argc, ase_char_t* argv[], struct argout_t* ao)
case ASE_T('f'): case ASE_T('f'):
{ {
if (isfl >= isfc) if (isfl >= isfc-1) /* -1 for last ASE_NULL */
{ {
ase_char_t** tmp; ase_char_t** tmp;
tmp = (ase_char_t**)realloc (isf, ASE_SIZEOF(*isf)*(isfc+16)); tmp = (ase_char_t**) realloc (isf, ASE_SIZEOF(*isf)*(isfc+16));
if (tmp == ASE_NULL) if (tmp == ASE_NULL)
{ {
out_of_memory (); out_of_memory ();
@ -478,6 +478,7 @@ static int handle_args (int argc, ase_char_t* argv[], struct argout_t* ao)
} }
} }
isf[isfl] = ASE_NULL;
if (isfl <= 0) if (isfl <= 0)
{ {
@ -488,15 +489,13 @@ static int handle_args (int argc, ase_char_t* argv[], struct argout_t* ao)
} }
/* the source code is the string, not from the file */ /* the source code is the string, not from the file */
ao->iss = argv[opt.ind++]; /* source string */ ao->ist = ASE_AWK_PARSE_STRING;
ao->isf = ASE_NULL; /* no source file */ ao->isp = argv[opt.ind++];
ao->isfl = 0;
} }
else else
{ {
ao->iss = ASE_NULL; ao->ist = ASE_AWK_PARSE_FILES;
ao->isf = isf; ao->isp = isf;
ao->isfl = isfl;
} }
/* the remaining arguments are input console file names */ /* the remaining arguments are input console file names */
@ -552,6 +551,7 @@ static void init_awk_extension (ase_awk_t* awk)
ext->prmfns.pow = custom_awk_pow; ext->prmfns.pow = custom_awk_pow;
ext->prmfns.sprintf = custom_awk_sprintf; ext->prmfns.sprintf = custom_awk_sprintf;
ext->prmfns.data = ASE_NULL; ext->prmfns.data = ASE_NULL;
ase_awk_setprmfns (awk, &ext->prmfns); ase_awk_setprmfns (awk, &ext->prmfns);
} }
@ -667,8 +667,7 @@ static int awk_main (int argc, ase_char_t* argv[])
app_awk = awk; app_awk = awk;
if (ase_awk_parsesimple ( if (ase_awk_parsesimple (awk, ao.isp, ao.ist, ao.osf) == -1)
awk, ao.isf, ao.isfl, ao.osf, ASE_AWK_PARSE_FILES) == -1)
{ {
ase_printf ( ase_printf (
ASE_T("PARSE ERROR: CODE [%d] LINE [%u] %s\n"), ASE_T("PARSE ERROR: CODE [%d] LINE [%u] %s\n"),
@ -703,8 +702,7 @@ static int awk_main (int argc, ase_char_t* argv[])
close_awk (awk); close_awk (awk);
if (ao.iss != ASE_NULL) free (ao.iss); if (ao.ist == ASE_AWK_PARSE_FILES && ao.isp != ASE_NULL) free (ao.isp);
if (ao.isf != ASE_NULL) free (ao.isf);
if (ao.osf != ASE_NULL) free (ao.osf); if (ao.osf != ASE_NULL) free (ao.osf);
if (ao.icf != ASE_NULL) free (ao.icf); if (ao.icf != ASE_NULL) free (ao.icf);
if (ao.vm != ASE_NULL) ase_map_close (ao.vm); if (ao.vm != ASE_NULL) ase_map_close (ao.vm);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.h 470 2008-12-11 13:43:05Z baconevi $ * $Id: awk.h 478 2008-12-12 09:42:32Z baconevi $
* *
* {License} * {License}
*/ */
@ -474,7 +474,7 @@ enum ase_awk_valtostr_opt_t
ASE_AWK_VALTOSTR_PRINT = (1 << 2) ASE_AWK_VALTOSTR_PRINT = (1 << 2)
}; };
enum ase_awk_parse_opt_t enum ase_awk_parse_ist_t
{ {
ASE_AWK_PARSE_FILES = 0, ASE_AWK_PARSE_FILES = 0,
ASE_AWK_PARSE_STRING = 1 ASE_AWK_PARSE_STRING = 1
@ -843,10 +843,9 @@ ase_awk_t* ase_awk_opensimple (void);
*/ */
int ase_awk_parsesimple ( int ase_awk_parsesimple (
ase_awk_t* awk, ase_awk_t* awk,
const void* is /* source file names or source string */, const void* isp /* source file names or source string */,
ase_size_t isl /* source file count or source string length */, int ist /* ASE_AWK_PARSE_FILES, ASE_AWK_PARSE_STRING */,
const ase_char_t* osf /* an output source file name */, const ase_char_t* osf /* an output source file name */
int opt /* ASE_AWK_PARSE_FILES, ASE_AWK_PARSE_STRING */
); );
/******/ /******/

View File

@ -64,8 +64,12 @@ struct sf_t
{ {
struct struct
{ {
const ase_char_t*const* files; int type;
ase_size_t count; /* the number of files */ union
{
const ase_char_t*const* files;
const ase_char_t* str;
} p;
ase_size_t index; /* current file index */ ase_size_t index; /* current file index */
ase_sio_t* handle; /* the handle to an open file */ ase_sio_t* handle; /* the handle to an open file */
} in; } in;
@ -86,29 +90,29 @@ 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 (cmd == ASE_AWK_IO_OPEN)
{ {
if (sf->in.index >= sf->in.count) return 0; if (sf->in.type == ASE_AWK_PARSE_FILES)
/*
if (sf->in.files[sf->in.index] == ASE_NULL) return 0;
*/
if (sf->in.files[sf->in.index][0] == ASE_T('\0'))
{ {
sf->in.handle = ase_sio_in; if (sf->in.p.files[sf->in.index] == ASE_NULL) return 0;
}
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;
}
/* if (sf->in.p.files[sf->in.index][0] == ASE_T('\0'))
ase_awk_setsinname (); {
*/ sf->in.handle = ase_sio_in;
}
else
{
sf->in.handle = ase_sio_open (
ase_awk_getmmgr(sf->awk),
0,
sf->in.p.files[sf->in.index],
ASE_SIO_READ
);
if (sf->in.handle == ASE_NULL) return -1;
}
/*
ase_awk_setsinname ();
*/
}
return 1; return 1;
} }
@ -127,36 +131,47 @@ static ase_ssize_t sf_in (int cmd, void* arg, ase_char_t* data, ase_size_t size)
else if (cmd == ASE_AWK_IO_READ) else if (cmd == ASE_AWK_IO_READ)
{ {
ase_ssize_t n = 0; ase_ssize_t n = 0;
ase_sio_t* fp;
retry: if (sf->in.type == ASE_AWK_PARSE_FILES)
fp = sf->in.handle;
n = ase_sio_getsx (fp, data, size);
if (n == 0 && ++sf->in.index < sf->in.count)
{ {
if (fp != ase_sio_in) ase_sio_close (fp); ase_sio_t* sio;
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... retry:
set new source name.... sio = sf->in.handle;
ase_awk_setsinname ();
*/
goto retry; n = ase_sio_getsx (sio, data, size);
if (n == 0 && sf->in.p.files[++sf->in.index] != ASE_NULL)
{
if (sio != ase_sio_in) ase_sio_close (sio);
if (sf->in.p.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.p.files[sf->in.index],
ASE_SIO_READ
);
if (sf->in.handle == ASE_NULL) return -1;
}
/* TODO: reset internal line counters...
set new source name....
ase_awk_setsinname ();
*/
goto retry;
}
}
else
{
while (n < size && sf->in.p.str[sf->in.index] != ASE_T('\0'))
{
data[n++] = sf->in.p.str[sf->in.index++];
}
} }
return n; return n;
@ -233,35 +248,29 @@ static ase_ssize_t sf_out (int cmd, void* arg, ase_char_t* data, ase_size_t size
} }
int ase_awk_parsesimple ( int ase_awk_parsesimple (
ase_awk_t* awk, const void* is, ase_size_t isl, ase_awk_t* awk, const void* isp, int ist, const ase_char_t* osf)
const ase_char_t* osf, int opt)
{ {
sf_t sf; sf_t sf;
ase_awk_srcios_t sio; ase_awk_srcios_t sio;
if (is == ASE_NULL || isl == 0) if (isp == ASE_NULL)
{ {
ase_awk_seterrnum (awk, ASE_AWK_EINVAL); ase_awk_seterrnum (awk, ASE_AWK_EINVAL);
return -1; return -1;
} }
if (opt == ASE_AWK_PARSE_FILES) if (ist == ASE_AWK_PARSE_FILES) sf.in.p.files = isp;
{ else if (ist == ASE_AWK_PARSE_STRING) sf.in.p.str = isp;
sf.in.files = is;
sf.in.count = isl;
sf.in.index = 0;
sf.in.handle = ASE_NULL;
}
else if (opt == ASE_AWK_PARSE_STRING)
{
/* TODO */
}
else else
{ {
ase_awk_seterrnum (awk, ASE_AWK_EINVAL); ase_awk_seterrnum (awk, ASE_AWK_EINVAL);
return -1; return -1;
} }
sf.in.type = ist;
sf.in.index = 0;
sf.in.handle = ASE_NULL;
sf.out.file = osf; sf.out.file = osf;
sf.out.handle = ASE_NULL; sf.out.handle = ASE_NULL;
sf.awk = awk; sf.awk = awk;
@ -277,8 +286,11 @@ int ase_awk_parsesimple (
typedef struct runio_data_t typedef struct runio_data_t
{ {
ase_char_t** icf; /* input console files */ struct
ase_size_t icf_cur; {
ase_char_t** files;
ase_size_t index;
} ic; /* input console */
} runio_data_t; } runio_data_t;
static ase_ssize_t awk_extio_pipe ( static ase_ssize_t awk_extio_pipe (
@ -456,14 +468,14 @@ static int open_extio_console (ase_awk_extio_t* epa)
if (epa->mode == ASE_AWK_EXTIO_CONSOLE_READ) if (epa->mode == ASE_AWK_EXTIO_CONSOLE_READ)
{ {
if (rd->icf[rd->icf_cur] == ASE_NULL) if (rd->ic.files[rd->ic.index] == ASE_NULL)
{ {
/* no more input file */ /* no more input file */
//dprint (ASE_T("console - no more file\n"));; //dprint (ASE_T("console - no more file\n"));;
return 0; return 0;
} }
if (rd->icf[rd->icf_cur][0] == ASE_T('\0')) if (rd->ic.files[rd->ic.index][0] == ASE_T('\0'))
{ {
//dprint (ASE_T(" console(r) - <standard input>\n")); //dprint (ASE_T(" console(r) - <standard input>\n"));
epa->handle = ase_sio_in; epa->handle = ase_sio_in;
@ -477,24 +489,24 @@ static int open_extio_console (ase_awk_extio_t* epa)
fp = ase_sio_open ( fp = ase_sio_open (
ase_awk_getrunmmgr(epa->run), ase_awk_getrunmmgr(epa->run),
0, 0,
rd->icf[rd->icf_cur], rd->ic.files[rd->ic.index],
ASE_SIO_READ ASE_SIO_READ
); );
if (fp == ASE_NULL) if (fp == ASE_NULL)
{ {
ase_cstr_t errarg; ase_cstr_t errarg;
errarg.ptr = rd->icf[rd->icf_cur]; errarg.ptr = rd->ic.files[rd->ic.index];
errarg.len = ase_strlen(rd->icf[rd->icf_cur]); errarg.len = ase_strlen(rd->ic.files[rd->ic.index]);
ase_awk_setrunerror (epa->run, ASE_AWK_EOPEN, 0, &errarg, 1); ase_awk_setrunerror (epa->run, ASE_AWK_EOPEN, 0, &errarg, 1);
return -1; return -1;
} }
//dprint (ASE_T(" console(r) - %s\n"), rd->icf[rd->icf_cur]); //dprint (ASE_T(" console(r) - %s\n"), rd->ic.files[rd->ic.index]);
if (ase_awk_setfilename ( if (ase_awk_setfilename (
epa->run, rd->icf[rd->icf_cur], epa->run, rd->ic.files[rd->ic.index],
ase_strlen(rd->icf[rd->icf_cur])) == -1) ase_strlen(rd->ic.files[rd->ic.index])) == -1)
{ {
ase_sio_close (fp); ase_sio_close (fp);
return -1; return -1;
@ -503,7 +515,7 @@ static int open_extio_console (ase_awk_extio_t* epa)
epa->handle = fp; epa->handle = fp;
} }
rd->icf_cur++; rd->ic.index++;
return 1; return 1;
} }
else if (epa->mode == ASE_AWK_EXTIO_CONSOLE_WRITE) else if (epa->mode == ASE_AWK_EXTIO_CONSOLE_WRITE)
@ -554,13 +566,13 @@ static ase_ssize_t awk_extio_console (
{ {
/* it has reached the end of the current file. /* it has reached the end of the current file.
* open the next file if available */ * open the next file if available */
if (rd->icf[rd->icf_cur] == ASE_NULL) if (rd->ic.files[rd->ic.index] == ASE_NULL)
{ {
/* no more input console */ /* no more input console */
return 0; return 0;
} }
if (rd->icf[rd->icf_cur][0] == ASE_T('\0')) if (rd->ic.files[rd->ic.index][0] == ASE_T('\0'))
{ {
if (epa->handle != ASE_NULL && if (epa->handle != ASE_NULL &&
epa->handle != ase_sio_in && epa->handle != ase_sio_in &&
@ -579,7 +591,7 @@ static ase_ssize_t awk_extio_console (
fp = ase_sio_open ( fp = ase_sio_open (
ase_awk_getrunmmgr(epa->run), ase_awk_getrunmmgr(epa->run),
0, 0,
rd->icf[rd->icf_cur], rd->ic.files[rd->ic.index],
ASE_SIO_READ ASE_SIO_READ
); );
@ -587,16 +599,16 @@ static ase_ssize_t awk_extio_console (
{ {
ase_cstr_t errarg; ase_cstr_t errarg;
errarg.ptr = rd->icf[rd->icf_cur]; errarg.ptr = rd->ic.files[rd->ic.index];
errarg.len = ase_strlen(rd->icf[rd->icf_cur]); errarg.len = ase_strlen(rd->ic.files[rd->ic.index]);
ase_awk_setrunerror (epa->run, ASE_AWK_EOPEN, 0, &errarg, 1); ase_awk_setrunerror (epa->run, ASE_AWK_EOPEN, 0, &errarg, 1);
return -1; return -1;
} }
if (ase_awk_setfilename ( if (ase_awk_setfilename (
epa->run, rd->icf[rd->icf_cur], epa->run, rd->ic.files[rd->ic.index],
ase_strlen(rd->icf[rd->icf_cur])) == -1) ase_strlen(rd->ic.files[rd->ic.index])) == -1)
{ {
ase_sio_close (fp); ase_sio_close (fp);
return -1; return -1;
@ -618,11 +630,11 @@ static ase_ssize_t awk_extio_console (
ase_sio_close ((ase_sio_t*)epa->handle); ase_sio_close ((ase_sio_t*)epa->handle);
} }
//dprint (ASE_T("open the next console [%s]\n"), rd->icf[rd->icf_cur]); //dprint (ASE_T("open the next console [%s]\n"), rd->ic.files[rd->ic.index]);
epa->handle = fp; epa->handle = fp;
} }
rd->icf_cur++; rd->ic.index++;
} }
return n; return n;
@ -675,8 +687,8 @@ int ase_awk_runsimple (ase_awk_t* awk, ase_char_t** icf)
ase_awk_runios_t runios; ase_awk_runios_t runios;
runio_data_t rd; runio_data_t rd;
rd.icf = icf; rd.ic.files = icf;
rd.icf_cur = 0; rd.ic.index = 0;
runios.pipe = awk_extio_pipe; runios.pipe = awk_extio_pipe;
runios.file = awk_extio_file; runios.file = awk_extio_file;