main: check for strfsong() failure, add fallback
[ncmpc.git] / src / conf.c
blob814a13b774486e2e204ee1d8bc61aba2ea12f8c6
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "conf.h"
21 #include "config.h"
22 #include "defaults.h"
23 #include "i18n.h"
24 #include "command.h"
25 #include "colors.h"
26 #include "screen_list.h"
27 #include "options.h"
29 #include <assert.h>
30 #include <ctype.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <glib.h>
36 #include <glib/gstdio.h>
38 #define MAX_LINE_LENGTH 1024
39 #define COMMENT_TOKEN '#'
41 /* configuration field names */
42 #define CONF_ENABLE_COLORS "enable-colors"
43 #define CONF_SCROLL_OFFSET "scroll-offset"
44 #define CONF_AUTO_CENTER "auto-center"
45 #define CONF_WIDE_CURSOR "wide-cursor"
46 #define CONF_KEY_DEFINITION "key"
47 #define CONF_COLOR "color"
48 #define CONF_COLOR_DEFINITION "colordef"
49 #define CONF_LIST_FORMAT "list-format"
50 #define CONF_SEARCH_FORMAT "search-format"
51 #define CONF_STATUS_FORMAT "status-format"
52 #define CONF_XTERM_TITLE_FORMAT "xterm-title-format"
53 #define CONF_LIST_WRAP "wrap-around"
54 #define CONF_FIND_WRAP "find-wrap"
55 #define CONF_FIND_SHOW_LAST "find-show-last"
56 #define CONF_AUDIBLE_BELL "audible-bell"
57 #define CONF_VISIBLE_BELL "visible-bell"
58 #define CONF_BELL_ON_WRAP "bell-on-wrap"
59 #define CONF_STATUS_MESSAGE_TIME "status-message-time"
60 #define CONF_XTERM_TITLE "set-xterm-title"
61 #define CONF_ENABLE_MOUSE "enable-mouse"
62 #define CONF_CROSSFADE_TIME "crossfade-time"
63 #define CONF_SEARCH_MODE "search-mode"
64 #define CONF_HIDE_CURSOR "hide-cursor"
65 #define CONF_SEEK_TIME "seek-time"
66 #define CONF_SCREEN_LIST "screen-list"
67 #define CONF_TIMEDISPLAY_TYPE "timedisplay-type"
68 #define CONF_HOST "host"
69 #define CONF_PORT "port"
70 #define CONF_PASSWORD "password"
71 #define CONF_TIMEOUT "timeout"
72 #define CONF_LYRICS_TIMEOUT "lyrics-timeout"
73 #define CONF_SCROLL "scroll"
74 #define CONF_SCROLL_SEP "scroll-sep"
75 #define CONF_VISIBLE_BITRATE "visible-bitrate"
76 #define CONF_HARDWARE_CURSOR "hardware-cursor"
77 #define CONF_WELCOME_SCREEN_LIST "welcome-screen-list"
78 #define CONF_DISPLAY_TIME "display-time"
79 #define CONF_JUMP_PREFIX_ONLY "jump-prefix-only"
80 #define CONF_LYRICS_AUTOSAVE "lyrics-autosave"
81 #define CONF_LYRICS_SHOW_PLUGIN "lyrics-show-plugin"
82 #define CONF_TEXT_EDITOR "text-editor"
83 #define CONF_TEXT_EDITOR_ASK "text-editor-ask"
84 #define CONF_CHAT_PREFIX "chat-prefix"
85 #define CONF_SECOND_COLUMN "second-column"
87 static bool
88 str2bool(char *str)
90 return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
91 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
94 static void
95 print_error(const char *msg, const char *input)
97 fprintf(stderr, "%s: %s ('%s')\n",
98 /* To translators: prefix for error messages */
99 _("Error"), msg, input);
102 static int
103 parse_key_value(char *str, char **end)
105 if (*str == '\'') {
106 if (str[1] == '\'' || str[2] != '\'') {
107 print_error(_("Malformed hotkey definition"), str);
108 return -1;
111 *end = str + 3;
112 return str[1];
113 } else {
114 long value = strtol(str, end, 0);
115 if (*end == str) {
116 print_error(_("Malformed hotkey definition"), str);
117 return -1;
120 return (int)value;
124 static bool
125 parse_key_definition(char *str)
127 /* get the command name */
128 const size_t len = strlen(str);
129 size_t i = 0;
130 int j = 0;
131 char buf[MAX_LINE_LENGTH];
132 memset(buf, 0, MAX_LINE_LENGTH);
133 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
134 buf[j++] = str[i++];
136 command_t cmd = get_key_command_from_name(buf);
137 if(cmd == CMD_NONE) {
138 /* the hotkey configuration contains an unknown
139 command */
140 print_error(_("Unknown command"), buf);
141 return false;
144 /* skip whitespace */
145 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
146 i++;
148 /* get the value part */
149 memset(buf, 0, MAX_LINE_LENGTH);
150 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
151 if (*buf == 0) {
152 /* the hotkey configuration line is incomplete */
153 print_error(_("Incomplete hotkey configuration"), str);
154 return false;
157 /* parse key values */
158 i = 0;
159 int key = 0;
160 char *p = buf;
162 int keys[MAX_COMMAND_KEYS];
163 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
164 while (i < MAX_COMMAND_KEYS && *p != 0 &&
165 (key = parse_key_value(p, &p)) >= 0) {
166 keys[i++] = key;
167 while (*p==',' || *p==' ' || *p=='\t')
168 p++;
171 if (key < 0)
172 return false;
174 return assign_keys(cmd, keys);
177 static bool
178 parse_timedisplay_type(const char *str)
180 if (strcmp(str, "elapsed") == 0)
181 return false;
182 else if (strcmp(str, "remaining") == 0)
183 return true;
184 else {
185 /* translators: ncmpc supports displaying the
186 "elapsed" or "remaining" time of a song being
187 played; in this case, the configuration file
188 contained an invalid setting */
189 print_error(_("Bad time display type"), str);
190 return false;
194 #ifdef ENABLE_COLORS
195 static char *
196 separate_value(char *p)
198 char *value = strchr(p, '=');
199 if (value == NULL) {
200 /* an equals sign '=' was expected while parsing a
201 configuration file line */
202 fprintf(stderr, "%s\n", _("Missing '='"));
203 return NULL;
206 *value++ = 0;
208 g_strchomp(p);
209 return g_strchug(value);
212 static int
213 parse_color(char *str)
215 char *value = separate_value(str);
216 if (value == NULL)
217 return -1;
219 return colors_assign(str, value);
223 * Returns the first non-whitespace character after the next comma
224 * character, or the end of the string. This is used to parse comma
225 * separated values.
227 static char *
228 after_comma(char *p)
230 char *comma = strchr(p, ',');
232 if (comma != NULL) {
233 *comma++ = 0;
234 comma = g_strchug(comma);
235 } else
236 comma = p + strlen(p);
238 g_strchomp(p);
239 return comma;
242 static int
243 parse_color_definition(char *str)
245 char *value = separate_value(str);
246 if (value == NULL)
247 return -1;
249 /* get the command name */
250 short color = colors_str2color(str);
251 if (color < 0) {
252 char buf[MAX_LINE_LENGTH];
253 print_error(_("Bad color name"), buf);
254 return -1;
257 /* parse r,g,b values */
259 short rgb[3];
260 for (unsigned i = 0; i < 3; ++i) {
261 char *next = after_comma(value), *endptr;
262 if (*value == 0) {
263 print_error(_("Incomplete color definition"), str);
264 return -1;
267 rgb[i] = strtol(value, &endptr, 0);
268 if (endptr == value || *endptr != 0) {
269 print_error(_("Invalid number"), value);
270 return -1;
273 value = next;
276 if (*value != 0) {
277 print_error(_("Malformed color definition"), str);
278 return -1;
281 return colors_define(str, rgb[0], rgb[1], rgb[2]);
283 #endif
285 static char *
286 get_format(char *str)
288 gsize len = strlen(str);
290 if (str && str[0]=='\"' && str[len-1] == '\"') {
291 str[len - 1] = '\0';
292 str++;
295 return g_strdup(str);
298 static char **
299 check_screen_list(char *value)
301 char **tmp = g_strsplit_set(value, " \t,", 100);
302 char **screen = NULL;
303 int i = 0, j = 0;
305 while( tmp && tmp[i] ) {
306 char *name = g_ascii_strdown(tmp[i], -1);
307 if (*name != '\0') {
308 if (screen_lookup_name(name) == NULL) {
309 /* an unknown screen name was specified in the
310 configuration file */
311 print_error(_("Unknown screen name"), name);
312 free(name);
313 } else {
314 screen = g_realloc(screen, (j+2)*sizeof(char *));
315 screen[j++] = name;
316 screen[j] = NULL;
319 i++;
321 g_strfreev(tmp);
322 if( screen == NULL )
323 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
325 return screen;
328 static int
329 get_search_mode(char *value)
331 char * test;
332 const int mode = strtol(value, &test, 10);
333 if (*test == '\0')
335 if (0 <= mode && mode <= 4)
336 return mode;
337 else
339 print_error(_("Invalid search mode"),value);
340 return 0;
343 else
345 for (int i = 0; value[i] != '\0'; i++)
346 value[i] = tolower(value[i]);
348 // TODO: modify screen_search so that its own list of modes can be used
349 // for comparison instead of specifying them here
350 if (strcmp(value, "title") == 0)
351 return 0;
352 else if (strcmp(value, "artist") == 0)
353 return 1;
354 else if (strcmp(value, "album") == 0)
355 return 2;
356 else if (strcmp(value, "filename") == 0)
357 return 3;
358 else if (strcmp(value, "artist+album") == 0)
359 return 4;
360 else
362 print_error(_("Unknown search mode"),value);
363 return 0;
368 static bool
369 parse_line(char *line)
371 size_t len = strlen(line), i = 0, j = 0;
373 /* get the name part */
374 char name[MAX_LINE_LENGTH];
375 while (i < len && line[i] != '=' && !g_ascii_isspace(line[i]))
376 name[j++] = line[i++];
378 name[j] = '\0';
380 /* skip '=' and whitespace */
381 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
382 i++;
384 /* get the value part */
385 char value[MAX_LINE_LENGTH];
386 j = 0;
387 while (i < len)
388 value[j++] = line[i++];
389 value[j] = '\0';
391 /* key definition */
392 if (!strcasecmp(CONF_KEY_DEFINITION, name))
393 parse_key_definition(value);
394 /* enable colors */
395 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
396 #ifdef ENABLE_COLORS
397 options.enable_colors = str2bool(value);
398 #else
400 #endif
401 else if (!strcasecmp(CONF_SCROLL_OFFSET, name))
402 options.scroll_offset = atoi(value);
403 /* auto center */
404 else if (!strcasecmp(CONF_AUTO_CENTER, name))
405 options.auto_center = str2bool(value);
406 /* color assignment */
407 else if (!strcasecmp(CONF_COLOR, name))
408 #ifdef ENABLE_COLORS
409 parse_color(value);
410 #else
412 #endif
413 /* wide cursor */
414 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
415 options.wide_cursor = str2bool(value);
416 else if (strcasecmp(name, CONF_HARDWARE_CURSOR) == 0)
417 options.hardware_cursor = str2bool(value);
418 /* welcome screen list */
419 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
420 options.welcome_screen_list = str2bool(value);
421 /* visible bitrate */
422 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
423 options.visible_bitrate = str2bool(value);
424 /* timer display type */
425 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name))
426 options.display_remaining_time = parse_timedisplay_type(value);
427 /* color definition */
428 else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
429 #ifdef ENABLE_COLORS
430 parse_color_definition(value);
431 #else
433 #endif
434 /* list format string */
435 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
436 g_free(options.list_format);
437 options.list_format = get_format(value);
438 /* search format string */
439 } else if (!strcasecmp(CONF_SEARCH_FORMAT, name)) {
440 g_free(options.search_format);
441 options.search_format = get_format(value);
442 /* status format string */
443 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
444 g_free(options.status_format);
445 options.status_format = get_format(value);
446 /* xterm title format string */
447 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
448 g_free(options.xterm_title_format);
449 options.xterm_title_format = get_format(value);
450 } else if (!strcasecmp(CONF_LIST_WRAP, name))
451 options.list_wrap = str2bool(value);
452 else if (!strcasecmp(CONF_FIND_WRAP, name))
453 options.find_wrap = str2bool(value);
454 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
455 options.find_show_last_pattern = str2bool(value);
456 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
457 options.audible_bell = str2bool(value);
458 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
459 options.visible_bell = str2bool(value);
460 else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
461 options.bell_on_wrap = str2bool(value);
462 else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
463 options.status_message_time = atoi(value);
464 else if (!strcasecmp(CONF_XTERM_TITLE, name))
465 options.enable_xterm_title = str2bool(value);
466 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
467 #ifdef HAVE_GETMOUSE
468 options.enable_mouse = str2bool(value);
469 #else
471 #endif
472 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
473 options.crossfade_time = atoi(value);
474 else if (!strcasecmp(CONF_SEARCH_MODE, name))
475 options.search_mode = get_search_mode(value);
476 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
477 options.hide_cursor = atoi(value);
478 else if (!strcasecmp(CONF_SEEK_TIME, name))
479 options.seek_time = atoi(value);
480 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
481 g_strfreev(options.screen_list);
482 options.screen_list = check_screen_list(value);
483 } else if (!strcasecmp(CONF_HOST, name))
484 options.host = get_format(value);
485 else if (!strcasecmp(CONF_PORT, name))
486 options.port = atoi(get_format(value));
487 else if (!strcasecmp(CONF_PASSWORD, name))
488 options.password = get_format(value);
489 else if (!strcasecmp(CONF_TIMEOUT, name))
490 options.timeout_ms = atoi(get_format(value))
491 * 1000 /* seconds -> milliseconds */;
492 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
493 #ifdef ENABLE_LYRICS_SCREEN
494 options.lyrics_timeout = atoi(get_format(value));
495 #else
497 #endif
498 else if (!strcasecmp(CONF_SCROLL, name))
499 options.scroll = str2bool(value);
500 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
501 g_free(options.scroll_sep);
502 options.scroll_sep = get_format(value);
503 } else if (!strcasecmp(CONF_DISPLAY_TIME, name))
504 /* obsolete, ignore */
506 else if (!strcasecmp(CONF_JUMP_PREFIX_ONLY, name))
507 #ifdef NCMPC_MINI
509 #else
510 options.jump_prefix_only = str2bool(value);
511 #endif
512 else if (!strcasecmp(CONF_LYRICS_AUTOSAVE, name))
513 #ifdef ENABLE_LYRICS_SCREEN
514 options.lyrics_autosave = str2bool(value);
515 #else
517 #endif
518 else if (!strcasecmp(CONF_LYRICS_SHOW_PLUGIN, name))
519 #ifdef ENABLE_LYRICS_SCREEN
520 options.lyrics_show_plugin = str2bool(value);
521 #else
523 #endif
524 else if (!strcasecmp(name, CONF_TEXT_EDITOR))
525 #ifdef ENABLE_LYRICS_SCREEN
527 g_free(options.text_editor);
528 options.text_editor = get_format(value);
530 #else
532 #endif
533 else if (!strcasecmp(name, CONF_TEXT_EDITOR_ASK))
534 #ifdef ENABLE_LYRICS_SCREEN
535 options.text_editor_ask = str2bool(value);
536 #else
538 #endif
539 else if (!strcasecmp(name, CONF_CHAT_PREFIX))
540 #ifdef ENABLE_CHAT_SCREEN
541 options.chat_prefix = get_format(value);
542 #else
544 #endif
545 else if (!strcasecmp(CONF_SECOND_COLUMN, name))
546 #ifdef NCMPC_MINI
548 #else
549 options.second_column = str2bool(value);
550 #endif
551 else {
552 print_error(_("Unknown configuration parameter"), name);
553 return false;
556 return true;
559 static int
560 read_rc_file(char *filename)
562 assert(filename != NULL);
564 FILE *file = fopen(filename, "r");
565 if (file == NULL) {
566 perror(filename);
567 return -1;
570 char line[MAX_LINE_LENGTH];
571 while (fgets(line, sizeof(line), file) != NULL) {
572 char *p = g_strchug(line);
574 if (*p != 0 && *p != COMMENT_TOKEN)
575 parse_line(g_strchomp(p));
578 fclose(file);
579 return 0;
582 bool
583 check_user_conf_dir(void)
585 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
587 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
588 g_free(directory);
589 return true;
592 bool success = g_mkdir(directory, 0755) == 0;
593 g_free(directory);
594 return success;
597 char *
598 build_user_conf_filename(void)
600 #ifdef WIN32
601 return g_build_filename(g_get_user_config_dir(), PACKAGE, "ncmpc.conf", NULL);
602 #else
603 return g_build_filename(g_get_home_dir(), "." PACKAGE, "config", NULL);
604 #endif
607 char *
608 build_system_conf_filename(void)
610 #ifdef WIN32
611 const gchar* const *system_data_dirs;
612 gchar *pathname = NULL;
614 for (system_data_dirs = g_get_system_config_dirs (); *system_data_dirs != NULL; system_data_dirs++)
616 pathname = g_build_filename(*system_data_dirs, PACKAGE, "ncmpc.conf", NULL);
617 if (g_file_test(pathname, G_FILE_TEST_EXISTS))
619 break;
621 else
623 g_free (pathname);
624 pathname = NULL;
627 return pathname;
628 #else
629 return g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
630 #endif
633 char *
634 build_user_key_binding_filename(void)
636 #ifdef WIN32
637 return g_build_filename(g_get_user_config_dir(), PACKAGE, "keys.conf", NULL);
638 #else
639 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
640 #endif
643 static char *
644 g_build_system_key_binding_filename(void)
646 #ifdef WIN32
647 const gchar* const *system_data_dirs;
648 gchar *pathname = NULL;
650 for (system_data_dirs = g_get_system_config_dirs (); *system_data_dirs != NULL; system_data_dirs++)
652 pathname = g_build_filename(*system_data_dirs, PACKAGE, "keys.conf", NULL);
653 if (g_file_test(pathname, G_FILE_TEST_EXISTS))
655 break;
657 else
659 g_free (pathname);
660 pathname = NULL;
663 return pathname;
664 #else
665 return g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
666 #endif
669 static char *
670 find_config_file(void)
672 /* check for command line configuration file */
673 if (options.config_file != NULL)
674 return g_strdup(options.config_file);
676 /* check for user configuration ~/.ncmpc/config */
677 char *filename = build_user_conf_filename();
678 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
679 return filename;
681 g_free(filename);
683 /* check for global configuration SYSCONFDIR/ncmpc/config */
684 filename = build_system_conf_filename();
685 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
686 return filename;
688 g_free(filename);
689 return NULL;
692 static char *
693 find_keys_file(void)
695 /* check for command line key binding file */
696 if (options.key_file != NULL)
697 return g_strdup(options.key_file);
699 /* check for user key bindings ~/.ncmpc/keys */
700 char *filename = build_user_key_binding_filename();
701 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
702 return filename;
704 g_free(filename);
706 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
707 filename = g_build_system_key_binding_filename();
708 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
709 return filename;
711 g_free(filename);
712 return NULL;
715 void
716 read_configuration(void)
718 /* load configuration */
719 char *filename = find_config_file();
720 if (filename != NULL) {
721 read_rc_file(filename);
722 g_free(filename);
725 /* load key bindings */
726 filename = find_keys_file();
727 if (filename != NULL) {
728 read_rc_file(filename);
729 g_free(filename);