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 "ncmpc.h"
25 #include "support.h"
26 #include "command.h"
27 #include "colors.h"
29 #include <ctype.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.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_XTERM_TITLE "set-xterm-title"
59 #define CONF_ENABLE_MOUSE "enable-mouse"
60 #define CONF_CROSSFADE_TIME "crossfade-time"
61 #define CONF_SEARCH_MODE "search-mode"
62 #define CONF_HIDE_CURSOR "hide-cursor"
63 #define CONF_SEEK_TIME "seek-time"
64 #define CONF_SCREEN_LIST "screen-list"
65 #define CONF_TIMEDISPLAY_TYPE "timedisplay-type"
66 #define CONF_HOST "host"
67 #define CONF_PORT "port"
68 #define CONF_PASSWORD "password"
69 #define CONF_LYRICS_TIMEOUT "lyrics-timeout"
70 #define CONF_SHOW_SPLASH "show-splash"
71 #define CONF_SCROLL "scroll"
72 #define CONF_SCROLL_SEP "scroll-sep"
73 #define CONF_VISIBLE_BITRATE "visible-bitrate"
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;
84 extern gint screen_get_id(char *name);
87 static gboolean
88 str2bool(char *str)
89 {
90 if (!strcasecmp(str, "yes") || !strcasecmp(str, "true") ||
91 !strcasecmp(str, "on") || !strcasecmp(str, "1"))
92 return TRUE;
93 return FALSE;
94 }
96 static int
97 parse_key_value(char *str, size_t len, char **end)
98 {
99 size_t i;
100 int value;
101 key_parser_state_t state;
103 i=0;
104 value=0;
105 state=KEY_PARSER_UNKNOWN;
106 *end = str;
108 while (i < len && state != KEY_PARSER_DONE) {
109 int next = 0;
110 int c = str[i];
112 if( i+1<len )
113 next = str[i+1];
115 switch(state) {
116 case KEY_PARSER_UNKNOWN:
117 if( c=='\'' )
118 state = KEY_PARSER_CHAR;
119 else if( c=='0' && next=='x' )
120 state = KEY_PARSER_HEX;
121 else if( isdigit(c) )
122 state = KEY_PARSER_DEC;
123 else {
124 fprintf(stderr,
125 _("Error: Unsupported key definition - %s\n"),
126 str);
127 return -1;
128 }
129 break;
130 case KEY_PARSER_CHAR:
131 if( next!='\'' ) {
132 fprintf(stderr,
133 _("Error: Unsupported key definition - %s\n"),
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 fprintf(stderr,_("Error: Digit expected after 0x - %s\n"), 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]!='=' && !IS_WHITESPACE(str[i]) )
180 buf[j++] = str[i++];
181 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
182 fprintf(stderr, _("Error: Unknown key command %s\n"), buf);
183 return -1;
184 }
186 /* skip whitespace */
187 while( i<len && (str[i]=='=' || IS_WHITESPACE(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 fprintf(stderr,_("Error: Incomplete key definition - %s\n"), 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 }
213 if( key<0 ) {
214 fprintf(stderr,_("Error: Bad key definition - %s\n"), str);
215 return -1;
216 }
218 return assign_keys(cmd, keys);
219 }
221 static const char *
222 parse_timedisplay_type(const char *str)
223 {
224 if((!strcmp(str,"elapsed")) || (!strcmp(str,"remaining"))){
225 return str;
226 } else {
227 fprintf(stderr,_("Error: Bad time display type - %s\n"), str);
228 return DEFAULT_TIMEDISPLAY_TYPE;
229 }
230 }
232 static int
233 parse_color(char *str)
234 {
235 const char *name = str;
236 const char *value = NULL;
237 int len,i;
239 i=0;
240 len=strlen(str);
241 /* get the color name */
242 while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
243 i++;
245 /* skip whitespace */
246 while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) ) {
247 str[i]='\0';
248 i++;
249 }
251 if( i<len )
252 value = str+i;
254 return colors_assign(name, value);
255 }
257 static int
258 parse_color_definition(char *str)
259 {
260 char buf[MAX_LINE_LENGTH];
261 char *p, *end, *name;
262 size_t len = strlen(str), i;
263 int j,value;
264 short color, rgb[3];
266 /* get the command name */
267 i=0;
268 j=0;
269 memset(buf, 0, MAX_LINE_LENGTH);
270 while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
271 buf[j++] = str[i++];
272 color=colors_str2color(buf);
273 if( color<0 ) {
274 fprintf(stderr,_("Error: Bad color %s [%d]\n"), buf, color);
275 return -1;
276 }
277 name = g_strdup(buf);
279 /* skip whitespace */
280 while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) )
281 i++;
283 /* get the value part */
284 memset(buf, 0, MAX_LINE_LENGTH);
285 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
286 len = strlen(buf);
287 if( len==0 ) {
288 fprintf(stderr,_("Error: Incomplete color definition - %s\n"), str);
289 g_free(name);
290 return -1;
291 }
293 /* parse r,g.b values with the key definition parser */
294 i = 0;
295 value = 0;
296 len = strlen(buf);
297 p = buf;
298 end = buf+len;
299 memset(rgb, 0, sizeof(short)*3);
300 while( i<3 && p<end && (value=parse_key_value(p,len+1,&p))>=0 ) {
301 rgb[i++] = value;
302 while( p<end && (*p==',' || *p==' ' || *p=='\t') )
303 p++;
304 len = strlen(p);
305 }
306 if( value<0 || i!=3) {
307 fprintf(stderr,_("Error: Bad color definition - %s\n"), str);
308 g_free(name);
309 return -1;
310 }
311 value = colors_define(name, rgb[0], rgb[1], rgb[2]);
312 g_free(name);
313 return value;
314 }
316 static char *
317 get_format(char *str)
318 {
319 gsize len = strlen(str);
321 if( str && str[0]=='\"' && str[len-1] == '\"' ) {
322 str[len-1] = '\0';
323 str++;
324 }
325 return g_strdup(str);
326 }
328 static char **
329 check_screen_list(char *value)
330 {
331 char **tmp = g_strsplit_set(value, " \t,", 100);
332 char **screen = NULL;
333 int i,j;
335 i=0;
336 j=0;
337 while( tmp && tmp[i] ) {
338 tmp[i] = lowerstr(tmp[i]);
339 if( screen_get_id(tmp[i]) == -1 )
340 fprintf(stderr,
341 _("Error: Unsupported screen \"%s\"\n"),
342 tmp[i]);
343 else {
344 screen = g_realloc(screen, (j+2)*sizeof(char *));
345 screen[j++] = g_strdup(tmp[i]);
346 screen[j] = NULL;
347 }
348 i++;
349 }
350 g_strfreev(tmp);
351 if( screen == NULL )
352 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
354 return screen;
355 }
357 static int
358 read_rc_file(char *filename, options_t *options)
359 {
360 int fd;
361 int quit = 0;
362 int free_filename = 0;
364 if( filename==NULL )
365 return -1;
367 D("Reading configuration file %s\n", filename);
368 if( (fd=open(filename,O_RDONLY)) <0 )
369 {
370 perror(filename);
371 if( free_filename )
372 g_free(filename);
373 return -1;
374 }
376 while( !quit )
377 {
378 int i,j;
379 int len;
380 int match_found;
381 char line[MAX_LINE_LENGTH];
382 char name[MAX_LINE_LENGTH];
383 char value[MAX_LINE_LENGTH];
385 line[0] = '\0';
386 value[0] = '\0';
388 i = 0;
389 /* read a line ending with '\n' */
390 do {
391 len = read( fd, &line[i], 1 );
392 if( len == 1 )
393 i++;
394 else
395 quit = 1;
396 } while( !quit && i<MAX_LINE_LENGTH && line[i-1]!='\n' );
399 /* remove trailing whitespace */
400 line[i] = '\0';
401 i--;
402 while( i>=0 && IS_WHITESPACE(line[i]) )
403 {
404 line[i] = '\0';
405 i--;
406 }
407 len = i+1;
409 if( len>0 )
410 {
411 i = 0;
412 /* skip whitespace */
413 while( i<len && IS_WHITESPACE(line[i]) )
414 i++;
416 /* continue if this line is not a comment */
417 if( line[i] != COMMENT_TOKEN )
418 {
419 /* get the name part */
420 j=0;
421 while( i<len && line[i]!='=' && !IS_WHITESPACE(line[i]) )
422 {
423 name[j++] = line[i++];
424 }
425 name[j] = '\0';
427 /* skip '=' and whitespace */
428 while( i<len && (line[i]=='=' || IS_WHITESPACE(line[i])) )
429 i++;
431 /* get the value part */
432 j=0;
433 while( i<len )
434 {
435 value[j++] = line[i++];
436 }
437 value[j] = '\0';
439 match_found = 1;
441 /* key definition */
442 if( !strcasecmp(CONF_KEY_DEFINITION, name) )
443 {
444 parse_key_definition(value);
445 }
446 /* enable colors */
447 else if( !strcasecmp(CONF_ENABLE_COLORS, name) )
448 {
449 options->enable_colors = str2bool(value);
450 }
451 /* auto center */
452 else if( !strcasecmp(CONF_AUTO_CENTER, name) )
453 {
454 options->auto_center = str2bool(value);
455 }
456 /* color assignment */
457 else if( !strcasecmp(CONF_COLOR, name) )
458 {
459 parse_color(value);
460 }
461 /* wide cursor */
462 else if( !strcasecmp(CONF_WIDE_CURSOR, name) )
463 {
464 options->wide_cursor = str2bool(value);
465 }
466 /* visible bitrate */
467 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name)) {
468 options->visible_bitrate = str2bool(value);
469 }
470 /* timer display type */
471 else if( !strcasecmp(CONF_TIMEDISPLAY_TYPE, name) )
472 {
473 g_free(options->timedisplay_type);
474 options->timedisplay_type=g_strdup(parse_timedisplay_type(value));
475 D("deb");
476 D(options->timedisplay_type);
477 }
478 /* color definition */
479 else if( !strcasecmp(CONF_COLOR_DEFINITION, name) )
480 {
481 parse_color_definition(value);
482 }
483 /* list format string */
484 else if( !strcasecmp(CONF_LIST_FORMAT, name) )
485 {
486 g_free(options->list_format);
487 options->list_format = get_format(value);
488 }
489 /* status format string */
490 else if( !strcasecmp(CONF_STATUS_FORMAT, name) )
491 {
492 g_free(options->status_format);
493 options->status_format = get_format(value);
494 }
495 /* xterm title format string */
496 else if( !strcasecmp(CONF_XTERM_TITLE_FORMAT, name) )
497 {
498 g_free(options->xterm_title_format);
499 options->xterm_title_format = get_format(value);
500 }
501 else if( !strcasecmp(CONF_LIST_WRAP, name) )
502 {
503 options->list_wrap = str2bool(value);
504 }
505 else if( !strcasecmp(CONF_FIND_WRAP, name) )
506 {
507 options->find_wrap = str2bool(value);
508 }
509 else if( !strcasecmp(CONF_FIND_SHOW_LAST,name) )
510 {
511 options->find_show_last_pattern = str2bool(value);
512 }
513 else if( !strcasecmp(CONF_AUDIBLE_BELL, name) )
514 {
515 options->audible_bell = str2bool(value);
516 }
517 else if( !strcasecmp(CONF_VISIBLE_BELL, name) )
518 {
519 options->visible_bell = str2bool(value);
520 }
521 else if( !strcasecmp(CONF_XTERM_TITLE, name) )
522 {
523 options->enable_xterm_title = str2bool(value);
524 }
525 else if( !strcasecmp(CONF_ENABLE_MOUSE, name) )
526 {
527 options->enable_mouse = str2bool(value);
528 }
529 else if( !strcasecmp(CONF_CROSSFADE_TIME, name) )
530 {
531 options->crossfade_time = atoi(value);
532 }
533 else if( !strcasecmp(CONF_SEARCH_MODE, name) )
534 {
535 options->search_mode = atoi(value);
536 }
537 else if( !strcasecmp(CONF_HIDE_CURSOR, name) )
538 {
539 options->hide_cursor = atoi(value);
540 }
541 else if( !strcasecmp(CONF_SEEK_TIME, name) )
542 {
543 options->seek_time = atoi(value);
544 }
545 else if( !strcasecmp(CONF_SCREEN_LIST, name) )
546 {
547 g_strfreev(options->screen_list);
548 options->screen_list = check_screen_list(value);
549 }
550 else if( !strcasecmp(CONF_SHOW_SPLASH, name) )
551 {
552 /* the splash screen was removed */
553 }
554 else if( !strcasecmp(CONF_HOST, name))
555 {
556 options->host = get_format(value);
557 }
558 else if( !strcasecmp(CONF_PORT, name))
559 {
560 options->port = atoi(get_format(value));
561 }
562 else if( !strcasecmp(CONF_PASSWORD, name))
563 {
564 options->password = get_format(value);
565 }
566 else if( !strcasecmp(CONF_LYRICS_TIMEOUT, name))
567 {
568 options->lyrics_timeout = atoi(get_format(value));
569 }
570 else if( !strcasecmp(CONF_SCROLL, name))
571 {
572 options->scroll = str2bool(value);
573 }
574 else if( !strcasecmp(CONF_SCROLL_SEP, name))
575 {
576 g_free(options->scroll_sep);
577 options->scroll_sep = get_format(value);
578 }
579 else
580 {
581 match_found = 0;
582 }
584 if( !match_found )
585 fprintf(stderr,
586 _("Unknown configuration parameter: %s\n"),
587 name);
588 D("conf> %s = %s %s\n", name, value,
589 match_found ? "" : "- UNKNOWN SETTING!" );
590 }
591 }
592 }
594 D("--\n\n");
596 if( free_filename )
597 g_free(filename);
599 return 0;
600 }
602 int
603 check_user_conf_dir(void)
604 {
605 int retval;
606 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
608 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
609 g_free(directory);
610 return 0;
611 }
613 retval = mkdir(directory, 0755);
614 g_free(directory);
615 return retval;
616 }
618 char *
619 get_user_key_binding_filename(void)
620 {
621 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
622 }
625 int
626 read_configuration(options_t *options)
627 {
628 char *filename = NULL;
630 /* check for command line configuration file */
631 if( options->config_file )
632 filename = g_strdup(options->config_file);
634 /* check for user configuration ~/.ncmpc/config */
635 if( filename == NULL )
636 {
637 filename = g_build_filename(g_get_home_dir(),
638 "." PACKAGE, "config", NULL);
639 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
640 {
641 g_free(filename);
642 filename = NULL;
643 }
644 }
646 /* check for global configuration SYSCONFDIR/ncmpc/config */
647 if( filename == NULL )
648 {
649 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
650 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
651 {
652 g_free(filename);
653 filename = NULL;
654 }
655 }
657 /* load configuration */
658 if( filename )
659 {
660 read_rc_file(filename, options);
661 g_free(filename);
662 filename = NULL;
663 }
665 /* check for command line key binding file */
666 if( options->key_file )
667 filename = g_strdup(options->key_file);
669 /* check for user key bindings ~/.ncmpc/keys */
670 if( filename == NULL )
671 {
672 filename = get_user_key_binding_filename();
673 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
674 {
675 g_free(filename);
676 filename = NULL;
677 }
678 }
680 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
681 if( filename == NULL )
682 {
683 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
684 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
685 {
686 g_free(filename);
687 filename = NULL;
688 }
689 }
691 /* load key bindings */
692 if( filename )
693 {
694 read_rc_file(filename, options);
695 g_free(filename);
696 filename = NULL;
697 }
699 return 0;
700 }