diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-05-10 21:46:51 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-05-10 22:18:02 +0300 |
commit | 84db86f0151385a8f05483bf691fde86b4e4e153 (patch) | |
tree | f4b3e184c30f2bbc462a2b67cb52f869881afd47 /src/config.c | |
parent | 18cf87abfc95f85d184c3f6b328c4a8228609f68 (diff) | |
download | wydawca-84db86f0151385a8f05483bf691fde86b4e4e153.tar.gz wydawca-84db86f0151385a8f05483bf691fde86b4e4e153.tar.bz2 |
Drop dependencies on argmatch and xgethostname.
Diffstat (limited to 'src/config.c')
-rw-r--r-- | src/config.c | 332 |
1 files changed, 145 insertions, 187 deletions
diff --git a/src/config.c b/src/config.c index 7353f29..df9816e 100644 --- a/src/config.c +++ b/src/config.c @@ -16,10 +16,40 @@ #include "wydawca.h" #include "sql.h" -#include "argmatch.h" #include <mail.h> +struct keyword +{ + char *name; + int tok; +}; + +static int +keyword_to_tok (const char *str, struct keyword *kw, int *pres) +{ + for (; kw->name; kw++) + if (strcmp (kw->name, str) == 0) + { + *pres = kw->tok; + return 0; + } + return 1; +} + +static int +tok_to_keyword (int tok, struct keyword *kw, const char **pres) +{ + for (; kw->name; kw++) + if (kw->tok == tok) + { + *pres = kw->name; + return 0; + } + return 1; +} + + static struct archive_descr default_archive_descr = { archive_none, NULL, @@ -35,7 +65,7 @@ static struct notification *default_notification = NULL; Strip trailing delimiter if present, unless it is the only character left. E.g.: - /home/user/../smith --> /home/smith + /home/user/../smith --> /home/smith /home/user/../.. --> / ../file --> NULL */ @@ -123,102 +153,82 @@ safe_file_name_alloc (const char *file_name) return ns; } -int -string_to (const char *what, const char *str, - const char **args, int *vals, - int *pret, - grecs_locus_t *locus) -{ - ptrdiff_t x = ARGMATCH (str, args, vals); - - if (x == (ptrdiff_t)-1) - { - grecs_error (locus, 0, _("unknown %s: %s"), what, str); - return 1; - } - else if (x == (ptrdiff_t)-2) - { - grecs_error (locus, 0, _("ambiguous %s: %s"), what, str); - return 1; - } - *pret = vals[x]; - return 0; -} -static const char *event_args[] = { - "success", - "bad-ownership", - "bad-directive-signature", - "bad-detached-signature", - "check-failure", - NULL -}; - -static int event_types[] = { - ev_success, - ev_bad_ownership, - ev_bad_directive_signature, - ev_bad_detached_signature, - ev_check_fail +static struct keyword event_tab[] = { + { "success", ev_success }, + { "bad-ownership", ev_bad_ownership }, + { "bad-directive-signature", ev_bad_directive_signature }, + { "bad-detached-signature", ev_bad_detached_signature }, + { "check-failure", ev_check_fail }, + { NULL } }; -ARGMATCH_VERIFY (event_args, event_types); - const char * notification_event_str (enum notification_event evt) { - return ARGMATCH_TO_ARGUMENT ((char*)&evt, event_args, event_types); + const char *ret; + if (tok_to_keyword (evt, event_tab, &ret)) + { + grecs_error (NULL, 0, + _("INTERNAL ERROR: unknown notification event number: %d"), + evt); + return NULL; + } + return ret; } int string_to_notification_event (grecs_locus_t *locus, const char *val, enum notification_event *pret) { - int rc, res; - rc = string_to ("notification event", val, - event_args, event_types, - &res, - locus); + int res; + if (keyword_to_tok (val, event_tab, &res)) + { + grecs_error (locus, 0, _("unknown notification event: %s"), val); + return 1; + } *pret = res; - return rc; + return 0; } -static const char *target_args[] = { - "read", - "message", - "admin", - "owner", - "user", - NULL -}; - -static int target_types[] = { - notify_read, /* Read recipients from the message headers */ - notify_read, - notify_admin, /* System administrator */ - notify_owner, /* Project admin */ - notify_user /* User (uploader) */ +static struct keyword target_tab[] = { + { "read", notify_read }, /* Read recipients from the message headers */ + { "message", notify_read }, + { "admin", notify_admin}, /* System administrator */ + { "owner", notify_owner }, /* Project admin */ + { "user", notify_user }, /* User (uploader) */ + { NULL } }; -ARGMATCH_VERIFY (target_args, target_types); const char * notification_target_str (enum notification_target tgt) { - return ARGMATCH_TO_ARGUMENT ((char*)&tgt, target_args, target_types); + const char *ret; + if (tok_to_keyword (tgt, target_tab, &ret)) + { + grecs_error (NULL, 0, + _("INTERNAL ERROR: unknown notification target number: %d"), + tgt); + return NULL; + } + return ret; } int string_to_notification_target (grecs_locus_t *locus, const char *val, enum notification_target *pret) { - int rc, res; - rc = string_to ("notification target", val, - target_args, target_types, - &res, - locus); + int res; + if (keyword_to_tok (val, target_tab, &res)) + { + grecs_error (locus, 0, + _("unknown notification target: %s"), + val); + return 1; + } *pret = res; - return rc; + return 0; } @@ -328,7 +338,7 @@ cb_email_address (enum grecs_callback_command cmd, grecs_error (locus, 0, _("too many arguments")); return 1; } - + *(mu_address_t*) varptr = addr; return rc; } @@ -400,47 +410,29 @@ cb_set_umask (enum grecs_callback_command cmd, } -static const char *stat_args[] = { - "errors", - "warnings", - "bad-signatures", - "access-violations", - "complete-triplets", - "incomplete-triplets", - "bad-triplets", - "expired-triplets", - "triplet-success", - "uploads", - "archives", - "symlinks", - "rmsymlinks", - NULL -}; - -static unsigned long stat_types[] = { - STAT_ERRORS, - STAT_WARNINGS, - STAT_BAD_SIGNATURE, - STAT_ACCESS_VIOLATIONS, - STAT_COMPLETE_TRIPLETS, - STAT_INCOMPLETE_TRIPLETS, - STAT_BAD_TRIPLETS, - STAT_EXPIRED_TRIPLETS, - STAT_TRIPLET_SUCCESS, - STAT_UPLOADS, - STAT_ARCHIVES, - STAT_SYMLINKS, - STAT_RMSYMLINKS +static struct keyword stat_tab[] = { + { "errors", STAT_ERRORS }, + { "warnings", STAT_WARNINGS }, + { "bad-signatures", STAT_BAD_SIGNATURE }, + { "access-violations", STAT_ACCESS_VIOLATIONS }, + { "complete-triplets", STAT_COMPLETE_TRIPLETS }, + { "incomplete-triplets", STAT_INCOMPLETE_TRIPLETS }, + { "bad-triplets", STAT_BAD_TRIPLETS }, + { "expired-triplets", STAT_EXPIRED_TRIPLETS }, + { "triplet-success", STAT_TRIPLET_SUCCESS }, + { "uploads", STAT_UPLOADS }, + { "archives", STAT_ARCHIVES }, + { "symlinks", STAT_SYMLINKS }, + { "rmsymlinks", STAT_RMSYMLINKS }, + { NULL }, }; -ARGMATCH_VERIFY (stat_args, stat_types); - static int parse_single_statmask (grecs_locus_t *locus, const grecs_value_t *val, unsigned long *pmask, int *invert) { const char *arg; - ptrdiff_t x; + int x; if (val->type != GRECS_TYPE_STRING) { @@ -463,19 +455,12 @@ parse_single_statmask (grecs_locus_t *locus, const grecs_value_t *val, return 0; } - x = ARGMATCH (arg, stat_args, stat_types); - - if (x == (ptrdiff_t)-1) + if (keyword_to_tok (arg, stat_tab, &x)) { grecs_error (locus, 0, _("unknown statistics type: %s"), arg); return 1; } - else if (x == (ptrdiff_t)-2) - { - grecs_error (locus, 0, _("ambiguous statistics type: %s"), arg); - return 1; - } - *pmask = STAT_MASK (stat_types[x]); + *pmask = STAT_MASK (x); return 0; } @@ -718,58 +703,36 @@ static struct grecs_keyword syslog_kw[] = { }; -static char const * const backup_args[] = -{ - /* In a series of synonyms, present the most meaningful first, so - that argmatch_valid be more readable. */ - "none", "off", - "simple", "never", - "existing", "nil", - "numbered", "t", - NULL -}; - -static const enum backup_type backup_types[] = +static struct keyword backup_tab[] = { - no_backups, no_backups, - simple_backups, simple_backups, - numbered_existing_backups, numbered_existing_backups, - numbered_backups, numbered_backups + { "none", no_backups }, + { "off", no_backups }, + { "simple", simple_backups }, + { "never", simple_backups }, + { "existing", numbered_existing_backups }, + { "nil", numbered_existing_backups }, + { "numbered", numbered_backups }, + { "t", numbered_backups }, + { NULL } }; -/* Ensure that these two vectors have the same number of elements, - not counting the final NULL in the first one. */ -ARGMATCH_VERIFY (backup_args, backup_types); - static enum backup_type -get_backup_version (grecs_locus_t *locus, const char *ctx, - const char *version) +get_backup_version (grecs_locus_t *locus, const char *ctx, const char *version) { + int d; + if (version == 0 || *version == 0) return numbered_existing_backups; - else + else if (keyword_to_tok (version, backup_tab, &d)) { - ptrdiff_t d = ARGMATCH (version, backup_args, backup_types); - if (d == -2) - { - if (ctx) - grecs_error (locus, 0, _("%s: ambiguous backup type `%s'"), - ctx, version); - else - grecs_error (locus, 0, _("ambiguous backup type `%s'"), version); - return no_backups; - } - else if (d == -1) - { - if (ctx) - grecs_error (locus, 0, _("%s: invalid backup type `%s'"), - ctx, version); - else - grecs_error (locus, 0, _("invalid backup type `%s'"), version); - return no_backups; - } - return backup_types[d]; + if (ctx) + grecs_error (locus, 0, _("%s: ambiguous backup type `%s'"), + ctx, version); + else + grecs_error (locus, 0, _("ambiguous backup type `%s'"), version); + return no_backups; } + return d; } static int @@ -1051,26 +1014,22 @@ static struct grecs_keyword dictionary_kw[] = { int string_to_dictionary_id (grecs_locus_t *locus, - const char *str, enum dictionary_id *idp) + const char *str, enum dictionary_id *idp) { - static const char *id_str[] = { - "project-uploader", - "project-owner", - NULL + static struct keyword id_tab[] = { + { "project-uploader", project_uploader_dict }, + { "project-owner", project_owner_dict }, + { NULL } }; - static int id_num[] = { - project_uploader_dict, - project_owner_dict - }; - ARGMATCH_VERIFY (id_str, id_num); + int res; - int rc, res; - rc = string_to ("dictionary", str, - id_str, id_num, - &res, - locus); + if (keyword_to_tok (str, id_tab, &res)) + { + grecs_error (locus, 0, _("unknown dictionary ID: %s"), str); + return 1; + } *idp = res; - return rc; + return 0; } static int @@ -1177,7 +1136,7 @@ static struct grecs_keyword spool_kw[] = { cb_notify_event, NULL, notify_event_kw }, { "check-script", NULL, N_("A /bin/sh script to verify the tarball"), grecs_type_string, NULL, offsetof(struct spool, check_script) }, - + { NULL } }; @@ -1252,7 +1211,7 @@ cb_spool (enum grecs_callback_command cmd, if (rc) return rc; - + if (!spool->notification) spool->notification = default_notification; mu_url_sget_path (spool->dest_url, &spool->dest_dir); @@ -1276,7 +1235,7 @@ cb_user (enum grecs_callback_command cmd, void *cb_data) { struct passwd *pw; - + if (assert_string_arg (locus, cmd, value)) return 1; @@ -1286,7 +1245,7 @@ cb_user (enum grecs_callback_command cmd, grecs_error (locus, 0, _("no such user: %s"), value->v.string); return 1; } - + wydawca_uid = pw->pw_uid; wydawca_gid = pw->pw_gid; return 0; @@ -1317,7 +1276,7 @@ cb_supp_groups (enum grecs_callback_command cmd, { int i; struct grecs_list_entry *ep; - + wydawca_supp_groups = xcalloc (wydawca_supp_groupc, sizeof (wydawca_supp_groups[0])); @@ -1325,7 +1284,7 @@ cb_supp_groups (enum grecs_callback_command cmd, { const grecs_value_t *vp = ep->data; struct group *grp; - + if (assert_string_arg (locus, cmd, vp)) break; grp = getgrnam (vp->v.string); @@ -1352,7 +1311,7 @@ static struct grecs_keyword locking_kw[] = { grecs_type_string, &lock_timeout, 0, cb_interval }, { NULL } }; - + static int cb_locking (enum grecs_callback_command cmd, grecs_locus_t *locus, @@ -1372,7 +1331,7 @@ cb_locking (enum grecs_callback_command cmd, } return 0; } - + static struct grecs_keyword wydawca_kw[] = { @@ -1391,11 +1350,11 @@ static struct grecs_keyword wydawca_kw[] = { grecs_type_string, NULL, 0, cb_user }, { "group", NULL, N_("Retain these supplementary groups"), grecs_type_string|GRECS_LIST, NULL, 0, cb_supp_groups }, - + { "locking", NULL, N_("Configure locking"), grecs_type_section, NULL, 0, cb_locking, NULL, locking_kw }, - + { "listen", N_("socket"), N_("Listen on this address"), grecs_type_sockaddr, &listen_sockaddr, }, @@ -1404,7 +1363,7 @@ static struct grecs_keyword wydawca_kw[] = { grecs_type_section, NULL, 0, NULL, NULL, tcpwrapper_kw }, #endif - + { "mailer", N_("url"), N_("Set mailer URL"), grecs_type_string, &mailer, 0, cb_mailer }, { "admin-address", N_("email"), N_("Set admin email address"), @@ -1423,7 +1382,7 @@ static struct grecs_keyword wydawca_kw[] = { { "archive-signatures", NULL, N_("Control implicit signature archivation"), grecs_type_bool, &archive_signatures }, - + { "statistics", N_("items"), N_("Print these stats at the end of run"), grecs_type_string, &print_stats, 0, cb_statistics }, @@ -1464,7 +1423,7 @@ static struct grecs_keyword wydawca_kw[] = { { "gpg-homedir", NULL, N_("GPG home directory"), grecs_type_string, &wydawca_gpg_homedir }, - + { NULL } }; @@ -1503,4 +1462,3 @@ config_finish (struct grecs_node *tree) if (grecs_tree_process (tree, wydawca_kw)) exit (EX_CONFIG); } - |