Randomize ephermal source ports.
[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  * $DragonFly: src/release/sysinstall/Attic/wizard.c,v 1.2 2003/06/17 04:27:21 dillon Exp $
11  *
12  */
13
14 #include "sysinstall.h"
15 #include <fcntl.h>
16 #include <err.h>
17
18 int
19 scan_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
30 void
31 Scan_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
64 void
65 slice_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             Free_Disk(d);
174             d = Open_Disk(d->name);
175             continue;
176         }
177         if (strcasecmp(*cmds,"help"))
178             printf("\007ERROR\n");
179         printf("CMDS:\n");
180         printf("allfreebsd\t\t");
181         printf("dedicate\t\t");
182         printf("bios cyl hd sect\n");
183         printf("collapse [pointer]\t\t");
184 #ifdef PC98
185         printf("create offset size enum subtype flags name\n");
186 #else
187         printf("create offset size enum subtype flags\n");
188 #endif
189         printf("subtype(part): swap=1, ffs=7\t\t");
190         printf("delete pointer\n");
191         printf("list\t\t");
192         printf("quit\n");
193         printf("read [disk]\t\t");
194         printf("scan\n");
195         printf("write\t\t");
196         printf("ENUM:\n\t");
197         for(i=0;chunk_n[i];i++)
198             printf("%d = %s%s",i,chunk_n[i],i == 4 ? "\n\t" : "  ");
199         printf("\n");
200
201     }
202     systemResumeDialog();
203 }