Code

652628c3c3bbe616a07de240a7798db0ff1c8d48
[ncmpc.git] / src / conf.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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.
18  */
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)
89 {
90         return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
91                 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
92 }
94 static void
95 print_error(const char *msg, const char *input)
96 {
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;
109                 }
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;
118                 }
120                 return (int)value;
121         }
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;
142         }
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;
155         }
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++;
169         }
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;
191         }
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;
204         }
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);
222 /**
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.
226  */
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;
255         }
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;
265                 }
267                 rgb[i] = strtol(value, &endptr, 0);
268                 if (endptr == value || *endptr != 0) {
269                         print_error(_("Invalid number"), value);
270                         return -1;
271                 }
273                 value = next;
274         }
276         if (*value != 0) {
277                 print_error(_("Malformed color definition"), str);
278                 return -1;
279         }
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++;
293         }
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;
317                         }
318                 }
319                 i++;
320         }
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')
334         {
335                 if (0 <= mode && mode <= 4)
336                         return mode;
337                 else
338                 {
339                         print_error(_("Invalid search mode"),value);
340                         return 0;
341                 }
342         }
343         else
344         {
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
361                 {
362                         print_error(_("Unknown search mode"),value);
363                         return 0;
364                 }
365         }
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
399         {}
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
411         {}
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
432         {}
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
470         {}
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
496         {}
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 #ifdef NCMPC_MINI
505                 {}
506 #else
507                 options.display_time = str2bool(value);
508 #endif
509         else if (!strcasecmp(CONF_JUMP_PREFIX_ONLY, name))
510 #ifdef NCMPC_MINI
511                 {}
512 #else
513                 options.jump_prefix_only = str2bool(value);
514 #endif
515         else if (!strcasecmp(CONF_LYRICS_AUTOSAVE, name))
516 #ifdef ENABLE_LYRICS_SCREEN
517                 options.lyrics_autosave = str2bool(value);
518 #else
519         {}
520 #endif
521         else if (!strcasecmp(CONF_LYRICS_SHOW_PLUGIN, name))
522 #ifdef ENABLE_LYRICS_SCREEN
523                 options.lyrics_show_plugin = str2bool(value);
524 #else
525                 {}
526 #endif
527         else if (!strcasecmp(name, CONF_TEXT_EDITOR))
528 #ifdef ENABLE_LYRICS_SCREEN
529                 {
530                         g_free(options.text_editor);
531                         options.text_editor = get_format(value);
532                 }
533 #else
534                 {}
535 #endif
536         else if (!strcasecmp(name, CONF_TEXT_EDITOR_ASK))
537 #ifdef ENABLE_LYRICS_SCREEN
538                 options.text_editor_ask = str2bool(value);
539 #else
540                 {}
541 #endif
542         else if (!strcasecmp(name, CONF_CHAT_PREFIX))
543 #ifdef ENABLE_CHAT_SCREEN
544                 options.chat_prefix = get_format(value);
545 #else
546                 {}
547 #endif
548         else if (!strcasecmp(CONF_SECOND_COLUMN, name))
549 #ifdef NCMPC_MINI
550                 {}
551 #else
552                 options.second_column = str2bool(value);
553 #endif
554         else {
555                 print_error(_("Unknown configuration parameter"), name);
556                 return false;
557         }
559         return true;
562 static int
563 read_rc_file(char *filename)
565         assert(filename != NULL);
567         FILE *file = fopen(filename, "r");
568         if (file == NULL) {
569                 perror(filename);
570                 return -1;
571         }
573         char line[MAX_LINE_LENGTH];
574         while (fgets(line, sizeof(line), file) != NULL) {
575                 char *p = g_strchug(line);
577                 if (*p != 0 && *p != COMMENT_TOKEN)
578                         parse_line(g_strchomp(p));
579         }
581         fclose(file);
582         return 0;
585 bool
586 check_user_conf_dir(void)
588         char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
590         if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
591                 g_free(directory);
592                 return true;
593         }
595         bool success = g_mkdir(directory, 0755) == 0;
596         g_free(directory);
597         return success;
600 char *
601 build_user_conf_filename(void)
603 #ifdef WIN32
604         return g_build_filename(g_get_user_config_dir(), PACKAGE, "ncmpc.conf", NULL);
605 #else
606         return g_build_filename(g_get_home_dir(), "." PACKAGE, "config", NULL);
607 #endif
610 char *
611 build_system_conf_filename(void)
613 #ifdef WIN32
614         const gchar* const *system_data_dirs;
615         gchar *pathname = NULL;
617         for (system_data_dirs = g_get_system_config_dirs (); *system_data_dirs != NULL; system_data_dirs++)
618         {
619                 pathname = g_build_filename(*system_data_dirs, PACKAGE, "ncmpc.conf", NULL);
620                 if (g_file_test(pathname, G_FILE_TEST_EXISTS))
621                 {
622                         break;
623                 }
624                 else
625                 {
626                         g_free (pathname);
627                         pathname = NULL;
628                 }
629         }
630         return pathname;
631 #else
632         return g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
633 #endif
636 char *
637 build_user_key_binding_filename(void)
639 #ifdef WIN32
640         return g_build_filename(g_get_user_config_dir(), PACKAGE, "keys.conf", NULL);
641 #else
642         return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
643 #endif
646 static char *
647 g_build_system_key_binding_filename(void)
649 #ifdef WIN32
650         const gchar* const *system_data_dirs;
651         gchar *pathname = NULL;
653         for (system_data_dirs = g_get_system_config_dirs (); *system_data_dirs != NULL; system_data_dirs++)
654         {
655                 pathname = g_build_filename(*system_data_dirs, PACKAGE, "keys.conf", NULL);
656                 if (g_file_test(pathname, G_FILE_TEST_EXISTS))
657                 {
658                         break;
659                 }
660                 else
661                 {
662                         g_free (pathname);
663                         pathname = NULL;
664                 }
665         }
666         return pathname;
667 #else
668         return g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
669 #endif
672 static char *
673 find_config_file(void)
675         /* check for command line configuration file */
676         if (options.config_file != NULL)
677                 return g_strdup(options.config_file);
679         /* check for user configuration ~/.ncmpc/config */
680         char *filename = build_user_conf_filename();
681         if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
682                 return filename;
684         g_free(filename);
686         /* check for  global configuration SYSCONFDIR/ncmpc/config */
687         filename = build_system_conf_filename();
688         if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
689                 return filename;
691         g_free(filename);
692         return NULL;
695 static char *
696 find_keys_file(void)
698         /* check for command line key binding file */
699         if (options.key_file != NULL)
700                 return g_strdup(options.key_file);
702         /* check for  user key bindings ~/.ncmpc/keys */
703         char *filename = build_user_key_binding_filename();
704         if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
705                 return filename;
707         g_free(filename);
709         /* check for  global key bindings SYSCONFDIR/ncmpc/keys */
710         filename = g_build_system_key_binding_filename();
711         if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
712                 return filename;
714         g_free(filename);
715         return NULL;
718 void
719 read_configuration(void)
721         /* load configuration */
722         char *filename = find_config_file();
723         if (filename != NULL) {
724                 read_rc_file(filename);
725                 g_free(filename);
726         }
728         /* load key bindings */
729         filename = find_keys_file();
730         if (filename != NULL) {
731                 read_rc_file(filename);
732                 g_free(filename);
733         }