NeoMutt  2019-11-11
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 "curs_lib.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 229 of file icommands.c.

231 {
232  FILE *fp_out = NULL;
233  char tempfile[PATH_MAX];
234  bool dump_all = false, bind = (data == 0);
235 
236  if (!MoreArgs(s))
237  dump_all = true;
238  else
240 
241  if (MoreArgs(s))
242  {
243  /* More arguments potentially means the user is using the
244  * ::command_t :bind command thus we delegate the task. */
245  return MUTT_CMD_ERROR;
246  }
247 
248  struct Buffer filebuf = mutt_buffer_make(4096);
249  if (dump_all || (mutt_str_strcasecmp(buf->data, "all") == 0))
250  {
251  dump_all_menus(&filebuf, bind);
252  }
253  else
254  {
255  const int menu_index = mutt_map_get_value(buf->data, Menus);
256  if (menu_index == -1)
257  {
258  // L10N: '%s' is the (misspelled) name of the menu, e.g. 'index' or 'pager'
259  mutt_buffer_printf(err, _("%s: no such menu"), buf->data);
260  mutt_buffer_dealloc(&filebuf);
261  return MUTT_CMD_ERROR;
262  }
263 
264  struct Mapping menu = { buf->data, menu_index };
265  dump_menu(&filebuf, &menu, bind);
266  }
267 
268  if (mutt_buffer_is_empty(&filebuf))
269  {
270  // L10N: '%s' is the name of the menu, e.g. 'index' or 'pager', it might
271  // L10N: also be 'all' when all menus are affected.
272  mutt_buffer_printf(err, bind ? _("%s: no binds for this menu") : _("%s: no macros for this menu"),
273  dump_all ? "all" : buf->data);
274  mutt_buffer_dealloc(&filebuf);
275  return MUTT_CMD_ERROR;
276  }
277 
278  mutt_mktemp(tempfile, sizeof(tempfile));
279  fp_out = mutt_file_fopen(tempfile, "w");
280  if (!fp_out)
281  {
282  // L10N: '%s' is the file name of the temporary file
283  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
284  mutt_buffer_dealloc(&filebuf);
285  return MUTT_CMD_ERROR;
286  }
287  fputs(filebuf.data, fp_out);
288 
289  mutt_file_fclose(&fp_out);
290  mutt_buffer_dealloc(&filebuf);
291 
292  struct Pager info = { 0 };
293  if (mutt_do_pager((bind) ? "bind" : "macro", tempfile, MUTT_PAGER_NO_FLAGS, &info) == -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:182
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
An email being displayed.
Definition: pager.h:65
#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:211
const struct Mapping Menus[]
Menu name lookup table.
Definition: keymap.c:62
#define mutt_mktemp(buf, buflen)
Definition: muttlib.h:76
#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:2655
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:631
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  struct Pager info = { 0 };
337  if (mutt_do_pager("set", tempfile, MUTT_PAGER_NO_FLAGS, &info) == -1)
338  {
339  // L10N: '%s' is the file name of the temporary file
340  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
341  return MUTT_CMD_ERROR;
342  }
343 
344  return MUTT_CMD_SUCCESS;
345 }
#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:41
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
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:152
#define mutt_mktemp(buf, buflen)
Definition: muttlib.h:76
#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:631
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 350 of file icommands.c.

352 {
353  char tempfile[PATH_MAX];
354  mutt_mktemp(tempfile, sizeof(tempfile));
355 
356  FILE *fp_out = mutt_file_fopen(tempfile, "w");
357  if (!fp_out)
358  {
359  // L10N: '%s' is the file name of the temporary file
360  mutt_buffer_printf(err, _("Could not create temporary file %s"), tempfile);
361  return MUTT_CMD_ERROR;
362  }
363 
364  print_version(fp_out);
365  mutt_file_fclose(&fp_out);
366 
367  struct Pager info = { 0 };
368  if (mutt_do_pager("version", tempfile, MUTT_PAGER_NO_FLAGS, &info) == -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: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:160
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:152
#define mutt_mktemp(buf, buflen)
Definition: muttlib.h:76
#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:631
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 75 of file icommands.c.

76 {
77  if (!line || !*line || !err)
78  return MUTT_CMD_ERROR;
79 
80  enum CommandResult rc = MUTT_CMD_ERROR;
81 
82  struct Buffer *token = mutt_buffer_pool_get();
83  struct Buffer expn = mutt_buffer_make(0);
84  mutt_buffer_addstr(&expn, line);
85  expn.dptr = expn.data;
86 
87  mutt_buffer_reset(err);
88 
89  SKIPWS(expn.dptr);
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 finish:
104  mutt_buffer_pool_release(&token);
105  mutt_buffer_dealloc(&expn);
106  return rc;
107 }
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:100
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:57
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:111
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:2655
char * dptr
Current read/write position.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:35
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: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 115 of file icommands.c.

116 {
117  char key_binding[32];
118  const char *fn_name = NULL;
119 
120  km_expand_key(key_binding, sizeof(key_binding), map);
121  if (map->op == OP_NULL)
122  {
123  mutt_buffer_add_printf(buf, "bind %s %s noop\n", menu->name, key_binding);
124  return;
125  }
126 
127  /* The pager and editor menus don't use the generic map,
128  * however for other menus try generic first. */
129  if ((menu->value != MENU_PAGER) && (menu->value != MENU_EDITOR) && (menu->value != MENU_GENERIC))
130  {
131  fn_name = mutt_get_func(OpGeneric, map->op);
132  }
133 
134  /* if it's one of the menus above or generic doesn't find
135  * the function, try with its own menu. */
136  if (!fn_name)
137  {
138  const struct Binding *bindings = km_get_table(menu->value);
139  if (!bindings)
140  return;
141 
142  fn_name = mutt_get_func(bindings, map->op);
143  }
144 
145  mutt_buffer_add_printf(buf, "bind %s %s %s\n", menu->name, key_binding, fn_name);
146 }
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:501
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:828
const struct Binding * km_get_table(enum MenuType menu)
Lookup a menu&#39;s keybindings.
Definition: keymap.c:1223
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 154 of file icommands.c.

155 {
156  char key_binding[MAX_SEQ];
157  km_expand_key(key_binding, MAX_SEQ, map);
158 
159  struct Buffer tmp = mutt_buffer_make(0);
160  escape_string(&tmp, map->macro);
161 
162  if (map->desc)
163  {
164  mutt_buffer_add_printf(buf, "macro %s %s \"%s\" \"%s\"\n", menu->name,
165  key_binding, tmp.data, map->desc);
166  }
167  else
168  {
169  mutt_buffer_add_printf(buf, "macro %s %s \"%s\"\n", menu->name, key_binding, tmp.data);
170  }
171 
172  mutt_buffer_dealloc(&tmp);
173 }
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:828
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 182 of file icommands.c.

183 {
184  bool empty = true;
185  struct Keymap *map = NULL, *next = NULL;
186 
187  for (map = Keymaps[menu->value]; map; map = next)
188  {
189  next = map->next;
190 
191  if (bind && (map->op != OP_MACRO))
192  {
193  empty = false;
194  dump_bind(buf, menu, map);
195  }
196  else if (!bind && (map->op == OP_MACRO))
197  {
198  empty = false;
199  dump_macro(buf, menu, map);
200  }
201  }
202 
203  return empty;
204 }
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:115
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:154
+ 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 211 of file icommands.c.

212 {
213  for (int i = 0; i < MENU_MAX; i++)
214  {
215  const char *menu_name = mutt_map_get_name(i, Menus);
216  struct Mapping menu = { menu_name, i };
217 
218  const bool empty = dump_menu(buf, &menu, bind);
219 
220  /* Add a new line for readability between menus. */
221  if (!empty && (i < (MENU_MAX - 1)))
222  mutt_buffer_addch(buf, '\n');
223  }
224 }
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:182
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:62
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:229
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:350
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 57 of file icommands.c.