NeoMutt  2019-12-07-60-g0cfa53
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/mutt.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "icommands.h"
#include "globals.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, unsigned long data, struct Buffer *err)
 Parse 'bind' and 'macro' commands - Implements icommand_t. More...
 
static enum CommandResult icmd_set (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err)
 Parse 'set' command to display config - Implements icommand_t. More...
 
static enum CommandResult icmd_version (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err)
 Parse 'version' command - Implements icommand_t. 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,
unsigned long  data,
struct Buffer err 
)
static

Parse 'bind' and 'macro' commands - Implements icommand_t.

Definition at line 230 of file icommands.c.

232 {
233  FILE *fp_out = NULL;
234  char tempfile[PATH_MAX];
235  bool dump_all = false, bind = (data == 0);
236 
237  if (!MoreArgs(s))
238  dump_all = true;
239  else
241 
242  if (MoreArgs(s))
243  {
244  /* More arguments potentially means the user is using the
245  * ::command_t :bind command thus we delegate the task. */
246  return MUTT_CMD_ERROR;
247  }
248 
249  struct Buffer filebuf = mutt_buffer_make(4096);
250  if (dump_all || (mutt_str_strcasecmp(buf->data, "all") == 0))
251  {
252  dump_all_menus(&filebuf, bind);
253  }
254  else
255  {
256  const int menu_index = mutt_map_get_value(buf->data, Menus);
257  if (menu_index == -1)
258  {
259  // L10N: '%s' is the (misspelled) name of the menu, e.g. 'index' or 'pager'
260  mutt_buffer_printf(err, _("%s: no such menu"), buf->data);
261  mutt_buffer_dealloc(&filebuf);
262  return MUTT_CMD_ERROR;
263  }
264 
265  struct Mapping menu = { buf->data, menu_index };
266  dump_menu(&filebuf, &menu, bind);
267  }
268 
269  if (mutt_buffer_is_empty(&filebuf))
270  {
271  // L10N: '%s' is the name of the menu, e.g. 'index' or 'pager', it might
272  // L10N: also be 'all' when all menus are affected.
273  mutt_buffer_printf(err, bind ? _("%s: no binds for this menu") : _("%s: no macros for this menu"),
274  dump_all ? "all" : buf->data);
275  mutt_buffer_dealloc(&filebuf);
276  return MUTT_CMD_ERROR;
277  }
278 
279  mutt_mktemp(tempfile, sizeof(tempfile));
280  fp_out = mutt_file_fopen(tempfile, "w");
281  if (!fp_out)
282  {
283  // L10N: '%s' is the file name of the temporary file
284  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
285  mutt_buffer_dealloc(&filebuf);
286  return MUTT_CMD_ERROR;
287  }
288  fputs(filebuf.data, fp_out);
289 
290  mutt_file_fclose(&fp_out);
291  mutt_buffer_dealloc(&filebuf);
292 
293  if (mutt_do_pager((bind) ? "bind" : "macro", tempfile, MUTT_PAGER_NO_FLAGS, NULL) == -1)
294  {
295  // L10N: '%s' is the file name of the temporary file
296  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
297  return MUTT_CMD_ERROR;
298  }
299 
300  return MUTT_CMD_SUCCESS;
301 }
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:183
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:152
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:212
const struct Mapping Menus[]
Menu name lookup table.
Definition: keymap.c:60
#define mutt_mktemp(buf, buflen)
Definition: muttlib.h:75
#define PATH_MAX
Definition: mutt.h:50
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:2674
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:664
int mutt_str_strcasecmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:628
Mapping between user-readable string and a constant.
Definition: mapping.h:29
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:585
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:76
int mutt_map_get_value(const char *name, const struct Mapping *map)
Lookup the constant for a string.
Definition: mapping.c:61
+ Here is the call graph for this function:

◆ icmd_set()

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

Parse 'set' command to display config - Implements icommand_t.

Definition at line 306 of file icommands.c.

308 {
309  char tempfile[PATH_MAX];
310  mutt_mktemp(tempfile, sizeof(tempfile));
311 
312  FILE *fp_out = mutt_file_fopen(tempfile, "w");
313  if (!fp_out)
314  {
315  // L10N: '%s' is the file name of the temporary file
316  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
317  return MUTT_CMD_ERROR;
318  }
319 
320  if (mutt_str_strcmp(s->data, "set all") == 0)
321  {
323  }
324  else if (mutt_str_strcmp(s->data, "set") == 0)
325  {
327  }
328  else
329  {
330  mutt_file_fclose(&fp_out);
331  return MUTT_CMD_ERROR;
332  }
333 
334  mutt_file_fclose(&fp_out);
335 
336  if (mutt_do_pager("set", tempfile, MUTT_PAGER_NO_FLAGS, NULL) == -1)
337  {
338  // L10N: '%s' is the file name of the temporary file
339  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
340  return MUTT_CMD_ERROR;
341  }
342 
343  return MUTT_CMD_SUCCESS;
344 }
#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:35
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:49
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
#define mutt_mktemp(buf, buflen)
Definition: muttlib.h:75
#define PATH_MAX
Definition: mutt.h:50
#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:198
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:664
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:38
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:585
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:615
#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,
unsigned long  data,
struct Buffer err 
)
static

Parse 'version' command - Implements icommand_t.

Definition at line 349 of file icommands.c.

