git: do not track vim swo temp file.
[dabba.git] / dabba / help.c
blobe7b58173876bbc09d39da1749c474f3c096f6211
1 /**
2 * \file help.c
3 * \author written by Emmanuel Roullit emmanuel.roullit@gmail.com (C) 2013
4 * \date 2013
5 */
8 #include <stdio.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <getopt.h>
13 #include <assert.h>
14 #include <limits.h>
15 #include <libdabba/macros.h>
17 static const char dabba_usage_string[] =
18 "dabba [--help] [--version] <command> [<subcommand>] <action> [<args>]\n";
19 static const char dabba_more_info_string[] =
20 "See 'dabba help <command> [<subcommand>]' for more specific information.";
22 struct cmdname_help {
23 char name[16];
24 char help[80];
27 /**
28 * \brief print option list to \c stdout
29 * \param[in] opt Pointer to option array
32 void show_usage(const struct option *opt)
34 assert(opt);
36 printf("%s", dabba_usage_string);
38 while (opt->name != NULL) {
39 printf(" --%s", opt->name);
40 if (opt->has_arg == required_argument)
41 printf(" <arg>\n");
42 else if (opt->has_arg == optional_argument)
43 printf(" [arg]\n");
44 else
45 printf("\n");
46 opt++;
50 /**
51 * \internal
52 * \brief add some indenting on \c stdout
53 * \param[in] c Character to indent
54 * \param[in] num Indent level
57 static inline void mput_char(const char c, uint32_t num)
59 while (num--)
60 putchar(c);
63 /**
64 * \brief print on \c stdout that the invoked command is unknown
65 * \param[in] cmd Pointer to invoked command string
68 void help_unknown_cmd(const char *const cmd)
70 assert(cmd);
71 printf("'%s' is not a dabba command. See 'dabba --help'.\n", cmd);
74 /**
75 * \brief list on \c stdout the commonly used commands.
78 static void list_common_cmds_help(void)
80 size_t i, longest = 0;
82 static struct cmdname_help common_cmds[] = {
83 {"interface", "perform an interface related command"},
84 {"thread", "perform a thread related command"},
85 {"capture", "capture live traffic from an interface"},
86 {"replay", "replay traffic from a pcap file"}
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[NAME_MAX];
115 int rc = 0, a;
116 size_t offset;
118 if (argc < 1 || !argv[0]) {
119 printf("usage: %s\n\n", dabba_usage_string);
120 list_common_cmds_help();
121 printf("\n%s\n", dabba_more_info_string);
122 } else {
123 offset = snprintf(help_name, sizeof(help_name), "dabba");
125 for (a = 0; a < argc; a++)
126 offset +=
127 snprintf(&help_name[offset],
128 sizeof(help_name) - offset, "-%s",
129 argv[a]);
131 rc = execlp("man", "man", help_name, NULL);
134 return rc ? errno : rc;
138 * \brief Print tool version number to \c stdout
139 * \param[in] argc Argument counter
140 * \param[in] argv Argument vector
141 * \return Always returns zero.
144 int cmd_version(int argc, const char **argv)
146 (void)argc;
147 (void)argv;
149 printf("dabba version %s\n", DABBA_VERSION);
150 return 0;