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