1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include "conf.h"
20 #include "config.h"
21 #include "defaults.h"
22 #include "i18n.h"
23 #include "command.h"
24 #include "colors.h"
25 #include "screen_list.h"
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <glib.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_AUTO_CENTER "auto-center"
44 #define CONF_WIDE_CURSOR "wide-cursor"
45 #define CONF_ENABLE_BELL "enable-bell"
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_XTERM_TITLE "set-xterm-title"
58 #define CONF_ENABLE_MOUSE "enable-mouse"
59 #define CONF_CROSSFADE_TIME "crossfade-time"
60 #define CONF_SEARCH_MODE "search-mode"
61 #define CONF_HIDE_CURSOR "hide-cursor"
62 #define CONF_SEEK_TIME "seek-time"
63 #define CONF_SCREEN_LIST "screen-list"
64 #define CONF_TIMEDISPLAY_TYPE "timedisplay-type"
65 #define CONF_HOST "host"
66 #define CONF_PORT "port"
67 #define CONF_PASSWORD "password"
68 #define CONF_LYRICS_TIMEOUT "lyrics-timeout"
69 #define CONF_SHOW_SPLASH "show-splash"
70 #define CONF_SCROLL "scroll"
71 #define CONF_SCROLL_SEP "scroll-sep"
72 #define CONF_VISIBLE_BITRATE "visible-bitrate"
73 #define CONF_WELCOME_SCREEN_LIST "welcome-screen-list"
75 typedef enum {
76 KEY_PARSER_UNKNOWN,
77 KEY_PARSER_CHAR,
78 KEY_PARSER_DEC,
79 KEY_PARSER_HEX,
80 KEY_PARSER_DONE
81 } key_parser_state_t;
83 static bool
84 str2bool(char *str)
85 {
86 return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
87 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
88 }
90 static void
91 print_error(const char *msg, const char *input)
92 {
93 fprintf(stderr, "%s: %s ('%s')\n",
94 /* To translators: prefix for error messages */
95 _("Error"), msg, input);
96 }
98 static int
99 parse_key_value(char *str, size_t len, char **end)
100 {
101 size_t i;
102 int value;
103 key_parser_state_t state;
105 i=0;
106 value=0;
107 state=KEY_PARSER_UNKNOWN;
108 *end = str;
110 while (i < len && state != KEY_PARSER_DONE) {
111 int next = 0;
112 int c = str[i];
114 if( i+1<len )
115 next = str[i+1];
117 switch(state) {
118 case KEY_PARSER_UNKNOWN:
119 if( c=='\'' )
120 state = KEY_PARSER_CHAR;
121 else if( c=='0' && next=='x' )
122 state = KEY_PARSER_HEX;
123 else if( isdigit(c) )
124 state = KEY_PARSER_DEC;
125 else {
126 print_error(_("Unsupported key definition"),
127 str);
128 return -1;
129 }
130 break;
131 case KEY_PARSER_CHAR:
132 if( next!='\'' ) {
133 print_error(_("Unsupported key definition"),
134 str);
135 return -1;
136 }
137 value = c;
138 *end = str+i+2;
139 state = KEY_PARSER_DONE;
140 break;
141 case KEY_PARSER_DEC:
142 value = (int) strtol(str+(i-1), end, 10);
143 state = KEY_PARSER_DONE;
144 break;
145 case KEY_PARSER_HEX:
146 if( !isdigit(next) ) {
147 print_error(_("Digit expected after 0x"), str);
148 return -1;
149 }
150 value = (int) strtol(str+(i+1), end, 16);
151 state = KEY_PARSER_DONE;
152 break;
153 case KEY_PARSER_DONE:
154 break;
155 }
156 i++;
157 }
159 if( *end> str+len )
160 *end = str+len;
162 return value;
163 }
165 static int
166 parse_key_definition(char *str)
167 {
168 char buf[MAX_LINE_LENGTH];
169 char *p, *end;
170 size_t len = strlen(str), i;
171 int j,key;
172 int keys[MAX_COMMAND_KEYS];
173 command_t cmd;
175 /* get the command name */
176 i=0;
177 j=0;
178 memset(buf, 0, MAX_LINE_LENGTH);
179 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
180 buf[j++] = str[i++];
181 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
182 print_error(_("Unknown key command"), buf);
183 return -1;
184 }
186 /* skip whitespace */
187 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
188 i++;
190 /* get the value part */
191 memset(buf, 0, MAX_LINE_LENGTH);
192 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
193 len = strlen(buf);
194 if (len == 0) {
195 print_error(_("Incomplete key definition"), str);
196 return -1;
197 }
199 /* parse key values */
200 i = 0;
201 key = 0;
202 len = strlen(buf);
203 p = buf;
204 end = buf+len;
205 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
206 while (i < MAX_COMMAND_KEYS && p < end &&
207 (key = parse_key_value(p, len + 1, &p)) >= 0) {
208 keys[i++] = key;
209 while (p < end && (*p==',' || *p==' ' || *p=='\t'))
210 p++;
211 len = strlen(p);
212 }
214 if (key < 0) {
215 print_error(_("Bad key definition"), str);
216 return -1;
217 }
219 return assign_keys(cmd, keys);
220 }
222 static const char *
223 parse_timedisplay_type(const char *str)
224 {
225 if (!strcmp(str,"elapsed") || !strcmp(str,"remaining"))
226 return str;
227 else {
228 print_error(_("Bad time display type"), str);
229 return DEFAULT_TIMEDISPLAY_TYPE;
230 }
231 }
233 #ifdef ENABLE_COLORS
234 static char *
235 separate_value(char *p)
236 {
237 char *value;
239 value = strchr(p, '=');
240 if (value == NULL) {
241 fprintf(stderr, "%s\n", _("Missing '='"));
242 return NULL;
243 }
245 *value++ = 0;
247 g_strchomp(p);
248 return g_strchug(value);
249 }
251 static int
252 parse_color(char *str)
253 {
254 char *value;
256 value = separate_value(str);
257 if (value == NULL)
258 return -1;
260 return colors_assign(str, value);
261 }
263 /**
264 * Returns the first non-whitespace character after the next comma
265 * character, or the end of the string. This is used to parse comma
266 * separated values.
267 */
268 static char *
269 after_comma(char *p)
270 {
271 char *comma = strchr(p, ',');
273 if (comma != NULL) {
274 *comma++ = 0;
275 comma = g_strchug(comma);
276 } else
277 comma = p + strlen(p);
279 g_strchomp(p);
280 return comma;
281 }
283 static int
284 parse_color_definition(char *str)
285 {
286 char buf[MAX_LINE_LENGTH];
287 char *value;
288 short color, rgb[3];
290 value = separate_value(str);
291 if (value == NULL)
292 return -1;
294 /* get the command name */
295 color = colors_str2color(str);
296 if (color < 0) {
297 print_error(_("Bad color"), buf);
298 return -1;
299 }
301 /* parse r,g,b values */
303 for (unsigned i = 0; i < 3; ++i) {
304 char *next = after_comma(value), *endptr;
305 if (*value == 0) {
306 print_error(_("Incomplete color definition"), str);
307 return -1;
308 }
310 rgb[i] = strtol(value, &endptr, 0);
311 if (endptr == value || *endptr != 0) {
312 print_error(_("Invalid number"), value);
313 return -1;
314 }
316 value = next;
317 }
319 if (*value != 0) {
320 print_error(_("Bad color definition"), str);
321 return -1;
322 }
324 return colors_define(str, rgb[0], rgb[1], rgb[2]);
325 }
326 #endif
328 static char *
329 get_format(char *str)
330 {
331 gsize len = strlen(str);
333 if (str && str[0]=='\"' && str[len-1] == '\"') {
334 str[len - 1] = '\0';
335 str++;
336 }
338 return g_strdup(str);
339 }
341 static char **
342 check_screen_list(char *value)
343 {
344 char **tmp = g_strsplit_set(value, " \t,", 100);
345 char **screen = NULL;
346 int i,j;
348 i=0;
349 j=0;
350 while( tmp && tmp[i] ) {
351 char *name = g_ascii_strdown(tmp[i], -1);
352 if (screen_lookup_name(name) == NULL) {
353 print_error(_("Unsupported screen"), name);
354 free(name);
355 } else {
356 screen = g_realloc(screen, (j+2)*sizeof(char *));
357 screen[j++] = name;
358 screen[j] = NULL;
359 }
360 i++;
361 }
362 g_strfreev(tmp);
363 if( screen == NULL )
364 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
366 return screen;
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 /* auto center */
409 else if (!strcasecmp(CONF_AUTO_CENTER, name))
410 options.auto_center = str2bool(value);
411 /* color assignment */
412 else if (!strcasecmp(CONF_COLOR, name))
413 #ifdef ENABLE_COLORS
414 parse_color(value);
415 #else
416 {}
417 #endif
418 /* wide cursor */
419 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
420 options.wide_cursor = str2bool(value);
421 /* welcome screen list */
422 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
423 options.welcome_screen_list = str2bool(value);
424 /* visible bitrate */
425 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
426 options.visible_bitrate = str2bool(value);
427 /* timer display type */
428 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name)) {
429 g_free(options.timedisplay_type);
430 options.timedisplay_type=g_strdup(parse_timedisplay_type(value));
431 /* color definition */
432 } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
433 #ifdef ENABLE_COLORS
434 parse_color_definition(value);
435 #else
436 {}
437 #endif
438 /* list format string */
439 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
440 g_free(options.list_format);
441 options.list_format = get_format(value);
442 /* status format string */
443 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
444 g_free(options.status_format);
445 options.status_format = get_format(value);
446 /* xterm title format string */
447 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
448 g_free(options.xterm_title_format);
449 options.xterm_title_format = get_format(value);
450 } else if (!strcasecmp(CONF_LIST_WRAP, name))
451 options.list_wrap = str2bool(value);
452 else if (!strcasecmp(CONF_FIND_WRAP, name))
453 options.find_wrap = str2bool(value);
454 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
455 options.find_show_last_pattern = str2bool(value);
456 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
457 options.audible_bell = str2bool(value);
458 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
459 options.visible_bell = str2bool(value);
460 else if (!strcasecmp(CONF_XTERM_TITLE, name))
461 options.enable_xterm_title = str2bool(value);
462 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
463 #ifdef HAVE_GETMOUSE
464 options.enable_mouse = str2bool(value);
465 #else
466 {}
467 #endif
468 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
469 options.crossfade_time = atoi(value);
470 else if (!strcasecmp(CONF_SEARCH_MODE, name))
471 options.search_mode = atoi(value);
472 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
473 options.hide_cursor = atoi(value);
474 else if (!strcasecmp(CONF_SEEK_TIME, name))
475 options.seek_time = atoi(value);
476 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
477 g_strfreev(options.screen_list);
478 options.screen_list = check_screen_list(value);
479 } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
480 /* the splash screen was removed */
481 } else if (!strcasecmp(CONF_HOST, name))
482 options.host = get_format(value);
483 else if (!strcasecmp(CONF_PORT, name))
484 options.port = atoi(get_format(value));
485 else if (!strcasecmp(CONF_PASSWORD, name))
486 options.password = get_format(value);
487 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
488 #ifdef ENABLE_LYRICS_SCREEN
489 options.lyrics_timeout = atoi(get_format(value));
490 #else
491 {}
492 #endif
493 else if (!strcasecmp(CONF_SCROLL, name))
494 options.scroll = str2bool(value);
495 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
496 g_free(options.scroll_sep);
497 options.scroll_sep = get_format(value);
498 } else
499 match_found = false;
501 if (!match_found)
502 print_error(_("Unknown configuration parameter"),
503 name);
505 return match_found;
506 }
508 static int
509 read_rc_file(char *filename)
510 {
511 FILE *file;
512 char line[MAX_LINE_LENGTH];
514 if (filename == NULL)
515 return -1;
517 file = fopen(filename, "r");
518 if (file == NULL) {
519 perror(filename);
520 return -1;
521 }
523 while (fgets(line, sizeof(line), file) != NULL) {
524 char *p = g_strchug(line);
526 if (*p != 0 && *p != COMMENT_TOKEN)
527 parse_line(g_strchomp(p));
528 }
530 fclose(file);
531 return 0;
532 }
534 int
535 check_user_conf_dir(void)
536 {
537 int retval;
538 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
540 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
541 g_free(directory);
542 return 0;
543 }
545 retval = mkdir(directory, 0755);
546 g_free(directory);
547 return retval;
548 }
550 char *
551 get_user_key_binding_filename(void)
552 {
553 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
554 }
556 int
557 read_configuration(void)
558 {
559 char *filename = NULL;
561 /* check for command line configuration file */
562 if (options.config_file)
563 filename = g_strdup(options.config_file);
565 /* check for user configuration ~/.ncmpc/config */
566 if (filename == NULL) {
567 filename = g_build_filename(g_get_home_dir(),
568 "." PACKAGE, "config", NULL);
569 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
570 g_free(filename);
571 filename = NULL;
572 }
573 }
575 /* check for global configuration SYSCONFDIR/ncmpc/config */
576 if (filename == NULL) {
577 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
578 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
579 g_free(filename);
580 filename = NULL;
581 }
582 }
584 /* load configuration */
585 if (filename) {
586 read_rc_file(filename);
587 g_free(filename);
588 filename = NULL;
589 }
591 /* check for command line key binding file */
592 if (options.key_file)
593 filename = g_strdup(options.key_file);
595 /* check for user key bindings ~/.ncmpc/keys */
596 if (filename == NULL) {
597 filename = get_user_key_binding_filename();
598 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
599 g_free(filename);
600 filename = NULL;
601 }
602 }
604 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
605 if (filename == NULL) {
606 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
607 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
608 g_free(filename);
609 filename = NULL;
610 }
611 }
613 /* load key bindings */
614 if (filename) {
615 read_rc_file(filename);
616 g_free(filename);
617 filename = NULL;
618 }
620 return 0;
621 }