replaced popen and stdio functions with qse_pcp_t functions in StdAwk.cpp

This commit is contained in:
hyung-hwan 2009-01-17 08:57:27 +00:00
parent 07d8efa7c2
commit 6aac3f59cf
5 changed files with 85 additions and 87 deletions

View File

@ -457,7 +457,7 @@ protected:
return n;
}
ssize_t writeConsole (Console& io, char_t* buf, size_t len)
ssize_t writeConsole (Console& io, const char_t* buf, size_t len)
{
ConTrack* t = (ConTrack*)io.getHandle();
FILE* fp = t->handle;
@ -646,6 +646,7 @@ static struct
{ QSE_T("shift"), TestAwk::OPT_SHIFT },
{ QSE_T("idiv"), TestAwk::OPT_IDIV },
{ QSE_T("extio"), TestAwk::OPT_EXTIO },
{ QSE_T("rwpipe"), TestAwk::OPT_RWPIPE },
{ QSE_T("newline"), TestAwk::OPT_NEWLINE },
{ QSE_T("baseone"), TestAwk::OPT_BASEONE },
{ QSE_T("stripspaces"), TestAwk::OPT_STRIPSPACES },

View File

@ -53,6 +53,9 @@ public:
/** Represents a key/value pair */
typedef qse_map_pair_t pair_t;
typedef qse_mmgr_t mmgr_t;
typedef qse_ccls_t ccls_t;
/** Represents an internal awk value */
typedef qse_awk_val_t val_t;
/** Represents the external I/O context */
@ -223,6 +226,9 @@ public:
const void* getHandle () const;
void setHandle (void* handle);
Awk* getAwk ();
const Awk* getAwk() const;
/**
* Returns the underlying extio_t handle
*/
@ -233,6 +239,7 @@ public:
* with the underlying extio_t handle
*/
const run_t* getRawRun () const;
const awk_t* getRawAwk () const;
protected:
extio_t* extio;
@ -249,7 +256,8 @@ public:
enum Mode
{
READ = QSE_AWK_EXTIO_PIPE_READ,
WRITE = QSE_AWK_EXTIO_PIPE_WRITE
WRITE = QSE_AWK_EXTIO_PIPE_WRITE,
RW = QSE_AWK_EXTIO_PIPE_RW
};
protected:
@ -775,6 +783,26 @@ public:
/** Returns the error message */
const char_t* getErrorMessage () const;
mmgr_t* getMmgr()
{
return qse_awk_getmmgr (awk);
}
const mmgr_t* getMmgr() const
{
return qse_awk_getmmgr (awk);
}
ccls_t* getCcls()
{
return qse_awk_getccls (awk);
}
const ccls_t* getCcls() const
{
return qse_awk_getccls (awk);
}
protected:
void setError (ErrorCode code);
void setError (ErrorCode code, size_t line);
@ -1013,7 +1041,7 @@ protected:
virtual int openPipe (Pipe& io) = 0;
virtual int closePipe (Pipe& io) = 0;
virtual ssize_t readPipe (Pipe& io, char_t* buf, size_t len) = 0;
virtual ssize_t writePipe (Pipe& io, char_t* buf, size_t len) = 0;
virtual ssize_t writePipe (Pipe& io, const char_t* buf, size_t len) = 0;
virtual int flushPipe (Pipe& io) = 0;
/*@}*/
@ -1025,7 +1053,7 @@ protected:
virtual int openFile (File& io) = 0;
virtual int closeFile (File& io) = 0;
virtual ssize_t readFile (File& io, char_t* buf, size_t len) = 0;
virtual ssize_t writeFile (File& io, char_t* buf, size_t len) = 0;
virtual ssize_t writeFile (File& io, const char_t* buf, size_t len) = 0;
virtual int flushFile (File& io) = 0;
/*@}*/
@ -1036,7 +1064,7 @@ protected:
virtual int openConsole (Console& io) = 0;
virtual int closeConsole (Console& io) = 0;
virtual ssize_t readConsole (Console& io, char_t* buf, size_t len) = 0;
virtual ssize_t writeConsole (Console& io, char_t* buf, size_t len) = 0;
virtual ssize_t writeConsole (Console& io, const char_t* buf, size_t len) = 0;
virtual int flushConsole (Console& io) = 0;
virtual int nextConsole (Console& io) = 0;
/*@}*/
@ -1111,8 +1139,8 @@ private:
void triggerOnRunStart (Run& run);
qse_mmgr_t mmgr;
qse_ccls_t ccls;
mmgr_t mmgr;
ccls_t ccls;
qse_awk_prmfns_t prmfns;
};

View File

@ -69,14 +69,14 @@ protected:
int openPipe (Pipe& io);
int closePipe (Pipe& io);
ssize_t readPipe (Pipe& io, char_t* buf, size_t len);
ssize_t writePipe (Pipe& io, char_t* buf, size_t len);
ssize_t writePipe (Pipe& io, const char_t* buf, size_t len);
int flushPipe (Pipe& io);
// file io handlers
int openFile (File& io);
int closeFile (File& io);
ssize_t readFile (File& io, char_t* buf, size_t len);
ssize_t writeFile (File& io, char_t* buf, size_t len);
ssize_t writeFile (File& io, const char_t* buf, size_t len);
int flushFile (File& io);
// primitive handlers

View File

