added nwio to StdAwk.

added StdAwk::getConcoleCmgr() and StdAwk::setConsoleCmgr().
made io attributes names case-insensitive
This commit is contained in:
hyung-hwan 2012-08-09 10:15:11 +00:00
parent 998799bd4a
commit 0bacc36751
8 changed files with 274 additions and 64 deletions

View File

@ -269,9 +269,13 @@ public:
public:
const char_t* getName() const;
const void* getHandle () const;
void setHandle (void* handle);
int getUflags () const;
void setUflags (int uflags);
operator Awk* () const;
operator awk_t* () const;
operator rio_arg_t* () const;

View File

@ -108,14 +108,30 @@ public:
const char_t* ptr;
};
StdAwk (Mmgr* mmgr = &StdMmgr::DFL): Awk (mmgr) {}
StdAwk (Mmgr* mmgr = &StdMmgr::DFL):
Awk (mmgr), console_cmgr (QSE_NULL)
{
}
int open ();
void close ();
Run* parse (Source& in, Source& out);
/// The setConsoleCmgr() function sets the encoding type of
/// the console streams. They include both the input and the output
/// streams. It provides no way to specify a different encoding
/// type for the input and the output stream.
void setConsoleCmgr (const qse_cmgr_t* cmgr);
/// The getConsoleCmgr() function returns the current encoding
/// type set for the console streams.
const qse_cmgr_t* getConsoleCmgr () const;
/// The addConsoleOutput() function adds a file to form an
/// output console stream.
int addConsoleOutput (const char_t* arg, size_t len);
int addConsoleOutput (const char_t* arg);
void clearConsoleOutputs ();
protected:
@ -169,7 +185,6 @@ protected:
void* reallocMem (void* ptr, size_t n);
void freeMem (void* ptr);
int vsprintf (char_t* buf, size_t size,
const char_t* fmt, va_list arg);
@ -190,17 +205,19 @@ protected:
qse_htb_t cmgrtab;
bool cmgrtab_inited;
/* global variables */
qse_cmgr_t* console_cmgr;
// global variables
int gbl_argc;
int gbl_argv;
int gbl_environ;
int gbl_procinfo;
/* standard input console - reuse runarg */
// standard input console - reuse runarg
size_t runarg_index;
size_t runarg_count;
/* standard output console */
// standard output console
xstrs_t ofile;
size_t ofile_index;
size_t ofile_count;
@ -209,7 +226,7 @@ public:
struct ioattr_t
{
qse_cmgr_t* cmgr;
char_t cmgr_name[64]; /* i assume that the cmgr name never exceeds this length */
char_t cmgr_name[64]; // i assume that the cmgr name never exceeds this length.
int tmout[4];
ioattr_t (): cmgr (QSE_NULL)
@ -226,9 +243,13 @@ protected:
ioattr_t* get_ioattr (const char_t* ptr, size_t len);
ioattr_t* find_or_make_ioattr (const char_t* ptr, size_t len);
private:
int open_console_in (Console& io);
int open_console_out (Console& io);
int open_pio (Pipe& io);
int open_nwio (Pipe& io, int flags, void* nwad);
};
/////////////////////////////////

View File

