Mercurial > ~mikael > mcabber > hg
annotate 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 |
rev | line source |
---|---|
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 |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
188
diff
changeset
|
8 #include "settings.h" |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
9 #include "commands.h" |
170 | 10 #include "utils.h" |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
11 #include "screen.h" |
24 | 12 |
297
9bdfef4f4735
Add comment for the cfg_file() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
13 // cfg_file(filename) |
9bdfef4f4735
Add comment for the cfg_file() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
14 // Read and parse config file "filename". If filename is NULL, |
9bdfef4f4735
Add comment for the cfg_file() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
15 // try to open the configuration file at the default locations. |
9bdfef4f4735
Add comment for the cfg_file() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
16 // |
9bdfef4f4735
Add comment for the cfg_file() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
17 // This function comes from Cabber, and has been slightly modified. |
24 | 18 int cfg_file(char *filename) |
19 { | |
20 FILE *fp; | |
21 char *buf; | |
22 char *line; | |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
23 unsigned int ln = 0; |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
24 int err = 0; |
24 | 25 |
169 | 26 if (!filename) { |
27 // Use default config file locations | |
28 char *home = getenv("HOME"); | |
29 if (!home) { | |
30 ut_WriteLog("Can't find home dir!\n"); | |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
31 fprintf(stderr, "Can't find home dir!\n"); |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
32 return -1; |
169 | 33 } |
34 filename = g_new(char, strlen(home)+24); | |
35 sprintf(filename, "%s/.mcabber/mcabberrc", home); | |
36 if ((fp = fopen(filename, "r")) == NULL) { | |
37 // 2nd try... | |
38 sprintf(filename, "%s/.mcabberrc", home); | |
39 if ((fp = fopen(filename, "r")) == NULL) { | |
40 fprintf(stderr, "Cannot open config file!\n"); | |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
41 return -1; |
169 | 42 } |
43 } | |
44 g_free(filename); | |
45 } | |
46 else if ((fp = fopen(filename, "r")) == NULL) { | |
24 | 47 perror("fopen (parsecfg.c:46)"); |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
48 return -1; |
24 | 49 } |
50 | |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
51 // This should be fully rewritten... |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
52 buf = g_new(char, 512); |
169 | 53 |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
54 while (fgets(buf+1, 511, fp) != NULL) { |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
55 line = buf+1; |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
56 ln++; |
24 | 57 |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
58 while (isspace(*line)) |
24 | 59 line++; |
60 | |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
61 while ((strlen(line) > 0) && isspace((int) line[strlen(line) - 1])) |
24 | 62 line[strlen(line) - 1] = '\0'; |
63 | |
64 if ((*line == '\n') || (*line == '\0') || (*line == '#')) | |
65 continue; | |
66 | |
67 if ((strchr(line, '=') != NULL)) { | |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
68 while ((strlen(line) > 0) && isspace(line[strlen(line) - 1])) |
24 | 69 line[strlen(line) - 1] = '\0'; |
70 | |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
71 if (strncmp(line, "set ", 4) && |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
72 strncmp(line, "bind ", 5) && |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
73 strncmp(line, "alias ", 6)) { |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
74 scr_LogPrint("Error in configuration file (l. %d)", ln); |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
75 err++; |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
76 continue; |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
77 } |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
78 *(--line) = '/'; |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
79 process_command(line); |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
80 } else { |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
81 scr_LogPrint("Error in configuration file (l. %d)", ln); |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
82 err++; |
24 | 83 } |
84 } | |
169 | 85 g_free(buf); |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
86 fclose(fp); |
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
87 return err; |
24 | 88 } |