Fixed variable const qualifiers.
[dabba.git] / dabba / help.c
blobe3cb66f652544332034f949666011ae09d826c50
1 /**
2 * \file help.c
3 * \author written by Emmanuel Roullit emmanuel.roullit@gmail.com (c) 2012
4 * \date 2012
5 */
7 /* __LICENSE_HEADER_BEGIN__ */
9 /*
10 * Copyright (C) 2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
28 /* __LICENSE_HEADER_END__ */
30 #include <stdio.h>
31 #include <string.h>
32 #include <getopt.h>
33 #include <assert.h>
34 #include <libdabba/macros.h>
36 static const char dabba_usage_string[] = "dabba [--help] <command> [<args>]\n";
37 static const char dabba_more_info_string[] =
38 "See 'dabba help <command>' for more info on a specific command.";
40 struct cmdname_help {
41 char name[16];
42 char help[80];
45 void show_usage(const struct option *opt)
47 assert(opt);
49 printf("%s", dabba_usage_string);
51 if (opt != NULL) {
52 while (opt->name != NULL) {
53 printf(" --%s", opt->name);
54 if (opt->has_arg == required_argument)
55 printf(" <arg>\n");
56 else if (opt->has_arg == optional_argument)
57 printf(" [arg]\n");
58 else
59 printf("\n");
60 opt++;
65 static inline void mput_char(char c, uint32_t num)
67 while (num--)
68 putchar(c);
71 void help_unknown_cmd(const char *const cmd)
73 assert(cmd);
74 printf("'%s' is not a dabba command. See 'dabba --help'.\n", cmd);
77 static void list_common_cmds_help(void)
79 size_t i, longest = 0;
81 static struct cmdname_help common_cmds[] = {
82 {"interface", "perform an interface related command"},
83 {"capture", "capture live traffic from an interface"}
86 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
87 if (longest < strlen(common_cmds[i].name))
88 longest = strlen(common_cmds[i].name);
91 printf("The most commonly used dabba commands are:\n");
92 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
93 printf(" %s ", common_cmds[i].name);
94 mput_char(' ', longest - strlen(common_cmds[i].name));
95 printf("%s\n", common_cmds[i].help);
99 /**
100 * \brief Print help
101 * \param[in] argc Argument counter
102 * \param[in] argv Argument vector
103 * \return Always returns 0.
105 * This function prints the basic dabba usage help message.
106 * Furthermore, it lists all available command.
109 int cmd_help(int argc, const char **argv)
111 /* cast here to avoid argc and argv unused GCC warning */
112 (void)argc;
113 (void)argv;
115 printf("usage: %s\n\n", dabba_usage_string);
116 list_common_cmds_help();
117 printf("\n%s\n", dabba_more_info_string);
119 return 0;