Code

Changed directory layout (for future use of gettext)
[ncmpc.git] / src / conf.c
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 <ctype.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
29 #include <glib.h>
30 #include <ncurses.h>
32 #include "config.h"
33 #include "options.h"
34 #include "support.h"
35 #include "command.h"
36 #include "colors.h"
37 #include "conf.h"
39 #ifdef DEBUG
40 #define D(x) x
41 #else
42 #define D(x)
43 #endif
45 #define ENABLE_OLD_COLOR_SYNTAX
47 #define MAX_LINE_LENGTH 1024
48 #define COMMENT_TOKEN   '#'
50 /* configuration field names */
51 #define CONF_ENABLE_COLORS           "enable_colors"
52 #define CONF_AUTO_CENTER             "auto_center"
53 #define CONF_WIDE_CURSOR             "wide_cursor"
54 #define CONF_KEY_DEFINITION          "key"
55 #define CONF_COLOR                   "color"
56 #define CONF_COLOR_DEFINITION        "colordef"
58 /* Deprecated - configuration field names */
59 #define CONF_COLOR_BACKGROUND        "background_color"
60 #define CONF_COLOR_TITLE             "title_color"
61 #define CONF_COLOR_LINE              "line_color"
62 #define CONF_COLOR_LIST              "list_color"
63 #define CONF_COLOR_PROGRESS          "progress_color"
64 #define CONF_COLOR_STATUS            "status_color"
65 #define CONF_COLOR_ALERT             "alert_color"
68 typedef enum {
69   KEY_PARSER_UNKNOWN,
70   KEY_PARSER_CHAR,
71   KEY_PARSER_DEC,
72   KEY_PARSER_HEX,
73   KEY_PARSER_DONE
74 } key_parser_state_t;
76 static int
77 str2bool(char *str)
78 {
79   if( !strcasecmp(str,"no")  || !strcasecmp(str,"false") || 
80       !strcasecmp(str,"off") || !strcasecmp(str,"0") )
81     return 0;
82   return 1;
83 }
85 static int
86 parse_key_value(char *str, size_t len, char **end)
87 {
88   int i, value;
89   key_parser_state_t state;
91   i=0;
92   value=0;
93   state=KEY_PARSER_UNKNOWN;
94   *end = str;
96   while( i<len && state!=KEY_PARSER_DONE )
97     {
98       int next = 0;
99       int c = str[i];
101       if( i+1<len )
102         next = str[i+1];
104       switch(state)
105         {
106         case KEY_PARSER_UNKNOWN:
107           if( c=='\'' )
108             state = KEY_PARSER_CHAR;
109           else if( c=='0' && next=='x' )
110             state = KEY_PARSER_HEX;
111           else if( isdigit(c) )
112             state = KEY_PARSER_DEC;
113           else {
114             fprintf(stderr, "Error: Unsupported key definition - %s\n", str);
115             return -1;
116           }
117           break;
118         case KEY_PARSER_CHAR:
119           if( next!='\'' )
120             {
121               fprintf(stderr, "Error: Unsupported key definition - %s\n", str);
122               return -1;
123             }
124           value = c;
125           *end = str+i+2;
126           state = KEY_PARSER_DONE;
127           break;
128         case KEY_PARSER_DEC:
129           value = (int) strtol(str+(i-1), end, 10);
130           state = KEY_PARSER_DONE;
131           break;
132         case KEY_PARSER_HEX:
133           if( !isdigit(next) )
134             {
135               fprintf(stderr, "Error: Digit expexted after 0x - %s\n", str);
136               return -1;
137             }
138           value = (int) strtol(str+(i+1), end, 16);
139           state = KEY_PARSER_DONE;
140           break;
141         case KEY_PARSER_DONE:
142           break;
143         }
144       i++;
145     }
147   if( *end> str+len )
148     *end = str+len;
150   return value;
153 static int
154 parse_key_definition(char *str)
156   char buf[MAX_LINE_LENGTH];
157   char *p, *end;
158   size_t len = strlen(str);
159   int i,j,key;
160   int keys[MAX_COMMAND_KEYS];
161   command_t cmd;
163   /* get the command name */
164   i=0;
165   j=0;
166   memset(buf, 0, MAX_LINE_LENGTH);
167   while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
168     buf[j++] = str[i++];
169   if( (cmd=get_key_command_from_name(buf)) == CMD_NONE )
170     {
171       fprintf(stderr, "Error: Unknown key command %s\n", buf);
172       return -1;
173     }
175   /* skip whitespace */
176   while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) )
177     i++;
179   /* get the value part */
180   memset(buf, 0, MAX_LINE_LENGTH);
181   strncpy(buf, str+i, len-i);
182   len = strlen(buf);
183   if( len==0 )
184     {
185       fprintf(stderr,"Error: Incomplete key definition - %s\n", str);
186       return -1;
187     }
189   /* parse key values */
190   i = 0;
191   key = 0;
192   len = strlen(buf);
193   p = buf;
194   end = buf+len;
195   memset(keys, 0, sizeof(int)*MAX_COMMAND_KEYS);
196   while( i<MAX_COMMAND_KEYS && p<end && (key=parse_key_value(p,len+1,&p))>=0 )
197     {
198       keys[i++] = key;
199       while( p<end && (*p==',' || *p==' ' || *p=='\t') )
200         p++;
201       len = strlen(p);
202     } 
203   if( key<0 )
204     {
205       fprintf(stderr,"Error: Bad key definition - %s\n", str);
206       return -1;
207     }
209   return assign_keys(cmd, keys);
212 static int
213 parse_color(char *str)
215   char *name = str;
216   char *value = NULL;
217   int len,i;
219   i=0;
220   len=strlen(str);
221   /* get the color name */
222   while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
223     i++;
224   
225   /* skip whitespace */
226   while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) )
227     {
228       str[i]='\0';
229       i++;
230     } 
231   
232   if( i<len )
233     value = str+i;
235   return colors_assign(name, value);
239 static int
240 parse_color_definition(char *str)
242   char buf[MAX_LINE_LENGTH];
243   char *p, *end, *name;
244   size_t len = strlen(str);
245   int i,j,value;
246   short color, rgb[3];
248   /* get the command name */
249   i=0;
250   j=0;
251   memset(buf, 0, MAX_LINE_LENGTH);
252   while( i<len && str[i]!='=' && !IS_WHITESPACE(str[i]) )
253     buf[j++] = str[i++];
254   color=colors_str2color(buf);
255   if( color<0 )
256     {
257       fprintf(stderr, "Error: Bad color %s [%d]\n", buf, color);
258       return -1;
259     }
260   name = g_strdup(buf);
262   /* skip whitespace */
263   while( i<len && (str[i]=='=' || IS_WHITESPACE(str[i])) )
264     i++;
266   /* get the value part */
267   memset(buf, 0, MAX_LINE_LENGTH);
268   strncpy(buf, str+i, len-i);
269   len = strlen(buf);
270   if( len==0 )
271     {
272       fprintf(stderr,"Error: Incomplete color definition - %s\n", str);
273       g_free(name);
274       return -1;
275     }
277   /* parse r,g.b values with the key definition parser */
278   i = 0;
279   value = 0;
280   len = strlen(buf);
281   p = buf;
282   end = buf+len;
283   memset(rgb, 0, sizeof(short)*3);
284   while( i<3 && p<end && (value=parse_key_value(p,len+1,&p))>=0 )
285     {
286       rgb[i++] = value;
287       while( p<end && (*p==',' || *p==' ' || *p=='\t') )
288         p++;
289       len = strlen(p);
290     } 
291   if( value<0 || i!=3)
292     {
293       fprintf(stderr,"Error: Bad color definition - %s\n", str);
294       g_free(name);
295       return -1;
296     }
297   value = colors_define(name, rgb[0], rgb[1], rgb[2]);
298   g_free(name);
299   return value;
303 static int
304 read_rc_file(char *filename, options_t *options)
306   int fd;
307   int quit  = 0;
308   int free_filename = 0;
310   if( filename==NULL )
311     return -1;
313   D(printf("Reading configuration file %s\n", filename));
314   if( (fd=open(filename,O_RDONLY)) <0 )
315     {
316       perror(filename);
317       if( free_filename )
318         g_free(filename);
319       return -1;
320     }
322   while( !quit )
323     {
324       int i,j;
325       int len;
326       int match_found;
327       char line[MAX_LINE_LENGTH];
328       char name[MAX_LINE_LENGTH];
329       char value[MAX_LINE_LENGTH];
331       line[0]  = '\0';
332       value[0] = '\0';
334       i = 0;
335       /* read a line ending with '\n' */
336       do {
337         len = read( fd, &line[i], 1 );
338         if( len == 1 )
339           i++;
340         else
341           quit = 1;
342       } while( !quit && i<MAX_LINE_LENGTH && line[i-1]!='\n' );
343       
344      
345       /* remove trailing whitespace */
346       line[i] = '\0';
347       i--;
348       while( i>=0 && IS_WHITESPACE(line[i]) )
349         {
350           line[i] = '\0';
351           i--;
352         }     
353       len = i+1;
355       if( len>0 )
356         {
357           i = 0;
358           /* skip whitespace */
359           while( i<len && IS_WHITESPACE(line[i]) )
360             i++;
361           
362           /* continue if this line is not a comment */
363           if( line[i] != COMMENT_TOKEN )
364             {
365               /* get the name part */
366               j=0;
367               while( i<len && line[i]!='=' && !IS_WHITESPACE(line[i]) )
368                 {
369                   name[j++] = line[i++];
370                 }
371               name[j] = '\0';
372               
373               /* skip '=' and whitespace */
374               while( i<len && (line[i]=='=' || IS_WHITESPACE(line[i])) )
375                 i++;
376               
377               /* get the value part */
378               j=0;
379               while( i<len )
380                 {
381                   value[j++] = line[i++];
382                 }
383               value[j] = '\0';
384               
385               match_found = 1;
387               /* key definition */
388               if( !strcasecmp(CONF_KEY_DEFINITION, name) )
389                 {
390                   parse_key_definition(value);
391                 }
392               /* enable colors */
393               else if( !strcasecmp(CONF_ENABLE_COLORS, name) )
394                 {
395                   options->enable_colors = str2bool(value);
396                 }
397               /* auto center */
398               else if( !strcasecmp(CONF_AUTO_CENTER, name) )
399                 {
400                   options->auto_center = str2bool(value);
401                 }
402               /* color assignment */
403               else if( !strcasecmp(CONF_COLOR, name) )
404                 {
405                   parse_color(value);
406                 }
407 #ifdef ENABLE_OLD_COLOR_SYNTAX
408               /* background color */
409               else if( !strcasecmp(CONF_COLOR_BACKGROUND, name) )
410                 {
411                   fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
412                   colors_assign("background", value);
413                 }
414               /* color - top (title) window */
415               else if( !strcasecmp(CONF_COLOR_TITLE, name) )
416                 {
417                   fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
418                   colors_assign("title", value);
419                   colors_assign("title2", value);
420                 }
421               /* color - line (title) window */
422               else if( !strcasecmp(CONF_COLOR_LINE, name) )
423                 {
424                   fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
425                   colors_assign("line", value);
426                   colors_assign("line2", value);
427                 }
428               /* color - list window */
429               else if( !strcasecmp(CONF_COLOR_LIST, name) )
430                 {
431                   fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
432                   colors_assign("list", value);
433                 }
434               /* color - progress bar */
435               else if( !strcasecmp(CONF_COLOR_PROGRESS, name) )
436                 {
437                   fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
438                   colors_assign("progressbar", value);
439                 }
440               /* color - status window */
441               else if( !strcasecmp(CONF_COLOR_STATUS, name) )
442                 {
443                   fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
444                   colors_assign("status", value);
445                   colors_assign("status2", value);
446                 }
447               /* color - alerts */
448               else if( !strcasecmp(CONF_COLOR_ALERT, name) )
449                 {
450                   fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
451                   colors_assign("alert", value);
452                 }
453 #endif
454               /* wide cursor */
455               else if( !strcasecmp(CONF_WIDE_CURSOR, name) )
456                 {
457                   options->wide_cursor = str2bool(value);
458                 }
459               /* color definition */
460               else if( !strcasecmp(CONF_COLOR_DEFINITION, name) )
461                 {
462                   parse_color_definition(value);
463                 }
464               else
465                 {
466                   match_found = 0;
467                 }
468               
470               if( !match_found )
471                 fprintf(stderr, 
472                         "Unknown configuration parameter: %s\n", 
473                         name);
474 #ifdef DEBUG
475               printf( "  %s = %s %s\n", 
476                       name, 
477                       value,
478                       match_found ? "" : "- UNKNOWN SETTING!" );
479 #endif
481             }
482         }         
483     }
485   D(printf( "--\n\n" ));
487   if( free_filename )
488     g_free(filename);
489  
490   return 0;
493 int
494 check_user_conf_dir(void)
496   int retval;
497   char *dirname = g_build_filename(g_get_home_dir(), "." PACKAGE, NULL);
498   
499   if( g_file_test(dirname, G_FILE_TEST_IS_DIR) )
500     {
501       g_free(dirname);
502       return 0;
503     }
504   retval = mkdir(dirname, 0755);
505   g_free(dirname);
506   return retval;
509 char *
510 get_user_key_binding_filename(void)
512   return g_build_filename(g_get_home_dir(), "." PACKAGE, "keys", NULL);
516 int
517 read_configuration(options_t *options)
519   char *filename = NULL;
521   /* check for command line configuration file */
522   if( options->config_file )
523     filename = g_strdup(options->config_file);
525   /* check for user configuration ~/.ncmpc/config */
526   if( filename == NULL )
527     {
528       filename = g_build_filename(g_get_home_dir(), 
529                                   "." PACKAGE, "config", NULL);
530       if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
531         {
532           g_free(filename);
533           filename = NULL;
534         }
535     }
537   /* check for  global configuration SYSCONFDIR/ncmpc/config */
538   if( filename == NULL )
539     {
540       filename = g_build_filename(SYSCONFDIR, PACKAGE, "config", NULL);
541       if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
542         {
543           g_free(filename);
544           filename = NULL;
545         }
546     }
548   /* load configuration */
549   if( filename )
550     {
551       read_rc_file(filename, options);
552       g_free(filename);
553       filename = NULL;
554     }
556   /* check for command line key binding file */
557   if( options->key_file )
558     filename = g_strdup(options->key_file);
560   /* check for  user key bindings ~/.ncmpc/keys */
561   if( filename == NULL )
562     {
563       filename = get_user_key_binding_filename();
564       if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
565         {
566           g_free(filename);
567           filename = NULL;
568         }
569     }
571   /* check for  global key bindings SYSCONFDIR/ncmpc/keys */
572   if( filename == NULL )
573     {
574       filename = g_build_filename(SYSCONFDIR, PACKAGE, "keys", NULL);
575       if( !g_file_test(filename, G_FILE_TEST_IS_REGULAR) )
576         {
577           g_free(filename);
578           filename = NULL;
579         }
580     }
582   /* load key bindings */
583   if( filename )
584     {
585       read_rc_file(filename, options);
586       g_free(filename);
587       filename = NULL;
588     }
590   return 0;