b49eeb2440ad4e920660252f7f63ff0d017470c4
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 size_t i;
98 int value;
99 key_parser_state_t state;
101 i=0;
102 value=0;
103 state=KEY_PARSER_UNKNOWN;
104 *end = str;
106 while (i < len && state != KEY_PARSER_DONE) {
107 int next = 0;
108 int c = str[i];
110 if( i+1<len )
111 next = str[i+1];
113 switch(state) {
114 case KEY_PARSER_UNKNOWN:
115 if( c=='\'' )
116 state = KEY_PARSER_CHAR;
117 else if( c=='0' && next=='x' )
118 state = KEY_PARSER_HEX;
119 else if( isdigit(c) )
120 state = KEY_PARSER_DEC;
121 else {
122 fprintf(stderr,
123 _("Error: Unsupported key definition - %s\n"),
124 str);
125 return -1;
126 }
127 break;
128 case KEY_PARSER_CHAR:
129 if( next!='\'' ) {
130 fprintf(stderr,
131 _("Error: Unsupported key definition - %s\n"),
132 str);
133 return -1;
134 }
135 value = c;
136 *end = str+i+2;
137 state = KEY_PARSER_DONE;
138 break;
139 case KEY_PARSER_DEC:
140 value = (int) strtol(str+(i-1), end, 10);
141 state = KEY_PARSER_DONE;
142 break;
143 case KEY_PARSER_HEX:
144 if( !isdigit(next) ) {
145 fprintf(stderr,_("Error: Digit expected after 0x - %s\n"), str);
146 return -1;
147 }
148 value = (int) strtol(str+(i+1), end, 16);
149 state = KEY_PARSER_DONE;
150 break;
151 case KEY_PARSER_DONE:
152 break;
153 }
154 i++;
155 }
157 if( *end> str+len )
158 *end = str+len;
160 return value;
161 }
163 static int
164 parse_key_definition(char *str)
165 {
166 char buf[MAX_LINE_LENGTH];
167 char *p, *end;
168 size_t len = strlen(str), i;
169 int j,key;
170 int keys[MAX_COMMAND_KEYS];
171 command_t cmd;
173 /* get the command name */
174 i=0;
175 j=0;
176 memset(buf, 0, MAX_LINE_LENGTH);
177 while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
178 buf[j++] = str[i++];
179 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
180 fprintf(stderr, _("Error: Unknown key command %s\n"), buf);
181 return -1;
182 }
184 /* skip whitespace */
185 while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) )
186 i++;
188 /* get the value part */
189 memset(buf, 0, MAX_LINE_LENGTH);
190 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
191 len = strlen(buf);
192 if( len==0 ) {
193 fprintf(stderr,_("Error: Incomplete key definition - %s\n"), str);
194 return -1;
195 }
197 /* parse key values */
198 i = 0;
199 key = 0;
200 len = strlen(buf);
201 p = buf;
202 end = buf+len;
203 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
204 while( i<MAX_COMMAND_KEYS && p<end &&
205 (key=parse_key_value(p,len+1,&p))>=0 ) {
206 keys[i++] = key;
207 while( p<end && (*p==',' || *p==' ' || *p=='\t') )
208 p++;
209 len = strlen(p);
210 }
211 if( key<0 ) {
212 fprintf(stderr,_("Error: Bad key definition - %s\n"), str);
213 return -1;
214 }
216 return assign_keys(cmd, keys);
217 }
219 static const char *
220 parse_timedisplay_type(const char *str)
221 {
222 if((!strcmp(str,"elapsed")) || (!strcmp(str,"remaining"))){
223 return str;
224 } else {
225 fprintf(stderr,_("Error: Bad time display type - %s\n"), str);
226 return DEFAULT_TIMEDISPLAY_TYPE;
227 }
228 }
230 static int
231 parse_color(char *str)
232 {
233 const char *name = str;
234 const char *value = NULL;
235 int len,i;
237 i=0;
238 len=strlen(str);
239 /* get the color name */
240 while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
241 i++;
243 /* skip whitespace */
244 while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) ) {
245 str[i]='\0';
246 i++;
247 }
249 if( i<len )
250 value = str+i;
252 return colors_assign(name, value);
253 }
255 static int
256 parse_color_definition(char *str)
257 {
258 char buf[MAX_LINE_LENGTH];
259 char *p, *end, *name;
260 size_t len = strlen(str), i;
261 int j,value;
262 short color, rgb[3];
264 /* get the command name */
265 i=0;
266 j=0;
267 memset(buf, 0, MAX_LINE_LENGTH);
268 while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
269 buf[j++] = str[i++];
270 color=colors_str2color(buf);
271 if( color<0 ) {
272 fprintf(stderr,_("Error: Bad color %s [%d]\n"), buf, color);
273 return -1;
274 }
275 name = g_strdup(buf);
277 /* skip whitespace */
278 while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) )
279 i++;
281 /* get the value part */
282 memset(buf, 0, MAX_LINE_LENGTH);
283 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
284 len = strlen(buf);
285 if( len==0 ) {
286 fprintf(stderr,_("Error: Incomplete color definition - %s\n"), str);
287 g_free(name);
288 return -1;
289 }
291 /* parse r,g.b values with the key definition parser */
292 i = 0;
293 value = 0;
294 len = strlen(buf);
295 p = buf;
296 end = buf+len;
297 memset(rgb, 0, sizeof(short)*3);
298 while( i<3 && p<end && (value=parse_key_value(p,len+1,&p))>=0 ) {
299 rgb[i++] = value;
300 while( p<end && (*p==',' || *p==' ' || *p=='\t') )
301 p++;
302 len = strlen(p);
303 }
304 if( value<0 || i!=3) {
305 fprintf(stderr,_("Error: Bad color definition - %s\n"), str);
306 g_free(name);
307 return -1;
308 }
309 value = colors_define(name, rgb[0], rgb[1], rgb[2]);
310 g_free(name);
311 return value;
312 }
314 static char *
315 get_format(char *str)
316 {
317 gsize len = strlen(str);
319 if( str && str[0]=='\"' && str[len-1] == '\"' ) {
320 str[len-1] = '\0';
321 str++;
322 }
323 return g_strdup(str);
324 }
326 static char **
327 check_screen_list(char *value)
328 {
329 char **tmp = g_strsplit_set(value, " \t,", 100);
330 char **screen = NULL;
331 int i,j;
333 i=0;
334 j=0;
335 while( tmp && tmp[i] ) {
336 tmp[i] = lowerstr(tmp[i]);
337 if( screen_get_id(tmp[i]) == -1 )
338 fprintf(stderr,
339 _("Error: Unsupported screen \"%s\"\n"),
340 tmp[i]);
341 else {
342 screen = g_realloc(screen, (j+2)*sizeof(char *));
343 screen[j++] = g_strdup(tmp[i]);
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 D("Reading configuration file %s\n", filename);
366 if( (fd=open(filename,O_RDONLY)) <0 )
367 {
368 perror(filename);
369 if( free_filename )
370 g_free(filename);
371 return -1;
372 }
374 while( !quit )
375 {
376 int i,j;
377 int len;
378 int match_found;
379 char line[MAX_LINE_LENGTH];
380 char name[MAX_LINE_LENGTH];
381 char value[MAX_LINE_LENGTH];
383 line[0] = '\0';
384 value[0] = '\0';
386 i = 0;
387 /* read a line ending with '\n' */
388 do {
389 len = read( fd, &line[i], 1 );
390 if( len == 1 )
391 i++;
392 else
393 quit = 1;
394 } while( !quit && i<MAX_LINE_LENGTH && line[i-1]!='\n' );
397 /* remove trailing whitespace */
398 line[i] = '\0';
399 i--;
400 while( i>=0 && IS_WHITESPACE(line[i]) )
401 {
402 line[i] = '\0';
403 i--;
404 }
405 len = i+1;
407 if( len>0 )
408 {
409 i = 0;
410 /* skip whitespace */
411 while( i<len && IS_WHITESPACE(line[i]) )
412 i++;
414 /* continue if this line is not a comment */
415 if( line[i] != COMMENT_TOKEN )
416 {
417 /* get the name part */
418 j=0;
419 while( i<len && line[i]!='=' && !IS_WHITESPACE(line[i]) )
420 {
421 name[j++] = line[i++];
422 }
423 name[j] = '\0';
425 /* skip '=' and whitespace */
426 while( i<len && (line[i]=='=' || IS_WHITESPACE(line[i])) )
427 i++;
429 /* get the value part */
430 j=0;
431 while( i<len )
432 {
433 value[j++] = line[i++];
434 }
435 value[j] = '\0';
437 match_found = 1;
439 /* key definition */
440 if( !strcasecmp(CONF_KEY_DEFINITION, name) )
441 {
442 parse_key_definition(value);
443 }
444 /* enable colors */
445 else if( !strcasecmp(CONF_ENABLE_COLORS, name) )
446 {
447 options->enable_colors = str2bool(value);
448 }
449 /* auto center */
450 else if( !strcasecmp(CONF_AUTO_CENTER, name) )
451 {
452 options->auto_center = str2bool(value);
453 }
454 /* color assignment */
455 else if( !strcasecmp(CONF_COLOR, name) )
456 {
457 parse_color(value);
458 }
459 /* wide cursor */
460 else if( !strcasecmp(CONF_WIDE_CURSOR, name) )
461 {
462 options->wide_cursor = str2bool(value);
463 }
464 /* timer display type */
465 else if( !strcasecmp(CONF_TIMEDISPLAY_TYPE, name) )
466 {
467 g_free(options->timedisplay_type);
468 options->timedisplay_type=g_strdup(parse_timedisplay_type(value));
469 D("deb");
470 D(options->timedisplay_type);
471 }
472 /* color definition */
473 else if( !strcasecmp(CONF_COLOR_DEFINITION, name) )
474 {
475 parse_color_definition(value);
476 }
477 /* list format string */
478 else if( !strcasecmp(CONF_LIST_FORMAT, name) )
479 {
480 g_free(options->list_format);
481 options->list_format = get_format(value);
482 }
483 /* status format string */
484 else if( !strcasecmp(CONF_STATUS_FORMAT, name) )
485 {
486 g_free(options->status_format);
487 options->status_format = get_format(value);
488 }
489 /* xterm title format string */
490 else if( !strcasecmp(CONF_XTERM_TITLE_FORMAT, name) )
491 {
492 g_free(options->xterm_title_format);
493 options->xterm_title_format = get_format(value);
494 }
495 else if( !strcasecmp(CONF_LIST_WRAP, name) )
496 {
497 options->list_wrap = str2bool(value);
498 }
499 else if( !strcasecmp(CONF_FIND_WRAP, name) )
500 {
501 options->find_wrap = str2bool(value);
502 }
503 else if( !strcasecmp(CONF_FIND_SHOW_LAST,name) )
504 {
505 options->find_show_last_pattern = str2bool(value);
506 }
507 else if( !strcasecmp(CONF_AUDIBLE_BELL, name) )
508 {
509 options->audible_bell = str2bool(value);
510 }
511 else if( !strcasecmp(CONF_VISIBLE_BELL, name) )
512 {
513 options->visible_bell = str2bool(value);
514 }
515 else if( !strcasecmp(CONF_XTERM_TITLE, name) )
516 {
517 options->enable_xterm_title = str2bool(value);
518 }
519 else if( !strcasecmp(CONF_ENABLE_MOUSE, name) )
520 {
521 options->enable_mouse = str2bool(value);
522 }
523 else if( !strcasecmp(CONF_CROSSFADE_TIME, name) )
524 {
525 options->crossfade_time = atoi(value);
526 }
527 else if( !strcasecmp(CONF_SEARCH_MODE, name) )
528 {
529 options->search_mode = atoi(value);
530 }
531 else if( !strcasecmp(CONF_HIDE_CURSOR, name) )
532 {
533 options->hide_cursor = atoi(value);
534 }
535 else if( !strcasecmp(CONF_SEEK_TIME, name) )
536 {
537 options->seek_time = atoi(value);
538 }
539 else if( !strcasecmp(CONF_SCREEN_LIST, name) )
540 {
541 g_strfreev(options->screen_list);
542 options->screen_list = check_screen_list(value);
543 }
544 else if( !strcasecmp(CONF_SHOW_SPLASH, name) )
545 {
546 /* the splash screen was removed */
547 }
548 else if( !strcasecmp(CONF_HOST, name))
549 {
550 options->host = get_format(value);
551 }
552 else if( !strcasecmp(CONF_PORT, name))
553 {
554 options->port = atoi(get_format(value));
555 }
556 else if( !strcasecmp(CONF_PASSWORD, name))
557 {
558 options->password = get_format(value);
559 }
560 else if( !strcasecmp(CONF_LYRICS_TIMEOUT, name))
561 {
562 options->lyrics_timeout = atoi(get_format(value));
563 }
564 else if( !strcasecmp(CONF_SCROLL, name))
565 {
566 options->scroll = str2bool(value);
567 }
568 else if( !strcasecmp(CONF_SCROLL_SEP, name))
569 {
570 g_free(options->scroll_sep);
571 options->scroll_sep = get_format(value);
572 }
573 else
574 {
575 match_found = 0;
576 }
578 if( !match_found )
579 fprintf(stderr,
580 _("Unknown configuration parameter: %s\n"),
581 name);
582 D("conf> %s = %s %s\n", name, value,
583 match_found ? "" : "- UNKNOWN SETTING!" );
584 }
585 }
586 }
588 D("--\n\n");
590 if( free_filename )
591 g_free(filename);
593 return 0;
594 }
596 int
597 check_user_conf_dir(void)
598 {
599 int retval;
600 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
602 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
603 g_free(directory);
604 return 0;
605 }
607 retval = mkdir(directory, 0755);
608 g_free(directory);
609 return retval;
610 }
612 char *
613 get_user_key_binding_filename(void)
614 {
615 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
616 }
619 int
620 read_configuration(options_t *options)
621 {
622 char *filename = NULL;
624 /* check for command line configuration file */
625 if( options->config_file )
626 filename = g_strdup(options->config_file);
628 /* check for user configuration ~/.ncmpc/config */
629 if( filename == NULL )
630 {
631 filename = g_build_filename(g_get_home_dir(),
632 "." PACKAGE, "config", NULL);
633 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
634 {
635 g_free(filename);
636 filename = NULL;
637 }
638 }
640 /* check for global configuration SYSCONFDIR/ncmpc/config */
641 if( filename == NULL )
642 {
643 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
644 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
645 {
646 g_free(filename);
647 filename = NULL;
648 }
649 }
651 /* load configuration */
652 if( filename )
653 {
654 read_rc_file(filename, options);
655 g_free(filename);
656 filename = NULL;
657 }
659 /* check for command line key binding file */
660 if( options->key_file )
661 filename = g_strdup(options->key_file);
663 /* check for user key bindings ~/.ncmpc/keys */
664 if( filename == NULL )
665 {
666 filename = get_user_key_binding_filename();
667 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
668 {
669 g_free(filename);
670 filename = NULL;
671 }
672 }
674 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
675 if( filename == NULL )
676 {
677 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
678 if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
679 {
680 g_free(filename);
681 filename = NULL;
682 }
683 }
685 /* load key bindings */
686 if( filename )
687 {
688 read_rc_file(filename, options);
689 g_free(filename);
690 filename = NULL;
691 }
693 return 0;
694 }