From 7875def835170df90b3313c49780504d21326272 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 30 May 2009 19:19:46 +0000 Subject: [PATCH] touched code a little bit --- qse/doc/page/sed.doc | 8 +- qse/include/qse/awk/awk.h | 296 +++++++++++++++++--------------------- qse/include/qse/cmn/pio.h | 78 +++++----- qse/include/qse/sed/sed.h | 4 +- qse/lib/cmn/pio.c | 4 +- qse/lib/sed/sed.c | 25 ++-- qse/lib/sed/sed.h | 232 ++++++++++++++++-------------- 7 files changed, 308 insertions(+), 339 deletions(-) diff --git a/qse/doc/page/sed.doc b/qse/doc/page/sed.doc index b845284f..5d0337be 100644 --- a/qse/doc/page/sed.doc +++ b/qse/doc/page/sed.doc @@ -3,7 +3,9 @@ STREAM EDITOR a stream editor is provided -@section sed_io_handler How to write an IO handler -IO handler has the following prototype... - +@section COMMAND +- : label label for b and t commands. a label can be composed of an + alpha-numeric character, an underscope, and an underline. +- # comment +- = print the current line number */ diff --git a/qse/include/qse/awk/awk.h b/qse/include/qse/awk/awk.h index 37458c18..036da8f8 100644 --- a/qse/include/qse/awk/awk.h +++ b/qse/include/qse/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h 151 2009-05-21 06:50:02Z hyunghwan.chung $ + * $Id: awk.h 168 2009-05-30 01:19:46Z hyunghwan.chung $ * Copyright 2006-2009 Chung, Hyung-Hwan. @@ -27,6 +27,9 @@ /** @file * An embeddable AWK interpreter is defined in this header files. * + * @todo + * - change the way to set a custom error string: follow qse_sed_errstr_t + * * @example awk01.c * This program demonstrates how to use qse_awk_rtx_loop(). * @example awk02.c @@ -38,22 +41,19 @@ */ /** @class qse_awk_t - * The qse_awk_t type defines an AWK interpreter. + * The qse_awk_t type defines an AWK interpreter. The details are hidden as + * it is a complex type susceptible to misuse. */ -typedef struct qse_awk_t qse_awk_t; +typedef struct qse_awk_t qse_awk_t; -/****t* AWK/qse_awk_rtx_t - * NAME - * qse_awk_rtx_t - define an AWK runtime context type - * SYNOPSIS +/** @class qse_awk_rtx_t + * The qse_awk_rtx_t type defines a runtime context. The details are hidden + * as it is a complex type susceptible to misuse. */ typedef struct qse_awk_rtx_t qse_awk_rtx_t; /* (R)untime con(T)e(X)t */ -/******/ -/****m* AWK/QSE_AWK_VAL_HDR - * NAME - * QSE_AWK_VAL_HDR - define the common header of a value - * SYNOPSIS +/** + * The QSE_AWK_VAL_HDR defines the common header of a value type. */ #if QSE_SIZEOF_INT == 2 # define QSE_AWK_VAL_HDR \ @@ -64,42 +64,31 @@ typedef struct qse_awk_rtx_t qse_awk_rtx_t; /* (R)untime con(T)e(X)t */ unsigned int type: 3; \ unsigned int ref: 29 #endif -/******/ #define QSE_AWK_VAL_TYPE(x) ((x)->type) -/****s* AWK/qse_awk_val_t - * NAME - * qse_awk_val_t - define an abstract value type - * SYNOPSIS +/** + * The qse_awk_val_t type is an abstract value type */ struct qse_awk_val_t { QSE_AWK_VAL_HDR; }; typedef struct qse_awk_val_t qse_awk_val_t; -/******/ -/****s* AWK/qse_awk_val_nil_t - * NAME - * qse_awk_val_nil_t - define a nil value type - * DESCRIPTION - * The type field is QSE_AWK_VAL_NIL. - * SYNOPSIS +/** + * The qse_awk_val_nil_t type is a nil value type. The type field is + * QSE_AWK_VAL_NIL. */ struct qse_awk_val_nil_t { QSE_AWK_VAL_HDR; }; typedef struct qse_awk_val_nil_t qse_awk_val_nil_t; -/******/ -/****s* AWK/qse_awk_val_int_t - * NAME - * qse_awk_val_int_t - define an integer number type - * DESCRIPTION - * The type field is QSE_AWK_VAL_INT. - * SYNOPSIS +/** + * The qse_awk_val_int_t type is an integer number type. The type field is + * QSE_AWK_VAL_INT. */ struct qse_awk_val_int_t { @@ -108,14 +97,10 @@ struct qse_awk_val_int_t void* nde; }; typedef struct qse_awk_val_int_t qse_awk_val_int_t; -/******/ -/****s* AWK/qse_awk_val_real_t - * NAME - * qse_awk_val_real_t - define a floating-point number type - * DESCRIPTION - * The type field is QSE_AWK_VAL_REAL. - * SYNOPSIS +/** + * The qse_awk_val_real_t type is a floating-point number type. The type field + * is QSE_AWK_VAL_REAL. */ struct qse_awk_val_real_t { @@ -124,14 +109,10 @@ struct qse_awk_val_real_t void* nde; }; typedef struct qse_awk_val_real_t qse_awk_val_real_t; -/******/ -/****s* AWK/qse_awk_val_str_t - * NAME - * qse_awk_val_str_t - define a string type - * DESCRIPTION - * The type field is QSE_AWK_VAL_STR. - * SYNOPSIS +/** + * The qse_awk_val_str_t type is a string type. The type field is + * QSE_AWK_VAL_STR. */ struct qse_awk_val_str_t { @@ -140,14 +121,10 @@ struct qse_awk_val_str_t qse_size_t len; }; typedef struct qse_awk_val_str_t qse_awk_val_str_t; -/******/ -/****s* AWK/qse_awk_val_rex_t - * NAME - * qse_awk_val_rex_t - define a regular expression type - * DESCRIPTION - * The type field is QSE_AWK_VAL_REX. - * SYNOPSIS +/** + * The qse_awk_val_rex_t type is a regular expression type. The type field + * is QSE_AWK_VAL_REX. */ struct qse_awk_val_rex_t { @@ -157,7 +134,6 @@ struct qse_awk_val_rex_t void* code; }; typedef struct qse_awk_val_rex_t qse_awk_val_rex_t; -/******/ /* QSE_AWK_VAL_MAP */ struct qse_awk_val_map_t @@ -1546,30 +1522,23 @@ qse_bool_t qse_awk_rtx_isstaticval ( qse_awk_val_t* val ); -/****f* AWK/qse_awk_rtx_refupval - * NAME - * qse_awk_rtx_refupval - increment a reference count - * SYNOPSIS +/** + * The qse_awk_rtx_refupval() function increments a reference count of a + * value @a val. */ void qse_awk_rtx_refupval ( - qse_awk_rtx_t* rtx, - qse_awk_val_t* val + qse_awk_rtx_t* rtx, /**< a runtime context */ + qse_awk_val_t* val /**< a value */ ); -/******/ -/****f* AWK/qse_awk_rtx_refdownval - * NAME - * qse_awk_rtx_refdownval - decrement a reference count - * DESCRIPTION - * The qse_awk_rtx_refdownval() function decrements a reference count of - * a value. It destroys the value if it has reached the count of 0. - * SYNOPSIS +/** + * The qse_awk_rtx_refdownval() function decrements a reference count of + * a value @a val. It destroys the value if it has reached the count of 0. */ void qse_awk_rtx_refdownval ( - qse_awk_rtx_t* rtx, - qse_awk_val_t* val + qse_awk_rtx_t* rtx, /**< a runtime context */ + qse_awk_val_t* val /**< a value */ ); -/******/ void qse_awk_rtx_refdownval_nofree ( qse_awk_rtx_t* rtx, @@ -1581,98 +1550,95 @@ qse_bool_t qse_awk_rtx_valtobool ( qse_awk_val_t* val ); -/****f* AWK/qse_awk_rtx_valtostr - * NAME - * qse_awk_rtx_valtostr - convert a value to a string - * DESCRIPTION - * The qse_awk_rtx_valtostr() function convers a value val to a string as - * instructed in the parameter out. Before the call to the function, you - * should initialize a variable of the qse_awk_rtx_valtostr_out_t type. +/** + * The qse_awk_rtx_valtostr() function convers a value val to a string as + * instructed in the parameter out. Before the call to the function, you + * should initialize a variable of the qse_awk_rtx_valtostr_out_t type. * - * The type field is one of the following qse_awk_rtx_valtostr_type_t values: - * * QSE_AWK_RTX_VALTOSTR_CPL - * * QSE_AWK_RTX_VALTOSTR_CPLDUP - * * QSE_AWK_RTX_VALTOSTR_STRP - * * QSE_AWK_RTX_VALTOSTR_STRPCAT - * It can optionally be ORed with QSE_AWK_RTX_VALTOSTR_PRINT. The option - * causes the function to use OFMT for real number conversion. Otherwise, - * it uses CONVFMT. + * The type field is one of the following qse_awk_rtx_valtostr_type_t values: * - * You should initialize or free other fields before and after the call - * depending on the type field as shown in EXAMPLES. + * - QSE_AWK_RTX_VALTOSTR_CPL + * - QSE_AWK_RTX_VALTOSTR_CPLDUP + * - QSE_AWK_RTX_VALTOSTR_STRP + * - QSE_AWK_RTX_VALTOSTR_STRPCAT + * + * It can optionally be ORed with QSE_AWK_RTX_VALTOSTR_PRINT. The option + * causes the function to use OFMT for real number conversion. Otherwise, + * it uses CONVFMT. + * + * You should initialize or free other fields before and after the call + * depending on the type field as shown below. * - * RETURN - * The qse_awk_rtx_valtostr() function returns the pointer to the string - * converted on success, and QSE_NULL on failure. - * - * EXAMPLES - * If you have a static buffer, use QSE_AWK_RTX_VALTOSTR_CPL. - * qse_awk_rtx_valtostr_out_t out; - * qse_char_t buf[100]; - * out.type = QSE_AWK_RTX_VALTOSTR_CPL; - * out.u.cpl.ptr = buf; - * out.u.cpl.len = QSE_COUNTOF(buf); - * if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops; - * qse_printf (QSE_T("%.*s\n"), (int)out.u.cpl.len, out.u.cpl.ptr); - * - * When unsure of the size of the string after conversion, you can use - * QSE_AWK_RTX_VALTOSTR_CPLDUP. However, you should free the memory block - * pointed to by the u.cpldup.ptr field after use. - * qse_awk_rtx_valtostr_out_t out; - * out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - * if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops; - * qse_printf (QSE_T("%.*s\n"), (int)out.u.cpldup.len, out.u.cpldup.ptr); - * qse_awk_rtx_free (rtx, out.u.cpldup.ptr); - * - * You may like to store the result in a dynamically resizable string. - * Consider QSE_AWK_RTX_VALTOSTR_STRP. - * qse_awk_rtx_valtostr_out_t out; - * qse_str_t str; - * qse_str_init (&str, qse_awk_rtx_getmmgr(rtx), 100); - * out.type = QSE_AWK_RTX_VALTOSTR_STRP; - * out.u.strp = str; - * if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops; - * qse_printf (QSE_T("%.*s\n"), - * (int)QSE_STR_LEN(out.u.strp), QSE_STR_PTR(out.u.strp)); - * qse_str_fini (&str); + * If you have a static buffer, use QSE_AWK_RTX_VALTOSTR_CPL. + * @code + * qse_awk_rtx_valtostr_out_t out; + * qse_char_t buf[100]; + * out.type = QSE_AWK_RTX_VALTOSTR_CPL; + * out.u.cpl.ptr = buf; + * out.u.cpl.len = QSE_COUNTOF(buf); + * if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops; + * qse_printf (QSE_T("%.*s\n"), (int)out.u.cpl.len, out.u.cpl.ptr); + * @endcode * - * If you want to append the converted string to an existing dynamically - * resizable string, QSE_AWK_RTX_VALTOSTR_STRPCAT is the answer. The usage is - * the same as QSE_AWK_RTX_VALTOSTR_STRP except that you have to use the - * u.strpcat field instead of the u.strp field. - * SYNOPSIS + * When unsure of the size of the string after conversion, you can use + * QSE_AWK_RTX_VALTOSTR_CPLDUP. However, you should free the memory block + * pointed to by the u.cpldup.ptr field after use. + * @code + * qse_awk_rtx_valtostr_out_t out; + * out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; + * if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops; + * qse_printf (QSE_T("%.*s\n"), (int)out.u.cpldup.len, out.u.cpldup.ptr); + * qse_awk_rtx_free (rtx, out.u.cpldup.ptr); + * @endcode + * + * You may like to store the result in a dynamically resizable string. + * Consider QSE_AWK_RTX_VALTOSTR_STRP. + * @code + * qse_awk_rtx_valtostr_out_t out; + * qse_str_t str; + * qse_str_init (&str, qse_awk_rtx_getmmgr(rtx), 100); + * out.type = QSE_AWK_RTX_VALTOSTR_STRP; + * out.u.strp = str; + * if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops; + * qse_printf (QSE_T("%.*s\n"), + * (int)QSE_STR_LEN(out.u.strp), QSE_STR_PTR(out.u.strp)); + * qse_str_fini (&str); + * @endcode + * + * If you want to append the converted string to an existing dynamically + * resizable string, QSE_AWK_RTX_VALTOSTR_STRPCAT is the answer. The usage is + * the same as QSE_AWK_RTX_VALTOSTR_STRP except that you have to use the + * u.strpcat field instead of the u.strp field. + * + * @return the pointer to a string converted on success, QSE_NULL on failure */ qse_char_t* qse_awk_rtx_valtostr ( - qse_awk_rtx_t* rtx, - qse_awk_val_t* val, - qse_awk_rtx_valtostr_out_t* out + qse_awk_rtx_t* rtx, /**< a runtime context */ + qse_awk_val_t* val, /**< a vlaue */ + qse_awk_rtx_valtostr_out_t* out /**< a output buffer */ ); /******/ -/****f* AWK/qse_awk_rtx_valtocpldup - * NAME - * qse_awk_rtx_valtocpldup - convert a value to a string - * DESCRIPTION - * The qse_awk_rtx_valtocpldup() function provides a shortcut to the - * qse_awk_rtx_valtostr() function with the QSE_AWK_RTX_VALTOSTR_CPLDUP type. - * It returns the pointer to the string converted and stores the length - * to memory pointed to by the parameter len. You should free the returned - * memory block after use. - * RETURN - * It returns the pointer to the string converted on success, and - * QSE_NULL on failure. - * EXAMPLE - * The example shows a simple usage of the function. - * ptr = qse_awk_rtx_valtocpldup (rtx, v, &len); - * if (str == QSE_NULL) goto oops; - * qse_printf (QSE_T("%.*s\n"), (int)len, ptr); - * qse_awk_rtx_free (rtx, ptr); - * SYNOPSIS +/** + * The qse_awk_rtx_valtocpldup() function provides a shortcut to the + * qse_awk_rtx_valtostr() function with the QSE_AWK_RTX_VALTOSTR_CPLDUP type. + * It returns the pointer to a string converted from @a val and stores its + * length to memory pointed to by @a len. You should free the returned + * memory block after use. See the code snippet below for a simple usage. + * + * @code + * ptr = qse_awk_rtx_valtocpldup (rtx, v, &len); + * if (str == QSE_NULL) handle_error(); + * qse_printf (QSE_T("%.*s\n"), (int)len, ptr); + * qse_awk_rtx_free (rtx, ptr); + * @endcode + * + * @return the pointer to a string converted on success, QSE_NULL on failure */ qse_char_t* qse_awk_rtx_valtocpldup ( - qse_awk_rtx_t* rtx, - qse_awk_val_t* val, - qse_size_t* len + qse_awk_rtx_t* rtx, /**< a runtime context */ + qse_awk_val_t* val, /**< a value to convert */ + qse_size_t* len /**< result length */ ); /******/ @@ -1721,30 +1687,24 @@ int qse_awk_rtx_strtonum ( ); /******/ -/****f* AWK/qse_awk_rtx_alloc - * NAME - * qse_awk_rtx_alloc - allocate memory - * RETURN - * The qse_awk_rtx_alloc() function returns a pointer to the memory block - * allocated on success, and QSE_NULL on failure. - * SYNOPSIS +/** + * The qse_awk_rtx_alloc() function allocats a memory block of @a size bytes + * using the memory manager associated with a runtime context @a rtx. + * @return the pointer to a memory block on success, QSE_NULL on failure. */ void* qse_awk_rtx_alloc ( - qse_awk_rtx_t* rtx, - qse_size_t size + qse_awk_rtx_t* rtx, /**< a runtime context */ + qse_size_t size /**< block size in bytes */ ); -/******/ -/****f* AWK/qse_awk_rtx_free - * NAME - * qse_awk_rtx_free - free memory - * SYNOPSIS +/** + * The qse_awk_rtx_free() function frees a memory block pointed to by @a ptr + * using the memory manager of a runtime ocntext @a rtx. */ void qse_awk_rtx_free ( - qse_awk_rtx_t* rtx, - void* ptr + qse_awk_rtx_t* rtx, /**< a runtime context */ + void* ptr /**< a memory block pointer */ ); -/******/ #ifdef __cplusplus } diff --git a/qse/include/qse/cmn/pio.h b/qse/include/qse/cmn/pio.h index 3f8ff3b9..0b9cc6e0 100644 --- a/qse/include/qse/cmn/pio.h +++ b/qse/include/qse/cmn/pio.h @@ -1,5 +1,5 @@ /* - * $Id: pio.h 75 2009-02-22 14:10:34Z hyunghwan.chung $ + * $Id: pio.h 168 2009-05-30 01:19:46Z hyunghwan.chung $ * Copyright 2006-2009 Chung, Hyung-Hwan. @@ -23,6 +23,13 @@ #include #include +/** @file + * Pipe I/O + * @todo + * - rename flags to option + * - write code for win32 + */ + enum qse_pio_open_flag_t { /* enable ase_char_t based IO */ @@ -65,7 +72,7 @@ enum qse_pio_io_flag_t QSE_PIO_WAIT_NORETRY = (1 << 5) }; -enum qse_pio_err_t +enum qse_pio_errnum_t { QSE_PIO_ENOERR = 0, QSE_PIO_ENOMEM, /* out of memory */ @@ -76,7 +83,7 @@ enum qse_pio_err_t }; typedef enum qse_pio_hid_t qse_pio_hid_t; -typedef enum qse_pio_err_t qse_pio_err_t; +typedef enum qse_pio_errnum_t qse_pio_errnum_t; #ifdef _WIN32 /* => typedef PVOID HANDLE; */ @@ -101,21 +108,18 @@ struct qse_pio_pin_t qse_pio_t* self; }; -/****s* Common/qse_pio_t - * NAME - * qse_pio_t - define an pipe IO type - * SYNOPSIS +/** + * The qse_pio_t type defines a pipe I/O type */ struct qse_pio_t { QSE_DEFINE_COMMON_FIELDS(pio) - int flags; - qse_pio_err_t errnum; - qse_pio_pid_t child; - qse_pio_pin_t pin[3]; + int flags; + qse_pio_errnum_t errnum; + qse_pio_pid_t child; + qse_pio_pin_t pin[3]; }; -/*****/ #define QSE_PIO_ERRNUM(pio) ((pio)->errnum) #define QSE_PIO_FLAGS(pio) ((pio)->flags) @@ -128,40 +132,27 @@ extern "C" { QSE_DEFINE_COMMON_FUNCTIONS (pio) -/****f* Common/qse_pio_open - * NAME - * qse_pio_open - open pipes to a child process - * - * DESCRIPTION - * QSE_PIO_SHELL drives the funcpion to execute the command via /bin/sh. - * If flags is clear of QSE_PIO_SHELL, you should pass the full program path. - * - * SYNOPSIS +/** + * The qse_pio_open() function opens pipes to a child process. + * QSE_PIO_SHELL drives the function to execute the command via /bin/sh. + * If flags is clear of QSE_PIO_SHELL, you should pass the full program path. */ qse_pio_t* qse_pio_open ( - qse_mmgr_t* mmgr, - qse_size_t ext, - const qse_char_t* cmd, - int flags + qse_mmgr_t* mmgr, /**< a memory manager */ + qse_size_t ext, /**< extension size */ + const qse_char_t* cmd, /**< a command to execute */ + int flags /**< options */ ); -/******/ -/****f* Common/qse_pio_close - * NAME - * qse_pio_close - close pipes to a child process - * - * SYNOPSIS +/** + * The qse_pio_close() function closes pipes to a child process. */ void qse_pio_close ( qse_pio_t* pio ); -/******/ -/****f* Common/qse_pio_init - * NAME - * qse_pio_init - initialize pipes to a child process - * - * SYNOPSIS +/** + * The qse_pio_init() function initializes pipes to a child process. */ qse_pio_t* qse_pio_init ( qse_pio_t* pio, @@ -169,18 +160,13 @@ qse_pio_t* qse_pio_init ( const qse_char_t* path, int flags ); -/******/ -/****f* Common/qse_pio_fini - * NAME - * qse_pio_fini - finalize pipes to a child process - * - * SYNOPSIS +/** + * The qse_pio_fini() function finalizes pipes to a child process. */ void qse_pio_fini ( qse_pio_t* pio ); -/******/ int qse_pio_getflags ( qse_pio_t* pio @@ -189,7 +175,7 @@ int qse_pio_getflags ( void qse_pio_setflags ( qse_pio_t* pio, int flags, - int op + int opt ); /****f* Common/qse_pio_geterrnum @@ -198,7 +184,7 @@ void qse_pio_setflags ( * * SYNOPSIS */ -qse_pio_err_t qse_pio_geterrnum ( +qse_pio_errnum_t qse_pio_geterrnum ( qse_pio_t* pio ); /******/ diff --git a/qse/include/qse/sed/sed.h b/qse/include/qse/sed/sed.h index 4eed997e..2a4d5fbb 100644 --- a/qse/include/qse/sed/sed.h +++ b/qse/include/qse/sed/sed.h @@ -36,8 +36,8 @@ * @endcode * * @todo - * - to allow flexible numbers of commands - * - to allow configurable recursion depth for a regular expression + * - allow flexible numbers of commands + * - allow configurable recursion depth for a regular expression * * @example sed01.c * This example shows how to embed a basic stream editor. diff --git a/qse/lib/cmn/pio.c b/qse/lib/cmn/pio.c index 1803b69f..485174cb 100644 --- a/qse/lib/cmn/pio.c +++ b/qse/lib/cmn/pio.c @@ -1,5 +1,5 @@ /* - * $Id: pio.c 76 2009-02-22 14:18:06Z hyunghwan.chung $ + * $Id: pio.c 168 2009-05-30 01:19:46Z hyunghwan.chung $ * Copyright 2006-2009 Chung, Hyung-Hwan. @@ -415,7 +415,7 @@ void qse_pio_setflags (qse_pio_t* pio, int flags, int op) else /*if (op < 0)*/ pio->flags &= ~flags; } -qse_pio_err_t qse_pio_geterrnum (qse_pio_t* pio) +qse_pio_errnum_t qse_pio_geterrnum (qse_pio_t* pio) { return pio->errnum; } diff --git a/qse/lib/sed/sed.c b/qse/lib/sed/sed.c index 01c49a0d..4130b417 100644 --- a/qse/lib/sed/sed.c +++ b/qse/lib/sed/sed.c @@ -137,9 +137,9 @@ static qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr) qse_map_setcopier (&sed->tmp.labs, QSE_MAP_KEY, QSE_MAP_COPIER_INLINE); qse_map_setscale (&sed->tmp.labs, QSE_MAP_KEY, QSE_SIZEOF(qse_char_t)); - /* TODO: use different data structure... */ + sed->cmd.len = 256; /* TODO: parameterize this value */ sed->cmd.buf = QSE_MMGR_ALLOC ( - sed->mmgr, QSE_SIZEOF(qse_sed_cmd_t) * 1000); + sed->mmgr, QSE_SIZEOF(qse_sed_cmd_t) * sed->cmd.len); if (sed->cmd.buf == QSE_NULL) { qse_map_fini (&sed->tmp.labs); @@ -148,7 +148,12 @@ static qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr) return QSE_NULL; } sed->cmd.cur = sed->cmd.buf; - sed->cmd.end = sed->cmd.buf + 1000 - 1; + sed->cmd.end = sed->cmd.buf + sed->cmd.len - 1; + +#if 0 + sed->cmd.lb = &sed->cmd.fb; /* on init, the last points to the first */ + sed->fb.len = 0; /* the block has no data yet */ +#endif if (qse_lda_init (&sed->e.txt.appended, mmgr, 32) == QSE_NULL) { @@ -405,7 +410,7 @@ static qse_sed_adr_t* get_address (qse_sed_t* sed, qse_sed_adr_t* a) } else if (c >= QSE_T('0') && c <= QSE_T('9')) { - qse_sed_line_t lno = 0; + qse_size_t lno = 0; do { lno = lno * 10 + c - QSE_T('0'); @@ -417,7 +422,7 @@ static qse_sed_adr_t* get_address (qse_sed_t* sed, qse_sed_adr_t* a) if (lno == 0) return QSE_NULL; a->type = QSE_SED_ADR_LINE; - a->u.line = lno; + a->u.lno = lno; } else if (c == QSE_T('\\')) { @@ -1947,7 +1952,7 @@ static int match_a (qse_sed_t* sed, qse_sed_cmd_t* cmd, qse_sed_adr_t* a) switch (a->type) { case QSE_SED_ADR_LINE: - return (sed->e.in.num == a->u.line)? 1: 0; + return (sed->e.in.num == a->u.lno)? 1: 0; case QSE_SED_ADR_REX: { @@ -2033,8 +2038,8 @@ static int match_address (qse_sed_t* sed, qse_sed_cmd_t* cmd) /* stepping address */ cmd->state.c_ready = 1; - if (sed->e.in.num < cmd->a1.u.line) return 0; - if ((sed->e.in.num - cmd->a1.u.line) % cmd->a2.u.line == 0) return 1; + if (sed->e.in.num < cmd->a1.u.lno) return 0; + if ((sed->e.in.num - cmd->a1.u.lno) % cmd->a2.u.lno == 0) return 1; return 0; } else if (cmd->a2.type != QSE_SED_ADR_NONE) @@ -2047,7 +2052,7 @@ static int match_address (qse_sed_t* sed, qse_sed_cmd_t* cmd) if (n == 0) { if (cmd->a2.type == QSE_SED_ADR_LINE && - sed->e.in.num > cmd->a2.u.line) + sed->e.in.num > cmd->a2.u.lno) { /* exit the range */ cmd->state.a1_matched = 0; @@ -2074,7 +2079,7 @@ static int match_address (qse_sed_t* sed, qse_sed_cmd_t* cmd) } if (cmd->a2.type == QSE_SED_ADR_LINE && - sed->e.in.num >= cmd->a2.u.line) + sed->e.in.num >= cmd->a2.u.lno) { /* the line number specified in the second * address is equal to or less than the current diff --git a/qse/lib/sed/sed.h b/qse/lib/sed/sed.h index 99aff492..fd460518 100644 --- a/qse/lib/sed/sed.h +++ b/qse/lib/sed/sed.h @@ -22,114 +22,9 @@ #include #include -typedef qse_int_t qse_sed_line_t; typedef struct qse_sed_adr_t qse_sed_adr_t; typedef struct qse_sed_cmd_t qse_sed_cmd_t; - -/** - * The qse_sed_t type defines a stream editor - */ -struct qse_sed_t -{ - QSE_DEFINE_COMMON_FIELDS (sed) - - qse_sed_errstr_t errstr; /**< error string getter */ - qse_sed_errnum_t errnum; /**< stores an error number */ - qse_char_t errmsg[128]; /**< error message holder */ - qse_size_t errlin; /**< no of the line where an error occurred */ - - int option; /**< stores options */ - - /** source text pointers */ - struct - { - qse_size_t lnum; /**< line number */ - qse_cint_t cc; /**< last character read */ - const qse_char_t* ptr; /**< beginning of the source text */ - const qse_char_t* end; /**< end of the source text */ - const qse_char_t* cur; /**< current source text pointer */ - } src; - - /** temporary data for compiling */ - 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 */ - struct - { - qse_sed_cmd_t* buf; /**< buffer holding compiled commands */ - qse_sed_cmd_t* end; /**< end of the buffer */ - qse_sed_cmd_t* cur; /**< points next to the last command */ - } cmd; - - /** data for execution */ - struct - { - /** data needed for output streams and files */ - struct - { - qse_sed_io_fun_t fun; /**< an output handler */ - qse_sed_io_arg_t arg; /**< output handling data */ - - qse_char_t buf[2048]; - qse_size_t len; - int eof; - - /*****************************************************/ - /* the following two fields are very tightly-coupled. - * don't make any partial changes */ - qse_map_t files; - qse_sed_t* files_ext; - /*****************************************************/ - } out; - - /** data needed for input streams */ - struct - { - qse_sed_io_fun_t fun; /**< an input handler */ - qse_sed_io_arg_t arg; /**< input handling data */ - - qse_char_t xbuf[1]; /**< a read-ahead buffer */ - int xbuf_len; /**< data length in the buffer */ - - qse_char_t buf[2048]; /**< input buffer */ - qse_size_t len; /**< data length in the buffer */ - qse_size_t pos; /**< current position in the buffer */ - int eof; /**< EOF indicator */ - - qse_str_t line; /**< pattern space */ - qse_size_t num; /**< current line number */ - } in; - - /** text buffers */ - struct - { - qse_lda_t appended; - qse_str_t read; - qse_str_t held; - qse_str_t subst; - } txt; - - /** indicates if a successful substitution has been made - * since the last read on the input stream. */ - int subst_done; - } e; -}; +typedef struct qse_sed_cmd_blk_t qse_sed_cmd_blk_t; struct qse_sed_adr_t { @@ -144,8 +39,8 @@ struct qse_sed_adr_t union { - qse_sed_line_t line; - void* rex; + qse_size_t lno; + void* rex; } u; }; @@ -226,4 +121,125 @@ struct qse_sed_cmd_t } state; }; +struct qse_sed_cmd_blk_t +{ + qse_size_t len; + qse_sed_cmd_t buf[512]; + qse_sed_cmd_blk_t* next; +}; + +/** + * The qse_sed_t type defines a stream editor + */ +struct qse_sed_t +{ + QSE_DEFINE_COMMON_FIELDS (sed) + + qse_sed_errstr_t errstr; /**< error string getter */ + qse_sed_errnum_t errnum; /**< stores an error number */ + qse_char_t errmsg[128]; /**< error message holder */ + qse_size_t errlin; /**< no of the line where an error occurred */ + + int option; /**< stores options */ + + /** source text pointers */ + struct + { + qse_size_t lnum; /**< line number */ + qse_cint_t cc; /**< last character read */ + const qse_char_t* ptr; /**< beginning of the source text */ + const qse_char_t* end; /**< end of the source text */ + const qse_char_t* cur; /**< current source text pointer */ + } src; + + /** temporary data for compiling */ + 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 */ + struct + { + qse_size_t len; /**< buffer size */ + qse_sed_cmd_t* buf; /**< buffer holding compiled commands */ + qse_sed_cmd_t* end; /**< end of the buffer */ + qse_sed_cmd_t* cur; /**< points next to the last command */ + } cmd; + +#if 0 + struct + { + qse_sed_cmd_blk_t fb; /**< the first block is static */ + qse_sed_cmd_blk_t* lb; /**< points to the last block */ + } cmd; +#endif + + /** data for execution */ + struct + { + /** data needed for output streams and files */ + struct + { + qse_sed_io_fun_t fun; /**< an output handler */ + qse_sed_io_arg_t arg; /**< output handling data */ + + qse_char_t buf[2048]; + qse_size_t len; + int eof; + + /*****************************************************/ + /* the following two fields are very tightly-coupled. + * don't make any partial changes */ + qse_map_t files; + qse_sed_t* files_ext; + /*****************************************************/ + } out; + + /** data needed for input streams */ + struct + { + qse_sed_io_fun_t fun; /**< an input handler */ + qse_sed_io_arg_t arg; /**< input handling data */ + + qse_char_t xbuf[1]; /**< a read-ahead buffer */ + int xbuf_len; /**< data length in the buffer */ + + qse_char_t buf[2048]; /**< input buffer */ + qse_size_t len; /**< data length in the buffer */ + qse_size_t pos; /**< current position in the buffer */ + int eof; /**< EOF indicator */ + + qse_str_t line; /**< pattern space */ + qse_size_t num; /**< current line number */ + } in; + + /** text buffers */ + struct + { + qse_lda_t appended; + qse_str_t read; + qse_str_t held; + qse_str_t subst; + } txt; + + /** indicates if a successful substitution has been made + * since the last read on the input stream. */ + int subst_done; + } e; +}; + #endif