NeoMutt  2025-01-09-117-gace867
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
rc.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stdbool.h>
31#include <stdio.h>
32#include "mutt/lib.h"
33#include "core/lib.h"
34#include "extract.h"
35
47enum CommandResult parse_rc_buffer(struct Buffer *line, struct Buffer *token,
48 struct Buffer *err)
49{
50 if (buf_is_empty(line))
51 return 0;
52
54
55 buf_reset(err);
56
57 /* Read from the beginning of line->data */
58 buf_seek(line, 0);
59
60 SKIPWS(line->dptr);
61 while (*line->dptr)
62 {
63 if (*line->dptr == '#')
64 break; /* rest of line is a comment */
65 if (*line->dptr == ';')
66 {
67 line->dptr++;
68 continue;
69 }
71
72 bool match = false;
73 const struct Command **cp = NULL;
75 {
76 const struct Command *cmd = *cp;
77
78 if (mutt_str_equal(token->data, cmd->name))
79 {
80 mutt_debug(LL_DEBUG1, "NT_COMMAND: %s\n", cmd->name);
81 rc = cmd->parse(token, line, cmd->data, err);
82 if ((rc == MUTT_CMD_WARNING) || (rc == MUTT_CMD_ERROR) || (rc == MUTT_CMD_FINISH))
83 goto finish; /* Propagate return code */
84
85 notify_send(NeoMutt->notify, NT_COMMAND, 0, (void *) cmd);
86 match = true;
87 break; /* Continue with next command */
88 }
89 }
90
91 if (!match)
92 {
93 buf_printf(err, _("%s: unknown command"), buf_string(token));
94 rc = MUTT_CMD_ERROR;
95 break; /* Ignore the rest of the line */
96 }
97 }
98
99finish:
100 return rc;
101}
102
109enum CommandResult parse_rc_line(const char *line, struct Buffer *err)
110{
111 if (!line || (*line == '\0'))
112 return MUTT_CMD_ERROR;
113
114 struct Buffer *line_buffer = buf_pool_get();
115 struct Buffer *token = buf_pool_get();
116
117 buf_strcpy(line_buffer, line);
118
119 enum CommandResult rc = parse_rc_buffer(line_buffer, token, err);
120
121 buf_pool_release(&line_buffer);
122 buf_pool_release(&token);
123 return rc;
124}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:214
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition: buffer.c:622
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:291
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
CommandResult
Error codes for command_t parse functions.
Definition: command.h:35
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:38
@ 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
Convenience wrapper for the core headers.
int parse_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: extract.c:49
Text parser.
#define TOKEN_NO_FLAGS
No flags are set.
Definition: extract.h:46
#define mutt_debug(LEVEL,...)
Definition: logging2.h:90
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:44
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:173
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:661
@ NT_COMMAND
A Command has been executed, Command.
Definition: notify_type.h:42
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
enum CommandResult parse_rc_line(const char *line, struct Buffer *err)
Parse a line of user config.
Definition: rc.c:109
enum CommandResult parse_rc_buffer(struct Buffer *line, struct Buffer *token, struct Buffer *err)
Parse a line of user config.
Definition: rc.c:47
#define SKIPWS(ch)
Definition: string2.h:45
String manipulation buffer.
Definition: buffer.h:36
char * dptr
Current read/write position.
Definition: buffer.h:38
char * data
Pointer to data.
Definition: buffer.h:37
enum CommandResult(* parse)(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Definition: command.h:64
intptr_t data
Data or flags to pass to the command.
Definition: command.h:66
const char * name
Name of the command.
Definition: command.h:51
Container for Accounts, Notifications.
Definition: neomutt.h:43
struct CommandArray commands
NeoMutt commands.
Definition: neomutt.h:51
struct Notify * notify
Notifications handler.
Definition: neomutt.h:44