NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
rc.c File Reference

Parse lines from a runtime configuration (rc) file. More...

#include "config.h"
#include <stdio.h>
#include "mutt/lib.h"
#include "core/lib.h"
#include "extract.h"
+ Include dependency graph for rc.c:

Go to the source code of this file.

Functions

enum CommandResult parse_rc_buffer (struct Buffer *line, struct Buffer *token, struct Buffer *err)
 Parse a line of user config.
 
enum CommandResult parse_rc_line (const char *line, struct Buffer *err)
 Parse a line of user config.
 

Detailed Description

Parse lines from a runtime configuration (rc) 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 rc.c.

Function Documentation

◆ parse_rc_buffer()

enum CommandResult parse_rc_buffer ( struct Buffer line,
struct Buffer token,
struct Buffer err 
)

Parse a line of user config.

Parameters
lineconfig line to read
tokenscratch buffer to be used by parser
errwhere to write error messages
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

The reason for token is to avoid having to allocate and deallocate a lot of memory if we are parsing many lines. the caller can pass in the memory to use, which avoids having to create new space for every call to this function.

Definition at line 46 of file rc.c.

48{
49 if (buf_is_empty(line))
50 return 0;
51
53
54 buf_reset(err);
55
56 /* Read from the beginning of line->data */
57 buf_seek(line, 0);
58
59 SKIPWS(line->dptr);
60 while (*line->dptr)
61 {
62 if (*line->dptr == '#')
63 break; /* rest of line is a comment */
64 if (*line->dptr == ';')
65 {
66 line->dptr++;
67 continue;
68 }
70
71 struct Command *cmd = NULL;
72 size_t size = commands_array(&cmd);
73 size_t i;
74 for (i = 0; i < size; i++)
75 {
76 if (mutt_str_equal(token->data, cmd[i].name))
77 {
78 mutt_debug(LL_DEBUG1, "NT_COMMAND: %s\n", cmd[i].name);
79 rc = cmd[i].parse(token, line, cmd[i].data, err);
80 if ((rc == MUTT_CMD_WARNING) || (rc == MUTT_CMD_ERROR) || (rc == MUTT_CMD_FINISH))
81 goto finish; /* Propagate return code */
82
83 notify_send(NeoMutt->notify, NT_COMMAND, i, (void *) cmd);
84 break; /* Continue with next command */
85 }
86 }
87 if (i == size)
88 {
89 buf_printf(err, _("%s: unknown command"), buf_string(token));
90 rc = MUTT_CMD_ERROR;
91 break; /* Ignore the rest of the line */
92 }
93 }
94finish:
95 return rc;
96}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition: buffer.c:621
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:75
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:290
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: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
size_t commands_array(struct Command **first)
Get Commands array.
Definition: command.c:75
int parse_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: extract.c:50
#define TOKEN_NO_FLAGS
No flags are set.
Definition: extract.h:46
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
#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:654
@ NT_COMMAND
A Command has been executed, Command.
Definition: notify_type.h:42
#define SKIPWS(ch)
Definition: string2.h:45
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: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:41
struct Notify * notify
Notifications handler.
Definition: neomutt.h:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_rc_line()

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

Parse a line of user config.

Parameters
lineConfig line to read
errWhere to write error messages
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

Definition at line 104 of file rc.c.

105{
106 if (!line || (*line == '\0'))
107 return MUTT_CMD_ERROR;
108
109 struct Buffer *line_buffer = buf_pool_get();
110 struct Buffer *token = buf_pool_get();
111
112 buf_strcpy(line_buffer, line);
113
114 enum CommandResult rc = parse_rc_buffer(line_buffer, token, err);
115
116 buf_pool_release(&line_buffer);
117 buf_pool_release(&token);
118 return rc;
119}
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:394
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
enum CommandResult parse_rc_buffer(struct Buffer *line, struct Buffer *token, struct Buffer *err)
Parse a line of user config.
Definition: rc.c:46
String manipulation buffer.
Definition: buffer.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function: