fixed wrong type casting bugs in StdAwk.

made changes to use qse_sio_openstd() in StdAwk.
fixed a bug of opening an output console file in the wrong mode in StdAwk
made changes to use qse_sio_openstd() in cut/std.c.
removed qse_sio_in/qse_sio_out/qse_sio_err.
This commit is contained in:
hyung-hwan 2011-12-20 16:59:21 +00:00
parent d5db73dfce
commit f4df349fc7
4 changed files with 137 additions and 316 deletions

View File

@ -51,6 +51,46 @@ QSE_BEGIN_NAMESPACE(QSE)
} \
} while (0)
static qse_sio_t* open_sio (Awk* awk, StdAwk::Run* run, const qse_char_t* file, int flags)
{
qse_sio_t* sio;
//sio = qse_sio_open ((run? ((Awk::awk_t*)*(Awk*)*run)->mmgr: awk->getMmgr()), 0, file, flags);
sio = qse_sio_open ((run? ((Awk*)*run)->getMmgr(): awk->getMmgr()), 0, file, flags);
if (sio == QSE_NULL)
{
qse_cstr_t ea;
ea.ptr = file;
ea.len = qse_strlen (file);
if (run) run->setError (QSE_AWK_EOPEN, &ea);
else awk->setError (QSE_AWK_EOPEN, &ea);
}
return sio;
}
static qse_sio_t* open_sio_std (Awk* awk, StdAwk::Run* run, qse_sio_std_t std, int flags)
{
qse_sio_t* sio;
static const qse_char_t* std_names[] =
{
QSE_T("stdin"),
QSE_T("stdout"),
QSE_T("stderr"),
};
//sio = qse_sio_openstd ((run? ((Awk::awk_t*)*(Awk*)*run)->mmgr: awk->getMmgr()), 0, std, flags);
sio = qse_sio_openstd ((run? ((Awk*)*run)->getMmgr(): awk->getMmgr()), 0, std, flags);
if (sio == QSE_NULL)
{
qse_cstr_t ea;
ea.ptr = std_names[std];
ea.len = qse_strlen (std_names[std]);
if (run) run->setError (QSE_AWK_EOPEN, &ea);
else awk->setError (QSE_AWK_EOPEN, &ea);
}
return sio;
}
int StdAwk::open ()
{
int n = Awk::open ();
@ -116,17 +156,11 @@ int StdAwk::system (Run& run, Value& ret, const Value* args, size_t nargs,
#else
qse_mchar_t* mbs;
mbs = qse_wcstombsdup (ptr, ((awk_t*)(Awk*)run)->mmgr);
if (mbs == QSE_NULL)
{
qse_awk_freemem ((awk_t*)(Awk*)run, mbs);
return -1;
}
mbs = qse_wcstombsdup (ptr, ((Awk*)run)->getMmgr());
if (mbs == QSE_NULL) return -1;
int n = ret.setInt ((long_t)::system(mbs));
qse_awk_freemem ((awk_t*)(Awk*)run, mbs);
QSE_MMGR_FREE (((Awk*)run)->getMmgr(), mbs);
return n;
#endif
}
@ -263,7 +297,7 @@ int StdAwk::flushFile (File& io)
int StdAwk::addConsoleOutput (const char_t* arg, size_t len)
{
QSE_ASSERT (awk != QSE_NULL);
int n = ofile.add (awk, arg, len);
int n = this->ofile.add (awk, arg, len);
if (n <= -1) setError (QSE_AWK_ENOMEM);
return n;
}
@ -275,21 +309,26 @@ int StdAwk::addConsoleOutput (const char_t* arg)
void StdAwk::clearConsoleOutputs ()
{
ofile.clear (awk);
this->ofile.clear (awk);
}
int StdAwk::open_console_in (Console& io)
{
qse_awk_rtx_t* rtx = (rtx_t*)io;
if (runarg.ptr == QSE_NULL)
if (this->runarg.ptr == QSE_NULL)
{
QSE_ASSERT (runarg.len == 0 && runarg.capa == 0);
QSE_ASSERT (this->runarg.len == 0 && this->runarg.capa == 0);
if (runarg_count == 0)
if (this->runarg_count == 0)
{
io.setHandle (qse_sio_in);
runarg_count++;
qse_sio_t* sio;
sio = open_sio_std (QSE_NULL, io, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL) return -1;
io.setHandle (sio);
this->runarg_count++;
return 1;
}
@ -308,13 +347,13 @@ int StdAwk::open_console_in (Console& io)
qse_awk_rtx_valtostr_out_t out;
nextfile:
file = runarg.ptr[runarg_index].ptr;
file = this->runarg.ptr[this->runarg_index].ptr;
if (file == QSE_NULL)
{
/* no more input file */
if (runarg_count == 0)
if (this->runarg_count == 0)
{
/* all ARGVs are empty strings.
* so no console files were opened.
@ -323,15 +362,18 @@ int StdAwk::open_console_in (Console& io)
* 'BEGIN { ARGV[1]=""; ARGV[2]=""; }
* { print $0; }' file1 file2
*/
io.setHandle (qse_sio_in);
runarg_count++;
sio = open_sio_std (QSE_NULL, io, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL) return -1;
io.setHandle (sio);
this->runarg_count++;
return 1;
}
return 0;
}
if (qse_strlen(file) != runarg.ptr[runarg_index].len)
if (qse_strlen(file) != this->runarg.ptr[this->runarg_index].len)
{
cstr_t arg;
arg.ptr = file;
@ -354,10 +396,10 @@ int StdAwk::open_console_in (Console& io)
map = ((qse_awk_val_map_t*)argv)->map;
QSE_ASSERT (map != QSE_NULL);
// ok to find ARGV[runarg_index] as ARGV[0]
// ok to find ARGV[this->runarg_index] as ARGV[0]
// has been skipped.
ibuflen = qse_awk_longtostr (
rtx->awk, runarg_index,
rtx->awk, this->runarg_index,
10, QSE_NULL,
ibuf, QSE_COUNTOF(ibuf)
);
@ -375,7 +417,7 @@ int StdAwk::open_console_in (Console& io)
{
/* the name is empty */
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
runarg_index++;
this->runarg_index++;
goto nextfile;
}
@ -393,29 +435,19 @@ int StdAwk::open_console_in (Console& io)
file = out.u.cpldup.ptr;
if (file[0] == QSE_T('-') && file[1] == QSE_T('\0'))
{
/* special file name '-' */
sio = qse_sio_in;
}
sio = open_sio_std (QSE_NULL, io, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
else
sio = open_sio (QSE_NULL, io, file, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL)
{
sio = qse_sio_open (
rtx->awk->mmgr, 0, file, QSE_SIO_READ);
if (sio == QSE_NULL)
{
cstr_t arg;
arg.ptr = file;
arg.len = qse_strlen (arg.ptr);
((Run*)io)->setError (QSE_AWK_EOPEN, &arg);
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
return -1;
}
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
return -1;
}
if (qse_awk_rtx_setfilename (
rtx, file, qse_strlen(file)) == -1)
{
if (sio != qse_sio_in) qse_sio_close (sio);
qse_sio_close (sio);
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
return -1;
}
@ -424,8 +456,8 @@ int StdAwk::open_console_in (Console& io)
io.setHandle (sio);
/* increment the counter of files successfully opened */
runarg_count++;
runarg_index++;
this->runarg_count++;
this->runarg_index++;
return 1;
}
@ -435,14 +467,17 @@ int StdAwk::open_console_out (Console& io)
{
qse_awk_rtx_t* rtx = (rtx_t*)io;
if (ofile.ptr == QSE_NULL)
if (this->ofile.ptr == QSE_NULL)
{
QSE_ASSERT (ofile.len == 0 && ofile.capa == 0);
QSE_ASSERT (this->ofile.len == 0 && this->ofile.capa == 0);
if (ofile_count == 0)
if (this->ofile_count == 0)
{
io.setHandle (qse_sio_out);
ofile_count++;
qse_sio_t* sio;
sio = open_sio_std (QSE_NULL, io, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL) return -1;
io.setHandle (sio);
this->ofile_count++;
return 1;
}
@ -455,7 +490,7 @@ int StdAwk::open_console_out (Console& io)
qse_sio_t* sio;
const qse_char_t* file;
file = ofile.ptr[ofile_index].ptr;
file = this->ofile.ptr[this->ofile_index].ptr;
if (file == QSE_NULL)
{
@ -463,7 +498,7 @@ int StdAwk::open_console_out (Console& io)
return 0;
}
if (qse_strlen(file) != ofile.ptr[ofile_index].len)
if (qse_strlen(file) != this->ofile.ptr[this->ofile_index].len)
{
cstr_t arg;
arg.ptr = file;
@ -473,23 +508,10 @@ int StdAwk::open_console_out (Console& io)
}
if (file[0] == QSE_T('-') && file[1] == QSE_T('\0'))
{
/* special file name '-' */
sio = qse_sio_out;
}
sio = open_sio_std (QSE_NULL, io, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR);
else
{
sio = qse_sio_open (
rtx->awk->mmgr, 0, file, QSE_SIO_READ);
if (sio == QSE_NULL)
{
cstr_t arg;
arg.ptr = file;
arg.len = qse_strlen (arg.ptr);
((Run*)io)->setError (QSE_AWK_EOPEN, &arg);
return -1;
}
}
sio = open_sio (QSE_NULL, io, file, QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_TRUNCATE | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL) return -1;
if (qse_awk_rtx_setofilename (
rtx, file, qse_strlen(file)) == -1)
@ -500,8 +522,8 @@ int StdAwk::open_console_out (Console& io)
io.setHandle (sio);
ofile_index++;
ofile_count++;
this->ofile_index++;
this->ofile_count++;
return 1;
}
}
@ -512,12 +534,12 @@ int StdAwk::openConsole (Console& io)
if (mode == Console::READ)
{
runarg_count = 0;
runarg_index = 0;
if (runarg.len > 0)
this->runarg_count = 0;
this->runarg_index = 0;
if (this->runarg.len > 0)
{
// skip ARGV[0]
runarg_index++;
this->runarg_index++;
}
return open_console_in (io);
}
@ -525,8 +547,8 @@ int StdAwk::openConsole (Console& io)
{
QSE_ASSERT (mode == Console::WRITE);
ofile_count = 0;
ofile_index = 0;
this->ofile_count = 0;
this->ofile_index = 0;
return open_console_out (io);
}
}
@ -756,36 +778,31 @@ int StdAwk::SourceFile::open (Data& io)
{
// open the main source file.
if (name[0] == QSE_T('-') && name[1] == QSE_T('\0'))
if (this->name[0] == QSE_T('-') && this->name[1] == QSE_T('\0'))
{
sio = (io.getMode() == READ)? qse_sio_in: qse_sio_out;
if (io.getMode() == READ)
sio = open_sio_std (io, QSE_NULL, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
else
sio = open_sio_std (io, QSE_NULL, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_TRUNCATE | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL) return -1;
}
else
{
const qse_char_t* base;
sio = qse_sio_open (
((awk_t*)io)->mmgr,
0,
name,
sio = open_sio (
io, QSE_NULL, this->name,
(io.getMode() == READ?
QSE_SIO_READ:
(QSE_SIO_WRITE|QSE_SIO_CREATE|QSE_SIO_TRUNCATE))
(QSE_SIO_WRITE|QSE_SIO_CREATE|QSE_SIO_TRUNCATE|QSE_SIO_IGNOREMBWCERR))
);
if (sio == QSE_NULL)
{
qse_cstr_t ea;
ea.ptr = name;
ea.len = qse_strlen(name);
((Awk*)io)->setError (QSE_AWK_EOPEN, &ea);
return -1;
}
if (sio == QSE_NULL) return -1;
base = qse_basename (name);
if (base != name)
base = qse_basename (this->name);
if (base != this->name)
{
dir.ptr = name;
dir.len = base - name;
dir.ptr = this->name;
dir.len = base - this->name;
}
}
}
@ -805,7 +822,7 @@ int StdAwk::SourceFile::open (Data& io)
if (totlen >= QSE_COUNTOF(fbuf))
{
dbuf = (qse_char_t*) QSE_MMGR_ALLOC (
((awk_t*)io)->mmgr,
((Awk*)io)->getMmgr(),
QSE_SIZEOF(qse_char_t) * (totlen + 1)
);
if (dbuf == QSE_NULL)
@ -818,29 +835,18 @@ int StdAwk::SourceFile::open (Data& io)
}
else file = fbuf;
tmplen = qse_strncpy (
(char_t*)file, dir.ptr, dir.len);
tmplen = qse_strncpy ((char_t*)file, dir.ptr, dir.len);
qse_strcpy ((char_t*)file + tmplen, ioname);
}
sio = qse_sio_open (
((awk_t*)io)->mmgr,
0,
file,
sio = open_sio (
io, QSE_NULL, file,
(io.getMode() == READ?
QSE_SIO_READ:
(QSE_SIO_WRITE|QSE_SIO_CREATE|QSE_SIO_TRUNCATE))
(QSE_SIO_WRITE|QSE_SIO_CREATE|QSE_SIO_TRUNCATE|QSE_SIO_IGNOREMBWCERR))
);
if (dbuf != QSE_NULL) QSE_MMGR_FREE (((awk_t*)io)->mmgr, dbuf);
if (sio == QSE_NULL)
{
qse_cstr_t ea;
ea.ptr = file;
ea.len = qse_strlen(file);
((Awk*)io)->setError (QSE_AWK_EOPEN, &ea);
return -1;
}
if (dbuf) QSE_MMGR_FREE (((Awk*)io)->getMmgr(), dbuf);
if (sio == QSE_NULL) return -1;
}
io.setHandle (sio);
@ -881,7 +887,7 @@ int StdAwk::SourceString::open (Data& io)
{
// open an included file
sio = qse_sio_open (
((awk_t*)io)->mmgr,
((Awk*)io)->getMmgr(),
0,
ioname,
(io.getMode() == READ?

View File

@ -94,12 +94,9 @@ void qse_assert_failed (
qse_sio_t* sio, siobuf;
sio = &siobuf;
if (qse_sio_initstd (
qse_sio_initstd (
sio, QSE_MMGR_GETDFL(), QSE_SIO_STDERR,
QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR | QSE_SIO_NOAUTOFLUSH) <= -1)
{
sio = QSE_SIO_ERR;
}
QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR | QSE_SIO_NOAUTOFLUSH);
qse_sio_putmbs (sio, QSE_MT("=[ASSERTION FAILURE]============================================================\n"));
@ -155,7 +152,7 @@ void qse_assert_failed (
qse_sio_putmbs (sio, QSE_MT("================================================================================\n"));
qse_sio_flush (sio);
if (sio != QSE_SIO_ERR) qse_sio_fini (sio);
qse_sio_fini (sio);
#if defined(_WIN32)
ExitProcess (249);

View File

@ -31,154 +31,6 @@ static qse_ssize_t __sio_output (qse_tio_cmd_t cmd, void* arg, void* buf, qse_si
# include <os2.h>
#endif
static qse_sio_t __sio_in =
{
QSE_NULL, /* mmgr */
/* fio */
{
QSE_NULL, /* mmgr */
#if defined(_WIN32)
/* this is not a handle. it is adjusted to
* an actual handle in __sio_input () */
(HANDLE)STD_INPUT_HANDLE, /* handle */
#elif defined(__OS2__)
(HFILE)0, /* handle */
#elif defined(__DOS__)
0, /* handle */
#else
0, /* handle */
#endif
0, /* flags */
QSE_NULL /* tio */
},
/* tio */
{
QSE_NULL,
QSE_TIO_ENOERR,
QSE_TIO_IGNOREMBWCERR,
__sio_input,
__sio_output,
&__sio_in,
&__sio_in,
0,
0,
0,
0,
0,
0,
{ 0 },
{ 0 },
{ 0 }
}
};
static qse_sio_t __sio_out =
{
QSE_NULL, /* mmgr */
/* fio */
{
QSE_NULL,
#if defined(_WIN32)
/* this is not a handle. it is adjusted to
* an actual handle in __sio_output () */
(HANDLE)STD_OUTPUT_HANDLE,
#elif defined(__OS2__)
(HFILE)1,
#elif defined(__DOS__)
1,
#else
1,
#endif
0,
QSE_NULL
},
/* tio */
{
QSE_NULL,
QSE_TIO_ENOERR,
QSE_TIO_IGNOREMBWCERR,
__sio_input,
__sio_output,
&__sio_out,
&__sio_out,
0,
0,
0,
0,
0,
0,
{ 0 },
{ 0 },
{ 0 }
}
};
static qse_sio_t __sio_err =
{
QSE_NULL, /* mmgr */
/* fio */
{
QSE_NULL,
#if defined(_WIN32)
/* this is not a handle. it is adjusted to
* an actual handle in __sio_output () */
(HANDLE)STD_ERROR_HANDLE,
#elif defined(__OS2__)
(HFILE)2,
#elif defined(__DOS__)
2,
#else
2,
#endif
0,
QSE_NULL
},
/* tio */
{
QSE_NULL,
QSE_TIO_ENOERR,
QSE_TIO_IGNOREMBWCERR,
__sio_input,
__sio_output,
&__sio_err,
&__sio_err,
0,
0,
0,
0,
0,
0,
{ 0 },
{ 0 },
{ 0 }
}
};
qse_sio_t* qse_sio_in = &__sio_in;
qse_sio_t* qse_sio_out = &__sio_out;
qse_sio_t* qse_sio_err = &__sio_err;
qse_sio_t* qse_sio_open (
qse_mmgr_t* mmgr, qse_size_t xtnsize, const qse_char_t* file, int flags)
{
@ -272,10 +124,6 @@ void qse_sio_fini (qse_sio_t* sio)
qse_sio_flush (sio);
qse_tio_fini (&sio->tio);
qse_fio_fini (&sio->fio);
if (sio == qse_sio_in) qse_sio_in = QSE_NULL;
else if (sio == qse_sio_out) qse_sio_out = QSE_NULL;
else if (sio == qse_sio_err) qse_sio_err = QSE_NULL;
}
qse_sio_errnum_t qse_sio_geterrnum (qse_sio_t* sio)
@ -425,21 +273,6 @@ static qse_ssize_t __sio_input (
if (cmd == QSE_TIO_IO_DATA)
{
#if defined(_WIN32)
/* TODO: I hate this way of adjusting the handle value
* Is there any good ways to do it statically? */
HANDLE h = sio->fio.handle;
if (h == (HANDLE)STD_INPUT_HANDLE ||
h == (HANDLE)STD_OUTPUT_HANDLE ||
h == (HANDLE)STD_ERROR_HANDLE)
{
h = GetStdHandle((DWORD)h);
if (h != INVALID_HANDLE_VALUE && h != NULL)
{
sio->fio.handle = h;
}
}
#endif
return qse_fio_read (&sio->fio, buf, size);
}
@ -455,21 +288,6 @@ static qse_ssize_t __sio_output (
if (cmd == QSE_TIO_IO_DATA)
{
#if defined(_WIN32)
/* TODO: I hate this way of adjusting the handle value
* Is there any good ways to do it statically? */
HANDLE h = sio->fio.handle;
if (h == (HANDLE)STD_INPUT_HANDLE ||
h == (HANDLE)STD_OUTPUT_HANDLE ||
h == (HANDLE)STD_ERROR_HANDLE)
{
h = GetStdHandle((DWORD)h);
if (h != INVALID_HANDLE_VALUE && h != NULL)
{
sio->fio.handle = h;
}
}
#endif
return qse_fio_write (&sio->fio, buf, size);
}

View File

@ -79,15 +79,15 @@ static qse_sio_t* open_sio (qse_cut_t* cut, const qse_char_t* file, int flags)
return sio;
}
static const qse_char_t* sio_std_names[] =
{
QSE_T("stdin"),
QSE_T("stdout"),
QSE_T("stderr"),
};
static qse_sio_t* open_sio_std (qse_cut_t* cut, qse_sio_std_t std, int flags)
{
static const qse_char_t* sio_std_names[] =
{
QSE_T("stdin"),
QSE_T("stdout"),
QSE_T("stderr"),
};
qse_sio_t* sio;
sio = qse_sio_openstd (cut->mmgr, 0, std, flags);
@ -113,9 +113,9 @@ static qse_ssize_t xin (
case QSE_CUT_IO_OPEN:
{
/* main data stream */
sio = (xtn->infile == QSE_NULL)?
open_sio_std (cut, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR):
open_sio (cut, xtn->infile, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
sio = xtn->infile?
open_sio (cut, xtn->infile, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR):
open_sio_std (cut, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL) return -1;
arg->handle = sio;
return 1;
@ -134,7 +134,7 @@ static qse_ssize_t xin (
if (n == -1)
{
if (xtn->infile != QSE_NULL)
if (xtn->infile)
{
qse_cstr_t ea;
ea.ptr = xtn->infile;
@ -162,9 +162,9 @@ static qse_ssize_t xout (
{
case QSE_CUT_IO_OPEN:
{
sio = (xtn->infile == QSE_NULL)?
open_sio_std (cut, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR):
open_sio (cut, xtn->outfile, QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_TRUNCATE | QSE_SIO_IGNOREMBWCERR);
sio = xtn->outfile?
open_sio (cut, xtn->outfile, QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_TRUNCATE | QSE_SIO_IGNOREMBWCERR):
open_sio_std (cut, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL) return -1;
arg->handle = sio;
return 1;
@ -184,11 +184,11 @@ static qse_ssize_t xout (
if (n == -1)
{
if (xtn->infile != QSE_NULL)
if (xtn->outfile)
{
qse_cstr_t ea;
ea.ptr = xtn->infile;
ea.len = qse_strlen (xtn->infile);
ea.ptr = xtn->outfile;
ea.len = qse_strlen (xtn->outfile);
qse_cut_seterrnum (cut, QSE_CUT_EIOFIL, &ea);
}
}