@ -538,11 +538,14 @@ typedef enum qse_awk_rio_rwcmode_t qse_awk_rio_rwcmode_t;
*/
struct qse_awk_rio_arg_t
{
/* read-only. a user handler shouldn't change any of these fields */
qse_awk_rio_mode_t mode; /**< opening mode */
qse_char_t* name; /**< name of I/O object */
qse_awk_rio_rwcmode_t rwcmode; /**< closing mode for rwpipe */
/* read-write. a user handler can do whatever it likes to do with these. */
void* handle; /**< I/O handle set by a handler */
void* handle2; /**< secondary I/O handle set by a handler */
int uflags; /**< flags set by a handler */
/*-- from here down, internal use only --*/
int type;

View File

@ -69,6 +69,16 @@ void Awk::RIOBase::setHandle (void* handle)
this->riod->handle = handle;
}
int Awk::RIOBase::getUflags () const
{
return this->riod->uflags;
}
void Awk::RIOBase::setUflags (int uflags)
{
this->riod->uflags = uflags;
}
Awk::RIOBase::operator Awk* () const
{
return this->run->awk;

View File

@ -24,6 +24,7 @@
#include <qse/cmn/time.h>
#include <qse/cmn/pio.h>
#include <qse/cmn/sio.h>
#include <qse/cmn/nwio.h>
#include <qse/cmn/path.h>
#include <qse/cmn/stdio.h>
#include "awk.h"
@ -460,8 +461,10 @@ qse_cmgr_t* StdAwk::getcmgr (const char_t* ioname)
{
QSE_ASSERT (this->cmgrtab_inited == true);
qse_htb_pair_t* pair = qse_htb_search (&this->cmgrtab, ioname, qse_strlen(ioname));
if (pair) return (qse_cmgr_t*)QSE_HTB_VPTR(pair);
#if defined(QSE_CHAR_IS_WCHAR)
ioattr_t* ioattr = get_ioattr (ioname, qse_strlen(ioname));
if (ioattr) return ioattr->cmgr;
#endif
return QSE_NULL;
}
@ -482,11 +485,15 @@ StdAwk::ioattr_t* StdAwk::find_or_make_ioattr (const char_t* ptr, size_t len)
pair = qse_htb_search (&this->cmgrtab, ptr, len);
if (pair == QSE_NULL)
{
ioattr_t ioattr;
pair = qse_htb_insert (
&this->cmgrtab, (void*)ptr, len,
(void*)&ioattr, QSE_SIZEOF(ioattr));
if (pair == QSE_NULL) setError (QSE_AWK_ENOMEM);
(void*)&StdAwk::default_ioattr,
QSE_SIZEOF(StdAwk::default_ioattr));
if (pair == QSE_NULL)
{
setError (QSE_AWK_ENOMEM);
return QSE_NULL;
}
}
return (ioattr_t*)QSE_HTB_VPTR(pair);
@ -494,10 +501,10 @@ StdAwk::ioattr_t* StdAwk::find_or_make_ioattr (const char_t* ptr, size_t len)
static int timeout_code (const qse_char_t* name)
{
if (qse_strcmp (name, QSE_T("rtimeout")) == 0) return 0;
if (qse_strcmp (name, QSE_T("wtimeout")) == 0) return 1;
if (qse_strcmp (name, QSE_T("ctimeout")) == 0) return 2;
if (qse_strcmp (name, QSE_T("atimeout")) == 0) return 3;
if (qse_strcasecmp (name, QSE_T("rtimeout")) == 0) return 0;
if (qse_strcasecmp (name, QSE_T("wtimeout")) == 0) return 1;
if (qse_strcasecmp (name, QSE_T("ctimeout")) == 0) return 2;
if (qse_strcasecmp (name, QSE_T("atimeout")) == 0) return 3;
return -1;
}
@ -536,7 +543,7 @@ int StdAwk::setioattr (
return ret.setInt ((long_t)0);
}
#if defined(QSE_CHAR_IS_WCHAR)
else if (qse_strcmp (ptr[1], QSE_T("codepage")) == 0)
else if (qse_strcasecmp (ptr[1], QSE_T("codepage")) == 0)
{
ioattr_t* ioattr;
qse_cmgr_t* cmgr;
@ -544,7 +551,7 @@ int StdAwk::setioattr (
if (ptr[2][0] == QSE_T('\0')) cmgr = QSE_NULL;
else
{
cmgr = qse_findcmgr (ptr[1]);
cmgr = qse_findcmgr (ptr[2]);
if (cmgr == QSE_NULL) return ret.setInt ((long_t)-1);
}
@ -589,7 +596,7 @@ int StdAwk::getioattr (
return ret.setInt ((long_t)ioattr->tmout[tmout]);
}
#if defined(QSE_CHAR_IS_WCHAR)
else if (qse_strcmp (ptr[1], QSE_T("codepage")) == 0)
else if (qse_strcasecmp (ptr[1], QSE_T("codepage")) == 0)
{
return ret.setStr (ioattr->cmgr_name);
}
@ -601,7 +608,42 @@ int StdAwk::getioattr (
}
}
int StdAwk::openPipe (Pipe& io)
int StdAwk::open_nwio (Pipe& io, int flags, void* nwad)
{
qse_nwio_tmout_t tmout_buf;
qse_nwio_tmout_t* tmout = QSE_NULL;
const qse_char_t* name = io.getName();
ioattr_t* ioattr = get_ioattr (name, qse_strlen(name));
if (ioattr)
{
tmout = &tmout_buf;
tmout->r = ioattr->tmout[0];
tmout->w = ioattr->tmout[1];
tmout->c = ioattr->tmout[2];
tmout->a = ioattr->tmout[3];
}
qse_nwio_t* handle = qse_nwio_open (
this->getMmgr(), 0, (qse_nwad_t*)nwad,
flags | QSE_NWIO_TEXT | QSE_NWIO_IGNOREMBWCERR |
QSE_NWIO_REUSEADDR | QSE_NWIO_READNORETRY | QSE_NWIO_WRITENORETRY,
tmout
);
if (handle == QSE_NULL) return -1;
#if defined(QSE_CHAR_IS_WCHAR)
qse_cmgr_t* cmgr = this->getcmgr (io.getName());
if (cmgr) qse_nwio_setcmgr (handle, cmgr);
#endif
io.setHandle ((void*)handle);
io.setUflags (1);
return 1;
}
int StdAwk::open_pio (Pipe& io)
{
Awk::Pipe::Mode mode = io.getMode();
qse_pio_t* pio = QSE_NULL;
@ -645,10 +687,63 @@ int StdAwk::openPipe (Pipe& io)
}
#endif
io.setHandle (pio);
io.setUflags (0);
return 1;
}
static int parse_rwpipe_uri (const qse_char_t* uri, int* flags, qse_nwad_t* nwad)
{
static struct
{
const qse_char_t* prefix;
qse_size_t len;
int flags;
} x[] =
{
{ QSE_T("tcp://"), 6, QSE_NWIO_TCP },
{ QSE_T("udp://"), 6, QSE_NWIO_UDP },
{ QSE_T("tcpd://"), 7, QSE_NWIO_TCP | QSE_NWIO_PASSIVE },
{ QSE_T("udpd://"), 7, QSE_NWIO_UDP | QSE_NWIO_PASSIVE }
};
StdAwk::size_t i;
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_strzcmp (uri, x[i].prefix, x[i].len) == 0)
{
if (qse_strtonwad (uri + x[i].len, nwad) <= -1) return -1;
*flags = x[i].flags;
return 0;
}
}
return -1;
}
int StdAwk::openPipe (Pipe& io)
{
int flags;
qse_nwad_t nwad;
if (io.getMode() != Awk::Pipe::RW ||
parse_rwpipe_uri (io.getName(), &flags, &nwad) <= -1)
{
return open_pio (io);
}
else
{
return open_nwio (io, flags, &nwad);
}
}
int StdAwk::closePipe (Pipe& io)
{
if (io.getUflags() > 0)
{
/* nwio can't honor partical close */
qse_nwio_close ((qse_nwio_t*)io.getHandle());
}
else
{
qse_pio_t* pio = (qse_pio_t*)io.getHandle();
if (io.getMode() == Awk::Pipe::RW)
@ -667,22 +762,29 @@ int StdAwk::closePipe (Pipe& io)
}
qse_pio_close (pio);
}
return 0;
}
StdAwk::ssize_t StdAwk::readPipe (Pipe& io, char_t* buf, size_t len)
{
return qse_pio_read ((qse_pio_t*)io.getHandle(), QSE_PIO_OUT, buf, len);
return (io.getUflags() > 0)?
qse_nwio_read ((qse_nwio_t*)io.getHandle(), buf, len):
qse_pio_read ((qse_pio_t*)io.getHandle(), QSE_PIO_OUT, buf, len);
}
StdAwk::ssize_t StdAwk::writePipe (Pipe& io, const char_t* buf, size_t len)
{
return qse_pio_write ((qse_pio_t*)io.getHandle(), QSE_PIO_IN, buf, len);
return (io.getUflags() > 0)?
qse_nwio_write ((qse_nwio_t*)io.getHandle(), buf, len):
qse_pio_write ((qse_pio_t*)io.getHandle(), QSE_PIO_IN, buf, len);
}
int StdAwk::flushPipe (Pipe& io)
{
return qse_pio_flush ((qse_pio_t*)io.getHandle(), QSE_PIO_IN);
return (io.getUflags() > 0)?
qse_nwio_flush ((qse_nwio_t*)io.getHandle()):
qse_pio_flush ((qse_pio_t*)io.getHandle(), QSE_PIO_IN);
}
int StdAwk::openFile (File& io)
@ -739,6 +841,16 @@ int StdAwk::flushFile (File& io)
return qse_sio_flush ((qse_sio_t*)io.getHandle());
}
void StdAwk::setConsoleCmgr (const qse_cmgr_t* cmgr)
{
this->console_cmgr = (qse_cmgr_t*)cmgr;
}
const qse_cmgr_t* StdAwk::getConsoleCmgr () const
{
return this->console_cmgr;
}
int StdAwk::addConsoleOutput (const char_t* arg, size_t len)
{
QSE_ASSERT (awk != QSE_NULL);
@ -769,9 +881,14 @@ int StdAwk::open_console_in (Console& io)
{
qse_sio_t* sio;
sio = open_sio_std (QSE_NULL, io, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
sio = open_sio_std (
QSE_NULL, io, QSE_SIO_STDIN,
QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL) return -1;
if (this->console_cmgr)
qse_sio_setcmgr (sio, this->console_cmgr);
io.setHandle (sio);
this->runarg_count++;
return 1;
@ -807,9 +924,14 @@ int StdAwk::open_console_in (Console& io)
* 'BEGIN { ARGV[1]=""; ARGV[2]=""; }
* { print $0; }' file1 file2
*/
sio = open_sio_std (QSE_NULL, io, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
sio = open_sio_std (
QSE_NULL, io, QSE_SIO_STDIN,
QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL) return -1;
if (this->console_cmgr)
qse_sio_setcmgr (sio, this->console_cmgr);
io.setHandle (sio);
this->runarg_count++;
return 1;
@ -898,6 +1020,10 @@ int StdAwk::open_console_in (Console& io)
}
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
if (this->console_cmgr)
qse_sio_setcmgr (sio, this->console_cmgr);
io.setHandle (sio);
/* increment the counter of files successfully opened */
@ -919,8 +1045,14 @@ int StdAwk::open_console_out (Console& io)
if (this->ofile_count == 0)
{
qse_sio_t* sio;
sio = open_sio_std (QSE_NULL, io, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR);
sio = open_sio_std (
QSE_NULL, io, QSE_SIO_STDOUT,
QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL) return -1;
if (this->console_cmgr)
qse_sio_setcmgr (sio, this->console_cmgr);
io.setHandle (sio);
this->ofile_count++;
return 1;
@ -965,6 +1097,8 @@ int StdAwk::open_console_out (Console& io)
return -1;
}
if (this->console_cmgr)
qse_sio_setcmgr (sio, this->console_cmgr);
io.setHandle (sio);
this->ofile_index++;

View File

@ -842,7 +842,8 @@ static qse_ssize_t nwio_handler_open (
}
#endif
riod->handle2 = (void*)handle;
riod->handle = (void*)handle;
riod->uflags = 1; /* nwio indicator */
return 1;
}
@ -860,25 +861,24 @@ static qse_ssize_t nwio_handler_rest (
case QSE_AWK_RIO_CLOSE:
{
qse_nwio_close ((qse_nwio_t*)riod->handle2);
riod->handle2 = QSE_NULL;
qse_nwio_close ((qse_nwio_t*)riod->handle);
return 0;
}
case QSE_AWK_RIO_READ:
{
return qse_nwio_read ((qse_nwio_t*)riod->handle2, data, size);
return qse_nwio_read ((qse_nwio_t*)riod->handle, data, size);
}
case QSE_AWK_RIO_WRITE:
{
return qse_nwio_write ((qse_nwio_t*)riod->handle2, data, size);
return qse_nwio_write ((qse_nwio_t*)riod->handle, data, size);
}
case QSE_AWK_RIO_FLUSH:
{
/*if (riod->mode == QSE_AWK_RIO_PIPE_READ) return -1;*/
return qse_nwio_flush ((qse_nwio_t*)riod->handle2);
return qse_nwio_flush ((qse_nwio_t*)riod->handle);
}
case QSE_AWK_RIO_NEXT:
@ -893,7 +893,7 @@ static int parse_rwpipe_uri (const qse_char_t* uri, int* flags, qse_nwad_t* nwad
{
static struct
{
qse_char_t* prefix;
const qse_char_t* prefix;
qse_size_t len;
int flags;
} x[] =
@ -970,6 +970,7 @@ static qse_ssize_t pio_handler_open (
#endif
riod->handle = (void*)handle;
riod->uflags = 0; /* pio indicator */
return 1;
}
@ -990,7 +991,7 @@ static qse_ssize_t pio_handler_rest (
qse_pio_t* pio = (qse_pio_t*)riod->handle;
if (riod->mode == QSE_AWK_RIO_PIPE_RW)
{
/* specialy treatment is needef for rwpipe.
/* specialy treatment is needed for rwpipe.
* inspect rwcmode to see if partial closing is
* requested. */
if (riod->rwcmode == QSE_AWK_RIO_CLOSE_READ)
@ -1006,7 +1007,6 @@ static qse_ssize_t pio_handler_rest (
}
qse_pio_close (pio);
riod->handle = QSE_NULL;
return 0;
}
@ -1063,7 +1063,8 @@ static qse_ssize_t awk_rio_pipe (
rxtn = (rxtn_t*) QSE_XTN (rtx);
ioattr = get_ioattr (&rxtn->cmgrtab, riod->name, qse_strlen(riod->name));
ioattr = get_ioattr (
&rxtn->cmgrtab, riod->name, qse_strlen(riod->name));
if (ioattr)
{
tmout = &tmout_buf;
@ -1076,7 +1077,7 @@ static qse_ssize_t awk_rio_pipe (
return nwio_handler_open (rtx, riod, flags, &nwad, tmout);
}
}
else if (riod->handle2)
else if (riod->uflags > 0)
return nwio_handler_rest (rtx, cmd, riod, data, size);
else
return pio_handler_rest (rtx, cmd, riod, data, size);
@ -2104,10 +2105,10 @@ static int fnc_time (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
static int timeout_code (const qse_char_t* name)
{
if (qse_strcmp (name, QSE_T("rtimeout")) == 0) return 0;
if (qse_strcmp (name, QSE_T("wtimeout")) == 0) return 1;
if (qse_strcmp (name, QSE_T("ctimeout")) == 0) return 2;
if (qse_strcmp (name, QSE_T("atimeout")) == 0) return 3;
if (qse_strcasecmp (name, QSE_T("rtimeout")) == 0) return 0;
if (qse_strcasecmp (name, QSE_T("wtimeout")) == 0) return 1;
if (qse_strcasecmp (name, QSE_T("ctimeout")) == 0) return 2;
if (qse_strcasecmp (name, QSE_T("atimeout")) == 0) return 3;
return -1;
}
@ -2225,7 +2226,7 @@ static int fnc_setioattr (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
ioattr->tmout[tmout] = l;
}
#if defined(QSE_CHAR_IS_WCHAR)
else if (qse_strcmp (ptr[1], QSE_T("codepage")) == 0)
else if (qse_strcasecmp (ptr[1], QSE_T("codepage")) == 0)
{
ioattr_t* ioattr;
qse_cmgr_t* cmgr;
@ -2338,7 +2339,7 @@ static int fnc_getioattr (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
}
}
#if defined(QSE_CHAR_IS_WCHAR)
else if (qse_strcmp (ptr[1], QSE_T("codepage")) == 0)
else if (qse_strcasecmp (ptr[1], QSE_T("codepage")) == 0)
{
rv = qse_awk_rtx_makestrval0 (rtx, ioattr->cmgr_name);
if (rv == QSE_NULL)

View File

@ -84,11 +84,11 @@ qse_cmgr_t* qse_findcmgr (const qse_char_t* name)
if (cmgr) return cmgr;
}
if (qse_strcmp(name, QSE_T("")) == 0) return dfl_cmgr;
if (qse_strcmp(name, QSE_T("utf8")) == 0) return qse_utf8cmgr;
if (qse_strcmp(name, QSE_T("cp949")) == 0) return qse_cp949cmgr;
if (qse_strcmp(name, QSE_T("cp950")) == 0) return qse_cp950cmgr;
if (qse_strcmp(name, QSE_T("slmb")) == 0) return qse_slmbcmgr;
if (qse_strcasecmp(name, QSE_T("")) == 0) return dfl_cmgr;
if (qse_strcasecmp(name, QSE_T("utf8")) == 0) return qse_utf8cmgr;
if (qse_strcasecmp(name, QSE_T("cp949")) == 0) return qse_cp949cmgr;
if (qse_strcasecmp(name, QSE_T("cp950")) == 0) return qse_cp950cmgr;
if (qse_strcasecmp(name, QSE_T("slmb")) == 0) return qse_slmbcmgr;
}
return QSE_NULL;
}

View File

@ -438,5 +438,42 @@ static int awk_main (int argc, qse_char_t* argv[])
int qse_main (int argc, qse_achar_t* argv[])
{
return qse_runmain (argc,argv,awk_main);
int ret;
#if defined(_WIN32)
char locale[100];
UINT codepage;
WSADATA wsadata;
codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8)
{
/*SetConsoleOUtputCP (CP_UTF8);*/
qse_setdflcmgr (qse_utf8cmgr);
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
qse_setdflcmgr (qse_slmbcmgr);
}
if (WSAStartup (MAKEWORD(2,0), &wsadata) != 0)
{
print_error (QSE_T("Failed to start up winsock\n"));
return -1;
}
#else
setlocale (LC_ALL, "");
qse_setdflcmgr (qse_slmbcmgr);
#endif
ret = qse_runmain (argc, argv, awk_main);
#if defined(_WIN32)
WSACleanup ();
#endif
return ret;
}