capture: Prepare a 32 frame packet mmap buffer by default.
[dabba.git] / dabba / help.c
blobc693798841c551cb4a78295ee9786cc2daacddb7
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 <unistd.h>
33 #include <errno.h>
34 #include <getopt.h>
35 #include <assert.h>
36 #include <libdabba/macros.h>
38 static const char dabba_usage_string[] =
39 "dabba [--help] [--version] <command> [<args>]\n";
40 static const char dabba_more_info_string[] =
41 "See 'dabba help <command>' for more info on a specific command.";
43 struct cmdname_help {
44 char name[16];
45 char help[80];
48 void show_usage(const struct option *opt)
50 assert(opt);
52 printf("%s", dabba_usage_string);
54 if (opt != NULL) {
55 while (opt->name != NULL) {
56 printf(" --%s", opt->name);
57 if (opt->has_arg == required_argument)
58 printf(" <arg>\n");
59 else if (opt->has_arg == optional_argument)
60 printf(" [arg]\n");
61 else
62 printf("\n");
63 opt++;
68 static inline void mput_char(char c, uint32_t num)
70 while (num--)
71 putchar(c);
74 void help_unknown_cmd(const char *const cmd)
76 assert(cmd);
77 printf("'%s' is not a dabba command. See 'dabba --help'.\n", cmd);
80 static void list_common_cmds_help(void)
82 size_t i, longest = 0;
84 static struct cmdname_help common_cmds[] = {
85 {"interface", "perform an interface related command"},
86 {"capture", "capture live traffic from an interface"}
89 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
90 if (longest < strlen(common_cmds[i].name))
91 longest = strlen(common_cmds[i].name);
94 printf("The most commonly used dabba commands are:\n");
95 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
96 printf(" %s ", common_cmds[i].name);
97 mput_char(' ', longest - strlen(common_cmds[i].name));
98 printf("%s\n", common_cmds[i].help);
103 * \brief Print help
104 * \param[in] argc Argument counter
105 * \param[in] argv Argument vector
106 * \return Returns 0 when the help could be printed, \c errno otherwise
108 * This function prints the basic dabba usage help message.
109 * Furthermore, it lists all available command.
112 int cmd_help(int argc, const char **argv)
114 char help_name[32];
115 int rc = 0;
117 if (argc < 1 || !argv[0]) {
118 printf("usage: %s\n\n", dabba_usage_string);
119 list_common_cmds_help();
120 printf("\n%s\n", dabba_more_info_string);
121 } else {
122 snprintf(help_name, sizeof(help_name), "dabba-%s", argv[0]);
123 rc = execlp("man", "man", help_name, (char *)NULL);
126 return rc ? errno : rc;
129 int cmd_version(int argc, const char **argv)
131 (void)argc;
132 (void)argv;
134 printf("dabba version %s\n", DABBA_VERSION);
135 return 0;