24
|
1 |
#include <stdio.h> |
|
2 |
#include <stdlib.h> |
|
3 |
#include <unistd.h> |
|
4 |
#include <ctype.h> |
|
5 |
#include <string.h> |
169
|
6 |
#include <glib.h> |
24
|
7 |
|
|
8 |
#include "list.h" |
|
9 |
|
|
10 |
#define MAX_LENGHT_INPUT 1024 |
|
11 |
#define cfg_entry(n) list_entry(n, cfg_entry_t, list) |
|
12 |
|
|
13 |
typedef struct _cfg_entry_t { |
|
14 |
char *key; |
|
15 |
char *value; |
|
16 |
struct list_head list; |
|
17 |
} cfg_entry_t; |
|
18 |
|
|
19 |
static LIST_HEAD(cfg_list); |
|
20 |
|
|
21 |
|
|
22 |
void push_in_list(char *key, char *value) |
|
23 |
{ |
169
|
24 |
cfg_entry_t *new_entry = g_new0(1, sizeof(cfg_entry_t)); |
24
|
25 |
|
169
|
26 |
new_entry->key = (char *) g_new0(1, strlen(key) + 1); |
|
27 |
new_entry->value = (char *) g_new0(1, strlen(value) + 1); |
24
|
28 |
|
|
29 |
strcpy(new_entry->key, key); |
|
30 |
strcpy(new_entry->value, value); |
|
31 |
|
|
32 |
list_add(&new_entry->list, &cfg_list); |
|
33 |
} |
|
34 |
|
|
35 |
int cfg_file(char *filename) |
|
36 |
{ |
|
37 |
FILE *fp; |
|
38 |
char *buf; |
|
39 |
char *line; |
|
40 |
char *value; |
|
41 |
|
169
|
42 |
if (!filename) { |
|
43 |
// Use default config file locations |
|
44 |
char *home = getenv("HOME"); |
|
45 |
if (!home) { |
|
46 |
ut_WriteLog("Can't find home dir!\n"); |
|
47 |
exit(EXIT_FAILURE); |
|
48 |
} |
|
49 |
filename = g_new(char, strlen(home)+24); |
|
50 |
sprintf(filename, "%s/.mcabber/mcabberrc", home); |
|
51 |
if ((fp = fopen(filename, "r")) == NULL) { |
|
52 |
// 2nd try... |
|
53 |
sprintf(filename, "%s/.mcabberrc", home); |
|
54 |
if ((fp = fopen(filename, "r")) == NULL) { |
|
55 |
fprintf(stderr, "Cannot open config file!\n"); |
|
56 |
exit(EXIT_FAILURE); |
|
57 |
} |
|
58 |
} |
|
59 |
g_free(filename); |
|
60 |
} |
|
61 |
else if ((fp = fopen(filename, "r")) == NULL) { |
24
|
62 |
perror("fopen (parsecfg.c:46)"); |
|
63 |
exit(EXIT_FAILURE); |
|
64 |
} |
|
65 |
|
169
|
66 |
buf = g_new(255); |
|
67 |
|
24
|
68 |
while (fgets(buf, 255, fp) != NULL) { |
|
69 |
line = buf; |
|
70 |
|
|
71 |
while (isspace((int) *line)) |
|
72 |
line++; |
|
73 |
|
|
74 |
while ((strlen(line) > 0) |
|
75 |
&& isspace((int) line[strlen(line) - 1])) |
|
76 |
line[strlen(line) - 1] = '\0'; |
|
77 |
|
|
78 |
if ((*line == '\n') || (*line == '\0') || (*line == '#')) |
|
79 |
continue; |
|
80 |
|
|
81 |
if ((strchr(line, '=') != NULL)) { |
|
82 |
value = strchr(line, '='); |
|
83 |
*value = '\0'; |
|
84 |
value++; |
|
85 |
|
|
86 |
while (isspace((int) *value)) |
|
87 |
value++; |
|
88 |
|
|
89 |
while ((strlen(line) > 0) |
|
90 |
&& isspace((int) line[strlen(line) - 1])) |
|
91 |
line[strlen(line) - 1] = '\0'; |
|
92 |
|
|
93 |
push_in_list(line, value); |
|
94 |
continue; |
|
95 |
} |
|
96 |
fprintf(stderr, "CFG: orphaned line \"%s\"\n", line); |
|
97 |
} |
169
|
98 |
g_free(buf); |
24
|
99 |
return 1; |
|
100 |
} |
|
101 |
|
|
102 |
char *cfg_read(char *key) |
|
103 |
{ |
|
104 |
struct list_head *n, *pos; |
|
105 |
cfg_entry_t *search_entry = NULL; |
|
106 |
|
|
107 |
list_for_each_safe(pos, n, &cfg_list) { |
|
108 |
search_entry = cfg_entry(pos); |
|
109 |
if (search_entry->key) { |
|
110 |
if (!strcasecmp(search_entry->key, key)) { |
|
111 |
return search_entry->value; |
|
112 |
} |
|
113 |
} |
|
114 |
} |
|
115 |
return NULL; |
|
116 |
} |