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