351 {
352  char tempfile[PATH_MAX];
353  mutt_mktemp(tempfile, sizeof(tempfile));
354 
355  FILE *fp_out = mutt_file_fopen(tempfile, "w");
356  if (!fp_out)
357  {
358  // L10N: '%s' is the file name of the temporary file
359  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
360  return MUTT_CMD_ERROR;
361  }
362 
363  print_version(fp_out);
364  mutt_file_fclose(&fp_out);
365 
366  if (mutt_do_pager("version", tempfile, MUTT_PAGER_NO_FLAGS, NULL) == -1)
367  {
368  // L10N: '%s' is the file name of the temporary file
369  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
370  return MUTT_CMD_ERROR;
371  }
372 
373  return MUTT_CMD_SUCCESS;
374 }
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:388
#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:152
#define mutt_mktemp(buf, buflen)
Definition: muttlib.h:75
#define PATH_MAX
Definition: mutt.h:50
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:664
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:585
#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_ERRORError (no message): command not found
MUTT_CMD_ERRORError with message: command failed
MUTT_CMD_WARNINGWarning with message: command failed

Definition at line 76 of file icommands.c.

77 {
78  if (!line || !*line || !err)
79  return MUTT_CMD_ERROR;
80 
81  enum CommandResult rc = MUTT_CMD_ERROR;
82 
83  struct Buffer *token = mutt_buffer_pool_get();
84  struct Buffer expn = mutt_buffer_make(0);
85  mutt_buffer_addstr(&expn, line);
86  expn.dptr = expn.data;
87 
88  mutt_buffer_reset(err);
89 
90  SKIPWS(expn.dptr);
92  for (size_t i = 0; ICommandList[i].name; i++)
93  {
94  if (mutt_str_strcmp(token->data, ICommandList[i].name) != 0)
95  continue;
96 
97  rc = ICommandList[i].func(token, &expn, ICommandList[i].data, err);
98  if (rc != 0)
99  goto finish;
100 
101  break; /* Continue with next command */
102  }
103 
104 finish:
105  mutt_buffer_pool_release(&token);
106  mutt_buffer_dealloc(&expn);
107  return rc;
108 }
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:46
const struct ICommand ICommandList[]
All available informational commands.
Definition: icommands.c:58
icommand_t func
Function to parse the command.
Definition: icommands.h:47
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:47
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
const char * line
Definition: common.c:36
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:2674
char * dptr
Current read/write position.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:35
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:615
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:76
+ 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 116 of file icommands.c.

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

156 {
157  char key_binding[MAX_SEQ];
158  km_expand_key(key_binding, MAX_SEQ, map);
159 
160  struct Buffer tmp = mutt_buffer_make(0);
161  escape_string(&tmp, map->macro);
162 
163  if (map->desc)
164  {
165  mutt_buffer_add_printf(buf, "macro %s %s \"%s\" \"%s\"\n", menu->name,
166  key_binding, tmp.data, map->desc);
167  }
168  else
169  {
170  mutt_buffer_add_printf(buf, "macro %s %s \"%s\"\n", menu->name, key_binding, tmp.data);
171  }
172 
173  mutt_buffer_dealloc(&tmp);
174 }
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:826
char * macro
macro expansion (op == OP_MACRO)
Definition: keymap.h:48
char * data
Pointer to data.
Definition: buffer.h:35
char * desc
description of a macro for the help menu
Definition: keymap.h:49
#define MAX_SEQ
Definition: keymap.h:34
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:31
+ 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 183 of file icommands.c.

184 {
185  bool empty = true;
186  struct Keymap *map = NULL, *next = NULL;
187 
188  for (map = Keymaps[menu->value]; map; map = next)
189  {
190  next = map->next;
191 
192  if (bind && (map->op != OP_MACRO))
193  {
194  empty = false;
195  dump_bind(buf, menu, map);
196  }
197  else if (!bind && (map->op == OP_MACRO))
198  {
199  empty = false;
200  dump_macro(buf, menu, map);
201  }
202  }
203 
204  return empty;
205 }
struct Keymap * Keymaps[MENU_MAX]
Array of Keymap keybindings, one for each Menu.
Definition: keymap.c:147
static void dump_bind(struct Buffer *buf, struct Mapping *menu, struct Keymap *map)
Dump a bind map to a buffer.
Definition: icommands.c:116
int value
Definition: mapping.h:32
short op
operation to perform
Definition: keymap.h:51
struct Keymap * next
next key in map
Definition: keymap.h:50
A keyboard mapping.
Definition: keymap.h:46
static void dump_macro(struct Buffer *buf, struct Mapping *menu, struct Keymap *map)
Dump a macro map to a buffer.
Definition: icommands.c:155
+ 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 212 of file icommands.c.

213 {
214  for (int i = 0; i < MENU_MAX; i++)
215  {
216  const char *menu_name = mutt_map_get_name(i, Menus);
217  struct Mapping menu = { menu_name, i };
218 
219  const bool empty = dump_menu(buf, &menu, bind);
220 
221  /* Add a new line for readability between menus. */
222  if (!empty && (i < (MENU_MAX - 1)))
223  mutt_buffer_addch(buf, '\n');
224  }
225 }
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:183
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:60
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:29
+ 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_bind(struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err)
Parse &#39;bind&#39; and &#39;macro&#39; commands - Implements icommand_t.
Definition: icommands.c:230
static enum CommandResult icmd_version(struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err)
Parse &#39;version&#39; command - Implements icommand_t.
Definition: icommands.c:349
static enum CommandResult icmd_set(struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err)
Parse &#39;set&#39; command to display config - Implements icommand_t.
Definition: icommands.c:306

All available informational commands.

Note
These commands take precedence over conventional NeoMutt rc-lines

Definition at line 58 of file icommands.c.