NeoMutt  2025-12-11-911-gd8d604
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 "module_data.h"
46#include "muttlib.h"
47
48bool lua_init_state(lua_State **l);
49
56enum CommandResult parse_lua(const struct Command *cmd, struct Buffer *line,
57 const struct ParseContext *pc, struct ParseError *pe)
58{
60 struct Buffer *err = pe->message;
61
62 if (!MoreArgs(line))
63 {
64 buf_printf(err, _("%s: too few arguments"), cmd->name);
65 return MUTT_CMD_WARNING;
66 }
67
68 // From here on, use the remainder of `line`, raw
70
71 lua_State *lua_state = mod_data->lua_state;
72 lua_init_state(&lua_state);
73 mod_data->lua_state = lua_state;
74 mutt_debug(LL_DEBUG2, "%s\n", line->dptr);
75
76 if (luaL_dostring(lua_state, line->dptr) != LUA_OK)
77 {
78 mutt_debug(LL_DEBUG2, "%s -> failure\n", line->dptr);
79 buf_printf(err, "%s: %s", line->dptr, lua_tostring(lua_state, -1));
80 /* pop error message from the stack */
81 lua_pop(lua_state, 1);
82 goto done;
83 }
84 mutt_debug(LL_DEBUG2, "%s -> success\n", line->dptr);
85 buf_reset(line); // Clear the rest of the line
86
88
89done:
90 return rc;
91}
92
99enum CommandResult parse_lua_source(const struct Command *cmd, struct Buffer *line,
100 const struct ParseContext *pc, struct ParseError *pe)
101{
103 struct Buffer *err = pe->message;
104
105 if (!MoreArgs(line))
106 {
107 buf_printf(err, _("%s: too few arguments"), cmd->name);
108 return MUTT_CMD_WARNING;
109 }
110
111 struct Buffer *token = buf_pool_get();
113
114 mutt_debug(LL_DEBUG2, "enter\n");
115
116 lua_State *lua_state = mod_data->lua_state;
117 lua_init_state(&lua_state);
118 mod_data->lua_state = lua_state;
119
120 if (parse_extract_token(token, line, TOKEN_NONE) != 0)
121 {
122 buf_printf(err, _("source: error at %s"), line->dptr);
123 goto done;
124 }
125 if (MoreArgs(line))
126 {
127 buf_printf(err, _("%s: too many arguments"), cmd->name);
128 rc = MUTT_CMD_WARNING;
129 goto done;
130 }
131
132 expand_path(token, false);
133
134 if (luaL_dofile(lua_state, buf_string(token)) != LUA_OK)
135 {
136 mutt_error(_("Couldn't source lua source: %s"), lua_tostring(lua_state, -1));
137 lua_pop(lua_state, 1);
138 goto done;
139 }
140
141 rc = MUTT_CMD_SUCCESS;
142
143done:
144 buf_pool_release(&token);
145 return rc;
146}
147
151const struct Command LuaCommands[] = {
152 // clang-format off
153 { "lua", CMD_LUA, parse_lua,
154 N_("Run a Lua expression or call a Lua function"),
155 N_("lua <lua-command>"),
156 "optionalfeatures.html#lua" },
157 { "lua-source", CMD_LUA_SOURCE, parse_lua_source,
158 N_("Execute a Lua script file"),
159 N_("lua-source <filename>"),
160 "optionalfeatures.html#lua" },
161
162 { NULL, CMD_NONE, NULL, NULL, NULL, NULL, CF_NONE },
163 // clang-format on
164};
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
@ CF_NONE
No flags are set.
Definition command.h:49
@ CMD_LUA_SOURCE
:lua-source
Definition command.h:92
@ CMD_LUA
:lua
Definition command.h:91
@ CMD_NONE
No Command.
Definition command.h:62
CommandResult
Error codes for command_t parse functions.
Definition command.h:37
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition command.h:40
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition command.h:38
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition command.h:39
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:49
#define MoreArgs(buf)
Definition extract.h:31
@ TOKEN_NONE
No flags are set.
Definition extract.h:49
enum CommandResult parse_lua_source(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'lua-source' command - Implements Command::parse() -.
Definition commands.c:99
enum CommandResult parse_lua(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'lua' command - Implements Command::parse() -.
Definition commands.c:56
#define mutt_error(...)
Definition logging2.h:94
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:46
bool lua_init_state(lua_State **l)
Initialise a Lua State.
Definition lua.c:443
const struct Command LuaCommands[]
List of NeoMutt commands to register.
Definition commands.c:151
Integrated Lua scripting.
Lua private Module data.
@ MODULE_ID_LUA
ModuleLua, Integrated Lua scripting
Definition module_api.h:74
Convenience wrapper for the library headers.
#define N_(a)
Definition message.h:32
#define _(a)
Definition message.h:28
void expand_path(struct Buffer *buf, bool regex)
Create the canonical path.
Definition muttlib.c:122
Some miscellaneous functions.
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:663
Text parsing functions.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
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:162
Lua private Module data.
Definition module_data.h:35
lua_State * lua_state
Lua State.
Definition module_data.h:37
Container for Accounts, Notifications.
Definition neomutt.h:41
Context for config parsing (history/backtrace)
Definition pcontext.h:34
Detailed error information from config parsing.
Definition perror.h:34
struct Buffer * message
Error message.
Definition perror.h:35