touched up sed

This commit is contained in:
hyung-hwan 2009-09-08 07:34:49 +00:00
parent f9d4ccc50a
commit faf8d996db
11 changed files with 189 additions and 72 deletions

View File

@ -18,12 +18,12 @@
#include <qse/sed/sed.h>
#include <qse/cmn/str.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/chr.h>
#include <qse/cmn/opt.h>
#include <qse/cmn/misc.h>
#include <qse/cmn/stdio.h>
#include <qse/cmn/main.h>
#include <stdlib.h>
static const qse_char_t* g_script_file = QSE_NULL;
static qse_char_t* g_script = QSE_NULL;
@ -31,7 +31,7 @@ static const qse_char_t* g_infile = QSE_NULL;
static int g_option = 0;
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, qse_char_t* buf, qse_size_t size)
{
switch (cmd)
{
@ -68,9 +68,10 @@ static qse_ssize_t in (
case QSE_SED_IO_READ:
{
qse_cint_t c;
/* TODO: read more characters */
c = qse_fgetc (arg->handle);
if (c == QSE_CHAR_EOF) return 0;
arg->u.r.buf[0] = c;
buf[0] = c;
return 1;
}
@ -80,7 +81,7 @@ static qse_ssize_t in (
}
static qse_ssize_t out (
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, qse_char_t* data, qse_size_t len)
{
switch (cmd)
{
@ -105,9 +106,9 @@ static qse_ssize_t out (
case QSE_SED_IO_WRITE:
{
qse_size_t i = 0;
for (i = 0; i < arg->u.w.len; i++)
qse_fputc (arg->u.w.data[i], arg->handle);
return arg->u.w.len;
for (i = 0; i < len; i++)
qse_fputc (data[i], arg->handle);
return len;
}
default:
@ -211,6 +212,40 @@ static int handle_args (int argc, qse_char_t* argv[])
return 1;
}
qse_char_t* load_script_file (const qse_char_t* file)
{
qse_cint_t c;
qse_str_t script;
QSE_FILE* fp;
qse_xstr_t xstr;
fp = qse_fopen (file, QSE_T("r"));
if (fp == QSE_NULL) return QSE_NULL;
if (qse_str_init (&script, QSE_MMGR_GETDFL(), 1024) == QSE_NULL)
{
qse_fclose (fp);
return QSE_NULL;
}
while ((c = qse_fgetc (fp)) != QSE_CHAR_EOF)
{
if (qse_str_ccat (&script, c) == (qse_size_t)-1)
{
qse_fclose (fp);
qse_str_fini (&script);
return QSE_NULL;
}
}
qse_fclose (fp);
qse_str_yield (&script, &xstr, 0);
qse_str_fini (&script);
return xstr.ptr;
}
int sed_main (int argc, qse_char_t* argv[])
{
qse_sed_t* sed = QSE_NULL;
@ -234,8 +269,13 @@ int sed_main (int argc, qse_char_t* argv[])
if (g_script_file != QSE_NULL)
{
QSE_ASSERT (g_script == QSE_NULL);
qse_fprintf (QSE_STDERR, QSE_T("-f file not implemented yet\n"));
goto oops;
g_script = load_script_file (g_script_file);
if (g_script == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot load %s\n"), g_script_file);
goto oops;
}
/* TODO: load script from a file */
}
@ -287,7 +327,8 @@ int sed_main (int argc, qse_char_t* argv[])
oops:
if (sed != QSE_NULL) qse_sed_close (sed);
if (g_script_file != QSE_NULL && g_script != QSE_NULL) free (g_script);
if (g_script_file != QSE_NULL && g_script != QSE_NULL)
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), g_script);
return ret;
}

View File

@ -1,5 +1,5 @@
/*
* $Id: Sed.hpp 269 2009-08-26 03:03:51Z hyunghwan.chung $
* $Id: Sed.hpp 280 2009-09-07 13:34:49Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -399,8 +399,10 @@ protected:
errstr_t dflerrstr;
private:
static ssize_t xin (sed_t* s, io_cmd_t cmd, io_arg_t* arg);
static ssize_t xout (sed_t* s, io_cmd_t cmd, io_arg_t* arg);
static ssize_t xin (
sed_t* s, io_cmd_t cmd, io_arg_t* arg, char_t* buf, size_t len);
static ssize_t xout (
sed_t* s, io_cmd_t cmd, io_arg_t* arg, char_t* dat, size_t len);
static const char_t* xerrstr (sed_t* s, errnum_t num);
private:

View File

@ -1,5 +1,5 @@
/*
* $Id: sed.h 277 2009-09-02 12:55:55Z hyunghwan.chung $
* $Id: sed.h 280 2009-09-07 13:34:49Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -170,23 +170,6 @@ struct qse_sed_io_arg_t
{
void* handle; /**< IO handle */
const qse_char_t* path; /**< file path. QSE_NULL for a console */
union
{
/** read buffer */
struct
{
qse_char_t* buf; /**< buffer pointer */
qse_size_t len; /**< buffer size */
} r;
/** data to write */
struct
{
const qse_char_t* data; /**< data pointer */
qse_size_t len; /**< data length */
} w;
} u;
};
typedef struct qse_sed_io_arg_t qse_sed_io_arg_t;
@ -197,7 +180,9 @@ typedef struct qse_sed_io_arg_t qse_sed_io_arg_t;
typedef qse_ssize_t (*qse_sed_io_fun_t) (
qse_sed_t* sed,
qse_sed_io_cmd_t cmd,
qse_sed_io_arg_t* arg
qse_sed_io_arg_t* arg,
qse_char_t* data,
qse_size_t count
);
/**

View File

@ -1,5 +1,5 @@
/*
* $Id: Sed.cpp 269 2009-08-26 03:03:51Z hyunghwan.chung $
* $Id: Sed.cpp 280 2009-09-07 13:34:49Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -125,7 +125,8 @@ void Sed::setConsoleLine (size_t num)
qse_sed_setlinnum (sed, num);
}
Sed::ssize_t Sed::xin (sed_t* s, io_cmd_t cmd, io_arg_t* arg)
Sed::ssize_t Sed::xin (
sed_t* s, io_cmd_t cmd, io_arg_t* arg, char_t* buf, size_t len)
{
Sed* sed = *(Sed**)QSE_XTN(s);
@ -142,8 +143,7 @@ Sed::ssize_t Sed::xin (sed_t* s, io_cmd_t cmd, io_arg_t* arg)
case QSE_SED_IO_CLOSE:
return sed->closeConsole (io);
case QSE_SED_IO_READ:
return sed->readConsole (
io, arg->u.r.buf, arg->u.r.len);
return sed->readConsole (io, buf, len);
default:
return -1;
}
@ -166,8 +166,7 @@ Sed::ssize_t Sed::xin (sed_t* s, io_cmd_t cmd, io_arg_t* arg)
case QSE_SED_IO_CLOSE:
return sed->closeFile (io);
case QSE_SED_IO_READ:
return sed->readFile (
io, arg->u.r.buf, arg->u.r.len);
return sed->readFile (io, buf, len);
default:
return -1;
}
@ -179,7 +178,8 @@ Sed::ssize_t Sed::xin (sed_t* s, io_cmd_t cmd, io_arg_t* arg)
}
}
Sed::ssize_t Sed::xout (sed_t* s, io_cmd_t cmd, io_arg_t* arg)
Sed::ssize_t Sed::xout (
sed_t* s, io_cmd_t cmd, io_arg_t* arg, char_t* dat, size_t len)
{
Sed* sed = *(Sed**)QSE_XTN(s);
@ -196,8 +196,7 @@ Sed::ssize_t Sed::xout (sed_t* s, io_cmd_t cmd, io_arg_t* arg)
case QSE_SED_IO_CLOSE:
return sed->closeConsole (io);
case QSE_SED_IO_WRITE:
return sed->writeConsole (
io, arg->u.w.data, arg->u.w.len);
return sed->writeConsole (io, dat, len);
default:
return -1;
}
@ -220,8 +219,7 @@ Sed::ssize_t Sed::xout (sed_t* s, io_cmd_t cmd, io_arg_t* arg)
case QSE_SED_IO_CLOSE:
return sed->closeFile (io);
case QSE_SED_IO_WRITE:
return sed->writeFile (
io, arg->u.w.data, arg->u.w.len);
return sed->writeFile (io, dat, len);
default:
return -1;
}

View File

@ -1,5 +1,5 @@
/*
* $Id: sed.c 278 2009-09-04 13:08:19Z hyunghwan.chung $
* $Id: sed.c 280 2009-09-07 13:34:49Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -1375,10 +1375,9 @@ static int read_char (qse_sed_t* sed, qse_char_t* c)
if (sed->e.in.pos >= sed->e.in.len)
{
sed->errnum = QSE_SED_ENOERR;
sed->e.in.arg.u.r.buf = sed->e.in.buf;
sed->e.in.arg.u.r.len = QSE_COUNTOF(sed->e.in.buf);
n = sed->e.in.fun (
sed, QSE_SED_IO_READ, &sed->e.in.arg
sed, QSE_SED_IO_READ, &sed->e.in.arg,
sed->e.in.buf, QSE_COUNTOF(sed->e.in.buf)
);
if (n <= -1)
{
@ -1419,7 +1418,7 @@ static int read_file (
arg.path = path;
sed->errnum = QSE_SED_ENOERR;
n = sed->e.in.fun (sed, QSE_SED_IO_OPEN, &arg);
n = sed->e.in.fun (sed, QSE_SED_IO_OPEN, &arg, QSE_NULL, 0);
if (n <= -1)
{
/*if (sed->errnum != QSE_SED_ENOERR)
@ -1431,20 +1430,18 @@ static int read_file (
if (n == 0)
{
/* EOF - no data */
sed->e.in.fun (sed, QSE_SED_IO_CLOSE, &arg);
sed->e.in.fun (sed, QSE_SED_IO_CLOSE, &arg, QSE_NULL, 0);
return 0;
}
while (1)
{
arg.u.r.buf = buf;
arg.u.r.len = QSE_COUNTOF(buf);
sed->errnum = QSE_SED_ENOERR;
n = sed->e.in.fun (sed, QSE_SED_IO_READ, &arg);
n = sed->e.in.fun (
sed, QSE_SED_IO_READ, &arg, buf, QSE_COUNTOF(buf));
if (n <= -1)
{
sed->e.in.fun (sed, QSE_SED_IO_CLOSE, &arg);
sed->e.in.fun (sed, QSE_SED_IO_CLOSE, &arg, QSE_NULL, 0);
if (sed->errnum == QSE_SED_ENOERR)
SETERR1 (sed, QSE_SED_EIOFIL, path, plen, &cmd->loc);
else sed->errloc = cmd->loc;
@ -1460,7 +1457,9 @@ static int read_file (
{
if (qse_str_ccat (&sed->e.txt.read, buf[i]) == (qse_size_t)-1)
{
sed->e.in.fun (sed, QSE_SED_IO_CLOSE, &arg);
sed->e.in.fun (
sed, QSE_SED_IO_CLOSE,
&arg, QSE_NULL, 0);
SETERR0 (sed, QSE_SED_ENOMEM, &cmd->loc);
return -1;
}
@ -1473,7 +1472,9 @@ static int read_file (
{
if (qse_str_ncat (&sed->e.txt.read, buf, n) == (qse_size_t)-1)
{
sed->e.in.fun (sed, QSE_SED_IO_CLOSE, &arg);
sed->e.in.fun (
sed, QSE_SED_IO_CLOSE,
&arg, QSE_NULL, 0);
SETERR0 (sed, QSE_SED_ENOMEM, &cmd->loc);
return -1;
}
@ -1481,7 +1482,7 @@ static int read_file (
}
done:
sed->e.in.fun (sed, QSE_SED_IO_CLOSE, &arg);
sed->e.in.fun (sed, QSE_SED_IO_CLOSE, &arg, QSE_NULL, 0);
return 0;
}
@ -1540,9 +1541,9 @@ static int flush (qse_sed_t* sed)
while (sed->e.out.len > 0)
{
sed->errnum = QSE_SED_ENOERR;
sed->e.out.arg.u.w.data = &sed->e.out.buf[pos];
sed->e.out.arg.u.w.len = sed->e.out.len;
n = sed->e.out.fun (sed, QSE_SED_IO_WRITE, &sed->e.out.arg);
n = sed->e.out.fun (
sed, QSE_SED_IO_WRITE, &sed->e.out.arg,
&sed->e.out.buf[pos], sed->e.out.len);
if (n <= -1)
{
@ -1758,7 +1759,7 @@ static int write_str_to_file (
{
sed->errnum = QSE_SED_ENOERR;
ap->path = path;
n = sed->e.out.fun (sed, QSE_SED_IO_OPEN, ap);
n = sed->e.out.fun (sed, QSE_SED_IO_OPEN, ap, QSE_NULL, 0);
if (n <= -1)
{
if (sed->errnum == QSE_SED_ENOERR)
@ -1771,7 +1772,7 @@ static int write_str_to_file (
/* EOF is returned upon opening a write stream.
* it is also an error as it can't write
* a requested string */
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, ap);
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, ap, QSE_NULL, 0);
ap->handle = QSE_NULL;
SETERR1 (sed, QSE_SED_EIOFIL, path, plen, &cmd->loc);
return -1;
@ -1781,12 +1782,11 @@ static int write_str_to_file (
while (len > 0)
{
sed->errnum = QSE_SED_ENOERR;
ap->u.w.data = str;
ap->u.w.len = len;
n = sed->e.out.fun (sed, QSE_SED_IO_WRITE, ap);
n = sed->e.out.fun (
sed, QSE_SED_IO_WRITE, ap, (qse_char_t*)str, len);
if (n <= -1)
{
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, ap);
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, ap, QSE_NULL, 0);
ap->handle = QSE_NULL;
if (sed->errnum == QSE_SED_ENOERR)
SETERR1 (sed, QSE_SED_EIOFIL, path, plen, &cmd->loc);
@ -1798,7 +1798,7 @@ static int write_str_to_file (
{
/* eof is returned on the write stream.
* it is also an error as it can't write any more */
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, ap);
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, ap, QSE_NULL, 0);
ap->handle = QSE_NULL;
SETERR1 (sed, QSE_SED_EIOFIL, path, plen, &cmd->loc);
return -1;
@ -2498,7 +2498,7 @@ static void close_outfile (qse_map_t* map, void* dptr, qse_size_t dlen)
if (arg->handle != QSE_NULL)
{
qse_sed_t* sed = *(qse_sed_t**)QSE_XTN(map);
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, arg);
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, arg, QSE_NULL, 0);
arg->handle = QSE_NULL;
}
}
@ -2655,7 +2655,7 @@ int qse_sed_exec (qse_sed_t* sed, qse_sed_io_fun_t inf, qse_sed_io_fun_t outf)
sed->errnum = QSE_SED_ENOERR;
sed->e.in.arg.path = QSE_NULL;
n = sed->e.in.fun (sed, QSE_SED_IO_OPEN, &sed->e.in.arg);
n = sed->e.in.fun (sed, QSE_SED_IO_OPEN, &sed->e.in.arg, QSE_NULL, 0);
if (n <= -1)
{
ret = -1;
@ -2672,7 +2672,7 @@ int qse_sed_exec (qse_sed_t* sed, qse_sed_io_fun_t inf, qse_sed_io_fun_t outf)
sed->errnum = QSE_SED_ENOERR;
sed->e.out.arg.path = QSE_NULL;
n = sed->e.out.fun (sed, QSE_SED_IO_OPEN, &sed->e.out.arg);
n = sed->e.out.fun (sed, QSE_SED_IO_OPEN, &sed->e.out.arg, QSE_NULL, 0);
if (n <= -1)
{
ret = -1;
@ -2766,9 +2766,9 @@ int qse_sed_exec (qse_sed_t* sed, qse_sed_io_fun_t inf, qse_sed_io_fun_t outf)
done:
qse_map_clear (&sed->e.out.files);
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, &sed->e.out.arg);
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, &sed->e.out.arg, QSE_NULL, 0);
done2:
sed->e.in.fun (sed, QSE_SED_IO_CLOSE, &sed->e.in.arg);
sed->e.in.fun (sed, QSE_SED_IO_CLOSE, &sed->e.in.arg, QSE_NULL, 0);
done3:
qse_str_fini (&sed->e.in.line);
qse_map_fini (&sed->e.out.files);

23
qse/regress/sed/002.dat Normal file
View File

@ -0,0 +1,23 @@
ab...c
------
AAA
de...f
gh...i
------
AAA1
jk...l
mn...o
pq...r
------
AAA2
kbs
------
ddd
dif
------
cccc

9
qse/regress/sed/002.sed Normal file
View File

@ -0,0 +1,9 @@
#
# author: bushi @ kldp
#
:a $!N
/\n------/N
s/\n------\n/ /
ta
P
D

23
qse/regress/sed/003.dat Normal file
View File

@ -0,0 +1,23 @@
ab...c
------
AAA
de...f
gh...i
------
AAA1
jk...l
mn...o
pq...r
------
AAA2
kbs
------
ddd
dif
------
cccc

13
qse/regress/sed/003.sed Normal file
View File

@ -0,0 +1,13 @@
#
# author: APRIL1024 @ kldp
#
/^[^ ][^ ]*/{
N
/--*/!{
P
D
}
N
s/\n.*\n/ /
}

9
qse/regress/sed/004.dat Normal file
View File

@ -0,0 +1,9 @@
linux {
Host: hellow.com
Address: 3.32.22.22
}
linux {
Host: ip.com
Address: 99.32.22.22
}

14
qse/regress/sed/004.sed Normal file
View File

@ -0,0 +1,14 @@
#
# author: pynoos @ kldp
#
/^linux/ {
:grab
/^}/ b end
s/^Host:.*$/HOST: com.com/
s/^Address:.*$/ADDRESS: 45.34.34.33/
n
b grab
:end
n
p
}