1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #define NO_GLOBAL_OPTIONS
20 #include "conf.h"
21 #include "config.h"
22 #include "defaults.h"
23 #include "i18n.h"
24 #include "command.h"
25 #include "colors.h"
26 #include "screen_list.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>
37 #include <glib.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"
74 #define CONF_WELCOME_SCREEN_LIST "welcome-screen-list"
76 typedef enum {
77 KEY_PARSER_UNKNOWN,
78 KEY_PARSER_CHAR,
79 KEY_PARSER_DEC,
80 KEY_PARSER_HEX,
81 KEY_PARSER_DONE
82 } key_parser_state_t;
84 static bool
85 str2bool(char *str)
86 {
87 return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
88 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
89 }
91 static int
92 parse_key_value(char *str, size_t len, char **end)
93 {
94 size_t i;
95 int value;
96 key_parser_state_t state;
98 i=0;
99 value=0;
100 state=KEY_PARSER_UNKNOWN;
101 *end = str;
103 while (i < len && state != KEY_PARSER_DONE) {
104 int next = 0;
105 int c = str[i];
107 if( i+1<len )
108 next = str[i+1];
110 switch(state) {
111 case KEY_PARSER_UNKNOWN:
112 if( c=='\'' )
113 state = KEY_PARSER_CHAR;
114 else if( c=='0' && next=='x' )
115 state = KEY_PARSER_HEX;
116 else if( isdigit(c) )
117 state = KEY_PARSER_DEC;
118 else {
119 fprintf(stderr,
120 _("Error: Unsupported key definition - %s\n"),
121 str);
122 return -1;
123 }
124 break;
125 case KEY_PARSER_CHAR:
126 if( next!='\'' ) {
127 fprintf(stderr,
128 _("Error: Unsupported key definition - %s\n"),
129 str);
130 return -1;
131 }
132 value = c;
133 *end = str+i+2;
134 state = KEY_PARSER_DONE;
135 break;
136 case KEY_PARSER_DEC:
137 value = (int) strtol(str+(i-1), end, 10);
138 state = KEY_PARSER_DONE;
139 break;
140 case KEY_PARSER_HEX:
141 if( !isdigit(next) ) {
142 fprintf(stderr,_("Error: Digit expected after 0x - %s\n"), str);
143 return -1;
144 }
145 value = (int) strtol(str+(i+1), end, 16);
146 state = KEY_PARSER_DONE;
147 break;
148 case KEY_PARSER_DONE:
149 break;
150 }
151 i++;
152 }
154 if( *end> str+len )
155 *end = str+len;
157 return value;
158 }
160 static int
161 parse_key_definition(char *str)
162 {
163 char buf[MAX_LINE_LENGTH];
164 char *p, *end;
165 size_t len = strlen(str), i;
166 int j,key;
167 int keys[MAX_COMMAND_KEYS];
168 command_t cmd;
170 /* get the command name */
171 i=0;
172 j=0;
173 memset(buf, 0, MAX_LINE_LENGTH);
174 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
175 buf[j++] = str[i++];
176 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
177 fprintf(stderr, _("Error: Unknown key command %s\n"), buf);
178 return -1;
179 }
181 /* skip whitespace */
182 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
183 i++;
185 /* get the value part */
186 memset(buf, 0, MAX_LINE_LENGTH);
187 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
188 len = strlen(buf);
189 if (len == 0) {
190 fprintf(stderr,_("Error: Incomplete key definition - %s\n"), str);
191 return -1;
192 }
194 /* parse key values */
195 i = 0;
196 key = 0;
197 len = strlen(buf);
198 p = buf;
199 end = buf+len;
200 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
201 while (i < MAX_COMMAND_KEYS && p < end &&
202 (key = parse_key_value(p, len + 1, &p)) >= 0) {
203 keys[i++] = key;
204 while (p < end && (*p==',' || *p==' ' || *p=='\t'))
205 p++;
206 len = strlen(p);
207 }
209 if (key < 0) {
210 fprintf(stderr,_("Error: Bad key definition - %s\n"), str);
211 return -1;
212 }
214 return assign_keys(cmd, keys);
215 }
217 static const char *
218 parse_timedisplay_type(const char *str)
219 {
220 if (!strcmp(str,"elapsed") || !strcmp(str,"remaining"))
221 return str;
222 else {
223 fprintf(stderr,_("Error: Bad time display type - %s\n"), str);
224 return DEFAULT_TIMEDISPLAY_TYPE;
225 }
226 }
228 #ifdef ENABLE_COLORS
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] != '=' && !g_ascii_isspace(str[i]))
240 i++;
242 /* skip whitespace */
243 while (i < len && (str[i] == '=' || g_ascii_isspace(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] != '=' && !g_ascii_isspace(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] == '=' || g_ascii_isspace(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"),
286 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 &&
299 (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 }
306 if (value < 0 || i != 3) {
307 fprintf(stderr, _("Error: Bad color definition - %s\n"), str);
308 g_free(name);
309 return -1;
310 }
312 value = colors_define(name, rgb[0], rgb[1], rgb[2]);
313 g_free(name);
314 return value;
315 }
316 #endif
318 static char *
319 get_format(char *str)
320 {
321 gsize len = strlen(str);
323 if (str && str[0]=='\"' && str[len-1] == '\"') {
324 str[len - 1] = '\0';
325 str++;
326 }
328 return g_strdup(str);
329 }
331 static char **
332 check_screen_list(char *value)
333 {
334 char **tmp = g_strsplit_set(value, " \t,", 100);
335 char **screen = NULL;
336 int i,j;
338 i=0;
339 j=0;
340 while( tmp && tmp[i] ) {
341 char *name = g_ascii_strdown(tmp[i], -1);
342 if (screen_lookup_name(name) == NULL) {
343 fprintf(stderr,
344 _("Error: Unsupported screen \"%s\"\n"),
345 name);
346 free(name);
347 } else {
348 screen = g_realloc(screen, (j+2)*sizeof(char *));
349 screen[j++] = name;
350 screen[j] = NULL;
351 }
352 i++;
353 }
354 g_strfreev(tmp);
355 if( screen == NULL )
356 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
358 return screen;
359 }
361 static int
362 read_rc_file(char *filename, options_t *options)
363 {
364 int fd;
365 int quit = 0;
366 int free_filename = 0;
368 if (filename == NULL)
369 return -1;
371 if ((fd = open(filename,O_RDONLY)) < 0) {
372 perror(filename);
373 if (free_filename)
374 g_free(filename);
375 return -1;
376 }
378 while (!quit) {
379 int i,j;
380 int len;
381 int match_found;
382 char line[MAX_LINE_LENGTH];
383 char name[MAX_LINE_LENGTH];
384 char value[MAX_LINE_LENGTH];
386 line[0] = '\0';
387 value[0] = '\0';
389 i = 0;
390 /* read a line ending with '\n' */
391 do {
392 len = read(fd, &line[i], 1);
393 if (len == 1)
394 i++;
395 else
396 quit = 1;
397 } while (!quit && i < MAX_LINE_LENGTH && line[i-1] != '\n');
400 /* remove trailing whitespace */
401 line[i] = '\0';
402 i--;
403 while (i >= 0 && g_ascii_isspace(line[i])) {
404 line[i] = '\0';
405 i--;
406 }
408 len = i + 1;
409 if (len > 0) {
410 i = 0;
411 /* skip whitespace */
412 while (i < len && g_ascii_isspace(line[i]))
413 i++;
415 /* continue if this line is not a comment */
416 if (line[i] != COMMENT_TOKEN) {
417 /* get the name part */
418 j = 0;
419 while (i < len && line[i] != '=' &&
420 !g_ascii_isspace(line[i])) {
421 name[j++] = line[i++];
422 }
424 name[j] = '\0';
426 /* skip '=' and whitespace */
427 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
428 i++;
430 /* get the value part */
431 j = 0;
432 while (i < len)
433 value[j++] = line[i++];
434 value[j] = '\0';
436 match_found = 1;
438 /* key definition */
439 if (!strcasecmp(CONF_KEY_DEFINITION, name))
440 parse_key_definition(value);
441 /* enable colors */
442 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
443 #ifdef ENABLE_COLORS
444 options->enable_colors = str2bool(value);
445 #else
446 {}
447 #endif
448 /* auto center */
449 else if (!strcasecmp(CONF_AUTO_CENTER, name))
450 options->auto_center = str2bool(value);
451 /* color assignment */
452 else if (!strcasecmp(CONF_COLOR, name))
453 #ifdef ENABLE_COLORS
454 parse_color(value);
455 #else
456 {}
457 #endif
458 /* wide cursor */
459 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
460 options->wide_cursor = str2bool(value);
461 /* welcome screen list */
462 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
463 options->welcome_screen_list = str2bool(value);
464 /* visible bitrate */
465 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
466 options->visible_bitrate = str2bool(value);
467 /* timer display type */
468 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name)) {
469 g_free(options->timedisplay_type);
470 options->timedisplay_type=g_strdup(parse_timedisplay_type(value));
471 /* color definition */
472 } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
473 #ifdef ENABLE_COLORS
474 parse_color_definition(value);
475 #else
476 {}
477 #endif
478 /* list format string */
479 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
480 g_free(options->list_format);
481 options->list_format = get_format(value);
482 /* status format string */
483 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
484 g_free(options->status_format);
485 options->status_format = get_format(value);
486 /* xterm title format string */
487 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
488 g_free(options->xterm_title_format);
489 options->xterm_title_format = get_format(value);
490 } else if (!strcasecmp(CONF_LIST_WRAP, name))
491 options->list_wrap = str2bool(value);
492 else if (!strcasecmp(CONF_FIND_WRAP, name))
493 options->find_wrap = str2bool(value);
494 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
495 options->find_show_last_pattern = str2bool(value);
496 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
497 options->audible_bell = str2bool(value);
498 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
499 options->visible_bell = str2bool(value);
500 else if (!strcasecmp(CONF_XTERM_TITLE, name))
501 options->enable_xterm_title = str2bool(value);
502 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
503 options->enable_mouse = str2bool(value);
504 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
505 options->crossfade_time = atoi(value);
506 else if (!strcasecmp(CONF_SEARCH_MODE, name))
507 options->search_mode = atoi(value);
508 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
509 options->hide_cursor = atoi(value);
510 else if (!strcasecmp(CONF_SEEK_TIME, name))
511 options->seek_time = atoi(value);
512 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
513 g_strfreev(options->screen_list);
514 options->screen_list = check_screen_list(value);
515 } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
516 /* the splash screen was removed */
517 } else if (!strcasecmp(CONF_HOST, name))
518 options->host = get_format(value);
519 else if (!strcasecmp(CONF_PORT, name))
520 options->port = atoi(get_format(value));
521 else if (!strcasecmp(CONF_PASSWORD, name))
522 options->password = get_format(value);
523 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
524 options->lyrics_timeout = atoi(get_format(value));
525 else if (!strcasecmp(CONF_SCROLL, name))
526 options->scroll = str2bool(value);
527 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
528 g_free(options->scroll_sep);
529 options->scroll_sep = get_format(value);
530 } else
531 match_found = 0;
533 if (!match_found)
534 fprintf(stderr,
535 _("Unknown configuration parameter: %s\n"),
536 name);
537 }
538 }
539 }
541 if (free_filename)
542 g_free(filename);
544 return 0;
545 }
547 int
548 check_user_conf_dir(void)
549 {
550 int retval;
551 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
553 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
554 g_free(directory);
555 return 0;
556 }
558 retval = mkdir(directory, 0755);
559 g_free(directory);
560 return retval;
561 }
563 char *
564 get_user_key_binding_filename(void)
565 {
566 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
567 }
569 int
570 read_configuration(options_t *options)
571 {
572 char *filename = NULL;
574 /* check for command line configuration file */
575 if (options->config_file)
576 filename = g_strdup(options->config_file);
578 /* check for user configuration ~/.ncmpc/config */
579 if (filename == NULL) {
580 filename = g_build_filename(g_get_home_dir(),
581 "." PACKAGE, "config", NULL);
582 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
583 g_free(filename);
584 filename = NULL;
585 }
586 }
588 /* check for global configuration SYSCONFDIR/ncmpc/config */
589 if (filename == NULL) {
590 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
591 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
592 g_free(filename);
593 filename = NULL;
594 }
595 }
597 /* load configuration */
598 if (filename) {
599 read_rc_file(filename, options);
600 g_free(filename);
601 filename = NULL;
602 }
604 /* check for command line key binding file */
605 if (options->key_file)
606 filename = g_strdup(options->key_file);
608 /* check for user key bindings ~/.ncmpc/keys */
609 if (filename == NULL) {
610 filename = get_user_key_binding_filename();
611 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
612 g_free(filename);
613 filename = NULL;
614 }
615 }
617 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
618 if (filename == NULL) {
619 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
620 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
621 g_free(filename);
622 filename = NULL;
623 }
624 }
626 /* load key bindings */
627 if (filename) {
628 read_rc_file(filename, options);
629 g_free(filename);
630 filename = NULL;
631 }
633 return 0;
634 }