NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
message.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <ctype.h>
31#include <stdbool.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include "private.h"
36#include "mutt/lib.h"
37#include "core/lib.h"
38#include "lib.h"
39#include "menu/lib.h"
40#include "mview.h"
41
42#define KILO 1024
43#define MEGA 1048576
44
49{
53};
54
62static int report_regerror(int regerr, regex_t *preg, struct Buffer *err)
63{
64 size_t ds = err->dsize;
65
66 if (regerror(regerr, preg, err->data, ds) > ds)
67 mutt_debug(LL_DEBUG2, "warning: buffer too small for regerror\n");
68 /* The return value is fixed, exists only to shorten code at callsite */
69 return RANGE_E_SYNTAX;
70}
71
82static bool is_menu_available(struct Buffer *s, regmatch_t pmatch[], int kind,
83 struct Buffer *err, const struct Menu *menu)
84{
85 const char *context_req_chars[] = {
86 [RANGE_K_REL] = ".0123456789",
87 [RANGE_K_ABS] = ".",
88 [RANGE_K_LT] = "",
89 [RANGE_K_GT] = "",
90 [RANGE_K_BARE] = ".",
91 };
92
93 /* First decide if we're going to need the menu at all.
94 * Relative patterns need it if they contain a dot or a number.
95 * Absolute patterns only need it if they contain a dot. */
96 char *context_loc = strpbrk(s->dptr + pmatch[0].rm_so, context_req_chars[kind]);
97 if (!context_loc || (context_loc >= &s->dptr[pmatch[0].rm_eo]))
98 return true;
99
100 /* We need a current message. Do we actually have one? */
101 if (menu)
102 return true;
103
104 /* Nope. */
105 buf_strcpy(err, _("No current message"));
106 return false;
107}
108
118static int scan_range_num(struct Buffer *s, regmatch_t pmatch[], int group,
119 int kind, struct MailboxView *mv)
120{
121 int num = (int) strtol(&s->dptr[pmatch[group].rm_so], NULL, 0);
122 unsigned char c = (unsigned char) (s->dptr[pmatch[group].rm_eo - 1]);
123 if (toupper(c) == 'K')
124 num *= KILO;
125 else if (toupper(c) == 'M')
126 num *= MEGA;
127 switch (kind)
128 {
129 case RANGE_K_REL:
130 {
131 struct Mailbox *m = mv->mailbox;
132 struct Menu *menu = mv->menu;
133 struct Email *e = mutt_get_virt_email(m, menu_get_index(menu));
134 if (!e)
135 return num;
136 return num + email_msgno(e);
137 }
138 case RANGE_K_LT:
139 return num - 1;
140 case RANGE_K_GT:
141 return num + 1;
142 default:
143 return num;
144 }
145}
146
157static int scan_range_slot(struct Buffer *s, regmatch_t pmatch[], int grp,
158 int side, int kind, struct MailboxView *mv)
159{
160 struct Mailbox *m = mv->mailbox;
161 struct Menu *menu = mv->menu;
162
163 /* This means the left or right subpattern was empty, e.g. ",." */
164 if ((pmatch[grp].rm_so == -1) || (pmatch[grp].rm_so == pmatch[grp].rm_eo))
165 {
166 if (side == RANGE_S_LEFT)
167 return 1;
168 if (side == RANGE_S_RIGHT)
169 return m->msg_count;
170 }
171 /* We have something, so determine what */
172 unsigned char c = (unsigned char) (s->dptr[pmatch[grp].rm_so]);
173 switch (c)
174 {
175 case RANGE_CIRCUM:
176 return 1;
177 case RANGE_DOLLAR:
178 return m->msg_count;
179 case RANGE_DOT:
180 {
181 struct Email *e = mutt_get_virt_email(m, menu_get_index(menu));
182 if (!e)
183 return 1;
184 return email_msgno(e);
185 }
186 case RANGE_LT:
187 case RANGE_GT:
188 return scan_range_num(s, pmatch, grp + 1, kind, mv);
189 default:
190 /* Only other possibility: a number */
191 return scan_range_num(s, pmatch, grp, kind, mv);
192 }
193}
194
199static void order_range(struct Pattern *pat)
200{
201 if (pat->min <= pat->max)
202 return;
203 long num = pat->min;
204 pat->min = pat->max;
205 pat->max = num;
206}
207
217static int eat_range_by_regex(struct Pattern *pat, struct Buffer *s, int kind,
218 struct Buffer *err, struct MailboxView *mv)
219{
220 int regerr;
221 regmatch_t pmatch[RANGE_RX_GROUPS] = { 0 };
222 struct RangeRegex *pspec = &RangeRegexes[kind];
223
224 /* First time through, compile the big regex */
225 if (!pspec->ready)
226 {
227 regerr = regcomp(&pspec->cooked, pspec->raw, REG_EXTENDED);
228 if (regerr != 0)
229 return report_regerror(regerr, &pspec->cooked, err);
230 pspec->ready = true;
231 }
232
233 /* Match the pattern buffer against the compiled regex.
234 * No match means syntax error. */
235 regerr = regexec(&pspec->cooked, s->dptr, RANGE_RX_GROUPS, pmatch, 0);
236 if (regerr != 0)
237 return report_regerror(regerr, &pspec->cooked, err);
238
239 struct Mailbox *m = mv->mailbox;
240 struct Menu *menu = mv->menu;
241 if (!is_menu_available(s, pmatch, kind, err, menu))
242 return RANGE_E_MVIEW;
243
244 /* Snarf the contents of the two sides of the range. */
245 pat->min = scan_range_slot(s, pmatch, pspec->lgrp, RANGE_S_LEFT, kind, mv);
246 pat->max = scan_range_slot(s, pmatch, pspec->rgrp, RANGE_S_RIGHT, kind, mv);
247 mutt_debug(LL_DEBUG1, "pat->min=%ld pat->max=%ld\n", pat->min, pat->max);
248
249 /* Special case for a bare 0. */
250 if ((kind == RANGE_K_BARE) && (pat->min == 0) && (pat->max == 0))
251 {
252 if (!m || !menu)
253 {
254 buf_strcpy(err, _("No current message"));
255 return RANGE_E_MVIEW;
256 }
257 struct Email *e = mutt_get_virt_email(m, menu_get_index(menu));
258 if (!e)
259 return RANGE_E_MVIEW;
260
261 pat->max = email_msgno(e);
262 pat->min = pat->max;
263 }
264
265 /* Since we don't enforce order, we must swap bounds if they're backward */
266 order_range(pat);
267
268 /* Slide pointer past the entire match. */
269 s->dptr += pmatch[0].rm_eo;
270 return RANGE_E_OK;
271}
272
283 struct Buffer *s, struct Buffer *err, struct MailboxView *mv)
284{
285 if (!mv || !mv->mailbox || !mv->menu)
286 {
287 // We need these for pretty much anything
288 buf_strcpy(err, _("No mailbox is open"));
289 return false;
290 }
291
292 bool skip_quote = false;
293
294 /* If simple_search is set to "~m %s", the range will have double quotes
295 * around it... */
296 if (*s->dptr == '"')
297 {
298 s->dptr++;
299 skip_quote = true;
300 }
301
302 for (int i_kind = 0; i_kind != RANGE_K_INVALID; i_kind++)
303 {
304 switch (eat_range_by_regex(pat, s, i_kind, err, mv))
305 {
306 case RANGE_E_MVIEW:
307 /* This means it matched syntactically but lacked context.
308 * No point in continuing. */
309 break;
310 case RANGE_E_SYNTAX:
311 /* Try another syntax, then */
312 continue;
313 case RANGE_E_OK:
314 if (skip_quote && (*s->dptr == '"'))
315 s->dptr++;
316 SKIPWS(s->dptr);
317 return true;
318 }
319 }
320 return false;
321}
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:394
Convenience wrapper for the core headers.
bool eat_message_range(struct Pattern *pat, PatternCompFlags flags, struct Buffer *s, struct Buffer *err, struct MailboxView *mv)
Parse a range of message numbers - Implements eat_arg_t -.
Definition: message.c:282
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
GUI present the user with a selectable list.
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition: menu.c:160
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
struct Email * mutt_get_virt_email(struct Mailbox *m, int vnum)
Get a virtual Email.
Definition: mview.c:418
View of a Mailbox.
uint8_t PatternCompFlags
Flags for mutt_pattern_comp(), e.g. MUTT_PC_FULL_MSG.
Definition: lib.h:67
static int eat_range_by_regex(struct Pattern *pat, struct Buffer *s, int kind, struct Buffer *err, struct MailboxView *mv)
Parse a range given as a regex.
Definition: message.c:217
static int scan_range_slot(struct Buffer *s, regmatch_t pmatch[], int grp, int side, int kind, struct MailboxView *mv)
Parse a range of message numbers.
Definition: message.c:157
#define MEGA
Definition: message.c:43
EatRangeError
Error codes for eat_range_by_regex()
Definition: message.c:49
@ RANGE_E_MVIEW
Range requires MailboxView, but none available.
Definition: message.c:52
@ RANGE_E_OK
Range is valid.
Definition: message.c:50
@ RANGE_E_SYNTAX
Range contains syntax error.
Definition: message.c:51
static int scan_range_num(struct Buffer *s, regmatch_t pmatch[], int group, int kind, struct MailboxView *mv)
Parse a number range.
Definition: message.c:118
#define KILO
Definition: message.c:42
static void order_range(struct Pattern *pat)
Put a range in order.
Definition: message.c:199
static int report_regerror(int regerr, regex_t *preg, struct Buffer *err)
Create a regex error message.
Definition: message.c:62
static bool is_menu_available(struct Buffer *s, regmatch_t pmatch[], int kind, struct Buffer *err, const struct Menu *menu)
Do we need a MailboxView for this Pattern?
Definition: message.c:82
@ RANGE_S_LEFT
Left side of range.
Definition: private.h:138
@ RANGE_S_RIGHT
Right side of range.
Definition: private.h:139
#define RANGE_DOLLAR
Definition: private.h:129
#define RANGE_GT
Definition: private.h:131
#define RANGE_RX_GROUPS
Definition: private.h:125
#define RANGE_CIRCUM
Definition: private.h:128
#define RANGE_DOT
Definition: private.h:127
static int email_msgno(struct Email *e)
Helper to get the Email's message number.
Definition: private.h:147
#define RANGE_LT
Definition: private.h:130
@ RANGE_K_REL
Relative range.
Definition: private.h:101
@ RANGE_K_ABS
Absolute range.
Definition: private.h:102
@ RANGE_K_LT
Less-than range.
Definition: private.h:103
@ RANGE_K_INVALID
Range is invalid.
Definition: private.h:107
@ RANGE_K_BARE
Single symbol.
Definition: private.h:105
@ RANGE_K_GT
Greater-than range.
Definition: private.h:104
struct RangeRegex RangeRegexes[]
Set of Regexes for various range types.
Definition: pattern.c:65
GUI display the mailboxes in a side panel.
Key value store.
#define SKIPWS(ch)
Definition: string2.h:45
String manipulation buffer.
Definition: buffer.h:36
char * dptr
Current read/write position.
Definition: buffer.h:38
size_t dsize
Length of data.
Definition: buffer.h:39
char * data
Pointer to data.
Definition: buffer.h:37
The envelope/body of an email.
Definition: email.h:39
View of a Mailbox.
Definition: mview.h:40
struct Menu * menu
Needed for pattern compilation.
Definition: mview.h:47
struct Mailbox * mailbox
Current Mailbox.
Definition: mview.h:51
A mailbox.
Definition: mailbox.h:79
int msg_count
Total number of messages.
Definition: mailbox.h:88
Definition: lib.h:79
A simple (non-regex) pattern.
Definition: lib.h:77
long min
Minimum for range checks.
Definition: lib.h:88
long max
Maximum for range checks.
Definition: lib.h:89
Regular expression representing a range.
Definition: private.h:88
int lgrp
Paren group matching the left side.
Definition: private.h:90
int rgrp
Paren group matching the right side.
Definition: private.h:91
regex_t cooked
Compiled form.
Definition: private.h:93
bool ready
Compiled yet?
Definition: private.h:92
const char * raw
Regex as string.
Definition: private.h:89