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>
@ -304,9 +304,9 @@ static void out_of_memory (void)
struct argout_t
{
ase_char_t* iss; /* input source string */
ase_char_t** isf; /* input source files */
ase_size_t isfl; /* the number of input source filse */
void* isp; /* input source files or string */
int ist; /* input source type */
ase_size_t isfl; /* the number of input source files */
ase_char_t* osf; /* output source file */
ase_char_t** icf; /* input console files */
ase_size_t icfl; /* the number of input console files */
@ -396,7 +396,7 @@ static int handle_args (int argc, ase_char_t* argv[], struct argout_t* ao)
case ASE_T('f'):
{
if (isfl >= isfc)
if (isfl >= isfc-1) /* -1 for last ASE_NULL */
{
ase_char_t** tmp;
tmp = (ase_char_t**) realloc (isf, ASE_SIZEOF(*isf)*(isfc+16));
@ -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)
{
@ -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 */
ao->iss = argv[opt.ind++]; /* source string */
ao->isf = ASE_NULL; /* no source file */
ao->isfl = 0;
ao->ist = ASE_AWK_PARSE_STRING;
ao->isp = argv[opt.ind++];
}
else
{
ao->iss = ASE_NULL;
ao->isf = isf;
ao->isfl = isfl;
ao->ist = ASE_AWK_PARSE_FILES;
ao->isp = isf;
}
/* 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.sprintf = custom_awk_sprintf;
ext->prmfns.data = ASE_NULL;
ase_awk_setprmfns (awk, &ext->prmfns);
}
@ -667,8 +667,7 @@ static int awk_main (int argc, ase_char_t* argv[])
app_awk = awk;
if (ase_awk_parsesimple (
awk, ao.isf, ao.isfl, ao.osf, ASE_AWK_PARSE_FILES) == -1)
if (ase_awk_parsesimple (awk, ao.isp, ao.ist, ao.osf) == -1)
{
ase_printf (
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);
if (ao.iss != ASE_NULL) free (ao.iss);
if (ao.isf != ASE_NULL) free (ao.isf);
if (ao.ist == ASE_AWK_PARSE_FILES && ao.isp != ASE_NULL) free (ao.isp);
if (ao.osf != ASE_NULL) free (ao.osf);
if (ao.icf != ASE_NULL) free (ao.icf);
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}
*/
@ -474,7 +474,7 @@ enum ase_awk_valtostr_opt_t
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_STRING = 1
@ -843,10 +843,9 @@ ase_awk_t* ase_awk_opensimple (void);
*/
int ase_awk_parsesimple (
ase_awk_t* awk,
const void* is /* source file names or source string */,
ase_size_t isl /* source file count or source string length */,
const ase_char_t* osf /* an output source file name */,
int opt /* ASE_AWK_PARSE_FILES, ASE_AWK_PARSE_STRING */
const void* isp /* source file names or source string */,
int ist /* ASE_AWK_PARSE_FILES, ASE_AWK_PARSE_STRING */,
const ase_char_t* osf /* an output source file name */
);
/******/

View File

@ -63,9 +63,13 @@ typedef struct sf_t sf_t;
struct sf_t
{
struct
{
int type;
union
{
const ase_char_t*const* files;
ase_size_t count; /* the number of files */
const ase_char_t* str;
} p;
ase_size_t index; /* current file index */
ase_sio_t* handle; /* the handle to an open file */
} in;
@ -86,12 +90,11 @@ 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.type == ASE_AWK_PARSE_FILES)
{
if (sf->in.p.files[sf->in.index] == ASE_NULL) return 0;
if (sf->in.files[sf->in.index][0] == ASE_T('\0'))
if (sf->in.p.files[sf->in.index][0] == ASE_T('\0'))
{
sf->in.handle = ase_sio_in;
}
@ -100,7 +103,7 @@ static ase_ssize_t sf_in (int cmd, void* arg, ase_char_t* data, ase_size_t size)
sf->in.handle = ase_sio_open (
ase_awk_getmmgr(sf->awk),
0,
sf->in.files[sf->in.index],
sf->in.p.files[sf->in.index],
ASE_SIO_READ
);
if (sf->in.handle == ASE_NULL) return -1;
@ -109,6 +112,7 @@ static ase_ssize_t sf_in (int cmd, void* arg, ase_char_t* data, ase_size_t size)
/*
ase_awk_setsinname ();
*/
}
return 1;
}
@ -127,16 +131,19 @@ 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)
{
ase_ssize_t n = 0;
ase_sio_t* fp;
if (sf->in.type == ASE_AWK_PARSE_FILES)
{
ase_sio_t* sio;
retry:
fp = sf->in.handle;
sio = sf->in.handle;
n = ase_sio_getsx (fp, data, size);
if (n == 0 && ++sf->in.index < sf->in.count)
n = ase_sio_getsx (sio, data, size);
if (n == 0 && sf->in.p.files[++sf->in.index] != ASE_NULL)
{
if (fp != ase_sio_in) ase_sio_close (fp);
if (sf->in.files[sf->in.index][0] == ASE_T('\0'))
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;
}
@ -145,7 +152,7 @@ static ase_ssize_t sf_in (int cmd, void* arg, ase_char_t* data, ase_size_t size)
sf->in.handle = ase_sio_open (
ase_awk_getmmgr(sf->awk),
0,
sf->in.files[sf->in.index],
sf->in.p.files[sf->in.index],
ASE_SIO_READ
);
if (sf->in.handle == ASE_NULL) return -1;
@ -158,6 +165,14 @@ static ase_ssize_t sf_in (int cmd, void* arg, ase_char_t* data, ase_size_t size)
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;
}
@ -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 (
ase_awk_t* awk, const void* is, ase_size_t isl,
const ase_char_t* osf, int opt)
ase_awk_t* awk, const void* isp, int ist, const ase_char_t* osf)
{
sf_t sf;
ase_awk_srcios_t sio;
if (is == ASE_NULL || isl == 0)
if (isp == ASE_NULL)
{
ase_awk_seterrnum (awk, ASE_AWK_EINVAL);
return -1;
}
if (opt == ASE_AWK_PARSE_FILES)
{
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 */
}
if (ist == ASE_AWK_PARSE_FILES) sf.in.p.files = isp;
else if (ist == ASE_AWK_PARSE_STRING) sf.in.p.str = isp;
else
{
ase_awk_seterrnum (awk, ASE_AWK_EINVAL);
return -1;
}
sf.in.type = ist;
sf.in.index = 0;
sf.in.handle = ASE_NULL;
sf.out.file = osf;
sf.out.handle = ASE_NULL;
sf.awk = awk;
@ -277,8 +286,11 @@ int ase_awk_parsesimple (
typedef struct runio_data_t
{
ase_char_t** icf; /* input console files */
ase_size_t icf_cur;
struct
{
ase_char_t** files;
ase_size_t index;
} ic; /* input console */
} runio_data_t;
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 (rd->icf[rd->icf_cur] == ASE_NULL)
if (rd->ic.files[rd->ic.index] == 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'))
if (rd->ic.files[rd->ic.index][0] == ASE_T('\0'))
{
//dprint (ASE_T(" console(r) - <standard input>\n"));
epa->handle = ase_sio_in;
@ -477,24 +489,24 @@ static int open_extio_console (ase_awk_extio_t* epa)
fp = ase_sio_open (
ase_awk_getrunmmgr(epa->run),
0,
rd->icf[rd->icf_cur],
rd->ic.files[rd->ic.index],
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]);
errarg.ptr = rd->ic.files[rd->ic.index];
errarg.len = ase_strlen(rd->ic.files[rd->ic.index]);
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]);
//dprint (ASE_T(" console(r) - %s\n"), rd->ic.files[rd->ic.index]);
if (ase_awk_setfilename (
epa->run, rd->icf[rd->icf_cur],
ase_strlen(rd->icf[rd->icf_cur])) == -1)
epa->run, rd->ic.files[rd->ic.index],
ase_strlen(rd->ic.files[rd->ic.index])) == -1)
{
ase_sio_close (fp);
return -1;
@ -503,7 +515,7 @@ static int open_extio_console (ase_awk_extio_t* epa)
epa->handle = fp;
}
rd->icf_cur++;
rd->ic.index++;
return 1;
}
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.
* 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 */
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 &&
epa->handle != ase_sio_in &&
@ -579,7 +591,7 @@ static ase_ssize_t awk_extio_console (
fp = ase_sio_open (
ase_awk_getrunmmgr(epa->run),
0,
rd->icf[rd->icf_cur],
rd->ic.files[rd->ic.index],
ASE_SIO_READ
);
@ -587,16 +599,16 @@ static ase_ssize_t awk_extio_console (
{
ase_cstr_t errarg;
errarg.ptr = rd->icf[rd->icf_cur];
errarg.len = ase_strlen(rd->icf[rd->icf_cur]);
errarg.ptr = rd->ic.files[rd->ic.index];
errarg.len = ase_strlen(rd->ic.files[rd->ic.index]);
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)
epa->run, rd->ic.files[rd->ic.index],
ase_strlen(rd->ic.files[rd->ic.index])) == -1)
{
ase_sio_close (fp);
return -1;
@ -618,11 +630,11 @@ static ase_ssize_t awk_extio_console (
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;
}
rd->icf_cur++;
rd->ic.index++;
}
return n;
@ -675,8 +687,8 @@ int ase_awk_runsimple (ase_awk_t* awk, ase_char_t** icf)
ase_awk_runios_t runios;
runio_data_t rd;
rd.icf = icf;
rd.icf_cur = 0;
rd.ic.files = icf;
rd.ic.index = 0;
runios.pipe = awk_extio_pipe;
runios.file = awk_extio_file;