Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / d0 / 29c208fd6d646b01bd95f9ae53c0ea8eb29e20
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
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -2.3\r
10 X-Spam-Level: \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
33         notmuch.\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
37 In-Reply-To:\r
38  <7574621578db9bbfc88348c87e2f5431f4cb2296.1321223343.git.jani@nikula.org>\r
39 References:\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
44 Precedence: list\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
55 \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
61 ---\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
68 \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
74         notmuch-count.c         \\r
75         notmuch-dump.c          \\r
76         notmuch-new.c           \\r
77 +       notmuch-opts.c          \\r
78         notmuch-reply.c         \\r
79         notmuch-restore.c       \\r
80         notmuch-search.c        \\r
81 diff --git a/notmuch-opts.c b/notmuch-opts.c\r
82 new file mode 100644\r
83 index 0000000..6aac94c\r
84 --- /dev/null\r
85 +++ b/notmuch-opts.c\r
86 @@ -0,0 +1,71 @@\r
87 +#include <assert.h>\r
88 +#include <string.h>\r
89 +#include <stdio.h>\r
90 +#include "notmuch-opts.h"\r
91 +\r
92 +notmuch_bool_t\r
93 +parse_argument(const char *name,\r
94 +              const char *arg,\r
95 +              const notmuch_opt_desc_t *options,\r
96 +              notmuch_opt_t *state){\r
97 +\r
98 +    assert(arg);\r
99 +    assert(name);\r
100 +    assert(options);\r
101 +    assert(state);\r
102 +\r
103 +    state->arg_id = 0;\r
104 +    state->keyword_id = 0;\r
105 +\r
106 +    if (strncmp (arg,"--",2) != 0) {\r
107 +       state->arg_id = '*';\r
108 +       state->string = arg;\r
109 +       return TRUE;\r
110 +    }\r
111 +\r
112 +    if (strlen (arg) == 2) {\r
113 +       state->arg_id = '-';\r
114 +       return FALSE;\r
115 +    }\r
116 +\r
117 +    arg += 2;\r
118 +\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
126 +\r
127 +               if (arg_str[0] != ':' && arg_str[0] != '=') {\r
128 +                   fprintf (stderr, "%s %s: syntax error\n", name, arg);\r
129 +                   return FALSE;\r
130 +               }\r
131 +\r
132 +               /* skip delimiter */\r
133 +               arg_str++;\r
134 +\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
138 +                   }\r
139 +                   try_arg++;\r
140 +               }\r
141 +               if (state->keyword_id == 0) {\r
142 +                   fprintf (stderr, "%s %s: missing keyword\n", name, arg);\r
143 +                   return FALSE;\r
144 +               }\r
145 +\r
146 +\r
147 +           }\r
148 +       }\r
149 +       try++;\r
150 +    }\r
151 +    if (state->arg_id == 0) {\r
152 +       fprintf (stderr, "%s %s: unknown argument\n", name, arg);\r
153 +       return FALSE;\r
154 +    }\r
155 +\r
156 +    return TRUE;\r
157 +}\r
158 diff --git a/notmuch-opts.h b/notmuch-opts.h\r
159 new file mode 100644\r
160 index 0000000..c442e3f\r
161 --- /dev/null\r
162 +++ b/notmuch-opts.h\r
163 @@ -0,0 +1,34 @@\r
164 +#ifndef NOTMUCH_OPTS_H\r
165 +#define NOTMUCH_OPTS_H\r
166 +\r
167 +#include "notmuch.h"\r
168 +\r
169 +enum notmuch_opt_type {\r
170 +    NOTMUCH_OPT_NULL = 0,\r
171 +    NOTMUCH_OPT_NO_ARG,\r
172 +    NOTMUCH_OPT_KEYWORD\r
173 +};\r
174 +\r
175 +typedef struct notmuch_keyword {\r
176 +    const char *name;\r
177 +    int keyword_id;\r
178 +} notmuch_keyword_t;\r
179 +\r
180 +typedef struct notmuch_opt_desc {\r
181 +    const char *name;\r
182 +    int  arg_id;\r
183 +    enum notmuch_opt_type opt_type;\r
184 +    struct notmuch_keyword *keywords;\r
185 +} notmuch_opt_desc_t;\r
186 +\r
187 +typedef struct notmuch_opt {\r
188 +    int arg_id;\r
189 +    int keyword_id;\r
190 +    const char *string;\r
191 +} notmuch_opt_t;\r
192 +\r
193 +notmuch_bool_t\r
194 +parse_argument (const char* name, const char *arg, const notmuch_opt_desc_t *options,\r
195 +               notmuch_opt_t *result);\r
196 +\r
197 +#endif\r
198 -- \r
199 1.7.5.4\r
200 \r