I fixed one sed labels related bug. Sed labels were not been handled properly.
Code has been updated and can be obtained by mercurial as :
$ hg clone ssh://[EMAIL PROTECTED]/hg/webstack/mod_sed
Regards,
Basant.
diff -r 6dedc621b00d libsed.h
--- a/libsed.h Thu Apr 17 15:15:01 2008 -0700
+++ b/libsed.h Thu Apr 24 17:23:06 2008 -0700
@@ -104,6 +104,7 @@ struct sed_commands_s {
sed_reptr_t *rep;
int nrep;
apr_pool_t *pool;
+ int canbefinal;
};
typedef struct sed_eval_s sed_eval_t;
@@ -157,7 +158,8 @@ apr_status_t sed_init_commands(sed_comma
apr_pool_t *p);
apr_status_t sed_compile_string(sed_commands_t *commands, const char *s);
apr_status_t sed_compile_file(sed_commands_t *commands, apr_file_t *fin);
-apr_status_t sed_finalize_commands(sed_commands_t *commands);
+char* sed_get_finalize_error(const sed_commands_t *commands, apr_pool_t* pool);
+int sed_canbe_finalized(const sed_commands_t *commands);
void sed_destroy_commands(sed_commands_t *commands);
apr_status_t sed_init_eval(sed_eval_t *eval, sed_commands_t *commands,
diff -r 6dedc621b00d regexp.h
--- a/regexp.h Thu Apr 17 15:15:01 2008 -0700
+++ b/regexp.h Thu Apr 24 17:23:06 2008 -0700
@@ -102,6 +102,7 @@ extern void command_errf(sed_commands_t
#define SEDERR_TSNTSS "transform strings not the same size: %s"
#define SEDERR_OLTL "output line too long."
#define SEDERR_HSOVERFLOW "hold space overflowed."
+#define SEDERR_INTERNAL "internal sed error"
#ifdef __cplusplus
}
diff -r 6dedc621b00d sed0.c
--- a/sed0.c Thu Apr 17 15:15:01 2008 -0700
+++ b/sed0.c Thu Apr 24 17:23:06 2008 -0700
@@ -39,6 +39,7 @@ static char *comple(sed_commands_t *comm
static char *comple(sed_commands_t *commands, sed_comp_args *compargs,
char *x1, char *ep, char *x3, char x4);
static sed_reptr_t *alloc_reptr(sed_commands_t *commands);
+static int check_finalized(const sed_commands_t *commands);
void command_errf(sed_commands_t *commands, const char *fmt, ...)
{
@@ -80,6 +81,7 @@ apr_status_t sed_init_commands(sed_comma
commands->rep->ad1 = commands->respace;
commands->reend = &commands->respace[RESIZE - 1];
commands->labend = &commands->labtab[SED_LABSIZE];
+ commands->canbefinal = 1;
return APR_SUCCESS;
}
@@ -102,6 +104,8 @@ apr_status_t sed_compile_string(sed_comm
commands->eflag = 1;
rv = fcomp(commands, NULL);
+ if (rv == APR_SUCCESS)
+ commands->canbefinal = check_finalized(commands);
commands->eflag = 0;
@@ -118,40 +122,75 @@ apr_status_t sed_compile_file(sed_comman
}
/*
- * sed_finalize_commands
- */
-apr_status_t sed_finalize_commands(sed_commands_t *commands)
-{
- sed_label_t *lab;
-
+ * sed_get_finalize_error
+ */
+char* sed_get_finalize_error(const sed_commands_t *commands, apr_pool_t* pool)
+{
+ const sed_label_t *lab;
if (commands->depth) {
- command_errf(commands, SEDERR_TMOMES);
- return APR_EGENERAL;
- }
-
- for (lab = commands->labtab; lab < commands->lab; lab++) {
+ return SEDERR_TMOMES;
+ }
+
+ /* Empty branch chain is not a issue */
+ for (lab = commands->labtab + 1; lab < commands->lab; lab++) {
+ char *error;
if (lab->address == 0) {
- command_errf(commands, SEDERR_ULMES, lab->asc);
- return APR_EGENERAL;
+ error = apr_psprintf(pool, SEDERR_ULMES, lab->asc);
+ return error;
}
if (lab->chain) {
- sed_reptr_t *rep;
-
- rep = lab->chain;
- while (rep->lb1) {
- sed_reptr_t *next;
-
- next = rep->lb1;
- rep->lb1 = lab->address;
- rep = next;
- }
-
- rep->lb1 = lab->address;
- }
- }
-
- return APR_SUCCESS;
+ return SEDERR_INTERNAL;
+ }
+ }
+ return NULL;
+}
+
+/*
+ * sed_canbe_finalized
+ */
+int sed_canbe_finalized(const sed_commands_t *commands)
+{
+ return commands->canbefinal;
+}
+
+/*
+ * check_finalized
+ */
+static int check_finalized(const sed_commands_t *commands)
+{
+ const sed_label_t *lab;
+ if (commands->depth) {
+ return 0;
+ }
+
+ /* Empty branch chain is not a issue */
+ for (lab = commands->labtab + 1; lab < commands->lab; lab++) {
+ if (lab->address == 0 || (lab->chain)) {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+/*
+ * dechain
+ */
+static void dechain(sed_label_t *lpt, sed_reptr_t *address)
+{
+ sed_reptr_t *rep;
+ if ((lpt == NULL) || (lpt->chain == NULL) || (address == NULL))
+ return;
+ rep = lpt->chain;
+ while (rep->lb1) {
+ sed_reptr_t *next;
+
+ next = rep->lb1;
+ rep->lb1 = address;
+ rep = next;
+ }
+ rep->lb1 = address;
+ lpt->chain = NULL;
}
/*
@@ -309,6 +348,7 @@ swit:
command_errf(commands, SEDERR_DLMES, commands->linebuf);
return -1;
}
+ dechain(lpt, commands->rep);
} else {
commands->lab->chain = 0;
lpt = commands->lab;
diff -r 6dedc621b00d sed1.c
--- a/sed1.c Thu Apr 17 15:15:01 2008 -0700
+++ b/sed1.c Thu Apr 24 17:23:06 2008 -0700
@@ -207,6 +207,15 @@ apr_status_t sed_eval_buffer(sed_eval_t
if (eval->quitflag)
return APR_SUCCESS;
+
+ if (!sed_canbe_finalized(eval->commands)) {
+ /* Commands were not finalized properly. */
+ const char* error = sed_get_finalize_error(eval->commands, eval->pool);
+ if (error) {
+ eval_errf(eval, error);
+ return APR_EGENERAL;
+ }
+ }
eval->fout = fout;