Mercurial > hg
comparison mcabber/src/parsecfg.c @ 333:db5bebe96c89
New config. file parsing
The "set", "alias" and "bind" keywords can now be used in config files.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Mon, 18 Jul 2005 20:02:38 +0100 |
parents | 12f919be3da5 |
children |
comparison
equal
deleted
inserted
replaced
332:a1901741890e | 333:db5bebe96c89 |
---|---|
4 #include <ctype.h> | 4 #include <ctype.h> |
5 #include <string.h> | 5 #include <string.h> |
6 #include <glib.h> | 6 #include <glib.h> |
7 | 7 |
8 #include "settings.h" | 8 #include "settings.h" |
9 #include "commands.h" | |
9 #include "utils.h" | 10 #include "utils.h" |
11 #include "screen.h" | |
10 | 12 |
11 // cfg_file(filename) | 13 // cfg_file(filename) |
12 // Read and parse config file "filename". If filename is NULL, | 14 // Read and parse config file "filename". If filename is NULL, |
13 // try to open the configuration file at the default locations. | 15 // try to open the configuration file at the default locations. |
14 // | 16 // |
16 int cfg_file(char *filename) | 18 int cfg_file(char *filename) |
17 { | 19 { |
18 FILE *fp; | 20 FILE *fp; |
19 char *buf; | 21 char *buf; |
20 char *line; | 22 char *line; |
21 char *value; | 23 unsigned int ln = 0; |
24 int err = 0; | |
22 | 25 |
23 if (!filename) { | 26 if (!filename) { |
24 // Use default config file locations | 27 // Use default config file locations |
25 char *home = getenv("HOME"); | 28 char *home = getenv("HOME"); |
26 if (!home) { | 29 if (!home) { |
27 ut_WriteLog("Can't find home dir!\n"); | 30 ut_WriteLog("Can't find home dir!\n"); |
28 exit(EXIT_FAILURE); | 31 fprintf(stderr, "Can't find home dir!\n"); |
32 return -1; | |
29 } | 33 } |
30 filename = g_new(char, strlen(home)+24); | 34 filename = g_new(char, strlen(home)+24); |
31 sprintf(filename, "%s/.mcabber/mcabberrc", home); | 35 sprintf(filename, "%s/.mcabber/mcabberrc", home); |
32 if ((fp = fopen(filename, "r")) == NULL) { | 36 if ((fp = fopen(filename, "r")) == NULL) { |
33 // 2nd try... | 37 // 2nd try... |
34 sprintf(filename, "%s/.mcabberrc", home); | 38 sprintf(filename, "%s/.mcabberrc", home); |
35 if ((fp = fopen(filename, "r")) == NULL) { | 39 if ((fp = fopen(filename, "r")) == NULL) { |
36 fprintf(stderr, "Cannot open config file!\n"); | 40 fprintf(stderr, "Cannot open config file!\n"); |
37 exit(EXIT_FAILURE); | 41 return -1; |
38 } | 42 } |
39 } | 43 } |
40 g_free(filename); | 44 g_free(filename); |
41 } | 45 } |
42 else if ((fp = fopen(filename, "r")) == NULL) { | 46 else if ((fp = fopen(filename, "r")) == NULL) { |
43 perror("fopen (parsecfg.c:46)"); | 47 perror("fopen (parsecfg.c:46)"); |
44 exit(EXIT_FAILURE); | 48 return -1; |
45 } | 49 } |
46 | 50 |
47 buf = g_new(char, 256); | 51 // This should be fully rewritten... |
52 buf = g_new(char, 512); | |
48 | 53 |
49 while (fgets(buf, 256, fp) != NULL) { | 54 while (fgets(buf+1, 511, fp) != NULL) { |
50 line = buf; | 55 line = buf+1; |
56 ln++; | |
51 | 57 |
52 while (isspace((int) *line)) | 58 while (isspace(*line)) |
53 line++; | 59 line++; |
54 | 60 |
55 while ((strlen(line) > 0) | 61 while ((strlen(line) > 0) && isspace((int) line[strlen(line) - 1])) |
56 && isspace((int) line[strlen(line) - 1])) | |
57 line[strlen(line) - 1] = '\0'; | 62 line[strlen(line) - 1] = '\0'; |
58 | 63 |
59 if ((*line == '\n') || (*line == '\0') || (*line == '#')) | 64 if ((*line == '\n') || (*line == '\0') || (*line == '#')) |
60 continue; | 65 continue; |
61 | 66 |
62 if ((strchr(line, '=') != NULL)) { | 67 if ((strchr(line, '=') != NULL)) { |
63 value = strchr(line, '='); | 68 while ((strlen(line) > 0) && isspace(line[strlen(line) - 1])) |
64 *value = '\0'; | |
65 value++; | |
66 | |
67 while (isspace((int) *value)) | |
68 value++; | |
69 | |
70 while ((strlen(line) > 0) | |
71 && isspace((int) line[strlen(line) - 1])) | |
72 line[strlen(line) - 1] = '\0'; | 69 line[strlen(line) - 1] = '\0'; |
73 | 70 |
74 settings_set(SETTINGS_TYPE_OPTION, line, value); | 71 if (strncmp(line, "set ", 4) && |
75 continue; | 72 strncmp(line, "bind ", 5) && |
73 strncmp(line, "alias ", 6)) { | |
74 scr_LogPrint("Error in configuration file (l. %d)", ln); | |
75 err++; | |
76 continue; | |
77 } | |
78 *(--line) = '/'; | |
79 process_command(line); | |
80 } else { | |
81 scr_LogPrint("Error in configuration file (l. %d)", ln); | |
82 err++; | |
76 } | 83 } |
77 fprintf(stderr, "CFG: orphaned line \"%s\"\n", line); | |
78 } | 84 } |
79 g_free(buf); | 85 g_free(buf); |
80 return 1; | 86 fclose(fp); |
87 return err; | |
81 } | 88 } |