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 #include "conf.h"
20 #include "config.h"
21 #include "defaults.h"
22 #include "i18n.h"
23 #include "command.h"
24 #include "colors.h"
25 #include "screen_list.h"
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <glib.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"
72 #define CONF_VISIBLE_BITRATE "visible-bitrate"
73 #define CONF_WELCOME_SCREEN_LIST "welcome-screen-list"
75 static bool
76 str2bool(char *str)
77 {
78 return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
79 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
80 }
82 static void
83 print_error(const char *msg, const char *input)
84 {
85 fprintf(stderr, "%s: %s ('%s')\n",
86 /* To translators: prefix for error messages */
87 _("Error"), msg, input);
88 }
90 static int
91 parse_key_value(char *str, char **end)
92 {
93 if (*str == '\'') {
94 if (str[1] == '\'' || str[2] != '\'') {
95 print_error(_("Malformed hotkey definition"), str);
96 return -1;
97 }
99 *end = str + 3;
100 return str[1];
101 } else {
102 long value = strtol(str, end, 0);
103 if (*end == str) {
104 print_error(_("Malformed hotkey definition"), str);
105 return -1;
106 }
108 return (int)value;
109 }
110 }
112 static int
113 parse_key_definition(char *str)
114 {
115 char buf[MAX_LINE_LENGTH];
116 char *p;
117 size_t len = strlen(str), i;
118 int j,key;
119 int keys[MAX_COMMAND_KEYS];
120 command_t cmd;
122 /* get the command name */
123 i=0;
124 j=0;
125 memset(buf, 0, MAX_LINE_LENGTH);
126 while (i < len && str[i] != '=' && !g_ascii_isspace(str[i]))
127 buf[j++] = str[i++];
128 if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
129 print_error(_("Unknown command"), buf);
130 return -1;
131 }
133 /* skip whitespace */
134 while (i < len && (str[i] == '=' || g_ascii_isspace(str[i])))
135 i++;
137 /* get the value part */
138 memset(buf, 0, MAX_LINE_LENGTH);
139 g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
140 if (*buf == 0) {
141 print_error(_("Incomplete hotkey configuration"), str);
142 return -1;
143 }
145 /* parse key values */
146 i = 0;
147 key = 0;
148 p = buf;
149 memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
150 while (i < MAX_COMMAND_KEYS && *p != 0 &&
151 (key = parse_key_value(p, &p)) >= 0) {
152 keys[i++] = key;
153 while (*p==',' || *p==' ' || *p=='\t')
154 p++;
155 }
157 if (key < 0)
158 return -1;
160 return assign_keys(cmd, keys);
161 }
163 static const char *
164 parse_timedisplay_type(const char *str)
165 {
166 if (!strcmp(str,"elapsed") || !strcmp(str,"remaining"))
167 return str;
168 else {
169 print_error(_("Bad time display type"), str);
170 return DEFAULT_TIMEDISPLAY_TYPE;
171 }
172 }
174 #ifdef ENABLE_COLORS
175 static char *
176 separate_value(char *p)
177 {
178 char *value;
180 value = strchr(p, '=');
181 if (value == NULL) {
182 fprintf(stderr, "%s\n", _("Missing '='"));
183 return NULL;
184 }
186 *value++ = 0;
188 g_strchomp(p);
189 return g_strchug(value);
190 }
192 static int
193 parse_color(char *str)
194 {
195 char *value;
197 value = separate_value(str);
198 if (value == NULL)
199 return -1;
201 return colors_assign(str, value);
202 }
204 /**
205 * Returns the first non-whitespace character after the next comma
206 * character, or the end of the string. This is used to parse comma
207 * separated values.
208 */
209 static char *
210 after_comma(char *p)
211 {
212 char *comma = strchr(p, ',');
214 if (comma != NULL) {
215 *comma++ = 0;
216 comma = g_strchug(comma);
217 } else
218 comma = p + strlen(p);
220 g_strchomp(p);
221 return comma;
222 }
224 static int
225 parse_color_definition(char *str)
226 {
227 char buf[MAX_LINE_LENGTH];
228 char *value;
229 short color, rgb[3];
231 value = separate_value(str);
232 if (value == NULL)
233 return -1;
235 /* get the command name */
236 color = colors_str2color(str);
237 if (color < 0) {
238 print_error(_("Bad color name"), buf);
239 return -1;
240 }
242 /* parse r,g,b values */
244 for (unsigned i = 0; i < 3; ++i) {
245 char *next = after_comma(value), *endptr;
246 if (*value == 0) {
247 print_error(_("Incomplete color definition"), str);
248 return -1;
249 }
251 rgb[i] = strtol(value, &endptr, 0);
252 if (endptr == value || *endptr != 0) {
253 print_error(_("Invalid number"), value);
254 return -1;
255 }
257 value = next;
258 }
260 if (*value != 0) {
261 print_error(_("Malformed color definition"), str);
262 return -1;
263 }
265 return colors_define(str, rgb[0], rgb[1], rgb[2]);
266 }
267 #endif
269 static char *
270 get_format(char *str)
271 {
272 gsize len = strlen(str);
274 if (str && str[0]=='\"' && str[len-1] == '\"') {
275 str[len - 1] = '\0';
276 str++;
277 }
279 return g_strdup(str);
280 }
282 static char **
283 check_screen_list(char *value)
284 {
285 char **tmp = g_strsplit_set(value, " \t,", 100);
286 char **screen = NULL;
287 int i,j;
289 i=0;
290 j=0;
291 while( tmp && tmp[i] ) {
292 char *name = g_ascii_strdown(tmp[i], -1);
293 if (screen_lookup_name(name) == NULL) {
294 print_error(_("Unknown screen name"), name);
295 free(name);
296 } else {
297 screen = g_realloc(screen, (j+2)*sizeof(char *));
298 screen[j++] = name;
299 screen[j] = NULL;
300 }
301 i++;
302 }
303 g_strfreev(tmp);
304 if( screen == NULL )
305 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
307 return screen;
308 }
310 static bool
311 parse_line(char *line)
312 {
313 size_t len = strlen(line), i = 0, j;
314 char name[MAX_LINE_LENGTH];
315 char value[MAX_LINE_LENGTH];
316 bool match_found;
318 /* get the name part */
319 j = 0;
320 while (i < len && line[i] != '=' &&
321 !g_ascii_isspace(line[i])) {
322 name[j++] = line[i++];
323 }
325 name[j] = '\0';
327 /* skip '=' and whitespace */
328 while (i < len && (line[i] == '=' || g_ascii_isspace(line[i])))
329 i++;
331 /* get the value part */
332 j = 0;
333 while (i < len)
334 value[j++] = line[i++];
335 value[j] = '\0';
337 match_found = true;
339 /* key definition */
340 if (!strcasecmp(CONF_KEY_DEFINITION, name))
341 parse_key_definition(value);
342 /* enable colors */
343 else if(!strcasecmp(CONF_ENABLE_COLORS, name))
344 #ifdef ENABLE_COLORS
345 options.enable_colors = str2bool(value);
346 #else
347 {}
348 #endif
349 /* auto center */
350 else if (!strcasecmp(CONF_AUTO_CENTER, name))
351 options.auto_center = str2bool(value);
352 /* color assignment */
353 else if (!strcasecmp(CONF_COLOR, name))
354 #ifdef ENABLE_COLORS
355 parse_color(value);
356 #else
357 {}
358 #endif
359 /* wide cursor */
360 else if (!strcasecmp(CONF_WIDE_CURSOR, name))
361 options.wide_cursor = str2bool(value);
362 /* welcome screen list */
363 else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name))
364 options.welcome_screen_list = str2bool(value);
365 /* visible bitrate */
366 else if (!strcasecmp(CONF_VISIBLE_BITRATE, name))
367 options.visible_bitrate = str2bool(value);
368 /* timer display type */
369 else if (!strcasecmp(CONF_TIMEDISPLAY_TYPE, name)) {
370 g_free(options.timedisplay_type);
371 options.timedisplay_type=g_strdup(parse_timedisplay_type(value));
372 /* color definition */
373 } else if (!strcasecmp(CONF_COLOR_DEFINITION, name))
374 #ifdef ENABLE_COLORS
375 parse_color_definition(value);
376 #else
377 {}
378 #endif
379 /* list format string */
380 else if (!strcasecmp(CONF_LIST_FORMAT, name)) {
381 g_free(options.list_format);
382 options.list_format = get_format(value);
383 /* status format string */
384 } else if (!strcasecmp(CONF_STATUS_FORMAT, name)) {
385 g_free(options.status_format);
386 options.status_format = get_format(value);
387 /* xterm title format string */
388 } else if (!strcasecmp(CONF_XTERM_TITLE_FORMAT, name)) {
389 g_free(options.xterm_title_format);
390 options.xterm_title_format = get_format(value);
391 } else if (!strcasecmp(CONF_LIST_WRAP, name))
392 options.list_wrap = str2bool(value);
393 else if (!strcasecmp(CONF_FIND_WRAP, name))
394 options.find_wrap = str2bool(value);
395 else if (!strcasecmp(CONF_FIND_SHOW_LAST,name))
396 options.find_show_last_pattern = str2bool(value);
397 else if (!strcasecmp(CONF_AUDIBLE_BELL, name))
398 options.audible_bell = str2bool(value);
399 else if (!strcasecmp(CONF_VISIBLE_BELL, name))
400 options.visible_bell = str2bool(value);
401 else if (!strcasecmp(CONF_XTERM_TITLE, name))
402 options.enable_xterm_title = str2bool(value);
403 else if (!strcasecmp(CONF_ENABLE_MOUSE, name))
404 #ifdef HAVE_GETMOUSE
405 options.enable_mouse = str2bool(value);
406 #else
407 {}
408 #endif
409 else if (!strcasecmp(CONF_CROSSFADE_TIME, name))
410 options.crossfade_time = atoi(value);
411 else if (!strcasecmp(CONF_SEARCH_MODE, name))
412 options.search_mode = atoi(value);
413 else if (!strcasecmp(CONF_HIDE_CURSOR, name))
414 options.hide_cursor = atoi(value);
415 else if (!strcasecmp(CONF_SEEK_TIME, name))
416 options.seek_time = atoi(value);
417 else if (!strcasecmp(CONF_SCREEN_LIST, name)) {
418 g_strfreev(options.screen_list);
419 options.screen_list = check_screen_list(value);
420 } else if (!strcasecmp(CONF_SHOW_SPLASH, name)) {
421 /* the splash screen was removed */
422 } else if (!strcasecmp(CONF_HOST, name))
423 options.host = get_format(value);
424 else if (!strcasecmp(CONF_PORT, name))
425 options.port = atoi(get_format(value));
426 else if (!strcasecmp(CONF_PASSWORD, name))
427 options.password = get_format(value);
428 else if (!strcasecmp(CONF_LYRICS_TIMEOUT, name))
429 #ifdef ENABLE_LYRICS_SCREEN
430 options.lyrics_timeout = atoi(get_format(value));
431 #else
432 {}
433 #endif
434 else if (!strcasecmp(CONF_SCROLL, name))
435 options.scroll = str2bool(value);
436 else if (!strcasecmp(CONF_SCROLL_SEP, name)) {
437 g_free(options.scroll_sep);
438 options.scroll_sep = get_format(value);
439 } else
440 match_found = false;
442 if (!match_found)
443 print_error(_("Unknown configuration parameter"),
444 name);
446 return match_found;
447 }
449 static int
450 read_rc_file(char *filename)
451 {
452 FILE *file;
453 char line[MAX_LINE_LENGTH];
455 if (filename == NULL)
456 return -1;
458 file = fopen(filename, "r");
459 if (file == NULL) {
460 perror(filename);
461 return -1;
462 }
464 while (fgets(line, sizeof(line), file) != NULL) {
465 char *p = g_strchug(line);
467 if (*p != 0 && *p != COMMENT_TOKEN)
468 parse_line(g_strchomp(p));
469 }
471 fclose(file);
472 return 0;
473 }
475 int
476 check_user_conf_dir(void)
477 {
478 int retval;
479 char *directory = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
481 if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
482 g_free(directory);
483 return 0;
484 }
486 retval = mkdir(directory, 0755);
487 g_free(directory);
488 return retval;
489 }
491 char *
492 get_user_key_binding_filename(void)
493 {
494 return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
495 }
497 int
498 read_configuration(void)
499 {
500 char *filename = NULL;
502 /* check for command line configuration file */
503 if (options.config_file)
504 filename = g_strdup(options.config_file);
506 /* check for user configuration ~/.ncmpc/config */
507 if (filename == NULL) {
508 filename = g_build_filename(g_get_home_dir(),
509 "." PACKAGE, "config", NULL);
510 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
511 g_free(filename);
512 filename = NULL;
513 }
514 }
516 /* check for global configuration SYSCONFDIR/ncmpc/config */
517 if (filename == NULL) {
518 filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
519 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
520 g_free(filename);
521 filename = NULL;
522 }
523 }
525 /* load configuration */
526 if (filename) {
527 read_rc_file(filename);
528 g_free(filename);
529 filename = NULL;
530 }
532 /* check for command line key binding file */
533 if (options.key_file)
534 filename = g_strdup(options.key_file);
536 /* check for user key bindings ~/.ncmpc/keys */
537 if (filename == NULL) {
538 filename = get_user_key_binding_filename();
539 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
540 g_free(filename);
541 filename = NULL;
542 }
543 }
545 /* check for global key bindings SYSCONFDIR/ncmpc/keys */
546 if (filename == NULL) {
547 filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
548 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
549 g_free(filename);
550 filename = NULL;
551 }
552 }
554 /* load key bindings */
555 if (filename) {
556 read_rc_file(filename);
557 g_free(filename);
558 filename = NULL;
559 }
561 return 0;
562 }