Initial import from FreeBSD RELENG_4:
[dragonfly.git] / release / sysinstall / wizard.c
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $FreeBSD: src/release/sysinstall/wizard.c,v 1.15.2.5 2002/07/02 21:27:28 jhb Exp $
10  *
11  */
12
13 #include "sysinstall.h"
14 #include <fcntl.h>
15 #include <err.h>
16
17 int
18 scan_block(int fd, daddr_t block)
19 {
20     u_char foo[512];
21
22     if (-1 == lseek(fd,block * 512,SEEK_SET))
23         err(1,"lseek");
24     if (512 != read(fd,foo, 512))
25         return 1;
26     return 0;
27 }
28
29 void
30 Scan_Disk(Disk *d)
31 {
32     char device[64];
33     u_long l;
34     int i,j,fd;
35
36     strcpy(device,"/dev/");
37     strcat(device,d->name);
38
39     fd = open(device,O_RDWR);
40     if (fd < 0) {
41         msgWarn("open(%s) failed", device);
42         return;
43     }
44     for(i=-1,l=0;;l++) {
45         j = scan_block(fd,l);
46         if (j != i) {
47             if (i == -1) {
48                 printf("%c: %lu.",j ? 'B' : 'G', l);
49                 fflush(stdout);
50             } else if (i == 0) {
51                 printf(".%lu\nB: %lu.",l-1,l);
52                 fflush(stdout);
53             } else {
54                 printf(".%lu\nG: %lu.",l-1,l);
55                 fflush(stdout);
56             }
57             i = j;
58         }
59     }
60     close(fd);
61 }
62
63 void
64 slice_wizard(Disk *d)
65 {
66     Disk *db;
67     char myprompt[BUFSIZ];
68     char input[BUFSIZ];
69     char *p,*q=0;
70     char **cp,*cmds[200];
71     int ncmd,i;
72
73     systemSuspendDialog();
74     sprintf(myprompt,"%s> ", d->name);
75     while(1) {
76         printf("--==##==--\n");
77         Debug_Disk(d);
78         p = CheckRules(d);
79         if (p) {
80             printf("%s",p);
81             free(p);
82         }
83         printf("%s", myprompt);
84         fflush(stdout);
85         q = p = fgets(input,sizeof(input),stdin);
86         if(!p)
87             break;
88         for(cp = cmds; (*cp = strsep(&p, " \t\n")) != NULL;)
89             if (**cp != '\0')
90                 cp++;
91         ncmd = cp - cmds;
92         if(!ncmd)
93             continue;
94         if (!strcasecmp(*cmds,"quit")) { break; }
95         if (!strcasecmp(*cmds,"exit")) { break; }
96         if (!strcasecmp(*cmds,"q")) { break; }
97         if (!strcasecmp(*cmds,"x")) { break; }
98         if (!strcasecmp(*cmds,"delete") && ncmd == 2) {
99             printf("delete = %d\n",
100                    Delete_Chunk(d,
101                                 (struct chunk *)strtol(cmds[1],0,0)));
102             continue;
103         }
104         if (!strcasecmp(*cmds,"allfreebsd")) {
105             All_FreeBSD(d, 0);
106             continue;
107         }
108         if (!strcasecmp(*cmds,"dedicate")) {
109             All_FreeBSD(d, 1);
110             continue;
111         }
112         if (!strcasecmp(*cmds,"bios") && ncmd == 4) {
113             Set_Bios_Geom(d,
114                           strtol(cmds[1],0,0),
115                           strtol(cmds[2],0,0),
116                           strtol(cmds[3],0,0));
117             continue;
118         }
119         if (!strcasecmp(*cmds,"list")) {
120             cp = Disk_Names();
121             printf("Disks:");
122             for(i=0;cp[i];i++) {
123                 printf(" %s",cp[i]);
124                 free(cp[i]);
125             }
126             free(cp);
127             continue;
128         }
129 #ifdef PC98
130         if (!strcasecmp(*cmds,"create") && ncmd == 7) {
131             printf("Create=%d\n",
132                    Create_Chunk(d,
133                                 strtol(cmds[1],0,0),
134                                 strtol(cmds[2],0,0),
135                                 strtol(cmds[3],0,0),
136                                 strtol(cmds[4],0,0),
137                                 strtol(cmds[5],0,0),
138                                 cmds[6]));
139             continue;
140         }
141 #else
142         if (!strcasecmp(*cmds,"create") && ncmd == 6) {
143             printf("Create=%d\n",
144                    Create_Chunk(d,
145                                 strtol(cmds[1],0,0),
146                                 strtol(cmds[2],0,0),
147                                 strtol(cmds[3],0,0),
148                                 strtol(cmds[4],0,0),
149                                 strtol(cmds[5],0,0)));
150             continue;
151         }
152 #endif
153         if (!strcasecmp(*cmds,"read")) {
154             db = d;
155             if (ncmd > 1)
156                 d = Open_Disk(cmds[1]);
157             else
158                 d = Open_Disk(d->name);
159             if (d)
160                 Free_Disk(db);
161             else
162                 d = db;
163             continue;
164         }
165         if (!strcasecmp(*cmds,"scan")) {
166             Scan_Disk(d);
167             continue;
168         }
169         if (!strcasecmp(*cmds,"write")) {
170             printf("Write=%d\n",
171                    Fake ? 0 : Write_Disk(d));
172             Free_Disk(d);
173             d = Open_Disk(d->name);
174             continue;
175         }
176         if (strcasecmp(*cmds,"help"))
177             printf("\007ERROR\n");
178         printf("CMDS:\n");
179         printf("allfreebsd\t\t");
180         printf("dedicate\t\t");
181         printf("bios cyl hd sect\n");
182         printf("collapse [pointer]\t\t");
183 #ifdef PC98
184         printf("create offset size enum subtype flags name\n");
185 #else
186         printf("create offset size enum subtype flags\n");
187 #endif
188         printf("subtype(part): swap=1, ffs=7\t\t");
189         printf("delete pointer\n");
190         printf("list\t\t");
191         printf("quit\n");
192         printf("read [disk]\t\t");
193         printf("scan\n");
194         printf("write\t\t");
195         printf("ENUM:\n\t");
196         for(i=0;chunk_n[i];i++)
197             printf("%d = %s%s",i,chunk_n[i],i == 4 ? "\n\t" : "  ");
198         printf("\n");
199
200     }
201     systemResumeDialog();
202 }