Code

2b63b9b3623162f1ef4eeca9c40312fec5555b56
[ncmpc.git] / src / conf.c
1 /*
2  * (c) 2004 by Kalle Wallin <kaw@linux.se>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include "conf.h"
20 #include "config.h"
21 #include "defaults.h"
22 #include "i18n.h"
23 #include "command.h"
24 #include "colors.h"
25 #include "screen_list.h"
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <glib.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_AUTO_CENTER "auto-center"
44 #define CONF_WIDE_CURSOR "wide-cursor"
45 #define CONF_ENABLE_BELL "enable-bell"
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_STATUS_FORMAT "status-format"
51 #define CONF_XTERM_TITLE_FORMAT "xterm-title-format"
52 #define CONF_LIST_WRAP "wrap-around"
53 #define CONF_FIND_WRAP "find-wrap"
54 #define CONF_FIND_SHOW_LAST "find-show-last"
55 #define CONF_AUDIBLE_BELL "audible-bell"
56 #define CONF_VISIBLE_BELL "visible-bell"
57 #define CONF_XTERM_TITLE "set-xterm-title"
58 #define CONF_ENABLE_MOUSE "enable-mouse"
59 #define CONF_CROSSFADE_TIME "crossfade-time"
60 #define CONF_SEARCH_MODE "search-mode"
61 #define CONF_HIDE_CURSOR "hide-cursor"
62 #define CONF_SEEK_TIME "seek-time"
63 #define CONF_SCREEN_LIST "screen-list"
64 #define CONF_TIMEDISPLAY_TYPE "timedisplay-type"
65 #define CONF_HOST "host"
66 #define CONF_PORT "port"
67 #define CONF_PASSWORD "password"
68 #define CONF_LYRICS_TIMEOUT "lyrics-timeout"
69 #define CONF_SHOW_SPLASH "show-splash"
70 #define CONF_SCROLL "scroll"
71 #define CONF_SCROLL_SEP "scroll-sep"
72 #define CONF_VISIBLE_BITRATE "visible-bitrate"
73 #define CONF_WELCOME_SCREEN_LIST "welcome-screen-list"
75 static bool
76 str2bool(char *str)
77 {
78         return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
79                 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
80 }
82 static void
83 print_error(const char *msg, const char *input)
84 {
85         fprintf(stderr, "%s: %s ('%s')\n",
86                 /* To translators: prefix for error messages */
87                 _("Error"), msg, input);
88 }
90 static int
91 parse_key_value(char *str, char **end)
92 {
93         if (*str == '\'') {
94                 if (str[1] == '\'' || str[2] != '\'') {
95                         print_error(_("Malformed hotkey definition"), str);
96                         return -1;
97                 }
99                 *end = str + 3;
100                 return str[1];
101         } else {
102                 long value = strtol(str, end, 0);
103                 if (*end == str) {
104                         print_error(_("Malformed hotkey definition"), str);
105                         return -1;
106                 }
108                 return (int)value;
109         }
112 static int
113 parse_key_definition(char *str)
115         char buf[MAX_LINE_LENGTH];
116         char *p;
117         size_t len = strlen(str), i;
118         int j,key;
119         int keys[MAX_COMMAND_KEYS];
120         command_t cmd;
122         /* get the command name */
123         i=0;
124         j=0;
125         memset(buf, 0, MAX_LINE_LENGTH);
126         while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
127                 buf[j++] = str[i++];
128         if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
129                 /* the hotkey configuration contains an unknown
130                    command */
131                 print_error(_("Unknown command"), buf);
132                 return -1;
133         }
135         /* skip whitespace */
136         while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
137                 i++;
139         /* get the value part */
140         memset(buf, 0, MAX_LINE_LENGTH);
141         g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
142         if (*buf == 0) {
143                 /* the hotkey configuration line is incomplete */
144                 print_error(_("Incomplete hotkey configuration"), str);
145                 return -1;
146         }
148         /* parse key values */
149         i = 0;
150         key = 0;
151         p = buf;
152         memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
153         while (i < MAX_COMMAND_KEYS && *p != 0 &&
154                (key = parse_key_value(p, &p)) >= 0) {
155                 keys[i++] = key;
156                 while (*p==',' || *p==' ' || *p=='\t')
157                         p++;
158         }
160         if (key < 0)
161                 return -1;
163         return assign_keys(cmd, keys);
166 static const char *
167 parse_timedisplay_type(const char *str)
169         if (!strcmp(str,"elapsed") || !strcmp(str,"remaining"))
170                 return str;
171         else {
172                 /* translators: ncmpc supports displaying the
173                    "elapsed" or "remaining" time of a song being
174                    played; in this case, the configuration file
175                    contained an invalid setting */
176                 print_error(_("Bad time display type"), str);
177                 return DEFAULT_TIMEDISPLAY_TYPE;
178         }
181 #ifdef ENABLE_COLORS
182 static char *
183 separate_value(char *p)
185         char *value;
187         value = strchr(p, '=');
188         if (value == NULL) {
189                 /* an equals sign '=' was expected while parsing a
190                    configuration file line */
191                 fprintf(stderr, "%s\n", _("Missing '='"));
192                 return NULL;
193         }
195         *value++ = 0;
197         g_strchomp(p);
198         return g_strchug(value);
201 static int
202 parse_color(char *str)
204         char *value;
206         value = separate_value(str);
207         if (value == NULL)
208                 return -1;
210         return colors_assign(str, value);
213 /**
214  * Returns the first non-whitespace character after the next comma
215  * character, or the end of the string.  This is used to parse comma
216  * separated values.
217  */
218 static char *
219 after_comma(char *p)
221         char *comma = strchr(p, ',');
223         if (comma != NULL) {
224                 *comma++ = 0;
225                 comma = g_strchug(comma);
226         } else
227                 comma = p + strlen(p);
229         g_strchomp(p);
230         return comma;
233 static int
234 parse_color_definition(char *str)
236         char buf[MAX_LINE_LENGTH];
237         char *value;
238         short color, rgb[3];
240         value = separate_value(str);
241         if (value == NULL)
242                 return -1;
244         /* get the command name */
245         color = colors_str2color(str);
246         if (color < 0) {
247                 print_error(_("Bad color name"), buf);
248                 return -1;
249         }
251         /* parse r,g,b values */
253         for (unsigned i = 0; i < 3; ++i) {
254                 char *next = after_comma(value), *endptr;
255                 if (*value == 0) {
256                         print_error(_("Incomplete color definition"), str);
257                         return -1;
258                 }
260                 rgb[i] = strtol(value, &endptr, 0);
261                 if (endptr == value || *endptr != 0) {
262                         print_error(_("Invalid number"), value);
263                         return -1;
264                 }
266                 value = next;
267         }
269         if (*value != 0) {
270                 print_error(_("Malformed color definition"), str);
271                 return -1;
272         }
274         return colors_define(str, rgb[0], rgb[1], rgb[2]);
276 #endif
278 static char *
279 get_format(char *str)
281         gsize len = strlen(str);
283         if (str && str[0]=='\"' && str[len-1] == '\"') {
284                 str[len - 1] = '\0';
285                 str++;
286         }
288         return g_strdup(str);
291 static char **
292 check_screen_list(char *value)
294         char **tmp = g_strsplit_set(value, " \t,", 100);
295         char **screen = NULL;
296         int i,j;
298         i=0;
299         j=0;
300         while( tmp && tmp[i] ) {
301                 char *name = g_ascii_strdown(tmp[i], -1);
302                 if (screen_lookup_name(name) == NULL) {
303                         /* an unknown screen name was specified in the
304                            configuration file */
305                         print_error(_("Unknown screen name"), name);
306                         free(name);
307                 } else {
308                         screen = g_realloc(screen, (j+2)*sizeof(char *));
309                         screen[j++] = name;
310                         screen[j] = NULL;
311                 }
312                 i++;
313         }
314         g_strfreev(tmp);
315         if( screen == NULL )
316                 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
318         return screen;
321 static bool
322 parse_line(char *line)
324         size_t len = strlen(line), i = 0, j;
325         char name[MAX_LINE_LENGTH];
326         char value[MAX_LINE_LENGTH];
327         bool match_found;
329         /* get the name part */
330         j = 0;
331         while (i < len && line[i] != '=' &&
332                !g_ascii_isspace(line[i])) {
333                 name[j++] = line[i++];
334         }
336         name[j] = '\0';
338         /* skip '=' and whitespace */
339         while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
340                 i++;
342         /* get the value part */
343         j = 0;
344         while (i < len)
345                 value[j++] = line[i++];
346         value[j] = '\0';
348         match_found = true;
350         /* key definition */
351         if (!strcasecmp(CONF_KEY_DEFINITION, name))
352                 parse_key_definition(value);
353         /* enable colors */
354         else if(!strcasecmp(CONF_ENABLE_COLORS, name))
355 #ifdef ENABLE_COLORS
356                 options.enable_colors = str2bool(value);
357 #else
358         {}
359 #endif
360         /* auto center */
361         else if (!strcasecmp(CONF_AUTO_CENTER, name))
362                 options.auto_center = str2bool(value);
363         /* color assignment */
364         else if (!strcasecmp(CONF_COLOR, name))
365 #ifdef ENABLE_COLORS
366                 parse_color(value);
367 #else
368         {}
369 #endif
370         /* wide cursor */
371         else if (!strcasecmp(CONF_WIDE_CURSOR, name))
372                 options.wide_cursor = str2bool(value);
373         /* welcome screen list */
374         else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
375                 options.welcome_screen_list = str2bool(value);
376         /* visible bitrate */
377         else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
378                 options.visible_bitrate = str2bool(value);
379         /* timer display type */
380         else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name)) {
381                 g_free(options.timedisplay_type);
382                 options.timedisplay_type=g_strdup(parse_timedisplay_type(value));
383                 /* color definition */
384         } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
385 #ifdef ENABLE_COLORS
386                 parse_color_definition(value);
387 #else
388         {}
389 #endif
390         /* list format string */
391         else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
392                 g_free(options.list_format);
393                 options.list_format = get_format(value);
394                 /* status format string */
395         } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
396                 g_free(options.status_format);
397                 options.status_format = get_format(value);
398                 /* xterm title format string */
399         } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
400                 g_free(options.xterm_title_format);
401                 options.xterm_title_format = get_format(value);
402         } else if (!strcasecmp(CONF_LIST_WRAP, name))
403                 options.list_wrap = str2bool(value);
404         else if (!strcasecmp(CONF_FIND_WRAP, name))
405                 options.find_wrap = str2bool(value);
406         else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
407                 options.find_show_last_pattern = str2bool(value);
408         else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
409                 options.audible_bell = str2bool(value);
410         else if (!strcasecmp(CONF_VISIBLE_BELL, name))
411                 options.visible_bell = str2bool(value);
412         else if (!strcasecmp(CONF_XTERM_TITLE, name))
413                 options.enable_xterm_title = str2bool(value);
414         else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
415 #ifdef HAVE_GETMOUSE
416                 options.enable_mouse = str2bool(value);
417 #else
418         {}
419 #endif
420         else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
421                 options.crossfade_time = atoi(value);
422         else if (!strcasecmp(CONF_SEARCH_MODE, name))
423                 options.search_mode = atoi(value);
424         else if (!strcasecmp(CONF_HIDE_CURSOR, name))
425                 options.hide_cursor = atoi(value);
426         else if (!strcasecmp(CONF_SEEK_TIME, name))
427                 options.seek_time = atoi(value);
428         else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
429                 g_strfreev(options.screen_list);
430                 options.screen_list = check_screen_list(value);
431         } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
432                 /* the splash screen was removed */
433         } else if (!strcasecmp(CONF_HOST, name))
434                 options.host = get_format(value);
435         else if (!strcasecmp(CONF_PORT, name))
436                 options.port = atoi(get_format(value));
437         else if (!strcasecmp(CONF_PASSWORD, name))
438                 options.password = get_format(value);
439         else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
440 #ifdef ENABLE_LYRICS_SCREEN
441                 options.lyrics_timeout = atoi(get_format(value));
442 #else
443         {}
444 #endif
445         else if (!strcasecmp(CONF_SCROLL, name))
446                 options.scroll = str2bool(value);
447         else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
448                 g_free(options.scroll_sep);
449                 options.scroll_sep = get_format(value);
450         } else
451                 match_found = false;
453         if (!match_found)
454                 print_error(_("Unknown configuration parameter"),
455                             name);
457         return match_found;
460 static int
461 read_rc_file(char *filename)
463         FILE *file;
464         char line[MAX_LINE_LENGTH];
466         if (filename == NULL)
467                 return -1;
469         file = fopen(filename, "r");
470         if (file == NULL) {
471                         perror(filename);
472                         return -1;
473                 }
475         while (fgets(line, sizeof(line), file) != NULL) {
476                 char *p = g_strchug(line);
478                 if (*p != 0 && *p != COMMENT_TOKEN)
479                         parse_line(g_strchomp(p));
480         }
482         fclose(file);
483         return 0;
486 int
487 check_user_conf_dir(void)
489         int retval;
490         char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
492         if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
493                 g_free(directory);
494                 return 0;
495         }
497         retval = mkdir(directory, 0755);
498         g_free(directory);
499         return retval;
502 char *
503 get_user_key_binding_filename(void)
505         return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
508 int
509 read_configuration(void)
511         char *filename = NULL;
513         /* check for command line configuration file */
514         if (options.config_file)
515                 filename = g_strdup(options.config_file);
517         /* check for user configuration ~/.ncmpc/config */
518         if (filename == NULL) {
519                 filename = g_build_filename(g_get_home_dir(),
520                                             "." PACKAGE, "config", NULL);
521                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
522                         g_free(filename);
523                         filename = NULL;
524                 }
525         }
527         /* check for  global configuration SYSCONFDIR/ncmpc/config */
528         if (filename == NULL) {
529                 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
530                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
531                         g_free(filename);
532                         filename = NULL;
533                 }
534         }
536         /* load configuration */
537         if (filename) {
538                 read_rc_file(filename);
539                 g_free(filename);
540                 filename = NULL;
541         }
543         /* check for command line key binding file */
544         if (options.key_file)
545                 filename = g_strdup(options.key_file);
547         /* check for  user key bindings ~/.ncmpc/keys */
548         if (filename == NULL) {
549                 filename = get_user_key_binding_filename();
550                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
551                         g_free(filename);
552                         filename = NULL;
553                 }
554         }
556         /* check for  global key bindings SYSCONFDIR/ncmpc/keys */
557         if (filename == NULL) {
558                 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
559                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
560                         g_free(filename);
561                         filename = NULL;
562                 }
563         }
565         /* load key bindings */
566         if (filename) {
567                 read_rc_file(filename);
568                 g_free(filename);
569                 filename = NULL;
570         }
572         return 0;