@ -71,6 +71,16 @@ void Awk::Extio::setHandle (void* handle)
extio->handle = handle;
}
Awk::Awk* Awk::Extio::getAwk ()
{
return (Awk::Awk*)extio->data;
}
const Awk::Awk* Awk::Extio::getAwk () const
{
return (const Awk::Awk*)extio->data;
}
const Awk::extio_t* Awk::Extio::getRawExtio () const
{
return extio;
@ -81,6 +91,11 @@ const Awk::run_t* Awk::Extio::getRawRun () const
return extio->run;
}
const Awk::awk_t* Awk::Extio::getRawAwk () const
{
return qse_awk_getrunawk (extio->run);
}
//////////////////////////////////////////////////////////////////
// Awk::Pipe
//////////////////////////////////////////////////////////////////
@ -119,7 +134,7 @@ Awk::Console::~Console ()
{
if (filename != QSE_NULL)
{
qse_awk_free (qse_awk_getrunawk(extio->run), filename);
qse_awk_free ((qse_awk_t*)getRawAwk(), filename);
}
}

View File

@ -19,6 +19,7 @@
#include <qse/awk/StdAwk.hpp>
#include <qse/cmn/str.h>
#include <qse/cmn/time.h>
#include <qse/cmn/pcp.h>
#include <qse/utl/stdio.h>
#include <stdlib.h>
@ -281,104 +282,57 @@ int StdAwk::system (Run& run, Return& ret, const Argument* args, size_t nargs,
int StdAwk::openPipe (Pipe& io)
{
Awk::Pipe::Mode mode = io.getMode();
FILE* fp = NULL;
qse_pcp_t* pcp = QSE_NULL;
int flags;
switch (mode)
{
case Awk::Pipe::READ:
fp = qse_popen (io.getName(), QSE_T("r"));
/* TODO: should we specify ERRTOOUT? */
flags = QSE_PCP_READOUT |
QSE_PCP_ERRTOOUT;
break;
case Awk::Pipe::WRITE:
fp = qse_popen (io.getName(), QSE_T("w"));
flags = QSE_PCP_WRITEIN;
break;
case Awk::Pipe::RW:
flags = QSE_PCP_READOUT |
QSE_PCP_ERRTOOUT |
QSE_PCP_WRITEIN;
break;
}
if (fp == NULL) return -1;
pcp = qse_pcp_open (
io.getAwk()->getMmgr(),
0,
io.getName(),
flags|QSE_PCP_TEXT|QSE_PCP_SHELL
);
if (pcp == QSE_NULL) return -1;
io.setHandle (fp);
io.setHandle (pcp);
return 1;
}
int StdAwk::closePipe (Pipe& io)
{
fclose ((FILE*)io.getHandle());
qse_pcp_close ((qse_pcp_t*)io.getHandle());
return 0;
}
StdAwk::ssize_t StdAwk::readPipe (Pipe& io, char_t* buf, size_t len)
{
FILE* fp = (FILE*)io.getHandle();
ssize_t n = 0;
while (n < (ssize_t)len)
{
qse_cint_t c = qse_fgetc (fp);
if (c == QSE_CHAR_EOF)
{
if (qse_ferror(fp)) n = -1;
break;
}
buf[n++] = c;
if (c == QSE_T('\n')) break;
}
return n;
return qse_pcp_read ((qse_pcp_t*)io.getHandle(), buf, len, QSE_PCP_OUT);
}
StdAwk::ssize_t StdAwk::writePipe (Pipe& io, char_t* buf, size_t len)
StdAwk::ssize_t StdAwk::writePipe (Pipe& io, const char_t* buf, size_t len)
{
FILE* fp = (FILE*)io.getHandle();
size_t left = len;
while (left > 0)
{
if (*buf == QSE_T('\0'))
{
#if defined(QSE_CHAR_IS_WCHAR) && defined(__linux)
if (fputc ('\0', fp) == EOF)
#else
if (qse_fputc (*buf, fp) == QSE_CHAR_EOF)
#endif
{
return -1;
}
left -= 1; buf += 1;
}
else
{
#if defined(QSE_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 > QSE_TYPE_MAX(int))? QSE_TYPE_MAX(int): (int)left;
int n = fprintf (fp, "%.*ls", chunk, buf);
if (n >= 0)
{
size_t x;
for (x = 0; x < chunk; x++)
{
if (buf[x] == QSE_T('\0')) break;
}
n = x;
}
#else
int chunk = (left > QSE_TYPE_MAX(int))? QSE_TYPE_MAX(int): (int)left;
int n = qse_fprintf (fp, QSE_T("%.*s"), chunk, buf);
#endif
if (n < 0 || n > chunk) return -1;
left -= n; buf += n;
}
}
return len;
return qse_pcp_write ((qse_pcp_t*)io.getHandle(), buf, len, QSE_PCP_IN);
}
int StdAwk::flushPipe (Pipe& io)
{
return ::fflush ((FILE*)io.getHandle());
return qse_pcp_flush ((qse_pcp_t*)io.getHandle(), QSE_PCP_IN);
}
// file io handlers
@ -433,7 +387,7 @@ StdAwk::ssize_t StdAwk::readFile (File& io, char_t* buf, size_t len)
return n;
}
StdAwk::ssize_t StdAwk::writeFile (File& io, char_t* buf, size_t len)
StdAwk::ssize_t StdAwk::writeFile (File& io, const char_t* buf, size_t len)
{
FILE* fp = (FILE*)io.getHandle();
size_t left = len;