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