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