NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
message.c File Reference

Pattern handling for messages. More...

#include "config.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "private.h"
#include "mutt/lib.h"
#include "core/lib.h"
#include "lib.h"
#include "menu/lib.h"
#include "mview.h"
+ Include dependency graph for message.c:

Go to the source code of this file.

Macros

#define KILO   1024
 
#define MEGA   1048576
 

Enumerations

enum  EatRangeError { RANGE_E_OK , RANGE_E_SYNTAX , RANGE_E_MVIEW }
 Error codes for eat_range_by_regex() More...
 

Functions

static int report_regerror (int regerr, regex_t *preg, struct Buffer *err)
 Create a regex error message.
 
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?
 
static int scan_range_num (struct Buffer *s, regmatch_t pmatch[], int group, int kind, struct MailboxView *mv)
 Parse a number range.
 
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.
 
static void order_range (struct Pattern *pat)
 Put a range in order.
 
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.
 
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 -.
 

Detailed Description

Pattern handling for messages.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file message.c.

Macro Definition Documentation

◆ KILO

#define KILO   1024

Definition at line 42 of file message.c.

◆ MEGA

#define MEGA   1048576

Definition at line 43 of file message.c.

Enumeration Type Documentation

◆ EatRangeError

Error codes for eat_range_by_regex()

Enumerator
RANGE_E_OK 

Range is valid.

RANGE_E_SYNTAX 

Range contains syntax error.

RANGE_E_MVIEW 

Range requires MailboxView, but none available.

Definition at line 48 of file message.c.

49{
53};
@ 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

Function Documentation

◆ report_regerror()

static int report_regerror ( int  regerr,
regex_t *  preg,
struct Buffer err 
)
static

Create a regex error message.

Parameters
regerrRegex error code
pregRegex pattern buffer
errBuffer for error messages
Return values
RANGE_E_SYNTAXAlways

Definition at line 62 of file message.c.

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}
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
size_t dsize
Length of data.
Definition: buffer.h:39
char * data
Pointer to data.
Definition: buffer.h:37
+ Here is the caller graph for this function:

◆ is_menu_available()

static bool is_menu_available ( struct Buffer s,
regmatch_t  pmatch[],
int  kind,
struct Buffer err,
const struct Menu menu 
)
static

Do we need a MailboxView for this Pattern?

Parameters
sString to check
pmatchRegex matches
kindRange type, e.g. RANGE_K_REL
errBuffer for error messages
menuCurrent Menu
Return values
falseMailboxView is required, but not available
trueOtherwise

Definition at line 82 of file message.c.

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}
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
#define _(a)
Definition: message.h:28
@ 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_BARE
Single symbol.
Definition: private.h:94
@ RANGE_K_GT
Greater-than range.
Definition: private.h:93
char * dptr
Current read/write position.
Definition: buffer.h:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scan_range_num()

static int scan_range_num ( struct Buffer s,
regmatch_t  pmatch[],
int  group,
int  kind,
struct MailboxView mv 
)
static

Parse a number range.

Parameters
sString to parse
pmatchArray of regex matches
groupIndex of regex match to use
kindRange type, e.g. RANGE_K_REL
mvMailbox view
Return values
numParse number

Definition at line 118 of file message.c.

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}
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition: menu.c:160
struct Email * mutt_get_virt_email(struct Mailbox *m, int vnum)
Get a virtual Email.
Definition: mview.c:418
#define MEGA
Definition: message.c:43
#define KILO
Definition: message.c:42
static int email_msgno(struct Email *e)
Helper to get the Email's message number.
Definition: private.h:136
The envelope/body of an email.
Definition: email.h:39
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
Definition: lib.h:79
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scan_range_slot()

static int scan_range_slot ( struct Buffer s,
regmatch_t  pmatch[],
int  grp,
int  side,
int  kind,
struct MailboxView mv 
)
static

Parse a range of message numbers.

Parameters
sString to parse
pmatchRegex matches
grpWhich regex match to use
sideWhich side of the range is this? RANGE_S_LEFT or RANGE_S_RIGHT
kindRange type, e.g. RANGE_K_REL
mvMailbox view
Return values
numIndex number for the message specified

Definition at line 157 of file message.c.

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}
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
@ 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_CIRCUM
Definition: private.h:117
#define RANGE_DOT
Definition: private.h:116
#define RANGE_LT
Definition: private.h:119
int msg_count
Total number of messages.
Definition: mailbox.h:88
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ order_range()

static void order_range ( struct Pattern pat)
static

Put a range in order.

Parameters
patPattern to check

Definition at line 199 of file message.c.

200{
201 if (pat->min <= pat->max)
202 return;
203 long num = pat->min;
204 pat->min = pat->max;
205 pat->max = num;
206}
long min
Minimum for range checks.
Definition: lib.h:87
long max
Maximum for range checks.
Definition: lib.h:88
+ Here is the caller graph for this function:

◆ eat_range_by_regex()

static int eat_range_by_regex ( struct Pattern pat,
struct Buffer s,
int  kind,
struct Buffer err,
struct MailboxView mv 
)
static

Parse a range given as a regex.

Parameters
patPattern to store the range in
sString to parse
kindRange type, e.g. RANGE_K_REL
errBuffer for error messages
mvMailbox view
Return values
numEatRangeError code, e.g. RANGE_E_OK

Definition at line 217 of file message.c.

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}
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
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
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
#define RANGE_RX_GROUPS
Definition: private.h:114
struct RangeRegex RangeRegexes[]
Set of Regexes for various range types.
Definition: pattern.c:65
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
+ Here is the call graph for this function:
+ Here is the caller graph for this function: