1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 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"
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 <fcntl.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_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_BELL_ON_WRAP "bell-on-wrap"
58 #define CONF_STATUS_MESSAGE_TIME "status-message-time"
59 #define CONF_XTERM_TITLE "set-xterm-title"
60 #define CONF_ENABLE_MOUSE "enable-mouse"
61 #define CONF_CROSSFADE_TIME "crossfade-time"
62 #define CONF_SEARCH_MODE "search-mode"
63 #define CONF_HIDE_CURSOR "hide-cursor"
64 #define CONF_SEEK_TIME "seek-time"
65 #define CONF_SCREEN_LIST "screen-list"
66 #define CONF_TIMEDISPLAY_TYPE "timedisplay-type"
67 #define CONF_HOST "host"
68 #define CONF_PORT "port"
69 #define CONF_PASSWORD "password"
70 #define CONF_TIMEOUT "timeout"
71 #define CONF_LYRICS_TIMEOUT "lyrics-timeout"
72 #define CONF_SCROLL "scroll"
73 #define CONF_SCROLL_SEP "scroll-sep"
74 #define CONF_VISIBLE_BITRATE "visible-bitrate"
75 #define CONF_HARDWARE_CURSOR "hardware-cursor"
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"
80 #define CONF_LYRICS_SHOW_PLUGIN "lyrics-show-plugin"
81 #define CONF_TEXT_EDITOR "text-editor"
82 #define CONF_TEXT_EDITOR_ASK "text-editor-ask"
83 #define CONF_SECOND_COLUMN "second-column"
85 static bool
86 str2bool(char *str)
87 {
88 return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
89 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
90 }
92 static void
93 print_error(const char *msg, const char *input)
94 {
95 fprintf(stderr, "%s: %s ('%s')\n",
96 /* To translators: prefix for error messages */
97 _("Error"), msg, input);
98 }
100 static int
101 parse_key_value(char *str, char **end)
102 {
103 if (*str == '\'') {
104 if (str[1] == '\'' || str[2] != '\'') {
105 print_error(_("Malformed hotkey definition"), str);
106 return -1;
107 }
109 *end = str + 3;
110 return str[1];
111 } else {
112 long value = strtol(str, end, 0);
113 if (*end == str) {
114 print_error(_("Malformed hotkey definition"), str);
115 return -1;
116 }
118 return (int)value;
119 }
120 }
122 static int
123 parse_key_definition(char *str)
124 {
125 char buf[MAX_LINE_LENGTH];
126 char *p;
127 size_t len = strlen(str), i;
128 int j,key;
129 int keys[MAX_COMMAND_KEYS];
130 command_t cmd;
132 /* get the command name */
133 i=0;
134 j=0;
135 memset(buf, 0, MAX_LINE_LENGTH);
136 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
137 buf[j++] = str[i++];
138 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
139 /* the hotkey configuration contains an unknown
140 command */
141 print_error(_("Unknown command"), buf);
142 return -1;
143 }
145 /* skip whitespace */
146 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
147 i++;
149 /* get the value part */
150 memset(buf, 0, MAX_LINE_LENGTH);
151 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
152 if (*buf == 0) {
153 /* the hotkey configuration line is incomplete */
154 print_error(_("Incomplete hotkey configuration"), str);
155 return -1;
156 }
158 /* parse key values */
159 i = 0;
160 key = 0;
161 p = buf;
162 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
163 while (i < MAX_COMMAND_KEYS && *p != 0 &&
164 (key = parse_key_value(p, &p)) >= 0) {
165 keys[i++] = key;
166 while (*p==',' || *p==' ' || *p=='\t')
167 p++;
168 }
170 if (key < 0)
171 return -1;
173 return assign_keys(cmd, keys);
174 }
176 static bool
177 parse_timedisplay_type(const char *str)
178 {
179 if (strcmp(str, "elapsed") == 0)
180 return false;
181 else if (strcmp(str, "remaining") == 0)
182 return true;
183 else {
184 /* translators: ncmpc supports displaying the
185 "elapsed" or "remaining" time of a song being
186 played; in this case, the configuration file
187 contained an invalid setting */
188 print_error(_("Bad time display type"), str);
189 return false;
190 }
191 }
193 #ifdef ENABLE_COLORS
194 static char *
195 separate_value(char *p)
196 {
197 char *value;
199 value = strchr(p, '=');
200 if (value == NULL) {
201 /* an equals sign '=' was expected while parsing a
202 configuration file line */
203 fprintf(stderr, "%s\n", _("Missing '='"));
204 return NULL;
205 }
207 *value++ = 0;
209 g_strchomp(p);
210 return g_strchug(value);
211 }
213 static int
214 parse_color(char *str)
215 {
216 char *value;
218 value = separate_value(str);
219 if (value == NULL)
220 return -1;
222 return colors_assign(str, value);
223 }
225 /**
226 * Returns the first non-whitespace character after the next comma
227 * character, or the end of the string. This is used to parse comma
228 * separated values.
229 */
230 static char *
231 after_comma(char *p)
232 {
233 char *comma = strchr(p, ',');
235 if (comma != NULL) {
236 *comma++ = 0;
237 comma = g_strchug(comma);
238 } else
239 comma = p + strlen(p);
241 g_strchomp(p);
242 return comma;
243 }
245 static int
246 parse_color_definition(char *str)
247 {
248 char buf[MAX_LINE_LENGTH];
249 char *value;
250 short color, rgb[3];
252 value = separate_value(str);
253 if (value == NULL)
254 return -1;
256 /* get the command name */
257 color = colors_str2color(str);
258 if (color < 0) {
259 print_error(_("Bad color name"), buf);
260 return -1;
261 }
263 /* parse r,g,b values */
265 for (unsigned i = 0; i < 3; ++i) {
266 char *next = after_comma(value), *endptr;
267 if (*value == 0) {
268 print_error(_("Incomplete color definition"), str);
269 return -1;
270 }
272 rgb[i] = strtol(value, &endptr, 0);
273 if (endptr == value || *endptr != 0) {
274 print_error(_("Invalid number"), value);
275 return -1;
276 }
278 value = next;
279 }
281 if (*value != 0) {
282 print_error(_("Malformed color definition"), str);
283 return -1;
284 }
286 return colors_define(str, rgb[0], rgb[1], rgb[2]);
287 }
288 #endif
290 static char *
291 get_format(char *str)
292 {
293 gsize len = strlen(str);
295 if (str && str[0]=='\"' && str[len-1] == '\"') {
296 str[len - 1] = '\0';
297 str++;
298 }
300 return g_strdup(str);
301 }
303 static char **
304 check_screen_list(char *value)
305 {
306 char **tmp = g_strsplit_set(value, " \t,", 100);
307 char **screen = NULL;
308 int i = 0, j = 0;
310 while( tmp && tmp[i] ) {
311 char *name = g_ascii_strdown(tmp[i], -1);
312 if (*name != '\0') {
313 if (screen_lookup_name(name) == NULL) {
314 /* an unknown screen name was specified in the
315 configuration file */
316 print_error(_("Unknown screen name"), name);
317 free(name);
318 } else {
319 screen = g_realloc(screen, (j+2)*sizeof(char *));
320 screen[j++] = name;
321 screen[j] = NULL;
322 }
323 }
324 i++;
325 }
326 g_strfreev(tmp);
327 if( screen == NULL )
328 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
330 return screen;
331 }
333 static int
334 get_search_mode(char *value)
335 {
336 char * test;
337 int mode;
338 mode= strtol(value, &test, 10);
339 if (*test == '\0')
340 {
341 if (0 <= mode && mode <= 4)
342 return mode;
343 else
344 {
345 print_error(_("Invalid search mode"),value);
346 return 0;
347 }
348 }
349 else
350 {
351 for (int i = 0; value[i] != '\0'; i++)
352 value[i] = tolower(value[i]);
354 // TODO: modify screen_search so that its own list of modes can be used
355 // for comparison instead of specifying them here
356 if (strcmp(value, "title") == 0)
357 return 0;
358 else if (strcmp(value, "artist") == 0)
359 return 1;
360 else if (strcmp(value, "album") == 0)
361 return 2;
362 else if (strcmp(value, "filename") == 0)
363 return 3;
364 else if (strcmp(value, "artist+album") == 0)
365 return 4;
366 else
367 {
368 print_error(_("Unknown search mode"),value);
369 return 0;
370 }
371 }
372 }
374 static bool
375 parse_line(char *line)
376 {
377 size_t len = strlen(line), i = 0, j = 0;
378 char name[MAX_LINE_LENGTH];
379 char value[MAX_LINE_LENGTH];
380 bool match_found;
382 /* get the name part */
383 while (i < len && line[i] != '=' && !g_ascii_isspace(line[i]))
384 name[j++] = line[i++];
386 name[j] = '\0';
388 /* skip '=' and whitespace */
389 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
390 i++;
392 /* get the value part */
393 j = 0;
394 while (i < len)
395 value[j++] = line[i++];
396 value[j] = '\0';
398 match_found = true;
400 /* key definition */
401 if (!strcasecmp(CONF_KEY_DEFINITION, name))
402 parse_key_definition(value);
403 /* enable colors */
404 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
405 #ifdef ENABLE_COLORS
406 options.enable_colors = str2bool(value);
407 #else
408 {}
409 #endif
410 else if (!strcasecmp(CONF_SCROLL_OFFSET, name))
411 options.scroll_offset = atoi(value);
412 /* auto center */
413 else if (!strcasecmp(CONF_AUTO_CENTER, name))
414 options.auto_center = str2bool(value);
415 /* color assignment */
416 else if (!strcasecmp(CONF_COLOR, name))
417 #ifdef ENABLE_COLORS
418 parse_color(value);
419 #else
420 {}
421 #endif
422 /* wide cursor */
423 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
424 options.wide_cursor = str2bool(value);
425 else if (strcasecmp(name, CONF_HARDWARE_CURSOR) == 0)
426 options.hardware_cursor = str2bool(value);
427 /* welcome screen list */
428 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
429 options.welcome_screen_list = str2bool(value);
430 /* visible bitrate */
431 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
432 options.visible_bitrate = str2bool(value);
433 /* timer display type */
434 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name))
435 options.display_remaining_time = parse_timedisplay_type(value);
436 /* color definition */
437 else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
438 #ifdef ENABLE_COLORS
439 parse_color_definition(value);
440 #else
441 {}
442 #endif
443 /* list format string */
444 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
445 g_free(options.list_format);
446 options.list_format = get_format(value);
447 /* status format string */
448 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
449 g_free(options.status_format);
450 options.status_format = get_format(value);
451 /* xterm title format string */
452 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
453 g_free(options.xterm_title_format);
454 options.xterm_title_format = get_format(value);
455 } else if (!strcasecmp(CONF_LIST_WRAP, name))
456 options.list_wrap = str2bool(value);
457 else if (!strcasecmp(CONF_FIND_WRAP, name))
458 options.find_wrap = str2bool(value);
459 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
460 options.find_show_last_pattern = str2bool(value);
461 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
462 options.audible_bell = str2bool(value);
463 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
464 options.visible_bell = str2bool(value);
465 else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
466 options.bell_on_wrap = str2bool(value);
467 else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
468 options.status_message_time = atoi(value);
469 else if (!strcasecmp(CONF_XTERM_TITLE, name))
470 options.enable_xterm_title = str2bool(value);
471 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
472 #ifdef HAVE_GETMOUSE
473 options.enable_mouse = str2bool(value);
474 #else
475 {}
476 #endif
477 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
478 options.crossfade_time = atoi(value);
479 else if (!strcasecmp(CONF_SEARCH_MODE, name))
480 options.search_mode = get_search_mode(value);
481 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
482 options.hide_cursor = atoi(value);
483 else if (!strcasecmp(CONF_SEEK_TIME, name))
484 options.seek_time = atoi(value);
485 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
486 g_strfreev(options.screen_list);
487 options.screen_list = check_screen_list(value);
488 } else if (!strcasecmp(CONF_HOST, name))
489 options.host = get_format(value);
490 else if (!strcasecmp(CONF_PORT, name))
491 options.port = atoi(get_format(value));
492 else if (!strcasecmp(CONF_PASSWORD, name))
493 options.password = get_format(value);
494 else if (!strcasecmp(CONF_TIMEOUT, name))
495 options.timeout_ms = atoi(get_format(value))
496 * 1000 /* seconds -> milliseconds */;
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 if (!strcasecmp(CONF_LYRICS_SHOW_PLUGIN, name))
527 #ifdef ENABLE_LYRICS_SCREEN
528 options.lyrics_show_plugin = str2bool(value);
529 #else
530 {}
531 #endif
532 else if (!strcasecmp(name, CONF_TEXT_EDITOR))
533 #ifdef ENABLE_LYRICS_SCREEN
534 {
535 g_free(options.text_editor);
536 options.text_editor = get_format(value);
537 }
538 #else
539 {}
540 #endif
541 else if (!strcasecmp(name, CONF_TEXT_EDITOR_ASK))
542 #ifdef ENABLE_LYRICS_SCREEN
543 options.text_editor_ask = str2bool(value);
544 #else
545 {}
546 #endif
547 else if (!strcasecmp(CONF_SECOND_COLUMN, name))
548 #ifdef NCMPC_MINI
549 {}
550 #else
551 options.second_column = str2bool(value);
552 #endif
553 else
554 match_found = false;
556 if (!match_found)
557 print_error(_("Unknown configuration parameter"), name);
559 return match_found;
560 }
562 static int
563 read_rc_file(char *filename)
564 {
565 FILE *file;
566 char line[MAX_LINE_LENGTH];
568 if (filename == NULL)
569 return -1;
571 file = fopen(filename, "r");
572 if (file == NULL) {
573 perror(filename);
574 return -1;
575 }
577 while (fgets(line, sizeof(line), file) != NULL) {
578 char *p = g_strchug(line);
580 if (*p != 0 && *p != COMMENT_TOKEN)
581 parse_line(g_strchomp(p));
582 }
584 fclose(file);
585 return 0;
586 }
588 int
589 check_user_conf_dir(void)
590 {
591 int retval;
592 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
594 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
595 g_free(directory);
596 return 0;
597 }
599 retval = g_mkdir(directory, 0755);
600 g_free(directory);
601 return retval;
602 }
604 char *
605 build_user_conf_filename(void)
606 {
607 return g_build_filename(g_get_home_dir(), "." PACKAGE, "config", NULL);
608 }
610 char *
611 build_system_conf_filename(void)
612 {
613 return g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
614 }
616 char *
617 build_user_key_binding_filename(void)
618 {
619 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
620 }
622 void
623 read_configuration(void)
624 {
625 char *filename = NULL;
627 /* check for command line configuration file */
628 if (options.config_file)
629 filename = g_strdup(options.config_file);
631 /* check for user configuration ~/.ncmpc/config */
632 if (filename == NULL) {
633 filename = build_user_conf_filename();
634 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
635 g_free(filename);
636 filename = NULL;
637 }
638 }
640 /* check for global configuration SYSCONFDIR/ncmpc/config */
641 if (filename == NULL) {
642 filename = build_system_conf_filename();
643 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
644 g_free(filename);
645 filename = NULL;
646 }
647 }
649 /* load configuration */
650 if (filename) {
651 read_rc_file(filename);
652 g_free(filename);
653 filename = NULL;
654 }
656 /* check for command line key binding file */
657 if (options.key_file)
658 filename = g_strdup(options.key_file);
660 /* check for user key bindings ~/.ncmpc/keys */
661 if (filename == NULL) {
662 filename = build_user_key_binding_filename();
663 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
664 g_free(filename);
665 filename = NULL;
666 }
667 }
669 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
670 if (filename == NULL) {
671 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
672 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
673 g_free(filename);
674 filename = NULL;
675 }
676 }
678 /* load key bindings */
679 if (filename) {
680 read_rc_file(filename);
681 g_free(filename);
682 filename = NULL;
683 }
684 }