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_ENABLE_BELL "enable-bell"
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_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 (screen_lookup_name(name) == NULL) {
306 /* an unknown screen name was specified in the
307 configuration file */
308 print_error(_("Unknown screen name"), name);
309 free(name);
310 } else {
311 screen = g_realloc(screen, (j+2)*sizeof(char *));
312 screen[j++] = name;
313 screen[j] = NULL;
314 }
315 i++;
316 }
317 g_strfreev(tmp);
318 if( screen == NULL )
319 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
321 return screen;
322 }
324 static bool
325 parse_line(char *line)
326 {
327 size_t len = strlen(line), i = 0, j;
328 char name[MAX_LINE_LENGTH];
329 char value[MAX_LINE_LENGTH];
330 bool match_found;
332 /* get the name part */
333 j = 0;
334 while (i < len && line[i] != '=' &&
335 !g_ascii_isspace(line[i])) {
336 name[j++] = line[i++];
337 }
339 name[j] = '\0';
341 /* skip '=' and whitespace */
342 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
343 i++;
345 /* get the value part */
346 j = 0;
347 while (i < len)
348 value[j++] = line[i++];
349 value[j] = '\0';
351 match_found = true;
353 /* key definition */
354 if (!strcasecmp(CONF_KEY_DEFINITION, name))
355 parse_key_definition(value);
356 /* enable colors */
357 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
358 #ifdef ENABLE_COLORS
359 options.enable_colors = str2bool(value);
360 #else
361 {}
362 #endif
363 /* auto center */
364 else if (!strcasecmp(CONF_AUTO_CENTER, name))
365 options.auto_center = str2bool(value);
366 /* color assignment */
367 else if (!strcasecmp(CONF_COLOR, name))
368 #ifdef ENABLE_COLORS
369 parse_color(value);
370 #else
371 {}
372 #endif
373 /* wide cursor */
374 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
375 options.wide_cursor = str2bool(value);
376 /* welcome screen list */
377 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
378 options.welcome_screen_list = str2bool(value);
379 /* visible bitrate */
380 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
381 options.visible_bitrate = str2bool(value);
382 /* timer display type */
383 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name)) {
384 g_free(options.timedisplay_type);
385 options.timedisplay_type=g_strdup(parse_timedisplay_type(value));
386 /* color definition */
387 } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
388 #ifdef ENABLE_COLORS
389 parse_color_definition(value);
390 #else
391 {}
392 #endif
393 /* list format string */
394 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
395 g_free(options.list_format);
396 options.list_format = get_format(value);
397 /* status format string */
398 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
399 g_free(options.status_format);
400 options.status_format = get_format(value);
401 /* xterm title format string */
402 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
403 g_free(options.xterm_title_format);
404 options.xterm_title_format = get_format(value);
405 } else if (!strcasecmp(CONF_LIST_WRAP, name))
406 options.list_wrap = str2bool(value);
407 else if (!strcasecmp(CONF_FIND_WRAP, name))
408 options.find_wrap = str2bool(value);
409 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
410 options.find_show_last_pattern = str2bool(value);
411 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
412 options.audible_bell = str2bool(value);
413 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
414 options.visible_bell = str2bool(value);
415 else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
416 options.bell_on_wrap = str2bool(value);
417 else if (!strcasecmp(CONF_XTERM_TITLE, name))
418 options.enable_xterm_title = str2bool(value);
419 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
420 #ifdef HAVE_GETMOUSE
421 options.enable_mouse = str2bool(value);
422 #else
423 {}
424 #endif
425 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
426 options.crossfade_time = atoi(value);
427 else if (!strcasecmp(CONF_SEARCH_MODE, name))
428 options.search_mode = atoi(value);
429 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
430 options.hide_cursor = atoi(value);
431 else if (!strcasecmp(CONF_SEEK_TIME, name))
432 options.seek_time = atoi(value);
433 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
434 g_strfreev(options.screen_list);
435 options.screen_list = check_screen_list(value);
436 } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
437 /* the splash screen was removed */
438 } else if (!strcasecmp(CONF_HOST, name))
439 options.host = get_format(value);
440 else if (!strcasecmp(CONF_PORT, name))
441 options.port = atoi(get_format(value));
442 else if (!strcasecmp(CONF_PASSWORD, name))
443 options.password = get_format(value);
444 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
445 #ifdef ENABLE_LYRICS_SCREEN
446 options.lyrics_timeout = atoi(get_format(value));
447 #else
448 {}
449 #endif
450 else if (!strcasecmp(CONF_SCROLL, name))
451 options.scroll = str2bool(value);
452 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
453 g_free(options.scroll_sep);
454 options.scroll_sep = get_format(value);
455 } else if (!strcasecmp(CONF_DISPLAY_TIME, name))
456 #ifdef NCMPC_MINI
457 {}
458 #else
459 options.display_time = str2bool(value);
460 #endif
461 else
462 match_found = false;
464 if (!match_found)
465 print_error(_("Unknown configuration parameter"),
466 name);
468 return match_found;
469 }
471 static int
472 read_rc_file(char *filename)
473 {
474 FILE *file;
475 char line[MAX_LINE_LENGTH];
477 if (filename == NULL)
478 return -1;
480 file = fopen(filename, "r");
481 if (file == NULL) {
482 perror(filename);
483 return -1;
484 }
486 while (fgets(line, sizeof(line), file) != NULL) {
487 char *p = g_strchug(line);
489 if (*p != 0 && *p != COMMENT_TOKEN)
490 parse_line(g_strchomp(p));
491 }
493 fclose(file);
494 return 0;
495 }
497 int
498 check_user_conf_dir(void)
499 {
500 int retval;
501 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
503 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
504 g_free(directory);
505 return 0;
506 }
508 retval = mkdir(directory, 0755);
509 g_free(directory);
510 return retval;
511 }
513 char *
514 get_user_key_binding_filename(void)
515 {
516 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
517 }
519 int
520 read_configuration(void)
521 {
522 char *filename = NULL;
524 /* check for command line configuration file */
525 if (options.config_file)
526 filename = g_strdup(options.config_file);
528 /* check for user configuration ~/.ncmpc/config */
529 if (filename == NULL) {
530 filename = g_build_filename(g_get_home_dir(),
531 "." PACKAGE, "config", NULL);
532 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
533 g_free(filename);
534 filename = NULL;
535 }
536 }
538 /* check for global configuration SYSCONFDIR/ncmpc/config */
539 if (filename == NULL) {
540 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
541 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
542 g_free(filename);
543 filename = NULL;
544 }
545 }
547 /* load configuration */
548 if (filename) {
549 read_rc_file(filename);
550 g_free(filename);
551 filename = NULL;
552 }
554 /* check for command line key binding file */
555 if (options.key_file)
556 filename = g_strdup(options.key_file);
558 /* check for user key bindings ~/.ncmpc/keys */
559 if (filename == NULL) {
560 filename = get_user_key_binding_filename();
561 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
562 g_free(filename);
563 filename = NULL;
564 }
565 }
567 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
568 if (filename == NULL) {
569 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
570 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
571 g_free(filename);
572 filename = NULL;
573 }
574 }
576 /* load key bindings */
577 if (filename) {
578 read_rc_file(filename);
579 g_free(filename);
580 filename = NULL;
581 }
583 return 0;
584 }