]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/install+setup/libsmooth/main.c
Installer:
[people/pmueller/ipfire-2.x.git] / src / install+setup / libsmooth / main.c
1 /* SmoothWall libsmooth.
2 *
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
5 *
6 * (c) Lawrence Manning, 2001
7 * Contains library functions.
8 *
9 * $Id: main.c,v 1.6.2.9 2005/12/09 22:31:41 franck78 Exp $
10 *
11 */
12
13 #include "libsmooth.h"
14
15 extern FILE *flog;
16 extern char *mylog;
17
18 extern char **ctr;
19
20 /* reboot(). reboots. */
21 void reboot(void)
22 {
23 mysystem("/etc/halt");
24 }
25
26 /* stripnl(). Replaces \n with \0 */
27 void stripnl(char *s)
28 {
29 char *t = strchr(s, '\n');
30 if (t) *t = '\0';
31 }
32
33 /* Little wrapper. */
34 int mysystem(char *command)
35 {
36 char mycommand[STRING_SIZE];
37
38 snprintf(mycommand, STRING_SIZE, "%s >>%s 2>>%s", command, mylog, mylog);
39 fprintf(flog, "Running command: %s\n", command);
40 return system(mycommand);
41 }
42
43 void errorbox(char *message)
44 {
45 newtWinMessage(ctr[TR_ERROR], ctr[TR_OK], message);
46 }
47
48 void statuswindow(int width, int height, char *title, char *text, ...)
49 {
50 newtComponent t, f;
51 char *buf = NULL;
52 int size = 0;
53 int i = 0;
54 va_list args;
55
56 va_start(args, text);
57
58 do {
59 size += 1000;
60 if (buf) free(buf);
61 buf = malloc(size);
62 i = vsnprintf(buf, size, text, args);
63 } while (i == size);
64
65 va_end(args);
66
67 newtCenteredWindow(width, height, title);
68
69 t = newtTextbox(1, 1, width - 2, height - 2, NEWT_TEXTBOX_WRAP);
70 newtTextboxSetText(t, buf);
71 f = newtForm(NULL, NULL, 0);
72
73 free(buf);
74
75 newtFormAddComponent(f, t);
76
77 newtDrawForm(f);
78 newtRefresh();
79 newtFormDestroy(f);
80 }
81
82 int runcommandwithstatus(char *command, char *message)
83 {
84 int rc;
85 char title[STRING_SIZE];
86
87 sprintf (title, "%s v%s - %s", NAME, VERSION, SLOGAN);
88 statuswindow(60, 4, title, message);
89 rc = mysystem(command);
90 newtPopWindow();
91
92 return rc;
93 }
94
95 int runhiddencommandwithstatus(char *command, char *message)
96 {
97 int rc;
98 char title[STRING_SIZE];
99 char mycommand[STRING_SIZE];
100
101 sprintf (title, "%s v%s - %s", NAME, VERSION, SLOGAN);
102 statuswindow(60, 4, title, message);
103 snprintf(mycommand, STRING_SIZE, "%s >>%s 2>>%s", command, mylog, mylog);
104 fprintf(flog, "Running command: ***** HIDDEN *****\n");
105 rc = system(mycommand);
106 newtPopWindow();
107
108 return rc;
109 }
110
111 /* This one borrowed from redhat installer. */
112 int runcommandwithprogress(int width, int height, char *title, char *command,
113 int lines, char *text, ...)
114 {
115 newtComponent t, f, s;
116 char *buf = NULL;
117 int size = 0;
118 int i = 0;
119 va_list args;
120 int rc = 0;
121 FILE *p;
122 char buffer[STRING_SIZE];
123 int progress = 0;
124 char mycommand[STRING_SIZE];
125
126 va_start(args, text);
127
128 do {
129 size += 1000;
130 if (buf) free(buf);
131 buf = malloc(size);
132 i = vsnprintf(buf, size, text, args);
133 } while (i == size);
134
135 va_end(args);
136
137 newtCenteredWindow(width, height, title);
138
139 t = newtTextbox(1, 1, width - 2, height - 2, NEWT_TEXTBOX_WRAP);
140 newtTextboxSetText(t, buf);
141 f = newtForm(NULL, NULL, 0);
142
143 free(buf);
144
145 newtFormAddComponent(f, t);
146
147 s = newtScale(1, 3, width - 2, lines);
148 newtScaleSet(s, progress);
149
150 newtFormAddComponent(f, s);
151
152 newtDrawForm(f);
153 newtRefresh();
154
155 snprintf(mycommand, STRING_SIZE, "%s 2>>%s", command, mylog);
156 fprintf(flog, "Running command: %s\n", command);
157
158 if (!(p = popen(command, "r")))
159 {
160 rc = 1;
161 goto EXIT;
162 }
163 setvbuf(p, NULL, _IOLBF, 255);
164
165 while (fgets(buffer, STRING_SIZE, p))
166 {
167 newtScaleSet(s, ++progress);
168 newtRefresh();
169 fprintf(flog, "%s", buffer);
170 }
171
172 rc = pclose(p);
173
174 EXIT:
175 newtFormDestroy(f);
176 newtPopWindow();
177
178 return rc;
179 }
180
181 int checkformodule(char *module)
182 {
183 FILE *file;
184 char buffer[STRING_SIZE];
185 int result = 0;
186
187 if (!(file = fopen("/proc/modules", "r")))
188 {
189 fprintf(flog, "Unable to open /proc/modules in checkformodule()\n");
190 return 0;
191 }
192
193 while (fgets(buffer, STRING_SIZE, file))
194 {
195 if (strncmp(buffer, module, strlen(module)) == 0)
196 {
197 if (buffer[strlen(module)] == ' ')
198 {
199 result = 1;
200 goto EXIT;
201 }
202 }
203 }
204
205 EXIT:
206 fclose(file);
207
208 return result;
209 }
210
211 int _replace_string(char string[], char *from, char *to)
212 {
213 int fromlen = strlen(from);
214 int tolen = strlen(to);
215 char *start, *p1, *p2;
216 for(start = string; *start != '\0'; start++)
217 {
218 p1 = from;
219 p2 = start;
220 while(*p1 != '\0')
221 {
222 if(*p1 != *p2)
223 break;
224 p1++;
225 p2++;
226 }
227 if(*p1 == '\0')
228 {
229 if(fromlen != tolen)
230 {
231 memmove(start + tolen, start + fromlen,
232 strlen(start + fromlen) + 1);
233 }
234 for(p1 = to; *p1 != '\0'; p1++)
235 *start++ = *p1;
236 return 1;
237 }
238 }
239 return 0;
240 }
241
242 int replace(char filename1[], char *from, char *to)
243 {
244 FILE *file1, *file2;
245 char filename2[1000];
246 char temp[1000];
247 int ret = 0;
248
249 /* Open the source and destination files */
250 strcpy (filename2, filename1);
251 strcat (filename2, ".new");
252 if (!(file1 = fopen (filename1, "r"))) return 1;
253 if (!(file2 = fopen (filename2, "w"))) {
254 fclose(file1);
255 return -1;
256 }
257
258 /* Start reading in lines */
259 while (fgets (temp, 1000, file1) != NULL) {
260
261 if (strlen(to) > 0) {
262 /* Replace string */
263 ret = _replace_string (temp, from, to);
264
265 /* Write string to new file */
266 fputs(temp, file2);
267 } else {
268 /* Remove string when to is NULL */
269 if (!strstr(temp, from))
270 fputs(temp, file2);
271 }
272 }
273
274 /* Close source and destination */
275 fclose (file1);
276 fclose (file2);
277
278 /* Move the file */
279 rename (filename2, filename1);
280
281 return (ret);
282 }
283
284 /* Include enabled languages */
285 #ifdef LANG_EN_ONLY
286 #include "lang_en.c"
287 #elifdef LANG_ALL
288 #include "lang_bz.c"
289 #include "lang_cs.c"
290 #include "lang_da.c"
291 #include "lang_de.c"
292 #include "lang_en.c"
293 #include "lang_es.c"
294 #include "lang_fi.c"
295 #include "lang_fr.c"
296 #include "lang_el.c"
297 #include "lang_it.c"
298 #include "lang_hu.c"
299 #include "lang_la.c"
300 #include "lang_nl.c"
301 #include "lang_no.c"
302 #include "lang_pl.c"
303 #include "lang_pt.c"
304 #include "lang_sk.c"
305 #include "lang_so.c"
306 #include "lang_sv.c"
307 #include "lang_tr.c"
308 #include "lang_vi.c"
309 #else
310 #include "lang_de.c"
311 #include "lang_en.c"
312
313 #endif
314 // returns a pointer to the actual running version number of IPCop.
315 // Successive updates increase effective version but not VERSION !
316 char g_title[STRING_SIZE] = "";
317 char* get_version(void) {
318 FILE *f_title;
319 if ((f_title = fopen ("/etc/issue", "r"))) {
320 fgets (g_title, STRING_SIZE, f_title);
321 fclose (f_title);
322 if (g_title[strlen(g_title) - 1] == '\n') g_title[strlen(g_title) - 1] = '\0';
323 } else {
324 sprintf (g_title, "%s v%s - %s", NAME, VERSION, SLOGAN);
325 }
326 return g_title;
327 }