aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Carl Worth <cworth@cworth.org>2011-05-24 12:00:27 -0700
committerGravatar Carl Worth <cworth@cworth.org>2011-05-24 12:04:08 -0700
commit150db11214fead32af8b8a90920947d3a6570295 (patch)
tree201826fb98d7830ce51cb4b02a360ea9b9a022d1
parent2f8871df6ea3c0b44f85a0fc1b4f58a6b70b0a0e (diff)
notmuch: Support "notmuch part" as an alias for "notmuch show --format=raw"
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.
-rw-r--r--notmuch.c51
1 files changed, 49 insertions, 2 deletions
diff --git a/notmuch.c b/notmuch.c
index 2e98f25d..dc21096c 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -32,6 +32,17 @@ typedef struct command {
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[]);
@@ -115,7 +126,7 @@ static const char search_terms_help[] =
"\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.",
@@ -546,7 +557,9 @@ main (int argc, char *argv[])
{
void *local;
command_t *command;
- unsigned int i;
+ alias_t *alias;
+ unsigned int i, j;
+ const char **argv_local;
talloc_enable_null_tracking ();
@@ -565,6 +578,40 @@ main (int argc, char *argv[])
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];