]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-config.c
cli: clean up exit status code returned by the cli commands
[notmuch] / notmuch-config.c
index d9c2eb3fe65957917bef62bfab61dbe74da32a8a..e06142b528bced29a0cda16cfeed1ffecde90020 100644 (file)
@@ -456,10 +456,19 @@ notmuch_config_save (notmuch_config_t *config)
     /* Try not to overwrite symlinks. */
     filename = realpath (config->filename, NULL);
     if (! filename) {
-       fprintf (stderr, "Error canonicalizing %s: %s\n", config->filename,
-                strerror (errno));
-       g_free (data);
-       return 1;
+       if (errno == ENOENT) {
+           filename = strdup (config->filename);
+           if (! filename) {
+               fprintf (stderr, "Out of memory.\n");
+               g_free (data);
+               return 1;
+           }
+       } else {
+           fprintf (stderr, "Error canonicalizing %s: %s\n", config->filename,
+                    strerror (errno));
+           g_free (data);
+           return 1;
+       }
     }
 
     if (! g_file_set_contents (filename, data, length, &error)) {
@@ -695,7 +704,7 @@ _item_split (char *item, char **group, char **key)
 
     *group = item;
 
-    period = index (item, '.');
+    period = strchr (item, '.');
     if (period == NULL || *(period+1) == '\0') {
        fprintf (stderr,
                 "Invalid configuration name: %s\n"
@@ -830,34 +839,39 @@ notmuch_config_command_list (notmuch_config_t *config)
 int
 notmuch_config_command (notmuch_config_t *config, int argc, char *argv[])
 {
+    int ret;
+
     argc--; argv++; /* skip subcommand argument */
 
     if (argc < 1) {
        fprintf (stderr, "Error: notmuch config requires at least one argument.\n");
-       return 1;
+       return EXIT_FAILURE;
     }
 
     if (strcmp (argv[0], "get") == 0) {
        if (argc != 2) {
            fprintf (stderr, "Error: notmuch config get requires exactly "
                     "one argument.\n");
-           return 1;
+           return EXIT_FAILURE;
        }
-       return notmuch_config_command_get (config, argv[1]);
+       ret = notmuch_config_command_get (config, argv[1]);
     } else if (strcmp (argv[0], "set") == 0) {
        if (argc < 2) {
            fprintf (stderr, "Error: notmuch config set requires at least "
                     "one argument.\n");
-           return 1;
+           return EXIT_FAILURE;
        }
-       return notmuch_config_command_set (config, argv[1], argc - 2, argv + 2);
+       ret = notmuch_config_command_set (config, argv[1], argc - 2, argv + 2);
     } else if (strcmp (argv[0], "list") == 0) {
-       return notmuch_config_command_list (config);
+       ret = notmuch_config_command_list (config);
+    } else {
+       fprintf (stderr, "Unrecognized argument for notmuch config: %s\n",
+                argv[0]);
+       return EXIT_FAILURE;
     }
 
-    fprintf (stderr, "Unrecognized argument for notmuch config: %s\n",
-            argv[0]);
-    return 1;
+    return ret ? EXIT_FAILURE : EXIT_SUCCESS;
+
 }
 
 notmuch_bool_t