NeoMutt  2025-12-11-58-g09398d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
commands.c
Go to the documentation of this file.
1
22
28
29#ifndef LUA_COMPAT_ALL
30#define LUA_COMPAT_ALL
31#endif
32#ifndef LUA_COMPAT_5_1
33#define LUA_COMPAT_5_1
34#endif
35
36#include "config.h"
37#include <lauxlib.h>
38#include <lua.h>
39#include <stdbool.h>
40#include <stdio.h>
41#include "mutt/lib.h"
42#include "core/lib.h"
43#include "lib.h"
44#include "parse/lib.h"
45#include "muttlib.h"
46
47extern lua_State *LuaState;
48
49bool lua_init_state(lua_State **l);
50
57enum CommandResult parse_lua(const struct Command *cmd, struct Buffer *line, struct Buffer *err)
58{
59 if (!MoreArgs(line))
60 {
61 buf_printf(err, _("%s: too few arguments"), cmd->name);
62 return MUTT_CMD_WARNING;
63 }
64
65 struct Buffer *token = buf_pool_get();
67
69
71 mutt_debug(LL_DEBUG2, "%s\n", buf_string(token));
72
73 if (luaL_dostring(LuaState, buf_string(token)) != LUA_OK)
74 {
75 mutt_debug(LL_DEBUG2, "%s -> failure\n", buf_string(token));
76 buf_printf(err, "%s: %s", buf_string(token), lua_tostring(LuaState, -1));
77 /* pop error message from the stack */
78 lua_pop(LuaState, 1);
79 goto done;
80 }
81 mutt_debug(LL_DEBUG2, "%s -> success\n", buf_string(token));
82 buf_reset(line); // Clear the rest of the line
83
85
86done:
87 buf_pool_release(&token);
88 return rc;
89}
90
97enum CommandResult parse_lua_source(const struct Command *cmd,
98 struct Buffer *line, struct Buffer *err)
99{
100 if (!MoreArgs(line))
101 {
102 buf_printf(err, _("%s: too few arguments"), cmd->name);
103 return MUTT_CMD_WARNING;
104 }
105
106 struct Buffer *token = buf_pool_get();
108
109 mutt_debug(LL_DEBUG2, "enter\n");
110
112
113 if (parse_extract_token(token, line, TOKEN_NO_FLAGS) != 0)
114 {
115 buf_printf(err, _("source: error at %s"), line->dptr);
116 goto done;
117 }
118 if (MoreArgs(line))
119 {
120 buf_printf(err, _("%s: too many arguments"), cmd->name);
121 rc = MUTT_CMD_WARNING;
122 goto done;
123 }
124
125 buf_expand_path(token);
126
127 if (luaL_dofile(LuaState, buf_string(token)) != LUA_OK)
128 {
129 mutt_error(_("Couldn't source lua source: %s"), lua_tostring(LuaState, -1));
130 lua_pop(LuaState, 1);
131 goto done;
132 }
133
134 rc = MUTT_CMD_SUCCESS;
135
136done:
137 buf_pool_release(&token);
138 return rc;
139}
140
144static const struct Command LuaCommands[] = {
145 // clang-format off
146 { "lua", parse_lua, 0,
147 N_("Run a Lua expression or call a Lua function"),
148 N_("lua '<lua-commands>'"),
149 "optionalfeatures.html#lua-commands" },
150 { "lua-source", parse_lua_source, 0,
151 N_("Execute a Lua script file"),
152 N_("lua-source <file>"),
153 "optionalfeatures.html#lua-commands" },
154
155 { NULL, NULL, 0, NULL, NULL, NULL, CF_NO_FLAGS },
156 // clang-format on
157};
158
166
170void lua_cleanup(void)
171{
172 if (LuaState)
173 {
174 lua_close(LuaState);
175 LuaState = NULL;
176 }
177}
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
#define CF_NO_FLAGS
No flags are set.
Definition command.h:46
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
bool commands_register(struct CommandArray *ca, const struct Command *cmds)
Add commands to Commands array.
Definition command.c:51
Convenience wrapper for the core headers.
int parse_extract_token(struct Buffer *dest, struct Buffer *line, 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
enum CommandResult parse_lua_source(const struct Command *cmd, struct Buffer *line, struct Buffer *err)
Parse the 'lua-source' command - Implements Command::parse() -.
Definition commands.c:97
enum CommandResult parse_lua(const struct Command *cmd, struct Buffer *line, struct Buffer *err)
Parse the 'lua' command - Implements Command::parse() -.
Definition commands.c:57
#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
bool lua_init_state(lua_State **l)
Initialise a Lua State.
Definition lua.c:436
void lua_init(void)
Setup feature commands.
Definition commands.c:162
lua_State * LuaState
Global Lua State.
Definition lua.c:56
void lua_cleanup(void)
Clean up Lua.
Definition commands.c:170
static const struct Command LuaCommands[]
List of NeoMutt commands to register.
Definition commands.c:144
Integrated Lua scripting.
Convenience wrapper for the library headers.
#define N_(a)
Definition message.h:32
#define _(a)
Definition message.h:28
void buf_expand_path(struct Buffer *buf)
Create the canonical path.
Definition muttlib.c:314
Some miscellaneous functions.
Text parsing functions.
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
char * dptr
Current read/write position.
Definition buffer.h:38
const char * name
Name of the command.
Definition command.h:59
Container for Accounts, Notifications.
Definition neomutt.h:43
struct CommandArray commands
NeoMutt commands.
Definition neomutt.h:51