Code

moved i18n macros to i18n.h
[ncmpc.git] / src / conf.c
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 "defaults.h"
25 #include "i18n.h"
26 #include "support.h"
27 #include "command.h"
28 #include "colors.h"
29 #include "screen_list.h"
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
41 #define MAX_LINE_LENGTH 1024
42 #define COMMENT_TOKEN   '#'
44 /* configuration field names */
45 #define CONF_ENABLE_COLORS           "enable-colors"
46 #define CONF_AUTO_CENTER             "auto-center"
47 #define CONF_WIDE_CURSOR             "wide-cursor"
48 #define CONF_ENABLE_BELL             "enable-bell"
49 #define CONF_KEY_DEFINITION          "key"
50 #define CONF_COLOR                   "color"
51 #define CONF_COLOR_DEFINITION        "colordef"
52 #define CONF_LIST_FORMAT             "list-format"
53 #define CONF_STATUS_FORMAT           "status-format"
54 #define CONF_XTERM_TITLE_FORMAT      "xterm-title-format"
55 #define CONF_LIST_WRAP               "wrap-around"
56 #define CONF_FIND_WRAP               "find-wrap"
57 #define CONF_FIND_SHOW_LAST          "find-show-last"
58 #define CONF_AUDIBLE_BELL            "audible-bell"
59 #define CONF_VISIBLE_BELL            "visible-bell"
60 #define CONF_XTERM_TITLE             "set-xterm-title"
61 #define CONF_ENABLE_MOUSE            "enable-mouse"
62 #define CONF_CROSSFADE_TIME          "crossfade-time"
63 #define CONF_SEARCH_MODE             "search-mode"
64 #define CONF_HIDE_CURSOR             "hide-cursor"
65 #define CONF_SEEK_TIME               "seek-time"
66 #define CONF_SCREEN_LIST             "screen-list"
67 #define CONF_TIMEDISPLAY_TYPE        "timedisplay-type"
68 #define CONF_HOST                    "host"
69 #define CONF_PORT                    "port"
70 #define CONF_PASSWORD                "password"
71 #define CONF_LYRICS_TIMEOUT          "lyrics-timeout"
72 #define CONF_SHOW_SPLASH             "show-splash"
73 #define CONF_SCROLL                  "scroll"
74 #define CONF_SCROLL_SEP              "scroll-sep"
75 #define CONF_VISIBLE_BITRATE         "visible-bitrate"
76 #define CONF_WELCOME_SCREEN_LIST     "welcome-screen-list"
78 typedef enum {
79   KEY_PARSER_UNKNOWN,
80   KEY_PARSER_CHAR,
81   KEY_PARSER_DEC,
82   KEY_PARSER_HEX,
83   KEY_PARSER_DONE
84 } key_parser_state_t;
86 static bool
87 str2bool(char *str)
88 {
89         return strcasecmp(str, "yes") == 0 || strcasecmp(str, "true") == 0 ||
90                 strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0;
91 }
93 static int
94 parse_key_value(char *str, size_t len, char **end)
95 {
96         size_t i;
97         int value;
98         key_parser_state_t state;
100         i=0;
101         value=0;
102         state=KEY_PARSER_UNKNOWN;
103         *end = str;
105         while (i < len && state != KEY_PARSER_DONE) {
106                 int next = 0;
107                 int c = str[i];
109                 if( i+1<len )
110                         next = str[i+1];
112                 switch(state) {
113                 case KEY_PARSER_UNKNOWN:
114                         if( c=='\'' )
115                                 state = KEY_PARSER_CHAR;
116                         else if( c=='0' && next=='x' )
117                                 state = KEY_PARSER_HEX;
118                         else if( isdigit(c) )
119                                 state = KEY_PARSER_DEC;
120                         else {
121                                 fprintf(stderr,
122                                         _("Error: Unsupported key definition - %s\n"),
123                                         str);
124                                 return -1;
125                         }
126                         break;
127                 case KEY_PARSER_CHAR:
128                         if( next!='\'' ) {
129                                 fprintf(stderr,
130                                         _("Error: Unsupported key definition - %s\n"),
131                                         str);
132                                 return -1;
133                         }
134                         value = c;
135                         *end = str+i+2;
136                         state = KEY_PARSER_DONE;
137                         break;
138                 case KEY_PARSER_DEC:
139                         value = (int) strtol(str+(i-1), end, 10);
140                         state = KEY_PARSER_DONE;
141                         break;
142                 case KEY_PARSER_HEX:
143                         if( !isdigit(next) ) {
144                                 fprintf(stderr,_("Error: Digit expected after 0x - %s\n"), str);
145                                 return -1;
146                         }
147                         value = (int) strtol(str+(i+1), end, 16);
148                         state = KEY_PARSER_DONE;
149                         break;
150                 case KEY_PARSER_DONE:
151                         break;
152                 }
153                 i++;
154         }
156         if( *end> str+len )
157                 *end = str+len;
159         return value;
162 static int
163 parse_key_definition(char *str)
165         char buf[MAX_LINE_LENGTH];
166         char *p, *end;
167         size_t len = strlen(str), i;
168         int j,key;
169         int keys[MAX_COMMAND_KEYS];
170         command_t cmd;
172         /* get the command name */
173         i=0;
174         j=0;
175         memset(buf, 0, MAX_LINE_LENGTH);
176         while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
177                 buf[j++] = str[i++];
178         if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
179                 fprintf(stderr, _("Error: Unknown key command %s\n"), buf);
180                 return -1;
181         }
183         /* skip whitespace */
184         while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) )
185                 i++;
187         /* get the value part */
188         memset(buf, 0, MAX_LINE_LENGTH);
189         g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
190         len = strlen(buf);
191         if( len==0 ) {
192                 fprintf(stderr,_("Error: Incomplete key definition - %s\n"), str);
193                 return -1;
194         }
196         /* parse key values */
197         i = 0;
198         key = 0;
199         len = strlen(buf);
200         p = buf;
201         end = buf+len;
202         memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
203         while( i<MAX_COMMAND_KEYS && p<end &&
204                (key=parse_key_value(p,len+1,&p))>=0 ) {
205                 keys[i++] = key;
206                 while( p<end && (*p==',' || *p==' ' || *p=='\t') )
207                         p++;
208                 len = strlen(p);
209         }
210         if( key<0 ) {
211                 fprintf(stderr,_("Error: Bad key definition - %s\n"), str);
212                 return -1;
213         }
215         return assign_keys(cmd, keys);
218 static const char *
219 parse_timedisplay_type(const char *str)
221         if((!strcmp(str,"elapsed")) || (!strcmp(str,"remaining"))){
222                 return str;
223         } else {
224                 fprintf(stderr,_("Error: Bad time display type - %s\n"), str);
225                 return DEFAULT_TIMEDISPLAY_TYPE;
226         }
229 static int
230 parse_color(char *str)
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]!='=' && !IS_WHITESPACE(str[i]) )
240                 i++;
242         /* skip whitespace */
243         while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) ) {
244                 str[i]='\0';
245                 i++;
246         }
248         if( i<len )
249                 value = str+i;
251         return colors_assign(name, value);
254 static int
255 parse_color_definition(char *str)
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]!='=' && !IS_WHITESPACE(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]=='=' || IS_WHITESPACE(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"), 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 && (value=parse_key_value(p,len+1,&p))>=0 ) {
298                 rgb[i++] = value;
299                 while( p<end && (*p==',' || *p==' ' || *p=='\t') )
300                         p++;
301                 len = strlen(p);
302         }
303         if( value<0 || i!=3) {
304                 fprintf(stderr,_("Error: Bad color definition - %s\n"), str);
305                 g_free(name);
306                 return -1;
307         }
308         value = colors_define(name, rgb[0], rgb[1], rgb[2]);
309         g_free(name);
310         return value;
313 static char *
314 get_format(char *str)
316         gsize len = strlen(str);
318         if( str && str[0]=='\"' && str[len-1] == '\"' ) {
319                 str[len-1] = '\0';
320                 str++;
321         }
322         return g_strdup(str);
325 static char **
326 check_screen_list(char *value)
328         char **tmp = g_strsplit_set(value, " \t,", 100);
329         char **screen = NULL;
330         int i,j;
332         i=0;
333         j=0;
334         while( tmp && tmp[i] ) {
335                 tmp[i] = lowerstr(tmp[i]);
336                 if (screen_lookup_name(tmp[i]) == NULL)
337                         fprintf(stderr,
338                                 _("Error: Unsupported screen \"%s\"\n"),
339                                 tmp[i]);
340                 else {
341                         screen = g_realloc(screen, (j+2)*sizeof(char *));
342                         screen[j++] = g_strdup(tmp[i]);
343                         screen[j] = NULL;
344                 }
345                 i++;
346         }
347         g_strfreev(tmp);
348         if( screen == NULL )
349                 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
351         return screen;
354 static int
355 read_rc_file(char *filename, options_t *options)
357   int fd;
358   int quit  = 0;
359   int free_filename = 0;
361   if( filename==NULL )
362     return -1;
364   if( (fd=open(filename,O_RDONLY)) <0 )
365     {
366       perror(filename);
367       if( free_filename )
368         g_free(filename);
369       return -1;
370     }
372   while( !quit )
373     {
374       int i,j;
375       int len;
376       int match_found;
377       char line[MAX_LINE_LENGTH];
378       char name[MAX_LINE_LENGTH];
379       char value[MAX_LINE_LENGTH];
381       line[0]  = '\0';
382       value[0] = '\0';
384       i = 0;
385       /* read a line ending with '\n' */
386       do {
387         len = read( fd, &line[i], 1 );
388         if( len == 1 )
389           i++;
390         else
391           quit = 1;
392       } while( !quit && i<MAX_LINE_LENGTH && line[i-1]!='\n' );
393       
394      
395       /* remove trailing whitespace */
396       line[i] = '\0';
397       i--;
398       while( i>=0 && IS_WHITESPACE(line[i]) )
399         {
400           line[i] = '\0';
401           i--;
402         }     
403       len = i+1;
405       if( len>0 )
406         {
407           i = 0;
408           /* skip whitespace */
409           while( i<len && IS_WHITESPACE(line[i]) )
410             i++;
411           
412           /* continue if this line is not a comment */
413           if( line[i] != COMMENT_TOKEN )
414             {
415               /* get the name part */
416               j=0;
417               while( i<len && line[i]!='=' && !IS_WHITESPACE(line[i]) )
418                 {
419                   name[j++] = line[i++];
420                 }
421               name[j] = '\0';
422               
423               /* skip '=' and whitespace */
424               while( i<len && (line[i]=='=' || IS_WHITESPACE(line[i])) )
425                 i++;
426               
427               /* get the value part */
428               j=0;
429               while( i<len )
430                 {
431                   value[j++] = line[i++];
432                 }
433               value[j] = '\0';
434               
435               match_found = 1;
437               /* key definition */
438               if( !strcasecmp(CONF_KEY_DEFINITION, name) )
439                 {
440                   parse_key_definition(value);
441                 }
442               /* enable colors */
443               else if( !strcasecmp(CONF_ENABLE_COLORS, name) )
444                 {
445                   options->enable_colors = str2bool(value);
446                 }
447               /* auto center */
448               else if( !strcasecmp(CONF_AUTO_CENTER, name) )
449                 {
450                   options->auto_center = str2bool(value);
451                 }
452               /* color assignment */
453               else if( !strcasecmp(CONF_COLOR, name) )
454                 {
455                   parse_color(value);
456                 }
457               /* wide cursor */
458               else if( !strcasecmp(CONF_WIDE_CURSOR, name) )
459                 {
460                   options->wide_cursor = str2bool(value);
461                 }
462               /* welcome screen list */
463               else if (!strcasecmp(CONF_WELCOME_SCREEN_LIST, name)) {
464                   options->welcome_screen_list = str2bool(value);
465               }
466               /* visible bitrate */
467               else if (!strcasecmp(CONF_VISIBLE_BITRATE, name)) {
468                       options->visible_bitrate = str2bool(value);
469               }
470               /* timer display type */
471               else if( !strcasecmp(CONF_TIMEDISPLAY_TYPE, name) )
472                 {
473                     g_free(options->timedisplay_type);
474                     options->timedisplay_type=g_strdup(parse_timedisplay_type(value));
475                 }
476               /* color definition */
477               else if( !strcasecmp(CONF_COLOR_DEFINITION, name) )
478                 {
479                   parse_color_definition(value);
480                 }
481               /* list format string */
482               else if( !strcasecmp(CONF_LIST_FORMAT, name) )
483                 {
484                   g_free(options->list_format);
485                   options->list_format = get_format(value);
486                 }
487               /* status format string */
488               else if( !strcasecmp(CONF_STATUS_FORMAT, name) )
489                 {
490                   g_free(options->status_format);
491                   options->status_format = get_format(value);
492                 }
493               /* xterm title format string */
494               else if( !strcasecmp(CONF_XTERM_TITLE_FORMAT, name) )
495                 {
496                   g_free(options->xterm_title_format);
497                   options->xterm_title_format = get_format(value);
498                 }
499               else if( !strcasecmp(CONF_LIST_WRAP, name) )
500                 {
501                   options->list_wrap = str2bool(value);
502                 }
503               else if( !strcasecmp(CONF_FIND_WRAP, name) )
504                 {
505                   options->find_wrap = str2bool(value);
506                 }
507               else if( !strcasecmp(CONF_FIND_SHOW_LAST,name) )
508                 {
509                   options->find_show_last_pattern = str2bool(value);
510                 }
511               else if( !strcasecmp(CONF_AUDIBLE_BELL, name) )
512                 {
513                   options->audible_bell = str2bool(value);
514                 }
515               else if( !strcasecmp(CONF_VISIBLE_BELL, name) )
516                 {
517                   options->visible_bell = str2bool(value);
518                 }
519               else if( !strcasecmp(CONF_XTERM_TITLE, name) )
520                 {
521                   options->enable_xterm_title = str2bool(value);
522                 }
523               else if( !strcasecmp(CONF_ENABLE_MOUSE, name) )
524                 {
525                   options->enable_mouse = str2bool(value);
526                 }
527               else if( !strcasecmp(CONF_CROSSFADE_TIME, name) )
528                 {
529                   options->crossfade_time = atoi(value);
530                 }
531               else if( !strcasecmp(CONF_SEARCH_MODE, name) )
532                 {
533                   options->search_mode = atoi(value);
534                 }
535               else if( !strcasecmp(CONF_HIDE_CURSOR, name) )
536                 {
537                   options->hide_cursor = atoi(value);
538                 }
539               else if( !strcasecmp(CONF_SEEK_TIME, name) )
540                 {
541                   options->seek_time = atoi(value);
542                 }
543               else if( !strcasecmp(CONF_SCREEN_LIST, name) )
544                 {
545                   g_strfreev(options->screen_list);
546                   options->screen_list = check_screen_list(value);
547                 }
548               else if( !strcasecmp(CONF_SHOW_SPLASH, name) )
549                 {
550                   /* the splash screen was removed */
551                 }
552             else if( !strcasecmp(CONF_HOST, name))
553             {
554             options->host = get_format(value);
555             }
556             else if( !strcasecmp(CONF_PORT, name))
557             {
558             options->port = atoi(get_format(value));
559             }
560             else if( !strcasecmp(CONF_PASSWORD, name))
561               {
562                 options->password = get_format(value);
563               }
564             else if( !strcasecmp(CONF_LYRICS_TIMEOUT, name))
565             {
566             options->lyrics_timeout = atoi(get_format(value));
567             }           
568             else if( !strcasecmp(CONF_SCROLL, name))
569             {
570             options->scroll = str2bool(value);
571             }
572             else if( !strcasecmp(CONF_SCROLL_SEP, name))
573             {
574             g_free(options->scroll_sep);
575             options->scroll_sep = get_format(value);
576             }
577               else
578                 {
579                   match_found = 0;
580                 }
582               if( !match_found )
583                 fprintf(stderr, 
584                         _("Unknown configuration parameter: %s\n"), 
585                         name);
586             }
587         }         
588     }
590   if( free_filename )
591     g_free(filename);
592  
593   return 0;
596 int
597 check_user_conf_dir(void)
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;
612 char *
613 get_user_key_binding_filename(void)
615         return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
619 int
620 read_configuration(options_t *options)
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;