fixed quite a few bugs and enhanced error handling in sed
This commit is contained in:
parent
78146a8f91
commit
2a4dd14b2a
@ -87,6 +87,7 @@ enum qse_sed_errnum_t
|
|||||||
QSE_SED_EOCSDU, /**< multiple occurrence specifiers */
|
QSE_SED_EOCSDU, /**< multiple occurrence specifiers */
|
||||||
QSE_SED_EOCSZE, /**< occurrence specifier to s is zero */
|
QSE_SED_EOCSZE, /**< occurrence specifier to s is zero */
|
||||||
QSE_SED_EOCSTL, /**< occurrence specifier too large */
|
QSE_SED_EOCSTL, /**< occurrence specifier too large */
|
||||||
|
QSE_SED_EIOFIL, /**< file io error */
|
||||||
QSE_SED_EIOUSR /**< user io error */
|
QSE_SED_EIOUSR /**< user io error */
|
||||||
};
|
};
|
||||||
typedef enum qse_sed_errnum_t qse_sed_errnum_t;
|
typedef enum qse_sed_errnum_t qse_sed_errnum_t;
|
||||||
@ -271,8 +272,8 @@ int qse_sed_comp (
|
|||||||
*/
|
*/
|
||||||
int qse_sed_exec (
|
int qse_sed_exec (
|
||||||
qse_sed_t* sed, /**< a stream editor */
|
qse_sed_t* sed, /**< a stream editor */
|
||||||
qse_sed_io_fun_t in, /**< stream reader */
|
qse_sed_io_fun_t inf, /**< stream reader */
|
||||||
qse_sed_io_fun_t out /**< stream writer */
|
qse_sed_io_fun_t outf /**< stream writer */
|
||||||
);
|
);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
@ -26,7 +26,7 @@ static const qse_char_t* geterrstr (int errnum)
|
|||||||
QSE_T("out of memory"),
|
QSE_T("out of memory"),
|
||||||
QSE_T("command '${0}' not recognized"),
|
QSE_T("command '${0}' not recognized"),
|
||||||
QSE_T("command code missing"),
|
QSE_T("command code missing"),
|
||||||
QSE_T("command '${0}' not terminated properly"),
|
QSE_T("command '${0}' incomplete"),
|
||||||
QSE_T("regular expression '${0}' incomplete"),
|
QSE_T("regular expression '${0}' incomplete"),
|
||||||
QSE_T("failed to compile regular expression '${0}'"),
|
QSE_T("failed to compile regular expression '${0}'"),
|
||||||
QSE_T("failed to match regular expression"),
|
QSE_T("failed to match regular expression"),
|
||||||
@ -49,6 +49,7 @@ static const qse_char_t* geterrstr (int errnum)
|
|||||||
QSE_T("multiple occurrence specifier"),
|
QSE_T("multiple occurrence specifier"),
|
||||||
QSE_T("occurrence specifier is zero"),
|
QSE_T("occurrence specifier is zero"),
|
||||||
QSE_T("occurrence specifier too large"),
|
QSE_T("occurrence specifier too large"),
|
||||||
|
QSE_T("io error with file '${0}'"),
|
||||||
QSE_T("error returned by user io handler")
|
QSE_T("error returned by user io handler")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -49,8 +49,24 @@ struct qse_sed_t
|
|||||||
const qse_char_t* cur; /**< current source text pointer */
|
const qse_char_t* cur; /**< current source text pointer */
|
||||||
} src;
|
} src;
|
||||||
|
|
||||||
/** temporary regular expression buffer */
|
/** temporary data for compiling */
|
||||||
qse_str_t rexbuf;
|
struct
|
||||||
|
{
|
||||||
|
qse_str_t rex; /**< regular expression buffer */
|
||||||
|
qse_str_t lab; /**< label name buffer */
|
||||||
|
|
||||||
|
/** data structure to compile command groups */
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
/** current level of command group nesting */
|
||||||
|
int level;
|
||||||
|
/** keeps track of the begining of nested groups */
|
||||||
|
qse_sed_cmd_t* cmd[128];
|
||||||
|
} grp;
|
||||||
|
|
||||||
|
/** a table storing labels seen */
|
||||||
|
qse_map_t labs;
|
||||||
|
} tmp;
|
||||||
|
|
||||||
/** compiled commands */
|
/** compiled commands */
|
||||||
struct
|
struct
|
||||||
@ -60,18 +76,6 @@ struct qse_sed_t
|
|||||||
qse_sed_cmd_t* cur; /**< points next to the last command */
|
qse_sed_cmd_t* cur; /**< points next to the last command */
|
||||||
} cmd;
|
} cmd;
|
||||||
|
|
||||||
/** a table storing labels seen */
|
|
||||||
qse_map_t labs;
|
|
||||||
|
|
||||||
/** data structure to compile command groups */
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
/** current level of command group nesting */
|
|
||||||
int level;
|
|
||||||
/** keeps track of the begining of nested command groups */
|
|
||||||
qse_sed_cmd_t* cmd[128];
|
|
||||||
} grp;
|
|
||||||
|
|
||||||
/** data for execution */
|
/** data for execution */
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
@ -174,6 +178,7 @@ struct qse_sed_adr_t
|
|||||||
struct qse_sed_cmd_t
|
struct qse_sed_cmd_t
|
||||||
{
|
{
|
||||||
qse_char_t type;
|
qse_char_t type;
|
||||||
|
qse_size_t lnum;
|
||||||
|
|
||||||
int negated;
|
int negated;
|
||||||
|
|
||||||
|
@ -22,6 +22,8 @@
|
|||||||
#include <qse/cmn/str.h>
|
#include <qse/cmn/str.h>
|
||||||
#include <qse/cmn/chr.h>
|
#include <qse/cmn/chr.h>
|
||||||
|
|
||||||
|
const qse_char_t* instream = QSE_NULL;
|
||||||
|
|
||||||
static qse_ssize_t in (
|
static qse_ssize_t in (
|
||||||
qse_sed_t* sed, qse_sed_io_cmd_t cmd, qse_sed_io_arg_t* arg)
|
qse_sed_t* sed, qse_sed_io_cmd_t cmd, qse_sed_io_arg_t* arg)
|
||||||
{
|
{
|
||||||
@ -31,7 +33,19 @@ static qse_ssize_t in (
|
|||||||
if (arg->open.path == QSE_NULL ||
|
if (arg->open.path == QSE_NULL ||
|
||||||
arg->open.path[0] == QSE_T('\0'))
|
arg->open.path[0] == QSE_T('\0'))
|
||||||
{
|
{
|
||||||
arg->open.handle = QSE_STDIN;
|
if (instream)
|
||||||
|
{
|
||||||
|
arg->open.handle = qse_fopen (instream, QSE_T("r"));
|
||||||
|
if (arg->open.handle == QSE_NULL)
|
||||||
|
{
|
||||||
|
qse_cstr_t errarg;
|
||||||
|
errarg.ptr = instream;
|
||||||
|
errarg.len = qse_strlen(instream);
|
||||||
|
qse_sed_seterror (sed, QSE_SED_EIOFIL, 0, &errarg);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else arg->open.handle = QSE_STDIN;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -102,10 +116,12 @@ int sed_main (int argc, qse_char_t* argv[])
|
|||||||
|
|
||||||
if (argc != 2 && argc != 3)
|
if (argc != 2 && argc != 3)
|
||||||
{
|
{
|
||||||
qse_fprintf (QSE_STDERR, QSE_T("usage: %s string\n"), argv[0]);
|
qse_fprintf (QSE_STDERR, QSE_T("usage: %s string [file]\n"), argv[0]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (argc == 3) instream = argv[2];
|
||||||
|
|
||||||
sed = qse_sed_open (QSE_NULL, 0);
|
sed = qse_sed_open (QSE_NULL, 0);
|
||||||
if (sed == QSE_NULL)
|
if (sed == QSE_NULL)
|
||||||
{
|
{
|
||||||
@ -116,21 +132,44 @@ int sed_main (int argc, qse_char_t* argv[])
|
|||||||
if (argc == 3) qse_sed_setoption (sed, qse_strtoi(argv[2]));
|
if (argc == 3) qse_sed_setoption (sed, qse_strtoi(argv[2]));
|
||||||
|
|
||||||
if (qse_sed_comp (sed, argv[1], qse_strlen(argv[1])) == -1)
|
if (qse_sed_comp (sed, argv[1], qse_strlen(argv[1])) == -1)
|
||||||
|
{
|
||||||
|
qse_size_t errlin = qse_sed_geterrlin(sed);
|
||||||
|
if (errlin > 0)
|
||||||
{
|
{
|
||||||
qse_fprintf (QSE_STDERR,
|
qse_fprintf (QSE_STDERR,
|
||||||
QSE_T("cannot compile - %s at line %lu\n"),
|
QSE_T("cannot compile - %s at line %lu\n"),
|
||||||
qse_sed_geterrmsg(sed),
|
qse_sed_geterrmsg(sed),
|
||||||
(unsigned long)qse_sed_geterrlin(sed)
|
(unsigned long)errlin
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qse_fprintf (QSE_STDERR,
|
||||||
|
QSE_T("cannot compile - %s\n"),
|
||||||
|
qse_sed_geterrmsg(sed)
|
||||||
|
);
|
||||||
|
}
|
||||||
goto oops;
|
goto oops;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (qse_sed_exec (sed, in, out) == -1)
|
if (qse_sed_exec (sed, in, out) == -1)
|
||||||
|
{
|
||||||
|
qse_size_t errlin = qse_sed_geterrlin(sed);
|
||||||
|
if (errlin > 0)
|
||||||
|
{
|
||||||
|
qse_fprintf (QSE_STDERR,
|
||||||
|
QSE_T("cannot execute - %s at line %lu\n"),
|
||||||
|
qse_sed_geterrmsg(sed),
|
||||||
|
(unsigned long)errlin
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
qse_fprintf (QSE_STDERR,
|
qse_fprintf (QSE_STDERR,
|
||||||
QSE_T("cannot execute - %s\n"),
|
QSE_T("cannot execute - %s\n"),
|
||||||
qse_sed_geterrmsg(sed)
|
qse_sed_geterrmsg(sed)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
goto oops;
|
goto oops;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user