2 * command structure borrowed from udev
3 * (git://git.kernel.org/pub/scm/linux/hotplug/udev.git)
5 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
6 * 2005-2007 Takahiro Hirofuchi
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.
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.
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/>.
28 #include "usbip_common.h"
29 #include "usbip_network.h"
32 static int usbip_help(int argc
, char *argv
[]);
33 static int usbip_version(int argc
, char *argv
[]);
35 static const char usbip_version_string
[] = PACKAGE_STRING
;
37 static const char usbip_usage_string
[] =
38 "usbip [--debug] [--log] [--tcp-port PORT] [version]\n"
39 " [help] <command> <args>\n";
41 static void usbip_usage(void)
43 printf("usage: %s", usbip_usage_string
);
48 int (*fn
)(int argc
, char *argv
[]);
53 static const struct command cmds
[] = {
69 .help
= "Attach a remote USB device",
70 .usage
= usbip_attach_usage
75 .help
= "Detach a remote USB device",
76 .usage
= usbip_detach_usage
81 .help
= "List exportable or local USB devices",
82 .usage
= usbip_list_usage
87 .help
= "Bind device to " USBIP_HOST_DRV_NAME
".ko",
88 .usage
= usbip_bind_usage
93 .help
= "Unbind device from " USBIP_HOST_DRV_NAME
".ko",
94 .usage
= usbip_unbind_usage
98 .fn
= usbip_port_show
,
99 .help
= "Show imported USB devices",
102 { NULL
, NULL
, NULL
, NULL
}
105 static int usbip_help(int argc
, char *argv
[])
107 const struct command
*cmd
;
111 if (argc
> 1 && argv
++) {
112 for (i
= 0; cmds
[i
].name
!= NULL
; i
++)
113 if (!strcmp(cmds
[i
].name
, argv
[0]) && cmds
[i
].usage
) {
122 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++)
123 if (cmd
->help
!= NULL
)
124 printf(" %-10s %s\n", cmd
->name
, cmd
->help
);
130 static int usbip_version(int argc
, char *argv
[])
135 printf(PROGNAME
" (%s)\n", usbip_version_string
);
139 static int run_command(const struct command
*cmd
, int argc
, char *argv
[])
141 dbg("running command: `%s'", cmd
->name
);
142 return cmd
->fn(argc
, argv
);
145 int main(int argc
, char *argv
[])
147 static const struct option opts
[] = {
148 { "debug", no_argument
, NULL
, 'd' },
149 { "log", no_argument
, NULL
, 'l' },
150 { "tcp-port", required_argument
, NULL
, 't' },
158 usbip_use_stderr
= 1;
161 opt
= getopt_long(argc
, argv
, "+dlt:", opts
, NULL
);
171 usbip_use_syslog
= 1;
172 openlog("", LOG_PID
, LOG_USER
);
175 usbip_setup_port_number(optarg
);
178 printf("usbip: invalid option\n");
187 for (i
= 0; cmds
[i
].name
!= NULL
; i
++)
188 if (!strcmp(cmds
[i
].name
, cmd
)) {
192 rc
= run_command(&cmds
[i
], argc
, argv
);
197 /* invalid command */
200 return (rc
> -1 ? EXIT_SUCCESS
: EXIT_FAILURE
);