Sync with FreeBSD. This removes the need for perl.
[dragonfly.git] / release / sysinstall / misc.c
1 /*
2  * Miscellaneous support routines..
3  *
4  * $FreeBSD: src/release/sysinstall/misc.c,v 1.40.2.1 2001/09/27 06:51:09 murray Exp $
5  * $DragonFly: src/release/sysinstall/Attic/misc.c,v 1.3 2003/08/08 04:18:36 dillon Exp $
6  *
7  * Copyright (c) 1995
8  *      Jordan Hubbard.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer,
15  *    verbatim and that no modifications are made prior to this
16  *    point in the file.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  */
34
35 #include "sysinstall.h"
36 #include <ctype.h>
37 #include <unistd.h>
38 #include <sys/stat.h>
39 #include <sys/errno.h>
40 #include <sys/file.h>
41 #include <sys/types.h>
42 #include <dirent.h>
43 #include <sys/wait.h>
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <vfs/ufs/ufsmount.h>
47 #include <sys/reboot.h>
48 #include <sys/disklabel.h>
49
50 /* Quick check to see if a file is readable */
51 Boolean
52 file_readable(char *fname)
53 {
54     if (!access(fname, F_OK))
55         return TRUE;
56     return FALSE;
57 }
58
59 /* Quick check to see if a file is executable */
60 Boolean
61 file_executable(char *fname)
62 {
63     if (!access(fname, X_OK))
64         return TRUE;
65     return FALSE;
66 }
67
68 /* Concatenate two strings into static storage */
69 char *
70 string_concat(char *one, char *two)
71 {
72     static char tmp[FILENAME_MAX];
73
74     /* Yes, we're deliberately cavalier about not checking for overflow */
75     strcpy(tmp, one);
76     strcat(tmp, two);
77     return tmp;
78 }
79
80 /* sane strncpy() function */
81 char *
82 sstrncpy(char *dst, const char *src, int size)
83 {
84     dst[size] = '\0';
85     return strncpy(dst, src, size);
86 }
87
88 /* Concatenate three strings into static storage */
89 char *
90 string_concat3(char *one, char *two, char *three)
91 {
92     static char tmp[FILENAME_MAX];
93
94     /* Yes, we're deliberately cavalier about not checking for overflow */
95     strcpy(tmp, one);
96     strcat(tmp, two);
97     strcat(tmp, three);
98     return tmp;
99 }
100
101 /* Clip the whitespace off the end of a string */
102 char *
103 string_prune(char *str)
104 {
105     int len = str ? strlen(str) : 0;
106
107     while (len && isspace(str[len - 1]))
108         str[--len] = '\0';
109     return str;
110 }
111
112 /* run the whitespace off the front of a string */
113 char *
114 string_skipwhite(char *str)
115 {
116     while (*str && isspace(*str))
117         ++str;
118     return str;
119 }
120
121 /* copy optionally and allow second arg to be null */
122 char *
123 string_copy(char *s1, char *s2)
124 {
125     if (!s1)
126         return NULL;
127     if (!s2)
128         s1[0] = '\0';
129     else
130         strcpy(s1, s2);
131     return s1;
132 }
133
134 /* convert an integer to a string, using a static buffer */
135 char *
136 itoa(int value)
137 {
138     static char buf[13];
139
140     snprintf(buf, 12, "%d", value);
141     return buf;
142 }
143
144 Boolean
145 directory_exists(const char *dirname)
146 {
147     DIR *tptr;
148
149     if (!dirname)
150         return FALSE;
151     if (!strlen(dirname))
152         return FALSE;
153
154     tptr = opendir(dirname);
155     if (!tptr)
156         return (FALSE);
157
158     closedir(tptr);
159     return (TRUE);
160 }
161
162 char *
163 pathBaseName(const char *path)
164 {
165     char *pt;
166     char *ret = (char *)path;
167
168     pt = strrchr(path,(int)'/');
169
170     if (pt != 0)                        /* if there is a slash */
171     {
172         ret = ++pt;                     /* start the file after it */
173     }
174     
175     return(ret);
176 }
177
178 /* A free guaranteed to take NULL ptrs */
179 void
180 safe_free(void *ptr)
181 {
182     if (ptr)
183         free(ptr);
184 }
185
186 /* A malloc that checks errors */
187 void *
188 safe_malloc(size_t size)
189 {
190     void *ptr;
191
192     if (size <= 0)
193         msgFatal("Invalid malloc size of %ld!", (long)size);
194     ptr = malloc(size);
195     if (!ptr)
196         msgFatal("Out of memory!");
197     bzero(ptr, size);
198     return ptr;
199 }
200
201 /* A realloc that checks errors */
202 void *
203 safe_realloc(void *orig, size_t size)
204 {
205     void *ptr;
206
207     if (size <= 0)
208         msgFatal("Invalid realloc size of %ld!", (long)size);
209     ptr = realloc(orig, size);
210     if (!ptr)
211         msgFatal("Out of memory!");
212     return ptr;
213 }
214
215 /* Create a path biased from the VAR_INSTALL_ROOT variable (if not /) */
216 char *
217 root_bias(char *path)
218 {
219     static char tmp[FILENAME_MAX];
220     char *cp = variable_get(VAR_INSTALL_ROOT);
221
222     if (!strcmp(cp, "/"))
223         return path;
224     strcpy(tmp, variable_get(VAR_INSTALL_ROOT));
225     strcat(tmp, path);
226     return tmp;
227 }
228
229 /*
230  * These next routines are kind of specialized just for building item lists
231  * for dialog_menu().
232  */
233
234 /* Add an item to an item list */
235 dialogMenuItem *
236 item_add(dialogMenuItem *list, char *prompt, char *title,
237          int (*checked)(dialogMenuItem *self),
238          int (*fire)(dialogMenuItem *self),
239          void (*selected)(dialogMenuItem *self, int is_selected),
240          void *data, int aux, int *curr, int *max)
241 {
242     dialogMenuItem *d;
243
244     if (*curr == *max) {
245         *max += 20;
246         list = (dialogMenuItem *)realloc(list, sizeof(dialogMenuItem) * *max);
247     }
248     d = &list[(*curr)++];
249     bzero(d, sizeof(*d));
250     d->prompt = prompt ? strdup(prompt) : NULL;
251     d->title = title ? strdup(title) : NULL;
252     d->checked = checked;
253     d->fire = fire;
254     d->selected = selected;
255     d->data = data;
256     d->aux = aux;
257     return list;
258 }
259
260 /* Toss the items out */
261 void
262 items_free(dialogMenuItem *list, int *curr, int *max)
263 {
264     int i;
265
266     for (i = 0; list[i].prompt; i++) {
267         safe_free(list[i].prompt);
268         safe_free(list[i].title);
269     }
270     safe_free(list);
271     *curr = *max = 0;
272 }
273
274 int
275 Mkdir(char *ipath)
276 {
277     struct stat sb;
278     int final;
279     char *p, *path;
280
281     if (file_readable(ipath) || Fake)
282         return DITEM_SUCCESS;
283
284     path = strcpy(alloca(strlen(ipath) + 1), ipath);
285     if (isDebug())
286         msgDebug("mkdir(%s)\n", path);
287     p = path;
288     if (p[0] == '/')            /* Skip leading '/'. */
289         ++p;
290     for (final = FALSE; !final; ++p) {
291         if (p[0] == '\0' || (p[0] == '/' && p[1] == '\0'))
292             final = TRUE;
293         else if (p[0] != '/')
294             continue;
295         *p = '\0';
296         if (stat(path, &sb)) {
297             if (errno != ENOENT) {
298                 msgConfirm("Couldn't stat directory %s: %s", path, strerror(errno));
299                 return DITEM_FAILURE;
300             }
301             if (isDebug())
302                 msgDebug("mkdir(%s..)\n", path);
303             if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
304                 msgConfirm("Couldn't create directory %s: %s", path,strerror(errno));
305                 return DITEM_FAILURE;
306             }
307         }
308         *p = '/';
309     }
310     return DITEM_SUCCESS;
311 }
312
313 int
314 Mount(char *mountp, void *dev)
315 {
316     struct ufs_args ufsargs;
317     char device[80];
318     char mountpoint[FILENAME_MAX];
319
320     if (Fake)
321         return DITEM_SUCCESS;
322
323     if (*((char *)dev) != '/') {
324         sprintf(device, "%s/dev/%s", RunningAsInit ? "/mnt" : "", (char *)dev);
325         sprintf(mountpoint, "%s%s", RunningAsInit ? "/mnt" : "", mountp);
326     }
327     else {
328         strcpy(device, dev);
329         strcpy(mountpoint, mountp);
330     }
331     memset(&ufsargs,0,sizeof ufsargs);
332
333     if (Mkdir(mountpoint)) {
334         msgConfirm("Unable to make directory mountpoint for %s!", mountpoint);
335         return DITEM_FAILURE;
336     }
337     if (isDebug())
338         msgDebug("mount %s %s\n", device, mountpoint);
339
340     ufsargs.fspec = device;
341     if (mount("ufs", mountpoint, RunningAsInit ? MNT_ASYNC | MNT_NOATIME : 0,
342         (caddr_t)&ufsargs) == -1) {
343         msgConfirm("Error mounting %s on %s : %s", device, mountpoint, strerror(errno));
344         return DITEM_FAILURE;
345     }
346     return DITEM_SUCCESS;
347 }
348
349 WINDOW *
350 openLayoutDialog(char *helpfile, char *title, int x, int y, int width, int height)
351 {
352     WINDOW              *win;
353     static char         help[FILENAME_MAX];
354
355     /* We need a curses window */
356     win = newwin(LINES, COLS, 0, 0);
357     if (win) {
358         /* Say where our help comes from */
359         if (helpfile) {
360             use_helpline("Press F1 for more information on this screen.");
361             use_helpfile(systemHelpFile(helpfile, help));
362         }
363         /* Setup a nice screen for us to splat stuff onto */
364         draw_box(win, y, x, height, width, dialog_attr, border_attr);
365         wattrset(win, dialog_attr);
366         mvwaddstr(win, y, x + (COLS - strlen(title)) / 2, title);
367     }
368     return win;
369 }
370
371 ComposeObj *
372 initLayoutDialog(WINDOW *win, Layout *layout, int x, int y, int *max)
373 {
374     ComposeObj *obj = NULL, *first;
375     int n;
376
377     /* Loop over the layout list, create the objects, and add them
378        onto the chain of objects that dialog uses for traversal*/
379     
380     n = 0;
381     while (layout[n].help != NULL) {
382         int t = TYPE_OF_OBJ(layout[n].type);
383
384         switch (t) {
385         case STRINGOBJ:
386             layout[n].obj = NewStringObj(win, layout[n].prompt, layout[n].var,
387                                          layout[n].y + y, layout[n].x + x, layout[n].len, layout[n].maxlen);
388             ((StringObj *)layout[n].obj)->attr_mask = ATTR_OF_OBJ(layout[n].type);
389             break;
390             
391         case BUTTONOBJ:
392             layout[n].obj = NewButtonObj(win, layout[n].prompt, layout[n].var, layout[n].y + y, layout[n].x + x);
393             break;
394             
395         default:
396             msgFatal("Don't support this object yet!");
397         }
398         AddObj(&obj, t, (void *) layout[n].obj);
399         n++;
400     }
401     *max = n - 1;
402     /* Find the first object in the list */
403     for (first = obj; first->prev; first = first->prev);
404     return first;
405 }
406
407 int
408 layoutDialogLoop(WINDOW *win, Layout *layout, ComposeObj **obj, int *n, int max, int *cbutton, int *cancel)
409 {
410     char help_line[80];
411     int ret, i, len = strlen(layout[*n].help);
412
413     /* Display the help line at the bottom of the screen */
414     for (i = 0; i < 79; i++)
415         help_line[i] = (i < len) ? layout[*n].help[i] : ' ';
416     help_line[i] = '\0';
417     use_helpline(help_line);
418     display_helpline(win, LINES - 1, COLS - 1);
419     wrefresh(win);
420             
421     /* Ask for libdialog to do its stuff */
422     ret = PollObj(obj);
423     /* Handle special case stuff that libdialog misses. Sigh */
424     switch (ret) {
425     case SEL_ESC:       /* Bail out */
426         *cancel = TRUE;
427         return FALSE;
428               
429         /* This doesn't work for list dialogs. Oh well. Perhaps
430            should special case the move from the OK button ``up''
431            to make it go to the interface list, but then it gets
432            awkward for the user to go back and correct screw up's
433            in the per-interface section */
434     case KEY_DOWN:
435     case SEL_CR:
436     case SEL_TAB:
437         if (*n < max)
438             ++*n;
439         else
440             *n = 0;
441         break;
442               
443         /* The user has pressed enter over a button object */
444     case SEL_BUTTON:
445         if (cbutton && *cbutton)
446             *cancel = TRUE;
447         else
448             *cancel = FALSE;
449         return FALSE;
450         
451     case KEY_UP:
452     case SEL_BACKTAB:
453         if (*n)
454             --*n;
455         else
456             *n = max;
457         break;
458         
459     case KEY_F(1):
460         display_helpfile();
461         
462         /* They tried some key combination we don't support - tootle them forcefully! */
463     default:
464         beep();
465     }
466     return TRUE;
467 }
468
469 WINDOW *
470 savescr(void)
471 {
472     WINDOW *w;
473
474     w = dupwin(newscr);
475     return w;
476 }
477
478 void
479 restorescr(WINDOW *w)
480 {
481     touchwin(w);
482     wrefresh(w);
483     delwin(w);
484 }
485