Handle already exited ogg123(1) gracefully
[oggquiz.git] / options.c
blob8fed8bda0af4fc9e4beccf4c4199b41517bfa009
1 /*-
2 * "THE BEER-WARE LICENSE" (Revision 42):
3 * <tobias.rehbein@web.de> wrote this file. As long as you retain this notice
4 * you can do whatever you want with this stuff. If we meet some day, and you
5 * think this stuff is worth it, you can buy me a beer in return.
6 * Tobias Rehbein
7 */
9 #include <assert.h>
10 #include <err.h>
11 #include <getopt.h>
12 #include <limits.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sysexits.h>
17 #include "options.h"
19 enum {
20 TIME = 60,
21 CHOICES = 4,
22 PLAYERS = 4
25 static const char *VERSION = "v1.0.0";
26 static const char *OGG123 = "/usr/local/bin/ogg123";
27 static const char *OGG123_OPTIONS = "";
29 void init_options(struct opts_options *opts);
30 static void print_usage(void);
32 void
33 init_options(struct opts_options *opts)
35 assert(opts != NULL);
37 opts->time = TIME;
38 opts->choices = CHOICES;
39 opts->players = PLAYERS;
40 opts->ogg123 = OGG123;
41 opts->ogg123_options = OGG123_OPTIONS;
44 void
45 opts_parse_options(struct opts_options *opts, int argc, char **argv)
47 int ch;
49 assert(opts != NULL);
50 assert(argc >= 0);
51 assert(argv != NULL);
53 struct option longopts[] = {
54 {"time", required_argument, NULL, 't'},
55 {"choices", required_argument, NULL, 'c'},
56 {"players", required_argument, NULL, 'p'},
57 {"ogg123", required_argument, NULL, 'O'},
58 {"ogg123-options", required_argument, NULL, 'o'},
59 {"help", no_argument, NULL, 'h'}
62 init_options(opts);
64 while ((ch = getopt_long(argc, argv, "t:c:p:o:e:h", longopts, NULL)) != -1)
65 switch (ch) {
66 case 't':
67 opts->time = (int)strtol(optarg, (char **)NULL, 10);
68 break;
69 case 'c':
70 opts->choices = (int)strtol(optarg, (char **)NULL, 10);
71 if (opts->choices < 1 || opts->choices > CHOICES)
72 errx(EX_USAGE, "choices must not exceed %d", CHOICES);
73 break;
74 case 'p':
75 opts->players = (int)strtol(optarg, (char **)NULL, 10);
76 if (opts->players < 1 || opts->players > PLAYERS)
77 errx(EX_USAGE, "players must not exceed %d", PLAYERS);
78 break;
79 case 'O':
80 opts->ogg123 = optarg;
81 break;
82 case 'o':
83 opts->ogg123_options = optarg;
84 break;
85 default:
86 print_usage();
87 exit(1);
88 case 'h':
89 print_usage();
90 exit(0);
94 static void
95 print_usage()
97 printf("This is oggquiz %s\n\n", VERSION);
98 printf("oggquiz [-t | --time seconds] [-c | --choices choices] [-p | --players players]\n");
99 printf(" [-o | --ogg123-options options] [-O | --ogg123 command]\n\n");
100 printf("oggquiz {-h | --help}\n");