NeoMutt  2018-07-16 +2388-bcedc8
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 "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

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_alloc(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_free(&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_free(&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_free(&filebuf);
286  return MUTT_CMD_ERROR;
287  }
288  fputs(filebuf->data, fp_out);
289 
290  mutt_file_fclose(&fp_out);
291  mutt_buffer_free(&filebuf);
292 
293  struct Pager info = { 0 };
294  if (mutt_pager((bind) ? "bind" : "macro", tempfile, MUTT_PAGER_NO_FLAGS, &info) == -1)
295  {
296  // L10N: '%s' is the file name of the temporary file
297  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
298  return MUTT_CMD_ERROR;
299  }
300 
301  return MUTT_CMD_SUCCESS;
302 }
Error: Can&#39;t help the user.
Definition: mutt_commands.h:35
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:191
An email being displayed.
Definition: pager.h:65
struct Buffer * mutt_buffer_alloc(size_t size)
Create a new Buffer.
Definition: buffer.c:296
#define MoreArgs(buf)
Definition: buffer.h:43
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:149
void mutt_buffer_free(struct Buffer **p)
Release a Buffer and its contents.
Definition: buffer.c:134
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:76
#define PATH_MAX
Definition: mutt.h:51
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:2638
char * data
Pointer to data.
Definition: buffer.h:35
Success: Command worked.
Definition: mutt_commands.h:37
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
int mutt_pager(const char *banner, const char *fname, PagerFlags flags, struct Pager *extra)
Display a file, or help, in a window.
Definition: pager.c:2242
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:584
bool mutt_buffer_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:283
#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:77
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:

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 307 of file icommands.c.

309 {
310  char tempfile[PATH_MAX];
311  mutt_mktemp(tempfile, sizeof(tempfile));
312 
313  FILE *fp_out = mutt_file_fopen(tempfile, "w");
314  if (!fp_out)
315  {
316  // L10N: '%s' is the file name of the temporary file
317  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
318  return MUTT_CMD_ERROR;
319  }
320 
321  if (mutt_str_strcmp(s->data, "set all") == 0)
322  {
324  }
325  else if (mutt_str_strcmp(s->data, "set") == 0)
326  {
328  }
329  else
330  {
331  mutt_file_fclose(&fp_out);
332  return MUTT_CMD_ERROR;
333  }
334 
335  mutt_file_fclose(&fp_out);
336 
337  struct Pager info = { 0 };
338  if (mutt_pager("set", tempfile, MUTT_PAGER_NO_FLAGS, &info) == -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 }
#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
WHERE struct ConfigSet * Config
Wrapper around the user&#39;s config settings.
Definition: globals.h:39
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:191
An email being displayed.
Definition: pager.h:65
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:149
#define mutt_mktemp(buf, buflen)
Definition: muttlib.h:76
#define PATH_MAX
Definition: mutt.h:51
#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_pager(const char *banner, const char *fname, PagerFlags flags, struct Pager *extra)
Display a file, or help, in a window.
Definition: pager.c:2242
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:584
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:

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 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  struct Pager info = { 0 };
369  if (mutt_pager("version", tempfile, MUTT_PAGER_NO_FLAGS, &info) == -1)
370  {
371  // L10N: '%s' is the file name of the temporary file
372  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
373  return MUTT_CMD_ERROR;
374  }
375 
376  return MUTT_CMD_SUCCESS;
377 }
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:381
#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:191
An email being displayed.
Definition: pager.h:65
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:149
#define mutt_mktemp(buf, buflen)
Definition: muttlib.h:76
#define PATH_MAX
Definition: mutt.h:51
Success: Command worked.
Definition: mutt_commands.h:37
int mutt_pager(const char *banner, const char *fname, PagerFlags flags, struct Pager *extra)
Display a file, or help, in a window.
Definition: pager.c:2242
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:584
#define MUTT_PAGER_NO_FLAGS
No flags are set.
Definition: pager.h:43

+ Here is the call graph for this function:

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 74 of file icommands.c.

75 {
76  if (!line || !*line || !err)
77  return MUTT_CMD_ERROR;
78 
79  enum CommandResult rc = MUTT_CMD_ERROR;
80 
81  struct Buffer *token = mutt_buffer_pool_get();
82  struct Buffer *expn = mutt_buffer_from(line);
83  expn->dptr = expn->data;
84 
85  mutt_buffer_reset(err);
86 
87  SKIPWS(expn->dptr);
88  while (*expn->dptr != '\0')
89  {
91  for (size_t i = 0; ICommandList[i].name; i++)
92  {
93  if (mutt_str_strcmp(token->data, ICommandList[i].name) != 0)
94  continue;
95 
96  rc = ICommandList[i].func(token, expn, ICommandList[i].data, err);
97  if (rc != 0)
98  goto finish;
99 
100  break; /* Continue with next command */
101  }
102  }
103 
104 finish:
105  mutt_buffer_pool_release(&token);
106  mutt_buffer_free(&expn);
107  return rc;
108 }
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:75
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:56
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:86
String manipulation buffer.
Definition: buffer.h:33
#define SKIPWS(ch)
Definition: string2.h:47
void mutt_buffer_free(struct Buffer **p)
Release a Buffer and its contents.
Definition: buffer.c:134
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:2638
char * dptr
Current read/write position.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:35
struct Buffer * mutt_buffer_from(const char *seed)
Create Buffer from an existing string.
Definition: buffer.c:92
icommand_t * func
Function to parse the command.
Definition: icommands.h:47
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:77

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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:452
short op
operation to perform
Definition: keymap.h:54
int mutt_buffer_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:234
const struct Binding * km_get_table(int menu)
Lookup a menu&#39;s keybindings.
Definition: keymap.c:1218
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:779
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:105
Generic selection list.
Definition: keymap.h:76

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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_new();
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_free(&tmp);
174 }
String manipulation buffer.
Definition: buffer.h:33
void mutt_buffer_free(struct Buffer **p)
Release a Buffer and its contents.
Definition: buffer.c:134
int mutt_buffer_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:234
int km_expand_key(char *s, size_t len, struct Keymap *map)
Get the key string bound to a Keymap.
Definition: keymap.c:779
char * macro
macro expansion (op == OP_MACRO)
Definition: keymap.h:51
char * data
Pointer to data.
Definition: buffer.h:35
char * desc
description of a macro for the help menu
Definition: keymap.h:52
struct Buffer * mutt_buffer_new(void)
Create and initialise a Buffer.
Definition: buffer.c:45
#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:

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:54
struct Keymap * next
next key in map
Definition: keymap.h:53
A keyboard mapping.
Definition: keymap.h:49
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:

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:271
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

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:351
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:307

All available informational commands.

Note
These commands take precedence over conventional NeoMutt rc-lines

Definition at line 56 of file icommands.c.