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