Debian Codegraph @

src/cmdline.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2015, 2017 Moritz Grimm <[email protected]>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif /* HAVE_CONFIG_H */
20
21 #include "compat.h"
22
23 #include <assert.h>
24 #include <stdio.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif /* HAVE_UNISTD_H */
28
29 #include <string.h>
30
31 #include "cfg.h"
32 #include "cmdline.h"
33 #include "playlist.h"
34 #include "util.h"
35
36 #define OPTSTRING "c:hp:qrs:Vv"
37 enum opt_vals {
38 OPT_CONFIGFILE = 'c',
39 OPT_HELP = 'h',
40 OPT_PIDFILE = 'p',
41 OPT_QUIETSTDERR = 'q',
42 OPT_RTSTATUS = 'r',
43 OPT_SHUFFLEFILE = 's',
44 OPT_VERSION = 'V',
45 OPT_VERBOSE = 'v',
46 OPT_INVALID = '?'
47 };
48
49 static void _usage(void);
50 static void _usage_help(void);
51
52 static void
53 _usage(void)
54 {
55 fprintf(stderr, "usage: %s [-hqrVv] -c cfgfile [-p pidfile]\n",
56 cfg_get_program_name());
57 fprintf(stderr, " %s -s file\n",
58 cfg_get_program_name());
59 }
60
61 static void
62 _usage_help(void)
63 {
64 fprintf(stderr, "\n");
65 fprintf(stderr, " -c cfgfile use XML configuration in cfgfile\n");
66 fprintf(stderr, " -h print this help and exit\n");
67 fprintf(stderr, " -p pidfile write PID to pidfile\n");
68 fprintf(stderr, " -q suppress STDERR output from external en-/decoders\n");
69 fprintf(stderr, " -r show real-time stream information on stdout\n");
70 fprintf(stderr, " -s file read lines from file, shuffle, print to STDOUT, then exit\n");
71 fprintf(stderr, " -V print the version number and exit\n");
72 fprintf(stderr, " -v increase logging verbosity\n");
73 }
74
75 int
76 cmdline_parse(int argc, char *argv[], int *ret_p)
77 {
78 const char *playlistFile = NULL;
79 unsigned int verbosity = 0;
80 const char *err_str;
81
82 (void)cfg_set_program_name(util_get_progname(argv[0]), NULL);
83
84 optind = 1;
85 for (;;) {
86 int ch;
87
88 ch = getopt(argc, argv, OPTSTRING);
89 if (0 > ch)
90 break;
91
92 switch (ch) {
93 case OPT_CONFIGFILE:
94 assert(0 == cfg_set_program_config_type(CFG_TYPE_XMLFILE,
95 NULL));
96 if (0 > cfg_set_program_config_file(optarg, &err_str)) {
97 fprintf(stderr, "-%c: argument %s\n",
98 OPT_CONFIGFILE, err_str);
99 _usage();
100 *ret_p = 2;
101 return (-1);
102 }
103 break;
104 case OPT_HELP:
105 _usage();
106 _usage_help();
107 *ret_p = 0;
108 return (-1);
109 case OPT_PIDFILE:
110 if (0 > cfg_set_program_pid_file(optarg, &err_str)) {
111 fprintf(stderr, "-%c: argument %s\n",
112 OPT_PIDFILE, err_str);
113 _usage();
114 *ret_p = 2;
115 return (-1);
116 }
117 break;
118 case OPT_RTSTATUS:
119 cfg_set_program_rtstatus_output(1, NULL);
120 /* FALLTHROUGH */
121 case OPT_QUIETSTDERR:
122 cfg_set_program_quiet_stderr(1, NULL);
123 break;
124 case OPT_SHUFFLEFILE:
125 playlistFile = optarg;
126 break;
127 case OPT_VERSION:
128 fprintf(stdout, "%s version %s\n",
129 PACKAGE_NAME, PACKAGE_VERSION);
130 *ret_p = 0;
131 return (-1);
132 case OPT_VERBOSE:
133 verbosity++;
134 break;
135 case OPT_INVALID:
136 default:
137 _usage();
138 *ret_p = 2;
139 return (-1);
140 }
141 }
142 argc -= optind;
143 argv += optind;
144
145 cfg_set_program_verbosity(verbosity, NULL);
146
147 if (playlistFile) {
148 playlist_t pl;
149
150 if (0 > playlist_init()) {
151 *ret_p = 1;
152 return (-1);
153 }
154 if (0 == strcmp(playlistFile, "-")) {
155 pl = playlist_read(NULL);
156 } else {
157 pl = playlist_read(playlistFile);
158 }
159 if (pl == NULL) {
160 *ret_p = 1;
161 } else {
162 const char *entry;
163
164 playlist_shuffle(pl);
165 while (NULL != (entry = playlist_get_next(pl)))
166 printf("%s\n", entry);
167 playlist_free(&pl);
168 *ret_p = 0;
169 }
170 playlist_exit();
171 return (-1);
172 }
173
174 if (!cfg_get_program_config_file()) {
175 fprintf(stderr, "either -%c or -%c must be provided\n",
176 OPT_CONFIGFILE, OPT_SHUFFLEFILE);
177 _usage();
178 *ret_p = 2;
179 return (-1);
180 }
181
182 return (0);
183 }