Coverage Report

Created: 2025-04-22 06:17

/src/neomutt/core/command.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * @file
3
 * NeoMutt Commands
4
 *
5
 * @authors
6
 * Copyright (C) 2023 Richard Russon <rich@flatcap.org>
7
 *
8
 * @copyright
9
 * This program is free software: you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License as published by the Free Software
11
 * Foundation, either version 2 of the License, or (at your option) any later
12
 * version.
13
 *
14
 * This program is distributed in the hope that it will be useful, but WITHOUT
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU General Public License along with
20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
/**
24
 * @page core_command NeoMutt Commands
25
 *
26
 * NeoMutt Commands
27
 */
28
29
#include "config.h"
30
#include <stddef.h>
31
#include "mutt/lib.h"
32
#include "command.h"
33
34
/**
35
 * commands_sort - Compare two commands by name - Implements ::sort_t - @ingroup sort_api
36
 */
37
static int commands_sort(const void *a, const void *b, void *sdata)
38
0
{
39
0
  const struct Command *x = *(const struct Command **) a;
40
0
  const struct Command *y = *(const struct Command **) b;
41
42
0
  return mutt_str_cmp(x->name, y->name);
43
0
}
44
45
/**
46
 * commands_register - Add commands to Commands array
47
 * @param ca   Command Array
48
 * @param cmds New Commands to add
49
 * @retval true Success
50
 */
51
bool commands_register(struct CommandArray *ca, const struct Command *cmds)
52
0
{
53
0
  if (!ca || !cmds)
54
0
    return false;
55
56
0
  for (int i = 0; cmds[i].name; i++)
57
0
  {
58
0
    ARRAY_ADD(ca, &cmds[i]);
59
0
  }
60
0
  ARRAY_SORT(ca, commands_sort, NULL);
61
62
0
  return true;
63
0
}
64
65
/**
66
 * commands_clear - Clear an Array of Commands
67
 *
68
 * @note The Array itself is not freed
69
 */
70
void commands_clear(struct CommandArray *ca)
71
0
{
72
0
  ARRAY_FREE(ca);
73
0
}
74
75
/**
76
 * commands_get - Get a Command by its name
77
 * @param ca   Command Array
78
 * @param name Command name to lookup
79
 * @retval ptr  Success, Command
80
 * @retval NULL Error, no such command
81
 */
82
const struct Command *commands_get(struct CommandArray *ca, const char *name)
83
0
{
84
0
  const struct Command **cp = NULL;
85
0
  ARRAY_FOREACH(cp, ca)
86
0
  {
87
0
    const struct Command *cmd = *cp;
88
89
0
    if (mutt_str_equal(name, cmd->name))
90
0
      return cmd;
91
0
  }
92
0
  return NULL;
93
0
}