Code

forgot to add this
[ncmpc.git] / conf.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <ncurses.h>
11 #include "config.h"
12 #include "options.h"
13 #include "support.h"
14 #include "conf.h"
16 #ifdef DEBUG
17 #define D(x) x
18 #else
19 #define D(x)
20 #endif
22 #define RCFILE "." PACKAGE "rc"
24 #define MAX_LINE_LENGTH 1024
25 #define COMMENT_TOKEN   '#'
27 /* configuration field names */
28 #define CONF_ENABLE_COLORS           "enable_colors"
29 #define CONF_COLOR_BACKGROUND        "background_color"
30 #define CONF_COLOR_TITLE             "title_color"
31 #define CONF_COLOR_LINE              "line_color"
32 #define CONF_COLOR_LIST              "list_color"
33 #define CONF_COLOR_PROGRESS          "progress_color"
34 #define CONF_COLOR_STATUS            "status_color"
35 #define CONF_COLOR_ALERT             "alert_color"
38 #define IS_WHITESPACE(c) (c==' ' || c=='\t' || c=='\r' || c=='\n')
41 static int
42 str2bool(char *str)
43 {
44   if( !strcasecmp(str,"no")  || !strcasecmp(str,"false") || 
45       !strcasecmp(str,"off") || !strcasecmp(str,"0") )
46     return 0;
47   return 1;
48 }
50 static int
51 str2color(char *str)
52 {
53   if( !strcasecmp(str,"black") )
54     return COLOR_BLACK;
55   else if( !strcasecmp(str,"red") )
56     return COLOR_RED;
57   else if( !strcasecmp(str,"green") )
58     return COLOR_GREEN;
59   else if( !strcasecmp(str,"yellow") )
60     return COLOR_YELLOW;
61   else if( !strcasecmp(str,"blue") )
62     return COLOR_BLUE;
63   else if( !strcasecmp(str,"magenta") )
64     return COLOR_MAGENTA;
65   else if( !strcasecmp(str,"cyan") )
66     return COLOR_CYAN;
67   else if( !strcasecmp(str,"white") )
68     return COLOR_WHITE;
69 #if 0
70    else if( !strcasecmp(str,"grey") ) 
71      return COLOR_BLACK | A_BOLD;
72   else if( !strcasecmp(str,"brightred") )
73     return COLOR_RED | A_BOLD;
74   else if( !strcasecmp(str,"brightgreen") )
75     return COLOR_GREEN | A_BOLD;
76   else if( !strcasecmp(str,"brightyellow") )
77     return COLOR_YELLOW | A_BOLD;
78   else if( !strcasecmp(str,"brightblue") )
79     return COLOR_BLUE | A_BOLD;
80   else if( !strcasecmp(str,"brightmagenta") )
81     return COLOR_MAGENTA | A_BOLD;
82   else if( !strcasecmp(str,"brightcyan") )
83     return COLOR_CYAN | A_BOLD;
84   else if( !strcasecmp(str,"brightwhite") )
85     return COLOR_WHITE | A_BOLD;
86 #endif
87   fprintf(stderr,"Warning: unknown color %s\n", str);
88   return -1;
89 }
91 int
92 read_rc_file(char *filename, options_t *options)
93 {
94   int fd;
95   int quit  = 0;
96   int color = -1;
97   int free_filename = 0;
99   if( filename==NULL )
100     {
101       filename = concat_path(getenv("HOME"), RCFILE);
102       free_filename = 1;
103     }
105   D(printf("\n--Reading configuration file %s\n", filename));
106   if( (fd=open(filename,O_RDONLY)) <0 )
107     {
108       D(perror(filename));
109       if( free_filename )
110         free(filename);
111       return -1;
112     }
114   while( !quit )
115     {
116       int i,j;
117       int len;
118       int match_found;
119       char line[MAX_LINE_LENGTH];
120       char name[MAX_LINE_LENGTH];
121       char value[MAX_LINE_LENGTH];
123       line[0]  = '\0';
124       value[0] = '\0';
126       i = 0;
127       /* read a line ending with '\n' */
128       do {
129         len = read( fd, &line[i], 1 );
130         if( len == 1 )
131           i++;
132         else
133           quit = 1;
134       } while( !quit && i<MAX_LINE_LENGTH && line[i-1]!='\n' );
135       
136      
137       /* remove trailing whitespace */
138       line[i] = '\0';
139       i--;
140       while( i>=0 && IS_WHITESPACE(line[i]) )
141         {
142           line[i] = '\0';
143           i--;
144         }     
145       len = i+1;
147       if( len>0 )
148         {
149           i = 0;
150           /* skip whitespace */
151           while( i<len && IS_WHITESPACE(line[i]) )
152             i++;
153           
154           /* continue if this line is not a comment */
155           if( line[i] != COMMENT_TOKEN )
156             {
157               /* get the name part */
158               j=0;
159               while( i<len && line[i]!='=' && !IS_WHITESPACE(line[i]) )
160                 {
161                   name[j++] = line[i++];
162                 }
163               name[j] = '\0';
164               
165               /* skip '=' and whitespace */
166               while( i<len && (line[i]=='=' || IS_WHITESPACE(line[i])) )
167                 i++;
168               
169               /* get the value part */
170               j=0;
171               while( i<len )
172                 {
173                   value[j++] = line[i++];
174                 }
175               value[j] = '\0';
176               
177               match_found = 0;
178               
179               /* enable colors */
180               if( !strcasecmp(CONF_ENABLE_COLORS, name) )
181                 {
182                   options->enable_colors = str2bool(value);
183                   match_found = 1;
184                 }
185               /* background color */
186               else if( !strcasecmp(CONF_COLOR_BACKGROUND, name) )
187                 {
188                   if( (color=str2color(value)) >= 0 )
189                     options->bg_color = color;
190                   match_found = 1;
191                 }
192               /* color - top (title) window */
193               else if( !strcasecmp(CONF_COLOR_TITLE, name) )
194                 {
195                   if( (color=str2color(value)) >= 0 )
196                     options->title_color = color;
197                   match_found = 1;
198                 }
199               /* color - line (title) window */
200               else if( !strcasecmp(CONF_COLOR_LINE, name) )
201                 {
202                   if( (color=str2color(value)) >= 0 )
203                     options->line_color = color;
204                   match_found = 1;
205                 }
206               /* color - list window */
207               else if( !strcasecmp(CONF_COLOR_LIST, name) )
208                 {
209                   if( (color=str2color(value)) >= 0 )
210                     options->list_color = color;
211                   match_found = 1;
212                 }
213               /* color - progress bar */
214               else if( !strcasecmp(CONF_COLOR_PROGRESS, name) )
215                 {
216                   if( (color=str2color(value)) >= 0 )
217                     options->progress_color = color;
218                   match_found = 1;
219                 }
220               /* color - status window */
221               else if( !strcasecmp(CONF_COLOR_STATUS, name) )
222                 {
223                   if( (color=str2color(value)) >= 0 )
224                     options->status_color = color;
225                   match_found = 1;
226                 }
227               /* color - alerts */
228               else if( !strcasecmp(CONF_COLOR_ALERT, name) )
229                 {
230                   if( (color=str2color(value)) >= 0 )
231                     options->alert_color = color;
232                   match_found = 1;
233                 }
234               
236               if( !match_found )
237                 fprintf(stderr, 
238                         "Unknown configuration parameter: %s\n", 
239                         name);
240 #ifdef DEBUG
241               printf( "  %s = %s %s\n", 
242                       name, 
243                       value,
244                       match_found ? "" : "- UNKNOWN SETTING!" );
245 #endif
247             }
248         }         
249     }
251   D(printf( "--\n\n" ));
253   if( free_filename )
254     free(filename);
255  
256   return 0;