summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/newt/grid.c
blob: 4330113962f29b4f6f4dd58fd02b6a7564e02b51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <alloca.h>
#include <stdlib.h>
#include <string.h>

#include "newt.h"
#include "newt_pr.h"

struct gridField {
    enum newtGridElement type;
    union {
	newtGrid grid;
	newtComponent co;
    } u;
    int padLeft, padTop, padRight, padBottom;
    int anchor;
    int flags;
}; 

struct grid_s {
    int rows, cols;
    int width, height;		/* totals, -1 means unknown */
    struct gridField ** fields;
};

/* this is a bit of a hack */
extern struct componentOps formOps[];

newtGrid newtCreateGrid(int cols, int rows) {
    newtGrid grid;

    grid = malloc(sizeof(*grid));
    grid->rows = rows;
    grid->cols = cols;

    grid->fields = malloc(sizeof(*grid->fields) * cols);
    while (cols--) {
	grid->fields[cols] = malloc(sizeof(**(grid->fields)) * rows);
	memset(grid->fields[cols], 0, sizeof(**(grid->fields)) * rows);
    }

    grid->width = grid->height = -1;

    return grid;
}

void newtGridSetField(newtGrid grid, int col, int row, 
		      enum newtGridElement type, void * val, int padLeft,
		      int padTop, int padRight, int padBottom, int anchor,
		      int flags) {
    struct gridField * field = &grid->fields[col][row];

    if (field->type == NEWT_GRID_SUBGRID) 
	newtGridFree(field->u.grid, 1);

    field->type = type;
    field->u.co = (void *) val;

    field->padLeft = padLeft;
    field->padRight = padRight;
    field->padTop = padTop;
    field->padBottom = padBottom;
    field->anchor = anchor;
    field->flags = flags;

    grid->width = grid->height = -1;
}

static void distSpace(int extra, int items, int * list) {
    int all, some, i;

    all = extra / items;
    some = extra % items;
    for (i = 0; i < items; i++) {
	list[i] += all;
	if (some) {
	    list[i]++;
	    some--;
	}
    }
}

static void shuffleGrid(newtGrid grid, int left, int top, int set) {
    struct gridField * field;
    int row, col;
    int i, j;
    int minWidth, minHeight;
    int * widths, * heights;
    int thisLeft, thisTop;
    int x, y, remx, remy;

    widths = alloca(sizeof(*widths) * grid->cols);
    memset(widths, 0, sizeof(*widths) * grid->cols);
    heights = alloca(sizeof(*heights) * grid->rows);
    memset(heights, 0, sizeof(*heights) * grid->rows);

    minWidth = 0;
    for (row = 0; row < grid->rows; row++) {
	i = 0;
	for (col = 0; col < grid->cols; col++) {
	    field = &grid->fields[col][row];
	    if (field->type == NEWT_GRID_SUBGRID) {
		/* we'll have to redo this later */
		if (field->u.grid->width == -1) 
		    shuffleGrid(field->u.grid, left, top, 0);
		j = field->u.grid->width;
	    } else if (field->type == NEWT_GRID_COMPONENT) {
		if (field->u.co->ops == formOps)
		    newtFormSetSize(field->u.co);
		j = field->u.co->width;
	    } else 
		j = 0;

	    j += field->padLeft + field->padRight;

	    if (j > widths[col]) widths[col] = j;
	    i += widths[col];
	}

	if (i > minWidth) minWidth = i;
    }

    minHeight = 0;
    for (col = 0; col < grid->cols; col++) {
	i = 0;
	for (row = 0; row < grid->rows; row++) {
	    field = &grid->fields[col][row];
	    if (field->type == NEWT_GRID_SUBGRID) {
		/* we'll have to redo this later */
		if (field->u.grid->height == -1) 
		    shuffleGrid(field->u.grid, 0, 0, 0);
		j = field->u.grid->height;
	    } else if (field->type == NEWT_GRID_COMPONENT){
		j = field->u.co->height;
	    } else 
		j = 0;

	    j += field->padTop + field->padBottom;

	    if (j > heights[row]) heights[row] = j;
	    i += heights[row];
	}

	if (i > minHeight) minHeight = i;
    }

    /* this catches the -1 case */
    if (grid->width < minWidth) grid->width = minWidth;		/* ack! */
    if (grid->height < minHeight) grid->height = minHeight;	/* ditto! */

    if (!set) return;

    distSpace(grid->width - minWidth, grid->cols, widths);
    distSpace(grid->height - minHeight, grid->rows, heights);

    thisTop = top;
    for (row = 0; row < grid->rows; row++) {
	i = 0;
	thisLeft = left;
	for (col = 0; col < grid->cols; col++) {
	    field = &grid->fields[col][row];

	    if (field->type == NEWT_GRID_EMPTY) continue;

	    x = thisLeft + field->padLeft;
	    remx = widths[col] - field->padLeft - field->padRight;
	    y = thisTop + field->padTop;
	    remy = heights[row] - field->padTop - field->padBottom;

	    if (field->type == NEWT_GRID_SUBGRID) {
		remx -= field->u.grid->width;
		remy -= field->u.grid->height;
	    } else if (field->type == NEWT_GRID_COMPONENT) {
		remx -= field->u.co->width;
		remy -= field->u.co->height;
	    }

	    if (!(field->flags & NEWT_GRID_FLAG_GROWX)) {
		if (field->anchor & NEWT_ANCHOR_RIGHT)
		    x += remx;
		else if (!(field->anchor & NEWT_ANCHOR_LEFT))
		    x += (remx / 2);
	    }
	 
	    if (!(field->flags & NEWT_GRID_FLAG_GROWY)) {
		if (field->anchor & NEWT_ANCHOR_BOTTOM)
		    y += remx;
		else if (!(field->anchor & NEWT_ANCHOR_TOP))
		    y += (remy / 2);
	    }

	    if (field->type == NEWT_GRID_SUBGRID) {
		if (field->flags & NEWT_GRID_FLAG_GROWX)
		    field->u.grid->width = widths[col] - field->padLeft 
						- field->padRight;
		if (field->flags & NEWT_GRID_FLAG_GROWY)
		    field->u.grid->height = heights[col] - field->padTop
						- field->padBottom;

		shuffleGrid(field->u.grid, x, y, 1);
	    } else if (field->type == NEWT_GRID_COMPONENT) {
		field->u.co->ops->place(field->u.co, x, y);
	    }

	    thisLeft += widths[col];
	}

	thisTop += heights[row];
    }
}

