]> git.ipfire.org Git - ipfire-2.x.git/blob - src/install+setup/libsmooth/main.c
Merge commit 'origin/master' into next
[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 int scrollmsgbox(int width, int height, char *title, char *text, ...)
49 {
50 int rc = 0;
51 newtComponent t, f, b, c;
52 char *buf = NULL;
53 char checkbox;
54 int size = 0;
55 int i = 0;
56 va_list args;
57
58 va_start(args, text);
59
60 do {
61 size += 40000;
62 if (buf) free(buf);
63 buf = malloc(size);
64 i = vsnprintf(buf, size, text, args);
65 } while (i == size);
66
67 va_end(args);
68
69 newtCenteredWindow(width, height, title);
70
71 b = newtCompactButton(width - 15 ,height - 2, ctr[TR_OK]);
72 c = newtCheckbox(3, height - 2, ctr[TR_LICENSE_ACCEPT], ' ', " *", &checkbox);
73
74 t = newtTextbox(1, 1, width - 2, height - 4, NEWT_TEXTBOX_WRAP+NEWT_TEXTBOX_SCROLL);
75 newtTextboxSetText(t, buf);
76
77 f = newtForm(NULL, NULL, 0);
78 free(buf);
79
80 newtFormAddComponent(f, c);
81 newtFormAddComponent(f, b);
82 newtFormAddComponent(f, t);
83
84 newtRunForm(f);
85 if (checkbox=='*') rc=1;
86 newtFormDestroy(f);
87 return rc;
88 }
89
90 int disclaimerbox(char *message)
91 {
92 int rc;
93 char title[STRING_SIZE];
94
95 sprintf (title, "%s v%s - %s", NAME, VERSION, SLOGAN);
96 rc = scrollmsgbox(75, 20, title, message);
97 newtPopWindow();
98
99 return rc;
100 }
101
102
103 void statuswindow(int width, int height, char *title, char *text, ...)
104 {
105 newtComponent t, f;
106 char *buf = NULL;
107 int size = 0;
108 int i = 0;
109 va_list args;
110
111 va_start(args, text);
112
113 do {
114 size += 1000;
115 if (buf) free(buf);
116 buf = malloc(size);
117 i = vsnprintf(buf, size, text, args);
118 } while (i == size);
119
120 va_end(args);
121
122 newtCenteredWindow(width, height, title);
123
124 t = newtTextbox(1, 1, width - 2, height - 2, NEWT_TEXTBOX_WRAP);
125 newtTextboxSetText(t, buf);
126 f = newtForm(NULL, NULL, 0);
127
128 free(buf);
129
130 newtFormAddComponent(f, t);
131
132 newtDrawForm(f);
133 newtRefresh();
134 newtFormDestroy(f);
135 }
136
137 int runcommandwithstatus(char *command, char *message)
138 {
139 int rc;
140 char title[STRING_SIZE];
141
142 sprintf (title, "%s v%s - %s", NAME, VERSION, SLOGAN);
143 statuswindow(60, 4, title, message);
144 rc = mysystem(command);
145 newtPopWindow();
146
147 return rc;
148 }
149
150 int runhiddencommandwithstatus(char *command, char *message)
151 {
152 int rc;
153 char title[STRING_SIZE];
154 char mycommand[STRING_SIZE];
155
156 sprintf (title, "%s v%s - %s", NAME, VERSION, SLOGAN);
157 statuswindow(60, 4, title, message);
158 snprintf(mycommand, STRING_SIZE, "%s >>%s 2>>%s", command, mylog, mylog);
159 fprintf(flog, "Running command: ***** HIDDEN *****\n");
160 rc = system(mycommand);
161 newtPopWindow();
162
163 return rc;
164 }
165
166 /* This one borrowed from redhat installer. */
167 int runcommandwithprogress(int width, int height, char *title, char *command,
168 int lines, char *text, ...)
169 {
170 newtComponent t, f, s;
171 char *buf = NULL;
172 int size = 0;
173 int i = 0;
174 va_list args;
175 int rc = 0;
176 FILE *p;
177 char buffer[STRING_SIZE];
178 int progress = 0;
179 char mycommand[STRING_SIZE];
180
181 va_start(args, text);
182
183 do {
184 size += 1000;
185 if (buf) free(buf);
186 buf = malloc(size);
187 i = vsnprintf(buf, size, text, args);
188 } while (i == size);
189
190 va_end(args);
191
192 newtCenteredWindow(width, height, title);
193
194 t = newtTextbox(1, 1, width - 2, height - 2, NEWT_TEXTBOX_WRAP);
195 newtTextboxSetText(t, buf);
196 f = newtForm(NULL, NULL, 0);
197
198 free(buf);
199
200 newtFormAddComponent(f, t);
201
202 s = newtScale(1, 3, width - 2, lines);
203 newtScaleSet(s, progress);
204
205 newtFormAddComponent(f, s);
206
207 newtDrawForm(f);
208 newtRefresh();
209
210 snprintf(mycommand, STRING_SIZE, "%s 2>>%s", command, mylog);
211 fprintf(flog, "Running command: %s\n", command);
212
213 if (!(p = popen(command, "r")))
214 {
215 rc = 1;
216 goto EXIT;
217 }
218 setvbuf(p, NULL, _IOLBF, 255);
219
220 while (fgets(buffer, STRING_SIZE, p))
221 {
222 newtScaleSet(s, ++progress);
223 newtRefresh();
224 fprintf(flog, "%s", buffer);
225 }
226
227 rc = pclose(p);
228
229 EXIT:
230 newtFormDestroy(f);
231 newtPopWindow();
232
233 return rc;
234 }
235
236 int checkformodule(char *module)
237 {
238 FILE *file;
239 char buffer[STRING_SIZE];
240 int result = 0;
241
242 if (!(file = fopen("/proc/modules", "r")))
243 {
244 fprintf(flog, "Unable to open /proc/modules in checkformodule()\n");
245 return 0;
246 }
247
248 while (fgets(buffer, STRING_SIZE, file))
249 {
250 if (strncmp(buffer, module, strlen(module)) == 0)
251 {
252 if (buffer[strlen(module)] == ' ')
253 {
254 result = 1;
255 goto EXIT;
256 }
257 }
258 }
259
260 EXIT:
261 fclose(file);
262
263 return result;
264 }
265
266 int _replace_string(char string[], char *from, char *to)
267 {
268 int fromlen = strlen(from);
269 int tolen = strlen(to);
270 char *start, *p1, *p2;
271 for(start = string; *start != '\0'; start++)
272 {
273 p1 = from;
274 p2 = start;
275 while(*p1 != '\0')
276 {
277 if(*p1 != *p2)
278 break;
279 p1++;
280 p2++;
281 }
282 if(*p1 == '\0')
283 {
284 if(fromlen != tolen)
285 {
286 memmove(start + tolen, start + fromlen,
287 strlen(start + fromlen) + 1);
288 }
289 for(p1 = to; *p1 != '\0'; p1++)
290 *start++ = *p1;
291 return 1;
292 }
293 }
294 return 0;
295 }
296
297 int replace(char filename1[], char *from, char *to)
298 {
299 FILE *file1, *file2;
300 char filename2[1000];
301 char temp[1000];
302 int ret = 0;
303
304 /* Open the source and destination files */
305 strcpy (filename2, filename1);
306 strcat (filename2, ".new");
307 if (!(file1 = fopen (filename1, "r"))) return 1;
308 if (!(file2 = fopen (filename2, "w"))) {
309 fclose(file1);
310 return -1;
311 }
312
313 /* Start reading in lines */
314 while (fgets (temp, 1000, file1) != NULL) {
315
316 if (strlen(to) > 0) {
317 /* Replace string */
318 ret = _replace_string (temp, from, to);
319
320 /* Write string to new file */
321 fputs(temp, file2);
322 } else {
323 /* Remove string when to is NULL */
324 if (!strstr(temp, from))
325 fputs(temp, file2);
326 }
327 }
328
329 /* Close source and destination */
330 fclose (file1);
331 fclose (file2);
332
333 /* Move the file */
334 rename (filename2, filename1);
335
336 return (ret);
337 }
338
339 /* Include enabled languages */
340 #ifdef LANG_EN_ONLY
341 #include "lang_en.c"
342 #else
343 #include "lang_de.c"
344 #include "lang_en.c"
345 #include "lang_es.c"
346 #include "lang_fr.c"
347 #endif
348
349 // returns a pointer to the actual running version number of IPFire.
350 // Successive updates increase effective version but not VERSION !
351 char g_title[STRING_SIZE] = "";
352 char* get_version(void) {
353 FILE *f_title;
354 if ((f_title = fopen ("/etc/issue", "r"))) {
355 fgets (g_title, STRING_SIZE, f_title);
356 fclose (f_title);
357 if (g_title[strlen(g_title) - 1] == '\n') g_title[strlen(g_title) - 1] = '\0';
358 } else {
359 sprintf (g_title, "%s %s - %s", NAME, VERSION, SLOGAN);
360 }
361 return g_title;
362 }