NeoMutt  2025-09-05-55-g97fc89
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
commands.h File Reference

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

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

Go to the source code of this file.

Macros

#define MUTT_NAMED   (1 << 0)
 

Functions

enum CommandResult parse_mailboxes (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'mailboxes' command - Implements Command::parse() -.
 
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() -.
 
enum CommandResult parse_subjectrx_list (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'subjectrx' command - Implements Command::parse() -.
 
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() -.
 
enum CommandResult parse_unalternates (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unalternates' command - Implements Command::parse() -.
 
enum CommandResult parse_unmailboxes (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unmailboxes' command - Implements Command::parse() -.
 
enum CommandResult parse_unsubjectrx_list (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unsubjectrx' command - Implements Command::parse() -.
 
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() -.
 
enum CommandResult parse_rc_line_cwd (const char *line, char *cwd, struct Buffer *err)
 Parse and run a muttrc line in a relative directory.
 
char * mutt_get_sourced_cwd (void)
 Get the current file path that is being parsed.
 
bool mailbox_add_simple (const char *mailbox, struct Buffer *err)
 Add a new Mailbox.
 
int parse_grouplist (struct GroupList *gl, struct Buffer *buf, struct Buffer *s, struct Buffer *err)
 Parse a group context.
 
void source_stack_cleanup (void)
 Free memory from the stack used for the source command.
 
int source_rc (const char *rcfile_path, struct Buffer *err)
 Read an initialization file.
 
enum CommandResult set_dump (enum GetElemListFlags flags, struct Buffer *err)
 Dump list of config variables into a file/pager.
 

Detailed Description

Functions to parse commands in a config file.

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

Macro Definition Documentation

◆ MUTT_NAMED

#define MUTT_NAMED   (1 << 0)

Definition at line 36 of file commands.h.

Function Documentation

◆ parse_rc_line_cwd()

enum CommandResult 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 178 of file commands.c.

179{
181
182 enum CommandResult ret = parse_rc_line(line, err);
183
184 struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
186 FREE(&np->data);
187 FREE(&np);
188
189 return ret;
190}
CommandResult
Error codes for command_t parse functions.
Definition command.h:35
static struct ListHead MuttrcStack
LIFO designed to contain the list of config files that have been sourced and avoid cyclic sourcing.
Definition commands.c:77
struct ListNode * mutt_list_insert_head(struct ListHead *h, char *s)
Insert a string at the beginning of a List.
Definition list.c:46
#define FREE(x)
Definition memory.h:62
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:255
#define STAILQ_REMOVE_HEAD(head, field)
Definition queue.h:461
#define STAILQ_FIRST(head)
Definition queue.h:388
enum CommandResult parse_rc_line(const char *line, struct Buffer *err)
Parse a line of user config.
Definition rc.c:109
#define NONULL(x)
Definition string2.h:43
A List node for strings.
Definition list.h:37
char * data
String.
Definition list.h:38
+ 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 198 of file commands.c.

199{
200 struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
201 if (np && np->data)
202 return mutt_str_dup(np->data);
203
204 // stack is empty, return our own dummy file relative to cwd
205 struct Buffer *cwd = buf_pool_get();
206 mutt_path_getcwd(cwd);
207 buf_addstr(cwd, "/dummy.rc");
208 char *ret = buf_strdup(cwd);
209 buf_pool_release(&cwd);
210 return ret;
211}
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:226
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
const char * mutt_path_getcwd(struct Buffer *cwd)
Get the current working directory.
Definition path.c:476
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:96
String manipulation buffer.
Definition buffer.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_add_simple()

bool mailbox_add_simple ( const char * mailbox,
struct Buffer * err )

Add a new Mailbox.

Parameters
mailboxMailbox to add
errBuffer for error messages
Return values
trueSuccess

Definition at line 741 of file commands.c.

742{
743 enum CommandResult rc = mailbox_add("", mailbox, NULL, TB_UNSET, TB_UNSET, err);
744
745 return (rc == MUTT_CMD_SUCCESS);
746}
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition command.h:38
static enum CommandResult mailbox_add(const char *folder, const char *mailbox, const char *label, enum TriBool poll, enum TriBool notify, struct Buffer *err)
Add a new Mailbox.
Definition commands.c:637
@ TB_UNSET
Value hasn't been set.
Definition commands.c:86
+ 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 144 of file commands.c.

146{
147 while (mutt_istr_equal(buf->data, "-group"))
148 {
149 if (!MoreArgs(s))
150 {
151 buf_strcpy(err, _("-group: no group name"));
152 return -1;
153 }
154
156
158
159 if (!MoreArgs(s))
160 {
161 buf_strcpy(err, _("out of arguments"));
162 return -1;
163 }
164
166 }
167
168 return 0;
169}
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:395
int parse_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition extract.c:48
#define MoreArgs(buf)
Definition extract.h:30
#define TOKEN_NO_FLAGS
No flags are set.
Definition extract.h:44
void mutt_grouplist_add(struct GroupList *gl, struct Group *group)
Add a Group to a GroupList.
Definition group.c:182
struct Group * mutt_pattern_group(const char *pat)
Match a pattern to a Group.
Definition group.c:117
#define _(a)
Definition message.h:28
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:672
char * data
Pointer to data.
Definition buffer.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ source_stack_cleanup()

void source_stack_cleanup ( void )

Free memory from the stack used for the source command.

Definition at line 1671 of file commands.c.

1672{
1674}
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition list.c:123
+ 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 219 of file commands.c.

220{
221 int lineno = 0, rc = 0, warnings = 0;
222 enum CommandResult line_rc;
223 struct Buffer *token = NULL, *linebuf = NULL;
224 char *line = NULL;
225 char *currentline = NULL;
226 char rcfile[PATH_MAX + 1] = { 0 };
227 size_t linelen = 0;
228 pid_t pid;
229
230 mutt_str_copy(rcfile, rcfile_path, sizeof(rcfile));
231
232 size_t rcfilelen = mutt_str_len(rcfile);
233 if (rcfilelen == 0)
234 return -1;
235
236 bool ispipe = rcfile[rcfilelen - 1] == '|';
237
238 if (!ispipe)
239 {
240 struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
241 if (!mutt_path_to_absolute(rcfile, np ? NONULL(np->data) : ""))
242 {
243 mutt_error(_("Error: Can't build path of '%s'"), rcfile_path);
244 return -1;
245 }
246
247 STAILQ_FOREACH(np, &MuttrcStack, entries)
248 {
249 if (mutt_str_equal(np->data, rcfile))
250 {
251 break;
252 }
253 }
254 if (np)
255 {
256 mutt_error(_("Error: Cyclic sourcing of configuration file '%s'"), rcfile);
257 return -1;
258 }
259
261 }
262
263 mutt_debug(LL_DEBUG2, "Reading configuration file '%s'\n", rcfile);
264
265 FILE *fp = mutt_open_read(rcfile, &pid);
266 if (!fp)
267 {
268 buf_printf(err, "%s: %s", rcfile, strerror(errno));
269 return -1;
270 }
271
272 token = buf_pool_get();
273 linebuf = buf_pool_get();
274
275 const char *const c_config_charset = cs_subset_string(NeoMutt->sub, "config_charset");
276 const char *const c_charset = cc_charset();
277 while ((line = mutt_file_read_line(line, &linelen, fp, &lineno, MUTT_RL_CONT)) != NULL)
278 {
279 const bool conv = c_config_charset && c_charset;
280 if (conv)
281 {
282 currentline = mutt_str_dup(line);
283 if (!currentline)
284 continue;
285 mutt_ch_convert_string(&currentline, c_config_charset, c_charset, MUTT_ICONV_NO_FLAGS);
286 }
287 else
288 {
289 currentline = line;
290 }
291
292 buf_strcpy(linebuf, currentline);
293
294 buf_reset(err);
295 line_rc = parse_rc_buffer(linebuf, token, err);
296 if (line_rc == MUTT_CMD_ERROR)
297 {
298 mutt_error("%s:%d: %s", rcfile, lineno, buf_string(err));
299 if (--rc < -MAX_ERRS)
300 {
301 if (conv)
302 FREE(&currentline);
303 break;
304 }
305 }
306 else if (line_rc == MUTT_CMD_WARNING)
307 {
308 /* Warning */
309 mutt_warning("%s:%d: %s", rcfile, lineno, buf_string(err));
310 warnings++;
311 }
312 else if (line_rc == MUTT_CMD_FINISH)
313 {
314 if (conv)
315 FREE(&currentline);
316 break; /* Found "finish" command */
317 }
318 else
319 {
320 if (rc < 0)
321 rc = -1;
322 }
323 if (conv)
324 FREE(&currentline);
325 }
326
327 FREE(&line);
328 mutt_file_fclose(&fp);
329 if (pid != -1)
330 filter_wait(pid);
331
332 if (rc)
333 {
334 /* the neomuttrc source keyword */
335 buf_reset(err);
336 buf_printf(err, (rc >= -MAX_ERRS) ? _("source: errors in %s") : _("source: reading aborted due to too many errors in %s"),
337 rcfile);
338 rc = -1;
339 }
340 else
341 {
342 /* Don't alias errors with warnings */
343 if (warnings > 0)
344 {
345 buf_printf(err, ngettext("source: %d warning in %s", "source: %d warnings in %s", warnings),
346 warnings, rcfile);
347 rc = -2;
348 }
349 }
350
351 if (!ispipe && !STAILQ_EMPTY(&MuttrcStack))
352 {
353 struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
355 FREE(&np->data);
356 FREE(&np);
357 }
358
359 buf_pool_release(&token);
360 buf_pool_release(&linebuf);
361 return rc;
362}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition command.h:36
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition command.h:37
@ MUTT_CMD_FINISH
Finish: Stop processing this file.
Definition command.h:39
#define MAX_ERRS
Definition commands.c:79
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
const char * cc_charset(void)
Get the cached value of $charset.
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:685
#define MUTT_RL_CONT
-continuation
Definition file.h:41
#define mutt_file_fclose(FP)
Definition file.h:139
#define mutt_warning(...)
Definition logging2.h:91
#define mutt_error(...)
Definition logging2.h:93
#define mutt_debug(LEVEL,...)
Definition logging2.h:90
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:45
int mutt_ch_convert_string(char **ps, const char *from, const char *to, uint8_t flags)
Convert a string between encodings.
Definition charset.c:831
#define MUTT_ICONV_NO_FLAGS
No flags are set.
Definition charset.h:64
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition filter.c:220
bool mutt_path_to_absolute(char *path, const char *reference)
Convert a relative path to its absolute form.
Definition path.c:333
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:660
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:498
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:581
#define PATH_MAX
Definition mutt.h:42
FILE * mutt_open_read(const char *path, pid_t *thepid)
Run a command to read from.
Definition muttlib.c:699
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
#define STAILQ_EMPTY(head)
Definition queue.h:382
enum CommandResult parse_rc_buffer(struct Buffer *line, struct Buffer *token, struct Buffer *err)
Parse a line of user config.
Definition rc.c:47
Container for Accounts, Notifications.
Definition neomutt.h:43
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:47
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_dump()

enum CommandResult set_dump ( enum GetElemListFlags flags,
struct Buffer * err )

Dump list of config variables into a file/pager.

Parameters
flagsWhich config to dump, e.g. GEL_CHANGED_CONFIG
errBuffer for error message
Returns
num See CommandResult

FIXME: Move me into parse/set.c. Note: this function currently depends on pager, which is the reason it is not included in the parse library.

Definition at line 888 of file commands.c.

889{
890 struct Buffer *tempfile = buf_pool_get();
891 buf_mktemp(tempfile);
892
893 FILE *fp_out = mutt_file_fopen(buf_string(tempfile), "w");
894 if (!fp_out)
895 {
896 // L10N: '%s' is the file name of the temporary file
897 buf_printf(err, _("Could not create temporary file %s"), buf_string(tempfile));
898 buf_pool_release(&tempfile);
899 return MUTT_CMD_ERROR;
900 }
901
902 struct ConfigSet *cs = NeoMutt->sub->cs;
903 struct HashElemArray hea = get_elem_list(cs, flags);
904 dump_config(cs, &hea, CS_DUMP_NO_FLAGS, fp_out);
905 ARRAY_FREE(&hea);
906
907 mutt_file_fclose(&fp_out);
908
909 struct PagerData pdata = { 0 };
910 struct PagerView pview = { &pdata };
911
912 pdata.fname = buf_string(tempfile);
913
914 pview.banner = "set";
916 pview.mode = PAGER_MODE_OTHER;
917
918 mutt_do_pager(&pview, NULL);
919 buf_pool_release(&tempfile);
920
921 return MUTT_CMD_SUCCESS;
922}
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:204
bool dump_config(struct ConfigSet *cs, struct HashElemArray *hea, ConfigDumpFlags flags, FILE *fp)
Write all the config to a file.
Definition dump.c:196
#define CS_DUMP_NO_FLAGS
No flags are set.
Definition dump.h:36
int mutt_do_pager(struct PagerView *pview, struct Email *e)
Display some page-able text to the user (help or attachment)
Definition do_pager.c:122
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
#define MUTT_PAGER_NO_FLAGS
No flags are set.
Definition lib.h:60
@ PAGER_MODE_OTHER
Pager is invoked via 3rd path. Non-email content is likely to be shown.
Definition lib.h:140
Container for lots of config items.
Definition set.h:248
struct ConfigSet * cs
Parent ConfigSet.
Definition subset.h:50
Data to be displayed by PagerView.
Definition lib.h:159
const char * fname
Name of the file to read.
Definition lib.h:163
Paged view into some data.
Definition lib.h:170
struct PagerData * pdata
Data that pager displays. NOTNULL.
Definition lib.h:171
enum PagerMode mode
Pager mode.
Definition lib.h:172
PagerFlags flags
Additional settings to tweak pager's function.
Definition lib.h:173
const char * banner
Title to display in status bar.
Definition lib.h:174
struct HashElemArray get_elem_list(struct ConfigSet *cs, enum GetElemListFlags flags)
Create a sorted list of all config items.
Definition subset.c:81
#define buf_mktemp(buf)
Definition tmp.h:33
+ Here is the call graph for this function:
+ Here is the caller graph for this function: