NeoMutt  2025-01-09-117-gace867
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
command.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stddef.h>
31#include "mutt/lib.h"
32#include "command.h"
33
37static int commands_sort(const void *a, const void *b, void *sdata)
38{
39 const struct Command *x = *(const struct Command **) a;
40 const struct Command *y = *(const struct Command **) b;
41
42 return mutt_str_cmp(x->name, y->name);
43}
44
51bool commands_register(struct CommandArray *ca, const struct Command *cmds)
52{
53 if (!ca || !cmds)
54 return false;
55
56 for (int i = 0; cmds[i].name; i++)
57 {
58 ARRAY_ADD(ca, &cmds[i]);
59 }
60 ARRAY_SORT(ca, commands_sort, NULL);
61
62 return true;
63}
64
70void commands_clear(struct CommandArray *ca)
71{
72 ARRAY_FREE(ca);
73}
74
82const struct Command *commands_get(struct CommandArray *ca, const char *name)
83{
84 const struct Command **cp = NULL;
85 ARRAY_FOREACH(cp, ca)
86 {
87 const struct Command *cmd = *cp;
88
89 if (mutt_str_equal(name, cmd->name))
90 return cmd;
91 }
92 return NULL;
93}
#define ARRAY_SORT(head, fn, sdata)
Sort an array.
Definition: array.h:335
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:156
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:214
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:204
NeoMutt commands API.
bool commands_register(struct CommandArray *ca, const struct Command *cmds)
Add commands to Commands array.
Definition: command.c:51
const struct Command * commands_get(struct CommandArray *ca, const char *name)
Get a Command by its name.
Definition: command.c:82
void commands_clear(struct CommandArray *ca)
Clear an Array of Commands.
Definition: command.c:70
static int commands_sort(const void *a, const void *b, void *sdata)
Compare two commands by name - Implements sort_t -.
Definition: command.c:37
Convenience wrapper for the library headers.
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:400
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:661
const char * name
Name of the command.
Definition: command.h:51