Code

code style, indent with tabs
[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 #include <ctype.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
31 #include <glib.h>
32 #include <ncurses.h>
33 #include "config.h"
34 #include "ncmpc.h"
35 #include "options.h"
36 #include "support.h"
37 #include "command.h"
38 #include "colors.h"
39 #include "conf.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"
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;
85 extern gint screen_get_id(char *name);
88 static gboolean
89 str2bool(char *str)
90 {
91         if (!strcasecmp(str, "yes") || !strcasecmp(str, "true") ||
92             !strcasecmp(str, "on") || !strcasecmp(str, "1"))
93                 return TRUE;
94         return FALSE;
95 }
97 static int
98 parse_key_value(char *str, size_t len, char **end)
99 {
100         int i, value;
101         key_parser_state_t state;
103         i=0;
104         value=0;
105         state=KEY_PARSER_UNKNOWN;
106         *end = str;
108         while (i < len && state != KEY_PARSER_DONE) {
109                 int next = 0;
110                 int c = str[i];
112                 if( i+1<len )
113                         next = str[i+1];
115                 switch(state) {
116                 case KEY_PARSER_UNKNOWN:
117                         if( c=='\'' )
118                                 state = KEY_PARSER_CHAR;
119                         else if( c=='0' && next=='x' )
120                                 state = KEY_PARSER_HEX;
121                         else if( isdigit(c) )
122                                 state = KEY_PARSER_DEC;
123                         else {
124                                 fprintf(stderr,
125                                         _("Error: Unsupported key definition - %s\n"),
126                                         str);
127                                 return -1;
128                         }
129                         break;
130                 case KEY_PARSER_CHAR:
131                         if( next!='\'' ) {
132                                 fprintf(stderr,
133                                         _("Error: Unsupported key definition - %s\n"),
134                                         str);
135                                 return -1;
136                         }
137                         value = c;
138                         *end = str+i+2;
139                         state = KEY_PARSER_DONE;
140                         break;
141                 case KEY_PARSER_DEC:
142                         value = (int) strtol(str+(i-1), end, 10);
143                         state = KEY_PARSER_DONE;
144                         break;
145                 case KEY_PARSER_HEX:
146                         if( !isdigit(next) ) {
147                                 fprintf(stderr,_("Error: Digit expected after 0x - %s\n"), str);
148                                 return -1;
149                         }
150                         value = (int) strtol(str+(i+1), end, 16);
151                         state = KEY_PARSER_DONE;
152                         break;
153                 case KEY_PARSER_DONE:
154                         break;
155                 }
156                 i++;
157         }
159         if( *end> str+len )
160                 *end = str+len;
162         return value;
165 static int
166 parse_key_definition(char *str)
168         char buf[MAX_LINE_LENGTH];
169         char *p, *end;
170         size_t len = strlen(str);
171         int i,j,key;
172         int keys[MAX_COMMAND_KEYS];
173         command_t cmd;
175         /* get the command name */
176         i=0;
177         j=0;
178         memset(buf, 0, MAX_LINE_LENGTH);
179         while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
180                 buf[j++] = str[i++];
181         if( (cmd=get_key_command_from_name(buf)) == CMD_NONE ) {
182                 fprintf(stderr, _("Error: Unknown key command %s\n"), buf);
183                 return -1;
184         }
186         /* skip whitespace */
187         while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) )
188                 i++;
190         /* get the value part */
191         memset(buf, 0, MAX_LINE_LENGTH);
192         g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
193         len = strlen(buf);
194         if( len==0 ) {
195                 fprintf(stderr,_("Error: Incomplete key definition - %s\n"), str);
196                 return -1;
197         }
199         /* parse key values */
200         i = 0;
201         key = 0;
202         len = strlen(buf);
203         p = buf;
204         end = buf+len;
205         memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
206         while( i<MAX_COMMAND_KEYS && p<end &&
207                (key=parse_key_value(p,len+1,&p))>=0 ) {
208                 keys[i++] = key;
209                 while( p<end && (*p==',' || *p==' ' || *p=='\t') )
210                         p++;
211                 len = strlen(p);
212         }
213         if( key<0 ) {
214                 fprintf(stderr,_("Error: Bad key definition - %s\n"), str);
215                 return -1;
216         }
218         return assign_keys(cmd, keys);
221 static char *
222 parse_timedisplay_type(char *str)
224         if((!strcmp(str,"elapsed")) || (!strcmp(str,"remaining"))){
225                 return str;
226         } else {
227                 fprintf(stderr,_("Error: Bad time display type - %s\n"), str);
228                 return DEFAULT_TIMEDISPLAY_TYPE;
229         }
232 static int
233 parse_color(char *str)
235         char *name = str;
236         char *value = NULL;
237         int len,i;
239         i=0;
240         len=strlen(str);
241         /* get the color name */
242         while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
243                 i++;
245         /* skip whitespace */
246         while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) ) {
247                 str[i]='\0';
248                 i++;
249         }
251         if( i<len )
252                 value = str+i;
254         return colors_assign(name, value);
257 static int
258 parse_color_definition(char *str)
260         char buf[MAX_LINE_LENGTH];
261         char *p, *end, *name;
262         size_t len = strlen(str);
263         int i,j,value;
264         short color, rgb[3];
266         /* get the command name */
267         i=0;
268         j=0;
269         memset(buf, 0, MAX_LINE_LENGTH);
270         while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
271                 buf[j++] = str[i++];
272         color=colors_str2color(buf);
273         if( color<0 ) {
274                 fprintf(stderr,_("Error: Bad color %s [%d]\n"), buf, color);
275                 return -1;
276         }
277         name = g_strdup(buf);
279         /* skip whitespace */
280         while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) )
281                 i++;
283         /* get the value part */
284         memset(buf, 0, MAX_LINE_LENGTH);
285         g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
286         len = strlen(buf);
287         if( len==0 ) {
288                 fprintf(stderr,_("Error: Incomplete color definition - %s\n"), str);
289                 g_free(name);
290                 return -1;
291         }
293         /* parse r,g.b values with the key definition parser */
294         i = 0;
295         value = 0;
296         len = strlen(buf);
297         p = buf;
298         end = buf+len;
299         memset(rgb, 0, sizeof(short)*3);
300         while( i<3 && p<end && (value=parse_key_value(p,len+1,&p))>=0 ) {
301                 rgb[i++] = value;
302                 while( p<end && (*p==',' || *p==' ' || *p=='\t') )
303                         p++;
304                 len = strlen(p);
305         }
306         if( value<0 || i!=3) {
307                 fprintf(stderr,_("Error: Bad color definition - %s\n"), str);
308                 g_free(name);
309                 return -1;
310         }
311         value = colors_define(name, rgb[0], rgb[1], rgb[2]);
312         g_free(name);
313         return value;
316 static char *
317 get_format(char *str)
319         gsize len = strlen(str);
321         if( str && str[0]=='\"' && str[len-1] == '\"' ) {
322                 str[len-1] = '\0';
323                 str++;
324         }
325         return g_strdup(str);
328 static char **
329 check_screen_list(char *value)
331         char **tmp = g_strsplit_set(value, " \t,", 100);
332         char **screen = NULL;
333         int i,j;
335         i=0;
336         j=0;
337         while( tmp && tmp[i] ) {
338                 tmp[i] = lowerstr(tmp[i]);
339                 if( screen_get_id(tmp[i]) == -1 )
340                         fprintf(stderr,
341                                 _("Error: Unsupported screen \"%s\"\n"),
342                                 tmp[i]);
343                 else {
344                         screen = g_realloc(screen, (j+2)*sizeof(char *));
345                         screen[j++] = g_strdup(tmp[i]);
346                         screen[j] = NULL;
347                 }
348                 i++;
349         }
350         g_strfreev(tmp);
351         if( screen == NULL )
352                 return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
354         return screen;
357 static int
358 read_rc_file(char *filename, options_t *options)
360   int fd;
361   int quit  = 0;
362   int free_filename = 0;
364   if( filename==NULL )
365     return -1;
367   D("Reading configuration file %s\n", filename);
368   if( (fd=open(filename,O_RDONLY)) <0 )
369     {
370       perror(filename);
371       if( free_filename )
372         g_free(filename);
373       return -1;
374     }
376   while( !quit )
377     {
378       int i,j;
379       int len;
380       int match_found;
381       char line[MAX_LINE_LENGTH];
382       char name[MAX_LINE_LENGTH];
383       char value[MAX_LINE_LENGTH];
385       line[0]  = '\0';
386       value[0] = '\0';
388       i = 0;
389       /* read a line ending with '\n' */
390       do {
391         len = read( fd, &line[i], 1 );
392         if( len == 1 )
393           i++;
394         else
395           quit = 1;
396       } while( !quit && i<MAX_LINE_LENGTH && line[i-1]!='\n' );
397       
398      
399       /* remove trailing whitespace */
400       line[i] = '\0';
401       i--;
402       while( i>=0 && IS_WHITESPACE(line[i]) )
403         {
404           line[i] = '\0';
405           i--;
406         }     
407       len = i+1;
409       if( len>0 )
410         {
411           i = 0;
412           /* skip whitespace */
413           while( i<len && IS_WHITESPACE(line[i]) )
414             i++;
415           
416           /* continue if this line is not a comment */
417           if( line[i] != COMMENT_TOKEN )
418             {
419               /* get the name part */
420               j=0;
421               while( i<len && line[i]!='=' && !IS_WHITESPACE(line[i]) )
422                 {
423                   name[j++] = line[i++];
424                 }
425               name[j] = '\0';
426               
427               /* skip '=' and whitespace */
428               while( i<len && (line[i]=='=' || IS_WHITESPACE(line[i])) )
429                 i++;
430               
431               /* get the value part */
432               j=0;
433               while( i<len )
434                 {
435                   value[j++] = line[i++];
436                 }
437               value[j] = '\0';
438               
439               match_found = 1;
441               /* key definition */
442               if( !strcasecmp(CONF_KEY_DEFINITION, name) )
443                 {
444                   parse_key_definition(value);
445                 }
446               /* enable colors */
447               else if( !strcasecmp(CONF_ENABLE_COLORS, name) )
448                 {
449                   options->enable_colors = str2bool(value);
450                 }
451               /* auto center */
452               else if( !strcasecmp(CONF_AUTO_CENTER, name) )
453                 {
454                   options->auto_center = str2bool(value);
455                 }
456               /* color assignment */
457               else if( !strcasecmp(CONF_COLOR, name) )
458                 {
459                   parse_color(value);
460                 }
461               /* wide cursor */
462               else if( !strcasecmp(CONF_WIDE_CURSOR, name) )
463                 {
464                   options->wide_cursor = str2bool(value);
465                 }
466               /* timer display type */
467               else if( !strcasecmp(CONF_TIMEDISPLAY_TYPE, name) )
468                 {
469                     g_free(options->timedisplay_type);
470                     options->timedisplay_type=g_strdup(parse_timedisplay_type(value));
471                     D("deb");
472                     D(options->timedisplay_type);
473                 }
474               /* color definition */
475               else if( !strcasecmp(CONF_COLOR_DEFINITION, name) )
476                 {
477                   parse_color_definition(value);
478                 }
479               /* list format string */
480               else if( !strcasecmp(CONF_LIST_FORMAT, name) )
481                 {
482                   g_free(options->list_format);
483                   options->list_format = get_format(value);
484                 }
485               /* status format string */
486               else if( !strcasecmp(CONF_STATUS_FORMAT, name) )
487                 {
488                   g_free(options->status_format);
489                   options->status_format = get_format(value);
490                 }
491               /* xterm title format string */
492               else if( !strcasecmp(CONF_XTERM_TITLE_FORMAT, name) )
493                 {
494                   g_free(options->xterm_title_format);
495                   options->xterm_title_format = get_format(value);
496                 }
497               else if( !strcasecmp(CONF_LIST_WRAP, name) )
498                 {
499                   options->list_wrap = str2bool(value);
500                 }
501               else if( !strcasecmp(CONF_FIND_WRAP, name) )
502                 {
503                   options->find_wrap = str2bool(value);
504                 }
505               else if( !strcasecmp(CONF_FIND_SHOW_LAST,name) )
506                 {
507                   options->find_show_last_pattern = str2bool(value);
508                 }
509               else if( !strcasecmp(CONF_AUDIBLE_BELL, name) )
510                 {
511                   options->audible_bell = str2bool(value);
512                 }
513               else if( !strcasecmp(CONF_VISIBLE_BELL, name) )
514                 {
515                   options->visible_bell = str2bool(value);
516                 }
517               else if( !strcasecmp(CONF_XTERM_TITLE, name) )
518                 {
519                   options->enable_xterm_title = str2bool(value);
520                 }
521               else if( !strcasecmp(CONF_ENABLE_MOUSE, name) )
522                 {
523                   options->enable_mouse = str2bool(value);
524                 }
525               else if( !strcasecmp(CONF_CROSSFADE_TIME, name) )
526                 {
527                   options->crossfade_time = atoi(value);
528                 }
529               else if( !strcasecmp(CONF_SEARCH_MODE, name) )
530                 {
531                   options->search_mode = atoi(value);
532                 }
533               else if( !strcasecmp(CONF_HIDE_CURSOR, name) )
534                 {
535                   options->hide_cursor = atoi(value);
536                 }
537               else if( !strcasecmp(CONF_SEEK_TIME, name) )
538                 {
539                   options->seek_time = atoi(value);
540                 }
541               else if( !strcasecmp(CONF_SCREEN_LIST, name) )
542                 {
543                   g_strfreev(options->screen_list);
544                   options->screen_list = check_screen_list(value);
545                 }
546               else if( !strcasecmp(CONF_SHOW_SPLASH, name) )
547                 {
548                   /* the splash screen was removed */
549                 }
550             else if( !strcasecmp(CONF_HOST, name))
551             {
552             options->host = get_format(value);
553             }
554             else if( !strcasecmp(CONF_PORT, name))
555             {
556             options->port = atoi(get_format(value));
557             }
558             else if( !strcasecmp(CONF_PASSWORD, name))
559               {
560                 options->password = get_format(value);
561               }
562             else if( !strcasecmp(CONF_LYRICS_TIMEOUT, name))
563             {
564             options->lyrics_timeout = atoi(get_format(value));
565             }           
566             else if( !strcasecmp(CONF_SCROLL, name))
567             {
568             options->scroll = str2bool(value);
569             }
570             else if( !strcasecmp(CONF_SCROLL_SEP, name))
571             {
572             g_free(options->scroll_sep);
573             options->scroll_sep = get_format(value);
574             }
575               else
576                 {
577                   match_found = 0;
578                 }
580               if( !match_found )
581                 fprintf(stderr, 
582                         _("Unknown configuration parameter: %s\n"), 
583                         name);
584               D("conf>  %s = %s %s\n", name, value,
585                 match_found ? "" : "- UNKNOWN SETTING!" );
586             }
587         }         
588     }
590   D("--\n\n");
592   if( free_filename )
593     g_free(filename);
594  
595   return 0;
598 int
599 check_user_conf_dir(void)
601         int retval;
602         char *dirname = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
604         if (g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
605                 g_free(dirname);
606                 return 0;
607         }
609         retval = mkdir(dirname, 0755);
610         g_free(dirname);
611         return retval;
614 char *
615 get_user_key_binding_filename(void)
617         return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
621 int
622 read_configuration(options_t *options)
624   char *filename = NULL;
626   /* check for command line configuration file */
627   if( options->config_file )
628     filename = g_strdup(options->config_file);
630   /* check for user configuration ~/.ncmpc/config */
631   if( filename == NULL )
632     {
633       filename = g_build_filename(g_get_home_dir(), 
634                                   "." PACKAGE, "config", NULL);
635       if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
636         {
637           g_free(filename);
638           filename = NULL;
639         }
640     }
642   /* check for  global configuration SYSCONFDIR/ncmpc/config */
643   if( filename == NULL )
644     {
645       filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
646       if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
647         {
648           g_free(filename);
649           filename = NULL;
650         }
651     }
653   /* load configuration */
654   if( filename )
655     {
656       read_rc_file(filename, options);
657       g_free(filename);
658       filename = NULL;
659     }
661   /* check for command line key binding file */
662   if( options->key_file )
663     filename = g_strdup(options->key_file);
665   /* check for  user key bindings ~/.ncmpc/keys */
666   if( filename == NULL )
667     {
668       filename = get_user_key_binding_filename();
669       if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
670         {
671           g_free(filename);
672           filename = NULL;
673         }
674     }
676   /* check for  global key bindings SYSCONFDIR/ncmpc/keys */
677   if( filename == NULL )
678     {
679       filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
680       if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
681         {
682           g_free(filename);
683           filename = NULL;
684         }
685     }
687   /* load key bindings */
688   if( filename )
689     {
690       read_rc_file(filename, options);
691       g_free(filename);
692       filename = NULL;
693     }
695   return 0;