1dc1a4333c9d99b918643854ca9b6ee5af083756
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_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"
79 static bool
80 str2bool(char *str)
81 {
82 return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
83 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
84 }
86 static void
87 print_error(const char *msg, const char *input)
88 {
89 fprintf(stderr, "%s: %s ('%s')\n",
90 /* To translators: prefix for error messages */
91 _("Error"), msg, input);
92 }
94 static int
95 parse_key_value(char *str, char **end)
96 {
97 if (*str == '\'') {
98 if (str[1] == '\'' || str[2] != '\'') {
99 print_error(_("Malformed hotkey definition"), str);
100 return -1;
101 }
103 *end = str + 3;
104 return str[1];
105 } else {
106 long value = strtol(str, end, 0);
107 if (*end == str) {
108 print_error(_("Malformed hotkey definition"), str);
109 return -1;
110 }
112 return (int)value;
113 }
114 }
116 static int
117 parse_key_definition(char *str)
118 {
119 char buf[MAX_LINE_LENGTH];
120 char *p;
121 size_t len = strlen(str), i;
122 int j,key;
123 int keys[MAX_COMMAND_KEYS];
124 command_t cmd;
126 /* get the command name */
127 i=0;
128 j=0;
129 memset(buf, 0, MAX_LINE_LENGTH);
130 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
131 buf[j++] = str[i++];
132 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
133 /* the hotkey configuration contains an unknown
134 command */
135 print_error(_("Unknown command"), buf);
136 return -1;
137 }
139 /* skip whitespace */
140 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
141 i++;
143 /* get the value part */
144 memset(buf, 0, MAX_LINE_LENGTH);
145 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
146 if (*buf == 0) {
147 /* the hotkey configuration line is incomplete */
148 print_error(_("Incomplete hotkey configuration"), str);
149 return -1;
150 }
152 /* parse key values */
153 i = 0;
154 key = 0;
155 p = buf;
156 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
157 while (i < MAX_COMMAND_KEYS && *p != 0 &&
158 (key = parse_key_value(p, &p)) >= 0) {
159 keys[i++] = key;
160 while (*p==',' || *p==' ' || *p=='\t')
161 p++;
162 }
164 if (key < 0)
165 return -1;
167 return assign_keys(cmd, keys);
168 }
170 static const char *
171 parse_timedisplay_type(const char *str)
172 {
173 if (!strcmp(str,"elapsed") || !strcmp(str,"remaining"))
174 return str;
175 else {
176 /* translators: ncmpc supports displaying the
177 "elapsed" or "remaining" time of a song being
178 played; in this case, the configuration file
179 contained an invalid setting */
180 print_error(_("Bad time display type"), str);
181 return DEFAULT_TIMEDISPLAY_TYPE;
182 }
183 }
185 #ifdef ENABLE_COLORS
186 static char *
187 separate_value(char *p)
188 {
189 char *value;
191 value = strchr(p, '=');
192 if (value == NULL) {
193 /* an equals sign '=' was expected while parsing a
194 configuration file line */
195 fprintf(stderr, "%s\n", _("Missing '='"));
196 return NULL;
197 }
199 *value++ = 0;
201 g_strchomp(p);
202 return g_strchug(value);
203 }
205 static int
206 parse_color(char *str)
207 {
208 char *value;
210 value = separate_value(str);
211 if (value == NULL)
212 return -1;
214 return colors_assign(str, value);
215 }
217 /**
218 * Returns the first non-whitespace character after the next comma
219 * character, or the end of the string. This is used to parse comma
220 * separated values.
221 */
222 static char *
223 after_comma(char *p)
224 {
225 char *comma = strchr(p, ',');
227 if (comma != NULL) {
228 *comma++ = 0;
229 comma = g_strchug(comma);
230 } else
231 comma = p + strlen(p);
233 g_strchomp(p);
234 return comma;
235 }
237 static int
238 parse_color_definition(char *str)
239 {
240 char buf[MAX_LINE_LENGTH];
241 char *value;
242 short color, rgb[3];
244 value = separate_value(str);
245 if (value == NULL)
246 return -1;
248 /* get the command name */
249 color = colors_str2color(str);
250 if (color < 0) {
251 print_error(_("Bad color name"), buf);
252 return -1;
253 }
255 /* parse r,g,b values */
257 for (unsigned i = 0; i < 3; ++i) {
258 char *next = after_comma(value), *endptr;
259 if (*value == 0) {
260 print_error(_("Incomplete color definition"), str);
261 return -1;
262 }
264 rgb[i] = strtol(value, &endptr, 0);
265 if (endptr == value || *endptr != 0) {
266 print_error(_("Invalid number"), value);
267 return -1;
268 }
270 value = next;
271 }
273 if (*value != 0) {
274 print_error(_("Malformed color definition"), str);
275 return -1;
276 }
278 return colors_define(str, rgb[0], rgb[1], rgb[2]);
279 }
280 #endif
282 static char *
283 get_format(char *str)
284 {
285 gsize len = strlen(str);
287 if (str && str[0]=='\"' && str[len-1] == '\"') {
288 str[len - 1] = '\0';
289 str++;
290 }
292 return g_strdup(str);
293 }
295 static char **
296 check_screen_list(char *value)
297 {
298 char **tmp = g_strsplit_set(value, " \t,", 100);
299 char **screen = NULL;
300 int i,j;
302 i=0;
303 j=0;
304 while( tmp && tmp[i] ) {
305 char *name = g_ascii_strdown(tmp[i], -1);
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 i++;
317 }
318 g_strfreev(tmp);
319 if( screen == NULL )
320 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
322 return screen;
323 }
325 static bool
326 parse_line(char *line)
327 {
328 size_t len = strlen(line), i = 0, j;
329 char name[MAX_LINE_LENGTH];
330 char value[MAX_LINE_LENGTH];
331 bool match_found;
333 /* get the name part */
334 j = 0;
335 while (i < len && line[i] != '=' &&
336 !g_ascii_isspace(line[i])) {
337 name[j++] = line[i++];
338 }
340 name[j] = '\0';
342 /* skip '=' and whitespace */
343 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
344 i++;
346 /* get the value part */
347 j = 0;
348 while (i < len)
349 value[j++] = line[i++];
350 value[j] = '\0';
352 match_found = true;
354 /* key definition */
355 if (!strcasecmp(CONF_KEY_DEFINITION, name))
356 parse_key_definition(value);
357 /* enable colors */
358 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
359 #ifdef ENABLE_COLORS
360 options.enable_colors = str2bool(value);
361 #else
362 {}
363 #endif
364 /* auto center */
365 else if (!strcasecmp(CONF_AUTO_CENTER, name))
366 options.auto_center = str2bool(value);
367 /* color assignment */
368 else if (!strcasecmp(CONF_COLOR, name))
369 #ifdef ENABLE_COLORS
370 parse_color(value);
371 #else
372 {}
373 #endif
374 /* wide cursor */
375 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
376 options.wide_cursor = str2bool(value);
377 /* welcome screen list */
378 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
379 options.welcome_screen_list = str2bool(value);
380 /* visible bitrate */
381 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
382 options.visible_bitrate = str2bool(value);
383 /* timer display type */
384 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name)) {
385 g_free(options.timedisplay_type);
386 options.timedisplay_type=g_strdup(parse_timedisplay_type(value));
387 /* color definition */
388 } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
389 #ifdef ENABLE_COLORS
390 parse_color_definition(value);
391 #else
392 {}
393 #endif
394 /* list format string */
395 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
396 g_free(options.list_format);
397 options.list_format = get_format(value);
398 /* status format string */
399 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
400 g_free(options.status_format);
401 options.status_format = get_format(value);
402 /* xterm title format string */
403 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
404 g_free(options.xterm_title_format);
405 options.xterm_title_format = get_format(value);
406 } else if (!strcasecmp(CONF_LIST_WRAP, name))
407 options.list_wrap = str2bool(value);
408 else if (!strcasecmp(CONF_FIND_WRAP, name))
409 options.find_wrap = str2bool(value);
410 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
411 options.find_show_last_pattern = str2bool(value);
412 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
413 options.audible_bell = str2bool(value);
414 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
415 options.visible_bell = str2bool(value);
416 else if (!strcasecmp(CONF_BELL_ON_WRAP, name))
417 options.bell_on_wrap = str2bool(value);
418 else if (!strcasecmp(CONF_STATUS_MESSAGE_TIME, name))
419 options.status_message_time = atoi(value);
420 else if (!strcasecmp(CONF_XTERM_TITLE, name))
421 options.enable_xterm_title = str2bool(value);
422 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
423 #ifdef HAVE_GETMOUSE
424 options.enable_mouse = str2bool(value);
425 #else
426 {}
427 #endif
428 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
429 options.crossfade_time = atoi(value);
430 else if (!strcasecmp(CONF_SEARCH_MODE, name))
431 options.search_mode = atoi(value);
432 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
433 options.hide_cursor = atoi(value);
434 else if (!strcasecmp(CONF_SEEK_TIME, name))
435 options.seek_time = atoi(value);
436 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
437 g_strfreev(options.screen_list);
438 options.screen_list = check_screen_list(value);
439 } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
440 /* the splash screen was removed */
441 } else if (!strcasecmp(CONF_HOST, name))
442 options.host = get_format(value);
443 else if (!strcasecmp(CONF_PORT, name))
444 options.port = atoi(get_format(value));
445 else if (!strcasecmp(CONF_PASSWORD, name))
446 options.password = get_format(value);
447 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
448 #ifdef ENABLE_LYRICS_SCREEN
449 options.lyrics_timeout = atoi(get_format(value));
450 #else
451 {}
452 #endif
453 else if (!strcasecmp(CONF_SCROLL, name))
454 options.scroll = str2bool(value);
455 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
456 g_free(options.scroll_sep);
457 options.scroll_sep = get_format(value);
458 } else if (!strcasecmp(CONF_DISPLAY_TIME, name))
459 #ifdef NCMPC_MINI
460 {}
461 #else
462 options.display_time = str2bool(value);
463 #endif
464 else
465 match_found = false;
467 if (!match_found)
468 print_error(_("Unknown configuration parameter"),
469 name);
471 return match_found;
472 }
474 static int
475 read_rc_file(char *filename)
476 {
477 FILE *file;
478 char line[MAX_LINE_LENGTH];
480 if (filename == NULL)
481 return -1;
483 file = fopen(filename, "r");
484 if (file == NULL) {
485 perror(filename);
486 return -1;
487 }
489 while (fgets(line, sizeof(line), file) != NULL) {
490 char *p = g_strchug(line);
492 if (*p != 0 && *p != COMMENT_TOKEN)
493 parse_line(g_strchomp(p));
494 }
496 fclose(file);
497 return 0;
498 }
500 int
501 check_user_conf_dir(void)
502 {
503 int retval;
504 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
506 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
507 g_free(directory);
508 return 0;
509 }
511 retval = mkdir(directory, 0755);
512 g_free(directory);
513 return retval;
514 }
516 char *
517 get_user_key_binding_filename(void)
518 {
519 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
520 }
522 int
523 read_configuration(void)
524 {
525 char *filename = NULL;
527 /* check for command line configuration file */
528 if (options.config_file)
529 filename = g_strdup(options.config_file);
531 /* check for user configuration ~/.ncmpc/config */
532 if (filename == NULL) {
533 filename = g_build_filename(g_get_home_dir(),
534 "." PACKAGE, "config", NULL);
535 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
536 g_free(filename);
537 filename = NULL;
538 }
539 }
541 /* check for global configuration SYSCONFDIR/ncmpc/config */
542 if (filename == NULL) {
543 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
544 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
545 g_free(filename);
546 filename = NULL;
547 }
548 }
550 /* load configuration */
551 if (filename) {
552 read_rc_file(filename);
553 g_free(filename);
554 filename = NULL;
555 }
557 /* check for command line key binding file */
558 if (options.key_file)
559 filename = g_strdup(options.key_file);
561 /* check for user key bindings ~/.ncmpc/keys */
562 if (filename == NULL) {
563 filename = get_user_key_binding_filename();
564 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
565 g_free(filename);
566 filename = NULL;
567 }
568 }
570 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
571 if (filename == NULL) {
572 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
573 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
574 g_free(filename);
575 filename = NULL;
576 }
577 }
579 /* load key bindings */
580 if (filename) {
581 read_rc_file(filename);
582 g_free(filename);
583 filename = NULL;
584 }
586 return 0;
587 }