Coverage Report

Created: 2026-11-28 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/commands/help.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
// SPDX-FileCopyrightText: © 2000-2003 Wolfgang Denk <wd@denx.de>, DENX Software Engineering
3
4
#include <common.h>
5
#include <command.h>
6
#include <getopt.h>
7
#include <complete.h>
8
9
10
static void list_group(int verbose, const char *grpname, uint32_t group)
11
0
{
12
0
  struct command *cmdtp;
13
0
  bool first = true;
14
0
  int pos = 0;
15
0
  int len;
16
17
0
  for_each_command(cmdtp) {
18
0
    if (cmdtp->group != group)
19
0
      continue;
20
0
    if (first) {
21
0
      first = false;
22
0
      printf("%s commands:\n", grpname);
23
0
      if (!verbose) {
24
0
        printf("  ");
25
0
        pos = 2;
26
0
      }
27
0
    }
28
0
    if (verbose) {
29
0
      printf("  %-21s %s\n", cmdtp->name, cmdtp->desc);
30
0
      continue;
31
0
    }
32
0
    len = strlen(cmdtp->name);
33
0
    if (pos + len + 2 > 77) {
34
0
      printf("\n  %s", cmdtp->name);
35
0
      pos = len + 2;
36
0
    } else {
37
0
      if (pos != 2) {
38
0
        printf(", ");
39
0
        pos += 2;
40
0
      }
41
0
      puts(cmdtp->name);
42
0
      pos += len;
43
0
    }
44
0
  }
45
0
  if (!first)
46
0
    printf("\n");
47
0
}
48
49
static void list_commands(int verbose)
50
0
{
51
0
  putchar('\n');
52
0
  list_group(verbose, "Information",           CMD_GRP_INFO);
53
0
  list_group(verbose, "Boot",                  CMD_GRP_BOOT);
54
0
  list_group(verbose, "Partition",             CMD_GRP_PART);
55
0
  list_group(verbose, "Environment",           CMD_GRP_ENV);
56
0
  list_group(verbose, "File",                  CMD_GRP_FILE);
57
0
  list_group(verbose, "Scripting",             CMD_GRP_SCRIPT);
58
0
  list_group(verbose, "Network",               CMD_GRP_NET);
59
0
  list_group(verbose, "Console",               CMD_GRP_CONSOLE);
60
0
  list_group(verbose, "Memory",                CMD_GRP_MEM);
61
0
  list_group(verbose, "Hardware manipulation", CMD_GRP_HWMANIP);
62
0
  list_group(verbose, "Security",              CMD_GRP_SECURITY);
63
0
  list_group(verbose, "Miscellaneous",         CMD_GRP_MISC);
64
0
  list_group(verbose, "Ungrouped",             0);
65
0
  printf("Use 'help COMMAND' for more details.\n\n");
66
0
}
67
68
69
/*
70
 * Use puts() instead of printf() to avoid printf buffer overflow
71
 * for long help messages
72
 */
73
static int do_help(int argc, char *argv[])
74
0
{
75
0
  struct command *cmdtp;
76
0
  int opt, verbose = 0, all = 0;
77
78
0
  while ((opt = getopt(argc, argv, "va")) > 0) {
79
0
    switch (opt) {
80
0
    case 'v':
81
0
      verbose = 1;
82
0
      break;
83
0
    case 'a':
84
0
      all = 1;
85
0
      break;
86
0
    default:
87
0
      return COMMAND_ERROR_USAGE;
88
0
    }
89
0
  }
90
91
0
  if (all) {
92
0
    for_each_command(cmdtp)
93
0
      barebox_cmd_usage(cmdtp);
94
0
    return 0;
95
0
  }
96
97
0
  if (optind == argc) { /* show list of commands */
98
0
    list_commands(verbose);
99
0
    return 0;
100
0
  }
101
102
103
  /* command help (long version) */
104
0
  if ((cmdtp = find_cmd(argv[optind])) != NULL) {
105
0
    barebox_cmd_usage(cmdtp);
106
0
    return 0;
107
0
  } else {
108
0
    printf ("Unknown command '%s' - try 'help'"
109
0
      " without arguments for list of all"
110
0
      " known commands\n\n", argv[optind]
111
0
        );
112
0
    return 1;
113
0
  }
114
0
}
115
116
117
BAREBOX_CMD_HELP_START(help)
118
BAREBOX_CMD_HELP_TEXT("Without arguments, lists all commands. With an argument, print help")
119
BAREBOX_CMD_HELP_TEXT("about the specified command. If the argument is 'all', then output help")
120
BAREBOX_CMD_HELP_TEXT("for all commands.")
121
BAREBOX_CMD_HELP_TEXT("")
122
BAREBOX_CMD_HELP_TEXT("Options:")
123
BAREBOX_CMD_HELP_OPT ("-a", "output help on all commands")
124
BAREBOX_CMD_HELP_OPT ("-v", "verbose")
125
BAREBOX_CMD_HELP_END
126
127
static const char * const help_aliases[] = { "?", NULL};
128
129
BAREBOX_CMD_START(help)
130
  .cmd    = do_help,
131
  .aliases  = help_aliases,
132
  BAREBOX_CMD_DESC("print online help")
133
  BAREBOX_CMD_OPTS("[-v] [COMMAND]")
134
  BAREBOX_CMD_GROUP(CMD_GRP_INFO)
135
  BAREBOX_CMD_HELP(cmd_help_help)
136
  BAREBOX_CMD_COMPLETE(command_complete)
137
BAREBOX_CMD_END