3 * \author written by Emmanuel Roullit emmanuel.roullit@gmail.com (C) 2013
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.";
28 * \brief print option list to \c stdout
29 * \param[in] opt Pointer to option array
32 void show_usage(const struct option
*opt
)
36 printf("%s", dabba_usage_string
);
38 while (opt
->name
!= NULL
) {
39 printf(" --%s", opt
->name
);
40 if (opt
->has_arg
== required_argument
)
42 else if (opt
->has_arg
== optional_argument
)
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
)
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
)
71 printf("'%s' is not a dabba command. See 'dabba --help'.\n", cmd
);
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
);
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
];
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
);
123 offset
= snprintf(help_name
, sizeof(help_name
), "dabba");
125 for (a
= 0; a
< argc
; a
++)
127 snprintf(&help_name
[offset
],
128 sizeof(help_name
) - offset
, "-%s",
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
)
149 printf("dabba version %s\n", DABBA_VERSION
);