Mercurial > hg
comparison mcabber/src/settings.c @ 341:dea407d53fe6
Improve configuration file reading
* Move cfg_file() to settings.c, cfg_read_file()
* Optimize the function
* Remove parsecfg.[ch]
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Tue, 19 Jul 2005 17:54:03 +0100 |
parents | 3a25be278864 |
children | d8f147d6e872 |
comparison
equal
deleted
inserted
replaced
340:2362167ac0f3 | 341:dea407d53fe6 |
---|---|
22 #include <strings.h> | 22 #include <strings.h> |
23 #include <stdlib.h> | 23 #include <stdlib.h> |
24 #include <ctype.h> | 24 #include <ctype.h> |
25 | 25 |
26 #include "settings.h" | 26 #include "settings.h" |
27 #include "commands.h" | |
28 #include "utils.h" | |
29 #include "screen.h" | |
27 | 30 |
28 static GSList *option; | 31 static GSList *option; |
29 static GSList *alias; | 32 static GSList *alias; |
30 static GSList *binding; | 33 static GSList *binding; |
31 | 34 |
56 | 59 |
57 return ptr; | 60 return ptr; |
58 } | 61 } |
59 | 62 |
60 /* -- */ | 63 /* -- */ |
64 | |
65 // cfg_read_file(filename) | |
66 // Read and parse config file "filename". If filename is NULL, | |
67 // try to open the configuration file at the default locations. | |
68 // | |
69 int cfg_read_file(char *filename) | |
70 { | |
71 FILE *fp; | |
72 char *buf; | |
73 char *line, *eol; | |
74 unsigned int ln = 0; | |
75 int err = 0; | |
76 | |
77 if (!filename) { | |
78 // Use default config file locations | |
79 char *home = getenv("HOME"); | |
80 if (!home) { | |
81 ut_WriteLog("Can't find home dir!\n"); | |
82 fprintf(stderr, "Can't find home dir!\n"); | |
83 return -1; | |
84 } | |
85 filename = g_new(char, strlen(home)+24); | |
86 sprintf(filename, "%s/.mcabber/mcabberrc", home); | |
87 if ((fp = fopen(filename, "r")) == NULL) { | |
88 // 2nd try... | |
89 sprintf(filename, "%s/.mcabberrc", home); | |
90 if ((fp = fopen(filename, "r")) == NULL) { | |
91 fprintf(stderr, "Cannot open config file!\n"); | |
92 return -1; | |
93 } | |
94 } | |
95 g_free(filename); | |
96 } | |
97 else if ((fp = fopen(filename, "r")) == NULL) { | |
98 perror("fopen (cfg_file())"); | |
99 return -1; | |
100 } | |
101 | |
102 buf = g_new(char, 512); | |
103 | |
104 while (fgets(buf+1, 511, fp) != NULL) { | |
105 // The first char is reserved to add a '/', to make a command line | |
106 line = buf+1; | |
107 ln++; | |
108 | |
109 // Strip leading spaces | |
110 while (isspace(*line)) | |
111 line++; | |
112 | |
113 // Make eol point to the last char of the line | |
114 for (eol = line ; *eol ; eol++) | |
115 ; | |
116 if (eol > line) | |
117 eol--; | |
118 | |
119 // Strip trailing spaces | |
120 while (eol > line && isspace(*eol)) | |
121 *eol-- = 0; | |
122 | |
123 // Ignore empty lines and comments | |
124 if ((*line == '\n') || (*line == '\0') || (*line == '#')) | |
125 continue; | |
126 | |
127 if ((strchr(line, '=') != NULL)) { | |
128 // Only accept the set, alias and bind commands | |
129 if (strncmp(line, "set ", 4) && | |
130 strncmp(line, "bind ", 5) && | |
131 strncmp(line, "alias ", 6)) { | |
132 scr_LogPrint("Error in configuration file (l. %d): bad command", ln); | |
133 err++; | |
134 continue; | |
135 } | |
136 *(--line) = '/'; // Set the leading '/' to build a command line | |
137 process_command(line); // Process the command | |
138 } else { | |
139 scr_LogPrint("Error in configuration file (l. %d): no assignment", ln); | |
140 err++; | |
141 } | |
142 } | |
143 g_free(buf); | |
144 fclose(fp); | |
145 return err; | |
146 } | |
61 | 147 |
62 // parse_assigment(assignment, pkey, pval) | 148 // parse_assigment(assignment, pkey, pval) |
63 // Read assignment and split it to key, value | 149 // Read assignment and split it to key, value |
64 // | 150 // |
65 // If this is an assignment, the function will return TRUE and | 151 // If this is an assignment, the function will return TRUE and |