File: Options.cxx

package info (click to toggle)
ncmpc 0.33-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,976 kB
  • sloc: cpp: 10,895; python: 133; makefile: 39; ruby: 28; sh: 11
file content (349 lines) | stat: -rw-r--r-- 8,377 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* ncmpc (Ncurses MPD Client)
 * (c) 2004-2018 The Music Player Daemon Project
 * Project homepage: http://musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "Options.hxx"
#include "Bindings.hxx"
#include "GlobalBindings.hxx"
#include "config.h"
#include "charset.hxx"
#include "conf.hxx"
#include "i18n.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define ERROR_UNKNOWN_OPTION    0x01
#define ERROR_BAD_ARGUMENT      0x02
#define ERROR_GOT_ARGUMENT      0x03
#define ERROR_MISSING_ARGUMENT  0x04

struct OptionDefinition {
	int shortopt;
	const char *longopt;
	const char *argument;
	const char *descrition;
};

Options options;

static constexpr OptionDefinition option_table[] = {
	{ '?', "help", nullptr, "Show this help message" },
	{ 'V', "version", nullptr, "Display version information" },
	{ 'c', "colors", nullptr, "Enable colors" },
	{ 'C', "no-colors", nullptr, "Disable colors" },
#ifdef HAVE_GETMOUSE
	{ 'm', "mouse", nullptr, "Enable mouse" },
	{ 'M', "no-mouse", nullptr, "Disable mouse" },
#endif
	{ 'e', "exit", nullptr, "Exit on connection errors" },
	{ 'p', "port", "PORT", "Connect to server on port" },
	{ 'h', "host", "HOST", "Connect to server on host" },
	{ 'P', "password","PASSWORD", "Connect with password" },
	{ 'f', "config", "FILE", "Read configuration from file" },
	{ 'k', "key-file","FILE", "Read key bindings from file" },
#ifndef NDEBUG
	{ 'K', "dump-keys", nullptr, "Dump key bindings to stdout" },
#endif
};

gcc_pure
static const OptionDefinition *
FindOption(int s) noexcept
{
	for (const auto &i : option_table)
		if (s == i.shortopt)
			return &i;

	return nullptr;
}

static const OptionDefinition *
FindOption(const char *l) noexcept
{
	for (const auto &i : option_table)
		if (strcmp(l, i.longopt) == 0)
			return &i;

	return nullptr;
}

[[noreturn]]
static void
option_error(int error, const char *option, const char *arg)
{
	switch (error) {
	case ERROR_UNKNOWN_OPTION:
		fprintf(stderr, PACKAGE ": invalid option %s\n", option);
		break;
	case ERROR_BAD_ARGUMENT:
		fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
		break;
	case ERROR_GOT_ARGUMENT:
		fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
		break;
	case ERROR_MISSING_ARGUMENT:
		fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
		break;
	default:
		fprintf(stderr, PACKAGE ": internal error %d\n", error);
		break;
	}

	exit(EXIT_FAILURE);
}

static void
display_help()
{
	printf("Usage: %s [OPTION]...\n", PACKAGE);

	for (const auto &i : option_table) {
		char tmp[32];

		if (i.argument)
			snprintf(tmp, sizeof(tmp), "%s=%s",
				 i.longopt,
				 i.argument);
		else
			snprintf(tmp, sizeof(tmp), "%s",
				 i.longopt);

		printf("  -%c, --%-20s %s\n",
		       i.shortopt,
		       tmp,
		       i.descrition);
	}
}

