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 static int
39 str2bool(char *str)
40 {
41 if( !strcasecmp(str,"no") || !strcasecmp(str,"false") ||
42 !strcasecmp(str,"off") || !strcasecmp(str,"0") )
43 return 0;
44 return 1;
45 }
47 static int
48 str2color(char *str)
49 {
50 if( !strcasecmp(str,"black") )
51 return COLOR_BLACK;
52 else if( !strcasecmp(str,"red") )
53 return COLOR_RED;
54 else if( !strcasecmp(str,"green") )
55 return COLOR_GREEN;
56 else if( !strcasecmp(str,"yellow") )
57 return COLOR_YELLOW;
58 else if( !strcasecmp(str,"blue") )
59 return COLOR_BLUE;
60 else if( !strcasecmp(str,"magenta") )
61 return COLOR_MAGENTA;
62 else if( !strcasecmp(str,"cyan") )
63 return COLOR_CYAN;
64 else if( !strcasecmp(str,"white") )
65 return COLOR_WHITE;
66 #if 0
67 else if( !strcasecmp(str,"grey") )
68 return COLOR_BLACK | A_BOLD;
69 else if( !strcasecmp(str,"brightred") )
70 return COLOR_RED | A_BOLD;
71 else if( !strcasecmp(str,"brightgreen") )
72 return COLOR_GREEN | A_BOLD;
73 else if( !strcasecmp(str,"brightyellow") )
74 return COLOR_YELLOW | A_BOLD;
75 else if( !strcasecmp(str,"brightblue") )
76 return COLOR_BLUE | A_BOLD;
77 else if( !strcasecmp(str,"brightmagenta") )
78 return COLOR_MAGENTA | A_BOLD;
79 else if( !strcasecmp(str,"brightcyan") )
80 return COLOR_CYAN | A_BOLD;
81 else if( !strcasecmp(str,"brightwhite") )
82 return COLOR_WHITE | A_BOLD;
83 #endif
84 fprintf(stderr,"Warning: unknown color %s\n", str);
85 return -1;
86 }
88 int
89 read_rc_file(char *filename, options_t *options)
90 {
91 int fd;
92 int quit = 0;
93 int color = -1;
94 int free_filename = 0;
96 if( filename==NULL )
97 {
98 filename = concat_path(getenv("HOME"), RCFILE);
99 free_filename = 1;
100 }
102 D(printf("\n--Reading configuration file %s\n", filename));
103 if( (fd=open(filename,O_RDONLY)) <0 )
104 {
105 D(perror(filename));
106 if( free_filename )
107 free(filename);
108 return -1;
109 }
111 while( !quit )
112 {
113 int i,j;
114 int len;
115 int match_found;
116 char line[MAX_LINE_LENGTH];
117 char name[MAX_LINE_LENGTH];
118 char value[MAX_LINE_LENGTH];
120 line[0] = '\0';
121 value[0] = '\0';
123 i = 0;
124 /* read a line ending with '\n' */
125 do {
126 len = read( fd, &line[i], 1 );
127 if( len == 1 )
128 i++;
129 else
130 quit = 1;
131 } while( !quit && i<MAX_LINE_LENGTH && line[i-1]!='\n' );
134 /* remove trailing whitespace */
135 line[i] = '\0';
136 i--;
137 while( i>=0 && IS_WHITESPACE(line[i]) )
138 {
139 line[i] = '\0';
140 i--;
141 }
142 len = i+1;
144 if( len>0 )
145 {
146 i = 0;
147 /* skip whitespace */
148 while( i<len && IS_WHITESPACE(line[i]) )
149 i++;
151 /* continue if this line is not a comment */
152 if( line[i] != COMMENT_TOKEN )
153 {
154 /* get the name part */
155 j=0;
156 while( i<len && line[i]!='=' && !IS_WHITESPACE(line[i]) )
157 {
158 name[j++] = line[i++];
159 }
160 name[j] = '\0';
162 /* skip '=' and whitespace */
163 while( i<len && (line[i]=='=' || IS_WHITESPACE(line[i])) )
164 i++;
166 /* get the value part */
167 j=0;
168 while( i<len )
169 {
170 value[j++] = line[i++];
171 }
172 value[j] = '\0';
174 match_found = 0;
176 /* enable colors */
177 if( !strcasecmp(CONF_ENABLE_COLORS, name) )
178 {
179 options->enable_colors = str2bool(value);
180 match_found = 1;
181 }
182 /* background color */
183 else if( !strcasecmp(CONF_COLOR_BACKGROUND, name) )
184 {
185 if( (color=str2color(value)) >= 0 )
186 options->bg_color = color;
187 match_found = 1;
188 }
189 /* color - top (title) window */
190 else if( !strcasecmp(CONF_COLOR_TITLE, name) )
191 {
192 if( (color=str2color(value)) >= 0 )
193 options->title_color = color;
194 match_found = 1;
195 }
196 /* color - line (title) window */
197 else if( !strcasecmp(CONF_COLOR_LINE, name) )
198 {
199 if( (color=str2color(value)) >= 0 )
200 options->line_color = color;
201 match_found = 1;
202 }
203 /* color - list window */
204 else if( !strcasecmp(CONF_COLOR_LIST, name) )
205 {
206 if( (color=str2color(value)) >= 0 )
207 options->list_color = color;
208 match_found = 1;
209 }
210 /* color - progress bar */
211 else if( !strcasecmp(CONF_COLOR_PROGRESS, name) )
212 {
213 if( (color=str2color(value)) >= 0 )
214 options->progress_color = color;
215 match_found = 1;
216 }
217 /* color - status window */
218 else if( !strcasecmp(CONF_COLOR_STATUS, name) )
219 {
220 if( (color=str2color(value)) >= 0 )
221 options->status_color = color;
222 match_found = 1;
223 }
224 /* color - alerts */
225 else if( !strcasecmp(CONF_COLOR_ALERT, name) )
226 {
227 if( (color=str2color(value)) >= 0 )
228 options->alert_color = color;
229 match_found = 1;
230 }
233 if( !match_found )
234 fprintf(stderr,
235 "Unknown configuration parameter: %s\n",
236 name);
237 #ifdef DEBUG
238 printf( " %s = %s %s\n",
239 name,
240 value,
241 match_found ? "" : "- UNKNOWN SETTING!" );
242 #endif
244 }
245 }
246 }
248 D(printf( "--\n\n" ));
250 if( free_filename )
251 free(filename);
253 return 0;
254 }