NeoMutt  2023-11-03-107-g582dc1
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 <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.
 
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 (ConfigDumpFlags flags, struct Buffer *err)
 Dump list of config variables into a file/pager.
 

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 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 158 of file commands.c.

159{
161
162 enum CommandResult ret = parse_rc_line(line, err);
163
164 struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
166 FREE(&np->data);
167 FREE(&np);
168
169 return ret;
170}
CommandResult
Error codes for command_t parse functions.
Definition: command.h:36
static struct ListHead MuttrcStack
LIFO designed to contain the list of config files that have been sourced and avoid cyclic sourcing.
Definition: commands.c:70
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:45
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
#define STAILQ_REMOVE_HEAD(head, field)
Definition: queue.h:422
#define STAILQ_FIRST(head)
Definition: queue.h:350
enum CommandResult parse_rc_line(const char *line, struct Buffer *err)
Parse a line of user config.
Definition: rc.c:104
#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 178 of file commands.c.

179{
180 struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
181 if (np && np->data)
182 return mutt_str_dup(np->data);
183
184 // stack is empty, return our own dummy file relative to cwd
185 struct Buffer *cwd = buf_pool_get();
186 mutt_path_getcwd(cwd);
187 buf_addstr(cwd, "/dummy.rc");
188 char *ret = buf_strdup(cwd);
189 buf_pool_release(&cwd);
190 return ret;
191}
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:238
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:542
const char * mutt_path_getcwd(struct Buffer *cwd)
Get the current working directory.
Definition: path.c:469
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
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 124 of file commands.c.

126{
127 while (mutt_istr_equal(buf->data, "-group"))
128 {
129 if (!MoreArgs(s))
130 {
131 buf_strcpy(err, _("-group: no group name"));
132 return -1;
133 }
134
136
138
139 if (!MoreArgs(s))
140 {
141 buf_strcpy(err, _("out of arguments"));
142 return -1;
143 }
144
146 }
147
148 return 0;
149}
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:407
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:31
#define TOKEN_NO_FLAGS
No flags are set.
Definition: extract.h:45
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:775
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:

◆ source_stack_cleanup()

void source_stack_cleanup ( void  )

Free memory from the stack used for the source command.

Definition at line 1621 of file commands.c.

1622{
1624}
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 199 of file commands.c.

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

◆ set_dump()

enum CommandResult set_dump ( ConfigDumpFlags  flags,
struct Buffer err 
)

Dump list of config variables into a file/pager.

Parameters
flagswhat configs to dump: see ConfigDumpFlags
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 847 of file commands.c.

848{
849 struct Buffer *tempfile = buf_pool_get();
850 buf_mktemp(tempfile);
851
852 FILE *fp_out = mutt_file_fopen(buf_string(tempfile), "w");
853 if (!fp_out)
854 {
855 // L10N: '%s' is the file name of the temporary file
856 buf_printf(err, _("Could not create temporary file %s"), buf_string(tempfile));
857 buf_pool_release(&tempfile);
858 return MUTT_CMD_ERROR;
859 }
860
861 dump_config(NeoMutt->sub->cs, flags, fp_out);
862
863 mutt_file_fclose(&fp_out);
864
865 struct PagerData pdata = { 0 };
866 struct PagerView pview = { &pdata };
867
868 pdata.fname = buf_string(tempfile);
869
870 pview.banner = "set";
872 pview.mode = PAGER_MODE_OTHER;
873
874 mutt_do_pager(&pview, NULL);
875 buf_pool_release(&tempfile);
876
877 return MUTT_CMD_SUCCESS;
878}
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:39
bool dump_config(struct ConfigSet *cs, ConfigDumpFlags flags, FILE *fp)
Write all the config to a file.
Definition: dump.c:165
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:123
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:636
#define MUTT_PAGER_NO_FLAGS
No flags are set.
Definition: lib.h:59
@ PAGER_MODE_OTHER
Pager is invoked via 3rd path. Non-email content is likely to be shown.
Definition: lib.h:141
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
Data to be displayed by PagerView.
Definition: lib.h:160
const char * fname
Name of the file to read.
Definition: lib.h:164
Paged view into some data.
Definition: lib.h:171
struct PagerData * pdata
Data that pager displays. NOTNULL.
Definition: lib.h:172
enum PagerMode mode
Pager mode.
Definition: lib.h:173
PagerFlags flags
Additional settings to tweak pager's function.
Definition: lib.h:174
const char * banner
Title to display in status bar.
Definition: lib.h:175
#define buf_mktemp(buf)
Definition: tmp.h:33
+ Here is the call graph for this function:
+ Here is the caller graph for this function: