Added dabba capture help command unit tests.
[dabba.git] / dabba / dabba.c
blob1ca8fd9dd0fa69545ecc290797d1d44af99e2f9f
1 /**
2 * \file dabba.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__ */
32 =head1 NAME
34 dabba - Send queries to dabbad and process its replies.
36 =head1 SYNOPSIS
38 dabba <command> <arguments> [--help]
40 =head1 DESCRIPTION
42 dabba is here to generate IPC queries to dabbad and output its
43 replies formatted in a YAML output
45 =head1 OPTIONS
47 =over
49 =item --help
51 Prints help output and the currently supported commands.
53 =back
55 =head1 AUTHOR
57 Written by Emmanuel Roullit <emmanuel.roullit@gmail.com>
59 =head1 BUGS
61 =over
63 =item Please report bugs to <https://github.com/eroullit/dabba/issues>
65 =item dabba project project page: <https://github.com/eroullit/dabba>
67 =back
69 =head1 COPYRIGHT
71 =over
73 =item Copyright © 2012 Emmanuel Roullit.
75 =item License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.
77 =item This is free software: you are free to change and redistribute it.
79 =item There is NO WARRANTY, to the extent permitted by law.
81 =back
83 =cut
87 #include <stdio.h>
88 #include <stdlib.h>
89 #include <string.h>
90 #include <unistd.h>
91 #include <assert.h>
92 #include <errno.h>
93 #include <sys/stat.h>
95 #include <libdabba/macros.h>
96 #include <dabbad/dabbad.h>
97 #include <dabba/dabba.h>
98 #include <dabba/help.h>
99 #include <dabba/interface.h>
100 #include <dabba/capture.h>
102 int run_builtin(struct cmd_struct *p, int argc, const char **argv)
104 int status;
105 struct stat st;
107 assert(p);
109 status = p->fn(argc, argv);
110 if (status)
111 return status;
113 /* Somebody closed stdout? */
114 if (fstat(fileno(stdout), &st))
115 return 0;
116 /* Ignore write errors for pipes and sockets.. */
117 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
118 return 0;
120 /* Check for ENOSPC and EIO errors.. */
121 if (fflush(stdout)) {
122 perror("write failure on standard output");
123 return (errno);
126 if (ferror(stdout)) {
127 perror("unknown write failure on standard output");
128 return (1);
131 if (fclose(stdout)) {
132 perror("close failed on standard output");
133 return (errno);
136 return 0;
139 static int handle_internal_command(int argc, const char **argv)
141 size_t i;
142 const char *cmd = argv[0];
143 static struct cmd_struct commands[] = {
144 {"interface", cmd_interface},
145 {"capture", cmd_capture},
146 {"help", cmd_help}
149 if (argc == 0 || cmd == NULL || !strcmp(cmd, "--help"))
150 cmd = "help";
152 /* Turn "dabba cmd --help" into "dabba help cmd" */
153 if (argc > 1 && !strcmp(argv[1], "--help")) {
154 argv[1] = argv[0];
155 argv[0] = cmd = "help";
158 for (i = 0; i < ARRAY_SIZE(commands); i++) {
159 struct cmd_struct *p = commands + i;
160 if (strcmp(p->cmd, cmd))
161 continue;
163 argc--;
164 argv++;
166 return (run_builtin(p, argc, argv));
169 help_unknown_cmd(cmd);
171 return ENOSYS;
174 int main(int argc, const char **argv)
176 assert(argc);
177 assert(argv);
179 argc--;
180 argv++;
182 return (handle_internal_command(argc, argv));