Rename mu_sieve_machine_init to mu_sieve_machine_create
Showing
7 changed files
with
129 additions
and
50 deletions
... | @@ -222,8 +222,8 @@ typedef int (*mu_sieve_printf_t) (void *@var{data}, | ... | @@ -222,8 +222,8 @@ typedef int (*mu_sieve_printf_t) (void *@var{data}, |
222 | 222 | ||
223 | @table @var | 223 | @table @var |
224 | @item data | 224 | @item data |
225 | A pointer to application specific data. These data are passed as | 225 | A pointer to application specific data. It is set using |
226 | second argument to @code{mu_sieve_machine_init()}. | 226 | the @code{mu_sieve_set_data} call. |
227 | @item fmt | 227 | @item fmt |
228 | Printf-like format string. | 228 | Printf-like format string. |
229 | @item ap | 229 | @item ap |
... | @@ -362,13 +362,12 @@ case. [FIXME: describe how to do that] | ... | @@ -362,13 +362,12 @@ case. [FIXME: describe how to do that] |
362 | This subsection describes functions used to create an instance of the | 362 | This subsection describes functions used to create an instance of the |
363 | sieve machine, read or alter its internal fields and destroy it. | 363 | sieve machine, read or alter its internal fields and destroy it. |
364 | 364 | ||
365 | @deftypefun int mu_sieve_machine_init (mu_sieve_machine_t *@var{mach}, void *@var{data}) | 365 | @deftypefun int mu_sieve_machine_create (mu_sieve_machine_t *@var{mach}) |
366 | 366 | ||
367 | The @code{mu_sieve_machine_init()} function creates an instance of a sieve | 367 | The @code{mu_sieve_machine_create()} function creates an instance of a sieve |
368 | machine. A pointer to the instance itself is returned in the argument | 368 | machine. A pointer to the instance itself is returned in the argument |
369 | @var{mach}. The user-specific data to be associated with the new machine | 369 | @var{mach}. The function returns 0 on success, and a non-zero error |
370 | are passed in @var{data} argument. The function returns 0 on success, | 370 | code otherwise. |
371 | non-zero error code otherwise, | ||
372 | @end deftypefun | 371 | @end deftypefun |
373 | 372 | ||
374 | @deftypefun void mu_sieve_machine_destroy (mu_sieve_machine_t *@var{pmach}) | 373 | @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) | ... | @@ -417,7 +416,7 @@ match_part_checker (const char *name, list_t tags, list_t args) |
417 | 416 | ||
418 | @deftypefun {void *} mu_sieve_get_data (mu_sieve_machine_t @var{mach}) | 417 | @deftypefun {void *} mu_sieve_get_data (mu_sieve_machine_t @var{mach}) |
419 | This function returns the application-specific data associated with | 418 | This function returns the application-specific data associated with |
420 | the instance of sieve machine. See @code{mu_sieve_machine_init()}. | 419 | the instance of sieve machine. See @code{mu_sieve_machine_create()}. |
421 | @end deftypefun | 420 | @end deftypefun |
422 | 421 | ||
423 | @deftypefun mu_message_t mu_sieve_get_message (mu_sieve_machine_t @var{mach}) | 422 | @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, | ... | @@ -226,14 +226,14 @@ int mu_sieve_vlist_compare (mu_sieve_machine_t mach, |
226 | void *data, size_t *count); | 226 | void *data, size_t *count); |
227 | 227 | ||
228 | /* Functions to create and destroy sieve machine */ | 228 | /* Functions to create and destroy sieve machine */ |
229 | int mu_sieve_machine_init (mu_sieve_machine_t *mach); | 229 | int mu_sieve_machine_create (mu_sieve_machine_t *mach); |
230 | int mu_sieve_machine_dup (mu_sieve_machine_t const in, | 230 | int mu_sieve_machine_dup (mu_sieve_machine_t const in, |
231 | mu_sieve_machine_t *out); | 231 | mu_sieve_machine_t *out); |
232 | int mu_sieve_machine_inherit (mu_sieve_machine_t const in, | 232 | int mu_sieve_machine_inherit (mu_sieve_machine_t const in, |
233 | mu_sieve_machine_t *out); | 233 | mu_sieve_machine_t *out); |
234 | void mu_sieve_machine_destroy (mu_sieve_machine_t *pmach); | 234 | void mu_sieve_machine_destroy (mu_sieve_machine_t *pmach); |
235 | int mu_sieve_machine_add_destructor (mu_sieve_machine_t mach, | 235 | void mu_sieve_machine_add_destructor (mu_sieve_machine_t mach, |
236 | mu_sieve_destructor_t destr, void *ptr); | 236 | mu_sieve_destructor_t destr, void *ptr); |
237 | 237 | ||
238 | /* Functions for accessing sieve machine internals */ | 238 | /* Functions for accessing sieve machine internals */ |
239 | void mu_sieve_get_diag_stream (mu_sieve_machine_t mach, mu_stream_t *pstr); | 239 | 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) | ... | @@ -81,7 +81,7 @@ sieve_init (const char *prog, mu_script_descr_t *pdescr) |
81 | int rc; | 81 | int rc; |
82 | mu_sieve_machine_t mach; | 82 | mu_sieve_machine_t mach; |
83 | 83 | ||
84 | rc = mu_sieve_machine_init (&mach); | 84 | rc = mu_sieve_machine_create (&mach); |
85 | if (rc == 0) | 85 | if (rc == 0) |
86 | { | 86 | { |
87 | if (mu_script_sieve_log) | 87 | if (mu_script_sieve_log) | ... | ... |
... | @@ -25,7 +25,7 @@ using namespace mailutils; | ... | @@ -25,7 +25,7 @@ using namespace mailutils; |
25 | 25 | ||
26 | SieveMachine :: SieveMachine () | 26 | SieveMachine :: SieveMachine () |
27 | { | 27 | { |
28 | int status = mu_sieve_machine_init (&mach); | 28 | int status = mu_sieve_machine_create (&mach); |
29 | if (status) | 29 | if (status) |
30 | throw Exception ("SieveMachine::SieveMachine", status); | 30 | throw Exception ("SieveMachine::SieveMachine", status); |
31 | } | 31 | } | ... | ... |
... | @@ -980,7 +980,7 @@ mu_i_sv_error (mu_sieve_machine_t mach) | ... | @@ -980,7 +980,7 @@ mu_i_sv_error (mu_sieve_machine_t mach) |
980 | } | 980 | } |
981 | 981 | ||
982 | int | 982 | int |
983 | mu_sieve_machine_init (mu_sieve_machine_t *pmach) | 983 | mu_sieve_machine_create (mu_sieve_machine_t *pmach) |
984 | { | 984 | { |
985 | int rc; | 985 | int rc; |
986 | mu_sieve_machine_t mach; | 986 | mu_sieve_machine_t mach; |
... | @@ -1010,6 +1010,70 @@ mu_sieve_machine_init (mu_sieve_machine_t *pmach) | ... | @@ -1010,6 +1010,70 @@ mu_sieve_machine_init (mu_sieve_machine_t *pmach) |
1010 | return 0; | 1010 | return 0; |
1011 | } | 1011 | } |
1012 | 1012 | ||
1013 | void | ||
1014 | mu_i_sv_free_stringspace (mu_sieve_machine_t mach) | ||
1015 | { | ||
1016 | size_t i; | ||
1017 | |||
1018 | for (i = 0; i < mach->stringcount; i++) | ||
1019 | { | ||
1020 | if (mach->stringspace[i].rx) | ||
1021 | { | ||
1022 | regex_t *rx = mach->stringspace[i].rx; | ||
1023 | regfree (rx); | ||
1024 | } | ||
1025 | /* FIXME: Is it needed? | ||
1026 | if (mach->stringspace[i].exp) | ||
1027 | free (mach->stringspace[i].exp); | ||
1028 | */ | ||
1029 | } | ||
1030 | } | ||
1031 | |||
1032 | int | ||
1033 | mu_sieve_machine_reset (mu_sieve_machine_t mach) | ||
1034 | { | ||
1035 | switch (mach->state) | ||
1036 | { | ||
1037 | case mu_sieve_state_init: | ||
1038 | /* Nothing to do */ | ||
1039 | return 0; | ||
1040 | |||
1041 | case mu_sieve_state_error: | ||
1042 | case mu_sieve_state_compiled: | ||
1043 | /* Do the right thing */ | ||
1044 | break; | ||
1045 | |||
1046 | case mu_sieve_state_running: | ||
1047 | case mu_sieve_state_disass: | ||
1048 | /* Can't reset a running machine */ | ||
1049 | return MU_ERR_FAILURE; | ||
1050 | } | ||
1051 | |||
1052 | mu_i_sv_free_stringspace (mach); | ||
1053 | mu_list_clear (mach->memory_pool); | ||
1054 | mu_list_clear (mach->destr_list); | ||
1055 | mu_opool_free (mach->string_pool, NULL); | ||
1056 | mu_list_clear (mach->source_list); | ||
1057 | mu_list_clear (mach->test_list); | ||
1058 | mu_list_clear (mach->action_list); | ||
1059 | mu_list_clear (mach->comp_list); | ||
1060 | |||
1061 | mach->stringspace = NULL; | ||
1062 | mach->stringcount = 0; | ||
1063 | mach->stringmax = 0; | ||
1064 | |||
1065 | mach->valspace = NULL; | ||
1066 | mach->valcount = 0; | ||
1067 | mach->valmax = 0; | ||
1068 | |||
1069 | mach->progsize = 0; | ||
1070 | mach->prog = NULL; | ||
1071 | |||
1072 | mach->state = mu_sieve_state_init; | ||
1073 | |||
1074 | return 0; | ||
1075 | } | ||
1076 | |||
1013 | int | 1077 | int |
1014 | mu_sieve_machine_inherit (mu_sieve_machine_t const parent, | 1078 | mu_sieve_machine_inherit (mu_sieve_machine_t const parent, |
1015 | mu_sieve_machine_t *pmach) | 1079 | mu_sieve_machine_t *pmach) |
... | @@ -1020,7 +1084,7 @@ mu_sieve_machine_inherit (mu_sieve_machine_t const parent, | ... | @@ -1020,7 +1084,7 @@ mu_sieve_machine_inherit (mu_sieve_machine_t const parent, |
1020 | if (!parent || parent->state == mu_sieve_state_error) | 1084 | if (!parent || parent->state == mu_sieve_state_error) |
1021 | return EINVAL; | 1085 | return EINVAL; |
1022 | 1086 | ||
1023 | rc = mu_sieve_machine_init (&child); | 1087 | rc = mu_sieve_machine_create (&child); |
1024 | if (rc) | 1088 | if (rc) |
1025 | return rc; | 1089 | return rc; |
1026 | 1090 | ||
... | @@ -1029,9 +1093,14 @@ mu_sieve_machine_inherit (mu_sieve_machine_t const parent, | ... | @@ -1029,9 +1093,14 @@ mu_sieve_machine_inherit (mu_sieve_machine_t const parent, |
1029 | child->state_flags = parent->state_flags; | 1093 | child->state_flags = parent->state_flags; |
1030 | child->err_mode = parent->err_mode; | 1094 | child->err_mode = parent->err_mode; |
1031 | child->err_locus = parent->err_locus; | 1095 | child->err_locus = parent->err_locus; |
1096 | if (child->err_locus.mu_file) | ||
1097 | child->err_locus.mu_file = | ||
1098 | mu_sieve_strdup (child, child->err_locus.mu_file); | ||
1032 | child->dbg_mode = parent->dbg_mode; | 1099 | child->dbg_mode = parent->dbg_mode; |
1033 | child->dbg_locus = parent->dbg_locus; | 1100 | child->dbg_locus = parent->dbg_locus; |
1034 | 1101 | if (child->dbg_locus.mu_file) | |
1102 | child->dbg_locus.mu_file = | ||
1103 | mu_sieve_strdup (child, child->dbg_locus.mu_file); | ||
1035 | child->errstream = parent->errstream; | 1104 | child->errstream = parent->errstream; |
1036 | mu_stream_ref (child->errstream); | 1105 | mu_stream_ref (child->errstream); |
1037 | child->dbgstream = parent->dbgstream; | 1106 | child->dbgstream = parent->dbgstream; |
... | @@ -1213,54 +1282,61 @@ struct sieve_destr_record | ... | @@ -1213,54 +1282,61 @@ struct sieve_destr_record |
1213 | void *ptr; | 1282 | void *ptr; |
1214 | }; | 1283 | }; |
1215 | 1284 | ||
1216 | int | 1285 | static void |
1286 | run_destructor (void *data) | ||
1287 | { | ||
1288 | struct sieve_destr_record *p = data; | ||
1289 | p->destr (p->ptr); | ||
1290 | free (data); | ||
1291 | } | ||
1292 | |||
1293 | void | ||
1217 | mu_sieve_machine_add_destructor (mu_sieve_machine_t mach, | 1294 | mu_sieve_machine_add_destructor (mu_sieve_machine_t mach, |
1218 | mu_sieve_destructor_t destr, | 1295 | mu_sieve_destructor_t destr, |
1219 | void *ptr) | 1296 | void *ptr) |
1220 | { | 1297 | { |
1298 | int rc; | ||
1221 | struct sieve_destr_record *p; | 1299 | struct sieve_destr_record *p; |
1222 | 1300 | ||
1223 | if (!mach->destr_list && mu_list_create (&mach->destr_list)) | 1301 | if (!mach->destr_list) |
1224 | return 1; | 1302 | { |
1225 | p = mu_sieve_malloc (mach, sizeof (*p)); | 1303 | rc = mu_list_create (&mach->destr_list); |
1304 | if (rc) | ||
1305 | { | ||
1306 | mu_sieve_error (mach, "mu_list_create: %s", mu_strerror (rc)); | ||
1307 | destr (ptr); | ||
1308 | mu_sieve_abort (mach); | ||
1309 | } | ||
1310 | mu_list_set_destroy_item (mach->destr_list, run_destructor); | ||
1311 | } | ||
1312 | p = malloc (sizeof (*p)); | ||
1226 | if (!p) | 1313 | if (!p) |
1227 | return 1; | 1314 | { |
1315 | mu_sieve_error (mach, "%s", mu_strerror (errno)); | ||
1316 | destr (ptr); | ||
1317 | mu_sieve_abort (mach); | ||
1318 | } | ||
1228 | p->destr = destr; | 1319 | p->destr = destr; |
1229 | p->ptr = ptr; | 1320 | p->ptr = ptr; |
1230 | return mu_list_prepend (mach->destr_list, p); | 1321 | rc = mu_list_prepend (mach->destr_list, p); |
1231 | } | 1322 | if (rc) |
1232 | 1323 | { | |
1233 | static int | 1324 | mu_sieve_error (mach, "mu_list_prepend: %s", mu_strerror (rc)); |
1234 | _run_destructor (void *data, void *unused) | 1325 | destr (ptr); |
1235 | { | 1326 | free (p); |
1236 | struct sieve_destr_record *p = data; | 1327 | mu_sieve_abort (mach); |
1237 | p->destr (p->ptr); | 1328 | } |
1238 | return 0; | ||
1239 | } | 1329 | } |
1240 | 1330 | ||
1241 | void | 1331 | void |
1242 | mu_sieve_machine_destroy (mu_sieve_machine_t *pmach) | 1332 | mu_sieve_machine_destroy (mu_sieve_machine_t *pmach) |
1243 | { | 1333 | { |
1244 | mu_sieve_machine_t mach = *pmach; | 1334 | mu_sieve_machine_t mach = *pmach; |
1245 | size_t i; | 1335 | |
1246 | 1336 | mu_i_sv_free_stringspace (mach); | |
1247 | for (i = 0; i < mach->stringcount; i++) | ||
1248 | { | ||
1249 | if (mach->stringspace[i].rx) | ||
1250 | { | ||
1251 | regex_t *rx = mach->stringspace[i].rx; | ||
1252 | regfree (rx); | ||
1253 | } | ||
1254 | /* FIXME: Is it needed? | ||
1255 | if (mach->stringspace[i].exp) | ||
1256 | free (mach->stringspace[i].exp); | ||
1257 | */ | ||
1258 | } | ||
1259 | |||
1260 | mu_stream_destroy (&mach->errstream); | 1337 | mu_stream_destroy (&mach->errstream); |
1261 | mu_stream_destroy (&mach->dbgstream); | 1338 | mu_stream_destroy (&mach->dbgstream); |
1262 | mu_mailer_destroy (&mach->mailer); | 1339 | mu_mailer_destroy (&mach->mailer); |
1263 | mu_list_foreach (mach->destr_list, _run_destructor, NULL); | ||
1264 | mu_list_destroy (&mach->destr_list); | 1340 | mu_list_destroy (&mach->destr_list); |
1265 | mu_list_destroy (&mach->action_list); | 1341 | mu_list_destroy (&mach->action_list); |
1266 | mu_list_destroy (&mach->test_list); | 1342 | mu_list_destroy (&mach->test_list); |
... | @@ -1279,6 +1355,10 @@ with_machine (mu_sieve_machine_t mach, char const *name, | ... | @@ -1279,6 +1355,10 @@ with_machine (mu_sieve_machine_t mach, char const *name, |
1279 | int rc = 0; | 1355 | int rc = 0; |
1280 | mu_stream_t save_errstr; | 1356 | mu_stream_t save_errstr; |
1281 | 1357 | ||
1358 | rc = mu_sieve_machine_reset (mach); | ||
1359 | if (rc) | ||
1360 | return rc; | ||
1361 | |||
1282 | save_errstr = mu_strerr; | 1362 | save_errstr = mu_strerr; |
1283 | mu_stream_ref (save_errstr); | 1363 | mu_stream_ref (save_errstr); |
1284 | mu_strerr = mach->errstream; | 1364 | mu_strerr = mach->errstream; | ... | ... |
... | @@ -107,7 +107,7 @@ api_sieve_machine_init (PyObject *self, PyObject *args) | ... | @@ -107,7 +107,7 @@ api_sieve_machine_init (PyObject *self, PyObject *args) |
107 | if (status) | 107 | if (status) |
108 | return _ro (PyInt_FromLong (status)); | 108 | return _ro (PyInt_FromLong (status)); |
109 | 109 | ||
110 | status = mu_sieve_machine_init (&py_mach->mach); | 110 | status = mu_sieve_machine_create (&py_mach->mach); |
111 | if (status == 0) | 111 | if (status == 0) |
112 | mu_sieve_set_diag_stream (py_mach->mach, estr); | 112 | mu_sieve_set_diag_stream (py_mach->mach, estr); |
113 | mu_stream_unref (estr); | 113 | mu_stream_unref (estr); | ... | ... |
... | @@ -431,7 +431,7 @@ main (int argc, char *argv[]) | ... | @@ -431,7 +431,7 @@ main (int argc, char *argv[]) |
431 | } | 431 | } |
432 | 432 | ||
433 | /* Sieve interpreter setup. */ | 433 | /* Sieve interpreter setup. */ |
434 | rc = mu_sieve_machine_init (&mach); | 434 | rc = mu_sieve_machine_create (&mach); |
435 | if (rc) | 435 | if (rc) |
436 | { | 436 | { |
437 | mu_error (_("cannot initialize sieve machine: %s"), mu_strerror (rc)); | 437 | mu_error (_("cannot initialize sieve machine: %s"), mu_strerror (rc)); | ... | ... |
-
Please register or sign in to post a comment