970bc0889d1650ed8fbcab6646091cfa0240d30b
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"
74 typedef enum {
75 KEY_PARSER_UNKNOWN,
76 KEY_PARSER_CHAR,
77 KEY_PARSER_DEC,
78 KEY_PARSER_HEX,
79 KEY_PARSER_DONE
80 } key_parser_state_t;
83 extern gint screen_get_id(char *name);
86 static gboolean
87 str2bool(char *str)
88 {
89 if (!strcasecmp(str, "yes") || !strcasecmp(str, "true") ||
90 !strcasecmp(str, "on") || !strcasecmp(str, "1"))
91 return TRUE;
92 return FALSE;
93 }
95 static int
96 parse_key_value(char *str, size_t len, char **end)
97 {
98 size_t i;
99 int value;
100 key_parser_state_t state;
102 i=0;
103 value=0;
104 state=KEY_PARSER_UNKNOWN;
105 *end = str;
107 while (i < len && state != KEY_PARSER_DONE) {
108 int next = 0;
109 int c = str[i];
111 if( i+1<len )
112 next = str[i+1];
114 switch(state) {
115 case KEY_PARSER_UNKNOWN:
116 if( c=='\'' )
117 state = KEY_PARSER_CHAR;
118 else if( c=='0' && next=='x' )
119 state = KEY_PARSER_HEX;
120 else if( isdigit(c) )
121 state = KEY_PARSER_DEC;
122 else {
123 fprintf(stderr,
124 _("Error: Unsupported key definition - %s\n"),
125 str);
126 return -1;
127 }
128 break;
129 case KEY_PARSER_CHAR:
130 if( next!='\'' ) {
131 fprintf(stderr,
132 _("Error: Unsupported key definition - %s\n"),
133 str);
134 return -1;
135 }
136 value = c;
137 *end = str+i+2;
138 state = KEY_PARSER_DONE;
139 break;
140 case KEY_PARSER_DEC:
141 value = (int) strtol(str+(i-1), end, 10);
142 state = KEY_PARSER_DONE;
143 break;
144 case KEY_PARSER_HEX:
145 if( !isdigit(next) ) {
146 fprintf(stderr,_("Error: Digit expected after 0x - %s\n"), str);
147 return -1;
148 }
149 value = (int) strtol(str+(i+1), end, 16);
150 state = KEY_PARSER_DONE;
151 break;
152 case KEY_PARSER_DONE:
153 break;
154 }
155 i++;
156 }
158 if( *end> str+len )
159 *end = str+len;
161 return value;
162 }
164 static int
165 parse_key_definition(char *str)
166 {
167 char buf[MAX_LINE_LENGTH];
168 char *p, *end;
169 size_t len = strlen(str), i;
170 int j,key;
171 int keys[MAX_COMMAND_KEYS];
172 command_t cmd;
174 /* get the command name */
175 i=0;
176 j=0;
177 memset(buf, 0, MAX_LINE_LENGTH);
178 while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
179 buf[j++] = str[i++];
180 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
181 fprintf(stderr, _("Error: Unknown key command %s\n"), buf);
182 return -1;
183 }
185 /* skip whitespace */
186 while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) )
187 i++;
189 /* get the value part */
190 memset(buf, 0, MAX_LINE_LENGTH);
191 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
192 len = strlen(buf);
193 if( len==0 ) {
194 fprintf(stderr,_("Error: Incomplete key definition - %s\n"), str);
195 return -1;
196 }
198 /* parse key values */
199 i = 0;
200 key = 0;
201 len = strlen(buf);
202 p = buf;
203 end = buf+len;
204 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
205 while( i<MAX_COMMAND_KEYS && p<end &&
206 (key=parse_key_value(p,len+1,&p))>=0 ) {
207 keys[i++] = key;
208 while( p<end && (*p==',' || *p==' ' || *p=='\t') )
209 p++;
210 len = strlen(p);
211 }
212 if( key<0 ) {
213 fprintf(stderr,_("Error: Bad key definition - %s\n"), str);
214 return -1;
215 }
217 return assign_keys(cmd, keys);
218 }
220 static const char *
221 parse_timedisplay_type(const char *str)
222 {
223 if((!strcmp(str,"elapsed")) || (!strcmp(str,"remaining"))){
224 return str;
225 } else {
226 fprintf(stderr,_("Error: Bad time display type - %s\n"), str);
227 return DEFAULT_TIMEDISPLAY_TYPE;
228 }
229 }
231 static int
232 parse_color(char *str)
233 {
234 const char *name = str;
235 const char *value = NULL;
236 int len,i;
238 i=0;
239 len=strlen(str);
240 /* get the color name */
241 while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
242 i++;
244 /* skip whitespace */
245 while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) ) {
246 str[i]='\0';
247 i++;
248 }
250 if( i<len )
251 value = str+i;
253 return colors_assign(name, value);
254 }
256 static int
257 parse_color_definition(char *str)
258 {
259 char buf[MAX_LINE_LENGTH];
260 char *p, *end, *name;
261 size_t len = strlen(str), i;
262 int j,value;
263 short color, rgb[3];
265 /* get the command name */
266 i=0;
267 j=0;
268 memset(buf, 0, MAX_LINE_LENGTH);
269 while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
270 buf[j++] = str[i++];
271 color=colors_str2color(buf);
272 if( color<0 ) {
273 fprintf(stderr,_("Error: Bad color %s [%d]\n"), buf, color);
274 return -1;
275 }
276 name = g_strdup(buf);
278 /* skip whitespace */
279 while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) )
280 i++;
282 /* get the value part */
283 memset(buf, 0, MAX_LINE_LENGTH);
284 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
285 len = strlen(buf);
286 if( len==0 ) {
287 fprintf(stderr,_("Error: Incomplete color definition - %s\n"), str);
288 g_free(name);
289 return -1;
290 }
292 /* parse r,g.b values with the key definition parser */
293 i = 0;
294 value = 0;
295 len = strlen(buf);
296 p = buf;
297 end = buf+len;
298 memset(rgb, 0, sizeof(short)*3);
299 while( i<3 && p<end && (value=parse_key_value(p,len+1,&p))>=0 ) {
300 rgb[i++] = value;
301 while( p<end && (*p==',' || *p==' ' || *p=='\t') )
302 p++;
303 len = strlen(p);
304 }
305 if( value<0 || i!=3) {
306 fprintf(stderr,_("Error: Bad color definition - %s\n"), str);
307 g_free(name);
308 return -1;
309 }
310 value = colors_define(name, rgb[0], rgb[1], rgb[2]);
311 g_free(name);
312 return value;
313 }
315 static char *
316 get_format(char *str)
317 {
318 gsize len = strlen(str);
320 if( str && str[0]=='\"' && str[len-1] == '\"' ) {
321 str[len-1] = '\0';
322 str++;
323 }
324 return g_strdup(str);
325 }
327 static char **
328 check_screen_list(char *value)
329 {
330 char **tmp = g_strsplit_set(value, " \t,", 100);
331 char **screen = NULL;
332 int i,j;
334 i=0;
335 j=0;
336 while( tmp && tmp[i] ) {
337 tmp[i] = lowerstr(tmp[i]);
338 if( screen_get_id(tmp[i]) == -1 )
339 fprintf(stderr,
340 _("Error: Unsupported screen \"%s\"\n"),
341 tmp[i]);
342 else {
343 screen = g_realloc(screen, (j+2)*sizeof(char *));
344 screen[j++] = g_strdup(tmp[i]);
345 screen[j] = NULL;
346 }
347 i++;
348 }
349 g_strfreev(tmp);
350 if( screen == NULL )
351 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
353 return screen;
354 }
356 static int
357 read_rc_file(char *filename, options_t *options)
358 {
359 int fd;
360 int quit = 0;
361 int free_filename = 0;
363 if( filename==NULL )
364 return -1;
366 D("Reading configuration file %s\n", filename);
367 if( (fd=open(filename,O_RDONLY)) <0 )
368 {
369 perror(filename);
370 if( free_filename )
371 g_free(filename);
372 return -1;
373 }
375 while( !quit )
376 {
377 int i,j;
378 int len;
379 int match_found;
380 char line[MAX_LINE_LENGTH];
381 char name[MAX_LINE_LENGTH];
382 char value[MAX_LINE_LENGTH];
384 line[0] = '\0';
385 value[0] = '\0';
387 i = 0;
388 /* read a line ending with '\n' */
389 do {
390 len = read( fd, &line[i], 1 );
391 if( len == 1 )
392 i++;
393 else
394 quit = 1;
395 } while( !quit && i<MAX_LINE_LENGTH && line[i-1]!='\n' );
398 /* remove trailing whitespace */
399 line[i] = '\0';
400 i--;
401 while( i>=0 && IS_WHITESPACE(line[i]) )
402 {
403 line[i] = '\0';
404 i--;
405 }
406 len = i+1;
408 if( len>0 )
409 {
410 i = 0;
411 /* skip whitespace */
412 while( i<len && IS_WHITESPACE(line[i]) )
413 i++;
415 /* continue if this line is not a comment */
416 if( line[i] != COMMENT_TOKEN )
417 {
418 /* get the name part */
419 j=0;
420 while( i<len && line[i]!='=' && !IS_WHITESPACE(line[i]) )
421 {
422 name[j++] = line[i++];
423 }
424 name[j] = '\0';
426 /* skip '=' and whitespace */
427 while( i<len && (line[i]=='=' || IS_WHITESPACE(line[i])) )
428 i++;
430 /* get the value part */
431 j=0;
432 while( i<len )
433 {
434 value[j++] = line[i++];
435 }
436 value[j] = '\0';
438 match_found = 1;
440 /* key definition */
441 if( !strcasecmp(CONF_KEY_DEFINITION, name) )
442 {
443 parse_key_definition(value);
444 }
445 /* enable colors */
446 else if( !strcasecmp(CONF_ENABLE_COLORS, name) )
447 {
448 options->enable_colors = str2bool(value);
449 }
450 /* auto center */
451 else if( !strcasecmp(CONF_AUTO_CENTER, name) )
452 {
453 options->auto_center = str2bool(value);
454 }
455 /* color assignment */
456 else if( !strcasecmp(CONF_COLOR, name) )
457 {
458 parse_color(value);
459 }
460 /* wide cursor */
461 else if( !strcasecmp(CONF_WIDE_CURSOR, name) )
462 {
463 options->wide_cursor = str2bool(value);
464 }
465 /* timer display type */
466 else if( !strcasecmp(CONF_TIMEDISPLAY_TYPE, name) )
467 {
468 g_free(options->timedisplay_type);
469 options->timedisplay_type=g_strdup(parse_timedisplay_type(value));
470 D("deb");
471 D(options->timedisplay_type);
472 }
473 /* color definition */
474 else if( !strcasecmp(CONF_COLOR_DEFINITION, name) )
475 {
476 parse_color_definition(value);
477 }
478 /* list format string */
479 else if( !strcasecmp(CONF_LIST_FORMAT, name) )
480 {
481 g_free(options->list_format);
482 options->list_format = get_format(value);
483 }
484 /* status format string */
485 else if( !strcasecmp(CONF_STATUS_FORMAT, name) )
486 {
487 g_free(options->status_format);
488 options->status_format = get_format(value);
489 }
490 /* xterm title format string */
491 else if( !strcasecmp(CONF_XTERM_TITLE_FORMAT, name) )
492 {
493 g_free(options->xterm_title_format);
494 options->xterm_title_format = get_format(value);
495 }
496 else if( !strcasecmp(CONF_LIST_WRAP, name) )
497 {
498 options->list_wrap = str2bool(value);
499 }
500 else if( !strcasecmp(CONF_FIND_WRAP, name) )
501 {
502 options->find_wrap = str2bool(value);
503 }
504 else if( !strcasecmp(CONF_FIND_SHOW_LAST,name) )
505 {
506 options->find_show_last_pattern = str2bool(value);
507 }
508 else if( !strcasecmp(CONF_AUDIBLE_BELL, name) )
509 {
510 options->audible_bell = str2bool(value);
511 }
512 else if( !strcasecmp(CONF_VISIBLE_BELL, name) )
513 {
514 options->visible_bell = str2bool(value);
515 }
516 else if( !strcasecmp(CONF_XTERM_TITLE, name) )
517 {
518 options->enable_xterm_title = str2bool(value);
519 }
520 else if( !strcasecmp(CONF_ENABLE_MOUSE, name) )
521 {
522 options->enable_mouse = str2bool(value);
523 }
524 else if( !strcasecmp(CONF_CROSSFADE_TIME, name) )
525 {
526 options->crossfade_time = atoi(value);
527 }
528 else if( !strcasecmp(CONF_SEARCH_MODE, name) )
529 {
530 options->search_mode = atoi(value);
531 }
532 else if( !strcasecmp(CONF_HIDE_CURSOR, name) )
533 {
534 options->hide_cursor = atoi(value);
535 }
536 else if( !strcasecmp(CONF_SEEK_TIME, name) )
537 {
538 options->seek_time = atoi(value);
539 }
540 else if( !strcasecmp(CONF_SCREEN_LIST, name) )
541 {
542 g_strfreev(options->screen_list);
543 options->screen_list = check_screen_list(value);
544 }
545 else if( !strcasecmp(CONF_SHOW_SPLASH, name) )
546 {
547 /* the splash screen was removed */
548 }
549 else if( !strcasecmp(CONF_HOST, name))
550 {
551 options->host = get_format(value);
552 }
553 else if( !strcasecmp(CONF_PORT, name))
554 {
555 options->port = atoi(get_format(value));
556 }
557 else if( !strcasecmp(CONF_PASSWORD, name))
558 {
559 options->password = get_format(value);
560 }
561 else if( !strcasecmp(CONF_LYRICS_TIMEOUT, name))
562 {
563 options->lyrics_timeout = atoi(get_format(value));
564 }
565 else if( !strcasecmp(CONF_SCROLL, name))
566 {
567 options->scroll = str2bool(value);
568 }
569 else if( !strcasecmp(CONF_SCROLL_SEP, name))
570 {
571 g_free(options->scroll_sep);
572 options->scroll_sep = get_format(value);
573 }
574 else
575 {
576 match_found = 0;
577 }
579 if( !match_found )
580 fprintf(stderr,
581 _("Unknown configuration parameter: %s\n"),
582 name);
583 D("conf> %s = %s %s\n", name, value,
584 match_found ? "" : "- UNKNOWN SETTING!" );
585 }
586 }
587 }
589 D("--\n\n");
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 }