Add xterm-256color as a valid terminal.
[eco.git] / config.c
blobc94b35ad5302c1ecb4a3c0f6459446fe40918418
1 /*
2 * Copyright (C) 2008 Diego Hernan Borghetti.
3 * Eco
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
10 #include "debug.h"
11 #include "config.h"
14 /* sections list. */
15 E_Section *sections= NULL;
17 int e_config_read_line(FILE *fp, char *buf, int len)
19 int i, c;
21 for (i= 0; i < len; i++) {
22 c= fgetc(fp);
23 if (c == EOF) {
24 if (i) {
25 buf[i]= '\0';
26 return(i);
28 return(-1);
31 if (c == '\n') {
32 buf[i]= '\0';
33 return(i);
35 buf[i]= c;
38 /* the line is too big, so we skip this. */
39 return(0);
42 int __e_config_is_section(char *line)
44 char *p;
46 p= line;
47 while (p && (*p == ' ' || *p == '\t'))
48 p++;
50 if (*p == '[')
51 return(0);
52 return(1);
55 int __e_config_is_option(char *line)
57 char *p;
59 p= strchr(line, '=');
60 if (p)
61 return(0);
62 return(1);
65 E_Section *__e_config_section(char *line)
67 E_Section *sc;
68 char *p;
69 int i, found;
71 sc= (E_Section *)malloc(sizeof(E_Section));
72 sc->next= NULL;
73 sc->list= NULL;
74 sc->last= NULL;
76 /* is valid if the line have space or tab befor the '[' */
77 p= line;
78 while (*p == ' ' || *p == '\t')
79 p++;
81 /* ok, this is the first character != of ' ' or '\t'
82 * and is the start of the section, '[' that is why
83 * we add a + two character.
85 p+= 2;
87 /* at this point we are in the start of the section name,
88 * but need take care that the section name maybe have space,
89 * for eg. [ Keys ] and not [Keys].. so we support both model.
91 while (p && (*p == ' ' || *p == '\t'))
92 p++;
94 if (!p) {
95 free((void *)sc);
96 return(NULL);
99 /* count the number of character. */
100 for (i= 0, found= 0; i < strlen(p); i++) {
101 if (p[i] == ' ' || p[i] == ']') {
102 found= 1;
103 break;
107 if (!found) {
108 free((void *)sc);
109 return(NULL);
112 sc->name= (char *)malloc(i+1);
113 i= 0;
114 while (p) {
115 if (*p == ' ' || *p == ']') {
116 sc->name[i]= '\0';
117 break;
119 sc->name[i]= *p;
120 i++;
121 p++;
124 return(sc);
127 E_Config *__e_config_option(char *line)
129 E_Config *cf;
130 char *p;
131 int i;
133 cf= (E_Config *)malloc(sizeof(E_Config));
134 cf->next= NULL;
135 cf->key= NULL;
136 cf->value= NULL;
138 /* the options always can have tab or ' ' */
139 p= line;
140 while (*p == ' ' || *p == '\t')
141 p++;
143 /* count the number of characters. */
144 for (i= 0; i < strlen(p); i++) {
145 if (p[i] == ' ' || p[i] == '=')
146 break;
149 cf->key= (char *)malloc(i+1);
150 i= 0;
151 while (p) {
152 if (*p == ' ' || *p == '=') {
153 cf->key[i]= '\0';
154 break;
156 cf->key[i]= *p;
157 i++;
158 p++;
161 /* now the value. */
162 p= strchr(line, '=');
163 p++;
165 /* the value also have space or tab, so skip it. */
166 while (p && (*p == ' ' || *p == '\t'))
167 p++;
169 cf->value= (char *)malloc(strlen(p)+1);
170 strcpy(cf->value, p);
171 return(cf);
174 void e_config_parse(char *file)
176 E_Section *sc;
177 E_Config *cf;
178 FILE *fp;
179 char *line;
180 int i;
182 fp= fopen(file, "r");
183 if (!fp)
184 return;
186 line= (char *)malloc(1024);
187 sc= NULL;
188 i= e_config_read_line(fp, line, 1023);
189 while (i != -1) {
190 if (i == 0)
191 goto next_line;
193 /* skip comments and blank lines */
194 if (line[0] == '#' || line[0] == '\n')
195 goto next_line;
197 if (__e_config_is_section(line) == 0) {
198 sc= __e_config_section(line);
199 sc->next= sections;
200 sections= sc;
201 goto next_line;
204 /* if we don't have a section, skip this line. */
205 if (!sc)
206 goto next_line;
208 if (__e_config_is_option(line) == 0) {
209 cf= __e_config_option(line);
210 cf->next= NULL;
211 if (!sc->list)
212 sc->list= cf;
213 if (sc->last)
214 sc->last->next= cf;
215 sc->last= cf;
217 next_line:
218 i= e_config_read_line(fp, line, 1023);
221 #ifdef WITH_DEBUG
222 e_debug_printf("==> Config File <==\n");
223 sc= sections;
224 while (sc) {
225 e_debug_printf("Section: (%s)\n", sc->name);
226 cf= sc->list;
227 while (cf) {
228 e_debug_printf("\tKey: (%s)\n", cf->key);
229 e_debug_printf("\tValue: (%s)\n", cf->value);
230 cf= cf->next;
232 sc= sc->next;
234 e_debug_printf("==> End Config File <==\n");
235 #endif
237 fclose(fp);
238 free((void *)line);
241 void e_config_init(void)
243 char *home;
244 char *file;
246 home= getenv("HOME");
247 if (!home)
248 return;
250 file= (char *)malloc(strlen(home) + 6);
251 sprintf(file, "%s/.eco", home);
252 e_config_parse(file);
253 free((void *)file);
256 E_Section *e_config_find_section(char *name)
258 E_Section *p;
260 p= sections;
261 while (p) {
262 if (!strcmp(p->name, name))
263 return(p);
264 p= p->next;
266 return(NULL);
269 E_Config *e_config_find_config(E_Section *sc, char *key)
271 E_Config *p;
273 p= sc->list;
274 while (p) {
275 if (!strcmp(p->key, key))
276 return(p);
277 p= p->next;
279 return(NULL);
282 char *e_config_get(char *name, char *key)
284 E_Section *sc;
285 E_Config *cf;
287 sc= e_config_find_section(name);
288 if (sc) {
289 cf= e_config_find_config(sc, key);
290 if (cf)
291 return(cf->value);
293 return(NULL);
296 int e_config_get_int(char *name, char *key)
298 E_Section *sc;
299 E_Config *cf;
301 sc= e_config_find_section(name);
302 if (sc) {
303 cf= e_config_find_config(sc, key);
304 if (cf)
305 return(atoi(cf->value));
307 return(-1);
310 char e_config_get_char(char *name, char *key)
312 E_Section *sc;
313 E_Config *cf;
315 sc= e_config_find_section(name);
316 if (sc) {
317 cf= e_config_find_config(sc, key);
318 if (cf)
319 return((char)atoi(cf->value));
321 return((char)-1);