void newtGridPlace(newtGrid grid, int left, int top) {
    shuffleGrid(grid, left, top, 1);
}

void newtGridFree(newtGrid grid, int recurse) {
    int row, col;

    for (col = 0; col < grid->cols; col++) {
	if (recurse) {
	    for (row = 0; row < grid->rows; row++) {
		if (grid->fields[col][row].type == NEWT_GRID_SUBGRID)
		    newtGridFree(grid->fields[col][row].u.grid, 1);
	    }
	}

	free(grid->fields[col]);
    }

    free(grid->fields);
    free(grid);
}

void newtGridGetSize(newtGrid grid, int * width, int * height) {
    if (grid->width == -1 || grid->height == -1) {
	grid->width = grid->height = -1;
	shuffleGrid(grid, 0, 0, 1);
    }

    *width = grid->width;
    *height = grid->height;
}

void newtGridWrappedWindow(newtGrid grid, char * title) {
    int width, height, offset = 0;

    newtGridGetSize(grid, &width, &height);
    if (width < strlen(title) + 2) {
	offset = ((strlen(title) + 2) - width) / 2; 
	width = strlen(title) + 2;
    }
    newtCenteredWindow(width + 2, height + 2, title);
    newtGridPlace(grid, 1 + offset, 1);
}

void newtGridWrappedWindowAt(newtGrid grid, char * title, int left, int top) {
    int width, height;

    newtGridGetSize(grid, &width, &height);
    newtOpenWindow(left, top, width + 2, height + 2, title);
    newtGridPlace(grid, 1, 1);
}

void newtGridAddComponentsToForm(newtGrid grid, newtComponent form, 
				 int recurse) {
    int row, col;

    for (col = 0; col < grid->cols; col++) {
	for (row = 0; row < grid->rows; row++) {
	    if (grid->fields[col][row].type == NEWT_GRID_SUBGRID && recurse)
		newtGridAddComponentsToForm(grid->fields[col][row].u.grid,
					    form, 1);
	    else if (grid->fields[col][row].type == NEWT_GRID_COMPONENT)
		newtFormAddComponent(form, grid->fields[col][row].u.co);
	}
    }
}

