fixed a minor problem in the Awk class and enhanced a test program for sed

This commit is contained in:
hyung-hwan 2009-06-08 00:23:53 +00:00
parent df2c125673
commit a6c0d71710
3 changed files with 73 additions and 37 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: opt.h 75 2009-02-22 14:10:34Z hyunghwan.chung $ * $Id: opt.h 189 2009-06-07 06:23:53Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -25,23 +25,12 @@
typedef struct qse_opt_t qse_opt_t; typedef struct qse_opt_t qse_opt_t;
typedef struct qse_opt_lng_t qse_opt_lng_t; typedef struct qse_opt_lng_t qse_opt_lng_t;
/****t* Common/qse_opt_lng_t
* NAME
* qse_opt_lng_t - define a long option
* SYNOPSIS
*/
struct qse_opt_lng_t struct qse_opt_lng_t
{ {
const qse_char_t* str; const qse_char_t* str;
qse_cint_t val; qse_cint_t val;
}; };
/*****/
/****t* Common/qse_opt_t
* NAME
* qse_opt_t - define a command line option table
* SYNOPSIS
*/
struct qse_opt_t struct qse_opt_t
{ {
/* input */ /* input */
@ -61,26 +50,20 @@ struct qse_opt_t
/* input + output - internal*/ /* input + output - internal*/
qse_char_t* cur; qse_char_t* cur;
}; };
/******/
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/****f* Common/qse_getopt /**
* NAME * The qse_getopt() function returns QSE_CHAR_EOF when it finishes processing
* qse_getopt - process command line options * command line options. The return values of QSE_T('?') indicates an error.
* DESCRIPTION
* The qse_getopt() function returns QSE_CHAR_EOF when it finishes processing
* command line options. The return values of QSE_T('?') indicates an error.
* SYNOPSIS
*/ */
qse_cint_t qse_getopt ( qse_cint_t qse_getopt (
int argc /* argument count */, int argc /* argument count */,
qse_char_t* const* argv /* argument array */, qse_char_t* const* argv /* argument array */,
qse_opt_t* opt /* option configuration */ qse_opt_t* opt /* option configuration */
); );
/******/
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Id: Awk.cpp 182 2009-06-03 21:50:32Z hyunghwan.chung $ * $Id: Awk.cpp 189 2009-06-07 06:23:53Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -81,7 +81,7 @@ void Awk::RIO::setHandle (void* handle)
this->riod->handle = handle; this->riod->handle = handle;
} }
Awk::RIO::operator Awk::Awk* () const Awk::RIO::operator Awk* () const
{ {
rxtn_t* rxtn = (rxtn_t*) QSE_XTN (this->rtx); rxtn_t* rxtn = (rxtn_t*) QSE_XTN (this->rtx);
return rxtn->run->awk; return rxtn->run->awk;

View File

@ -21,8 +21,11 @@
#include <qse/cmn/main.h> #include <qse/cmn/main.h>
#include <qse/cmn/str.h> #include <qse/cmn/str.h>
#include <qse/cmn/chr.h> #include <qse/cmn/chr.h>
#include <qse/cmn/opt.h>
const qse_char_t* instream = QSE_NULL; static const qse_char_t* g_script = QSE_NULL;
static const qse_char_t* g_infile = QSE_NULL;
static int g_option = 0;
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)
@ -33,14 +36,14 @@ static qse_ssize_t in (
if (arg->path == QSE_NULL || if (arg->path == QSE_NULL ||
arg->path[0] == QSE_T('\0')) arg->path[0] == QSE_T('\0'))
{ {
if (instream) if (g_infile)
{ {
arg->handle = qse_fopen (instream, QSE_T("r")); arg->handle = qse_fopen (g_infile, QSE_T("r"));
if (arg->handle == QSE_NULL) if (arg->handle == QSE_NULL)
{ {
qse_cstr_t errarg; qse_cstr_t errarg;
errarg.ptr = instream; errarg.ptr = g_infile;
errarg.len = qse_strlen(instream); errarg.len = qse_strlen(g_infile);
qse_sed_seterror (sed, QSE_SED_EIOFIL, 0, &errarg); qse_sed_seterror (sed, QSE_SED_EIOFIL, 0, &errarg);
return -1; return -1;
} }
@ -109,18 +112,69 @@ static qse_ssize_t out (
} }
} }
static void print_usage (QSE_FILE* out, int argc, qse_char_t* argv[])
{
qse_fprintf (out, QSE_T("%s [options] script [file]\n"), argv[0]);
qse_fprintf (out, QSE_T("options as follows:\n"));
qse_fprintf (out, QSE_T(" -h show this message\n"));
qse_fprintf (out, QSE_T(" -n disable auto-print\n"));
qse_fprintf (out, QSE_T(" -c enable the classic mode\n"));
}
static int handle_args (int argc, qse_char_t* argv[])
{
static qse_opt_t opt =
{
QSE_T("hnc"),
QSE_NULL
};
qse_cint_t c;
while ((c = qse_getopt (argc, argv, &opt)) != QSE_CHAR_EOF)
{
switch (c)
{
default:
print_usage (QSE_STDERR, argc, argv);
return -1;
case QSE_T('h'):
print_usage (QSE_STDOUT, argc, argv);
return 0;
case QSE_T('n'):
g_option |= QSE_SED_QUIET;
break;
case QSE_T('c'):
g_option |= QSE_SED_CLASSIC;
break;
}
}
if (opt.ind < argc) g_script = argv[opt.ind++];
if (opt.ind < argc) g_infile = argv[opt.ind++];
if (g_script == QSE_NULL)
{
print_usage (QSE_STDERR, argc, argv);
return -1;
}
return 1;
}
int sed_main (int argc, qse_char_t* argv[]) int sed_main (int argc, qse_char_t* argv[])
{ {
qse_sed_t* sed = QSE_NULL; qse_sed_t* sed = QSE_NULL;
int ret = -1; int ret = -1;
if (argc != 2 && argc != 3) ret = handle_args (argc, argv);
{ if (ret <= -1) return -1;
qse_fprintf (QSE_STDERR, QSE_T("usage: %s string [file]\n"), argv[0]); if (ret == 0) return 0;
return -1;
}
if (argc == 3) instream = argv[2]; ret = -1;
sed = qse_sed_open (QSE_NULL, 0); sed = qse_sed_open (QSE_NULL, 0);
if (sed == QSE_NULL) if (sed == QSE_NULL)
@ -129,10 +183,9 @@ int sed_main (int argc, qse_char_t* argv[])
goto oops; goto oops;
} }
//if (argc == 3) qse_sed_setoption (sed, qse_strtoi(argv[2])); qse_sed_setoption (sed, g_option);
qse_sed_setoption (sed, QSE_SED_QUIET);
if (qse_sed_comp (sed, argv[1], qse_strlen(argv[1])) == -1) if (qse_sed_comp (sed, g_script, qse_strlen(g_script)) == -1)
{ {
qse_size_t errlin = qse_sed_geterrlin(sed); qse_size_t errlin = qse_sed_geterrlin(sed);
if (errlin > 0) if (errlin > 0)