Code

3411ed8ad4625a7e849f874443d2756ea571e7d5
[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(_("Unsupported key 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(_("Unsupported key 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                 print_error(_("Unknown key command"), buf);
130                 return -1;
131         }
133         /* skip whitespace */
134         while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
135                 i++;
137         /* get the value part */
138         memset(buf, 0, MAX_LINE_LENGTH);
139         g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
140         if (*buf == 0) {
141                 print_error(_("Incomplete key definition"), str);
142                 return -1;
143         }
145         /* parse key values */
146         i = 0;
147         key = 0;
148         p = buf;
149         memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
150         while (i < MAX_COMMAND_KEYS && *p != 0 &&
151                (key = parse_key_value(p, &p)) >= 0) {
152                 keys[i++] = key;
153                 while (*p==',' || *p==' ' || *p=='\t')
154                         p++;
155         }
157         if (key < 0) {
158                 print_error(_("Bad key definition"), str);
159                 return -1;
160         }
162         return assign_keys(cmd, keys);
165 static const char *
166 parse_timedisplay_type(const char *str)
168         if (!strcmp(str,"elapsed") || !strcmp(str,"remaining"))
169                 return str;
170         else {
171                 print_error(_("Bad time display type"), str);
172                 return DEFAULT_TIMEDISPLAY_TYPE;
173         }
176 #ifdef ENABLE_COLORS
177 static char *
178 separate_value(char *p)
180         char *value;
182         value = strchr(p, '=');
183         if (value == NULL) {
184                 fprintf(stderr, "%s\n", _("Missing '='"));
185                 return NULL;
186         }
188         *value++ = 0;
190         g_strchomp(p);
191         return g_strchug(value);
194 static int
195 parse_color(char *str)
197         char *value;
199         value = separate_value(str);
200         if (value == NULL)
201                 return -1;
203         return colors_assign(str, value);
206 /**
207  * Returns the first non-whitespace character after the next comma
208  * character, or the end of the string.  This is used to parse comma
209  * separated values.
210  */
211 static char *
212 after_comma(char *p)
214         char *comma = strchr(p, ',');
216         if (comma != NULL) {
217                 *comma++ = 0;
218                 comma = g_strchug(comma);
219         } else
220                 comma = p + strlen(p);
222         g_strchomp(p);
223         return comma;
226 static int
227 parse_color_definition(char *str)
229         char buf[MAX_LINE_LENGTH];
230         char *value;
231         short color, rgb[3];
233         value = separate_value(str);
234         if (value == NULL)
235                 return -1;
237         /* get the command name */
238         color = colors_str2color(str);
239         if (color < 0) {
240                 print_error(_("Bad color"), buf);
241                 return -1;
242         }
244         /* parse r,g,b values */
246         for (unsigned i = 0; i < 3; ++i) {
247                 char *next = after_comma(value), *endptr;
248                 if (*value == 0) {
249                         print_error(_("Incomplete color definition"), str);
250                         return -1;
251                 }
253                 rgb[i] = strtol(value, &endptr, 0);
254                 if (endptr == value || *endptr != 0) {
255                         print_error(_("Invalid number"), value);
256                         return -1;
257                 }
259                 value = next;
260         }
262         if (*value != 0) {
263                 print_error(_("Bad color definition"), str);
264                 return -1;
265         }
267         return colors_define(str, rgb[0], rgb[1], rgb[2]);
269 #endif
271 static char *
272 get_format(char *str)
274         gsize len = strlen(str);
276         if (str && str[0]=='\"' && str[len-1] == '\"') {
277                 str[len - 1] = '\0';
278                 str++;
279         }
281         return g_strdup(str);
284 static char **
285 check_screen_list(char *value)
287         char **tmp = g_strsplit_set(value, " \t,", 100);
288         char **screen = NULL;
289         int i,j;
291         i=0;
292         j=0;
293         while( tmp && tmp[i] ) {
294                 char *name = g_ascii_strdown(tmp[i], -1);
295                 if (screen_lookup_name(name) == NULL) {
296                         print_error(_("Unsupported screen"), name);
297                         free(name);
298                 } else {
299                         screen = g_realloc(screen, (j+2)*sizeof(char *));
300                         screen[j++] = name;
301                         screen[j] = NULL;
302                 }
303                 i++;
304         }
305         g_strfreev(tmp);
306         if( screen == NULL )
307                 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
309         return screen;
312 static bool
313 parse_line(char *line)
315         size_t len = strlen(line), i = 0, j;
316         char name[MAX_LINE_LENGTH];
317         char value[MAX_LINE_LENGTH];
318         bool match_found;
320         /* get the name part */
321         j = 0;
322         while (i < len && line[i] != '=' &&
323                !g_ascii_isspace(line[i])) {
324                 name[j++] = line[i++];
325         }
327         name[j] = '\0';
329         /* skip '=' and whitespace */
330         while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
331                 i++;
333         /* get the value part */
334         j = 0;
335         while (i < len)
336                 value[j++] = line[i++];
337         value[j] = '\0';
339         match_found = true;
341         /* key definition */
342         if (!strcasecmp(CONF_KEY_DEFINITION, name))
343                 parse_key_definition(value);
344         /* enable colors */
345         else if(!strcasecmp(CONF_ENABLE_COLORS, name))
346 #ifdef ENABLE_COLORS
347                 options.enable_colors = str2bool(value);
348 #else
349         {}
350 #endif
351         /* auto center */
352         else if (!strcasecmp(CONF_AUTO_CENTER, name))
353                 options.auto_center = str2bool(value);
354         /* color assignment */
355         else if (!strcasecmp(CONF_COLOR, name))
356 #ifdef ENABLE_COLORS
357                 parse_color(value);
358 #else
359         {}
360 #endif
361         /* wide cursor */
362         else if (!strcasecmp(CONF_WIDE_CURSOR, name))
363                 options.wide_cursor = str2bool(value);
364         /* welcome screen list */
365         else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
366                 options.welcome_screen_list = str2bool(value);
367         /* visible bitrate */
368         else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
369                 options.visible_bitrate = str2bool(value);
370         /* timer display type */
371         else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name)) {
372                 g_free(options.timedisplay_type);
373                 options.timedisplay_type=g_strdup(parse_timedisplay_type(value));
374                 /* color definition */
375         } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
376 #ifdef ENABLE_COLORS
377                 parse_color_definition(value);
378 #else
379         {}
380 #endif
381         /* list format string */
382         else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
383                 g_free(options.list_format);
384                 options.list_format = get_format(value);
385                 /* status format string */
386         } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
387                 g_free(options.status_format);
388                 options.status_format = get_format(value);
389                 /* xterm title format string */
390         } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
391                 g_free(options.xterm_title_format);
392                 options.xterm_title_format = get_format(value);
393         } else if (!strcasecmp(CONF_LIST_WRAP, name))
394                 options.list_wrap = str2bool(value);
395         else if (!strcasecmp(CONF_FIND_WRAP, name))
396                 options.find_wrap = str2bool(value);
397         else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
398                 options.find_show_last_pattern = str2bool(value);
399         else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
400                 options.audible_bell = str2bool(value);
401         else if (!strcasecmp(CONF_VISIBLE_BELL, name))
402                 options.visible_bell = str2bool(value);
403         else if (!strcasecmp(CONF_XTERM_TITLE, name))
404                 options.enable_xterm_title = str2bool(value);
405         else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
406 #ifdef HAVE_GETMOUSE
407                 options.enable_mouse = str2bool(value);
408 #else
409         {}
410 #endif
411         else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
412                 options.crossfade_time = atoi(value);
413         else if (!strcasecmp(CONF_SEARCH_MODE, name))
414                 options.search_mode = atoi(value);
415         else if (!strcasecmp(CONF_HIDE_CURSOR, name))
416                 options.hide_cursor = atoi(value);
417         else if (!strcasecmp(CONF_SEEK_TIME, name))
418                 options.seek_time = atoi(value);
419         else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
420                 g_strfreev(options.screen_list);
421                 options.screen_list = check_screen_list(value);
422         } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
423                 /* the splash screen was removed */
424         } else if (!strcasecmp(CONF_HOST, name))
425                 options.host = get_format(value);
426         else if (!strcasecmp(CONF_PORT, name))
427                 options.port = atoi(get_format(value));
428         else if (!strcasecmp(CONF_PASSWORD, name))
429                 options.password = get_format(value);
430         else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
431 #ifdef ENABLE_LYRICS_SCREEN
432                 options.lyrics_timeout = atoi(get_format(value));
433 #else
434         {}
435 #endif
436         else if (!strcasecmp(CONF_SCROLL, name))
437                 options.scroll = str2bool(value);
438         else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
439                 g_free(options.scroll_sep);
440                 options.scroll_sep = get_format(value);
441         } else
442                 match_found = false;
444         if (!match_found)
445                 print_error(_("Unknown configuration parameter"),
446                             name);
448         return match_found;
451 static int
452 read_rc_file(char *filename)
454         FILE *file;
455         char line[MAX_LINE_LENGTH];
457         if (filename == NULL)
458                 return -1;
460         file = fopen(filename, "r");
461         if (file == NULL) {
462                         perror(filename);
463                         return -1;
464                 }
466         while (fgets(line, sizeof(line), file) != NULL) {
467                 char *p = g_strchug(line);
469                 if (*p != 0 && *p != COMMENT_TOKEN)
470                         parse_line(g_strchomp(p));
471         }
473         fclose(file);
474         return 0;
477 int
478 check_user_conf_dir(void)
480         int retval;
481         char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
483         if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
484                 g_free(directory);
485                 return 0;
486         }
488         retval = mkdir(directory, 0755);
489         g_free(directory);
490         return retval;
493 char *
494 get_user_key_binding_filename(void)
496         return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
499 int
500 read_configuration(void)
502         char *filename = NULL;
504         /* check for command line configuration file */
505         if (options.config_file)
506                 filename = g_strdup(options.config_file);
508         /* check for user configuration ~/.ncmpc/config */
509         if (filename == NULL) {
510                 filename = g_build_filename(g_get_home_dir(),
511                                             "." PACKAGE, "config", NULL);
512                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
513                         g_free(filename);
514                         filename = NULL;
515                 }
516         }
518         /* check for  global configuration SYSCONFDIR/ncmpc/config */
519         if (filename == NULL) {
520                 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
521                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
522                         g_free(filename);
523                         filename = NULL;
524                 }
525         }
527         /* load configuration */
528         if (filename) {
529                 read_rc_file(filename);
530                 g_free(filename);
531                 filename = NULL;
532         }
534         /* check for command line key binding file */
535         if (options.key_file)
536                 filename = g_strdup(options.key_file);
538         /* check for  user key bindings ~/.ncmpc/keys */
539         if (filename == NULL) {
540                 filename = get_user_key_binding_filename();
541                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
542                         g_free(filename);
543                         filename = NULL;
544                 }
545         }
547         /* check for  global key bindings SYSCONFDIR/ncmpc/keys */
548         if (filename == NULL) {
549                 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
550                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
551                         g_free(filename);
552                         filename = NULL;
553                 }
554         }
556         /* load key bindings */
557         if (filename) {
558                 read_rc_file(filename);
559                 g_free(filename);
560                 filename = NULL;
561         }
563         return 0;