/* this handles up to 50 items */
static newtGrid stackem(int isVert, enum newtGridElement type1, void * what1,
			va_list args, int close) {
    struct item {
	enum newtGridElement type;
	void * what;
    } items[50];
    int i, num;
    newtGrid grid;
    
    items[0].type = type1, items[0].what = what1, num = 1;
    while (1) {
	items[num].type = va_arg(args, enum newtGridElement);
	if (items[num].type == NEWT_GRID_EMPTY) break;

	items[num].what = va_arg(args, void *);
	num++;
    }

    grid = newtCreateGrid(isVert ? 1 : num, isVert ? num : 1);

    for (i = 0; i < num; i++) {
	newtGridSetField(grid, isVert ? 0 : i, isVert ? i : 0, 
			 items[i].type, items[i].what,
			 close ? 0 : (i ? (isVert ? 0 : 1) : 0),
			 close ? 0 : (i ? (isVert ? 1 : 0) : 0), 0, 0, 0, 0);
    }

    return grid;
}

newtGrid newtGridHCloseStacked(enum newtGridElement type1, void * what1, ...) {
    va_list args;
    newtGrid grid;

    va_start(args, what1);

    grid = stackem(0, type1, what1, args, 1);

    va_start(args, what1);

    return grid;
}

newtGrid newtGridVCloseStacked(enum newtGridElement type1, void * what1, ...) {
    va_list args;
    newtGrid grid;

    va_start(args, what1);

    grid = stackem(1, type1, what1, args, 1);

    va_start(args, what1);

    return grid;
}

newtGrid newtGridVStacked(enum newtGridElement type1, void * what1, ...) {
    va_list args;
    newtGrid grid;

    va_start(args, what1);

    grid = stackem(1, type1, what1, args, 0);

    va_start(args, what1);

    return grid;
}

newtGrid newtGridHStacked(enum newtGridElement type1, void * what1, ...) {
    va_list args;
    newtGrid grid;

    va_start(args, what1);

    grid = stackem(0, type1, what1, args, 0);

    va_start(args, what1);

    return grid;
}

newtGrid newtGridBasicWindow(newtComponent text, newtGrid middle,
			     newtGrid buttons) {
    newtGrid grid;

    grid = newtCreateGrid(1, 3);
    newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, text,
		     0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0);
    newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, middle,
		     0, 1, 0, 0, 0, 0);
    newtGridSetField(grid, 0, 2, NEWT_GRID_SUBGRID, buttons,
		     0, 1, 0, 0, 0, NEWT_GRID_FLAG_GROWX);

    return grid;
}

