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 int
235 parse_color(char *str)
236 {
237 const char *name = str;
238 const char *value = NULL;
239 int len,i;
241 i=0;
242 len=strlen(str);
243 /* get the color name */
244 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
245 i++;
247 /* skip whitespace */
248 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i]))) {
249 str[i]='\0';
250 i++;
251 }
253 if (i < len)
254 value = str+i;
256 return colors_assign(name, value);
257 }
259 static int
260 parse_color_definition(char *str)
261 {
262 char buf[MAX_LINE_LENGTH];
263 char *p, *end, *name;
264 size_t len = strlen(str), i;
265 int j,value;
266 short color, rgb[3];
268 /* get the command name */
269 i=0;
270 j=0;
271 memset(buf, 0, MAX_LINE_LENGTH);
272 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
273 buf[j++] = str[i++];
274 color = colors_str2color(buf);
275 if (color < 0) {
276 print_error(_("Bad color"), buf);
277 return -1;
278 }
279 name = g_strdup(buf);
281 /* skip whitespace */
282 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
283 i++;
285 /* get the value part */
286 memset(buf, 0, MAX_LINE_LENGTH);
287 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
288 len = strlen(buf);
289 if (len == 0) {
290 print_error(_("Incomplete color definition"), str);
291 g_free(name);
292 return -1;
293 }
295 /* parse r,g.b values with the key definition parser */
296 i = 0;
297 value = 0;
298 len = strlen(buf);
299 p = buf;
300 end = buf + len;
301 memset(rgb, 0, sizeof(short)*3);
302 while (i < 3 && p < end &&
303 (value = parse_key_value(p,len+1,&p)) >= 0) {
304 rgb[i++] = value;
305 while( p<end && (*p==',' || *p==' ' || *p=='\t') )
306 p++;
307 len = strlen(p);
308 }
310 if (value < 0 || i != 3) {
311 print_error(_("Bad color definition"), str);
312 g_free(name);
313 return -1;
314 }
316 value = colors_define(name, rgb[0], rgb[1], rgb[2]);
317 g_free(name);
318 return value;
319 }
320 #endif
322 static char *
323 get_format(char *str)
324 {
325 gsize len = strlen(str);
327 if (str && str[0]=='\"' && str[len-1] == '\"') {
328 str[len - 1] = '\0';
329 str++;
330 }
332 return g_strdup(str);
333 }
335 static char **
336 check_screen_list(char *value)
337 {
338 char **tmp = g_strsplit_set(value, " \t,", 100);
339 char **screen = NULL;
340 int i,j;
342 i=0;
343 j=0;
344 while( tmp && tmp[i] ) {
345 char *name = g_ascii_strdown(tmp[i], -1);
346 if (screen_lookup_name(name) == NULL) {
347 print_error(_("Unsupported screen"), name);
348 free(name);
349 } else {
350 screen = g_realloc(screen, (j+2)*sizeof(char *));
351 screen[j++] = name;
352 screen[j] = NULL;
353 }
354 i++;
355 }
356 g_strfreev(tmp);
357 if( screen == NULL )
358 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
360 return screen;
361 }
363 static int
364 read_rc_file(char *filename)
365 {
366 int fd;
367 int quit = 0;
368 int free_filename = 0;
370 if (filename == NULL)
371 return -1;
373 if ((fd = open(filename,O_RDONLY)) < 0) {
374 perror(filename);
375 if (free_filename)
376 g_free(filename);
377 return -1;
378 }
380 while (!quit) {
381 int i,j;
382 int len;
383 int match_found;
384 char line[MAX_LINE_LENGTH];
385 char name[MAX_LINE_LENGTH];
386 char value[MAX_LINE_LENGTH];
388 line[0] = '\0';
389 value[0] = '\0';
391 i = 0;
392 /* read a line ending with '\n' */
393 do {
394 len = read(fd, &line[i], 1);
395 if (len == 1)
396 i++;
397 else
398 quit = 1;
399 } while (!quit && i < MAX_LINE_LENGTH && line[i-1] != '\n');
402 /* remove trailing whitespace */
403 line[i] = '\0';
404 i--;
405 while (i >= 0 && g_ascii_isspace(line[i])) {
406 line[i] = '\0';
407 i--;
408 }
410 len = i + 1;
411 if (len > 0) {
412 i = 0;
413 /* skip whitespace */
414 while (i < len && g_ascii_isspace(line[i]))
415 i++;
417 /* continue if this line is not a comment */
418 if (line[i] != COMMENT_TOKEN) {
419 /* get the name part */
420 j = 0;
421 while (i < len && line[i] != '=' &&
422 !g_ascii_isspace(line[i])) {
423 name[j++] = line[i++];
424 }
426 name[j] = '\0';
428 /* skip '=' and whitespace */
429 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
430 i++;
432 /* get the value part */
433 j = 0;
434 while (i < len)
435 value[j++] = line[i++];
436 value[j] = '\0';
438 match_found = 1;
440 /* key definition */
441 if (!strcasecmp(CONF_KEY_DEFINITION, name))
442 parse_key_definition(value);
443 /* enable colors */
444 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
445 #ifdef ENABLE_COLORS
446 options.enable_colors = str2bool(value);
447 #else
448 {}
449 #endif
450 /* auto center */
451 else if (!strcasecmp(CONF_AUTO_CENTER, name))
452 options.auto_center = str2bool(value);
453 /* color assignment */
454 else if (!strcasecmp(CONF_COLOR, name))
455 #ifdef ENABLE_COLORS
456 parse_color(value);
457 #else
458 {}
459 #endif
460 /* wide cursor */
461 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
462 options.wide_cursor = str2bool(value);
463 /* welcome screen list */
464 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
465 options.welcome_screen_list = str2bool(value);
466 /* visible bitrate */
467 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
468 options.visible_bitrate = str2bool(value);
469 /* timer display type */
470 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name)) {
471 g_free(options.timedisplay_type);
472 options.timedisplay_type=g_strdup(parse_timedisplay_type(value));
473 /* color definition */
474 } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
475 #ifdef ENABLE_COLORS
476 parse_color_definition(value);
477 #else
478 {}
479 #endif
480 /* list format string */
481 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
482 g_free(options.list_format);
483 options.list_format = get_format(value);
484 /* status format string */
485 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
486 g_free(options.status_format);
487 options.status_format = get_format(value);
488 /* xterm title format string */
489 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
490 g_free(options.xterm_title_format);
491 options.xterm_title_format = get_format(value);
492 } else if (!strcasecmp(CONF_LIST_WRAP, name))
493 options.list_wrap = str2bool(value);
494 else if (!strcasecmp(CONF_FIND_WRAP, name))
495 options.find_wrap = str2bool(value);
496 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
497 options.find_show_last_pattern = str2bool(value);
498 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
499 options.audible_bell = str2bool(value);
500 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
501 options.visible_bell = str2bool(value);
502 else if (!strcasecmp(CONF_XTERM_TITLE, name))
503 options.enable_xterm_title = str2bool(value);
504 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
505 #ifdef HAVE_GETMOUSE
506 options.enable_mouse = str2bool(value);
507 #else
508 {}
509 #endif
510 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
511 options.crossfade_time = atoi(value);
512 else if (!strcasecmp(CONF_SEARCH_MODE, name))
513 options.search_mode = atoi(value);
514 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
515 options.hide_cursor = atoi(value);
516 else if (!strcasecmp(CONF_SEEK_TIME, name))
517 options.seek_time = atoi(value);
518 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
519 g_strfreev(options.screen_list);
520 options.screen_list = check_screen_list(value);
521 } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
522 /* the splash screen was removed */
523 } else if (!strcasecmp(CONF_HOST, name))
524 options.host = get_format(value);
525 else if (!strcasecmp(CONF_PORT, name))
526 options.port = atoi(get_format(value));
527 else if (!strcasecmp(CONF_PASSWORD, name))
528 options.password = get_format(value);
529 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
530 #ifdef ENABLE_LYRICS_SCREEN
531 options.lyrics_timeout = atoi(get_format(value));
532 #else
533 {}
534 #endif
535 else if (!strcasecmp(CONF_SCROLL, name))
536 options.scroll = str2bool(value);
537 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
538 g_free(options.scroll_sep);
539 options.scroll_sep = get_format(value);
540 } else
541 match_found = 0;
543 if (!match_found)
544 print_error(_("Unknown configuration parameter"),
545 name);
546 }
547 }
548 }
550 if (free_filename)
551 g_free(filename);
553 return 0;
554 }
556 int
557 check_user_conf_dir(void)
558 {
559 int retval;
560 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
562 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
563 g_free(directory);
564 return 0;
565 }
567 retval = mkdir(directory, 0755);
568 g_free(directory);
569 return retval;
570 }
572 char *
573 get_user_key_binding_filename(void)
574 {
575 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
576 }
578 int
579 read_configuration(void)
580 {
581 char *filename = NULL;
583 /* check for command line configuration file */
584 if (options.config_file)
585 filename = g_strdup(options.config_file);
587 /* check for user configuration ~/.ncmpc/config */
588 if (filename == NULL) {
589 filename = g_build_filename(g_get_home_dir(),
590 "." PACKAGE, "config", NULL);
591 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
592 g_free(filename);
593 filename = NULL;
594 }
595 }
597 /* check for global configuration SYSCONFDIR/ncmpc/config */
598 if (filename == NULL) {
599 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
600 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
601 g_free(filename);
602 filename = NULL;
603 }
604 }
606 /* load configuration */
607 if (filename) {
608 read_rc_file(filename);
609 g_free(filename);
610 filename = NULL;
611 }
613 /* check for command line key binding file */
614 if (options.key_file)
615 filename = g_strdup(options.key_file);
617 /* check for user key bindings ~/.ncmpc/keys */
618 if (filename == NULL) {
619 filename = get_user_key_binding_filename();
620 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
621 g_free(filename);
622 filename = NULL;
623 }
624 }
626 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
627 if (filename == NULL) {
628 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
629 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
630 g_free(filename);
631 filename = NULL;
632 }
633 }
635 /* load key bindings */
636 if (filename) {
637 read_rc_file(filename);
638 g_free(filename);
639 filename = NULL;
640 }
642 return 0;
643 }