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 #include "conf.h"
22 #include "config.h"
23 #include "ncmpc.h"
24 #include "support.h"
25 #include "command.h"
26 #include "colors.h"
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.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"
73 typedef enum {
74 KEY_PARSER_UNKNOWN,
75 KEY_PARSER_CHAR,
76 KEY_PARSER_DEC,
77 KEY_PARSER_HEX,
78 KEY_PARSER_DONE
79 } key_parser_state_t;
82 extern gint screen_get_id(char *name);
85 static gboolean
86 str2bool(char *str)
87 {
88 if (!strcasecmp(str, "yes") || !strcasecmp(str, "true") ||
89 !strcasecmp(str, "on") || !strcasecmp(str, "1"))
90 return TRUE;
91 return FALSE;
92 }
94 static int
95 parse_key_value(char *str, size_t len, char **end)
96 {
97 int i, 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);
168 int i,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);
260 int i,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 tmp[i] = lowerstr(tmp[i]);
336 if( screen_get_id(tmp[i]) == -1 )
337 fprintf(stderr,
338 _("Error: Unsupported screen \"%s\"\n"),
339 tmp[i]);
340 else {
341 screen = g_realloc(screen, (j+2)*sizeof(char *));
342 screen[j++] = g_strdup(tmp[i]);
343 screen[j] = NULL;
344 }
345 i++;
346 }
347 g_strfreev(tmp);
348 if( screen == NULL )
349 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
351 return screen;
352 }
354 static int
355 read_rc_file(char *filename, options_t *options)
356 {
357 int fd;
358 int quit = 0;
359 int free_filename = 0;
361 if( filename==NULL )
362 return -1;
364 D("Reading configuration file %s\n", filename);
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 /* timer display type */
464 else if( !strcasecmp(CONF_TIMEDISPLAY_TYPE, name) )
465 {
466 g_free(options->timedisplay_type);
467 options->timedisplay_type=g_strdup(parse_timedisplay_type(value));
468 D("deb");
469 D(options->timedisplay_type);
470 }
471 /* color definition */
472 else if( !strcasecmp(CONF_COLOR_DEFINITION, name) )
473 {
474 parse_color_definition(value);
475 }
476 /* list format string */
477 else if( !strcasecmp(CONF_LIST_FORMAT, name) )
478 {
479 g_free(options->list_format);
480 options->list_format = get_format(value);
481 }
482 /* status format string */
483 else if( !strcasecmp(CONF_STATUS_FORMAT, name) )
484 {
485 g_free(options->status_format);
486 options->status_format = get_format(value);
487 }
488 /* xterm title format string */
489 else if( !strcasecmp(CONF_XTERM_TITLE_FORMAT, name) )
490 {
491 g_free(options->xterm_title_format);
492 options->xterm_title_format = get_format(value);
493 }
494 else if( !strcasecmp(CONF_LIST_WRAP, name) )
495 {
496 options->list_wrap = str2bool(value);
497 }
498 else if( !strcasecmp(CONF_FIND_WRAP, name) )
499 {
500 options->find_wrap = str2bool(value);
501 }
502 else if( !strcasecmp(CONF_FIND_SHOW_LAST,name) )
503 {
504 options->find_show_last_pattern = str2bool(value);
505 }
506 else if( !strcasecmp(CONF_AUDIBLE_BELL, name) )
507 {
508 options->audible_bell = str2bool(value);
509 }
510 else if( !strcasecmp(CONF_VISIBLE_BELL, name) )
511 {
512 options->visible_bell = str2bool(value);
513 }
514 else if( !strcasecmp(CONF_XTERM_TITLE, name) )
515 {
516 options->enable_xterm_title = str2bool(value);
517 }
518 else if( !strcasecmp(CONF_ENABLE_MOUSE, name) )
519 {
520 options->enable_mouse = str2bool(value);
521 }
522 else if( !strcasecmp(CONF_CROSSFADE_TIME, name) )
523 {
524 options->crossfade_time = atoi(value);
525 }
526 else if( !strcasecmp(CONF_SEARCH_MODE, name) )
527 {
528 options->search_mode = atoi(value);
529 }
530 else if( !strcasecmp(CONF_HIDE_CURSOR, name) )
531 {
532 options->hide_cursor = atoi(value);
533 }
534 else if( !strcasecmp(CONF_SEEK_TIME, name) )
535 {
536 options->seek_time = atoi(value);
537 }
538 else if( !strcasecmp(CONF_SCREEN_LIST, name) )
539 {
540 g_strfreev(options->screen_list);
541 options->screen_list = check_screen_list(value);
542 }
543 else if( !strcasecmp(CONF_SHOW_SPLASH, name) )
544 {
545 /* the splash screen was removed */
546 }
547 else if( !strcasecmp(CONF_HOST, name))
548 {
549 options->host = get_format(value);
550 }
551 else if( !strcasecmp(CONF_PORT, name))
552 {
553 options->port = atoi(get_format(value));
554 }
555 else if( !strcasecmp(CONF_PASSWORD, name))
556 {
557 options->password = get_format(value);
558 }
559 else if( !strcasecmp(CONF_LYRICS_TIMEOUT, name))
560 {
561 options->lyrics_timeout = atoi(get_format(value));
562 }
563 else if( !strcasecmp(CONF_SCROLL, name))
564 {
565 options->scroll = str2bool(value);
566 }
567 else if( !strcasecmp(CONF_SCROLL_SEP, name))
568 {
569 g_free(options->scroll_sep);
570 options->scroll_sep = get_format(value);
571 }
572 else
573 {
574 match_found = 0;
575 }
577 if( !match_found )
578 fprintf(stderr,
579 _("Unknown configuration parameter: %s\n"),
580 name);
581 D("conf> %s = %s %s\n", name, value,
582 match_found ? "" : "- UNKNOWN SETTING!" );
583 }
584 }
585 }
587 D("--\n\n");
589 if( free_filename )
590 g_free(filename);
592 return 0;
593 }
595 int
596 check_user_conf_dir(void)
597 {
598 int retval;
599 char *dirname = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
601 if (g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
602 g_free(dirname);
603 return 0;
604 }
606 retval = mkdir(dirname, 0755);
607 g_free(dirname);
608 return retval;
609 }
611 char *
612 get_user_key_binding_filename(void)
613 {
614 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
615 }
618 int
619 read_configuration(options_t *options)
620 {
621 char *filename = NULL;
623 /* check for command line configuration file */
624 if( options->config_file )
625 filename = g_strdup(options->config_file);
627 /* check for user configuration ~/.ncmpc/config */
628 if( filename == NULL )
629 {
630 filename = g_build_filename(g_get_home_dir(),
631 "." PACKAGE, "config", NULL);
632 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
633 {
634 g_free(filename);
635 filename = NULL;
636 }
637 }
639 /* check for global configuration SYSCONFDIR/ncmpc/config */
640 if( filename == NULL )
641 {
642 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
643 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
644 {
645 g_free(filename);
646 filename = NULL;
647 }
648 }
650 /* load configuration */
651 if( filename )
652 {
653 read_rc_file(filename, options);
654 g_free(filename);
655 filename = NULL;
656 }
658 /* check for command line key binding file */
659 if( options->key_file )
660 filename = g_strdup(options->key_file);
662 /* check for user key bindings ~/.ncmpc/keys */
663 if( filename == NULL )
664 {
665 filename = get_user_key_binding_filename();
666 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
667 {
668 g_free(filename);
669 filename = NULL;
670 }
671 }
673 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
674 if( filename == NULL )
675 {
676 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
677 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
678 {
679 g_free(filename);
680 filename = NULL;
681 }
682 }
684 /* load key bindings */
685 if( filename )
686 {
687 read_rc_file(filename, options);
688 g_free(filename);
689 filename = NULL;
690 }
692 return 0;
693 }