Coverage Report

Created: 2026-11-28 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/common/command.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * (C) Copyright 2000-2003
4
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5
 */
6
7
/*
8
 *  Command Processor Table
9
 */
10
11
#include <common.h>
12
#include <command.h>
13
#include <xfuncs.h>
14
#include <malloc.h>
15
#include <environment.h>
16
#include <linux/list.h>
17
#include <init.h>
18
#include <complete.h>
19
#include <getopt.h>
20
21
LIST_HEAD(command_list);
22
EXPORT_SYMBOL(command_list);
23
24
void barebox_cmd_usage(struct command *cmdtp)
25
0
{
26
0
  putchar('\n');
27
0
  if (cmdtp->desc)
28
0
    printf("%s - %s\n\n", cmdtp->name, cmdtp->desc);
29
0
  if (cmdtp->opts)
30
0
    printf("Usage: %s %s\n\n", cmdtp->name, cmdtp->opts);
31
0
#ifdef CONFIG_LONGHELP
32
  /* found - print (long) help info */
33
0
  if (cmdtp->help) {
34
0
    puts(cmdtp->help);
35
0
    putchar('\n');
36
0
  }
37
0
  if (cmdtp->usage)
38
0
    cmdtp->usage();
39
0
#endif
40
0
}
41
EXPORT_SYMBOL(barebox_cmd_usage);
42
43
static int compare(struct list_head *a, struct list_head *b)
44
322k
{
45
322k
  char *na = (char*)list_entry(a, struct command, list)->name;
46
322k
  char *nb = (char*)list_entry(b, struct command, list)->name;
47
48
322k
  return strcmp(na, nb);
49
322k
}
50
51
int execute_command(int argc, char **argv)
52
0
{
53
0
  struct command *cmdtp;
54
0
  int ret;
55
0
  struct getopt_context gc;
56
57
0
  getopt_context_store(&gc);
58
59
  /* Look up command in command table */
60
0
  if ((cmdtp = find_cmd(argv[0]))) {
61
    /* OK - call function to do the command */
62
0
    ret = cmdtp->cmd(argc, argv);
63
0
    if (ret == COMMAND_ERROR_USAGE) {
64
0
      barebox_cmd_usage(cmdtp);
65
0
      ret = COMMAND_ERROR;
66
0
    }
67
0
  } else {
68
0
#ifdef CONFIG_CMD_HELP
69
0
    printf ("Unknown command '%s' - try 'help'\n", argv[0]);
70
#else
71
    printf ("Unknown command '%s'\n", argv[0]);
72
#endif
73
0
    ret = COMMAND_ERROR; /* give up after bad command */
74
0
  }
75
76
0
  getopt_context_restore(&gc);
77
78
0
  return ret;
79
0
}
80
81
int register_command(struct command *cmd)
82
4.73k
{
83
  /*
84
   * We do not check if the command already exists.
85
   * This allows us to overwrite a builtin command
86
   * with a module.
87
   */
88
89
4.73k
  debug("register command %s\n", cmd->name);
90
91
4.73k
  list_add_sort(&cmd->list, &command_list, compare);
92
93
4.73k
  if (cmd->aliases) {
94
256
    const char * const *aliases = cmd->aliases;
95
576
    while(*aliases) {
96
320
      struct command *c = xzalloc(sizeof(struct command));
97
98
320
      memcpy(c, cmd, sizeof(struct command));
99
100
320
      c->name = *aliases;
101
320
      c->desc = cmd->desc;
102
320
      c->opts = cmd->opts;
103
104
320
      c->aliases = NULL;
105
106
320
      register_command(c);
107
108
320
      aliases++;
109
320
    }
110
256
  }
111
112
4.73k
  return 0;
113
4.73k
}
114
EXPORT_SYMBOL(register_command);
115
116
/*
117
 * find command table entry for a command
118
 */
119
struct command *find_cmd (const char *cmd)
120
0
{
121
0
  struct command *cmdtp;
122
123
0
  for_each_command(cmdtp)
124
0
    if (!strcmp(cmd, cmdtp->name))
125
0
      return cmdtp;
126
127
0
  return NULL; /* not found or ambiguous command */
128
0
}
129
EXPORT_SYMBOL(find_cmd);
130
131
int cmd_export_val(const char *variable, const char *val)
132
0
{
133
0
  if (!val)
134
0
    return -EINVAL;
135
136
0
  if (variable)
137
0
    return setenv(variable, val);
138
139
0
  printf("%s\n", val);
140
0
  return 0;
141
0
}
142
EXPORT_SYMBOL(cmd_export_val);
143
144
/*
145
 * Put all commands into a linked list. Without module support we could use
146
 * the raw command array, but with module support a list is easier to handle.
147
 * It does not create significant overhead so do it also without module
148
 * support.
149
 */
150
static int init_command_list(void)
151
32
{
152
32
  struct command * const *cmdtp;
153
154
32
  for (cmdtp = __barebox_cmd_start;
155
4.44k
      cmdtp != __barebox_cmd_end;
156
4.41k
      cmdtp++)
157
4.41k
    register_command(*cmdtp);
158
159
32
  return 0;
160
32
}
161
162
late_initcall(init_command_list);