fixed more file inclusion bugs

This commit is contained in:
hyung-hwan 2013-05-15 14:20:29 +00:00
parent 639391d887
commit caf3cf8171
7 changed files with 215 additions and 193 deletions

View File

@ -351,11 +351,13 @@ static int xli_main (int argc, qse_char_t* argv[])
if (errloc->line > 0 || errloc->colm > 0)
{
qse_fprintf (QSE_STDERR,
QSE_T("ERROR: cannot read %s - %s at line %lu column %lu\n"),
QSE_T("ERROR: cannot read %s - %s at line %lu column %lu%s%s\n"),
g_input_file,
qse_xli_geterrmsg(xli),
(unsigned long)errloc->line,
(unsigned long)errloc->colm
(unsigned long)errloc->colm,
(errloc->file? QSE_T(" in "): QSE_T("")),
(errloc->file? errloc->file: QSE_T(""))
);
}
else

View File

@ -207,11 +207,23 @@ public:
return this->mode;
}
bool isMaster() const
{
return this->arg->prev == QSE_NULL;
}
const char_t* getName() const
{
return this->arg->name;
}
// since it doesn't copy the contents,
// it should point to something that outlives this object.
void setName (const char_t* name)
{
this->arg->name = name;
}
const char_t* getPrevName() const
{
return this->arg->prev->name;

View File

@ -497,18 +497,13 @@ struct qse_awk_sio_lxc_t
};
typedef struct qse_awk_sio_lxc_t qse_awk_sio_lxc_t;
enum qse_awk_sio_arg_flag_t
{
QSE_AWK_SIO_INCLUDED = (1 << 0)
};
typedef struct qse_awk_sio_arg_t qse_awk_sio_arg_t;
struct qse_awk_sio_arg_t
{
/**
* [IN/OUT] name of I/O object.
* It is #QSE_NULL for the top-level stream. It points to a stream name
* for an included stream.
* for an included stream. This can be changed by an I/O handler.
*/
const qse_char_t* name;

View File

@ -231,9 +231,9 @@ typedef struct qse_xli_io_arg_t qse_xli_io_arg_t;
struct qse_xli_io_arg_t
{
/**
* [IN] name of I/O object.
* [IN/OUT] name of I/O object.
* It is #QSE_NULL for the top-level stream. It points to a stream name
* for an included stream.
* for an included stream. This can be changed by an I/O handler.
*/
const qse_char_t* name;

View File

@ -1500,7 +1500,7 @@ int StdAwk::SourceFile::open (Data& io)
{
qse_sio_t* sio;
if (io.getName() == QSE_NULL)
if (io.isMaster())
{
// open the main source file.
@ -1523,19 +1523,18 @@ int StdAwk::SourceFile::open (Data& io)
io, QSE_NULL, this->name,
(io.getMode() == READ?
(QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR | QSE_SIO_KEEPPATH):
(QSE_SIO_WRITE | QSE_SIO_CREATE |
QSE_SIO_TRUNCATE | QSE_SIO_IGNOREMBWCERR))
(QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_TRUNCATE | QSE_SIO_IGNOREMBWCERR))
);
if (sio == QSE_NULL) return -1;
}
if (this->cmgr) qse_sio_setcmgr (sio, this->cmgr);
io.setName (this->name);
}
else
{
// open an included file
const char_t* ioname, * file, * outer;
const char_t* ioname, * file;
char_t fbuf[64];
char_t* dbuf = QSE_NULL;
@ -1543,36 +1542,41 @@ int StdAwk::SourceFile::open (Data& io)
QSE_ASSERT (ioname != QSE_NULL);
file = ioname;
outer = qse_sio_getpath ((qse_sio_t*)io.getPrevHandle());
if (outer)
if (io.getPrevHandle())
{
const qse_char_t* base;
const char_t* outer;
base = qse_basename (outer);
if (base != outer && ioname[0] != QSE_T('/'))
outer = qse_sio_getpath ((qse_sio_t*)io.getPrevHandle());
if (outer)
{
size_t tmplen, totlen, dirlen;
dirlen = base - outer;
totlen = qse_strlen(ioname) + dirlen;
if (totlen >= QSE_COUNTOF(fbuf))
const qse_char_t* base;
base = qse_basename (outer);
if (base != outer && ioname[0] != QSE_T('/'))
{
dbuf = (qse_char_t*) QSE_MMGR_ALLOC (
((Awk*)io)->getMmgr(),
QSE_SIZEOF(qse_char_t) * (totlen + 1)
);
if (dbuf == QSE_NULL)
size_t tmplen, totlen, dirlen;
dirlen = base - outer;
totlen = qse_strlen(ioname) + dirlen;
if (totlen >= QSE_COUNTOF(fbuf))
{
((Awk*)io)->setError (QSE_AWK_ENOMEM);
return -1;
dbuf = (qse_char_t*) QSE_MMGR_ALLOC (
((Awk*)io)->getMmgr(),
QSE_SIZEOF(qse_char_t) * (totlen + 1)
);
if (dbuf == QSE_NULL)
{
((Awk*)io)->setError (QSE_AWK_ENOMEM);
return -1;
}
file = dbuf;
}
else file = fbuf;
file = dbuf;
tmplen = qse_strncpy ((char_t*)file, outer, dirlen);
qse_strcpy ((char_t*)file + tmplen, ioname);
}
else file = fbuf;
tmplen = qse_strncpy ((char_t*)file, outer, dirlen);
qse_strcpy ((char_t*)file + tmplen, ioname);
}
}
@ -1580,8 +1584,7 @@ int StdAwk::SourceFile::open (Data& io)
io, QSE_NULL, file,
(io.getMode() == READ?
(QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR | QSE_SIO_KEEPPATH):
(QSE_SIO_WRITE | QSE_SIO_CREATE |
QSE_SIO_TRUNCATE | QSE_SIO_IGNOREMBWCERR))
(QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_TRUNCATE | QSE_SIO_IGNOREMBWCERR))
);
if (dbuf) QSE_MMGR_FREE (((Awk*)io)->getMmgr(), dbuf);
if (sio == QSE_NULL) return -1;
@ -1623,25 +1626,63 @@ int StdAwk::SourceString::open (Data& io)
}
else
{
const char_t* ioname = io.getName();
// open an included file
sio = qse_sio_open (
((Awk*)io)->getMmgr(),
0,
ioname,
(io.getMode() == READ?
QSE_SIO_READ:
(QSE_SIO_WRITE|QSE_SIO_CREATE|QSE_SIO_TRUNCATE))
);
if (sio == QSE_NULL)
const char_t* ioname, * file;
char_t fbuf[64];
char_t* dbuf = QSE_NULL;
ioname = io.getName();
QSE_ASSERT (ioname != QSE_NULL);
file = ioname;
if (io.getPrevHandle())
{
qse_cstr_t ea;
ea.ptr = ioname;
ea.len = qse_strlen(ioname);
((Awk*)io)->setError (QSE_AWK_EOPEN, &ea);
return -1;
const qse_char_t* outer;
outer = qse_sio_getpath ((qse_sio_t*)io.getPrevHandle());
if (outer)
{
const qse_char_t* base;
base = qse_basename (outer);
if (base != outer && ioname[0] != QSE_T('/'))
{
size_t tmplen, totlen, dirlen;
dirlen = base - outer;
totlen = qse_strlen(ioname) + dirlen;
if (totlen >= QSE_COUNTOF(fbuf))
{
dbuf = (qse_char_t*) QSE_MMGR_ALLOC (
((Awk*)io)->getMmgr(),
QSE_SIZEOF(qse_char_t) * (totlen + 1)
);
if (dbuf == QSE_NULL)
{
((Awk*)io)->setError (QSE_AWK_ENOMEM);
return -1;
}
file = dbuf;
}
else file = fbuf;
tmplen = qse_strncpy ((char_t*)file, outer, dirlen);
qse_strcpy ((char_t*)file + tmplen, ioname);
}
}
}
sio = open_sio (
io, QSE_NULL, file,
(io.getMode() == READ?
(QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR | QSE_SIO_KEEPPATH):
(QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_TRUNCATE | QSE_SIO_IGNOREMBWCERR))
);
if (dbuf) QSE_MMGR_FREE (((Awk*)io)->getMmgr(), dbuf);
if (sio == QSE_NULL) return -1;
io.setHandle (sio);
}
@ -1650,14 +1691,13 @@ int StdAwk::SourceString::open (Data& io)
int StdAwk::SourceString::close (Data& io)
{
if (io.getName() != QSE_NULL)
qse_sio_close ((qse_sio_t*)io.getHandle());
if (!io.isMaster()) qse_sio_close ((qse_sio_t*)io.getHandle());
return 0;
}
StdAwk::ssize_t StdAwk::SourceString::read (Data& io, char_t* buf, size_t len)
{
if (io.getName() == QSE_NULL)
if (io.isMaster())
{
qse_size_t n = 0;
while (*ptr != QSE_T('\0') && n < len) buf[n++] = *ptr++;
@ -1671,7 +1711,7 @@ StdAwk::ssize_t StdAwk::SourceString::read (Data& io, char_t* buf, size_t len)
StdAwk::ssize_t StdAwk::SourceString::write (Data& io, const char_t* buf, size_t len)
{
if (io.getName() == QSE_NULL)
if (io.isMaster())
{
return -1;
}

View File

@ -622,13 +622,7 @@ static qse_sio_t* open_sio_rtx (qse_awk_rtx_t* rtx, const qse_char_t* file, int
return sio;
}
struct sio_std_name_t
{
const qse_char_t* ptr;
qse_size_t len;
};
static struct sio_std_name_t sio_std_names[] =
static qse_cstr_t sio_std_names[] =
{
{ QSE_T("stdin"), 5 },
{ QSE_T("stdout"), 6 },
@ -639,13 +633,7 @@ static qse_sio_t* open_sio_std (qse_awk_t* awk, qse_sio_std_t std, int flags)
{
qse_sio_t* sio;
sio = qse_sio_openstd (awk->mmgr, 0, std, flags);
if (sio == QSE_NULL)
{
qse_cstr_t ea;
ea.ptr = sio_std_names[std].ptr;
ea.len = sio_std_names[std].len;
qse_awk_seterrnum (awk, QSE_AWK_EOPEN, &ea);
}
if (sio == QSE_NULL) qse_awk_seterrnum (awk, QSE_AWK_EOPEN, &sio_std_names[std]);
return sio;
}
@ -654,13 +642,7 @@ static qse_sio_t* open_sio_std_rtx (qse_awk_rtx_t* rtx, qse_sio_std_t std, int f
qse_sio_t* sio;
sio = qse_sio_openstd (rtx->awk->mmgr, 0, std, flags);
if (sio == QSE_NULL)
{
qse_cstr_t ea;
ea.ptr = sio_std_names[std].ptr;
ea.len = sio_std_names[std].len;
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EOPEN, &ea);
}
if (sio == QSE_NULL) qse_awk_rtx_seterrnum (rtx, QSE_AWK_EOPEN, &sio_std_names[std]);
return sio;
}
@ -721,23 +703,26 @@ static int open_parsestd (qse_awk_t* awk, qse_awk_sio_arg_t* arg, xtn_t* xtn, qs
static qse_ssize_t sf_in_open (
qse_awk_t* awk, qse_awk_sio_arg_t* arg, xtn_t* xtn)
{
if (arg->name == QSE_NULL)
if (arg->prev == QSE_NULL)
{
/* handle normal source input streams specified
* to qse_awk_parsestd() */
qse_ssize_t x;
QSE_ASSERT (arg == &awk->sio.arg);
x = open_parsestd (awk, arg, xtn, 0);
if (x >= 0)
{
xtn->s.in.xindex = 0; /* update the current stream index */
#if 0
/* perform some manipulation about the top-level input information */
if (xtn->s.in.x[0].type == QSE_AWK_PARSESTD_FILE)
awk->sio.arg.name = xtn->s.in.x[0].u.file.path;
else
awk->sio.arg.name = QSE_NULL;
#endif
{
arg->name = xtn->s.in.x[0].u.file.path;
if (arg->name == QSE_NULL) arg->name = sio_std_names[QSE_SIO_STDIN].ptr;
}
else arg->name = QSE_NULL;
}
return x;
@ -745,38 +730,43 @@ static qse_ssize_t sf_in_open (
else
{
/* handle the included source file - @include */
const qse_char_t* path, * outer;
const qse_char_t* path;
qse_char_t fbuf[64];
qse_char_t* dbuf = QSE_NULL;
QSE_ASSERT (arg->name != QSE_NULL);
path = arg->name;
outer = qse_sio_getpath (arg->prev->handle);
if (outer)
if (arg->prev->handle)
{
const qse_char_t* base;
const qse_char_t* outer;
base = qse_basename (outer);
if (base != outer && arg->name[0] != QSE_T('/'))
outer = qse_sio_getpath (arg->prev->handle);
if (outer)
{
qse_size_t tmplen, totlen, dirlen;
dirlen = base - outer;
totlen = qse_strlen(arg->name) + dirlen;
if (totlen >= QSE_COUNTOF(fbuf))
{
dbuf = qse_awk_allocmem (
awk, QSE_SIZEOF(qse_char_t) * (totlen + 1)
);
if (dbuf == QSE_NULL) return -1;
path = dbuf;
}
else path = fbuf;
const qse_char_t* base;
tmplen = qse_strncpy ((qse_char_t*)path, outer, dirlen);
qse_strcpy ((qse_char_t*)path + tmplen, arg->name);
base = qse_basename (outer);
if (base != outer && arg->name[0] != QSE_T('/'))
{
qse_size_t tmplen, totlen, dirlen;
dirlen = base - outer;
totlen = qse_strlen(arg->name) + dirlen;
if (totlen >= QSE_COUNTOF(fbuf))
{
dbuf = qse_awk_allocmem (
awk, QSE_SIZEOF(qse_char_t) * (totlen + 1)
);
if (dbuf == QSE_NULL) return -1;
path = dbuf;
}
else path = fbuf;
tmplen = qse_strncpy ((qse_char_t*)path, outer, dirlen);
qse_strcpy ((qse_char_t*)path + tmplen, arg->name);
}
}
}
@ -801,7 +791,7 @@ static qse_ssize_t sf_in_open (
static qse_ssize_t sf_in_close (
qse_awk_t* awk, qse_awk_sio_arg_t* arg, xtn_t* xtn)
{
if (arg->name == QSE_NULL)
if (arg->prev == QSE_NULL)
{
switch (xtn->s.in.x[xtn->s.in.xindex].type)
{
@ -833,10 +823,12 @@ static qse_ssize_t sf_in_read (
qse_awk_t* awk, qse_awk_sio_arg_t* arg,
qse_char_t* data, qse_size_t size, xtn_t* xtn)
{
if (arg->name == QSE_NULL)
if (arg->prev == QSE_NULL)
{
qse_ssize_t n;
QSE_ASSERT (arg == &awk->sio.arg);
again:
switch (xtn->s.in.x[xtn->s.in.xindex].type)
{
@ -846,16 +838,9 @@ static qse_ssize_t sf_in_read (
if (n <= -1)
{
qse_cstr_t ea;
if (xtn->s.in.x[xtn->s.in.xindex].u.file.path)
{
ea.ptr = xtn->s.in.x[xtn->s.in.xindex].u.file.path;
ea.len = qse_strlen(ea.ptr);
}
else
{
ea.ptr = sio_std_names[QSE_SIO_STDIN].ptr;
ea.len = sio_std_names[QSE_SIO_STDIN].len;
}
ea.ptr = xtn->s.in.x[xtn->s.in.xindex].u.file.path;
if (ea.ptr == QSE_NULL) ea.ptr = sio_std_names[QSE_SIO_STDIN].ptr;
ea.len = qse_strlen(ea.ptr);
qse_awk_seterrnum (awk, QSE_AWK_EREAD, &ea);
}
break;
@ -885,8 +870,19 @@ static qse_ssize_t sf_in_read (
if (open_parsestd (awk, arg, xtn, next) <= -1) n = -1;
else
{
/* if successful, close the current stream */
xtn->s.in.xindex = next; /* update the next to the current */
/* update the I/O object name */
if (xtn->s.in.x[next].type == QSE_AWK_PARSESTD_FILE)
{
arg->name = xtn->s.in.x[next].u.file.path;
if (arg->name == QSE_NULL) arg->name = sio_std_names[QSE_SIO_STDIN].ptr;
}
else
arg->name = QSE_NULL;
arg->line = 0; /* reset the line number */
arg->colm = 0;
goto again;
}
}
@ -1020,16 +1016,9 @@ static qse_ssize_t sf_out (
if (n <= -1)
{
qse_cstr_t ea;
if (xtn->s.out.x->u.file.path)
{
ea.ptr = xtn->s.out.x->u.file.path;
ea.len = qse_strlen(ea.ptr);
}
else
{
ea.ptr = sio_std_names[QSE_SIO_STDOUT].ptr;
ea.len = sio_std_names[QSE_SIO_STDOUT].len;
}
ea.ptr = xtn->s.out.x->u.file.path;
if (ea.ptr == QSE_NULL) ea.ptr = sio_std_names[QSE_SIO_STDOUT].ptr;
ea.len = qse_strlen(ea.ptr);
qse_awk_seterrnum (awk, QSE_AWK_EWRITE, &ea);
}

View File

@ -122,13 +122,7 @@ static qse_sio_t* open_sio (qse_xli_t* xli, const qse_char_t* file, int flags)
return sio;
}
struct sio_std_name_t
{
const qse_char_t* ptr;
qse_size_t len;
};
static struct sio_std_name_t sio_std_names[] =
static qse_cstr_t sio_std_names[] =
{
{ QSE_T("stdin"), 5 },
{ QSE_T("stdout"), 6 },
@ -139,22 +133,18 @@ static qse_sio_t* open_sio_std (qse_xli_t* xli, qse_sio_std_t std, int flags)
{
qse_sio_t* sio;
sio = qse_sio_openstd (xli->mmgr, 0, std, flags);
if (sio == QSE_NULL)
{
qse_cstr_t ea;
ea.ptr = sio_std_names[std].ptr;
ea.len = sio_std_names[std].len;
qse_xli_seterrnum (xli, QSE_XLI_EIOFIL, &ea);
}
if (sio == QSE_NULL) qse_xli_seterrnum (xli, QSE_XLI_EIOFIL, &sio_std_names[std]);
return sio;
}
static qse_ssize_t sf_in_open (qse_xli_t* xli, qse_xli_io_arg_t* arg, xtn_t* xtn)
{
if (arg->name == QSE_NULL)
if (arg->prev == QSE_NULL)
{
qse_xli_iostd_t* psin = xtn->s.in.x;
QSE_ASSERT (arg == &xli->sio.arg);
switch (psin->type)
{
/* normal source files */
@ -172,6 +162,11 @@ static qse_ssize_t sf_in_open (qse_xli_t* xli, qse_xli_io_arg_t* arg, xtn_t* xtn
}
if (arg->handle == QSE_NULL) return -1;
if (psin->u.file.cmgr) qse_sio_setcmgr (arg->handle, psin->u.file.cmgr);
/* update the object name to something more specific */
arg->name = psin->u.file.path;
if (arg->name == QSE_NULL) arg->name = sio_std_names[QSE_SIO_STDIN].ptr;
return 0;
case QSE_XLI_IOSTD_STR:
@ -186,43 +181,47 @@ static qse_ssize_t sf_in_open (qse_xli_t* xli, qse_xli_io_arg_t* arg, xtn_t* xtn
}
else
{
QSE_ASSERT (arg->prev != QSE_NULL);
/* handle the included file - @include */
const qse_char_t* path, * outer;
const qse_char_t* path;
qse_char_t fbuf[64];
qse_char_t* dbuf = QSE_NULL;
QSE_ASSERT (arg->name != QSE_NULL);
path = arg->name;
outer = qse_sio_getpath (arg->prev->handle);
if (outer)
if (arg->prev->handle)
{
const qse_char_t* base;
const qse_char_t* outer;
/* i'm being included from another file */
base = qse_basename (outer);
if (base != outer && arg->name[0] != QSE_T('/'))
outer = qse_sio_getpath (arg->prev->handle);
if (outer)
{
qse_size_t tmplen, totlen, dirlen;
dirlen = base - outer;
totlen = qse_strlen(arg->name) + dirlen;
if (totlen >= QSE_COUNTOF(fbuf))
const qse_char_t* base;
/* i'm being included from another file */
base = qse_basename (outer);
if (base != outer && arg->name[0] != QSE_T('/'))
{
dbuf = qse_xli_allocmem (
xli, QSE_SIZEOF(qse_char_t) * (totlen + 1)
);
if (dbuf == QSE_NULL) return -1;
qse_size_t tmplen, totlen, dirlen;
path = dbuf;
dirlen = base - outer;
totlen = qse_strlen(arg->name) + dirlen;
if (totlen >= QSE_COUNTOF(fbuf))
{
dbuf = qse_xli_allocmem (
xli, QSE_SIZEOF(qse_char_t) * (totlen + 1)
);
if (dbuf == QSE_NULL) return -1;
path = dbuf;
}
else path = fbuf;
tmplen = qse_strncpy ((qse_char_t*)path, outer, dirlen);
qse_strcpy ((qse_char_t*)path + tmplen, arg->name);
}
else path = fbuf;
tmplen = qse_strncpy ((qse_char_t*)path, outer, dirlen);
qse_strcpy ((qse_char_t*)path + tmplen, arg->name);
}
}
@ -248,7 +247,7 @@ static qse_ssize_t sf_in_open (qse_xli_t* xli, qse_xli_io_arg_t* arg, xtn_t* xtn
static qse_ssize_t sf_in_close (
qse_xli_t* xli, qse_xli_io_arg_t* arg, xtn_t* xtn)
{
if (arg->name == QSE_NULL)
if (arg->prev == QSE_NULL)
{
switch (xtn->s.in.x->type)
{
@ -280,7 +279,7 @@ static qse_ssize_t sf_in_read (
qse_xli_t* xli, qse_xli_io_arg_t* arg,
qse_char_t* data, qse_size_t size, xtn_t* xtn)
{
if (arg->name == QSE_NULL)
if (arg->prev == QSE_NULL)
{
qse_ssize_t n;
@ -292,16 +291,9 @@ static qse_ssize_t sf_in_read (
if (n <= -1)
{
qse_cstr_t ea;
if (xtn->s.in.x->u.file.path)
{
ea.ptr = xtn->s.in.x->u.file.path;
ea.len = qse_strlen(ea.ptr);
}
else
{
ea.ptr = sio_std_names[QSE_SIO_STDIN].ptr;
ea.len = sio_std_names[QSE_SIO_STDIN].len;
}
ea.ptr = xtn->s.in.x->u.file.path;
if (ea.ptr == QSE_NULL) ea.ptr = sio_std_names[QSE_SIO_STDIN].ptr;
ea.len = qse_strlen(ea.ptr);
qse_xli_seterrnum (xli, QSE_XLI_EIOFIL, &ea);
}
break;
@ -384,7 +376,6 @@ static qse_ssize_t sf_out (
if (xtn->s.out.x->u.file.path == QSE_NULL ||
(xtn->s.out.x->u.file.path[0] == QSE_T('-') &&
xtn->s.out.x->u.file.path[1] == QSE_T('\0')))
{
/* no path name or - -> stdout */
xtn->s.out.u.file.sio = open_sio_std (
@ -451,16 +442,9 @@ static qse_ssize_t sf_out (
if (n <= -1)
{
qse_cstr_t ea;
if (xtn->s.out.x->u.file.path)
{
ea.ptr = xtn->s.out.x->u.file.path;
ea.len = qse_strlen(ea.ptr);
}
else
{
ea.ptr = sio_std_names[QSE_SIO_STDOUT].ptr;
ea.len = sio_std_names[QSE_SIO_STDOUT].len;
}
ea.ptr = xtn->s.out.x->u.file.path;
if (ea.ptr == QSE_NULL) ea.ptr = sio_std_names[QSE_SIO_STDOUT].ptr;
ea.len = qse_strlen(ea.ptr);
qse_xli_seterrnum (xli, QSE_XLI_EIOFIL, &ea);
}