Fixed variable const qualifiers.
[dabba.git] / dabba / dabba.c
bloba64802b54685e2ff84b70261132a4c738b9fbcfe
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__ */
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <assert.h>
35 #include <errno.h>
36 #include <sys/stat.h>
38 #include <libdabba/macros.h>
39 #include <dabbad/dabbad.h>
40 #include <dabba/dabba.h>
41 #include <dabba/help.h>
42 #include <dabba/interface.h>
43 #include <dabba/capture.h>
45 int run_builtin(struct cmd_struct *p, int argc, const char **argv)
47 int status;
48 struct stat st;
50 assert(p);
52 status = p->fn(argc, argv);
53 if (status)
54 return status;
56 /* Somebody closed stdout? */
57 if (fstat(fileno(stdout), &st))
58 return 0;
59 /* Ignore write errors for pipes and sockets.. */
60 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
61 return 0;
63 /* Check for ENOSPC and EIO errors.. */
64 if (fflush(stdout)) {
65 perror("write failure on standard output");
66 return (errno);
69 if (ferror(stdout)) {
70 perror("unknown write failure on standard output");
71 return (1);
74 if (fclose(stdout)) {
75 perror("close failed on standard output");
76 return (errno);
79 return 0;
82 static int handle_internal_command(int argc, const char **argv)
84 size_t i;
85 const char *cmd = argv[0];
86 static struct cmd_struct commands[] = {
87 {"interface", cmd_interface},
88 {"capture", cmd_capture},
89 {"help", cmd_help}
92 if (argc == 0 || cmd == NULL || !strcmp(cmd, "--help"))
93 cmd = "help";
95 /* Turn "dabba cmd --help" into "dabba help cmd" */
96 if (argc > 1 && !strcmp(argv[1], "--help")) {
97 argv[1] = argv[0];
98 argv[0] = cmd = "help";
101 for (i = 0; i < ARRAY_SIZE(commands); i++) {
102 struct cmd_struct *p = commands + i;
103 if (strcmp(p->cmd, cmd))
104 continue;
106 argc--;
107 argv++;
109 return (run_builtin(p, argc, argv));
112 help_unknown_cmd(cmd);
114 return ENOSYS;
117 int main(int argc, const char **argv)
119 assert(argc);
120 assert(argv);
122 argc--;
123 argv++;
125 return (handle_internal_command(argc, argv));