NeoMutt  2023-05-17-16-g61469c
Teaching an old dog new tricks
DOXYGEN
rc.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <ctype.h>
31#include <stdio.h>
32#include <string.h>
33#include <sys/types.h>
34#include "mutt/lib.h"
35#include "config/lib.h"
36#include "core/lib.h"
37#include "extract.h"
38
50enum CommandResult parse_rc_buffer(struct Buffer *line, struct Buffer *token,
51 struct Buffer *err)
52{
53 if (buf_len(line) == 0)
54 return 0;
55
57
58 buf_reset(err);
59
60 /* Read from the beginning of line->data */
61 buf_seek(line, 0);
62
63 SKIPWS(line->dptr);
64 while (*line->dptr)
65 {
66 if (*line->dptr == '#')
67 break; /* rest of line is a comment */
68 if (*line->dptr == ';')
69 {
70 line->dptr++;
71 continue;
72 }
74
75 struct Command *cmd = NULL;
76 size_t size = commands_array(&cmd);
77 size_t i;
78 for (i = 0; i < size; i++)
79 {
80 if (mutt_str_equal(token->data, cmd[i].name))
81 {
82 mutt_debug(LL_DEBUG1, "NT_COMMAND: %s\n", cmd[i].name);
83 rc = cmd[i].parse(token, line, cmd[i].data, err);
84 if ((rc == MUTT_CMD_WARNING) || (rc == MUTT_CMD_ERROR) || (rc == MUTT_CMD_FINISH))
85 goto finish; /* Propagate return code */
86
87 notify_send(NeoMutt->notify, NT_COMMAND, i, (void *) cmd);
88 break; /* Continue with next command */
89 }
90 }
91 if (i == size)
92 {
93 buf_printf(err, _("%s: unknown command"), NONULL(token->data));
94 rc = MUTT_CMD_ERROR;
95 break; /* Ignore the rest of the line */
96 }
97 }
98finish:
99 return rc;
100}
101
108enum CommandResult parse_rc_line(const char *line, struct Buffer *err)
109{
110 if (!line || (*line == '\0'))
111 return MUTT_CMD_ERROR;
112
113 struct Buffer *line_buffer = buf_pool_get();
114 struct Buffer *token = buf_pool_get();
115
116 buf_strcpy(line_buffer, line);
117
118 enum CommandResult rc = parse_rc_buffer(line_buffer, token, err);
119
120 buf_pool_release(&line_buffer);
121 buf_pool_release(&token);
122 return rc;
123}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:171
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition: buffer.c:526
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:414
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:86
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:370
CommandResult
Error codes for command_t parse functions.
Definition: command.h:36
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:39
@ 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
Convenience wrapper for the config headers.
size_t commands_array(struct Command **first)
Get Commands array.
Definition: command.c:75
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:47
Text parser.
#define TOKEN_NO_FLAGS
No flags are set.
Definition: extract.h:44
#define mutt_debug(LEVEL,...)
Definition: logging2.h:84
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:40
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:171
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
@ 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:106
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:119
enum CommandResult parse_rc_line(const char *line, struct Buffer *err)
Parse a line of user config.
Definition: rc.c:108
enum CommandResult parse_rc_buffer(struct Buffer *line, struct Buffer *token, struct Buffer *err)
Parse a line of user config.
Definition: rc.c:50
#define NONULL(x)
Definition: string2.h:37
#define SKIPWS(ch)
Definition: string2.h:45
String manipulation buffer.
Definition: buffer.h:34
char * dptr
Current read/write position.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:35
enum CommandResult(* parse)(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Definition: command.h:65
intptr_t data
Data or flags to pass to the command.
Definition: command.h:67
const char * name
Name of the command.
Definition: command.h:52
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38