forked from MagerValp/CGTerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
397 lines (345 loc) · 8.78 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include "config.h"
#ifdef WINDOWS
#define DIRCHAR '\\'
#else
#define DIRCHAR '/'
#endif
int cfg_read = 0;
#ifdef WINDOWS
#else
char *cfg_prefix = PREFIX;
#endif
char *cfg_homedir = NULL;
char *cfg_host = NULL;
unsigned int cfg_port = 23;
#ifdef WINDOWS
char *cfg_keyboard = "windows.kbd";
#else
char *cfg_keyboard = PREFIX "/share/cgterm/us.kbd";
#endif
int cfg_zoom = 2;
char *cfg_logfile = NULL;
int cfg_fullscreen = 0;
int cfg_localecho = 0;
int cfg_senddelay = 0;
int cfg_recvdelay = 0;
int cfg_reconnect = 0;
unsigned int cfg_nextreconnect = 0;
int cfg_columns = 40;
int cfg_rows = 25;
int cfg_sound = 1;
int cfg_numbookmarks = 0;
char *cfg_bookmark_alias[10];
char *cfg_bookmark_host[10];
int cfg_bookmark_port[10];
char cfg_xferdir[256];
int cfg_editmode = 0;
char host[256];
char keyboard[256];
char logfile[256];
char prefix[256];
FILE *fh = NULL;
int cfg_init(char *argv0) {
#ifdef WINDOWS
cfg_homedir = ".";
#else
int l;
if ((cfg_homedir = getenv("HOME")) == NULL) {
printf("$HOME is not set, using current directory\n");
cfg_homedir = ".";
}
if ((l = strlen(argv0)) > 22) {
if (strncmp("/Contents/MacOS/CG", argv0 + l - 22, 18) == 0) {
// we're running inside a macos .app
strncpy(prefix, argv0, l - 13);
prefix[l - 13] = 0;
strcat(prefix, "/Resources");
cfg_prefix = prefix;
strcpy(keyboard, prefix);
strcat(keyboard, "/share/cgterm/keyboard.kbd");
cfg_keyboard = keyboard;
}
}
#endif
getcwd(cfg_xferdir, 256);
return(0);
}
void real_cfg_change_dir(char *dirbuffer, char *newdir) {
int l, n;
char *p;
n = strlen(newdir);
if (n == 0 || (n == 1 && strcmp(".", newdir) == 0)) {
return;
}
#ifdef WINDOWS
if (newdir[n] == DIRCHAR) {
newdir[n--] = 0;
}
if (newdir[1] == ':' && isalpha(newdir[0])) {
strcpy(dirbuffer, newdir);
return;
}
#else
if (newdir[0] == '/') {
strcpy(dirbuffer, newdir);
return;
}
if (newdir[n - 1] == DIRCHAR) {
newdir[--n] = 0;
}
#endif
l = strlen(dirbuffer);
if (l == 0 || (l == 1 && strcmp(".", dirbuffer) == 0)) {
strcpy(dirbuffer, newdir);
return;
}
if (n == 2 && strcmp("..", newdir) == 0) {
if ((p = strrchr(dirbuffer, DIRCHAR))) {
if (p != dirbuffer) {
#ifdef WINDOWS
if (p == dirbuffer + 2 && dirbuffer[1] == ':') {
dirbuffer[3] = 0;
return;
}
#endif
*p = 0;
return;
} else {
dirbuffer[1] = 0;
return;
}
}
}
if (dirbuffer[l - 1] != DIRCHAR) {
dirbuffer[l++] = DIRCHAR;
dirbuffer[l] = 0;
}
strcat(dirbuffer, newdir);
}
int cfg_change_dir(char *dirbuffer, char *newdir) {
real_cfg_change_dir(dirbuffer, newdir);
return(1);
}
int cfg_file_exists(char *filename) {
if ((fh = fopen(filename, "r")) == NULL) {
return(0);
} else {
fclose(fh);
return(1);
}
}
void cfg_sethost(char *h) {
strcpy(host, h);
cfg_host = host;
}
void addhost(int num, char *alias, char *hostname, int port) {
char *ptr;
char *chr;
//printf("adding %s (%s:%d)\n", alias, hostname, port);
if ((ptr = malloc(strlen(hostname) + 1)) == NULL) {
printf("Malloc failed, prepare to crash\n"); // :P
}
strcpy(ptr, hostname);
if ((chr = strchr(ptr, ','))) {
*chr = 0;
}
cfg_bookmark_host[num] = ptr;
if ((ptr = malloc(strlen(alias) + 1)) == NULL) {
printf("Malloc failed, prepare to crash\n"); // :P
}
strcpy(ptr, alias);
if (strlen(alias) > 27) {
ptr[27] = 0;
}
if ((chr = strchr(ptr, ','))) {
*chr = 0;
}
cfg_bookmark_alias[num] = ptr;
cfg_bookmark_port[num] = port;
}
int addbookmark(char *line) {
char alias[256], hostname[256];
int port;
if (cfg_numbookmarks >= 10) {
printf("Too many bookmarks\n");
return(0);
}
port = 23;
// scanf is a piece of shit!
if (sscanf(line, "bookmark = %[^,] , %[a-zA-Z0-9.-] , %d \n", alias, hostname, &port) == 3) {
} else if (sscanf(line, "bookmark = %[a-zA-Z0-9.-] , %d \n", hostname, &port) == 2) {
strcpy(alias, hostname);
} else if (sscanf(line, "bookmark = %[^,] , %[a-zA-Z0-9.-] \n", alias, hostname) == 2) {
} else if (sscanf(line, "bookmark = %[a-zA-Z0-9.-] \n", hostname) == 1) {
strcpy(alias, hostname);
} else {
return(0);
}
addhost(cfg_numbookmarks, alias, hostname, port);
++cfg_numbookmarks;
return(1);
}
signed int cfg_readconfig(char *configfile) {
FILE *cfg;
char linebuf[256], key[16], value[256];
int line = 0;
if ((cfg = fopen(configfile, "r")) == NULL) {
return(0);
} else {
cfg_read = 1;
}
while (fgets(linebuf, sizeof(linebuf), cfg) != NULL) {
if (strlen(linebuf) >= sizeof(linebuf) - 1) {
printf("Line %d in %s is too long\n", line, configfile);
fclose(cfg);
return(-1);
}
if (linebuf[0] == '#') {
} else if (strlen(linebuf) >= 3) {
if (sscanf(linebuf, "%15s = %255s \n", key, value) == 2) {
if (strcmp(key, "host") == 0) {
if (strchr(value, '.')) {
cfg_sethost(value);
} else {
printf("Invalid hostname in %s: %s\n", configfile, value);
fclose(cfg);
return(-1);
}
} else if (strcmp(key, "port") == 0) {
cfg_port = strtol(value, (char **)NULL, 10);
if (cfg_port <= 0 || cfg_port > 65535) {
printf("Invalid port number in %s: %s\n", configfile, value);
fclose(cfg);
return(-1);
}
} else if (strcmp(key, "senddelay") == 0) {
cfg_senddelay = strtol(value, (char **)NULL, 10);
if (cfg_senddelay < 0 || cfg_senddelay > 10000) {
printf("Invalid send delay in %s: %s\n", configfile, value);
fclose(cfg);
return(-1);
}
} else if (strcmp(key, "receivedelay") == 0) {
cfg_recvdelay = strtol(value, (char **)NULL, 10);
if (cfg_recvdelay < 0 || cfg_recvdelay > 10000) {
printf("Invalid receive delay in %s: %s\n", configfile, value);
fclose(cfg);
return(-1);
}
} else if (strcmp(key, "keyboard") == 0) {
#ifdef WINDOWS
strcpy(keyboard, value);
#else
if (value[0] == '/') {
strcpy(keyboard, value);
} else {
strncpy(keyboard, cfg_prefix, 200);
strcat(keyboard, "/share/cgterm/");
strncat(keyboard, value, 40);
}
#endif
cfg_keyboard = keyboard;
} else if (strcmp(key, "logfile") == 0) {
strcpy(logfile, value);
cfg_logfile = logfile;
} else if (strcmp(key, "xferdir") == 0) {
cfg_change_dir(cfg_xferdir, value);
} else if (strcmp(key, "localecho") == 0) {
if (strcmp("yes", value) == 0) {
cfg_localecho = 1;
} else if (strcmp("no", value) == 0) {
cfg_localecho = 0;
} else {
printf("Invalid localecho value in %s: %s\n", configfile, value);
fclose(cfg);
return(-1);
}
} else if (strcmp(key, "fullscreen") == 0) {
if (strcmp("yes", value) == 0) {
cfg_fullscreen = 1;
} else if (strcmp("no", value) == 0) {
cfg_fullscreen = 0;
} else {
printf("Invalid fullscreen value in %s: %s\n", configfile, value);
fclose(cfg);
return(-1);
}
} else if (strcmp(key, "sound") == 0) {
if (strcmp("yes", value) == 0) {
cfg_sound = 1;
} else if (strcmp("no", value) == 0) {
cfg_sound = 0;
} else {
printf("Invalid sound value in %s: %s\n", configfile, value);
fclose(cfg);
return(-1);
}
} else if (strcmp(key, "zoom") == 0) {
cfg_zoom = strtol(value, (char **)NULL, 10);
if (cfg_zoom <= 0 || cfg_zoom > 8) {
printf("Invalid zoom value in %s: %s\n", configfile, value);
fclose(cfg);
return(-1);
}
} else if (strcmp(key, "reconnect") == 0) {
cfg_reconnect = strtol(value, (char **)NULL, 10);
if (cfg_reconnect <= 0 || cfg_reconnect > 10000) {
printf("Invalid reconnect delay in %s: %s\n", configfile, value);
fclose(cfg);
return(-1);
}
} else if (strcmp(key, "columns") == 0) {
cfg_columns = strtol(value, (char **)NULL, 10);
if (cfg_columns != 40 && cfg_columns != 80) {
printf("Invalid number of columns in %s: %s\n", configfile, value);
fclose(cfg);
return(-1);
}
} else if (strcmp(key, "bookmark") == 0) {
if (addbookmark(linebuf) == 0) {
printf("Syntax error in %s line %d\n", configfile, line + 1);
fclose(cfg);
return(-1);
}
} else {
printf("Unknown config key in %s line %d\n", configfile, line + 1);
fclose(cfg);
return(-1);
}
} else {
printf("Error in %s line %d\n", configfile, line + 1);
fclose(cfg);
return(-1);
}
} else {
if (!sscanf(linebuf, "%s", linebuf)) {
printf("Syntax error in %s line %d\n", configfile, line + 1);
fclose(cfg);
return(-1);
}
}
++line;
}
if (ferror(cfg)) {
printf("read error\n");
fclose(cfg);
return(-1);
}
fclose(cfg);
return(0);
}
void cfg_writeconfig(char **data, char *configfile) {
FILE *cfg;
if ((cfg = fopen(configfile, "w")) == NULL) {
return;
}
while (*data) {
fprintf(cfg, "%s\n", *data++);
}
fclose(cfg);
}