Commit fdf02db3 fdf02db320d6bd88920cdcd15db3c45f71d2fb74 by Sergey Poznyakoff

Rename mu_sieve_machine_init to mu_sieve_machine_create

1 parent f0d3890a
......@@ -222,8 +222,8 @@ typedef int (*mu_sieve_printf_t) (void *@var{data},
@table @var
@item data
A pointer to application specific data. These data are passed as
second argument to @code{mu_sieve_machine_init()}.
A pointer to application specific data. It is set using
the @code{mu_sieve_set_data} call.
@item fmt
Printf-like format string.
@item ap
......@@ -362,13 +362,12 @@ case. [FIXME: describe how to do that]
This subsection describes functions used to create an instance of the
sieve machine, read or alter its internal fields and destroy it.
@deftypefun int mu_sieve_machine_init (mu_sieve_machine_t *@var{mach}, void *@var{data})
@deftypefun int mu_sieve_machine_create (mu_sieve_machine_t *@var{mach})
The @code{mu_sieve_machine_init()} function creates an instance of a sieve
machine. A pointer to the instance itself is returned in the argument
@var{mach}. The user-specific data to be associated with the new machine
are passed in @var{data} argument. The function returns 0 on success,
non-zero error code otherwise,
The @code{mu_sieve_machine_create()} function creates an instance of a sieve
machine. A pointer to the instance itself is returned in the argument
@var{mach}. The function returns 0 on success, and a non-zero error
code otherwise.
@end deftypefun
@deftypefun void mu_sieve_machine_destroy (mu_sieve_machine_t *@var{pmach})
......@@ -417,7 +416,7 @@ match_part_checker (const char *name, list_t tags, list_t args)
@deftypefun {void *} mu_sieve_get_data (mu_sieve_machine_t @var{mach})
This function returns the application-specific data associated with
the instance of sieve machine. See @code{mu_sieve_machine_init()}.
the instance of sieve machine. See @code{mu_sieve_machine_create()}.
@end deftypefun
@deftypefun mu_message_t mu_sieve_get_message (mu_sieve_machine_t @var{mach})
......
......@@ -226,14 +226,14 @@ int mu_sieve_vlist_compare (mu_sieve_machine_t mach,
void *data, size_t *count);
/* Functions to create and destroy sieve machine */
int mu_sieve_machine_init (mu_sieve_machine_t *mach);
int mu_sieve_machine_create (mu_sieve_machine_t *mach);
int mu_sieve_machine_dup (mu_sieve_machine_t const in,
mu_sieve_machine_t *out);
int mu_sieve_machine_inherit (mu_sieve_machine_t const in,
mu_sieve_machine_t *out);
void mu_sieve_machine_destroy (mu_sieve_machine_t *pmach);
int mu_sieve_machine_add_destructor (mu_sieve_machine_t mach,
mu_sieve_destructor_t destr, void *ptr);
void mu_sieve_machine_add_destructor (mu_sieve_machine_t mach,
mu_sieve_destructor_t destr, void *ptr);
/* Functions for accessing sieve machine internals */
void mu_sieve_get_diag_stream (mu_sieve_machine_t mach, mu_stream_t *pstr);
......
......@@ -81,7 +81,7 @@ sieve_init (const char *prog, mu_script_descr_t *pdescr)
int rc;
mu_sieve_machine_t mach;
rc = mu_sieve_machine_init (&mach);
rc = mu_sieve_machine_create (&mach);
if (rc == 0)
{
if (mu_script_sieve_log)
......
......@@ -25,7 +25,7 @@ using namespace mailutils;
SieveMachine :: SieveMachine ()
{
int status = mu_sieve_machine_init (&mach);
int status = mu_sieve_machine_create (&mach);
if (status)
throw Exception ("SieveMachine::SieveMachine", status);
}
......
......@@ -980,7 +980,7 @@ mu_i_sv_error (mu_sieve_machine_t mach)
}
int
mu_sieve_machine_init (mu_sieve_machine_t *pmach)
mu_sieve_machine_create (mu_sieve_machine_t *pmach)
{
int rc;
mu_sieve_machine_t mach;
......@@ -1010,6 +1010,70 @@ mu_sieve_machine_init (mu_sieve_machine_t *pmach)
return 0;
}
void
mu_i_sv_free_stringspace (mu_sieve_machine_t mach)
{
size_t i;
for (i = 0; i < mach->stringcount; i++)
{
if (mach->stringspace[i].rx)
{
regex_t *rx = mach->stringspace[i].rx;
regfree (rx);
}
/* FIXME: Is it needed?
if (mach->stringspace[i].exp)
free (mach->stringspace[i].exp);
*/
}
}
int
mu_sieve_machine_reset (mu_sieve_machine_t mach)
{
switch (mach->state)
{
case mu_sieve_state_init:
/* Nothing to do */
return 0;
case mu_sieve_state_error:
case mu_sieve_state_compiled:
/* Do the right thing */
break;
case mu_sieve_state_running:
case mu_sieve_state_disass:
/* Can't reset a running machine */
return MU_ERR_FAILURE;
}
mu_i_sv_free_stringspace (mach);
mu_list_clear (mach->memory_pool);
mu_list_clear (mach->destr_list);
mu_opool_free (mach->string_pool, NULL);
mu_list_clear (mach->source_list);
mu_list_clear (mach->test_list);
mu_list_clear (mach->action_list);
mu_list_clear (mach->comp_list);
mach->stringspace = NULL;
mach->stringcount = 0;
mach->stringmax = 0;
mach->valspace = NULL;
mach->valcount = 0;
mach->valmax = 0;
mach->progsize = 0;
mach->prog = NULL;
mach->state = mu_sieve_state_init;
return 0;
}
int
mu_sieve_machine_inherit (mu_sieve_machine_t const parent,
mu_sieve_machine_t *pmach)
......@@ -1020,7 +1084,7 @@ mu_sieve_machine_inherit (mu_sieve_machine_t const parent,
if (!parent || parent->state == mu_sieve_state_error)
return EINVAL;
rc = mu_sieve_machine_init (&child);
rc = mu_sieve_machine_create (&child);
if (rc)
return rc;
......@@ -1029,9 +1093,14 @@ mu_sieve_machine_inherit (mu_sieve_machine_t const parent,
child->state_flags = parent->state_flags;
child->err_mode = parent->err_mode;
child->err_locus = parent->err_locus;
if (child->err_locus.mu_file)
child->err_locus.mu_file =
mu_sieve_strdup (child, child->err_locus.mu_file);
child->dbg_mode = parent->dbg_mode;
child->dbg_locus = parent->dbg_locus;
if (child->dbg_locus.mu_file)
child->dbg_locus.mu_file =
mu_sieve_strdup (child, child->dbg_locus.mu_file);
child->errstream = parent->errstream;
mu_stream_ref (child->errstream);
child->dbgstream = parent->dbgstream;
......@@ -1213,54 +1282,61 @@ struct sieve_destr_record
void *ptr;
};
int
static void
run_destructor (void *data)
{
struct sieve_destr_record *p = data;
p->destr (p->ptr);
free (data);
}
void
mu_sieve_machine_add_destructor (mu_sieve_machine_t mach,
mu_sieve_destructor_t destr,
void *ptr)
{
int rc;
struct sieve_destr_record *p;
if (!mach->destr_list && mu_list_create (&mach->destr_list))
return 1;
p = mu_sieve_malloc (mach, sizeof (*p));
if (!mach->destr_list)
{
rc = mu_list_create (&mach->destr_list);
if (rc)
{
mu_sieve_error (mach, "mu_list_create: %s", mu_strerror (rc));
destr (ptr);
mu_sieve_abort (mach);
}
mu_list_set_destroy_item (mach->destr_list, run_destructor);
}
p = malloc (sizeof (*p));
if (!p)
return 1;
{
mu_sieve_error (mach, "%s", mu_strerror (errno));
destr (ptr);
mu_sieve_abort (mach);
}
p->destr = destr;
p->ptr = ptr;
return mu_list_prepend (mach->destr_list, p);
}
static int
_run_destructor (void *data, void *unused)
{
struct sieve_destr_record *p = data;
p->destr (p->ptr);
return 0;
rc = mu_list_prepend (mach->destr_list, p);
if (rc)
{
mu_sieve_error (mach, "mu_list_prepend: %s", mu_strerror (rc));
destr (ptr);
free (p);
mu_sieve_abort (mach);
}
}
void
mu_sieve_machine_destroy (mu_sieve_machine_t *pmach)
{
mu_sieve_machine_t mach = *pmach;
size_t i;
for (i = 0; i < mach->stringcount; i++)
{
if (mach->stringspace[i].rx)
{
regex_t *rx = mach->stringspace[i].rx;
regfree (rx);
}
/* FIXME: Is it needed?
if (mach->stringspace[i].exp)
free (mach->stringspace[i].exp);
*/
}
mu_i_sv_free_stringspace (mach);
mu_stream_destroy (&mach->errstream);
mu_stream_destroy (&mach->dbgstream);
mu_mailer_destroy (&mach->mailer);
mu_list_foreach (mach->destr_list, _run_destructor, NULL);
mu_list_destroy (&mach->destr_list);
mu_list_destroy (&mach->action_list);
mu_list_destroy (&mach->test_list);
......@@ -1279,6 +1355,10 @@ with_machine (mu_sieve_machine_t mach, char const *name,
int rc = 0;
mu_stream_t save_errstr;
rc = mu_sieve_machine_reset (mach);
if (rc)
return rc;
save_errstr = mu_strerr;
mu_stream_ref (save_errstr);
mu_strerr = mach->errstream;
......
......@@ -107,7 +107,7 @@ api_sieve_machine_init (PyObject *self, PyObject *args)
if (status)
return _ro (PyInt_FromLong (status));
status = mu_sieve_machine_init (&py_mach->mach);
status = mu_sieve_machine_create (&py_mach->mach);
if (status == 0)
mu_sieve_set_diag_stream (py_mach->mach, estr);
mu_stream_unref (estr);
......
......@@ -431,7 +431,7 @@ main (int argc, char *argv[])
}
/* Sieve interpreter setup. */
rc = mu_sieve_machine_init (&mach);
rc = mu_sieve_machine_create (&mach);
if (rc)
{
mu_error (_("cannot initialize sieve machine: %s"), mu_strerror (rc));
......