wizard.c revision 61277
1/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dknet.dk> 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 61277 2000-06-05 13:17:23Z nyan $
10 *
11 */
12
13#include "sysinstall.h"
14#include <fcntl.h>
15#include <err.h>
16
17int
18scan_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
29void
30Scan_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
63void
64slice_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    sprintf(myprompt,"%s> ", d->name);
74    while(1) {
75	printf("--==##==--\n");
76	Debug_Disk(d);
77	p = CheckRules(d);
78	if (p) {
79	    printf("%s",p);
80	    free(p);
81	}
82	printf(myprompt);
83	fflush(stdout);
84	q = p = fgets(input,sizeof(input),stdin);
85	if(!p)
86	    break;
87	for(cp = cmds; (*cp = strsep(&p, " \t\n")) != NULL;)
88	    if (**cp != '\0')
89		cp++;
90	ncmd = cp - cmds;
91	if(!ncmd)
92	    continue;
93	if (!strcasecmp(*cmds,"quit")) { break; }
94	if (!strcasecmp(*cmds,"exit")) { break; }
95	if (!strcasecmp(*cmds,"q")) { break; }
96	if (!strcasecmp(*cmds,"x")) { break; }
97	if (!strcasecmp(*cmds,"delete") && ncmd == 2) {
98	    printf("delete = %d\n",
99		   Delete_Chunk(d,
100				(struct chunk *)strtol(cmds[1],0,0)));
101	    continue;
102	}
103	if (!strcasecmp(*cmds,"allfreebsd")) {
104	    All_FreeBSD(d, 0);
105	    continue;
106	}
107	if (!strcasecmp(*cmds,"dedicate")) {
108	    All_FreeBSD(d, 1);
109	    continue;
110	}
111	if (!strcasecmp(*cmds,"bios") && ncmd == 4) {
112	    Set_Bios_Geom(d,
113			  strtol(cmds[1],0,0),
114			  strtol(cmds[2],0,0),
115			  strtol(cmds[3],0,0));
116	    continue;
117	}
118	if (!strcasecmp(*cmds,"list")) {
119	    cp = Disk_Names();
120	    printf("Disks:");
121	    for(i=0;cp[i];i++) {
122		printf(" %s",cp[i]);
123		free(cp[i]);
124	    }
125	    free(cp);
126	    continue;
127	}
128#ifdef PC98
129	if (!strcasecmp(*cmds,"create") && ncmd == 7) {
130	    printf("Create=%d\n",
131		   Create_Chunk(d,
132				strtol(cmds[1],0,0),
133				strtol(cmds[2],0,0),
134				strtol(cmds[3],0,0),
135				strtol(cmds[4],0,0),
136				strtol(cmds[5],0,0),
137				cmds[6]));
138	    continue;
139	}
140#else
141	if (!strcasecmp(*cmds,"create") && ncmd == 6) {
142	    printf("Create=%d\n",
143		   Create_Chunk(d,
144				strtol(cmds[1],0,0),
145				strtol(cmds[2],0,0),
146				strtol(cmds[3],0,0),
147				strtol(cmds[4],0,0),
148				strtol(cmds[5],0,0)));
149	    continue;
150	}
151#endif
152	if (!strcasecmp(*cmds,"read")) {
153	    db = d;
154	    if (ncmd > 1)
155		d = Open_Disk(cmds[1]);
156	    else
157		d = Open_Disk(d->name);
158	    if (d)
159		Free_Disk(db);
160	    else
161		d = db;
162	    continue;
163	}
164	if (!strcasecmp(*cmds,"scan")) {
165	    Scan_Disk(d);
166	    continue;
167	}
168	if (!strcasecmp(*cmds,"write")) {
169	    printf("Write=%d\n",
170		   Fake ? 0 : Write_Disk(d));
171	    Free_Disk(d);
172	    d = Open_Disk(d->name);
173	    continue;
174	}
175	if (strcasecmp(*cmds,"help"))
176	    printf("\007ERROR\n");
177	printf("CMDS:\n");
178	printf("allfreebsd\t\t");
179	printf("dedicate\t\t");
180	printf("bios cyl hd sect\n");
181	printf("collapse [pointer]\t\t");
182#ifdef PC98
183	printf("create offset size enum subtype flags name\n");
184#else
185	printf("create offset size enum subtype flags\n");
186#endif
187	printf("subtype(part): swap=1, ffs=7\t\t");
188	printf("delete pointer\n");
189	printf("list\t\t");
190	printf("quit\n");
191	printf("read [disk]\t\t");
192	printf("scan\n");
193	printf("write\t\t");
194	printf("ENUM:\n\t");
195	for(i=0;chunk_n[i];i++)
196	    printf("%d = %s%s",i,chunk_n[i],i == 4 ? "\n\t" : "  ");
197	printf("\n");
198
199    }
200}
201