diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-12-21 15:57:21 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-12-21 15:57:57 +0200 |
commit | 43ba0c3da9ff8913f0286a01a82875fa59601dc0 (patch) | |
tree | fac6765cc2fb90ae39f5eb47d2c41abcaf93fb6d /src | |
parent | 30e1c54062fe7098b9c71601370df1ce27f873d3 (diff) | |
download | wydawca-43ba0c3da9ff8913f0286a01a82875fa59601dc0.tar.gz wydawca-43ba0c3da9ff8913f0286a01a82875fa59601dc0.tar.bz2 |
Namespace cleanup.
Rename "access method" to "dictionary".
All sources affected.
* src/method.c: renamed to...
* src/dictionary.c: ... this.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/builtin.c | 46 | ||||
-rw-r--r-- | src/builtin.h | 12 | ||||
-rw-r--r-- | src/config.c | 104 | ||||
-rw-r--r-- | src/dictionary.c | 228 | ||||
-rw-r--r-- | src/mail.c | 28 | ||||
-rw-r--r-- | src/meta.c | 10 | ||||
-rw-r--r-- | src/method.c | 226 | ||||
-rw-r--r-- | src/process.c | 14 | ||||
-rw-r--r-- | src/sql.c | 44 | ||||
-rw-r--r-- | src/sql.h | 20 | ||||
-rw-r--r-- | src/triplet.c | 8 | ||||
-rw-r--r-- | src/verify.c | 32 | ||||
-rw-r--r-- | src/wydawca.h | 60 |
14 files changed, 418 insertions, 416 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index c96fbcb..a599118 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,6 +20,7 @@ wydawca_SOURCES=\ builtin.h\ cmdline.h\ config.c\ + dictionary.c\ directive.c\ diskio.c\ exec.c\ @@ -28,7 +29,6 @@ wydawca_SOURCES=\ job.c\ lock.c\ meta.c\ - method.c\ net.c\ pidfile.c\ process.c\ diff --git a/src/builtin.c b/src/builtin.c index 73b1d23..5588fd6 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -20,21 +20,21 @@ #include "regex.h" int -builtin_init (struct access_method *meth) +builtin_init (struct dictionary *dict) { return 0; } int -builtin_done (struct access_method *meth) +builtin_done (struct dictionary *dict) { return 0; } void * -builtin_open (struct access_method *meth) +builtin_open (struct dictionary *dict) { - return meth; + return dict; } #define CMP_EXACT 0 @@ -159,7 +159,7 @@ static int default_ncol[] = { }; int -builtin_run (struct access_method *meth, void *handle, const char *req) +builtin_lookup (struct dictionary *dict, void *handle, const char *req) { int i; int rc; @@ -168,19 +168,19 @@ builtin_run (struct access_method *meth, void *handle, const char *req) int flags = 0; strcmp_fn cmpfn = cmp_exact; struct builtin_data_storage *bds; - int ncol = default_ncol[meth->id]; + int ncol = default_ncol[dict->id]; - if (meth->parmc == 0) + if (dict->parmc == 0) { - meth->nrow = meth->ncol = 0; + dict->nrow = dict->ncol = 0; return 0; } obstack_init (&stk); - for (i = 0; i < meth->parmc; i++) + for (i = 0; i < dict->parmc; i++) { - char *pat = meth->parmv[i]; + char *pat = dict->parmv[i]; if (pat[0] == '/') { @@ -189,7 +189,7 @@ builtin_run (struct access_method *meth, void *handle, const char *req) continue; } - if (i + ncol >= meth->parmc) + if (i + ncol >= dict->parmc) break; if (cmpfn (pat, req, flags)) @@ -197,7 +197,7 @@ builtin_run (struct access_method *meth, void *handle, const char *req) size_t j; for (j = 1; j <= ncol; j++) { - char *val = meth->parmv[i + j]; + char *val = dict->parmv[i + j]; obstack_grow (&stk, val, strlen (val) + 1); } count++; @@ -206,8 +206,8 @@ builtin_run (struct access_method *meth, void *handle, const char *req) i += ncol; } - meth->nrow = count; - meth->ncol = ncol; + dict->nrow = count; + dict->ncol = ncol; if (count == 0) { @@ -234,32 +234,32 @@ builtin_run (struct access_method *meth, void *handle, const char *req) rc = 0; } - meth->storage = bds; + dict->storage = bds; return rc; } int -builtin_free_result (struct access_method *method, void *handle) +builtin_free_result (struct dictionary *dict, void *handle) { - if (method->storage) + if (dict->storage) { - struct builtin_data_storage *bds = method->storage; + struct builtin_data_storage *bds = dict->storage; obstack_free (&bds->stk, NULL); free (bds->wp); free (bds); - method->storage = NULL; + dict->storage = NULL; } return 0; } int -builtin_get (struct access_method *method, void *handle, +builtin_get (struct dictionary *dict, void *handle, unsigned nrow, unsigned ncol) { - struct builtin_data_storage *bds = method->storage; - char *str = bds->wp[nrow * method->ncol + ncol]; - method_copy_result (method, str, strlen (str)); + struct builtin_data_storage *bds = dict->storage; + char *str = bds->wp[nrow * dict->ncol + ncol]; + dictionary_copy_result (dict, str, strlen (str)); return 0; } diff --git a/src/builtin.h b/src/builtin.h index 500a6cb..dd52bce 100644 --- a/src/builtin.h +++ b/src/builtin.h @@ -14,9 +14,9 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -int builtin_init (struct access_method *); -int builtin_done (struct access_method *); -void *builtin_open (struct access_method *); -int builtin_get (struct access_method *, void *, unsigned, unsigned); -int builtin_run (struct access_method *, void *, const char *); -int builtin_free_result (struct access_method *, void *); +int builtin_init (struct dictionary *); +int builtin_done (struct dictionary *); +void *builtin_open (struct dictionary *); +int builtin_get (struct dictionary *, void *, unsigned, unsigned); +int builtin_lookup (struct dictionary *, void *, const char *); +int builtin_free_result (struct dictionary *, void *); diff --git a/src/config.c b/src/config.c index f0a7042..15c191c 100644 --- a/src/config.c +++ b/src/config.c @@ -26,7 +26,7 @@ static struct archive_descr default_archive_descr = { no_backups }; -static struct access_method *default_access_method[access_method_count]; +static struct dictionary *default_dictionary[dictionary_count]; static struct notification *default_notification = NULL; @@ -953,44 +953,44 @@ cb_notify_event (enum grecs_callback_command cmd, -static enum access_method_type -string_to_access_method_type (const char *str) +static enum dictionary_type +string_to_dictionary_type (const char *str) { if (strcmp (str, "sql") == 0) - return method_sql; + return dictionary_sql; else if (strcmp (str, "builtin") == 0) - return method_builtin; + return dictionary_builtin; else if (strcmp (str, "external") == 0) - return method_external; + return dictionary_external; else - return method_none; + return dictionary_none; } static int -cb_access_method_type (enum grecs_callback_command cmd, +cb_dictionary_type (enum grecs_callback_command cmd, grecs_locus_t *locus, void *varptr, grecs_value_t *value, void *cb_data) { - enum access_method_type *ptype = varptr; + enum dictionary_type *ptype = varptr; if (assert_string_arg (locus, cmd, value)) return 1; - *ptype = string_to_access_method_type (value->v.string); - if (*ptype == method_none) - grecs_error (locus, 0, _("unknown access method: %s"), value->v.string); + *ptype = string_to_dictionary_type (value->v.string); + if (*ptype == dictionary_none) + grecs_error (locus, 0, _("unknown dictionary type: %s"), value->v.string); return 0; } static int -cb_access_method_params (enum grecs_callback_command cmd, +cb_dictionary_params (enum grecs_callback_command cmd, grecs_locus_t *locus, void *varptr, grecs_value_t *value, void *cb_data) { - struct access_method *meth = varptr; + struct dictionary *meth = varptr; size_t size; if (cmd != grecs_callback_set_value) @@ -1034,21 +1034,21 @@ cb_access_method_params (enum grecs_callback_command cmd, return 0; } -static struct grecs_keyword access_method_kw[] = { - { "type", N_("type"), N_("Method type"), - grecs_type_string, NULL, offsetof(struct access_method, type), - cb_access_method_type }, +static struct grecs_keyword dictionary_kw[] = { + { "type", N_("type"), N_("Dictionary type"), + grecs_type_string, NULL, offsetof(struct dictionary, type), + cb_dictionary_type }, { "query", N_("string"), N_("Query template"), - grecs_type_string, NULL, offsetof(struct access_method, query) }, - { "params", N_("arg"), N_("Set method parameters"), + grecs_type_string, NULL, offsetof(struct dictionary, query) }, + { "params", N_("arg"), N_("Set dictionary parameters"), grecs_type_string|GRECS_LIST, NULL, 0, - cb_access_method_params }, + cb_dictionary_params }, { NULL } }; int -string_to_access_method_id (grecs_locus_t *locus, - const char *str, enum access_method_id *idp) +string_to_dictionary_id (grecs_locus_t *locus, + const char *str, enum dictionary_id *idp) { static const char *id_str[] = { "project-uploader", @@ -1056,13 +1056,13 @@ string_to_access_method_id (grecs_locus_t *locus, NULL }; static int id_num[] = { - project_uploader_method, - project_owner_method + project_uploader_dict, + project_owner_dict }; ARGMATCH_VERIFY (id_str, id_num); int rc, res; - rc = string_to ("access method", str, + rc = string_to ("dictionary", str, id_str, id_num, &res, locus); @@ -1071,15 +1071,15 @@ string_to_access_method_id (grecs_locus_t *locus, } static int -cb_access_method (enum grecs_callback_command cmd, - grecs_locus_t *locus, - void *varptr, - grecs_value_t *value, - void *cb_data) +cb_dictionary (enum grecs_callback_command cmd, + grecs_locus_t *locus, + void *varptr, + grecs_value_t *value, + void *cb_data) { - struct access_method **pmeth, *meth; + struct dictionary **pmeth, *meth; void **pdata = cb_data; - enum access_method_id id; + enum dictionary_id id; switch (cmd) { case grecs_callback_section_begin: @@ -1088,10 +1088,10 @@ cb_access_method (enum grecs_callback_command cmd, grecs_error(locus, 0, _("tag must be a string")); return 0; } - if (string_to_access_method_id (locus, value->v.string, &id)) + if (string_to_dictionary_id (locus, value->v.string, &id)) return 1; - pmeth = (struct access_method **) varptr + id; - *pmeth = method_new (id, method_builtin); + pmeth = (struct dictionary **) varptr + id; + *pmeth = dictionary_new (id, dictionary_builtin); *pdata = *pmeth; break; @@ -1099,17 +1099,17 @@ cb_access_method (enum grecs_callback_command cmd, meth = *pdata; switch (meth->type) { - case method_sql: + case dictionary_sql: if (meth->parmc == 0 || !meth->parmv[0]) { grecs_error (locus, 0, _("SQL connection is not declared")); - meth->type = method_none; + meth->type = dictionary_none; } else if (!sql_connection_exists_p (meth->parmv[0])) { grecs_error (locus, 0, _("SQL connection `%s' not declared"), meth->parmv[0]); - meth->type = method_none; + meth->type = dictionary_none; } break; @@ -1171,9 +1171,9 @@ static struct grecs_keyword spool_kw[] = { { "file-sweep-time", N_("interval"), N_("Define file sweep time"), grecs_type_string, NULL, offsetof(struct spool, file_sweep_time), cb_interval }, - { "access-method", N_("ident"), N_("Define access method"), - grecs_type_section, NULL, offsetof(struct spool, access_method), - cb_access_method, NULL, access_method_kw }, + { "dictionary", N_("ident"), N_("Define data dictionary"), + grecs_type_section, NULL, offsetof(struct spool, dictionary), + cb_dictionary, NULL, dictionary_kw }, { "archive", N_("type: string"), N_("Set up archivation"), grecs_type_section, NULL, offsetof(struct spool, archive), cb_archive, NULL, archive_kw }, @@ -1208,8 +1208,8 @@ cb_spool (enum grecs_callback_command cmd, spool = xzalloc (sizeof (*spool)); spool->tag = xstrdup (value->v.string); spool->file_sweep_time = file_sweep_time; - for (i = 0; i < NITEMS (spool->access_method); i++) - spool->access_method[i] = default_access_method[i]; + for (i = 0; i < NITEMS (spool->dictionary); i++) + spool->dictionary[i] = default_dictionary[i]; spool->archive = default_archive_descr; *pdata = spool; break; @@ -1247,11 +1247,11 @@ cb_spool (enum grecs_callback_command cmd, && spool->vtab.test_url (spool->dest_url, locus)) rc = 1; - for (i = 0; i < access_method_count; i++) - if (spool->access_method[i]->type == method_external) + for (i = 0; i < dictionary_count; i++) + if (spool->dictionary[i]->type == dictionary_external) { grecs_error (locus, 0, - _("Sorry, method type `external' is not yet supported")); + _("Sorry, the dictionary type `external' is not yet supported")); rc = 1; } @@ -1452,9 +1452,9 @@ static struct grecs_keyword wydawca_kw[] = { grecs_type_section, &default_notification, 0, cb_notify_event, NULL, notify_event_kw }, - { "access-method", N_("ident"), N_("Define access method"), - grecs_type_section, default_access_method, 0, - cb_access_method, NULL, access_method_kw }, + { "dictionary", N_("ident"), N_("Define data dictionary"), + grecs_type_section, default_dictionary, 0, + cb_dictionary, NULL, dictionary_kw }, { "spool", N_("tag: string"), N_("Define distribution spool"), grecs_type_section, NULL, 0, @@ -1495,6 +1495,6 @@ config_init() if (serv != NULL) grecs_default_port = serv->s_port; - for (i = 0; i < access_method_count; i++) - default_access_method[i] = method_new (i, method_builtin); + for (i = 0; i < dictionary_count; i++) + default_dictionary[i] = dictionary_new (i, dictionary_builtin); } diff --git a/src/dictionary.c b/src/dictionary.c new file mode 100644 index 0000000..c31baf8 --- /dev/null +++ b/src/dictionary.c @@ -0,0 +1,228 @@ +/* wydawca - automatic release submission daemon + Copyright (C) 2007 Sergey Poznyakoff + + Wydawca is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3 of the License, or (at your + option) any later version. + + Wydawca is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with wydawca. If not, see <http://www.gnu.org/licenses/>. */ + +#include "wydawca.h" +#include "sql.h" +#include "builtin.h" + +struct dictionary_descr +{ + const char *name; + + int (*init) (struct dictionary *); + int (*done) (struct dictionary *); + int (*free) (struct dictionary *, void *); + + void *(*open) (struct dictionary *); + int (*close) (struct dictionary *, void *); + + int (*get) (struct dictionary *, void *, unsigned, unsigned); + int (*lookup) (struct dictionary *, void *, const char *); + int (*quote) (struct dictionary *, void *, const char *, char **, size_t *); +}; + +static struct dictionary_descr dictionary_tab[] = { + { "none", NULL, NULL, NULL, NULL, NULL, NULL, NULL }, + { "sql", sql_init_dictionary, sql_done_dictionary, sql_free_result, + sql_open, NULL, sql_get_dictionary, sql_lookup_dictionary, sql_quote }, + { "builtin", builtin_init, builtin_done, builtin_free_result, + builtin_open, NULL, + builtin_get, + builtin_lookup }, + { "external", NULL, NULL, NULL, NULL, NULL, NULL, NULL } +}; + +struct dictionary * +dictionary_new (enum dictionary_id id, enum dictionary_type type) +{ + struct dictionary *mp = xmalloc (sizeof mp[0]); + memset (mp, 0, sizeof mp[0]); + mp->id = id; + mp->type = type; + return mp; +} + +int +dictionary_init (struct dictionary *dict) +{ + struct dictionary_descr *mp = dictionary_tab + dict->type; + int rc = 0; + + if (dict->init_passed++) + return 0; + if (debug_level > 1) + { + int i; + logmsg (LOG_DEBUG, _("initializing dictionary: %s \"%s\""), + mp->name, SP (dict->query)); + for (i = 0; i < dict->parmc; i++) + logmsg (LOG_DEBUG, " parmv[%d]=%s", i, dict->parmv[i]); + } + if (mp->init) + rc = mp->init (dict); + if (rc == 0) + dict->init_passed = 1; + return rc; +} + +void * +dictionary_open (struct dictionary *dict) +{ + struct dictionary_descr *mp = dictionary_tab + dict->type; + + if (!mp->open) + return NULL; + return mp->open (dict); +} + +int +dictionary_close (struct dictionary *dict, void *handle) +{ + struct dictionary_descr *mp = dictionary_tab + dict->type; + if (!mp->close) + return 0; + return mp->close (dict, handle); +} + +int +dictionary_done (struct dictionary *dict) +{ + struct dictionary_descr *mp = dictionary_tab + dict->type; + int rc = 0; + + if (dict->init_passed == 0) + return 0; + if (--dict->init_passed) + return 0; + if (debug_level > 1) + { + int i; + logmsg (LOG_DEBUG, _("closing dictionary: %s \"%s\""), + mp->name, SP (dict->query)); + for (i = 0; i < dict->parmc; i++) + logmsg (LOG_DEBUG, " parmv[%d]=%s", i, dict->parmv[i]); + } + if (mp->done) + rc = mp->done (dict); + free (dict->result); + dict->result = NULL; + dict->result_size = 0; + return rc; +} + +int +dictionary_lookup (struct dictionary *dict, void *handle, const char *cmd) +{ + struct dictionary_descr *mp = dictionary_tab + dict->type; + + if (debug_level > 1) + { + if (cmd) + logmsg (LOG_DEBUG, _("dictionary lookup: %s \"%s\""), + mp->name, cmd); + else + logmsg (LOG_DEBUG, _("dictionary lookup: %s"), mp->name); + } + + if (!dict->init_passed) + { + logmsg (LOG_CRIT, + _("INTERNAL ERROR: dictionary %s \"%s\" not initialized"), + mp->name, SP (dict->query)); + return 1; + } + if (!mp->lookup) + { + logmsg (LOG_CRIT, + _("INTERNAL ERROR: no lookup function for dictionary %s \"%s\""), + mp->name, SP (dict->query)); + return 1; + } + if (mp->free) + mp->free (dict, handle); + return mp->lookup (dict, handle, cmd); +} + +unsigned +dictionary_num_rows (struct dictionary *dict) +{ + return dict->nrow; +} + +unsigned +dictionary_num_cols (struct dictionary *dict) +{ + return dict->ncol; +} + +const char * +dictionary_result (struct dictionary *dict, void *handle, + unsigned nrow, unsigned ncol) +{ + struct dictionary_descr *mp = dictionary_tab + dict->type; + + if (nrow >= dict->nrow || ncol >= dict->ncol + || mp->get (dict, handle, nrow, ncol)) + return NULL; + return dict->result; +} + +void +dictionary_copy_result (struct dictionary *dict, const char *res, size_t size) +{ + if (dict->result_size < size + 1) + { + dict->result_size = size + 1; + dict->result = x2realloc (dict->result, &dict->result_size); + } + memcpy (dict->result, res, size); + dict->result[size] = 0; +} + +/* Quote non-printable characters in INPUT. Point *OUTPUT to the malloc'ed + quoted string. Return its length. */ +int +dictionary_quote_string (struct dictionary *dict, void *handle, + const char *input, + char **poutput, size_t *psize) +{ + struct dictionary_descr *mp = dictionary_tab + dict->type; + size_t size; + int quote; + char *output; + + if (!input) + { + *poutput = xmalloc (1); + (*poutput)[0] = 0; + *psize = 1; + return 0; + } + + if (mp->quote) + return mp->quote (dict, handle, input, poutput, psize); + + size = wordsplit_quoted_length (input, 0, "e); + output = xmalloc (size + 1); + wordsplit_quote_copy (output, input, 0); + output[size] = 0; + + *poutput = output; + if (psize) + *psize = size; + return 0; +} + @@ -498,7 +498,7 @@ get_uploader_email (struct uploader_info *info, struct file_triplet *trp, } mu_address_t -get_recipient (struct access_method *method, struct file_triplet *trp, +get_recipient (struct dictionary *dict, struct file_triplet *trp, const char **errp) { unsigned nrows, ncols, i; @@ -507,32 +507,32 @@ get_recipient (struct access_method *method, struct file_triplet *trp, int rc; void *md; - if (method->type == method_none) + if (dict->type == dictionary_none) { - *errp = N_("access method is not configured"); + *errp = N_("dictionary is not configured"); return NULL; } - md = method_open (method); + md = dictionary_open (dict); if (!md) { - *errp = N_("failed to open access method"); + *errp = N_("failed to open dictionary"); return NULL; } - text = triplet_expand_method_query (method, md, trp); + text = triplet_expand_dictionary_query (dict, md, trp); - rc = method_run (method, md, text); + rc = dictionary_lookup (dict, md, text); free (text); if (rc) { *errp = N_("cannot obtain recipient emails"); - method_close (method, md); + dictionary_close (dict, md); return NULL; } - nrows = method_num_rows (method); - ncols = method_num_cols (method); + nrows = dictionary_num_rows (dict); + ncols = dictionary_num_cols (dict); if (nrows == 0) { @@ -543,19 +543,19 @@ get_recipient (struct access_method *method, struct file_triplet *trp, for (i = 0; i < nrows; i++) { mu_address_t addr; - const char *str = method_result (method, md, i, 0); + const char *str = dictionary_result (dict, md, i, 0); if (mu_address_create (&addr, str)) continue; if (ncols > 0) { - str = method_result (method, md, i, 1); + str = dictionary_result (dict, md, i, 1); if (str) mu_address_set_personal (addr, 1, str); } mu_address_union (&rcpt, addr); mu_address_destroy (&addr); } - method_close (method, md); + dictionary_close (dict, md); return rcpt; } @@ -583,7 +583,7 @@ do_notify (struct file_triplet *trp, enum notification_event ev, break; case notify_owner: - rcpt = get_recipient (trp->spool->access_method[project_owner_method], + rcpt = get_recipient (trp->spool->dictionary[project_owner_dict], trp, &errp); } @@ -51,7 +51,7 @@ find_expansion_word (const char *kw, size_t len, char * meta_expand_string (const char *string, struct metadef *def, void *data, - struct access_method *method, void *handle) + struct dictionary *dict, void *handle) { const char *p, *s; char *res; @@ -82,12 +82,12 @@ meta_expand_string (const char *string, struct metadef *def, void *data, e = strchr (p + 1, '}'); if (e && (s = find_expansion_word (p + 1, e - p - 1, def, data))) { - if (method) + if (dict) { char *newval; size_t len; /* FIXME: Return value? */ - method_quote_string (method, handle, s, &newval, &len); + dictionary_quote_string (dict, handle, s, &newval, &len); obstack_grow (&stk, newval, len); free (newval); } @@ -111,12 +111,12 @@ meta_expand_string (const char *string, struct metadef *def, void *data, len = 1; } - if (method) + if (dict) { char *newval; size_t len; /* FIXME: Return value? */ - method_quote_string (method, handle, s, &newval, &len); + dictionary_quote_string (dict, handle, s, &newval, &len); obstack_grow (&stk, newval, len); free (newval); } diff --git a/src/method.c b/src/method.c deleted file mode 100644 index d5ad0b4..0000000 --- a/src/method.c +++ /dev/null @@ -1,226 +0,0 @@ -/* wydawca - automatic release submission daemon - Copyright (C) 2007 Sergey Poznyakoff - - Wydawca is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by the - Free Software Foundation; either version 3 of the License, or (at your - option) any later version. - - Wydawca is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with wydawca. If not, see <http://www.gnu.org/licenses/>. */ - -#include "wydawca.h" -#include "sql.h" -#include "builtin.h" - -struct method_descr -{ - const char *name; - - int (*init) (struct access_method *); - int (*done) (struct access_method *); - int (*free) (struct access_method *, void *); - - void *(*open) (struct access_method *); - int (*close) (struct access_method *, void *); - - int (*get) (struct access_method *, void *, unsigned, unsigned); - int (*run) (struct access_method *, void *, const char *); - int (*quote) (struct access_method *, void *, const char *, char **, size_t *); -}; - -static struct method_descr method_tab[] = { - { "none", NULL, NULL, NULL, NULL, NULL, NULL, NULL }, - { "sql", sql_init_method, sql_done_method, sql_free_result, - sql_open, NULL, sql_get_method, sql_run_method, sql_quote }, - { "builtin", builtin_init, builtin_done, builtin_free_result, - builtin_open, NULL, - builtin_get, - builtin_run }, - { "external", NULL, NULL, NULL, NULL, NULL, NULL, NULL } -}; - -struct access_method * -method_new (enum access_method_id id, enum access_method_type type) -{ - struct access_method *mp = xmalloc (sizeof mp[0]); - memset (mp, 0, sizeof mp[0]); - mp->id = id; - mp->type = type; - return mp; -} - -int -method_init (struct access_method *method) -{ - struct method_descr *mp = method_tab + method->type; - int rc = 0; - - if (method->init_passed++) - return 0; - if (debug_level > 1) - { - int i; - logmsg (LOG_DEBUG, _("initializing method: %s \"%s\""), - mp->name, SP (method->query)); - for (i = 0; i < method->parmc; i++) - logmsg (LOG_DEBUG, " parmv[%d]=%s", i, method->parmv[i]); - } - if (mp->init) - rc = mp->init (method); - if (rc == 0) - method->init_passed = 1; - return rc; -} - -void * -method_open (struct access_method *method) -{ - struct method_descr *mp = method_tab + method->type; - - if (!mp->open) - return NULL; - return mp->open (method); -} - -int -method_close (struct access_method *method, void *handle) -{ - struct method_descr *mp = method_tab + method->type; - if (!mp->close) - return 0; - return mp->close (method, handle); -} - -int -method_done (struct access_method *method) -{ - struct method_descr *mp = method_tab + method->type; - int rc = 0; - - if (method->init_passed == 0) - return 0; - if (--method->init_passed) - return 0; - if (debug_level > 1) - { - int i; - logmsg (LOG_DEBUG, _("closing method: %s \"%s\""), - mp->name, SP (method->query)); - for (i = 0; i < method->parmc; i++) - logmsg (LOG_DEBUG, " parmv[%d]=%s", i, method->parmv[i]); - } - if (mp->done) - rc = mp->done (method); - free (method->result); - method->result = NULL; - method->result_size = 0; - return rc; -} - -int -method_run (struct access_method *method, void *handle, const char *cmd) -{ - struct method_descr *mp = method_tab + method->type; - - if (debug_level > 1) - { - if (cmd) - logmsg (LOG_DEBUG, _("running method: %s \"%s\""), mp->name, cmd); - else - logmsg (LOG_DEBUG, _("running method: %s"), mp->name); - } - - if (!method->init_passed) - { - logmsg (LOG_CRIT, _("INTERNAL ERROR: method %s \"%s\" not initialized"), - mp->name, SP (method->query)); - return 1; - } - if (!mp->run) - { - logmsg (LOG_CRIT, - _("INTERNAL ERROR: no run function for method %s \"%s\""), - mp->name, SP (method->query)); - return 1; - } - if (mp->free) - mp->free (method, handle); - return mp->run (method, handle, cmd); -} - -unsigned -method_num_rows (struct access_method *method) -{ - return method->nrow; -} - -unsigned -method_num_cols (struct access_method *method) -{ - return method->ncol; -} - -const char * -method_result (struct access_method *method, void *handle, - unsigned nrow, unsigned ncol) -{ - struct method_descr *mp = method_tab + method->type; - - if (nrow >= method->nrow || ncol >= method->ncol - || mp->get (method, handle, nrow, ncol)) - return NULL; - return method->result; -} - -void -method_copy_result (struct access_method *method, const char *res, size_t size) -{ - if (method->result_size < size + 1) - { - method->result_size = size + 1; - method->result = x2realloc (method->result, &method->result_size); - } - memcpy (method->result, res, size); - method->result[size] = 0; -} - -/* Quote non-printable characters in INPUT. Point *OUTPUT to the malloc'ed - quoted string. Return its length. */ -int -method_quote_string (struct access_method *method, void *handle, - const char *input, - char **poutput, size_t *psize) -{ - struct method_descr *mp = method_tab + method->type; - size_t size; - int quote; - char *output; - - if (!input) - { - *poutput = xmalloc (1); - (*poutput)[0] = 0; - *psize = 1; - return 0; - } - - if (mp->quote) - return mp->quote (method, handle, input, poutput, psize); - - size = wordsplit_quoted_length (input, 0, "e); - output = xmalloc (size + 1); - wordsplit_quote_copy (output, input, 0); - output[size] = 0; - - *poutput = output; - if (psize) - *psize = size; - return 0; -} - diff --git a/src/process.c b/src/process.c index a998edb..58df169 100644 --- a/src/process.c +++ b/src/process.c @@ -211,11 +211,11 @@ scan_spool_unlocked (const struct spool *spool, int uc, uid_t *uv) { int i; - for (i = 0; i < access_method_count; i++) + for (i = 0; i < dictionary_count; i++) { - if (method_init (spool->access_method[i])) + if (dictionary_init (spool->dictionary[i])) { - logmsg (LOG_ERR, _("failed to initialize access method %d"), i); + logmsg (LOG_ERR, _("failed to initialize dictionary %d"), i); return; } } @@ -250,11 +250,11 @@ scan_spool (const struct spool *spool, int uc, uid_t *uv) } static void -close_methods (struct spool *spool) |