X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch-config.c;h=dcdb0369c917cc562c28c5088c3443df0a616f6a;hp=2e81d1c22e4a27232d561f119b4962663c024fc0;hb=eb8caadd4889fdf7c95c9c853ca7fe0e957334ea;hpb=b9eac48c22f53f84ed1d9c1d8ca862a7b638c9ac diff --git a/notmuch-config.c b/notmuch-config.c index 2e81d1c2..dcdb0369 100644 --- a/notmuch-config.c +++ b/notmuch-config.c @@ -562,29 +562,42 @@ notmuch_config_set_new_tags (notmuch_config_t *config, config->new_tags = NULL; } -int -notmuch_config_command (void *ctx, int argc, char *argv[]) +/* Given a configuration item of the form . return the + * component group and key. If any error occurs, print a message on + * stderr and return 1. Otherwise, return 0. + * + * Note: This function modifies the original 'item' string. + */ +static int +_item_split (char *item, char **group, char **key) { - notmuch_config_t *config; - const char *item; + char *period; - if (argc != 2) { - fprintf (stderr, "Error: notmuch config requires two arguments.\n"); - return 1; - } + *group = item; - if (strcmp (argv[0], "get")) { - fprintf (stderr, "Unrecognized argument for notmuch config: %s\n", - argv[0]); + period = index (item, '.'); + if (period == NULL || *(period+1) == '\0') { + fprintf (stderr, + "Invalid configuration name: %s\n" + "(Should be of the form
.)\n", item); return 1; } + *period = '\0'; + *key = period + 1; + + return 0; +} + +static int +notmuch_config_command_get (void *ctx, char *item) +{ + notmuch_config_t *config; + config = notmuch_config_open (ctx, NULL, NULL); if (config == NULL) return 1; - item = argv[1]; - if (strcmp(item, "database.path") == 0) { printf ("%s\n", notmuch_config_get_database_path (config)); } else if (strcmp(item, "user.name") == 0) { @@ -606,12 +619,87 @@ notmuch_config_command (void *ctx, int argc, char *argv[]) for (i = 0; i < length; i++) printf ("%s\n", tags[i]); } else { - fprintf (stderr, "Unknown configuration item: %s\n", - argv[1]); - return 1; + char **value; + size_t i, length; + char *group, *key; + + if (_item_split (item, &group, &key)) + return 1; + + value = g_key_file_get_string_list (config->key_file, + group, key, + &length, NULL); + if (value == NULL) { + fprintf (stderr, "Unknown configuration item: %s.%s\n", + group, key); + return 1; + } + + for (i = 0; i < length; i++) + printf ("%s\n", value[i]); + + free (value); } notmuch_config_close (config); return 0; } + +static int +notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[]) +{ + notmuch_config_t *config; + char *group, *key; + int ret; + + if (_item_split (item, &group, &key)) + return 1; + + config = notmuch_config_open (ctx, NULL, NULL); + if (config == NULL) + return 1; + + /* With only the name of an item, we clear it from the + * configuration file. + * + * With a single value, we set it as a string. + * + * With multiple values, we set them as a string list. + */ + switch (argc) { + case 0: + g_key_file_remove_key (config->key_file, group, key, NULL); + break; + case 1: + g_key_file_set_string (config->key_file, group, key, argv[0]); + break; + default: + g_key_file_set_string_list (config->key_file, group, key, + (const gchar **) argv, argc); + break; + } + + ret = notmuch_config_save (config); + notmuch_config_close (config); + + return ret; +} + +int +notmuch_config_command (void *ctx, int argc, char *argv[]) +{ + if (argc < 2) { + fprintf (stderr, "Error: notmuch config requires at least two arguments.\n"); + return 1; + } + + if (strcmp (argv[0], "get") == 0) + return notmuch_config_command_get (ctx, argv[1]); + else if (strcmp (argv[0], "set") == 0) + return notmuch_config_command_set (ctx, argv[1], argc - 2, argv + 2); + + fprintf (stderr, "Unrecognized argument for notmuch config: %s\n", + argv[0]); + return 1; +}