1 /* some variables and functions common to all platforms and languages */
2
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <ctype.h>
8
9 #include "auto.h"
10 #include "common.h"
11 #include "lang.h"
12 #include "os.h"
13 #include "rlsubst.h"
14
15
16 char **cfg;
17
18
19 // numXXX are defined in common.h
20
21 char *nodelists[numNodelists] =
22 {
23 "nodelist", "points24"
24 };
25
ask(char * prompt,char * defaultValue)26 char *ask(char *prompt, char *defaultValue)
27 {
28 char *res;
29 // char inputBuf[1024];
30
31 // printf(prompt, defaultValue);
32 // fgets(inputBuf, 1024, stdin);
33
34 // while ((strlen(inputBuf) > 0) && (inputBuf[strlen(inputBuf) - 1] == '\n'))
35 // inputBuf[strlen(inputBuf) - 1] = 0;
36
37 // if (strlen(inputBuf) == 0) return strdup(defaultValue);
38
39 // return strdup(inputBuf);
40
41 add_history(defaultValue);
42 res = readline(prompt);
43 add_history(res);
44
45 return res;
46 }
47
48 // 0 if everything's allright
askAllright()49 int askAllright()
50 {
51 int c = ' ';
52
53 while ((c != yesKey) && (c != noKey))
54 {
55 printf(allrightText, yesKey, noKey);
56 c = fgetc(stdin);
57 printf("\n");
58 }
59
60 // get return
61 fgetc(stdin);
62
63 if (c == yesKey) return 0;
64 return 1;
65 }
66
67 // returns 0 on success
setTemplateVars()68 int setTemplateVars()
69 {
70 int rc;
71
72 rc = 0;
73
74 rc += setVar("amtnum", cfg[amtNumIdx]);
75 rc += setVar("bindir", cfg[binDirIdx]);
76 rc += setVar("cfgdir", cfg[cfgDirIdx]);
77 rc += setVar("datanum", cfg[dataNumIdx]);
78 rc += setVar("debug", cfg[debugIdx]);
79 rc += setVar("dirsep", dirSepS);
80 rc += setVar("fidoname", cfg[fidoNameIdx]);
81 rc += setVar("groupname", cfg[groupNameIdx]);
82 rc += setVar("homedir", cfg[homeDirIdx]);
83 rc += setVar("htmldir", cfg[htmlDirIdx]);
84 rc += setVar("inbound", cfg[inboundIdx]);
85 rc += setVar("incdir", cfg[incDirIdx]);
86 rc += setVar("infodir", cfg[infoDirIdx]);
87 rc += setVar("internatnum", cfg[internatNumIdx]);
88 rc += setVar("internatprefix", cfg[internatPrefixIdx]);
89 rc += setVar("isdndev", cfg[isdnDevIdx]);
90 rc += setVar("langdir", langDir);
91 rc += setVar("libcversion", cfg[libcVersionIdx]);
92 rc += setVar("libdir", cfg[libDirIdx]);
93 rc += setVar("localinbound", cfg[localInboundIdx]);
94 rc += setVar("localnum", cfg[localNumIdx]);
95 rc += setVar("localprefix", cfg[localPrefixIdx]);
96 rc += setVar("location", cfg[locationIdx]);
97 rc += setVar("logdir", cfg[logDirIdx]);
98 rc += setVar("mandir", cfg[manDirIdx]);
99 rc += setVar("modembaud", cfg[modemBaudIdx]);
100 rc += setVar("modemdev", cfg[modemDevIdx]);
101 rc += setVar("msgbasedir", cfg[msgbaseDirIdx]);
102 rc += setVar("netmaildir", cfg[netmailDirIdx]);
103 rc += setVar("nodelistdir", cfg[nodelistDirIdx]);
104 rc += setVar("outbound", cfg[outboundIdx]);
105 rc += setVar("packer", cfg[packerIdx]);
106 rc += setVar("pointnr", cfg[pointNrIdx]);
107 rc += setVar("protinbound", cfg[protInboundIdx]);
108 rc += setVar("scriptdir", cfg[scriptDirIdx]);
109 rc += setVar("sysopname", cfg[sysOpNameIdx]);
110 rc += setVar("tempinbound", cfg[tempInboundIdx]);
111 rc += setVar("tempoutbound", cfg[tempOutboundIdx]);
112 rc += setVar("uplinkaddr", cfg[uplinkAddrIdx]);
113 rc += setVar("uplinkname", cfg[uplinkNameIdx]);
114 rc += setVar("uplinkpwd", cfg[uplinkPwdIdx]);
115 rc += setVar("username", cfg[userNameIdx]);
116 rc += setVar("users", cfg[usersIdx]);
117 rc += setVar("voicenum", cfg[voiceNumIdx]);
118 rc += setVar("workdir", cfg[workDirIdx]);
119
120 return rc;
121 }
122
123 // returns 0 if successfull
processTemplate(char * templName,char * outName)124 int processTemplate(char *templName, char *outName)
125 {
126 FILE *templ;
127 FILE *out;
128 char lineIn[1024], lineOut[1024];
129 char *lineInTmp, *lineOutTmp;
130 int rc;
131 char *cmdLine;
132 char *tmpName;
133
134 setTemplateVars();
135
136 cmdLine = malloc(strlen(templName) + strlen(osDir) + strlen(osTmpDir) + 18);
137 sprintf(cmdLine, "%s" dirSepS "lwpp %s > %slwpp.tmp", osDir, templName,
138 osTmpDir);
139 rc = system(cmdLine);
140 free(cmdLine);
141 if (rc != 0)
142 {
143 printf("lwpp returned error code #%d!\n", rc);
144
145 return rc;
146 }
147
148 tmpName = malloc(strlen(osTmpDir)+9);
149 sprintf(tmpName, "%slwpp.tmp", osTmpDir);
150 templ = fopen(tmpName, "r");
151
152 if (templ == NULL)
153 {
154 printf("Could not open temporary file '%s'!\n", tmpName);
155 free(tmpName);
156
157 return 1;
158 }
159
160 out = fopen(outName, "w");
161
162 if (out == NULL)
163 {
164 printf("Cannot write '%s'!\n", outName);
165 fclose(templ);
166
167 return 2;
168 }
169
170 while (feof(templ) == 0)
171 {
172 *lineIn = 0;
173 lineInTmp = lineIn;
174 lineOutTmp = lineOut;
175 fgets(lineIn, 1024, templ);
176
177 while (*lineInTmp != 0)
178 {
179 if (*lineInTmp == '\\')
180 {
181 lineInTmp++;
182 *lineOutTmp = *lineInTmp;
183 lineInTmp++;
184 lineOutTmp++;
185 }
186 else if (*lineInTmp == '$')
187 {
188 int pos;
189 char *varName;
190 char *varContent;
191 char *varContTmp;
192 int i;
193
194 lineInTmp++;
195 if (strchr(lineInTmp, '$') != NULL)
196 pos = strchr(lineInTmp, '$') - lineInTmp;
197 else pos = 0;
198 varName = malloc(pos+1);
199 for (i = 0; i < pos; i++) varName[i] = tolower(lineInTmp[i]);
200 varName[pos] = 0;
201 varContent = getVar(varName);
202 free(varName);
203
204 for (varContTmp = varContent ; *varContTmp != 0; varContTmp++)
205 {
206 *lineOutTmp = *varContTmp;
207 lineOutTmp++;
208 }
209
210 lineInTmp = lineInTmp + pos + 1;
211 }
212 else
213 {
214 *lineOutTmp = *lineInTmp;
215 lineInTmp++;
216 lineOutTmp++;
217 }
218 }
219
220 *lineOutTmp = 0;
221
222 fputs(lineOut, out);
223 }
224
225 fclose(templ);
226 fclose(out);
227 unlink(tmpName);
228 free(tmpName);
229
230 return 0;
231 }
232
233 // returns 0 on success
saveConfig()234 int saveConfig()
235 {
236 FILE *f;
237 unsigned int i;
238
239 f = fopen(osDir dirSepS "huskypnt.cfg", "w");
240 if (!f)
241 {
242 printf("Could not open " osDir dirSepS "huskypnt.cfg for writing!\n");
243
244 return -1;
245 }
246
247 for (i = 0; i < numIdx; i++)
248 {
249 if (cfg[i]) fprintf(f, "%s\n", cfg[i]);
250 else fprintf(f, "\n");
251 }
252
253 fclose(f);
254 setMode(osDir dirSepS "huskypnt.cfg", 384); // Octal 600
255
256 return 0;
257 }
258
259 // returns 0 on success
loadConfig()260 int loadConfig()
261 {
262 FILE *f;
263 unsigned int i;
264 char **newCfg;
265 char *line, *lineTmp;
266 int rc;
267
268 f = fopen(osDir dirSepS "huskypnt.cfg", "r");
269 if (!f) return -1;
270
271 newCfg = calloc(numIdx, sizeof(char *));
272 line = malloc(1024);
273
274 // try to load the file
275 rc = 0;
276 for (i = 0; i < numIdx; i++)
277 {
278 *line = 0;
279
280 fgets(line, 1024, f);
281 if (! *line)
282 {
283 rc = i + 1;
284 break;
285 }
286
287 // strip trailing CR/LF
288 lineTmp = line + strlen(line) - 1;
289 while ((lineTmp > line) && ((*lineTmp == 10) || (*lineTmp == 13)))
290 lineTmp--;
291 if ((*lineTmp == 10) || (*lineTmp == 13)) newCfg[i] = strdup("");
292 else
293 {
294 *(lineTmp + 1) = 0;
295 newCfg[i] = strdup(line);
296 }
297 }
298
299 if (rc)
300 {
301 // could not load the file, free already allocated entries
302 for (i = 0; i < (rc - 1); i++) free(newCfg[i]);
303 free(newCfg);
304 }
305 else
306 {
307 // load succeeded, exchange config
308 free(cfg);
309 cfg = newCfg;
310 }
311
312 free(line);
313
314 return rc;
315 }
316
317
318