]> git.notmuchmail.org Git - notmuch/commitdiff
notmuch: Support "notmuch part" as an alias for "notmuch show --format=raw"
authorCarl Worth <cworth@cworth.org>
Tue, 24 May 2011 19:00:27 +0000 (12:00 -0700)
committerCarl Worth <cworth@cworth.org>
Tue, 24 May 2011 19:04:08 +0000 (12:04 -0700)
We unifed the "notmuch part" functionality into "notmuch show" where
the implementation is both simpler and more powerful. But there's no
good reason to break users of the old interface.

Add support for aliases, which are undocumented means of getting at
functionality through deprecated names. The first such alias is
"notmuch part" as implemented here.

notmuch.c

index 2e98f25d1e2aa9fea9e870099dcf2d96939e6dad..dc21096c554a4f435bc4dfda021e041936ab50e1 100644 (file)
--- a/notmuch.c
+++ b/notmuch.c
@@ -32,6 +32,17 @@ typedef struct command {
     const char *documentation;
 } command_t;
 
     const char *documentation;
 } command_t;
 
+#define MAX_ALIAS_SUBSTITUTIONS 2
+
+typedef struct alias {
+    const char *name;
+    const char *substitutions[MAX_ALIAS_SUBSTITUTIONS];
+} alias_t;
+
+alias_t aliases[] = {
+    { "part", { "show", "--format=raw"}}
+};
+
 static int
 notmuch_help_command (void *ctx, int argc, char *argv[]);
 
 static int
 notmuch_help_command (void *ctx, int argc, char *argv[]);
 
@@ -115,7 +126,7 @@ static const char search_terms_help[] =
     "\n"
     "\t\t$(date +%%s -d 2009-10-01)..$(date +%%s)\n\n";
 
     "\n"
     "\t\t$(date +%%s -d 2009-10-01)..$(date +%%s)\n\n";
 
-command_t commands[] = {
+static command_t commands[] = {
     { "setup", notmuch_setup_command,
       NULL,
       "Interactively setup notmuch for first use.",
     { "setup", notmuch_setup_command,
       NULL,
       "Interactively setup notmuch for first use.",
@@ -546,7 +557,9 @@ main (int argc, char *argv[])
 {
     void *local;
     command_t *command;
 {
     void *local;
     command_t *command;
-    unsigned int i;
+    alias_t *alias;
+    unsigned int i, j;
+    const char **argv_local;
 
     talloc_enable_null_tracking ();
 
 
     talloc_enable_null_tracking ();
 
@@ -565,6 +578,40 @@ main (int argc, char *argv[])
        return 0;
     }
 
        return 0;
     }
 
+    for (i = 0; i < ARRAY_SIZE (aliases); i++) {
+       alias = &aliases[i];
+
+       if (strcmp (argv[1], alias->name) == 0)
+       {
+           int substitutions;
+
+           argv_local = talloc_size (local, sizeof (char *) *
+                                     (argc + MAX_ALIAS_SUBSTITUTIONS - 1));
+           if (argv_local == NULL) {
+               fprintf (stderr, "Out of memory.\n");
+               return 1;
+           }
+
+           /* Copy all substution arguments from the alias. */
+           argv_local[0] = argv[0];
+           for (j = 0; j < MAX_ALIAS_SUBSTITUTIONS; j++) {
+               if (alias->substitutions[j] == NULL)
+                   break;
+               argv_local[j+1] = alias->substitutions[j];
+           }
+           substitutions = j;
+
+           /* And copy all original arguments (skipping the argument
+            * that matched the alias of course. */
+           for (j = 2; j < (unsigned) argc; j++) {
+               argv_local[substitutions+j-1] = argv[j];
+           }
+
+           argc += substitutions - 1;
+           argv = (char **) argv_local;
+       }
+    }
+
     for (i = 0; i < ARRAY_SIZE (commands); i++) {
        command = &commands[i];
 
     for (i = 0; i < ARRAY_SIZE (commands); i++) {
        command = &commands[i];