diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-02-16 13:09:28 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-02-16 13:09:28 +0200 |
commit | b0f10ec7274144b4e048953b11f2a4d98e055810 (patch) | |
tree | 4bcc06122d183582d0b53fe2b8ba4c87b289b96f /src/config.c | |
parent | 169a680208f2de8f92b4a2a4977953d4452e981f (diff) | |
download | wydawca-b0f10ec7274144b4e048953b11f2a4d98e055810.tar.gz wydawca-b0f10ec7274144b4e048953b11f2a4d98e055810.tar.bz2 |
Improve argument parsing
* src/cmdline.opt, src/getopt.m4: New files.
* src/wydawca.c: Switch to new way of command line parsing.
* src/wydawca.h (gettext): Define.
* bootstrap.conf: Add formatting flags for gconf_warning and gconf_error
* gconf-preproc.c (pp_list_find): Minor change.
* gconf/gconf.h (gconf_warning,gconf_error): Mark as printflike.
* src/.gitignore: Add cmdline.h
* src/Makefile.am (wydawca_SOURCES): Add cmdline.h.
(.opt.h): New rule.
* src/config.c (cb_archive): Archive is now a block statement (unless type
"none" is declared).
(all functions): Tighten input checking
* src/update-2.0.awk: Reflect the above change.
* src/directive.c (process_directives): Minor change.
* src/diskio.c (symlink_file): Minor change.
Diffstat (limited to 'src/config.c')
-rw-r--r-- | src/config.c | 170 |
1 files changed, 88 insertions, 82 deletions
diff --git a/src/config.c b/src/config.c index 3545e05..32b54dd 100644 --- a/src/config.c +++ b/src/config.c @@ -218,16 +218,16 @@ string_to_notification_target (gconf_locus_t *locus, const char *val, static int assert_string_arg (gconf_locus_t *locus, enum gconf_callback_command cmd, - gconf_value_t *value) + const gconf_value_t *value) { if (cmd != gconf_callback_set_value) { gconf_error (locus, 0, _("Unexpected block statement")); return 1; } - if (value->type != GCONF_TYPE_STRING) + if (!value || value->type != GCONF_TYPE_STRING) { - gconf_error (locus, 0, _("expected scalar value but found list")); + gconf_error (locus, 0, _("expected scalar value as a tag")); return 1; } return 0; @@ -267,9 +267,9 @@ cb_mailer (enum gconf_callback_command cmd, gconf_error (locus, 0, _("Unexpected block statement")); return 1; } - if (value->type != GCONF_TYPE_STRING) + if (!value || value->type != GCONF_TYPE_STRING) { - gconf_error (locus, 0, _("expected scalar value but found list")); + gconf_error (locus, 0, _("expected scalar value")); return 1; } rc = mu_mailer_create (&mailer, value->v.string); @@ -571,7 +571,7 @@ cb_sql (enum gconf_callback_command cmd, switch (cmd) { case gconf_callback_section_begin: - if (value->type != GCONF_TYPE_STRING || !value->v.string) + if (!value || value->type != GCONF_TYPE_STRING) { gconf_error(locus, 0, _("tag must be a string")); return 0; @@ -614,9 +614,6 @@ cb_syslog_facility (enum gconf_callback_command cmd, gconf_value_t *value, void *cb_data) { - struct sqlconn *pconn = varptr; - char *p; - if (assert_string_arg (locus, cmd, value)) return 1; @@ -641,7 +638,7 @@ cb_define_message (enum gconf_callback_command cmd, gconf_error (locus, 0, _("Unexpected block statement")); return 1; } - if (value->type != GCONF_TYPE_ARRAY || value->v.arg.c != 2) + if (!value || value->type != GCONF_TYPE_ARRAY || value->v.arg.c != 2) { gconf_error (locus, 0, _("expected two arguments")); return 1; @@ -736,6 +733,30 @@ get_backup_version (gconf_locus_t *locus, const char *ctx, } static int +cb_backup (enum gconf_callback_command cmd, + gconf_locus_t *locus, + void *varptr, + gconf_value_t *value, + void *cb_data) +{ + enum backup_type *ptype = varptr; + + if (assert_string_arg (locus, cmd, value)) + return 1; + *ptype = get_backup_version (locus, NULL, value->v.string); + return 0; +} + +static struct gconf_keyword archive_kw[] = { + { "name", N_("file-or-dir"), N_("Name of archive file or directory"), + gconf_type_string, NULL, offsetof(struct archive_descr, name) }, + { "backup", N_("type"), N_("Define backup type"), + gconf_type_string, NULL, offsetof(struct archive_descr, backup_type), + cb_backup }, + { NULL } +}; + +static int cb_archive (enum gconf_callback_command cmd, gconf_locus_t *locus, void *varptr, @@ -743,70 +764,60 @@ cb_archive (enum gconf_callback_command cmd, void *cb_data) { struct archive_descr *arch = varptr; - gconf_value_t *argp; - const char *type; - - if (cmd != gconf_callback_set_value) - { - gconf_error (locus, 0, _("Unexpected block statement")); - return 1; - } - - if (value->type != GCONF_TYPE_ARRAY || value->v.arg.c > 3) - { - gconf_error (locus, 0, _("expected 1-3 arguments")); - return 1; - } - - argp = get_arg (locus, value, 0, GCONF_TYPE_STRING); - if (!argp) - return 1; + void **pdata = cb_data; - type = argp->v.string; - if (strcmp (type, "none") == 0) - { - arch->type = archive_none; - if (value->v.arg.c > 1) - gconf_warning (locus, 0, - _("rest of line ignored for archive type `none'")); - } - else + switch (cmd) { - argp = get_arg (locus, value, 1, GCONF_TYPE_STRING); - if (!argp) - return 1; - arch->name = safe_file_name (xstrdup (argp->v.string)); - if (!arch->name) - { - gconf_error (locus, 0, _("invalid archive name: %s"), - argp->v.string); + case gconf_callback_section_begin: + *pdata = arch; + /* fallthrough */ + case gconf_callback_set_value: + if (!value) + { + gconf_error (locus, 0, _("expected tag")); return 1; } - - if (strcmp (type, "tar") == 0) + + if (value->type != GCONF_TYPE_STRING) { - arch->type = archive_tar; - if (value->v.arg.c > 2) - gconf_warning (locus, 0, - _("junk after the archive name ignored")); + gconf_error (locus, 0, _("expected scalar value but found list")); + return 1; } - else if (strcmp (type, "directory") == 0) + + if (strcmp (value->v.string, "none") == 0) + arch->type = archive_none; + else if (strcmp (value->v.string, "tar") == 0) + arch->type = archive_tar; + else if (strcmp (value->v.string, "directory") == 0) + arch->type = archive_directory; + else { - arch->type = archive_directory; - if (value->v.arg.c > 2) - { - argp = get_arg (locus, value, 2, GCONF_TYPE_STRING); - arch->backup_type = get_backup_version (locus, NULL, - argp->v.string); - } - else - get_backup_version (locus, - "VERSION_CONTROL environment variable", - getenv ("VERSION_CONTROL")); + gconf_error (locus, 0, _("unknown archive type")); + return 1; } - else - gconf_warning (locus, 0, _("unknown archive type")); + if (cmd == gconf_callback_section_begin) + return 0; + break; + + case gconf_callback_section_end: + break; + } + + if (arch->type == archive_none) + return 0; + + if (arch->name == NULL) + { + gconf_error (locus, 0, _("at least archive name must be set")); + return 1; + } + + if (arch->type == archive_tar && arch->backup_type != no_backups) + { + gconf_warning (locus, 0, _("backup type ignored for this archive type")); + return 1; } + return 0; } @@ -943,7 +954,7 @@ cb_access_method_params (enum gconf_callback_command cmd, gconf_error (locus, 0, _("Unexpected block statement")); return 1; } - if (value->type != GCONF_TYPE_LIST) + if (!value || value->type != GCONF_TYPE_LIST) { gconf_error (locus, 0, _("expected list value")); return 1; @@ -954,7 +965,7 @@ cb_access_method_params (enum gconf_callback_command cmd, param[0] = param[1] = NULL; else { - gconf_value_t *vp = gl_list_get_at (value->v.list, 0); + const gconf_value_t *vp = gl_list_get_at (value->v.list, 0); if (assert_string_arg (locus, cmd, vp)) return 1; @@ -1023,7 +1034,7 @@ cb_access_method (enum gconf_callback_command cmd, switch (cmd) { case gconf_callback_section_begin: - if (value->type != GCONF_TYPE_STRING || !value->v.string) + if (!value || value->type != GCONF_TYPE_STRING) { gconf_error(locus, 0, _("tag must be a string")); return 0; @@ -1079,11 +1090,9 @@ static struct gconf_keyword directory_kw[] = { { "access-method", N_("ident"), N_("Define access method"), gconf_type_section, NULL, offsetof(struct directory_pair, access_method), cb_access_method, NULL, access_method_kw }, - { "archive", - N_("<type: string> <archive-name: string> [<backup-method: method>]"), - N_("Set up archivation"), - gconf_type_string, NULL, offsetof(struct directory_pair, archive), - cb_archive }, + { "archive", N_("type: string"), N_("Set up archivation"), + gconf_type_section, NULL, offsetof(struct directory_pair, archive), + cb_archive, NULL, archive_kw }, { NULL } }; @@ -1101,7 +1110,7 @@ cb_directory (enum gconf_callback_command cmd, switch (cmd) { case gconf_callback_section_begin: - if (value->type != GCONF_TYPE_STRING || !value->v.string) + if (!value || value->type != GCONF_TYPE_STRING) { gconf_error (locus, 0, _("tag must be a string")); return 1; @@ -1124,8 +1133,7 @@ cb_directory (enum gconf_callback_command cmd, else if (test_dir (dpair->source_dir, &ec)) { if (ec) - gconf_error (locus, ec, _("cannot access %s"), - dpair->source_dir, strerror (ec)); + gconf_error (locus, ec, _("cannot access %s"), dpair->source_dir); else gconf_error (locus, 0, _("%s is not a directory"), dpair->source_dir); @@ -1133,8 +1141,7 @@ cb_directory (enum gconf_callback_command cmd, else if (test_dir (dpair->dest_dir, &ec)) { if (ec) - gconf_error (locus, ec, _("cannot access %s"), - dpair->dest_dir, strerror (ec)); + gconf_error (locus, ec, _("cannot access %s"), dpair->dest_dir); else gconf_error (locus, ec, _("%s is not a directory"), dpair->dest_dir); @@ -1202,10 +1209,9 @@ static struct gconf_keyword wydawca_kw[] = { N_("Define message text"), gconf_type_string, NULL, 0, cb_define_message }, - { "archive", - N_("<type: string> <archive-name: string> [<backup-method: method>]"), - N_("Set up archivation"), - gconf_type_string, &default_archive_descr, 0, cb_archive }, + { "archive", N_("type: string"), N_("Set up archivation"), + gconf_type_section, &default_archive_descr, 0, + cb_archive, NULL, archive_kw }, { "mail-statistics", NULL, N_("Send statistics"), gconf_type_section, NULL, 0, NULL, NULL, mail_statistics_kw }, |