Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 11566431FD0 for ; Wed, 30 Nov 2011 17:15:55 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -2.3 X-Spam-Level: X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id AABemqb8JyQ0 for ; Wed, 30 Nov 2011 17:15:54 -0800 (PST) Received: from tempo.its.unb.ca (tempo.its.unb.ca [131.202.1.21]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id B17C9429E26 for ; Wed, 30 Nov 2011 17:15:53 -0800 (PST) Received: from rocinante.cs.unb.ca (m3.kereda.com [207.194.238.3] (may be forged)) (authenticated bits=0) by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id pB11FkF4008705 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO); Wed, 30 Nov 2011 21:15:48 -0400 Received: from bremner by rocinante.cs.unb.ca with local (Exim 4.76) (envelope-from ) id 1RVvFZ-0006qC-Ra; Wed, 30 Nov 2011 17:15:45 -0800 From: David Bremner To: notmuch@notmuchmail.org Subject: [RFC PATCH 1/3] notmuch-opts.[ch]: new argument parsing framework for notmuch. Date: Wed, 30 Nov 2011 17:15:28 -0800 Message-Id: <1322702130-26068-1-git-send-email-bremner@debian.org> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <7574621578db9bbfc88348c87e2f5431f4cb2296.1321223343.git.jani@nikula.org> References: <7574621578db9bbfc88348c87e2f5431f4cb2296.1321223343.git.jani@nikula.org> Cc: David Bremner X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 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: Thu, 01 Dec 2011 01:15:55 -0000 As we noticed when Jani kindly converted things to getopt_long, much of the work in argument parsing in notmuch is due to the the key-value style arguments like --format=(raw|json|text). In this intitial attempt at factoring out some of the argument processing, we include support for this kind of arguments roughly in the style of getopt_long. --- Makefile.local | 1 + notmuch-opts.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ notmuch-opts.h | 34 ++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 0 deletions(-) create mode 100644 notmuch-opts.c create mode 100644 notmuch-opts.h diff --git a/Makefile.local b/Makefile.local index c94402b..6606be8 100644 --- a/Makefile.local +++ b/Makefile.local @@ -303,6 +303,7 @@ notmuch_client_srcs = \ notmuch-count.c \ notmuch-dump.c \ notmuch-new.c \ + notmuch-opts.c \ notmuch-reply.c \ notmuch-restore.c \ notmuch-search.c \ diff --git a/notmuch-opts.c b/notmuch-opts.c new file mode 100644 index 0000000..6aac94c --- /dev/null +++ b/notmuch-opts.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include "notmuch-opts.h" + +notmuch_bool_t +parse_argument(const char *name, + const char *arg, + const notmuch_opt_desc_t *options, + notmuch_opt_t *state){ + + assert(arg); + assert(name); + assert(options); + assert(state); + + state->arg_id = 0; + state->keyword_id = 0; + + if (strncmp (arg,"--",2) != 0) { + state->arg_id = '*'; + state->string = arg; + return TRUE; + } + + if (strlen (arg) == 2) { + state->arg_id = '-'; + return FALSE; + } + + arg += 2; + + const notmuch_opt_desc_t *try = options; + while (try->name) { + if (strncmp (arg, try->name, strlen(try->name) ) == 0) { + state->arg_id = try->arg_id; + if (try->opt_type == NOTMUCH_OPT_KEYWORD) { + notmuch_keyword_t *try_arg = try->keywords; + const char *arg_str=arg + strlen(try->name); + + if (arg_str[0] != ':' && arg_str[0] != '=') { + fprintf (stderr, "%s %s: syntax error\n", name, arg); + return FALSE; + } + + /* skip delimiter */ + arg_str++; + + while (try_arg->name && state->keyword_id == 0) { + if (strcmp (arg_str, try_arg->name) == 0) { + state->keyword_id = try_arg->keyword_id; + } + try_arg++; + } + if (state->keyword_id == 0) { + fprintf (stderr, "%s %s: missing keyword\n", name, arg); + return FALSE; + } + + + } + } + try++; + } + if (state->arg_id == 0) { + fprintf (stderr, "%s %s: unknown argument\n", name, arg); + return FALSE; + } + + return TRUE; +} diff --git a/notmuch-opts.h b/notmuch-opts.h new file mode 100644 index 0000000..c442e3f --- /dev/null +++ b/notmuch-opts.h @@ -0,0 +1,34 @@ +#ifndef NOTMUCH_OPTS_H +#define NOTMUCH_OPTS_H + +#include "notmuch.h" + +enum notmuch_opt_type { + NOTMUCH_OPT_NULL = 0, + NOTMUCH_OPT_NO_ARG, + NOTMUCH_OPT_KEYWORD +}; + +typedef struct notmuch_keyword { + const char *name; + int keyword_id; +} notmuch_keyword_t; + +typedef struct notmuch_opt_desc { + const char *name; + int arg_id; + enum notmuch_opt_type opt_type; + struct notmuch_keyword *keywords; +} notmuch_opt_desc_t; + +typedef struct notmuch_opt { + int arg_id; + int keyword_id; + const char *string; +} notmuch_opt_t; + +notmuch_bool_t +parse_argument (const char* name, const char *arg, const notmuch_opt_desc_t *options, + notmuch_opt_t *result); + +#endif -- 1.7.5.4