static void
handle_option(int c, const char *arg)
{
	switch (c) {
	case '?': /* --help */
		display_help();
		exit(EXIT_SUCCESS);
	case 'V': /* --version */
		puts(PACKAGE " version: " VERSION "\n"
		     "build options:"
#ifdef NCMPC_MINI
		     " mini"
#endif
#ifndef NDEBUG
		     " debug"
#endif
#ifdef ENABLE_MULTIBYTE
		     " multibyte"
#endif
#ifdef HAVE_CURSES_ENHANCED
		     " wide"
#endif
#ifdef ENABLE_LOCALE
		     " locale"
#endif
#ifdef HAVE_ICONV
		     " iconv"
#endif
#ifdef ENABLE_NLS
		     " nls"
#endif
#ifdef ENABLE_COLORS
		     " colors"
#else
		     " no-colors"
#endif
#ifdef ENABLE_LIRC
		     " lirc"
#endif
#ifdef HAVE_GETMOUSE
		     " getmouse"
#endif
#ifdef ENABLE_LIBRARY_PAGE
		     " artist-screen"
#endif
#ifdef ENABLE_HELP_SCREEN
		     " help-screen"
#endif
#ifdef ENABLE_SEARCH_SCREEN
		     " search-screen"
#endif
#ifdef ENABLE_SONG_SCREEN
		     " song-screen"
#endif
#ifdef ENABLE_KEYDEF_SCREEN
		     " key-screen"
#endif
#ifdef ENABLE_LYRICS_SCREEN
		     " lyrics-screen"
#endif
#ifdef ENABLE_OUTPUTS_SCREEN
		     " outputs-screen"
#endif
#ifdef ENABLE_CHAT_SCREEN
		     " chat-screen"
#endif

		     "\n");
#ifndef NCMPC_MINI
		printf("configuration files:\n"
		       " %s\n"
#ifndef _WIN32
		       " %s\n"
#endif
		       " %s\n\n",
		       GetUserConfigPath().c_str(),
#ifndef _WIN32
		       GetHomeConfigPath().c_str(),
#endif
		       GetSystemConfigPath().c_str());

		if (strcmp("translator-credits", _("translator-credits")) != 0)
			/* To translators: these credits are shown
			   when ncmpc is started with "--version" */
			printf("\n%s\n", _("translator-credits"));
#endif
		exit(EXIT_SUCCESS);
	case 'c': /* --colors */
#ifdef ENABLE_COLORS
		options.enable_colors = true;
#endif
		break;
	case 'C': /* --no-colors */
#ifdef ENABLE_COLORS
		options.enable_colors = false;
#endif
		break;
	case 'm': /* --mouse */
#ifdef HAVE_GETMOUSE
		options.enable_mouse = true;
#endif
		break;
	case 'M': /* --no-mouse */
#ifdef HAVE_GETMOUSE
		options.enable_mouse = false;
#endif
		break;
	case 'e': /* --exit */
		/* deprecated */
		break;
	case 'p': /* --port */
		options.port = atoi(arg);
		break;
	case 'h': /* --host */
		options.host = arg;
		break;
	case 'P': /* --password */
		options.password = LocaleToUtf8(arg).c_str();
		break;
	case 'f': /* --config */
		options.config_file = arg;
		break;
	case 'k': /* --key-file */
		options.key_file = arg;
		break;
#if !defined(NDEBUG) && !defined(NCMPC_MINI)
	case 'K': /* --dump-keys */
		read_configuration();
		GetGlobalKeyBindings().WriteToFile(stdout,
						   KEYDEF_WRITE_ALL | KEYDEF_COMMENT_ALL);
		exit(EXIT_SUCCESS);
		break;
#endif
	default:
		fprintf(stderr,"Unknown Option %c = %s\n", c, arg);
		break;
	}
}

void
options_parse(int argc, const char *argv[])
{
	const OptionDefinition *opt = nullptr;

	for (int i = 1; i < argc; i++) {
		const char *arg = argv[i];
		size_t len = strlen(arg);

		/* check for a long option */
		if (strncmp(arg, "--", 2) == 0) {
			/* make sure we got an argument for the previous option */
			if( opt && opt->argument )
				option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);

			/* retrieve a option argument */
			std::string name;
			const char *value = strrchr(arg + 2, '=');
			if (value != nullptr) {
				name.assign(arg, value);
				value++;
			} else
				name = arg;

			/* check if the option exists */
			if ((opt = FindOption(name.c_str() + 2)) == nullptr)
				option_error(ERROR_UNKNOWN_OPTION, name.c_str(), nullptr);

			/* abort if we got an argument to the option and don't want one */
			if( value && opt->argument==nullptr )
				option_error(ERROR_GOT_ARGUMENT, arg, value);

			/* execute option callback */
			if (value || opt->argument==nullptr) {
				handle_option(opt->shortopt, value);
				opt = nullptr;
			}
		}
		/* check for short options */
		else if (len >= 2 && *arg == '-') {
			size_t j;

			for(j=1; j<len; j++) {
				/* make sure we got an argument for the previous option */
				if (opt && opt->argument)
					option_error(ERROR_MISSING_ARGUMENT,
						     opt->longopt, opt->argument);

				/* check if the option exists */
				if ((opt = FindOption(arg[j])) == nullptr)
					option_error(ERROR_UNKNOWN_OPTION, arg, nullptr);

				/* if no option argument is needed execute callback */
				if (opt->argument == nullptr) {
					handle_option(opt->shortopt, nullptr);
					opt = nullptr;
				}
			}
		} else {
			/* is this a option argument? */
			if (opt && opt->argument) {
				handle_option (opt->shortopt, arg);
				opt = nullptr;
			} else
				option_error(ERROR_BAD_ARGUMENT, arg, nullptr);
		}
	}

	if (opt && opt->argument == nullptr)
		handle_option(opt->shortopt, nullptr);
	else if (opt && opt->argument)
		option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);

	if (options.host.empty() && getenv("MPD_HOST"))
		options.host = getenv("MPD_HOST");
}