interim commit

This commit is contained in:
hyung-hwan 2009-03-11 04:39:18 +00:00
parent 94ad148625
commit 791565aa2b
8 changed files with 240 additions and 53 deletions

View File

@ -11,15 +11,15 @@
#include <qse/macros.h> #include <qse/macros.h>
#if defined(_WIN32) && !defined(__MINGW32__) #if defined(_WIN32) && !defined(__MINGW32__)
#if defined(QSE_CHAR_IS_MCHAR) # if defined(QSE_CHAR_IS_MCHAR)
#define qse_main main # define qse_main main
typedef qse_mchar_t qse_achar_t; typedef qse_mchar_t qse_achar_t;
#else # else
#define qse_main wmain # define qse_main wmain
typedef qse_wchar_t qse_achar_t; typedef qse_wchar_t qse_achar_t;
#endif # endif
#else #else
#define qse_main main # define qse_main main
typedef qse_mchar_t qse_achar_t; typedef qse_mchar_t qse_achar_t;
#endif #endif
@ -27,7 +27,12 @@
extern "C" { extern "C" {
#endif #endif
int qse_runmain (int argc, qse_achar_t* argv[], int(*mf) (int,qse_char_t*[])); int qse_runmain (
int argc,
qse_achar_t* argv[],
int (*mf)(int,qse_char_t*[])
);
/* TODO - qse_runmain with env, namely, qse_runmaine */ /* TODO - qse_runmain with env, namely, qse_runmaine */
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -39,15 +39,20 @@ enum qse_sed_errnum_t
QSE_SED_EBSEXP /* \ is expected */ QSE_SED_EBSEXP /* \ is expected */
}; };
enum qse_sed_option_t
{
QSE_SED_STRIPLS = (1 << 0) /* strip leading spaces from text*/
};
typedef struct qse_sed_t qse_sed_t; typedef struct qse_sed_t qse_sed_t;
typedef struct qse_sed_c_t qse_sed_c_t; /* command */ typedef struct qse_sed_c_t qse_sed_c_t; /* command */
typedef enum qse_sed_errnum_t qse_sed_errnum_t; typedef enum qse_sed_errnum_t qse_sed_errnum_t;
struct qse_sed_t struct qse_sed_t
{ {
QSE_DEFINE_COMMON_FIELDS (sed) QSE_DEFINE_COMMON_FIELDS (sed)
qse_sed_errnum_t errnum; qse_sed_errnum_t errnum;
int option;
/* source code pointers */ /* source code pointers */
struct struct
@ -119,10 +124,33 @@ void qse_sed_fini (
); );
/******/ /******/
/****f* Text Processor/qse_sed_getoption
int qse_sed_execute ( * NAME
* qse_sed_getoption - get option
* SYNOPSIS
*/
int qse_sed_getoption (
qse_sed_t* sed qse_sed_t* sed
); );
/******/
/****f* Text Processor/qse_sed_setoption
* NAME
* qse_sed_setoption - set option
* SYNOPSIS
*/
void qse_sed_setoption (
qse_sed_t* sed,
int option
);
/*****/
int qse_sed_compile (
qse_sed_t* sed,
const qse_char_t* sptr,
qse_size_t slen
);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,7 +1,19 @@
/* /*
* $Id: chr.h 76 2009-02-22 14:18:06Z hyunghwan.chung $ * $Id: chr.h 97 2009-03-10 10:39:18Z hyunghwan.chung $
* *
* {License} Copyright 2006-2009 Chung, Hyung-Hwan.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ */
#ifndef _QSE_LIB_CMN_CHR_H_ #ifndef _QSE_LIB_CMN_CHR_H_

View File

@ -1,7 +1,19 @@
/* /*
* $Id: mem.h 76 2009-02-22 14:18:06Z hyunghwan.chung $ * $Id: mem.h 97 2009-03-10 10:39:18Z hyunghwan.chung $
* *
* {License} Copyright 2006-2009 Chung, Hyung-Hwan.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ */
#ifndef _QSE_LIB_CMN_MEM_H_ #ifndef _QSE_LIB_CMN_MEM_H_

View File

