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; 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(); ConTrack* t = (ConTrack*)io.getHandle();
FILE* fp = t->handle; FILE* fp = t->handle;
@ -646,6 +646,7 @@ static struct
{ QSE_T("shift"), TestAwk::OPT_SHIFT }, { QSE_T("shift"), TestAwk::OPT_SHIFT },
{ QSE_T("idiv"), TestAwk::OPT_IDIV }, { QSE_T("idiv"), TestAwk::OPT_IDIV },
{ QSE_T("extio"), TestAwk::OPT_EXTIO }, { QSE_T("extio"), TestAwk::OPT_EXTIO },
{ QSE_T("rwpipe"), TestAwk::OPT_RWPIPE },
{ QSE_T("newline"), TestAwk::OPT_NEWLINE }, { QSE_T("newline"), TestAwk::OPT_NEWLINE },
{ QSE_T("baseone"), TestAwk::OPT_BASEONE }, { QSE_T("baseone"), TestAwk::OPT_BASEONE },
{ QSE_T("stripspaces"), TestAwk::OPT_STRIPSPACES }, { QSE_T("stripspaces"), TestAwk::OPT_STRIPSPACES },

View File

@ -49,10 +49,13 @@ public:
/** Represents a floating-point number */ /** Represents a floating-point number */
typedef qse_real_t real_t; typedef qse_real_t real_t;
/** Represents the internal hash table */ /** Represents the internal hash table */
typedef qse_map_t map_t; typedef qse_map_t map_t;
/** Represents a key/value pair */ /** Represents a key/value pair */
typedef qse_map_pair_t pair_t; typedef qse_map_pair_t pair_t;
typedef qse_mmgr_t mmgr_t;
typedef qse_ccls_t ccls_t;
/** Represents an internal awk value */ /** Represents an internal awk value */
typedef qse_awk_val_t val_t; typedef qse_awk_val_t val_t;
/** Represents the external I/O context */ /** Represents the external I/O context */
@ -223,6 +226,9 @@ public:
const void* getHandle () const; const void* getHandle () const;
void setHandle (void* handle); void setHandle (void* handle);
Awk* getAwk ();
const Awk* getAwk() const;
/** /**
* Returns the underlying extio_t handle * Returns the underlying extio_t handle
*/ */
@ -233,6 +239,7 @@ public:
* with the underlying extio_t handle * with the underlying extio_t handle
*/ */
const run_t* getRawRun () const; const run_t* getRawRun () const;
const awk_t* getRawAwk () const;
protected: protected:
extio_t* extio; extio_t* extio;
@ -249,7 +256,8 @@ public:
enum Mode enum Mode
{ {
READ = QSE_AWK_EXTIO_PIPE_READ, 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: protected:
@ -775,6 +783,26 @@ public:
/** Returns the error message */ /** Returns the error message */
const char_t* getErrorMessage () const; 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: protected:
void setError (ErrorCode code); void setError (ErrorCode code);
void setError (ErrorCode code, size_t line); void setError (ErrorCode code, size_t line);
@ -1013,7 +1041,7 @@ protected:
virtual int openPipe (Pipe& io) = 0; virtual int openPipe (Pipe& io) = 0;
virtual int closePipe (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 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; virtual int flushPipe (Pipe& io) = 0;
/*@}*/ /*@}*/
@ -1025,7 +1053,7 @@ protected:
virtual int openFile (File& io) = 0; virtual int openFile (File& io) = 0;
virtual int closeFile (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 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; virtual int flushFile (File& io) = 0;
/*@}*/ /*@}*/
@ -1036,7 +1064,7 @@ protected:
virtual int openConsole (Console& io) = 0; virtual int openConsole (Console& io) = 0;
virtual int closeConsole (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 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 flushConsole (Console& io) = 0;
virtual int nextConsole (Console& io) = 0; virtual int nextConsole (Console& io) = 0;
/*@}*/ /*@}*/
@ -1111,8 +1139,8 @@ private:
void triggerOnRunStart (Run& run); void triggerOnRunStart (Run& run);
qse_mmgr_t mmgr; mmgr_t mmgr;
qse_ccls_t ccls; ccls_t ccls;
qse_awk_prmfns_t prmfns; qse_awk_prmfns_t prmfns;
}; };

View File

@ -69,14 +69,14 @@ protected:
int openPipe (Pipe& io); int openPipe (Pipe& io);
int closePipe (Pipe& io); int closePipe (Pipe& io);
ssize_t readPipe (Pipe& io, char_t* buf, size_t len); 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); int flushPipe (Pipe& io);
// file io handlers // file io handlers
int openFile (File& io); int openFile (File& io);
int closeFile (File& io); int closeFile (File& io);
ssize_t readFile (File& io, char_t* buf, size_t len); 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); int flushFile (File& io);
// primitive handlers // primitive handlers

View File

@ -71,6 +71,16 @@ void Awk::Extio::setHandle (void* handle)
extio->handle = 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 const Awk::extio_t* Awk::Extio::getRawExtio () const
{ {
return extio; return extio;
@ -81,6 +91,11 @@ const Awk::run_t* Awk::Extio::getRawRun () const
return extio->run; return extio->run;
} }
const Awk::awk_t* Awk::Extio::getRawAwk () const
{
return qse_awk_getrunawk (extio->run);
}
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Awk::Pipe // Awk::Pipe
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
@ -119,7 +134,7 @@ Awk::Console::~Console ()
{ {
if (filename != QSE_NULL) if (filename != QSE_NULL)
{ {
qse_awk_free (qse_awk_getrunawk(extio->run), filename); qse_awk_free ((qse_awk_t*)getRawAwk(), filename);
} }
} }
@ -1326,10 +1341,10 @@ int Awk::run (const char_t* main, const char_t** args, size_t nargs)
// make sure that the run field is set in Awk::onRunStart. // make sure that the run field is set in Awk::onRunStart.
Run runctx (this); Run runctx (this);
runios.pipe = pipeHandler; runios.pipe = pipeHandler;
runios.file = fileHandler; runios.file = fileHandler;
runios.console = consoleHandler; runios.console = consoleHandler;
runios.data = this; runios.data = this;
QSE_MEMSET (&runcbs, 0, QSE_SIZEOF(runcbs)); QSE_MEMSET (&runcbs, 0, QSE_SIZEOF(runcbs));
runcbs.on_start = onRunStart; runcbs.on_start = onRunStart;

View File

@ -19,6 +19,7 @@
#include <qse/awk/StdAwk.hpp> #include <qse/awk/StdAwk.hpp>
#include <qse/cmn/str.h> #include <qse/cmn/str.h>
#include <qse/cmn/time.h> #include <qse/cmn/time.h>
#include <qse/cmn/pcp.h>
#include <qse/utl/stdio.h> #include <qse/utl/stdio.h>
#include <stdlib.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) int StdAwk::openPipe (Pipe& io)
{ {
Awk::Pipe::Mode mode = io.getMode(); Awk::Pipe::Mode mode = io.getMode();
FILE* fp = NULL; qse_pcp_t* pcp = QSE_NULL;
int flags;
switch (mode) switch (mode)
{ {
case Awk::Pipe::READ: 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; break;
case Awk::Pipe::WRITE: 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; 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; return 1;
} }
int StdAwk::closePipe (Pipe& io) int StdAwk::closePipe (Pipe& io)
{ {
fclose ((FILE*)io.getHandle()); qse_pcp_close ((qse_pcp_t*)io.getHandle());
return 0; return 0;
} }
StdAwk::ssize_t StdAwk::readPipe (Pipe& io, char_t* buf, size_t len) StdAwk::ssize_t StdAwk::readPipe (Pipe& io, char_t* buf, size_t len)
{ {
FILE* fp = (FILE*)io.getHandle(); return qse_pcp_read ((qse_pcp_t*)io.getHandle(), buf, len, QSE_PCP_OUT);
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;
} }
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(); return qse_pcp_write ((qse_pcp_t*)io.getHandle(), buf, len, QSE_PCP_IN);
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;
} }
int StdAwk::flushPipe (Pipe& io) 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 // file io handlers
@ -433,7 +387,7 @@ StdAwk::ssize_t StdAwk::readFile (File& io, char_t* buf, size_t len)
return n; 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(); FILE* fp = (FILE*)io.getHandle();
size_t left = len; size_t left = len;