forked from AdamKorcz/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icommands.c
396 lines (336 loc) · 10.7 KB
/
icommands.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
* @file
* Information commands
*
* @authors
* Copyright (C) 2016 Christopher John Czettel <[email protected]>
* Copyright (C) 2018 Richard Russon <[email protected]>
* Copyright (C) 2019 Victor Fernandes <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
/**
* @page neo_icommands Information commands
*
* Information commands
*/
#include "config.h"
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "mutt.h"
#include "icommands.h"
#include "menu/lib.h"
#include "pager/lib.h"
#include "functions.h"
#include "init.h"
#include "keymap.h"
#include "muttlib.h"
#include "opcodes.h"
#include "version.h"
// clang-format off
static enum CommandResult icmd_bind (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err);
static enum CommandResult icmd_set (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err);
static enum CommandResult icmd_version(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err);
// clang-format on
/**
* ICommandList - All available informational commands
*
* @note These commands take precedence over conventional NeoMutt rc-lines
*/
static const struct ICommand ICommandList[] = {
// clang-format off
{ "bind", icmd_bind, 0 },
{ "macro", icmd_bind, 1 },
{ "set", icmd_set, 0 },
{ "version", icmd_version, 0 },
{ NULL, NULL, 0 },
// clang-format on
};
/**
* mutt_parse_icommand - Parse an informational command
* @param line Command to execute
* @param err Buffer for error messages
* @retval #MUTT_CMD_SUCCESS Success
* @retval #MUTT_CMD_WARNING Warning with message: command failed
* @retval #MUTT_CMD_ERROR
* - Error (no message): command not found
* - Error with message: command failed
*/
enum CommandResult mutt_parse_icommand(/* const */ char *line, struct Buffer *err)
{
if (!line || (*line == '\0') || !err)
return MUTT_CMD_ERROR;
enum CommandResult rc = MUTT_CMD_ERROR;
struct Buffer *token = mutt_buffer_pool_get();
struct Buffer expn = mutt_buffer_make(0);
mutt_buffer_addstr(&expn, line);
mutt_buffer_seek(&expn, 0);
mutt_buffer_reset(err);
SKIPWS(expn.dptr);
mutt_extract_token(token, &expn, MUTT_TOKEN_NO_FLAGS);
for (size_t i = 0; ICommandList[i].name; i++)
{
if (!mutt_str_equal(token->data, ICommandList[i].name))
continue;
rc = ICommandList[i].parse(token, &expn, ICommandList[i].data, err);
if (rc != 0)
goto finish;
break; /* Continue with next command */
}
finish:
mutt_buffer_pool_release(&token);
mutt_buffer_dealloc(&expn);
return rc;
}
/**
* dump_bind - Dump a bind map to a buffer
* @param buf Output buffer
* @param menu Map menu
* @param map Bind keymap
*/
static void dump_bind(struct Buffer *buf, struct Mapping *menu, struct Keymap *map)
{
char key_binding[32];
const char *fn_name = NULL;
km_expand_key(key_binding, sizeof(key_binding), map);
if (map->op == OP_NULL)
{
mutt_buffer_add_printf(buf, "bind %s %s noop\n", menu->name, key_binding);
return;
}
/* The pager and editor menus don't use the generic map,
* however for other menus try generic first. */
if ((menu->value != MENU_PAGER) && (menu->value != MENU_EDITOR) && (menu->value != MENU_GENERIC))
{
fn_name = mutt_get_func(OpGeneric, map->op);
}
/* if it's one of the menus above or generic doesn't find
* the function, try with its own menu. */
if (!fn_name)
{
const struct Binding *bindings = km_get_table(menu->value);
if (!bindings)
return;
fn_name = mutt_get_func(bindings, map->op);
}
mutt_buffer_add_printf(buf, "bind %s %s %s\n", menu->name, key_binding, fn_name);
}
/**
* dump_macro - Dump a macro map to a buffer
* @param buf Output buffer
* @param menu Map menu
* @param map Macro keymap
*/
static void dump_macro(struct Buffer *buf, struct Mapping *menu, struct Keymap *map)
{
char key_binding[MAX_SEQ];
km_expand_key(key_binding, MAX_SEQ, map);
struct Buffer tmp = mutt_buffer_make(0);
escape_string(&tmp, map->macro);
if (map->desc)
{
mutt_buffer_add_printf(buf, "macro %s %s \"%s\" \"%s\"\n", menu->name,
key_binding, tmp.data, map->desc);
}
else
{
mutt_buffer_add_printf(buf, "macro %s %s \"%s\"\n", menu->name, key_binding, tmp.data);
}
mutt_buffer_dealloc(&tmp);
}
/**
* dump_menu - Dumps all the binds or macros maps of a menu into a buffer
* @param buf Output buffer
* @param menu Menu to dump
* @param bind If true it's :bind, else :macro
* @retval true Menu is empty
* @retval false Menu is not empty
*/
static bool dump_menu(struct Buffer *buf, struct Mapping *menu, bool bind)
{
bool empty = true;
struct Keymap *map = NULL;
STAILQ_FOREACH(map, &Keymaps[menu->value], entries)
{
if (bind && (map->op != OP_MACRO))
{
empty = false;
dump_bind(buf, menu, map);
}
else if (!bind && (map->op == OP_MACRO))
{
empty = false;
dump_macro(buf, menu, map);
}
}
return empty;
}
/**
* dump_all_menus - Dumps all the binds or macros inside every menu
* @param buf Output buffer
* @param bind If true it's :bind, else :macro
*/
static void dump_all_menus(struct Buffer *buf, bool bind)
{
for (int i = 0; i < MENU_MAX; i++)
{
const char *menu_name = mutt_map_get_name(i, MenuNames);
struct Mapping menu = { menu_name, i };
const bool empty = dump_menu(buf, &menu, bind);
/* Add a new line for readability between menus. */
if (!empty && (i < (MENU_MAX - 1)))
mutt_buffer_addch(buf, '\n');
}
}
/**
* icmd_bind - Parse 'bind' and 'macro' commands - Implements ICommand::parse()
*/
static enum CommandResult icmd_bind(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
FILE *fp_out = NULL;
char tempfile[PATH_MAX];
bool dump_all = false, bind = (data == 0);
if (!MoreArgs(s))
dump_all = true;
else
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
if (MoreArgs(s))
{
/* More arguments potentially means the user is using the
* ::command_t :bind command thus we delegate the task. */
return MUTT_CMD_ERROR;
}
struct Buffer filebuf = mutt_buffer_make(4096);
if (dump_all || mutt_istr_equal(buf->data, "all"))
{
dump_all_menus(&filebuf, bind);
}
else
{
const int menu_index = mutt_map_get_value(buf->data, MenuNames);
if (menu_index == -1)
{
// L10N: '%s' is the (misspelled) name of the menu, e.g. 'index' or 'pager'
mutt_buffer_printf(err, _("%s: no such menu"), buf->data);
mutt_buffer_dealloc(&filebuf);
return MUTT_CMD_ERROR;
}
struct Mapping menu = { buf->data, menu_index };
dump_menu(&filebuf, &menu, bind);
}
if (mutt_buffer_is_empty(&filebuf))
{
// L10N: '%s' is the name of the menu, e.g. 'index' or 'pager',
// it might also be 'all' when all menus are affected.
mutt_buffer_printf(err, bind ? _("%s: no binds for this menu") : _("%s: no macros for this menu"),
dump_all ? "all" : buf->data);
mutt_buffer_dealloc(&filebuf);
return MUTT_CMD_ERROR;
}
mutt_mktemp(tempfile, sizeof(tempfile));
fp_out = mutt_file_fopen(tempfile, "w");
if (!fp_out)
{
// L10N: '%s' is the file name of the temporary file
mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
mutt_buffer_dealloc(&filebuf);
return MUTT_CMD_ERROR;
}
fputs(filebuf.data, fp_out);
mutt_file_fclose(&fp_out);
mutt_buffer_dealloc(&filebuf);
struct PagerData pdata = { 0 };
struct PagerView pview = { &pdata };
pdata.fname = tempfile;
pview.banner = (bind) ? "bind" : "macro";
pview.flags = MUTT_PAGER_NO_FLAGS;
pview.mode = PAGER_MODE_OTHER;
if (mutt_do_pager(&pview, NULL) == -1)
{
// L10N: '%s' is the file name of the temporary file
mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
return MUTT_CMD_ERROR;
}
return MUTT_CMD_SUCCESS;
}
/**
* icmd_set - Parse 'set' command to display config - Implements ICommand::parse()
*/
static enum CommandResult icmd_set(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
const bool set = mutt_str_equal(s->data, "set");
const bool set_all = mutt_str_equal(s->data, "set all");
if (!set && !set_all)
return MUTT_CMD_ERROR;
char tempfile[PATH_MAX];
mutt_mktemp(tempfile, sizeof(tempfile));
FILE *fp_out = mutt_file_fopen(tempfile, "w");
if (!fp_out)
{
// L10N: '%s' is the file name of the temporary file
mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
return MUTT_CMD_ERROR;
}
if (set_all)
dump_config(NeoMutt->sub->cs, CS_DUMP_NO_FLAGS, fp_out);
else
dump_config(NeoMutt->sub->cs, CS_DUMP_ONLY_CHANGED, fp_out);
mutt_file_fclose(&fp_out);
struct PagerData pdata = { 0 };
struct PagerView pview = { &pdata };
pdata.fname = tempfile;
pview.banner = "set";
pview.flags = MUTT_PAGER_NO_FLAGS;
pview.mode = PAGER_MODE_OTHER;
mutt_do_pager(&pview, NULL);
return MUTT_CMD_SUCCESS;
}
/**
* icmd_version - Parse 'version' command - Implements ICommand::parse()
*/
static enum CommandResult icmd_version(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
char tempfile[PATH_MAX];
mutt_mktemp(tempfile, sizeof(tempfile));
FILE *fp_out = mutt_file_fopen(tempfile, "w");
if (!fp_out)
{
// L10N: '%s' is the file name of the temporary file
mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
return MUTT_CMD_ERROR;
}
print_version(fp_out);
mutt_file_fclose(&fp_out);
struct PagerData pdata = { 0 };
struct PagerView pview = { &pdata };
pdata.fname = tempfile;
pview.banner = "version";
pview.flags = MUTT_PAGER_NO_FLAGS;
pview.mode = PAGER_MODE_OTHER;
if (mutt_do_pager(&pview, NULL) == -1)
{
// L10N: '%s' is the file name of the temporary file
mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
return MUTT_CMD_ERROR;
}
return MUTT_CMD_SUCCESS;
}