Code

3ec37b855776cde142e8eae35e854daa45bb835a
[ncmpc.git] / src / conf.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 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.
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.
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"
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <glib.h>
39 #define MAX_LINE_LENGTH 1024
40 #define COMMENT_TOKEN '#'
42 /* configuration field names */
43 #define CONF_ENABLE_COLORS "enable-colors"
44 #define CONF_SCROLL_OFFSET "scroll-offset"
45 #define CONF_AUTO_CENTER "auto-center"
46 #define CONF_WIDE_CURSOR "wide-cursor"
47 #define CONF_KEY_DEFINITION "key"
48 #define CONF_COLOR "color"
49 #define CONF_COLOR_DEFINITION "colordef"
50 #define CONF_LIST_FORMAT "list-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_LYRICS_TIMEOUT "lyrics-timeout"
72 #define CONF_SHOW_SPLASH "show-splash"
73 #define CONF_SCROLL "scroll"
74 #define CONF_SCROLL_SEP "scroll-sep"
75 #define CONF_VISIBLE_BITRATE "visible-bitrate"
76 #define CONF_WELCOME_SCREEN_LIST "welcome-screen-list"
77 #define CONF_DISPLAY_TIME "display-time"
78 #define CONF_JUMP_PREFIX_ONLY "jump-prefix-only"
79 #define CONF_LYRICS_AUTOSAVE "lyrics-autosave"
81 static bool
82 str2bool(char *str)
83 {
84         return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
85                 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
86 }
88 static void
89 print_error(const char *msg, const char *input)
90 {
91         fprintf(stderr, "%s: %s ('%s')\n",
92                 /* To translators: prefix for error messages */
93                 _("Error"), msg, input);
94 }
96 static int
97 parse_key_value(char *str, char **end)
98 {
99         if (*str == '\'') {
100                 if (str[1] == '\'' || str[2] != '\'') {
101                         print_error(_("Malformed hotkey definition"), str);
102                         return -1;
103                 }
105                 *end = str + 3;
106                 return str[1];
107         } else {
108                 long value = strtol(str, end, 0);
109                 if (*end == str) {
110                         print_error(_("Malformed hotkey definition"), str);
111                         return -1;
112                 }
114                 return (int)value;
115         }
118 static int
119 parse_key_definition(char *str)
121         char buf[MAX_LINE_LENGTH];
122         char *p;
123         size_t len = strlen(str), i;
124         int j,key;
125         int keys[MAX_COMMAND_KEYS];
126         command_t cmd;
128         /* get the command name */
129         i=0;
130         j=0;
131         memset(buf, 0, MAX_LINE_LENGTH);
132         while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
133                 buf[j++] = str[i++];
134         if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
135                 /* the hotkey configuration contains an unknown
136                    command */
137                 print_error(_("Unknown command"), buf);
138                 return -1;
139         }
141         /* skip whitespace */
142         while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
143                 i++;
145         /* get the value part */
146         memset(buf, 0, MAX_LINE_LENGTH);
147         g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
148         if (*buf == 0) {
149                 /* the hotkey configuration line is incomplete */
150                 print_error(_("Incomplete hotkey configuration"), str);
151                 return -1;
152         }
154         /* parse key values */
155         i = 0;
156         key = 0;
157         p = buf;
158         memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
159         while (i < MAX_COMMAND_KEYS && *p != 0 &&
160                (key = parse_key_value(p, &p)) >= 0) {
161                 keys[i++] = key;
162                 while (*p==',' || *p==' ' || *p=='\t')
163                         p++;
164         }
166         if (key < 0)
167                 return -1;
169         return assign_keys(cmd, keys);
172 static bool
173 parse_timedisplay_type(const char *str)
175         if (strcmp(str, "elapsed") == 0)
176                 return false;
177         else if (strcmp(str, "remaining") == 0)
178                 return true;
179         else {
180                 /* translators: ncmpc supports displaying the
181                    "elapsed" or "remaining" time of a song being
182                    played; in this case, the configuration file
183                    contained an invalid setting */
184                 print_error(_("Bad time display type"), str);
185                 return false;
186         }
189 #ifdef ENABLE_COLORS
190 static char *
191 separate_value(char *p)
193         char *value;
195         value = strchr(p, '=');
196         if (value == NULL) {
197                 /* an equals sign '=' was expected while parsing a
198                    configuration file line */
199                 fprintf(stderr, "%s\n", _("Missing '='"));
200                 return NULL;
201         }
203         *value++ = 0;
205         g_strchomp(p);
206         return g_strchug(value);
209 static int
210 parse_color(char *str)
212         char *value;
214         value = separate_value(str);
215         if (value == NULL)
216                 return -1;
218         return colors_assign(str, value);
221 /**
222  * Returns the first non-whitespace character after the next comma
223  * character, or the end of the string.  This is used to parse comma
224  * separated values.
225  */
226 static char *
227 after_comma(char *p)
229         char *comma = strchr(p, ',');
231         if (comma != NULL) {
232                 *comma++ = 0;
233                 comma = g_strchug(comma);
234         } else
235                 comma = p + strlen(p);
237         g_strchomp(p);
238         return comma;
241 static int
242 parse_color_definition(char *str)
244         char buf[MAX_LINE_LENGTH];
245         char *value;
246         short color, rgb[3];
248         value = separate_value(str);
249         if (value == NULL)
250                 return -1;
252         /* get the command name */
253         color = colors_str2color(str);
254         if (color < 0) {
255                 print_error(_("Bad color name"), buf);
256                 return -1;
257         }
259         /* parse r,g,b values */
261         for (unsigned i = 0; i < 3; ++i) {
262                 char *next = after_comma(value), *endptr;
263                 if (*value == 0) {
264                         print_error(_("Incomplete color definition"), str);
265                         return -1;
266                 }
268                 rgb[i] = strtol(value, &endptr, 0);
269                 if (endptr == value || *endptr != 0) {
270                         print_error(_("Invalid number"), value);
271                         return -1;
272                 }
274                 value = next;
275         }
277         if (*value != 0) {
278                 print_error(_("Malformed color definition"), str);
279                 return -1;
280         }
282         return colors_define(str, rgb[0], rgb[1], rgb[2]);
284 #endif
286 static char *
287 get_format(char *str)
289         gsize len = strlen(str);
291         if (str && str[0]=='\"' && str[len-1] == '\"') {
292                 str[len - 1] = '\0';
293                 str++;
294         }
296         return g_strdup(str);
299 static char **
300 check_screen_list(char *value)
302         char **tmp = g_strsplit_set(value, " \t,", 100);
303         char **screen = NULL;
304         int i,j;
306         i=0;
307         j=0;
308         while( tmp && tmp[i] ) {
309                 char *name = g_ascii_strdown(tmp[i], -1);
310                 if (*name != '\0') {
311                         if (screen_lookup_name(name) == NULL) {
312                                 /* an unknown screen name was specified in the
313                                    configuration file */
314                                 print_error(_("Unknown screen name"), name);
315                                 free(name);
316                         } else {
317                                 screen = g_realloc(screen, (j+2)*sizeof(char *));
318                                 screen[j++] = name;
319                                 screen[j] = NULL;
320                         }
321                 }
322                 i++;
323         }
324         g_strfreev(tmp);
325         if( screen == NULL )
326                 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
328         return screen;
331 static int
332 get_search_mode(char *value)
334         char * test;
335         int mode;
336         mode= strtol(value, &test, 10);
337         if (*test == '\0')
338         {
339                 if (0 <= mode && mode <= 4)
340                         return mode;
341                 else
342                 {
343                         print_error(_("Invalid search mode"),value);
344                         return 0;
345                 }
346         }
347         else
348         {
349                 for (int i = 0; value[i] != '\0'; i++)
350                         value[i] = tolower(value[i]);
352                 // TODO: modify screen_search so that its own list of modes can be used
353                 // for comparison instead of specifying them here
354                 if (strcmp(value, "title") == 0)
355                         return 0;
356                 else if (strcmp(value, "artist") == 0)
357                         return 1;
358                 else if (strcmp(value, "album") == 0)
359                         return 2;
360                 else if (strcmp(value, "filename") == 0)
361                         return 3;
362                 else if (strcmp(value, "artist+album") == 0)
363                         return 4;
364                 else
365                 {
366                         print_error(_("Unknown search mode"),value);
367                         return 0;
368                 }
369         }
372 static bool
373 parse_line(char *line)
375         size_t len = strlen(line), i = 0, j;
376         char name[MAX_LINE_LENGTH];
377         char value[MAX_LINE_LENGTH];
378         bool match_found;
380         /* get the name part */
381         j = 0;
382         while (i < len && line[i] != '=' &&
383                !g_ascii_isspace(line[i])) {
384                 name[j++] = line[i++];
385         }
387         name[j] = '\0';
389         /* skip '=' and whitespace */
390         while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
391                 i++;
393         /* get the value part */
394         j = 0;
395         while (i < len)
396                 value[j++] = line[i++];
397         value[j] = '\0';
399         match_found = true;
401         /* key definition */
402         if (!strcasecmp(CONF_KEY_DEFINITION, name))
403                 parse_key_definition(value);
404         /* enable colors */
405         else if(!strcasecmp(CONF_ENABLE_COLORS, name))
406 #ifdef ENABLE_COLORS
407                 options.enable_colors = str2bool(value);
408 #else
409         {}
410 #endif
411         else if (!strcasecmp(CONF_SCROLL_OFFSET, name))
412                 options.scroll_offset = atoi(value);
413         /* auto center */
414         else if (!strcasecmp(CONF_AUTO_CENTER, name))
415                 options.auto_center = str2bool(value);
416         /* color assignment */
417         else if (!strcasecmp(CONF_COLOR, name))
418 #ifdef ENABLE_COLORS
419                 parse_color(value);
420 #else
421         {}
422 #endif
423         /* wide cursor */
424         else if (!strcasecmp(CONF_WIDE_CURSOR, name))
425                 options.wide_cursor = str2bool(value);
426         else if (strcasecmp(name, "hardware-cursor") == 0)
427                 options.hardware_cursor = str2bool(value);
428         /* welcome screen list */
429         else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
430                 options.welcome_screen_list = str2bool(value);
431         /* visible bitrate */
432         else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
433                 options.visible_bitrate = str2bool(value);
434         /* timer display type */
435         else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name))
436                 options.display_remaining_time = parse_timedisplay_type(value);
437                 /* color definition */
438         else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
439 #ifdef ENABLE_COLORS
440                 parse_color_definition(value);
441 #else
442         {}
443 #endif
444         /* list format string */
445         else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
446                 g_free(options.list_format);
447                 options.list_format = get_format(value);
448                 /* status format string */
449         } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
450                 g_free(options.status_format);
451                 options.status_format = get_format(value);
452                 /* xterm title format string */
453         } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
454                 g_free(options.xterm_title_format);
455                 options.xterm_title_format = get_format(value);
456         } else if (!strcasecmp(CONF_LIST_WRAP, name))
457                 options.list_wrap = str2bool(value);
458         else if (!strcasecmp(CONF_FIND_WRAP, name))
459                 options.find_wrap = str2bool(value);
460         else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
461                 options.find_show_last_pattern = str2bool(value);
462         else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
463                 options.audible_bell = str2bool(value);
464         else if (!strcasecmp(CONF_VISIBLE_BELL, name))
465                 options.visible_bell = str2bool(value);
466         else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
467                 options.bell_on_wrap = str2bool(value);
468         else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
469                 options.status_message_time = atoi(value);
470         else if (!strcasecmp(CONF_XTERM_TITLE, name))
471                 options.enable_xterm_title = str2bool(value);
472         else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
473 #ifdef HAVE_GETMOUSE
474                 options.enable_mouse = str2bool(value);
475 #else
476         {}
477 #endif
478         else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
479                 options.crossfade_time = atoi(value);
480         else if (!strcasecmp(CONF_SEARCH_MODE, name))
481                 options.search_mode = get_search_mode(value);
482         else if (!strcasecmp(CONF_HIDE_CURSOR, name))
483                 options.hide_cursor = atoi(value);
484         else if (!strcasecmp(CONF_SEEK_TIME, name))
485                 options.seek_time = atoi(value);
486         else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
487                 g_strfreev(options.screen_list);
488                 options.screen_list = check_screen_list(value);
489         } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
490                 /* the splash screen was removed */
491         } else if (!strcasecmp(CONF_HOST, name))
492                 options.host = get_format(value);
493         else if (!strcasecmp(CONF_PORT, name))
494                 options.port = atoi(get_format(value));
495         else if (!strcasecmp(CONF_PASSWORD, name))
496                 options.password = get_format(value);
497         else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
498 #ifdef ENABLE_LYRICS_SCREEN
499                 options.lyrics_timeout = atoi(get_format(value));
500 #else
501         {}
502 #endif
503         else if (!strcasecmp(CONF_SCROLL, name))
504                 options.scroll = str2bool(value);
505         else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
506                 g_free(options.scroll_sep);
507                 options.scroll_sep = get_format(value);
508         } else if (!strcasecmp(CONF_DISPLAY_TIME, name))
509 #ifdef NCMPC_MINI
510                 {}
511 #else
512                 options.display_time = str2bool(value);
513 #endif
514         else if (!strcasecmp(CONF_JUMP_PREFIX_ONLY, name))
515 #ifdef NCMPC_MINI
516                 {}
517 #else
518                 options.jump_prefix_only = str2bool(value);
519 #endif
520         else if (!strcasecmp(CONF_LYRICS_AUTOSAVE, name))
521 #ifdef ENABLE_LYRICS_SCREEN
522                 options.lyrics_autosave = str2bool(value);
523 #else
524         {}
525 #endif
526         else
527                 match_found = false;
529         if (!match_found)
530                 print_error(_("Unknown configuration parameter"),
531                             name);
533         return match_found;
536 static int
537 read_rc_file(char *filename)
539         FILE *file;
540         char line[MAX_LINE_LENGTH];
542         if (filename == NULL)
543                 return -1;
545         file = fopen(filename, "r");
546         if (file == NULL) {
547                         perror(filename);
548                         return -1;
549                 }
551         while (fgets(line, sizeof(line), file) != NULL) {
552                 char *p = g_strchug(line);
554                 if (*p != 0 && *p != COMMENT_TOKEN)
555                         parse_line(g_strchomp(p));
556         }
558         fclose(file);
559         return 0;
562 int
563 check_user_conf_dir(void)
565         int retval;
566         char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
568         if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
569                 g_free(directory);
570                 return 0;
571         }
573         retval = mkdir(directory, 0755);
574         g_free(directory);
575         return retval;
578 char *
579 get_user_key_binding_filename(void)
581         return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
584 int
585 read_configuration(void)
587         char *filename = NULL;
589         /* check for command line configuration file */
590         if (options.config_file)
591                 filename = g_strdup(options.config_file);
593         /* check for user configuration ~/.ncmpc/config */
594         if (filename == NULL) {
595                 filename = g_build_filename(g_get_home_dir(),
596                                             "." PACKAGE, "config", NULL);
597                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
598                         g_free(filename);
599                         filename = NULL;
600                 }
601         }
603         /* check for  global configuration SYSCONFDIR/ncmpc/config */
604         if (filename == NULL) {
605                 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
606                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
607                         g_free(filename);
608                         filename = NULL;
609                 }
610         }
612         /* load configuration */
613         if (filename) {
614                 read_rc_file(filename);
615                 g_free(filename);
616                 filename = NULL;
617         }
619         /* check for command line key binding file */
620         if (options.key_file)
621                 filename = g_strdup(options.key_file);
623         /* check for  user key bindings ~/.ncmpc/keys */
624         if (filename == NULL) {
625                 filename = get_user_key_binding_filename();
626                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
627                         g_free(filename);
628                         filename = NULL;
629                 }
630         }
632         /* check for  global key bindings SYSCONFDIR/ncmpc/keys */
633         if (filename == NULL) {
634                 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
635                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
636                         g_free(filename);
637                         filename = NULL;
638                 }
639         }
641         /* load key bindings */
642         if (filename) {
643                 read_rc_file(filename);
644                 g_free(filename);
645                 filename = NULL;
646         }
648         return 0;