e60fb8f952de4cbbff24bd1fbb53055385206b91
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 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.
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"
80 static bool
81 str2bool(char *str)
82 {
83 return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
84 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
85 }
87 static void
88 print_error(const char *msg, const char *input)
89 {
90 fprintf(stderr, "%s: %s ('%s')\n",
91 /* To translators: prefix for error messages */
92 _("Error"), msg, input);
93 }
95 static int
96 parse_key_value(char *str, char **end)
97 {
98 if (*str == '\'') {
99 if (str[1] == '\'' || str[2] != '\'') {
100 print_error(_("Malformed hotkey definition"), str);
101 return -1;
102 }
104 *end = str + 3;
105 return str[1];
106 } else {
107 long value = strtol(str, end, 0);
108 if (*end == str) {
109 print_error(_("Malformed hotkey definition"), str);
110 return -1;
111 }
113 return (int)value;
114 }
115 }
117 static int
118 parse_key_definition(char *str)
119 {
120 char buf[MAX_LINE_LENGTH];
121 char *p;
122 size_t len = strlen(str), i;
123 int j,key;
124 int keys[MAX_COMMAND_KEYS];
125 command_t cmd;
127 /* get the command name */
128 i=0;
129 j=0;
130 memset(buf, 0, MAX_LINE_LENGTH);
131 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
132 buf[j++] = str[i++];
133 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
134 /* the hotkey configuration contains an unknown
135 command */
136 print_error(_("Unknown command"), buf);
137 return -1;
138 }
140 /* skip whitespace */
141 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
142 i++;
144 /* get the value part */
145 memset(buf, 0, MAX_LINE_LENGTH);
146 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
147 if (*buf == 0) {
148 /* the hotkey configuration line is incomplete */
149 print_error(_("Incomplete hotkey configuration"), str);
150 return -1;
151 }
153 /* parse key values */
154 i = 0;
155 key = 0;
156 p = buf;
157 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
158 while (i < MAX_COMMAND_KEYS && *p != 0 &&
159 (key = parse_key_value(p, &p)) >= 0) {
160 keys[i++] = key;
161 while (*p==',' || *p==' ' || *p=='\t')
162 p++;
163 }
165 if (key < 0)
166 return -1;
168 return assign_keys(cmd, keys);
169 }
171 static const char *
172 parse_timedisplay_type(const char *str)
173 {
174 if (!strcmp(str,"elapsed") || !strcmp(str,"remaining"))
175 return str;
176 else {
177 /* translators: ncmpc supports displaying the
178 "elapsed" or "remaining" time of a song being
179 played; in this case, the configuration file
180 contained an invalid setting */
181 print_error(_("Bad time display type"), str);
182 return DEFAULT_TIMEDISPLAY_TYPE;
183 }
184 }
186 #ifdef ENABLE_COLORS
187 static char *
188 separate_value(char *p)
189 {
190 char *value;
192 value = strchr(p, '=');
193 if (value == NULL) {
194 /* an equals sign '=' was expected while parsing a
195 configuration file line */
196 fprintf(stderr, "%s\n", _("Missing '='"));
197 return NULL;
198 }
200 *value++ = 0;
202 g_strchomp(p);
203 return g_strchug(value);
204 }
206 static int
207 parse_color(char *str)
208 {
209 char *value;
211 value = separate_value(str);
212 if (value == NULL)
213 return -1;
215 return colors_assign(str, value);
216 }
218 /**
219 * Returns the first non-whitespace character after the next comma
220 * character, or the end of the string. This is used to parse comma
221 * separated values.
222 */
223 static char *
224 after_comma(char *p)
225 {
226 char *comma = strchr(p, ',');
228 if (comma != NULL) {
229 *comma++ = 0;
230 comma = g_strchug(comma);
231 } else
232 comma = p + strlen(p);
234 g_strchomp(p);
235 return comma;
236 }
238 static int
239 parse_color_definition(char *str)
240 {
241 char buf[MAX_LINE_LENGTH];
242 char *value;
243 short color, rgb[3];
245 value = separate_value(str);
246 if (value == NULL)
247 return -1;
249 /* get the command name */
250 color = colors_str2color(str);
251 if (color < 0) {
252 print_error(_("Bad color name"), buf);
253 return -1;
254 }
256 /* parse r,g,b values */
258 for (unsigned i = 0; i < 3; ++i) {
259 char *next = after_comma(value), *endptr;
260 if (*value == 0) {
261 print_error(_("Incomplete color definition"), str);
262 return -1;
263 }
265 rgb[i] = strtol(value, &endptr, 0);
266 if (endptr == value || *endptr != 0) {
267 print_error(_("Invalid number"), value);
268 return -1;
269 }
271 value = next;
272 }
274 if (*value != 0) {
275 print_error(_("Malformed color definition"), str);
276 return -1;
277 }
279 return colors_define(str, rgb[0], rgb[1], rgb[2]);
280 }
281 #endif
283 static char *
284 get_format(char *str)
285 {
286 gsize len = strlen(str);
288 if (str && str[0]=='\"' && str[len-1] == '\"') {
289 str[len - 1] = '\0';
290 str++;
291 }
293 return g_strdup(str);
294 }
296 static char **
297 check_screen_list(char *value)
298 {
299 char **tmp = g_strsplit_set(value, " \t,", 100);
300 char **screen = NULL;
301 int i,j;
303 i=0;
304 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;
326 }
328 static int
329 get_search_mode(char *value)
330 {
331 char * test;
332 int mode;
333 mode= strtol(value, &test, 10);
334 if (*test == '\0')
335 {
336 if (0 <= mode && mode <= 4)
337 return mode;
338 else
339 {
340 print_error(_("Invalid search mode"),value);
341 return 0;
342 }
343 }
344 else
345 {
346 for (int i = 0; value[i] != '\0'; i++)
347 value[i] = tolower(value[i]);
349 // TODO: modify screen_search so that its own list of modes can be used
350 // for comparison instead of specifying them here
351 if (strcmp(value, "title") == 0)
352 return 0;
353 else if (strcmp(value, "artist") == 0)
354 return 1;
355 else if (strcmp(value, "album") == 0)
356 return 2;
357 else if (strcmp(value, "filename") == 0)
358 return 3;
359 else if (strcmp(value, "artist+album") == 0)
360 return 4;
361 else
362 {
363 print_error(_("Unknown search mode"),value);
364 return 0;
365 }
366 }
367 }
369 static bool
370 parse_line(char *line)
371 {
372 size_t len = strlen(line), i = 0, j;
373 char name[MAX_LINE_LENGTH];
374 char value[MAX_LINE_LENGTH];
375 bool match_found;
377 /* get the name part */
378 j = 0;
379 while (i < len && line[i] != '=' &&
380 !g_ascii_isspace(line[i])) {
381 name[j++] = line[i++];
382 }
384 name[j] = '\0';
386 /* skip '=' and whitespace */
387 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
388 i++;
390 /* get the value part */
391 j = 0;
392 while (i < len)
393 value[j++] = line[i++];
394 value[j] = '\0';
396 match_found = true;
398 /* key definition */
399 if (!strcasecmp(CONF_KEY_DEFINITION, name))
400 parse_key_definition(value);
401 /* enable colors */
402 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
403 #ifdef ENABLE_COLORS
404 options.enable_colors = str2bool(value);
405 #else
406 {}
407 #endif
408 else if (!strcasecmp(CONF_SCROLL_OFFSET, name))
409 options.scroll_offset = atoi(value);
410 /* auto center */
411 else if (!strcasecmp(CONF_AUTO_CENTER, name))
412 options.auto_center = str2bool(value);
413 /* color assignment */
414 else if (!strcasecmp(CONF_COLOR, name))
415 #ifdef ENABLE_COLORS
416 parse_color(value);
417 #else
418 {}
419 #endif
420 /* wide cursor */
421 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
422 options.wide_cursor = str2bool(value);
423 else if (strcasecmp(name, "hardware-cursor") == 0)
424 options.hardware_cursor = str2bool(value);
425 /* welcome screen list */
426 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
427 options.welcome_screen_list = str2bool(value);
428 /* visible bitrate */
429 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
430 options.visible_bitrate = str2bool(value);
431 /* timer display type */
432 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name)) {
433 g_free(options.timedisplay_type);
434 options.timedisplay_type=g_strdup(parse_timedisplay_type(value));
435 /* color definition */
436 } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
437 #ifdef ENABLE_COLORS
438 parse_color_definition(value);
439 #else
440 {}
441 #endif
442 /* list format string */
443 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
444 g_free(options.list_format);
445 options.list_format = get_format(value);
446 /* status format string */
447 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
448 g_free(options.status_format);
449 options.status_format = get_format(value);
450 /* xterm title format string */
451 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
452 g_free(options.xterm_title_format);
453 options.xterm_title_format = get_format(value);
454 } else if (!strcasecmp(CONF_LIST_WRAP, name))
455 options.list_wrap = str2bool(value);
456 else if (!strcasecmp(CONF_FIND_WRAP, name))
457 options.find_wrap = str2bool(value);
458 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
459 options.find_show_last_pattern = str2bool(value);
460 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
461 options.audible_bell = str2bool(value);
462 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
463 options.visible_bell = str2bool(value);
464 else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
465 options.bell_on_wrap = str2bool(value);
466 else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
467 options.status_message_time = atoi(value);
468 else if (!strcasecmp(CONF_XTERM_TITLE, name))
469 options.enable_xterm_title = str2bool(value);
470 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
471 #ifdef HAVE_GETMOUSE
472 options.enable_mouse = str2bool(value);
473 #else
474 {}
475 #endif
476 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
477 options.crossfade_time = atoi(value);
478 else if (!strcasecmp(CONF_SEARCH_MODE, name))
479 options.search_mode = get_search_mode(value);
480 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
481 options.hide_cursor = atoi(value);
482 else if (!strcasecmp(CONF_SEEK_TIME, name))
483 options.seek_time = atoi(value);
484 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
485 g_strfreev(options.screen_list);
486 options.screen_list = check_screen_list(value);
487 } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
488 /* the splash screen was removed */
489 } else if (!strcasecmp(CONF_HOST, name))
490 options.host = get_format(value);
491 else if (!strcasecmp(CONF_PORT, name))
492 options.port = atoi(get_format(value));
493 else if (!strcasecmp(CONF_PASSWORD, name))
494 options.password = get_format(value);
495 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
496 #ifdef ENABLE_LYRICS_SCREEN
497 options.lyrics_timeout = atoi(get_format(value));
498 #else
499 {}
500 #endif
501 else if (!strcasecmp(CONF_SCROLL, name))
502 options.scroll = str2bool(value);
503 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
504 g_free(options.scroll_sep);
505 options.scroll_sep = get_format(value);
506 } else if (!strcasecmp(CONF_DISPLAY_TIME, name))
507 #ifdef NCMPC_MINI
508 {}
509 #else
510 options.display_time = str2bool(value);
511 #endif
512 else if (!strcasecmp(CONF_JUMP_PREFIX_ONLY, name))
513 #ifdef NCMPC_MINI
514 {}
515 #else
516 options.jump_prefix_only = str2bool(value);
517 #endif
518 else
519 match_found = false;
521 if (!match_found)
522 print_error(_("Unknown configuration parameter"),
523 name);
525 return match_found;
526 }
528 static int
529 read_rc_file(char *filename)
530 {
531 FILE *file;
532 char line[MAX_LINE_LENGTH];
534 if (filename == NULL)
535 return -1;
537 file = fopen(filename, "r");
538 if (file == NULL) {
539 perror(filename);
540 return -1;
541 }
543 while (fgets(line, sizeof(line), file) != NULL) {
544 char *p = g_strchug(line);
546 if (*p != 0 && *p != COMMENT_TOKEN)
547 parse_line(g_strchomp(p));
548 }
550 fclose(file);
551 return 0;
552 }
554 int
555 check_user_conf_dir(void)
556 {
557 int retval;
558 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
560 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
561 g_free(directory);
562 return 0;
563 }
565 retval = mkdir(directory, 0755);
566 g_free(directory);
567 return retval;
568 }
570 char *
571 get_user_key_binding_filename(void)
572 {
573 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
574 }
576 int
577 read_configuration(void)
578 {
579 char *filename = NULL;
581 /* check for command line configuration file */
582 if (options.config_file)
583 filename = g_strdup(options.config_file);
585 /* check for user configuration ~/.ncmpc/config */
586 if (filename == NULL) {
587 filename = g_build_filename(g_get_home_dir(),
588 "." PACKAGE, "config", NULL);
589 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
590 g_free(filename);
591 filename = NULL;
592 }
593 }
595 /* check for global configuration SYSCONFDIR/ncmpc/config */
596 if (filename == NULL) {
597 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
598 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
599 g_free(filename);
600 filename = NULL;
601 }
602 }
604 /* load configuration */
605 if (filename) {
606 read_rc_file(filename);
607 g_free(filename);
608 filename = NULL;
609 }
611 /* check for command line key binding file */
612 if (options.key_file)
613 filename = g_strdup(options.key_file);
615 /* check for user key bindings ~/.ncmpc/keys */
616 if (filename == NULL) {
617 filename = get_user_key_binding_filename();
618 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
619 g_free(filename);
620 filename = NULL;
621 }
622 }
624 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
625 if (filename == NULL) {
626 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
627 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
628 g_free(filename);
629 filename = NULL;
630 }
631 }
633 /* load key bindings */
634 if (filename) {
635 read_rc_file(filename);
636 g_free(filename);
637 filename = NULL;
638 }
640 return 0;
641 }