1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
21 #define NO_GLOBAL_OPTIONS
22 #include "conf.h"
23 #include "config.h"
24 #include "defaults.h"
25 #include "i18n.h"
26 #include "support.h"
27 #include "command.h"
28 #include "colors.h"
29 #include "screen_list.h"
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
41 #define MAX_LINE_LENGTH 1024
42 #define COMMENT_TOKEN '#'
44 /* configuration field names */
45 #define CONF_ENABLE_COLORS "enable-colors"
46 #define CONF_AUTO_CENTER "auto-center"
47 #define CONF_WIDE_CURSOR "wide-cursor"
48 #define CONF_ENABLE_BELL "enable-bell"
49 #define CONF_KEY_DEFINITION "key"
50 #define CONF_COLOR "color"
51 #define CONF_COLOR_DEFINITION "colordef"
52 #define CONF_LIST_FORMAT "list-format"
53 #define CONF_STATUS_FORMAT "status-format"
54 #define CONF_XTERM_TITLE_FORMAT "xterm-title-format"
55 #define CONF_LIST_WRAP "wrap-around"
56 #define CONF_FIND_WRAP "find-wrap"
57 #define CONF_FIND_SHOW_LAST "find-show-last"
58 #define CONF_AUDIBLE_BELL "audible-bell"
59 #define CONF_VISIBLE_BELL "visible-bell"
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"
78 typedef enum {
79 KEY_PARSER_UNKNOWN,
80 KEY_PARSER_CHAR,
81 KEY_PARSER_DEC,
82 KEY_PARSER_HEX,
83 KEY_PARSER_DONE
84 } key_parser_state_t;
86 static bool
87 str2bool(char *str)
88 {
89 return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
90 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
91 }
93 static int
94 parse_key_value(char *str, size_t len, char **end)
95 {
96 size_t i;
97 int value;
98 key_parser_state_t state;
100 i=0;
101 value=0;
102 state=KEY_PARSER_UNKNOWN;
103 *end = str;
105 while (i < len && state != KEY_PARSER_DONE) {
106 int next = 0;
107 int c = str[i];
109 if( i+1<len )
110 next = str[i+1];
112 switch(state) {
113 case KEY_PARSER_UNKNOWN:
114 if( c=='\'' )
115 state = KEY_PARSER_CHAR;
116 else if( c=='0' && next=='x' )
117 state = KEY_PARSER_HEX;
118 else if( isdigit(c) )
119 state = KEY_PARSER_DEC;
120 else {
121 fprintf(stderr,
122 _("Error: Unsupported key definition - %s\n"),
123 str);
124 return -1;
125 }
126 break;
127 case KEY_PARSER_CHAR:
128 if( next!='\'' ) {
129 fprintf(stderr,
130 _("Error: Unsupported key definition - %s\n"),
131 str);
132 return -1;
133 }
134 value = c;
135 *end = str+i+2;
136 state = KEY_PARSER_DONE;
137 break;
138 case KEY_PARSER_DEC:
139 value = (int) strtol(str+(i-1), end, 10);
140 state = KEY_PARSER_DONE;
141 break;
142 case KEY_PARSER_HEX:
143 if( !isdigit(next) ) {
144 fprintf(stderr,_("Error: Digit expected after 0x - %s\n"), str);
145 return -1;
146 }
147 value = (int) strtol(str+(i+1), end, 16);
148 state = KEY_PARSER_DONE;
149 break;
150 case KEY_PARSER_DONE:
151 break;
152 }
153 i++;
154 }
156 if( *end> str+len )
157 *end = str+len;
159 return value;
160 }
162 static int
163 parse_key_definition(char *str)
164 {
165 char buf[MAX_LINE_LENGTH];
166 char *p, *end;
167 size_t len = strlen(str), i;
168 int j,key;
169 int keys[MAX_COMMAND_KEYS];
170 command_t cmd;
172 /* get the command name */
173 i=0;
174 j=0;
175 memset(buf, 0, MAX_LINE_LENGTH);
176 while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
177 buf[j++] = str[i++];
178 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
179 fprintf(stderr, _("Error: Unknown key command %s\n"), buf);
180 return -1;
181 }
183 /* skip whitespace */
184 while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) )
185 i++;
187 /* get the value part */
188 memset(buf, 0, MAX_LINE_LENGTH);
189 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
190 len = strlen(buf);
191 if( len==0 ) {
192 fprintf(stderr,_("Error: Incomplete key definition - %s\n"), str);
193 return -1;
194 }
196 /* parse key values */
197 i = 0;
198 key = 0;
199 len = strlen(buf);
200 p = buf;
201 end = buf+len;
202 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
203 while( i<MAX_COMMAND_KEYS && p<end &&
204 (key=parse_key_value(p,len+1,&p))>=0 ) {
205 keys[i++] = key;
206 while( p<end && (*p==',' || *p==' ' || *p=='\t') )
207 p++;
208 len = strlen(p);
209 }
210 if( key<0 ) {
211 fprintf(stderr,_("Error: Bad key definition - %s\n"), str);
212 return -1;
213 }
215 return assign_keys(cmd, keys);
216 }
218 static const char *
219 parse_timedisplay_type(const char *str)
220 {
221 if((!strcmp(str,"elapsed")) || (!strcmp(str,"remaining"))){
222 return str;
223 } else {
224 fprintf(stderr,_("Error: Bad time display type - %s\n"), str);
225 return DEFAULT_TIMEDISPLAY_TYPE;
226 }
227 }
229 static int
230 parse_color(char *str)
231 {
232 const char *name = str;
233 const char *value = NULL;
234 int len,i;
236 i=0;
237 len=strlen(str);
238 /* get the color name */
239 while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
240 i++;
242 /* skip whitespace */
243 while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) ) {
244 str[i]='\0';
245 i++;
246 }
248 if( i<len )
249 value = str+i;
251 return colors_assign(name, value);
252 }
254 static int
255 parse_color_definition(char *str)
256 {
257 char buf[MAX_LINE_LENGTH];
258 char *p, *end, *name;
259 size_t len = strlen(str), i;
260 int j,value;
261 short color, rgb[3];
263 /* get the command name */
264 i=0;
265 j=0;
266 memset(buf, 0, MAX_LINE_LENGTH);
267 while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
268 buf[j++] = str[i++];
269 color=colors_str2color(buf);
270 if( color<0 ) {
271 fprintf(stderr,_("Error: Bad color %s [%d]\n"), buf, color);
272 return -1;
273 }
274 name = g_strdup(buf);
276 /* skip whitespace */
277 while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) )
278 i++;
280 /* get the value part */
281 memset(buf, 0, MAX_LINE_LENGTH);
282 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
283 len = strlen(buf);
284 if( len==0 ) {
285 fprintf(stderr,_("Error: Incomplete color definition - %s\n"), str);
286 g_free(name);
287 return -1;
288 }
290 /* parse r,g.b values with the key definition parser */
291 i = 0;
292 value = 0;
293 len = strlen(buf);
294 p = buf;
295 end = buf+len;
296 memset(rgb, 0, sizeof(short)*3);
297 while( i<3 && p<end && (value=parse_key_value(p,len+1,&p))>=0 ) {
298 rgb[i++] = value;
299 while( p<end && (*p==',' || *p==' ' || *p=='\t') )
300 p++;
301 len = strlen(p);
302 }
303 if( value<0 || i!=3) {
304 fprintf(stderr,_("Error: Bad color definition - %s\n"), str);
305 g_free(name);
306 return -1;
307 }
308 value = colors_define(name, rgb[0], rgb[1], rgb[2]);
309 g_free(name);
310 return value;
311 }
313 static char *
314 get_format(char *str)
315 {
316 gsize len = strlen(str);
318 if( str && str[0]=='\"' && str[len-1] == '\"' ) {
319 str[len-1] = '\0';
320 str++;
321 }
322 return g_strdup(str);
323 }
325 static char **
326 check_screen_list(char *value)
327 {
328 char **tmp = g_strsplit_set(value, " \t,", 100);
329 char **screen = NULL;
330 int i,j;
332 i=0;
333 j=0;
334 while( tmp && tmp[i] ) {
335 char *name = g_ascii_strdown(tmp[i], -1);
336 if (screen_lookup_name(name) == NULL) {
337 fprintf(stderr,
338 _("Error: Unsupported screen \"%s\"\n"),
339 name);
340 free(name);
341 } else {
342 screen = g_realloc(screen, (j+2)*sizeof(char *));
343 screen[j++] = name;
344 screen[j] = NULL;
345 }
346 i++;
347 }
348 g_strfreev(tmp);
349 if( screen == NULL )
350 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
352 return screen;
353 }
355 static int
356 read_rc_file(char *filename, options_t *options)
357 {
358 int fd;
359 int quit = 0;
360 int free_filename = 0;
362 if( filename==NULL )
363 return -1;
365 if( (fd=open(filename,O_RDONLY)) <0 )
366 {
367 perror(filename);
368 if( free_filename )
369 g_free(filename);
370 return -1;
371 }
373 while( !quit )
374 {
375 int i,j;
376 int len;
377 int match_found;
378 char line[MAX_LINE_LENGTH];
379 char name[MAX_LINE_LENGTH];
380 char value[MAX_LINE_LENGTH];
382 line[0] = '\0';
383 value[0] = '\0';
385 i = 0;
386 /* read a line ending with '\n' */
387 do {
388 len = read( fd, &line[i], 1 );
389 if( len == 1 )
390 i++;
391 else
392 quit = 1;
393 } while( !quit && i<MAX_LINE_LENGTH && line[i-1]!='\n' );
396 /* remove trailing whitespace */
397 line[i] = '\0';
398 i--;
399 while( i>=0 && IS_WHITESPACE(line[i]) )
400 {
401 line[i] = '\0';
402 i--;
403 }
404 len = i+1;
406 if( len>0 )
407 {
408 i = 0;
409 /* skip whitespace */
410 while( i<len && IS_WHITESPACE(line[i]) )
411 i++;
413 /* continue if this line is not a comment */
414 if( line[i] != COMMENT_TOKEN )
415 {
416 /* get the name part */
417 j=0;
418 while( i<len && line[i]!='=' && !IS_WHITESPACE(line[i]) )
419 {
420 name[j++] = line[i++];
421 }
422 name[j] = '\0';
424 /* skip '=' and whitespace */
425 while( i<len && (line[i]=='=' || IS_WHITESPACE(line[i])) )
426 i++;
428 /* get the value part */
429 j=0;
430 while( i<len )
431 {
432 value[j++] = line[i++];
433 }
434 value[j] = '\0';
436 match_found = 1;
438 /* key definition */
439 if( !strcasecmp(CONF_KEY_DEFINITION, name) )
440 {
441 parse_key_definition(value);
442 }
443 /* enable colors */
444 else if( !strcasecmp(CONF_ENABLE_COLORS, name) )
445 {
446 options->enable_colors = str2bool(value);
447 }
448 /* auto center */
449 else if( !strcasecmp(CONF_AUTO_CENTER, name) )
450 {
451 options->auto_center = str2bool(value);
452 }
453 /* color assignment */
454 else if( !strcasecmp(CONF_COLOR, name) )
455 {
456 parse_color(value);
457 }
458 /* wide cursor */
459 else if( !strcasecmp(CONF_WIDE_CURSOR, name) )
460 {
461 options->wide_cursor = str2bool(value);
462 }
463 /* welcome screen list */
464 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name)) {
465 options->welcome_screen_list = str2bool(value);
466 }
467 /* visible bitrate */
468 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name)) {
469 options->visible_bitrate = str2bool(value);
470 }
471 /* timer display type */
472 else if( !strcasecmp(CONF_TIMEDISPLAY_TYPE, name) )
473 {
474 g_free(options->timedisplay_type);
475 options->timedisplay_type=g_strdup(parse_timedisplay_type(value));
476 }
477 /* color definition */
478 else if( !strcasecmp(CONF_COLOR_DEFINITION, name) )
479 {
480 parse_color_definition(value);
481 }
482 /* list format string */
483 else if( !strcasecmp(CONF_LIST_FORMAT, name) )
484 {
485 g_free(options->list_format);
486 options->list_format = get_format(value);
487 }
488 /* status format string */
489 else if( !strcasecmp(CONF_STATUS_FORMAT, name) )
490 {
491 g_free(options->status_format);
492 options->status_format = get_format(value);
493 }
494 /* xterm title format string */
495 else if( !strcasecmp(CONF_XTERM_TITLE_FORMAT, name) )
496 {
497 g_free(options->xterm_title_format);
498 options->xterm_title_format = get_format(value);
499 }
500 else if( !strcasecmp(CONF_LIST_WRAP, name) )
501 {
502 options->list_wrap = str2bool(value);
503 }
504 else if( !strcasecmp(CONF_FIND_WRAP, name) )
505 {
506 options->find_wrap = str2bool(value);
507 }
508 else if( !strcasecmp(CONF_FIND_SHOW_LAST,name) )
509 {
510 options->find_show_last_pattern = str2bool(value);
511 }
512 else if( !strcasecmp(CONF_AUDIBLE_BELL, name) )
513 {
514 options->audible_bell = str2bool(value);
515 }
516 else if( !strcasecmp(CONF_VISIBLE_BELL, name) )
517 {
518 options->visible_bell = str2bool(value);
519 }
520 else if( !strcasecmp(CONF_XTERM_TITLE, name) )
521 {
522 options->enable_xterm_title = str2bool(value);
523 }
524 else if( !strcasecmp(CONF_ENABLE_MOUSE, name) )
525 {
526 options->enable_mouse = str2bool(value);
527 }
528 else if( !strcasecmp(CONF_CROSSFADE_TIME, name) )
529 {
530 options->crossfade_time = atoi(value);
531 }
532 else if( !strcasecmp(CONF_SEARCH_MODE, name) )
533 {
534 options->search_mode = atoi(value);
535 }
536 else if( !strcasecmp(CONF_HIDE_CURSOR, name) )
537 {
538 options->hide_cursor = atoi(value);
539 }
540 else if( !strcasecmp(CONF_SEEK_TIME, name) )
541 {
542 options->seek_time = atoi(value);
543 }
544 else if( !strcasecmp(CONF_SCREEN_LIST, name) )
545 {
546 g_strfreev(options->screen_list);
547 options->screen_list = check_screen_list(value);
548 }
549 else if( !strcasecmp(CONF_SHOW_SPLASH, name) )
550 {
551 /* the splash screen was removed */
552 }
553 else if( !strcasecmp(CONF_HOST, name))
554 {
555 options->host = get_format(value);
556 }
557 else if( !strcasecmp(CONF_PORT, name))
558 {
559 options->port = atoi(get_format(value));
560 }
561 else if( !strcasecmp(CONF_PASSWORD, name))
562 {
563 options->password = get_format(value);
564 }
565 else if( !strcasecmp(CONF_LYRICS_TIMEOUT, name))
566 {
567 options->lyrics_timeout = atoi(get_format(value));
568 }
569 else if( !strcasecmp(CONF_SCROLL, name))
570 {
571 options->scroll = str2bool(value);
572 }
573 else if( !strcasecmp(CONF_SCROLL_SEP, name))
574 {
575 g_free(options->scroll_sep);
576 options->scroll_sep = get_format(value);
577 }
578 else
579 {
580 match_found = 0;
581 }
583 if( !match_found )
584 fprintf(stderr,
585 _("Unknown configuration parameter: %s\n"),
586 name);
587 }
588 }
589 }
591 if( free_filename )
592 g_free(filename);
594 return 0;
595 }
597 int
598 check_user_conf_dir(void)
599 {
600 int retval;
601 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
603 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
604 g_free(directory);
605 return 0;
606 }
608 retval = mkdir(directory, 0755);
609 g_free(directory);
610 return retval;
611 }
613 char *
614 get_user_key_binding_filename(void)
615 {
616 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
617 }
620 int
621 read_configuration(options_t *options)
622 {
623 char *filename = NULL;
625 /* check for command line configuration file */
626 if( options->config_file )
627 filename = g_strdup(options->config_file);
629 /* check for user configuration ~/.ncmpc/config */
630 if( filename == NULL )
631 {
632 filename = g_build_filename(g_get_home_dir(),
633 "." PACKAGE, "config", NULL);
634 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
635 {
636 g_free(filename);
637 filename = NULL;
638 }
639 }
641 /* check for global configuration SYSCONFDIR/ncmpc/config */
642 if( filename == NULL )
643 {
644 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
645 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
646 {
647 g_free(filename);
648 filename = NULL;
649 }
650 }
652 /* load configuration */
653 if( filename )
654 {
655 read_rc_file(filename, options);
656 g_free(filename);
657 filename = NULL;
658 }
660 /* check for command line key binding file */
661 if( options->key_file )
662 filename = g_strdup(options->key_file);
664 /* check for user key bindings ~/.ncmpc/keys */
665 if( filename == NULL )
666 {
667 filename = get_user_key_binding_filename();
668 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
669 {
670 g_free(filename);
671 filename = NULL;
672 }
673 }
675 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
676 if( filename == NULL )
677 {
678 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
679 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
680 {
681 g_free(filename);
682 filename = NULL;
683 }
684 }
686 /* load key bindings */
687 if( filename )
688 {
689 read_rc_file(filename, options);
690 g_free(filename);
691 filename = NULL;
692 }
694 return 0;
695 }