1 Return-Path: <bremner@unb.ca>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id 11566431FD0
\r
6 for <notmuch@notmuchmail.org>; Wed, 30 Nov 2011 17:15:55 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5
\r
12 tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled
\r
13 Received: from olra.theworths.org ([127.0.0.1])
\r
14 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
15 with ESMTP id AABemqb8JyQ0 for <notmuch@notmuchmail.org>;
\r
16 Wed, 30 Nov 2011 17:15:54 -0800 (PST)
\r
17 Received: from tempo.its.unb.ca (tempo.its.unb.ca [131.202.1.21])
\r
18 (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
\r
19 (No client certificate requested)
\r
20 by olra.theworths.org (Postfix) with ESMTPS id B17C9429E26
\r
21 for <notmuch@notmuchmail.org>; Wed, 30 Nov 2011 17:15:53 -0800 (PST)
\r
22 Received: from rocinante.cs.unb.ca (m3.kereda.com [207.194.238.3] (may be
\r
23 forged)) (authenticated bits=0)
\r
24 by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id pB11FkF4008705
\r
25 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO);
\r
26 Wed, 30 Nov 2011 21:15:48 -0400
\r
27 Received: from bremner by rocinante.cs.unb.ca with local (Exim 4.76)
\r
28 (envelope-from <bremner@tesseract.cs.unb.ca>)
\r
29 id 1RVvFZ-0006qC-Ra; Wed, 30 Nov 2011 17:15:45 -0800
\r
30 From: David Bremner <bremner@debian.org>
\r
31 To: notmuch@notmuchmail.org
\r
32 Subject: [RFC PATCH 1/3] notmuch-opts.[ch]: new argument parsing framework for
\r
34 Date: Wed, 30 Nov 2011 17:15:28 -0800
\r
35 Message-Id: <1322702130-26068-1-git-send-email-bremner@debian.org>
\r
36 X-Mailer: git-send-email 1.7.5.4
\r
38 <7574621578db9bbfc88348c87e2f5431f4cb2296.1321223343.git.jani@nikula.org>
\r
40 <7574621578db9bbfc88348c87e2f5431f4cb2296.1321223343.git.jani@nikula.org>
\r
41 Cc: David Bremner <bremner@debian.org>
\r
42 X-BeenThere: notmuch@notmuchmail.org
\r
43 X-Mailman-Version: 2.1.13
\r
45 List-Id: "Use and development of the notmuch mail system."
\r
46 <notmuch.notmuchmail.org>
\r
47 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
48 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
49 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
50 List-Post: <mailto:notmuch@notmuchmail.org>
\r
51 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
52 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
53 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
54 X-List-Received-Date: Thu, 01 Dec 2011 01:15:55 -0000
\r
56 As we noticed when Jani kindly converted things to getopt_long, much
\r
57 of the work in argument parsing in notmuch is due to the the key-value
\r
58 style arguments like --format=(raw|json|text). In this intitial
\r
59 attempt at factoring out some of the argument processing, we include
\r
60 support for this kind of arguments roughly in the style of getopt_long.
\r
62 Makefile.local | 1 +
\r
63 notmuch-opts.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
64 notmuch-opts.h | 34 ++++++++++++++++++++++++++
\r
65 3 files changed, 106 insertions(+), 0 deletions(-)
\r
66 create mode 100644 notmuch-opts.c
\r
67 create mode 100644 notmuch-opts.h
\r
69 diff --git a/Makefile.local b/Makefile.local
\r
70 index c94402b..6606be8 100644
\r
71 --- a/Makefile.local
\r
72 +++ b/Makefile.local
\r
73 @@ -303,6 +303,7 @@ notmuch_client_srcs = \
\r
81 diff --git a/notmuch-opts.c b/notmuch-opts.c
\r
82 new file mode 100644
\r
83 index 0000000..6aac94c
\r
85 +++ b/notmuch-opts.c
\r
87 +#include <assert.h>
\r
88 +#include <string.h>
\r
90 +#include "notmuch-opts.h"
\r
93 +parse_argument(const char *name,
\r
95 + const notmuch_opt_desc_t *options,
\r
96 + notmuch_opt_t *state){
\r
103 + state->arg_id = 0;
\r
104 + state->keyword_id = 0;
\r
106 + if (strncmp (arg,"--",2) != 0) {
\r
107 + state->arg_id = '*';
\r
108 + state->string = arg;
\r
112 + if (strlen (arg) == 2) {
\r
113 + state->arg_id = '-';
\r
119 + const notmuch_opt_desc_t *try = options;
\r
120 + while (try->name) {
\r
121 + if (strncmp (arg, try->name, strlen(try->name) ) == 0) {
\r
122 + state->arg_id = try->arg_id;
\r
123 + if (try->opt_type == NOTMUCH_OPT_KEYWORD) {
\r
124 + notmuch_keyword_t *try_arg = try->keywords;
\r
125 + const char *arg_str=arg + strlen(try->name);
\r
127 + if (arg_str[0] != ':' && arg_str[0] != '=') {
\r
128 + fprintf (stderr, "%s %s: syntax error\n", name, arg);
\r
132 + /* skip delimiter */
\r
135 + while (try_arg->name && state->keyword_id == 0) {
\r
136 + if (strcmp (arg_str, try_arg->name) == 0) {
\r
137 + state->keyword_id = try_arg->keyword_id;
\r
141 + if (state->keyword_id == 0) {
\r
142 + fprintf (stderr, "%s %s: missing keyword\n", name, arg);
\r
151 + if (state->arg_id == 0) {
\r
152 + fprintf (stderr, "%s %s: unknown argument\n", name, arg);
\r
158 diff --git a/notmuch-opts.h b/notmuch-opts.h
\r
159 new file mode 100644
\r
160 index 0000000..c442e3f
\r
162 +++ b/notmuch-opts.h
\r
164 +#ifndef NOTMUCH_OPTS_H
\r
165 +#define NOTMUCH_OPTS_H
\r
167 +#include "notmuch.h"
\r
169 +enum notmuch_opt_type {
\r
170 + NOTMUCH_OPT_NULL = 0,
\r
171 + NOTMUCH_OPT_NO_ARG,
\r
172 + NOTMUCH_OPT_KEYWORD
\r
175 +typedef struct notmuch_keyword {
\r
176 + const char *name;
\r
178 +} notmuch_keyword_t;
\r
180 +typedef struct notmuch_opt_desc {
\r
181 + const char *name;
\r
183 + enum notmuch_opt_type opt_type;
\r
184 + struct notmuch_keyword *keywords;
\r
185 +} notmuch_opt_desc_t;
\r
187 +typedef struct notmuch_opt {
\r
190 + const char *string;
\r
194 +parse_argument (const char* name, const char *arg, const notmuch_opt_desc_t *options,
\r
195 + notmuch_opt_t *result);
\r