newtGrid newtGridSimpleWindow(newtComponent text, newtComponent middle,
			     newtGrid buttons) {
    newtGrid grid;

    grid = newtCreateGrid(1, 3);
    newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, text,
		     0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0);
    newtGridSetField(grid, 0, 1, NEWT_GRID_COMPONENT, middle,
		     0, 1, 0, 0, 0, 0);
    newtGridSetField(grid, 0, 2, NEWT_GRID_SUBGRID, buttons,
		     0, 1, 0, 0, 0, NEWT_GRID_FLAG_GROWX);

    return grid;
}
/span> fopen(filename, "w"); if (!f) { log_perror(filename); return -1; } if (domain) fprintf(f, "search %s\n", domain); /* we can live without the domain search (user will have to enter fully-qualified names) */ fprintf(f, "nameserver %s\n", inet_ntoa(dns_server)); if (dns_server2.s_addr != 0) fprintf(f, "nameserver %s\n", inet_ntoa(dns_server2)); fclose(f); res_init(); /* reinit the resolver so DNS changes take affect */ return 0; } static int save_netinfo(struct interface_info * intf) { char * file_network = "/tmp/network"; char file_intf[500]; FILE * f; f = fopen(file_network, "w"); if (!f) { log_perror(file_network); return -1; } fprintf(f, "NETWORKING=yes\n"); fprintf(f, "FORWARD_IPV4=false\n"); if (hostname && !intf->boot_proto == BOOTPROTO_DHCP) fprintf(f, "HOSTNAME=%s\n", hostname); if (domain) fprintf(f, "DOMAINNAME=%s\n", domain); if (dhcp_hostname && !streq(dhcp_hostname, "")) fprintf(f, "DHCP_HOSTNAME=%s\n", dhcp_hostname); if (gateway.s_addr != 0) fprintf(f, "GATEWAY=%s\n", inet_ntoa(gateway)); fclose(f); strcpy(file_intf, "/tmp/ifcfg-"); strcat(file_intf, intf->device); f = fopen(file_intf, "w"); if (!f) { log_perror(file_intf); return -1; } fprintf(f, "DEVICE=%s\n", intf->device); if (intf->boot_proto == BOOTPROTO_DHCP) fprintf(f, "BOOTPROTO=dhcp\n"); else if (intf->boot_proto == BOOTPROTO_STATIC) { fprintf(f, "BOOTPROTO=static\n"); fprintf(f, "IPADDR=%s\n", inet_ntoa(intf->ip)); fprintf(f, "NETMASK=%s\n", inet_ntoa(intf->netmask)); fprintf(f, "NETWORK=%s\n", inet_ntoa(intf->network)); fprintf(f, "BROADCAST=%s\n", inet_ntoa(intf->broadcast)); } else if (intf->boot_proto == BOOTPROTO_ADSL_PPPOE) { fprintf(f, "BOOTPROTO=adsl_pppoe\n"); fprintf(f, "USER=%s\n", intf->user); fprintf(f, "PASS=%s\n", intf->pass); fprintf(f, "ACNAME=%s\n", intf->acname); } fclose(f); return 0; } char * guess_netmask(char * ip_addr) { struct in_addr addr; unsigned long int tmp; if (streq(ip_addr, "") || !inet_aton(ip_addr, &addr)) return ""; log_message("guessing netmask"); tmp = ntohl(addr.s_addr); if (((tmp & 0xFF000000) >> 24) <= 127) return "255.0.0.0"; else if (((tmp & 0xFF000000) >> 24) <= 191) return "255.255.0.0"; else return "255.255.255.0"; } char * guess_domain_from_hostname(char *hostname) { char *domain = strchr(strdup(hostname), '.'); if (!domain || domain[1] == '\0') { log_message("unable to guess domain from hostname: %s", hostname); return NULL; } return domain + 1; /* skip '.' */ } static void static_ip_callback(char ** strings) { struct in_addr addr; static int done = 0; if (done) return; if (streq(strings[0], "") || !inet_aton(strings[0], &addr)) return; done = 1; if (!strcmp(strings[1], "")) { char * ptr; strings[1] = strdup(strings[0]); ptr = strrchr(strings[1], '.'); if (ptr) *(ptr+1) = '\0'; } if (!strcmp(strings[2], "")) strings[2] = strdup(strings[1]); if (!strcmp(strings[3], "")) strings[3] = strdup(guess_netmask(strings[0])); } static enum return_type setup_network_interface(struct interface_info * intf) { enum return_type results; char * bootprotos[] = { "Static", "DHCP", "ADSL", NULL }; char * bootprotos_auto[] = { "static", "dhcp", "adsl" }; char * choice; results = ask_from_list_auto("Please choose the desired IP attribution.", bootprotos, &choice, "network", bootprotos_auto); if (results != RETURN_OK) return results; if (!strcmp(choice, "Static")) { char * questions[] = { "IP of this machine", "IP of DNS", "IP of default gateway", "Netmask", NULL }; char * questions_auto[] = { "ip", "dns", "gateway", "netmask" }; static char ** answers = NULL; struct in_addr addr; results = ask_from_entries_auto("Please enter the network information. (leave netmask blank for Internet standard)", questions, &answers, 16, questions_auto, static_ip_callback); if (results != RETURN_OK) return setup_network_interface(intf); if (streq(answers[0], "") || !inet_aton(answers[0], &addr)) { stg1_error_message("Invalid IP address."); return setup_network_interface(intf); } memcpy(&intf->ip, &addr, sizeof(addr)); if (!inet_aton(answers[1], &dns_server)) { log_message("invalid DNS"); dns_server.s_addr = 0; /* keep an understandable state */ } if (streq(answers[0], answers[1])) { log_message("IP and DNS are the same, guess you don't want a DNS, disabling it"); dns_server.s_addr = 0; /* keep an understandable state */ } if (!inet_aton(answers[2], &gateway)) { log_message("invalid gateway"); gateway.s_addr = 0; /* keep an understandable state */ } if ((streq(answers[3], "") && inet_aton(guess_netmask(answers[0]), &addr)) || inet_aton(answers[3], &addr)) memcpy(&intf->netmask, &addr, sizeof(addr)); else { stg1_error_message("Invalid netmask."); return setup_network_interface(intf); } *((uint32_t *) &intf->broadcast) = (*((uint32_t *) &intf->ip) & *((uint32_t *) &intf->netmask)) | ~(*((uint32_t *) &intf->netmask)); inet_aton("255.255.255.255", &addr); if (!memcmp(&addr, &intf->netmask, sizeof(addr))) { log_message("netmask is 255.255.255.255 -> point to point device"); intf->network = gateway; intf->is_ptp = 1; } else { *((uint32_t *) &intf->network) = *((uint32_t *) &intf->ip) & *((uint32_t *) &intf->netmask); intf->is_ptp = 0; } intf->boot_proto = BOOTPROTO_STATIC; if (configure_net_device(intf)) return RETURN_ERROR; } else if (streq(choice, "DHCP")) { results = perform_dhcp(intf); if (results == RETURN_BACK) return setup_network_interface(intf); if (results == RETURN_ERROR) return results; intf->boot_proto = BOOTPROTO_DHCP; if (configure_net_device(intf)) return RETURN_ERROR; } else if (streq(choice, "ADSL")) { results = perform_adsl(intf); if (results == RETURN_BACK) return setup_network_interface(intf); if (results == RETURN_ERROR) return results; } else return RETURN_ERROR; return add_default_route(); } static enum return_type configure_network(struct interface_info * intf) { char * dnshostname; if (hostname && domain) return RETURN_OK; dnshostname = mygethostbyaddr(inet_ntoa(intf->ip)); if (dnshostname) { if (intf->boot_proto == BOOTPROTO_STATIC) hostname = strdup(dnshostname); domain = guess_domain_from_hostname(dnshostname); if (domain) { log_message("got hostname and domain from dns entry, %s and %s", dnshostname, domain); return RETURN_OK; } } else log_message("reverse name lookup on self failed"); if (domain) return RETURN_OK; dnshostname = NULL; if (dns_server.s_addr != 0) { wait_message("Trying to resolve dns..."); dnshostname = mygethostbyaddr(inet_ntoa(dns_server)); remove_wait_message(); if (dnshostname) { log_message("got DNS fullname, %s", dnshostname); domain = guess_domain_from_hostname(dnshostname); } else log_message("reverse name lookup on DNS failed"); } else log_message("no DNS, unable to guess domain"); if (domain) { log_message("got domain from DNS fullname, %s", domain); } else { enum return_type results; char * questions[] = { "Host name", "Domain name", NULL }; char * questions_auto[] = { "hostname", "domain" }; static char ** answers = NULL; char * boulet; results = ask_from_entries_auto("I could not guess hostname and domain name; please fill in this information. " "Valid answers are for example: `mybox' for hostname and `mynetwork.com' for " "domain name, for a machine called `mybox.mynetwork.com' on the Internet.", questions, &answers, 32, questions_auto, NULL); if (results != RETURN_OK) return results; hostname = answers[0]; if ((boulet = strchr(hostname, '.')) != NULL) boulet[0] = '\0'; domain = answers[1]; } return RETURN_OK; } static enum return_type bringup_networking(struct interface_info * intf) { static struct interface_info loopback; enum return_type results = RETURN_ERROR; my_insmod("af_packet", ANY_DRIVER_TYPE, NULL, 1); while (results != RETURN_OK) { results = setup_network_interface(intf); if (results != RETURN_OK) return results; write_resolvconf(); results = configure_network(intf); } write_resolvconf(); /* maybe we have now domain to write also */ if (loopback.is_up == 0) { int rc; strcpy(loopback.device, "lo"); loopback.is_ptp = 0; loopback.is_up = 0; loopback.ip.s_addr = htonl(0x7f000001); loopback.netmask.s_addr = htonl(0xff000000); loopback.broadcast.s_addr = htonl(0x7fffffff); loopback.network.s_addr = htonl(0x7f000000); rc = configure_net_device(&loopback); if (rc) return RETURN_ERROR; } return RETURN_OK; } static char * interface_select(void) { char ** interfaces, ** ptr; char * descriptions[50]; char * choice; int i, count = 0; enum return_type results; interfaces = get_net_devices(); ptr = interfaces; while (ptr && *ptr) { count++; ptr++; } if (count == 0) { stg1_error_message("No NET device found.\n" "Hint: if you're using a Laptop, note that PCMCIA Network adapters are now supported either with `pcmcia.img' or `network.img', please try both these bootdisks."); i = ask_insmod(NETWORK_DEVICES); if (i == RETURN_BACK) return NULL; return interface_select(); } if (count == 1) return *interfaces; i = 0; while (interfaces[i]) { descriptions[i] = get_net_intf_description(interfaces[i]); i++; } results = ask_from_list_comments_auto("Please choose the NET device to use for the installation.", interfaces, descriptions, &choice, "interface", interfaces); if (results != RETURN_OK) return NULL; return choice; } #ifndef MANDRAKE_MOVE static enum return_type get_http_proxy(char **http_proxy_host, char **http_proxy_port) { char *questions[] = { "HTTP proxy host", "HTTP proxy port", NULL }; char *questions_auto[] = { "proxy_host", "proxy_port", NULL }; static char ** answers = NULL;