From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id BB4D86DE0946 for ; Sat, 22 Oct 2016 05:51:59 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: -0.233 X-Spam-Level: X-Spam-Status: No, score=-0.233 tagged_above=-999 required=5 tests=[AWL=-0.213, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id iPbKNt9dM403 for ; Sat, 22 Oct 2016 05:51:59 -0700 (PDT) Received: from mail-lf0-f67.google.com (mail-lf0-f67.google.com [209.85.215.67]) by arlo.cworth.org (Postfix) with ESMTPS id 901786DE02AC for ; Sat, 22 Oct 2016 05:51:58 -0700 (PDT) Received: by mail-lf0-f67.google.com with SMTP id n3so76937lfn.0 for ; Sat, 22 Oct 2016 05:51:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nikula-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=fWPkPdzXLhpBrq56ZJBo1mlzi6qkvFc0WriKKZH7vSU=; b=xuljNBgSpcSG9y+VdBD9mbjyw+QYzfF2WH7CMNqenn4Eu4firCrLrSfLH66+zNzs2+ RhQWldtXc3xvi/HaRhbKeuWnecJSk7H3uBaHMHgda0TkiYGs/hLM6Uvv3k4Bh4KQ30fM +t/eaktGnSjRjj5Z64+DZkaOFp9LKAKv6qcd+azvp+VmSnxoT8/Nioc5Wbm+sTUfVU/G 2NOP2VOT77KeprMazYLLZdsG/at6P4DzollCpiBA7Daf+g4C2WHrfcs33+MgsGWDjQ3S vKCWO8r4q6WCnyHJ7O8Q80o1I7L7xwttYV9F7jzw5bY4jKnG61IKqByv5M8qLhoBFeBE h8dA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=fWPkPdzXLhpBrq56ZJBo1mlzi6qkvFc0WriKKZH7vSU=; b=Lr4bIizoD+YKqF7tpuh2E2NCfg+QerpGOuBMcEXrD4w8CHWqggmlXjSLZgSyzc1+iz hRjndLhbgD6kpdIY4VbOF0EXaxYdof69oJE0upXjHkQGLlhxHplh5kFdFslhBW1DACz4 Q+i2t3bL62RfRd8zbJcaNYY5LeWWZqyYJEXFwxCyI2BK6tzC/pkZxszpvfLnHZRsxdVE LECLNa0E3G9gKp0IFBFEo2h9OkLiuiwJGaqtNQNFy98OxGzalY+gEGANqQVJ1T/S4EVJ t5FRhAS5l2eKADf5HuThLc1mbn2fkU0vwjcDFl8EwQP8E5j1B8mJf67LvHy0Ji+C9ck8 JdBg== X-Gm-Message-State: ABUngvcswGnNJKl15B+r8HHdg6nSMVgbpaq+Xd1tP4bUuJ/eE8cbPBqnRMK7e0nsaxeDTw== X-Received: by 10.25.7.201 with SMTP id 192mr2896036lfh.170.1477140716131; Sat, 22 Oct 2016 05:51:56 -0700 (PDT) Received: from localhost (mobile-access-bceef0-89.dhcp.inet.fi. [188.238.240.89]) by smtp.gmail.com with ESMTPSA id f23sm1299442lji.12.2016.10.22.05.51.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 22 Oct 2016 05:51:55 -0700 (PDT) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [PATCH] cli: try to run external notmuch- prefixed commands as subcommands Date: Sat, 22 Oct 2016 15:50:05 +0300 Message-Id: <1477140605-3011-1-git-send-email-jani@nikula.org> X-Mailer: git-send-email 2.1.4 X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 22 Oct 2016 12:51:59 -0000 If the given subcommand is not known to notmuch, try to execute external notmuch- instead. This allows users to have their own notmuch related tools be run via the notmuch command, not unlike git does. Also notmuch-emacs-mua will be executable via 'notmuch emacs-mua'. By design, this does not allow notmuch's own subcommands to be overriden using external commands. --- Whether internal subcommands can be overridden or not is up to debate, can be consider either a bug or a feature depending on how you look at it. --- notmuch.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/notmuch.c b/notmuch.c index 38b73c1d2acc..b9c320329dd5 100644 --- a/notmuch.c +++ b/notmuch.c @@ -363,6 +363,39 @@ notmuch_command (notmuch_config_t *config, return EXIT_SUCCESS; } +/* + * Try to run subcommand in argv[0] as notmuch- prefixed external + * command. argv must be NULL terminated (argv passed to main always + * is). + * + * Does not return if the external command is found and + * executed. Return TRUE if external command is not found. Return + * FALSE on errors. + */ +static notmuch_bool_t try_external_command(char *argv[]) +{ + char *old_argv0 = argv[0]; + notmuch_bool_t ret = TRUE; + + argv[0] = talloc_asprintf (NULL, "notmuch-%s", old_argv0); + + /* + * This will only return on errors. Not finding an external + * command (ENOENT) is not an error from our perspective. + */ + execvp (argv[0], argv); + if (errno != ENOENT) { + fprintf (stderr, "Error: Running external command '%s' failed: %s\n", + argv[0], strerror(errno)); + ret = FALSE; + } + + talloc_free (argv[0]); + argv[0] = old_argv0; + + return ret; +} + int main (int argc, char *argv[]) { @@ -406,8 +439,10 @@ main (int argc, char *argv[]) command = find_command (command_name); if (!command) { - fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n", - command_name); + /* This won't return if the external command is found. */ + if (try_external_command(argv + opt_index)) + fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n", + command_name); ret = EXIT_FAILURE; goto DONE; } -- 2.1.4