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 bool
364 parse_line(char *line)
365 {
366 size_t len = strlen(line), i = 0, j;
367 char name[MAX_LINE_LENGTH];
368 char value[MAX_LINE_LENGTH];
369 bool match_found;
371 /* get the name part */
372 j = 0;
373 while (i < len && line[i] != '=' &&
374 !g_ascii_isspace(line[i])) {
375 name[j++] = line[i++];
376 }
378 name[j] = '\0';
380 /* skip '=' and whitespace */
381 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
382 i++;
384 /* get the value part */
385 j = 0;
386 while (i < len)
387 value[j++] = line[i++];
388 value[j] = '\0';
390 match_found = true;
392 /* key definition */
393 if (!strcasecmp(CONF_KEY_DEFINITION, name))
394 parse_key_definition(value);
395 /* enable colors */
396 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
397 #ifdef ENABLE_COLORS
398 options.enable_colors = str2bool(value);
399 #else
400 {}
401 #endif
402 /* auto center */
403 else if (!strcasecmp(CONF_AUTO_CENTER, name))
404 options.auto_center = str2bool(value);
405 /* color assignment */
406 else if (!strcasecmp(CONF_COLOR, name))
407 #ifdef ENABLE_COLORS
408 parse_color(value);
409 #else
410 {}
411 #endif
412 /* wide cursor */
413 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
414 options.wide_cursor = str2bool(value);
415 /* welcome screen list */
416 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
417 options.welcome_screen_list = str2bool(value);
418 /* visible bitrate */
419 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
420 options.visible_bitrate = str2bool(value);
421 /* timer display type */
422 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name)) {
423 g_free(options.timedisplay_type);
424 options.timedisplay_type=g_strdup(parse_timedisplay_type(value));
425 /* color definition */
426 } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
427 #ifdef ENABLE_COLORS
428 parse_color_definition(value);
429 #else
430 {}
431 #endif
432 /* list format string */
433 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
434 g_free(options.list_format);
435 options.list_format = get_format(value);
436 /* status format string */
437 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
438 g_free(options.status_format);
439 options.status_format = get_format(value);
440 /* xterm title format string */
441 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
442 g_free(options.xterm_title_format);
443 options.xterm_title_format = get_format(value);
444 } else if (!strcasecmp(CONF_LIST_WRAP, name))
445 options.list_wrap = str2bool(value);
446 else if (!strcasecmp(CONF_FIND_WRAP, name))
447 options.find_wrap = str2bool(value);
448 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
449 options.find_show_last_pattern = str2bool(value);
450 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
451 options.audible_bell = str2bool(value);
452 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
453 options.visible_bell = str2bool(value);
454 else if (!strcasecmp(CONF_XTERM_TITLE, name))
455 options.enable_xterm_title = str2bool(value);
456 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
457 #ifdef HAVE_GETMOUSE
458 options.enable_mouse = str2bool(value);
459 #else
460 {}
461 #endif
462 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
463 options.crossfade_time = atoi(value);
464 else if (!strcasecmp(CONF_SEARCH_MODE, name))
465 options.search_mode = atoi(value);
466 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
467 options.hide_cursor = atoi(value);
468 else if (!strcasecmp(CONF_SEEK_TIME, name))
469 options.seek_time = atoi(value);
470 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
471 g_strfreev(options.screen_list);
472 options.screen_list = check_screen_list(value);
473 } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
474 /* the splash screen was removed */
475 } else if (!strcasecmp(CONF_HOST, name))
476 options.host = get_format(value);
477 else if (!strcasecmp(CONF_PORT, name))
478 options.port = atoi(get_format(value));
479 else if (!strcasecmp(CONF_PASSWORD, name))
480 options.password = get_format(value);
481 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
482 #ifdef ENABLE_LYRICS_SCREEN
483 options.lyrics_timeout = atoi(get_format(value));
484 #else
485 {}
486 #endif
487 else if (!strcasecmp(CONF_SCROLL, name))
488 options.scroll = str2bool(value);
489 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
490 g_free(options.scroll_sep);
491 options.scroll_sep = get_format(value);
492 } else
493 match_found = false;
495 if (!match_found)
496 print_error(_("Unknown configuration parameter"),
497 name);
499 return match_found;
500 }
502 static int
503 read_rc_file(char *filename)
504 {
505 FILE *file;
506 char line[MAX_LINE_LENGTH];
508 if (filename == NULL)
509 return -1;
511 file = fopen(filename, "r");
512 if (file == NULL) {
513 perror(filename);
514 return -1;
515 }
517 while (fgets(line, sizeof(line), file) != NULL) {
518 char *p = g_strchug(line);
520 if (*p != 0 && *p != COMMENT_TOKEN)
521 parse_line(g_strchomp(p));
522 }
524 fclose(file);
525 return 0;
526 }
528 int
529 check_user_conf_dir(void)
530 {
531 int retval;
532 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
534 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
535 g_free(directory);
536 return 0;
537 }
539 retval = mkdir(directory, 0755);
540 g_free(directory);
541 return retval;
542 }
544 char *
545 get_user_key_binding_filename(void)
546 {
547 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
548 }
550 int
551 read_configuration(void)
552 {
553 char *filename = NULL;
555 /* check for command line configuration file */
556 if (options.config_file)
557 filename = g_strdup(options.config_file);
559 /* check for user configuration ~/.ncmpc/config */
560 if (filename == NULL) {
561 filename = g_build_filename(g_get_home_dir(),
562 "." PACKAGE, "config", NULL);
563 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
564 g_free(filename);
565 filename = NULL;
566 }
567 }
569 /* check for global configuration SYSCONFDIR/ncmpc/config */
570 if (filename == NULL) {
571 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
572 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
573 g_free(filename);
574 filename = NULL;
575 }
576 }
578 /* load configuration */
579 if (filename) {
580 read_rc_file(filename);
581 g_free(filename);
582 filename = NULL;
583 }
585 /* check for command line key binding file */
586 if (options.key_file)
587 filename = g_strdup(options.key_file);
589 /* check for user key bindings ~/.ncmpc/keys */
590 if (filename == NULL) {
591 filename = get_user_key_binding_filename();
592 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
593 g_free(filename);
594 filename = NULL;
595 }
596 }
598 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
599 if (filename == NULL) {
600 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
601 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
602 g_free(filename);
603 filename = NULL;
604 }
605 }
607 /* load key bindings */
608 if (filename) {
609 read_rc_file(filename);
610 g_free(filename);
611 filename = NULL;
612 }
614 return 0;
615 }