1 /*
2 * command structure borrowed from udev
3 * (git://git.kernel.org/pub/scm/linux/hotplug/udev.git)
4 *
5 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
6 * 2005-2007 Takahiro Hirofuchi
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <getopt.h>
26 #include <syslog.h>
27
28 #include "usbip_common.h"
29 #include "usbip.h"
30
31 static int usbip_help(int argc, char *argv[]);
32 static int usbip_version(int argc, char *argv[]);
33
34 static const char usbip_version_string[] = PACKAGE_STRING;
35
36 static const char usbip_usage_string[] =
37 "usbip [--debug] [--log] [version]\n"
38 " [help] <command> <args>\n";
39
usbip_usage(void)40 static void usbip_usage(void)
41 {
42 printf("usage: %s", usbip_usage_string);
43 }
44
45 struct command {
46 const char *name;
47 int (*fn)(int argc, char *argv[]);
48 const char *help;
49 void (*usage)(void);
50 };
51
52 static const struct command cmds[] = {
53 {
54 .name = "help",
55 .fn = usbip_help,
56 .help = NULL,
57 .usage = NULL
58 },
59 {
60 .name = "version",
61 .fn = usbip_version,
62 .help = NULL,
63 .usage = NULL
64 },
65 {
66 .name = "attach",
67 .fn = usbip_attach,
68 .help = "Attach a remote USB device",
69 .usage = usbip_attach_usage
70 },
71 {
72 .name = "detach",
73 .fn = usbip_detach,
74 .help = "Detach a remote USB device",
75 .usage = usbip_detach_usage
76 },
77 {
78 .name = "list",
79 .fn = usbip_list,
80 .help = "List exportable or local USB devices",
81 .usage = usbip_list_usage
82 },
83 {
84 .name = "bind",
85 .fn = usbip_bind,
86 .help = "Bind device to " USBIP_HOST_DRV_NAME ".ko",
87 .usage = usbip_bind_usage
88 },
89 {
90 .name = "unbind",
91 .fn = usbip_unbind,
92 .help = "Unbind device from " USBIP_HOST_DRV_NAME ".ko",
93 .usage = usbip_unbind_usage
94 },
95 { NULL, NULL, NULL, NULL }
96 };
97
usbip_help(int argc,char * argv[])98 static int usbip_help(int argc, char *argv[])
99 {
100 const struct command *cmd;
101 int i;
102 int ret = 0;
103
104 if (argc > 1 && argv++) {
105 for (i = 0; cmds[i].name != NULL; i++)
106 if (!strcmp(cmds[i].name, argv[0]) && cmds[i].usage) {
107 cmds[i].usage();
108 goto done;
109 }
110 ret = -1;
111 }
112
113 usbip_usage();
114 printf("\n");
115 for (cmd = cmds; cmd->name != NULL; cmd++)
116 if (cmd->help != NULL)
117 printf(" %-10s %s\n", cmd->name, cmd->help);
118 printf("\n");
119 done:
120 return ret;
121 }
122
usbip_version(int argc,char * argv[])123 static int usbip_version(int argc, char *argv[])
124 {
125 (void) argc;
126 (void) argv;
127
128 printf(PROGNAME " (%s)\n", usbip_version_string);
129 return 0;
130 }
131
run_command(const struct command * cmd,int argc,char * argv[])132 static int run_command(const struct command *cmd, int argc, char *argv[])
133 {
134 dbg("running command: `%s'", cmd->name);
135 return cmd->fn(argc, argv);
136 }
137
main(int argc,char * argv[])138 int main(int argc, char *argv[])
139 {
140 static const struct option opts[] = {
141 { "debug", no_argument, NULL, 'd' },
142 { "log", no_argument, NULL, 'l' },
143 { NULL, 0, NULL, 0 }
144 };
145
146 char *cmd;
147 int opt;
148 int i, rc = -1;
149
150 usbip_use_stderr = 1;
151 opterr = 0;
152 for (;;) {
153 opt = getopt_long(argc, argv, "+d", opts, NULL);
154
155 if (opt == -1)
156 break;
157
158 switch (opt) {
159 case 'd':
160 usbip_use_debug = 1;
161 break;
162 case 'l':
163 usbip_use_syslog = 1;
164 openlog("", LOG_PID, LOG_USER);
165 break;
166 case '?':
167 printf("usbip: invalid option\n");
168 default:
169 usbip_usage();
170 goto out;
171 }
172 }
173
174 cmd = argv[optind];
175 if (cmd) {
176 for (i = 0; cmds[i].name != NULL; i++)
177 if (!strcmp(cmds[i].name, cmd)) {
178 argc -= optind;
179 argv += optind;
180 optind = 0;
181 rc = run_command(&cmds[i], argc, argv);
182 goto out;
183 }
184 }
185
186 /* invalid command */
187 usbip_help(0, NULL);
188 out:
189 return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);
190 }
191