@ -1,3 +1,21 @@
/*
* $Id: syscall.h 97 2009-03-10 10:39:18Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _QSE_LIB_CMN_SYSCALL_H_ #ifndef _QSE_LIB_CMN_SYSCALL_H_
#define _QSE_LIB_CMN_SYSCALL_H_ #define _QSE_LIB_CMN_SYSCALL_H_
@ -170,4 +188,5 @@
#else #else
# define QSE_cHROOT(path) chroot(path) # define QSE_cHROOT(path) chroot(path)
#endif #endif
#endif #endif

View File

@ -20,6 +20,14 @@
#include "../cmn/mem.h" #include "../cmn/mem.h"
#include <qse/cmn/rex.h> #include <qse/cmn/rex.h>
/* TODO: delete stdio.h */
#include <qse/utl/stdio.h>
/* POSIX http://www.opengroup.org/onlinepubs/009695399/utilities/sed.html
* ALSO READ
http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.bin/sed/POSIX?rev=1.4.6.1;content-type=text%2Fplain
*/
QSE_IMPLEMENT_COMMON_FUNCTIONS (sed) QSE_IMPLEMENT_COMMON_FUNCTIONS (sed)
qse_sed_t* qse_sed_open (qse_mmgr_t* mmgr, qse_size_t xtn) qse_sed_t* qse_sed_open (qse_mmgr_t* mmgr, qse_size_t xtn)
@ -83,13 +91,23 @@ void qse_sed_fini (qse_sed_t* sed)
qse_str_fini (&sed->rexbuf); qse_str_fini (&sed->rexbuf);
} }
void qse_sed_setoption (qse_sed_t* sed, int option)
{
sed->option = option;
}
int qse_sed_getoption (qse_sed_t* sed)
{
return sed->option;
}
/* get the current charanter of the source code */ /* get the current charanter of the source code */
#define CURSC(sed) \ #define CURSC(sed) \
(((sed)->src.cur < (sed)->src.end)? *(sed)->src.cur: QSE_CHAR_EOF) (((sed)->src.cur < (sed)->src.end)? (*(sed)->src.cur): QSE_CHAR_EOF)
/* advance the current pointer of the source code */ /* advance the current pointer of the source code */
#define ADVSCP(sed) ((sed)->src.cur++) #define ADVSCP(sed) ((sed)->src.cur++)
#define NXTSC(sed) \ #define NXTSC(sed) \
(((++(sed)->src.cur) < (sed)->src.end)? *(sed)->src.cur: QSE_CHAR_EOF) (((++(sed)->src.cur) < (sed)->src.end)? (*(sed)->src.cur): QSE_CHAR_EOF)
#define ISSPACE(c) (c == QSE_T(' ') || c == QSE_T('\t')) #define ISSPACE(c) (c == QSE_T(' ') || c == QSE_T('\t'))
@ -196,6 +214,76 @@ static qse_sed_a_t* address (qse_sed_t* sed, qse_sed_a_t* a)
return a; return a;
} }
/* get the text following 'a' and 'i' command.
* POSIX:
* The argument text shall consist of one or more lines. Each embedded
* <newline> in the text shall be preceded by a backslash. Other backslashes
* in text shall be removed, and the following character shall be treated
* literally. */
static qse_str_t* get_text (qse_sed_t* sed, qse_sed_c_t* cmd)
{
qse_cint_t c;
qse_str_t* text = QSE_NULL;
text = qse_str_open (sed->mmgr, 0, 128);
if (text == QSE_NULL) goto oops;
do
{
c = CURSC (sed);
if (sed->option & QSE_SED_STRIPLS)
{
/* get the first non-space character */
while (ISSPACE(c)) c = NXTSC (sed);
}
while (c != QSE_CHAR_EOF)
{
int nl = 0;
if (c == QSE_T('\\'))
{
c = NXTSC (sed);
if (c == QSE_CHAR_EOF) break;
/* TODO: alternate bahavior - add \ to text. */
}
else if (c == QSE_T('\n')) nl = 1;
if (qse_str_ccat (text, c) == (qse_size_t)-1)
{
sed->errnum = QSE_SED_ENOMEM;
goto oops;
}
if (c == QSE_T('\n'))
{
ADVSCP (sed);
if (nl) break;
}
c = NXTSC (sed);
}
}
while (c != QSE_CHAR_EOF);
if (/*(sed->option & QSE_SED_ENSURENL) &&*/ QSE_STR_LEN(text) == 0)
{
if (qse_str_ccat (text, QSE_T('\n')) == (qse_size_t)-1)
{
sed->errnum = QSE_SED_ENOMEM;
goto oops;
}
}
return text;
oops:
if (text != QSE_NULL) qse_str_close (text);
return QSE_NULL;
}
static int command (qse_sed_t* sed) static int command (qse_sed_t* sed)
{ {
qse_cint_t c; qse_cint_t c;
@ -205,6 +293,7 @@ static int command (qse_sed_t* sed)
switch (c) switch (c)
{ {
default: default:
qse_printf (QSE_T("command not recognized [%c]\n"), c);
sed->errnum = QSE_SED_ECMDNR; sed->errnum = QSE_SED_ECMDNR;
return -1; return -1;
@ -244,50 +333,49 @@ static int command (qse_sed_t* sed)
case QSE_SED_CMD_A: case QSE_SED_CMD_A:
case QSE_SED_CMD_I: case QSE_SED_CMD_I:
{
cmd->type = c; cmd->type = c;
/*
if (cmd->a2.type != QSE_SED_A_NONE) if (cmd->a2.type != QSE_SED_A_NONE)
{ {
sed->errnum = QSE_SED_EA2PHB; sed->errnum = QSE_SED_EA2PHB;
return -1; return -1;
} }
*/
/* get the first non-space character */
c = NXTSC (sed); c = NXTSC (sed);
while (ISSPACE(c)) c = NXTSC (sed); while (ISSPACE(c)) c = NXTSC (sed);
if (c == QSE_CHAR_EOF) if (c != QSE_T('\\'))
{ {
sed->errnum = QSE_SED_EBSEXP; sed->errnum = QSE_SED_EBSEXP;
return -1; return -1;
} }
c = NXTSC (sed);
while (ISSPACE(c)) c = NXTSC (sed);
do if (c != QSE_T('\n'))
{ {
if (c == QSE_T('\n')) /* TODO: change error code. garbage after
{ * backslash... */
ADVSCP (sed); sed->errnum = QSE_SED_EBSEXP;
break; return -1;
}
if (c == QSE_T('\\'))
{
c = NXTSC (sed);
if (c == QSE_CHAR_EOF) break;
if (c == QSE_T('\n'))
{
/* TODO: support different line end scheme... */
c = NXTSC (sed);
continue;
}
}
/* TODO: add c to cmd->u.text */
c = NXTSC (sed);
} }
while (c != QSE_CHAR_EOF);
ADVSCP (sed); /* skip the new line */
/* get_text() starts from the next line */
cmd->u.text = get_text (sed, cmd);
if (cmd->u.text == QSE_NULL) return -1;
{
qse_char_t ttt[1000];
fgets (ttt, QSE_COUNTOF(ttt), stdin);
qse_printf (QSE_T("%s%s"), ttt, QSE_STR_PTR(cmd->u.text));
}
break; break;
}
case QSE_T('c'): case QSE_T('c'):
break; break;
@ -429,6 +517,8 @@ static int compile_source (
if (sed->cmd.cur >= sed->cmd.end) if (sed->cmd.cur >= sed->cmd.end)
{ {
/* TODO: too many commands */ /* TODO: too many commands */
sed->errnum = QSE_SED_ENOMEM; /* TODO change it. */
return -1;
} }
cmd = ++sed->cmd.cur; cmd = ++sed->cmd.cur;
@ -436,3 +526,8 @@ static int compile_source (
return 0; return 0;
} }
int qse_sed_compile (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen)
{
return compile_source (sed, sptr, slen);
}

View File

@ -20,6 +20,7 @@
#define _QSE_LIB_UTL_SED_H_ #define _QSE_LIB_UTL_SED_H_
#include <qse/utl/sed.h> #include <qse/utl/sed.h>
#include <qse/cmn/str.h>
typedef qse_int_t qse_sed_line_t; typedef qse_int_t qse_sed_line_t;
@ -50,9 +51,9 @@ struct qse_sed_c_t
union union
{ {
void* rex; /* regular expression */ qse_str_t* text;
qse_char_t* text; /* added text or file name */ void* rex;
qse_sed_c_t* lbl; /* destination command of branch */ qse_sed_c_t* lbl; /* branch destination */
} u; } u;
qse_char_t* rhs; /* right-hand side of sustitution */ qse_char_t* rhs; /* right-hand side of sustitution */

View File

@ -22,12 +22,19 @@
#include <qse/utl/sed.h> #include <qse/utl/sed.h>
#include <qse/utl/stdio.h> #include <qse/utl/stdio.h>
#include <qse/utl/main.h>
int main () 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)
{
qse_fprintf (QSE_STDERR, QSE_T("usage: %s string\n"), argv[0]);
return -1;
}
sed = qse_sed_open (QSE_NULL, 0); sed = qse_sed_open (QSE_NULL, 0);
if (sed == QSE_NULL) if (sed == QSE_NULL)
{ {
@ -35,11 +42,14 @@ int main ()
goto oops; goto oops;
} }
//if (qse_sed_compile (sed, QSE_T("1,20"), 4) == -1) qse_sed_setoption (sed, QSE_SED_STRIPLS);
//{
if (qse_sed_compile (sed, argv[1], qse_strlen(argv[1])) == -1)
{
qse_fprintf (QSE_STDERR, QSE_T("cannot compile - %d\n"), sed->errnum);
//qse_fprintf (QSE_STDERR, QSE_T("cannot compile - %s\n"), qse_sed_geterrstr(sed)); //qse_fprintf (QSE_STDERR, QSE_T("cannot compile - %s\n"), qse_sed_geterrstr(sed));
// goto oops; goto oops;
//} }
//if (qse_sed_execute (sed, io) == -1) //if (qse_sed_execute (sed, io) == -1)
//{ //{
@ -51,4 +61,9 @@ oops:
return ret; return ret;
} }
int qse_main (int argc, char* argv[])
{
return qse_runmain (argc, argv, sed_main);
}
/******/ /******/