NeoMutt  2020-06-26-30-g76c339
Teaching an old dog new tricks
DOXYGEN
icommands.c File Reference

Information commands. More...

#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 "gui/lib.h"
#include "mutt.h"
#include "icommands.h"
#include "functions.h"
#include "init.h"
#include "keymap.h"
#include "muttlib.h"
#include "opcodes.h"
#include "pager.h"
#include "version.h"
+ Include dependency graph for icommands.c:

Go to the source code of this file.

Functions

static enum CommandResult icmd_bind (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse 'bind' and 'macro' commands - Implements ICommand::parse() More...
 
static enum CommandResult icmd_set (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse 'set' command to display config - Implements ICommand::parse() More...
 
static enum CommandResult icmd_version (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse 'version' command - Implements ICommand::parse() More...
 
enum CommandResult mutt_parse_icommand (char *line, struct Buffer *err)
 Parse an informational command. More...
 
static void dump_bind (struct Buffer *buf, struct Mapping *menu, struct Keymap *map)
 Dump a bind map to a buffer. More...
 
static void dump_macro (struct Buffer *buf, struct Mapping *menu, struct Keymap *map)
 Dump a macro map to a buffer. More...
 
static bool dump_menu (struct Buffer *buf, struct Mapping *menu, bool bind)
 Dumps all the binds or macros maps of a menu into a buffer. More...
 
static void dump_all_menus (struct Buffer *buf, bool bind)
 Dumps all the binds or macros inside every menu. More...
 

Variables

const struct ICommand ICommandList []
 All available informational commands. More...
 

Detailed Description

Information commands.

Authors
  • Christopher John Czettel
  • Richard Russon
  • Victor Fernandes

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 icommands.c.

Function Documentation

◆ icmd_bind()

static enum CommandResult icmd_bind ( struct Buffer buf,
struct Buffer s,
intptr_t  data,
struct Buffer err 
)
static

Parse 'bind' and 'macro' commands - Implements ICommand::parse()

Definition at line 232 of file icommands.c.

234 {
235  FILE *fp_out = NULL;
236  char tempfile[PATH_MAX];
237  bool dump_all = false, bind = (data == 0);
238 
239  if (!MoreArgs(s))
240  dump_all = true;
241  else
243 
244  if (MoreArgs(s))
245  {
246  /* More arguments potentially means the user is using the
247  * ::command_t :bind command thus we delegate the task. */
248  return MUTT_CMD_ERROR;
249  }
250 
251  struct Buffer filebuf = mutt_buffer_make(4096);
252  if (dump_all || mutt_istr_equal(buf->data, "all"))
253  {
254  dump_all_menus(&filebuf, bind);
255  }
256  else
257  {
258  const int menu_index = mutt_map_get_value(buf->data, Menus);
259  if (menu_index == -1)
260  {
261  // L10N: '%s' is the (misspelled) name of the menu, e.g. 'index' or 'pager'
262  mutt_buffer_printf(err, _("%s: no such menu"), buf->data);
263  mutt_buffer_dealloc(&filebuf);
264  return MUTT_CMD_ERROR;
265  }
266 
267  struct Mapping menu = { buf->data, menu_index };
268  dump_menu(&filebuf, &menu, bind);
269  }
270 
271  if (mutt_buffer_is_empty(&filebuf))
272  {
273  // L10N: '%s' is the name of the menu, e.g. 'index' or 'pager',
274  // it might also be 'all' when all menus are affected.
275  mutt_buffer_printf(err, bind ? _("%s: no binds for this menu") : _("%s: no macros for this menu"),
276  dump_all ? "all" : buf->data);
277  mutt_buffer_dealloc(&filebuf);
278  return MUTT_CMD_ERROR;
279  }
280 
281  mutt_mktemp(tempfile, sizeof(tempfile));
282  fp_out = mutt_file_fopen(tempfile, "w");
283  if (!fp_out)
284  {
285  // L10N: '%s' is the file name of the temporary file
286  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
287  mutt_buffer_dealloc(&filebuf);
288  return MUTT_CMD_ERROR;
289  }
290  fputs(filebuf.data, fp_out);
291 
292  mutt_file_fclose(&fp_out);
293  mutt_buffer_dealloc(&filebuf);
294 
295  if (mutt_do_pager((bind) ? "bind" : "macro", tempfile, MUTT_PAGER_NO_FLAGS, NULL) == -1)
296  {
297  // L10N: '%s' is the file name of the temporary file
298  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
299  return MUTT_CMD_ERROR;
300  }
301 
302  return MUTT_CMD_SUCCESS;
303 }
Error: Can&#39;t help the user.
Definition: mutt_commands.h:35
struct Buffer mutt_buffer_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:61
String manipulation buffer.
Definition: buffer.h:33
#define _(a)
Definition: message.h:28
static bool dump_menu(struct Buffer *buf, struct Mapping *menu, bool bind)
Dumps all the binds or macros maps of a menu into a buffer.
Definition: icommands.c:185
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
#define MoreArgs(buf)
Definition: buffer.h:43
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:153
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
static void dump_all_menus(struct Buffer *buf, bool bind)
Dumps all the binds or macros inside every menu.
Definition: icommands.c:214
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:891
const struct Mapping Menus[]
Menu name lookup table.
Definition: keymap.c:61
#define mutt_mktemp(buf, buflen)
Definition: muttlib.h:78
#define PATH_MAX
Definition: mutt.h:44
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:414
char * data
Pointer to data.
Definition: buffer.h:35
Success: Command worked.
Definition: mutt_commands.h:37
int mutt_do_pager(const char *banner, const char *tempfile, PagerFlags do_color, struct Pager *info)
Display some page-able text to the user.
Definition: curs_lib.c:665
Mapping between user-readable string and a constant.
Definition: mapping.h:31
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:588
bool mutt_buffer_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:252
#define MUTT_PAGER_NO_FLAGS
No flags are set.
Definition: pager.h:43
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:70
int mutt_map_get_value(const char *name, const struct Mapping *map)
Lookup the constant for a string.
Definition: mapping.c:85
+ Here is the call graph for this function:

◆ icmd_set()

static enum CommandResult icmd_set ( struct Buffer buf,
struct Buffer s,
intptr_t  data,
struct Buffer err 
)
static

Parse 'set' command to display config - Implements ICommand::parse()

Definition at line 308 of file icommands.c.

310 {
311  char tempfile[PATH_MAX];
312  mutt_mktemp(tempfile, sizeof(tempfile));
313 
314  FILE *fp_out = mutt_file_fopen(tempfile, "w");
315  if (!fp_out)
316  {
317  // L10N: '%s' is the file name of the temporary file
318  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
319  return MUTT_CMD_ERROR;
320  }
321 
322  if (mutt_str_equal(s->data, "set all"))
323  {
325  }
326  else if (mutt_str_equal(s->data, "set"))
327  {
329  }
330  else
331  {
332  mutt_file_fclose(&fp_out);
333  return MUTT_CMD_ERROR;
334  }
335 
336  mutt_file_fclose(&fp_out);
337 
338  if (mutt_do_pager("set", tempfile, MUTT_PAGER_NO_FLAGS, NULL) == -1)
339  {
340  // L10N: '%s' is the file name of the temporary file
341  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
342  return MUTT_CMD_ERROR;
343  }
344 
345  return MUTT_CMD_SUCCESS;
346 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:879
#define CS_DUMP_ONLY_CHANGED
Only show config that the user has changed.
Definition: dump.h:36
Error: Can&#39;t help the user.
Definition: mutt_commands.h:35
#define _(a)
Definition: message.h:28
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
Container for Accounts, Notifications.
Definition: neomutt.h:36
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:153
#define mutt_mktemp(buf, buflen)
Definition: muttlib.h:78
#define PATH_MAX
Definition: mutt.h:44
#define CS_DUMP_NO_FLAGS
No flags are set.
Definition: dump.h:35
char * data
Pointer to data.
Definition: buffer.h:35
bool dump_config(struct ConfigSet *cs, ConfigDumpFlags flags, FILE *fp)
Write all the config to a file.
Definition: dump.c:154
Success: Command worked.
Definition: mutt_commands.h:37
int mutt_do_pager(const char *banner, const char *tempfile, PagerFlags do_color, struct Pager *info)
Display some page-able text to the user.
Definition: curs_lib.c:665
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:588
#define MUTT_PAGER_NO_FLAGS
No flags are set.
Definition: pager.h:43
+ Here is the call graph for this function:

◆ icmd_version()

static enum CommandResult icmd_version ( struct Buffer buf,
struct Buffer s,
intptr_t  data,
struct Buffer err 
)
static

Parse 'version' command - Implements ICommand::parse()

Definition at line 351 of file icommands.c.

353 {
354  char tempfile[PATH_MAX];
355  mutt_mktemp(tempfile, sizeof(tempfile));
356 
357  FILE *fp_out = mutt_file_fopen(tempfile, "w");
358  if (!fp_out)
359  {
360  // L10N: '%s' is the file name of the temporary file
361  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
362  return MUTT_CMD_ERROR;
363  }
364 
365  print_version(fp_out);
366  mutt_file_fclose(&fp_out);
367 
368  if (mutt_do_pager("version", tempfile, MUTT_PAGER_NO_FLAGS, NULL) == -1)
369  {
370  // L10N: '%s' is the file name of the temporary file
371  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
372  return MUTT_CMD_ERROR;
373  }
374 
375  return MUTT_CMD_SUCCESS;
376 }
Error: Can&#39;t help the user.
Definition: mutt_commands.h:35
void print_version(FILE *fp)
Print system and compile info to a file.
Definition: version.c:424
#define _(a)
Definition: message.h:28
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:153
#define mutt_mktemp(buf, buflen)
Definition: muttlib.h:78
#define PATH_MAX
Definition: mutt.h:44
Success: Command worked.
Definition: mutt_commands.h:37
int mutt_do_pager(const char *banner, const char *tempfile, PagerFlags do_color, struct Pager *info)
Display some page-able text to the user.
Definition: curs_lib.c:665
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:588
#define MUTT_PAGER_NO_FLAGS
No flags are set.
Definition: pager.h:43
+ Here is the call graph for this function:

◆ mutt_parse_icommand()

enum CommandResult mutt_parse_icommand ( char *  line,
struct Buffer err 
)

Parse an informational command.

Parameters
lineCommand to execute
errBuffer for error messages
Return values
MUTT_CMD_SUCCESSSuccess
MUTT_CMD_WARNINGWarning with message: command failed
MUTT_CMD_ERROR
  • Error (no message): command not found
  • Error with message: command failed

Definition at line 78 of file icommands.c.

79 {
80  if (!line || (*line == '\0') || !err)
81  return MUTT_CMD_ERROR;
82 
83  enum CommandResult rc = MUTT_CMD_ERROR;
84 
85  struct Buffer *token = mutt_buffer_pool_get();
86  struct Buffer expn = mutt_buffer_make(0);
87  mutt_buffer_addstr(&expn, line);
88  expn.dptr = expn.data;
89 
90  mutt_buffer_reset(err);
91 
92  SKIPWS(expn.dptr);
94  for (size_t i = 0; ICommandList[i].name; i++)
95  {
96  if (!mutt_str_equal(token->data, ICommandList[i].name))
97  continue;
98 
99  rc = ICommandList[i].parse(token, &expn, ICommandList[i].data, err);
100  if (rc != 0)
101  goto finish;
102 
103  break; /* Continue with next command */
104  }
105 
106 finish:
107  mutt_buffer_pool_release(&token);
108  mutt_buffer_dealloc(&expn);
109  return rc;
110 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:879
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
CommandResult
Error codes for command_t parse functions.
Definition: mutt_commands.h:33
Error: Can&#39;t help the user.
Definition: mutt_commands.h:35
char * name
Name of the command.
Definition: icommands.h:37
const struct ICommand ICommandList[]
All available informational commands.
Definition: icommands.c:59
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer mutt_buffer_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:61
String manipulation buffer.
Definition: buffer.h:33
#define SKIPWS(ch)
Definition: string2.h:46
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
enum CommandResult(* parse)(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Function to parse information commands.
Definition: icommands.h:47
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:414
char * dptr
Current read/write position.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:35
int const char int line
Definition: acutest.h:617
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:70
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dump_bind()

static void dump_bind ( struct Buffer buf,
struct Mapping menu,
struct Keymap map 
)
static

Dump a bind map to a buffer.

Parameters
bufOutput buffer
menuMap menu
mapBind keymap

Definition at line 118 of file icommands.c.

119 {
120  char key_binding[32];
121  const char *fn_name = NULL;
122 
123  km_expand_key(key_binding, sizeof(key_binding), map);
124  if (map->op == OP_NULL)
125  {
126  mutt_buffer_add_printf(buf, "bind %s %s noop\n", menu->name, key_binding);
127  return;
128  }
129 
130  /* The pager and editor menus don't use the generic map,
131  * however for other menus try generic first. */
132  if ((menu->value != MENU_PAGER) && (menu->value != MENU_EDITOR) && (menu->value != MENU_GENERIC))
133  {
134  fn_name = mutt_get_func(OpGeneric, map->op);
135  }
136 
137  /* if it's one of the menus above or generic doesn't find
138  * the function, try with its own menu. */
139  if (!fn_name)
140  {
141  const struct Binding *bindings = km_get_table(menu->value);
142  if (!bindings)
143  return;
144 
145  fn_name = mutt_get_func(bindings, map->op);
146  }
147 
148  mutt_buffer_add_printf(buf, "bind %s %s %s\n", menu->name, key_binding, fn_name);
149 }
int value
Definition: mapping.h:34
Pager pager (email viewer)
Definition: keymap.h:79
const char * mutt_get_func(const struct Binding *bindings, int op)
Get the name of a function.
Definition: keymap.c:501
const struct Binding OpGeneric[]
Key bindings for the generic menu.
Definition: functions.c:55
short op
operation to perform
Definition: keymap.h:52
int mutt_buffer_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:203
int km_expand_key(char *s, size_t len, struct Keymap *map)
Get the key string bound to a Keymap.
Definition: keymap.c:871
const struct Binding * km_get_table(enum MenuType menu)
Lookup a menu&#39;s keybindings.
Definition: keymap.c:1265
Text entry area.
Definition: keymap.h:75
const char * name
Definition: mapping.h:33
Mapping between a user key and a function.
Definition: keymap.h:118
Generic selection list.
Definition: keymap.h:77
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dump_macro()

static void dump_macro ( struct Buffer buf,
struct Mapping menu,
struct Keymap map 
)
static

Dump a macro map to a buffer.

Parameters
bufOutput buffer
menuMap menu
mapMacro keymap

Definition at line 157 of file icommands.c.

158 {
159  char key_binding[MAX_SEQ];
160  km_expand_key(key_binding, MAX_SEQ, map);
161 
162  struct Buffer tmp = mutt_buffer_make(0);
163  escape_string(&tmp, map->macro);
164 
165  if (map->desc)
166  {
167  mutt_buffer_add_printf(buf, "macro %s %s \"%s\" \"%s\"\n", menu->name,
168  key_binding, tmp.data, map->desc);
169  }
170  else
171  {
172  mutt_buffer_add_printf(buf, "macro %s %s \"%s\"\n", menu->name, key_binding, tmp.data);
173  }
174 
175  mutt_buffer_dealloc(&tmp);
176 }
struct Buffer mutt_buffer_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:61
String manipulation buffer.
Definition: buffer.h:33
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
int mutt_buffer_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:203
int km_expand_key(char *s, size_t len, struct Keymap *map)
Get the key string bound to a Keymap.
Definition: keymap.c:871
char * macro
macro expansion (op == OP_MACRO)
Definition: keymap.h:49
char * data
Pointer to data.
Definition: buffer.h:35
char * desc
description of a macro for the help menu
Definition: keymap.h:50
#define MAX_SEQ
Definition: keymap.h:35
size_t escape_string(struct Buffer *buf, const char *src)
Write a string to a buffer, escaping special characters.
Definition: dump.c:46
const char * name
Definition: mapping.h:33
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dump_menu()

static bool dump_menu ( struct Buffer buf,
struct Mapping menu,
bool  bind 
)
static

Dumps all the binds or macros maps of a menu into a buffer.

Parameters
bufOutput buffer
menuMenu to dump
bindIf true it's :bind, else :macro
Return values
booltrue if menu is empty, false if not

Definition at line 185 of file icommands.c.

186 {
187  bool empty = true;
188  struct Keymap *map = NULL, *next = NULL;
189 
190  for (map = Keymaps[menu->value]; map; map = next)
191  {
192  next = map->next;
193 
194  if (bind && (map->op != OP_MACRO))
195  {
196  empty = false;
197  dump_bind(buf, menu, map);
198  }
199  else if (!bind && (map->op == OP_MACRO))
200  {
201  empty = false;
202  dump_macro(buf, menu, map);
203  }
204  }
205 
206  return empty;
207 }
struct Keymap * Keymaps[MENU_MAX]
Array of Keymap keybindings, one for each Menu.
Definition: keymap.c:149
static void dump_bind(struct Buffer *buf, struct Mapping *menu, struct Keymap *map)
Dump a bind map to a buffer.
Definition: icommands.c:118
int value
Definition: mapping.h:34
short op
operation to perform
Definition: keymap.h:52
struct Keymap * next
next key in map
Definition: keymap.h:51
A keyboard mapping.
Definition: keymap.h:47
static void dump_macro(struct Buffer *buf, struct Mapping *menu, struct Keymap *map)
Dump a macro map to a buffer.
Definition: icommands.c:157
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dump_all_menus()

static void dump_all_menus ( struct Buffer buf,
bool  bind 
)
static

Dumps all the binds or macros inside every menu.

Parameters
bufOutput buffer
bindIf true it's :bind, else :macro

Definition at line 214 of file icommands.c.

215 {
216  for (int i = 0; i < MENU_MAX; i++)
217  {
218  const char *menu_name = mutt_map_get_name(i, Menus);
219  struct Mapping menu = { menu_name, i };
220 
221  const bool empty = dump_menu(buf, &menu, bind);
222 
223  /* Add a new line for readability between menus. */
224  if (!empty && (i < (MENU_MAX - 1)))
225  mutt_buffer_addch(buf, '\n');
226  }
227 }
static bool dump_menu(struct Buffer *buf, struct Mapping *menu, bool bind)
Dumps all the binds or macros maps of a menu into a buffer.
Definition: icommands.c:185
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
const struct Mapping Menus[]
Menu name lookup table.
Definition: keymap.c:61
size_t mutt_buffer_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:240
Mapping between user-readable string and a constant.
Definition: mapping.h:31
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ ICommandList

const struct ICommand ICommandList[]
Initial value:
= {
{ "bind", icmd_bind, 0 },
{ "macro", icmd_bind, 1 },
{ "set", icmd_set, 0 },
{ "version", icmd_version, 0 },
{ NULL, NULL, 0 },
}
static enum CommandResult icmd_version(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse &#39;version&#39; command - Implements ICommand::parse()
Definition: icommands.c:351
static enum CommandResult icmd_set(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse &#39;set&#39; command to display config - Implements ICommand::parse()
Definition: icommands.c:308
static enum CommandResult icmd_bind(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse &#39;bind&#39; and &#39;macro&#39; commands - Implements ICommand::parse()
Definition: icommands.c:232

All available informational commands.

Note
These commands take precedence over conventional NeoMutt rc-lines

Definition at line 59 of file icommands.c.