-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
207 lines (194 loc) · 4.32 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "errors.h"
#include "config.h"
#define fmterr() do{fprintf(stderr,"Format error in configuration file %s on line %d\n", filename, lineno);}while(0)
const struct _config defconfig = {
.oneshot = false,
.forking = true,
.initialshow = true,
.listen_ins = true,
.listen_outs = true,
.login_command = NULL,
.login_message = "",
.logout_command = NULL,
.logout_message = ""
};
bool str_to_bool(char*);
char *chomp(char*);
void insert_option(char*, char*, struct _config*);
struct _config *load_config(char *filename)
{
static struct _config cfg;
cfg = defconfig;
FILE *fp = fopen(filename, "r");
if(errno == ENOENT)
return &cfg;
if(fp == NULL)
fatalperror("fopen");
char *line = NULL;
size_t len = 0;
int line_number = 0; // UNUSED?
while(getline(&line, &len, fp) != -1)
{
char *origline = line;
line_number++;
chomp(line);
// OPTIONAL Move past whitespace
if(line[0] == 0 || line[0] == '#')
{
line = origline;
continue; // Empty line/Comment
}
int i;
for(i = 0; isalnum(line[i]) || line[i] == '-'; i++);
char *name = malloc(i + 1); // Get memory for name + terminating NULL
if(name == NULL)
fatalperror("malloc");
strncpy(name, line, i);
name[i] = 0;
line += i;
// OPTIONAL Move past whitespace
if(line[0] != '=')
fatalerror("Syntax error in configuration file on line %d.\n", line_number);
line++; // Move past equal sign
// OPTIONAL Move past whitespace
for(i = 0; line[i] != 0; i++); // Count the bytes in the value
char *value = malloc(i + 1); // Get memory for value + terminating NULL
if(value == NULL)
fatalperror("malloc");
strncpy(value, line, i);
value[i] = 0;
// use name/value
insert_option(name, value, &cfg);
line = origline;
}
free(line);
fclose(fp);
return &cfg;
}
/*struct _config *load_config(char *filename)
{
#define skip_white() while(isspace(tmp[idx])){idx++}
static struct _config ret;
ret = defconfig;
FILE *c = fopen(filename, "r");
if(c == NULL)
fatalperror("fopen");
char *line = malloc(64);
if(line == NULL)
fatalperror("malloc");
ssize_t bytesread;
size_t n = 64;
unsigned int lineno = 0;
while((bytesread = getline(&line, &n, c)) > 0)
{
char *tmp = line;
unsigned int idx = 0;
size_t linelen = strlen(tmp);
chomp(tmp, &linelen); // Remove trailing newline
if(linelen == 0) // Skip if empty line
continue;
if(tmp[0] == '#') // Skip this line if it is a comment
continue;
for(idx = 0; tmp[idx] != 0; idx++)
{
}
lineno++;
}
free(line);
fclose(c);
if(bytesread == -1)
fatalperror("getline");
return &ret;
}*/
void insert_option(char *key, char *value, struct _config *cfg)
{
if(!strcmp(key, "login-command"))
{
cfg->login_command = value;
}
else if(!strcmp(key, "logout-command"))
{
cfg->logout_command = value;
}
else if(!strcmp(key, "login-message"))
{
cfg->login_message = value;
}
else if(!strcmp(key, "logout-message"))
{
cfg->logout_message = value;
}
else if(!strcmp(key, "oneshot"))
{
cfg->oneshot = str_to_bool(value);
}
else if(!strcmp(key, "forking"))
{
cfg->forking = str_to_bool(value);
}
else if(!strcmp(key, "initialshow"))
{
cfg->initialshow = str_to_bool(value);
}
else if(!strcmp(key, "listen"))
{
if(!strcmp(value, "all"))
{
cfg->listen_ins = true;
cfg->listen_outs = true;
}
else if(!strcmp(value, "none"))
{
cfg->listen_ins = false;
cfg->listen_outs = false;
}
else if(!strcmp(value, "ins"))
{
cfg->listen_ins = true;
cfg->listen_outs = false;
}
else if(!strcmp(value, "outs"))
{
cfg->listen_ins = false;
cfg->listen_outs = true;
}
else
{
nonfatalerror("Unknown value '%s' for 'listen', should be one of all,none,ins,outs.\n", value);
}
}
else
{
nonfatalerror("Unknown option '%s' in config file.\n", key);
}
(void)cfg;
}
bool str_to_bool(char *str)
{
if(!strcmp(str, "yes") || !strcmp(str, "true") || !strcmp(str, "1"))
return true;
else if(!strcmp(str, "no") || !strcmp(str, "false") || !strcmp(str, "0"))
return false;
else
nonfatalerror("Invalid boolean value '%s' in configuration file.\n", str);
return false;
}
char *chomp(char *buf)
{
char *c = buf + strlen(buf) - 1;
if(*c == '\n')
{
*c = 0;
c--;
if(*c == '\r')
{
*c = 0;
}
}
return buf;
}