5dff1ccdfd798e8555aca5abc672086ccef29307
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 static int
229 parse_color(char *str)
230 {
231 const char *name = str;
232 const char *value = NULL;
233 int len,i;
235 i=0;
236 len=strlen(str);
237 /* get the color name */
238 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
239 i++;
241 /* skip whitespace */
242 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i]))) {
243 str[i]='\0';
244 i++;
245 }
247 if (i < len)
248 value = str+i;
250 return colors_assign(name, value);
251 }
253 static int
254 parse_color_definition(char *str)
255 {
256 char buf[MAX_LINE_LENGTH];
257 char *p, *end, *name;
258 size_t len = strlen(str), i;
259 int j,value;
260 short color, rgb[3];
262 /* get the command name */
263 i=0;
264 j=0;
265 memset(buf, 0, MAX_LINE_LENGTH);
266 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
267 buf[j++] = str[i++];
268 color = colors_str2color(buf);
269 if (color < 0) {
270 fprintf(stderr,_("Error: Bad color %s [%d]\n"), buf, color);
271 return -1;
272 }
273 name = g_strdup(buf);
275 /* skip whitespace */
276 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
277 i++;
279 /* get the value part */
280 memset(buf, 0, MAX_LINE_LENGTH);
281 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
282 len = strlen(buf);
283 if (len == 0) {
284 fprintf(stderr, _("Error: Incomplete color definition - %s\n"),
285 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 &&
298 (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 }
305 if (value < 0 || i != 3) {
306 fprintf(stderr, _("Error: Bad color definition - %s\n"), str);
307 g_free(name);
308 return -1;
309 }
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 }
326 return g_strdup(str);
327 }
329 static char **
330 check_screen_list(char *value)
331 {
332 char **tmp = g_strsplit_set(value, " \t,", 100);
333 char **screen = NULL;
334 int i,j;
336 i=0;
337 j=0;
338 while( tmp && tmp[i] ) {
339 char *name = g_ascii_strdown(tmp[i], -1);
340 if (screen_lookup_name(name) == NULL) {
341 fprintf(stderr,
342 _("Error: Unsupported screen \"%s\"\n"),
343 name);
344 free(name);
345 } else {
346 screen = g_realloc(screen, (j+2)*sizeof(char *));
347 screen[j++] = name;
348 screen[j] = NULL;
349 }
350 i++;
351 }
352 g_strfreev(tmp);
353 if( screen == NULL )
354 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
356 return screen;
357 }
359 static int
360 read_rc_file(char *filename, options_t *options)
361 {
362 int fd;
363 int quit = 0;
364 int free_filename = 0;
366 if (filename == NULL)
367 return -1;
369 if ((fd = open(filename,O_RDONLY)) < 0) {
370 perror(filename);
371 if (free_filename)
372 g_free(filename);
373 return -1;
374 }
376 while (!quit) {
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 && g_ascii_isspace(line[i])) {
402 line[i] = '\0';
403 i--;
404 }
406 len = i + 1;
407 if (len > 0) {
408 i = 0;
409 /* skip whitespace */
410 while (i < len && g_ascii_isspace(line[i]))
411 i++;
413 /* continue if this line is not a comment */
414 if (line[i] != COMMENT_TOKEN) {
415 /* get the name part */
416 j = 0;
417 while (i < len && line[i] != '=' &&
418 !g_ascii_isspace(line[i])) {
419 name[j++] = line[i++];
420 }
422 name[j] = '\0';
424 /* skip '=' and whitespace */
425 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
426 i++;
428 /* get the value part */
429 j = 0;
430 while (i < len)
431 value[j++] = line[i++];
432 value[j] = '\0';
434 match_found = 1;
436 /* key definition */
437 if (!strcasecmp(CONF_KEY_DEFINITION, name))
438 parse_key_definition(value);
439 /* enable colors */
440 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
441 options->enable_colors = str2bool(value);
442 /* auto center */
443 else if (!strcasecmp(CONF_AUTO_CENTER, name))
444 options->auto_center = str2bool(value);
445 /* color assignment */
446 else if (!strcasecmp(CONF_COLOR, name))
447 parse_color(value);
448 /* wide cursor */
449 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
450 options->wide_cursor = str2bool(value);
451 /* welcome screen list */
452 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
453 options->welcome_screen_list = str2bool(value);
454 /* visible bitrate */
455 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
456 options->visible_bitrate = str2bool(value);
457 /* timer display type */
458 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name)) {
459 g_free(options->timedisplay_type);
460 options->timedisplay_type=g_strdup(parse_timedisplay_type(value));
461 /* color definition */
462 } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
463 parse_color_definition(value);
464 /* list format string */
465 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
466 g_free(options->list_format);
467 options->list_format = get_format(value);
468 /* status format string */
469 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
470 g_free(options->status_format);
471 options->status_format = get_format(value);
472 /* xterm title format string */
473 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
474 g_free(options->xterm_title_format);
475 options->xterm_title_format = get_format(value);
476 } else if (!strcasecmp(CONF_LIST_WRAP, name))
477 options->list_wrap = str2bool(value);
478 else if (!strcasecmp(CONF_FIND_WRAP, name))
479 options->find_wrap = str2bool(value);
480 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
481 options->find_show_last_pattern = str2bool(value);
482 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
483 options->audible_bell = str2bool(value);
484 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
485 options->visible_bell = str2bool(value);
486 else if (!strcasecmp(CONF_XTERM_TITLE, name))
487 options->enable_xterm_title = str2bool(value);
488 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
489 options->enable_mouse = str2bool(value);
490 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
491 options->crossfade_time = atoi(value);
492 else if (!strcasecmp(CONF_SEARCH_MODE, name))
493 options->search_mode = atoi(value);
494 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
495 options->hide_cursor = atoi(value);
496 else if (!strcasecmp(CONF_SEEK_TIME, name))
497 options->seek_time = atoi(value);
498 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
499 g_strfreev(options->screen_list);
500 options->screen_list = check_screen_list(value);
501 } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
502 /* the splash screen was removed */
503 } else if (!strcasecmp(CONF_HOST, name))
504 options->host = get_format(value);
505 else if (!strcasecmp(CONF_PORT, name))
506 options->port = atoi(get_format(value));
507 else if (!strcasecmp(CONF_PASSWORD, name))
508 options->password = get_format(value);
509 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
510 options->lyrics_timeout = atoi(get_format(value));
511 else if (!strcasecmp(CONF_SCROLL, name))
512 options->scroll = str2bool(value);
513 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
514 g_free(options->scroll_sep);
515 options->scroll_sep = get_format(value);
516 } else
517 match_found = 0;
519 if (!match_found)
520 fprintf(stderr,
521 _("Unknown configuration parameter: %s\n"),
522 name);
523 }
524 }
525 }
527 if (free_filename)
528 g_free(filename);
530 return 0;
531 }
533 int
534 check_user_conf_dir(void)
535 {
536 int retval;
537 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
539 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
540 g_free(directory);
541 return 0;
542 }
544 retval = mkdir(directory, 0755);
545 g_free(directory);
546 return retval;
547 }
549 char *
550 get_user_key_binding_filename(void)
551 {
552 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
553 }
555 int
556 read_configuration(options_t *options)
557 {
558 char *filename = NULL;
560 /* check for command line configuration file */
561 if (options->config_file)
562 filename = g_strdup(options->config_file);
564 /* check for user configuration ~/.ncmpc/config */
565 if (filename == NULL) {
566 filename = g_build_filename(g_get_home_dir(),
567 "." PACKAGE, "config", NULL);
568 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
569 g_free(filename);
570 filename = NULL;
571 }
572 }
574 /* check for global configuration SYSCONFDIR/ncmpc/config */
575 if (filename == NULL) {
576 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
577 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
578 g_free(filename);
579 filename = NULL;
580 }
581 }
583 /* load configuration */
584 if (filename) {
585 read_rc_file(filename, options);
586 g_free(filename);
587 filename = NULL;
588 }
590 /* check for command line key binding file */
591 if (options->key_file)
592 filename = g_strdup(options->key_file);
594 /* check for user key bindings ~/.ncmpc/keys */
595 if (filename == NULL) {
596 filename = get_user_key_binding_filename();
597 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
598 g_free(filename);
599 filename = NULL;
600 }
601 }
603 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
604 if (filename == NULL) {
605 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
606 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
607 g_free(filename);
608 filename = NULL;
609 }
610 }
612 /* load key bindings */
613 if (filename) {
614 read_rc_file(filename, options);
615 g_free(filename);
616 filename = NULL;
617 }
619 return 0;
620 }