[PATCH evemu 2/3] tools: play: use getopt
Peter Hutterer
peter.hutterer at who-t.net
Thu Jan 9 14:38:43 PST 2014
On Thu, Jan 09, 2014 at 05:17:43PM -0500, Benjamin Tissoires wrote:
> On Thu, Jan 9, 2014 at 5:06 PM, Benjamin Tissoires
> <benjamin.tissoires at gmail.com> wrote:
> > On Thu, Jan 9, 2014 at 5:07 PM, Peter Hutterer <peter.hutterer at who-t.net> wrote:
> >> On Thu, Jan 09, 2014 at 02:34:08PM -0500, Benjamin Tissoires wrote:
> >>> Instead of relying on argc and hard coded values, use getopt framework.
> >>>
> >>> Signed-off-by: Benjamin Tissoires <benjamin.tissoires at gmail.com>
> >>> ---
> >>> tools/evemu-play.c | 37 +++++++++++++++++++++++++++++++++----
> >>> 1 file changed, 33 insertions(+), 4 deletions(-)
> >>>
> >>> diff --git a/tools/evemu-play.c b/tools/evemu-play.c
> >>> index 8d14618..030e142 100644
> >>> --- a/tools/evemu-play.c
> >>> +++ b/tools/evemu-play.c
> >>> @@ -45,10 +45,18 @@
> >>> #include <fcntl.h>
> >>> #include <string.h>
> >>> #include <unistd.h>
> >>> +#include <getopt.h>
> >>> +
> >>> +static struct option opts[] = {
> >>> + { "help", no_argument, 0, 'h'},
> >>> +};
> >>>
> >>> static void usage(void)
> >>> {
> >>> - fprintf(stderr, "Usage: %s <device>\n", program_invocation_short_name);
> >>> + fprintf(stderr, "Usage: %s [OPTION] <device>\n", program_invocation_short_name);
> >>> + fprintf(stderr, "\n");
> >>> + fprintf(stderr, "Where OPTION is:\n");
> >>> + fprintf(stderr, " -h or --help: print this message\n");
> >>> fprintf(stderr, "\n");
> >>> fprintf(stderr, "Event data is read from standard input.\n");
> >>> }
> >>> @@ -56,11 +64,30 @@ static void usage(void)
> >>> int main(int argc, char *argv[])
> >>> {
> >>> int fd;
> >>> - if (argc != 2) {
> >>> +
> >>> + while(1) {
> >>> + int option_index = 0;
> >>> + int c;
> >>> +
> >>> + c = getopt_long(argc, argv, "h", opts, &option_index);
> >>
> >> don't you need GNU_SOURCE for getopt_long or is that pulled in from
> >> elsewhere?
> >
> > Yes, I need it... :(
> > I bet I missed my split between patches 2 and 3...
>
> Actually no. It was added in 1/3: tools: play: externalize usage() call
oh right, read over that. just move it to here please.
Cheers,
Peter
> >
> >> also, we could probably drop it from other files and use
> >> AC_USE_SYSTEM_EXTENSIONS in configure.ac.
> >
> > will see what I can do.
> >
> >>
> >>> + if (c == -1) /* Detect the end of the options. */
> >>> + break;
> >>> +
> >>> + switch(c) {
> >>> + case 'h': /* help */
> >>> + default:
> >>> + usage();
> >>> + goto out;
> >>> + }
> >>> + }
> >>> +
> >>> + /* if the device was not specified, fail */
> >>> + if (optind >= argc) {
> >>> usage();
> >>> - return -1;
> >>> + goto out;
> >>> }
> >>> - fd = open(argv[1], O_WRONLY);
> >>> +
> >>> + fd = open(argv[optind], O_WRONLY);
> >>> if (fd < 0) {
> >>> fprintf(stderr, "error: could not open device\n");
> >>> return -1;
> >>> @@ -70,4 +97,6 @@ int main(int argc, char *argv[])
> >>> }
> >>> close(fd);
> >>> return 0;
> >>> +out:
> >>> + return -1;
> >>
> >> we should probably return 1 instead of -1 but that's a follow-up patch.
> >
> > probably :)
> >
> > Cheers,
> > Benjamin
More information about the Input-tools
mailing list