NeoMutt  2022-04-29-215-gc12b98
Teaching an old dog new tricks
DOXYGEN
command_parse.h File Reference

Functions to parse commands in a config file. More...

#include "config.h"
#include <stdint.h>
#include "core/lib.h"
+ Include dependency graph for command_parse.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

enum CommandResult parse_alternates (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'alternates' command - Implements Command::parse() -. More...
 
enum CommandResult parse_cd (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'cd' command - Implements Command::parse() -. More...
 
enum CommandResult parse_echo (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'echo' command - Implements Command::parse() -. More...
 
enum CommandResult parse_finish (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'finish' command - Implements Command::parse() -. More...
 
enum CommandResult parse_group (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'group' and 'ungroup' commands - Implements Command::parse() -. More...
 
enum CommandResult parse_ifdef (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'ifdef' and 'ifndef' commands - Implements Command::parse() -. More...
 
enum CommandResult parse_ignore (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'ignore' command - Implements Command::parse() -. More...
 
enum CommandResult parse_lists (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'lists' command - Implements Command::parse() -. More...
 
enum CommandResult parse_mailboxes (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'mailboxes' command - Implements Command::parse() -. More...
 
enum CommandResult parse_my_hdr (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'my_hdr' command - Implements Command::parse() -. More...
 
enum CommandResult parse_set (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'set' family of commands - Implements Command::parse() -. More...
 
enum CommandResult parse_setenv (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'setenv' and 'unsetenv' commands - Implements Command::parse() -. More...
 
enum CommandResult parse_source (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'source' command - Implements Command::parse() -. More...
 
enum CommandResult parse_spam_list (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'spam' and 'nospam' commands - Implements Command::parse() -. More...
 
enum CommandResult parse_stailq (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse a list command - Implements Command::parse() -. More...
 
enum CommandResult parse_subjectrx_list (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'subjectrx' command - Implements Command::parse() -. More...
 
enum CommandResult parse_subscribe (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'subscribe' command - Implements Command::parse() -. More...
 
enum CommandResult parse_subscribe_to (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'subscribe-to' command - Implements Command::parse() -. More...
 
enum CommandResult parse_tag_formats (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'tag-formats' command - Implements Command::parse() -. More...
 
enum CommandResult parse_tag_transforms (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'tag-transforms' command - Implements Command::parse() -. More...
 
enum CommandResult parse_unalternates (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unalternates' command - Implements Command::parse() -. More...
 
enum CommandResult parse_unignore (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unignore' command - Implements Command::parse() -. More...
 
enum CommandResult parse_unlists (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unlists' command - Implements Command::parse() -. More...
 
enum CommandResult parse_unmailboxes (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unmailboxes' command - Implements Command::parse() -. More...
 
enum CommandResult parse_unmy_hdr (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unmy_hdr' command - Implements Command::parse() -. More...
 
enum CommandResult parse_unstailq (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse an unlist command - Implements Command::parse() -. More...
 
enum CommandResult parse_unsubjectrx_list (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unsubjectrx' command - Implements Command::parse() -. More...
 
enum CommandResult parse_unsubscribe (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unsubscribe' command - Implements Command::parse() -. More...
 
enum CommandResult parse_unsubscribe_from (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unsubscribe-from' command - Implements Command::parse() -. More...
 
enum CommandResult mutt_parse_rc_line_cwd (const char *line, char *cwd, struct Buffer *err)
 Parse and run a muttrc line in a relative directory. More...
 
char * mutt_get_sourced_cwd (void)
 Get the current file path that is being parsed. More...
 
int parse_grouplist (struct GroupList *gl, struct Buffer *buf, struct Buffer *s, struct Buffer *err)
 Parse a group context. More...
 
void clear_source_stack (void)
 Free memory from the stack used for the source command. More...
 
int source_rc (const char *rcfile_path, struct Buffer *err)
 Read an initialization file. More...
 

Detailed Description

Functions to parse commands in a config file.

Authors
  • Michael R. Elkins
  • g10 Code GmbH

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 command_parse.h.

Function Documentation

◆ mutt_parse_rc_line_cwd()

enum CommandResult mutt_parse_rc_line_cwd ( const char *  line,
char *  cwd,
struct Buffer err 
)

Parse and run a muttrc line in a relative directory.

Parameters
lineLine to be parsed
cwdFile relative where to run the line
errWhere to write error messages
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

Definition at line 145 of file command_parse.c.

146{
148
149 enum CommandResult ret = mutt_parse_rc_line(line, err);
150
151 struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
153 FREE(&np->data);
154 FREE(&np);
155
156 return ret;
157}
CommandResult
Error codes for command_t parse functions.
Definition: command.h:34
static struct ListHead MuttrcStack
Definition: command_parse.c:67
enum CommandResult mutt_parse_rc_line(const char *line, struct Buffer *err)
Parse a line of user config.
Definition: init.c:894
struct ListNode * mutt_list_insert_head(struct ListHead *h, char *s)
Insert a string at the beginning of a List.
Definition: list.c:45
#define FREE(x)
Definition: memory.h:43
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
#define STAILQ_REMOVE_HEAD(head, field)
Definition: queue.h:422
#define STAILQ_FIRST(head)
Definition: queue.h:350
#define NONULL(x)
Definition: string2.h:37
A List node for strings.
Definition: list.h:35
char * data
String.
Definition: list.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_get_sourced_cwd()

char * mutt_get_sourced_cwd ( void  )

Get the current file path that is being parsed.

Return values
ptrFile path that is being parsed or cwd at runtime
Note
Caller is responsible for freeing returned string

Definition at line 165 of file command_parse.c.

166{
167 struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
168 if (np && np->data)
169 return mutt_str_dup(np->data);
170
171 // stack is empty, return our own dummy file relative to cwd
172 struct Buffer *cwd = mutt_buffer_pool_get();
173 mutt_path_getcwd(cwd);
174 mutt_buffer_addstr(cwd, "/dummy.rc");
175 char *ret = mutt_buffer_strdup(cwd);
177 return ret;
178}
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:233
char * mutt_buffer_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:447
const char * mutt_path_getcwd(struct Buffer *cwd)
Get the current working directory.
Definition: path.c:561
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
String manipulation buffer.
Definition: buffer.h:34
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_grouplist()

int parse_grouplist ( struct GroupList *  gl,
struct Buffer buf,
struct Buffer s,
struct Buffer err 
)

Parse a group context.

Parameters
glGroupList to add to
bufTemporary Buffer space
sBuffer containing string to be parsed
errBuffer for error messages
Return values
0Success
-1Error

Definition at line 111 of file command_parse.c.

113{
114 while (mutt_istr_equal(buf->data, "-group"))
115 {
116 if (!MoreArgs(s))
117 {
118 mutt_buffer_strcpy(err, _("-group: no group name"));
119 return -1;
120 }
121
123
125
126 if (!MoreArgs(s))
127 {
128 mutt_buffer_strcpy(err, _("out of arguments"));
129 return -1;
130 }
131
133 }
134
135 return 0;
136}
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:327
#define MoreArgs(buf)
Definition: buffer.h:40
void mutt_grouplist_add(struct GroupList *gl, struct Group *group)
Add a Group to a GroupList.
Definition: group.c:181
struct Group * mutt_pattern_group(const char *pat)
Match a pattern to a Group.
Definition: group.c:112
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:273
#define _(a)
Definition: message.h:28
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:819
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:67
char * data
Pointer to data.
Definition: buffer.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ clear_source_stack()

void clear_source_stack ( void  )

Free memory from the stack used for the source command.

Definition at line 1740 of file command_parse.c.

1741{
1743}
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:122
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ source_rc()

int source_rc ( const char *  rcfile_path,
struct Buffer err 
)

Read an initialization file.

Parameters
rcfile_pathPath to initialization file
errBuffer for error messages
Return values
<0NeoMutt should pause to let the user know

Definition at line 186 of file command_parse.c.

187{
188 int lineno = 0, rc = 0, warnings = 0;
189 enum CommandResult line_rc;
190 struct Buffer *token = NULL, *linebuf = NULL;
191 char *line = NULL;
192 char *currentline = NULL;
193 char rcfile[PATH_MAX] = { 0 };
194 size_t linelen = 0;
195 pid_t pid;
196
197 mutt_str_copy(rcfile, rcfile_path, sizeof(rcfile));
198
199 size_t rcfilelen = mutt_str_len(rcfile);
200 if (rcfilelen == 0)
201 return -1;
202
203 bool ispipe = rcfile[rcfilelen - 1] == '|';
204
205 if (!ispipe)
206 {
207 struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
208 if (!mutt_path_to_absolute(rcfile, np ? NONULL(np->data) : ""))
209 {
210 mutt_error(_("Error: Can't build path of '%s'"), rcfile_path);
211 return -1;
212 }
213
214 STAILQ_FOREACH(np, &MuttrcStack, entries)
215 {
216 if (mutt_str_equal(np->data, rcfile))
217 {
218 break;
219 }
220 }
221 if (np)
222 {
223 mutt_error(_("Error: Cyclic sourcing of configuration file '%s'"), rcfile);
224 return -1;
225 }
226
228 }
229
230 mutt_debug(LL_DEBUG2, "Reading configuration file '%s'\n", rcfile);
231
232 FILE *fp = mutt_open_read(rcfile, &pid);
233 if (!fp)
234 {
235 mutt_buffer_printf(err, "%s: %s", rcfile, strerror(errno));
236 return -1;
237 }
238
239 token = mutt_buffer_pool_get();
240 linebuf = mutt_buffer_pool_get();
241
242 while ((line = mutt_file_read_line(line, &linelen, fp, &lineno, MUTT_RL_CONT)) != NULL)
243 {
244 const char *const c_config_charset = cs_subset_string(NeoMutt->sub, "config_charset");
245 const char *const c_charset = cs_subset_string(NeoMutt->sub, "charset");
246 const bool conv = c_config_charset && c_charset;
247 if (conv)
248 {
249 currentline = mutt_str_dup(line);
250 if (!currentline)
251 continue;
252 mutt_ch_convert_string(&currentline, c_config_charset, c_charset, MUTT_ICONV_NO_FLAGS);
253 }
254 else
255 currentline = line;
256
257 mutt_buffer_strcpy(linebuf, currentline);
258
260 line_rc = mutt_parse_rc_buffer(linebuf, token, err);
261 if (line_rc == MUTT_CMD_ERROR)
262 {
263 mutt_error(_("Error in %s, line %d: %s"), rcfile, lineno, err->data);
264 if (--rc < -MAX_ERRS)
265 {
266 if (conv)
267 FREE(&currentline);
268 break;
269 }
270 }
271 else if (line_rc == MUTT_CMD_WARNING)
272 {
273 /* Warning */
274 mutt_warning(_("Warning in %s, line %d: %s"), rcfile, lineno, err->data);
275 warnings++;
276 }
277 else if (line_rc == MUTT_CMD_FINISH)
278 {
279 if (conv)
280 FREE(&currentline);
281 break; /* Found "finish" command */
282 }
283 else
284 {
285 if (rc < 0)
286 rc = -1;
287 }
288 if (conv)
289 FREE(&currentline);
290 }
291
292 FREE(&line);
293 mutt_file_fclose(&fp);
294 if (pid != -1)
295 filter_wait(pid);
296
297 if (rc)
298 {
299 /* the neomuttrc source keyword */
302 (rc >= -MAX_ERRS) ?
303 _("source: errors in %s") :
304 _("source: reading aborted due to too many errors in %s"),
305 rcfile);
306 rc = -1;
307 }
308 else
309 {
310 /* Don't alias errors with warnings */
311 if (warnings > 0)
312 {
313 mutt_buffer_printf(err, ngettext("source: %d warning in %s", "source: %d warnings in %s", warnings),
314 warnings, rcfile);
315 rc = -2;
316 }
317 }
318
319 if (!ispipe && !STAILQ_EMPTY(&MuttrcStack))
320 {
321 struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
323 FREE(&np->data);
324 FREE(&np);
325 }
326
328 mutt_buffer_pool_release(&linebuf);
329 return rc;
330}
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:168
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:85
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:35
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition: command.h:36
@ MUTT_CMD_FINISH
Finish: Stop processing this file.
Definition: command.h:38
#define MAX_ERRS
Definition: command_parse.c:69
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
char * mutt_file_read_line(char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags)
Read a line from a file.
Definition: file.c:720
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
#define MUTT_RL_CONT
-continuation
Definition: file.h:39
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:217
#define mutt_warning(...)
Definition: logging.h:85
#define mutt_error(...)
Definition: logging.h:87
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
enum CommandResult mutt_parse_rc_buffer(struct Buffer *line, struct Buffer *token, struct Buffer *err)
Parse a line of user config.
Definition: init.c:836
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
int mutt_ch_convert_string(char **ps, const char *from, const char *to, uint8_t flags)
Convert a string between encodings.
Definition: charset.c:752
#define MUTT_ICONV_NO_FLAGS
No flags are set.
Definition: charset.h:71
bool mutt_path_to_absolute(char *path, const char *reference)
Convert relative filepath to an absolute path.
Definition: path.c:397
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:567
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:652
#define PATH_MAX
Definition: mutt.h:40
FILE * mutt_open_read(const char *path, pid_t *thepid)
Run a command to read from.
Definition: muttlib.c:1311
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_EMPTY(head)
Definition: queue.h:348
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function: