From a6d93b253994c20839ced649eaa5f7fa6606a86f Mon Sep 17 00:00:00 2001 From: Guillaume Cottenceau Date: Tue, 13 Feb 2001 21:29:45 +0000 Subject: write down Magic Callback (tm) for static ip stuff --- mdk-stage1/automatic.c | 4 +- mdk-stage1/automatic.h | 2 +- mdk-stage1/dhcp.c | 2 +- mdk-stage1/disk.c | 2 +- mdk-stage1/frontend.h | 2 +- mdk-stage1/network.c | 60 ++++++++++++++------- mdk-stage1/network.h | 2 +- mdk-stage1/newt-frontend.c | 125 +++++++++++++++++++++++++++++++++++++++++++- mdk-stage1/stdio-frontend.c | 2 +- 9 files changed, 173 insertions(+), 28 deletions(-) (limited to 'mdk-stage1') diff --git a/mdk-stage1/automatic.c b/mdk-stage1/automatic.c index 57ae1f3a6..486a829de 100644 --- a/mdk-stage1/automatic.c +++ b/mdk-stage1/automatic.c @@ -127,10 +127,10 @@ enum return_type ask_from_list_comments_auto(char *msg, char ** elems, char ** e } -enum return_type ask_from_entries_auto(char *msg, char ** questions, char *** answers, int entry_size, char ** questions_auto) +enum return_type ask_from_entries_auto(char *msg, char ** questions, char *** answers, int entry_size, char ** questions_auto, void (*callback_func)(char ** strings)) { if (!IS_AUTOMATIC) - return ask_from_entries(msg, questions, answers, entry_size); + return ask_from_entries(msg, questions, answers, entry_size, callback_func); else { char * tmp_answers[50]; int i = 0; diff --git a/mdk-stage1/automatic.h b/mdk-stage1/automatic.h index f51b1d749..c2ac0a73d 100644 --- a/mdk-stage1/automatic.h +++ b/mdk-stage1/automatic.h @@ -27,6 +27,6 @@ void grab_automatic_params(char * line); enum return_type ask_from_list_auto(char *msg, char ** elems, char ** choice, char * auto_param, char ** elems_auto); enum return_type ask_from_list_comments_auto(char *msg, char ** elems, char ** elems_comments, char ** choice, char * auto_param, char ** elems_auto); -enum return_type ask_from_entries_auto(char *msg, char ** questions, char *** answers, int entry_size, char ** questions_auto); +enum return_type ask_from_entries_auto(char *msg, char ** questions, char *** answers, int entry_size, char ** questions_auto, void (*callback_func)(char ** strings)); #endif diff --git a/mdk-stage1/dhcp.c b/mdk-stage1/dhcp.c index 7a4d4bd68..b7dcccf8e 100644 --- a/mdk-stage1/dhcp.c +++ b/mdk-stage1/dhcp.c @@ -185,7 +185,7 @@ void set_missing_ip_info(struct interface_info * intf) bp_int32 nmNum; if (intf->netmask.s_addr == 0) - guess_netmask(intf); + inet_aton(guess_netmask(inet_ntoa(intf->ip)), &intf->netmask); nmNum = *((bp_int32 *) &intf->netmask); diff --git a/mdk-stage1/disk.c b/mdk-stage1/disk.c index c7ad9c7f0..3dbebd0ec 100644 --- a/mdk-stage1/disk.c +++ b/mdk-stage1/disk.c @@ -111,7 +111,7 @@ static enum return_type try_with_device(char *dev_name) } if (ask_from_entries("Please enter the directory (or ISO image file) containing the " DISTRIB_NAME " Distribution.", - questions_location, &answers_location, 24) != RETURN_OK) { + questions_location, &answers_location, 24, NULL) != RETURN_OK) { umount(disk_own_mount); return try_with_device(dev_name); } diff --git a/mdk-stage1/frontend.h b/mdk-stage1/frontend.h index 9d001bdcf..2869590e7 100644 --- a/mdk-stage1/frontend.h +++ b/mdk-stage1/frontend.h @@ -36,6 +36,6 @@ void end_progression(void); enum return_type ask_yes_no(char *msg); enum return_type ask_from_list(char *msg, char ** elems, char ** choice); enum return_type ask_from_list_comments(char *msg, char ** elems, char ** elems_comments, char ** choice); -enum return_type ask_from_entries(char *msg, char ** questions, char *** answers, int entry_size); +enum return_type ask_from_entries(char *msg, char ** questions, char *** answers, int entry_size, void (*callback_func)(char ** strings)); #endif diff --git a/mdk-stage1/network.c b/mdk-stage1/network.c index 7c4207c36..47436a940 100644 --- a/mdk-stage1/network.c +++ b/mdk-stage1/network.c @@ -311,16 +311,45 @@ static int save_netinfo(struct interface_info * intf) { } -void guess_netmask(struct interface_info * intf) +char * guess_netmask(char * ip_addr) { - unsigned long int tmp = ntohl(intf->ip.s_addr); + struct in_addr addr; + unsigned long int tmp; + + if (!inet_aton(ip_addr, &addr)) + return ""; + + tmp = ntohl(addr.s_addr); + if (((tmp & 0xFF000000) >> 24) <= 127) - inet_aton("255.0.0.0", &intf->netmask); + return "255.0.0.0"; else if (((tmp & 0xFF000000) >> 24) <= 191) - inet_aton("255.255.0.0", &intf->netmask); + return "255.255.0.0"; else - inet_aton("255.255.255.0", &intf->netmask); - log_message("netmask guess: %s", inet_ntoa(intf->netmask)); + return "255.255.255.0"; +} + + +static void static_ip_callback(char ** strings) +{ + struct in_addr addr; + + if (!inet_aton(strings[0], &addr)) + return; + + 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])); } @@ -342,7 +371,7 @@ static enum return_type setup_network_interface(struct interface_info * intf) struct in_addr addr; results = ask_from_entries_auto("Please enter the network information. (leave netmask void for Internet standard)", - questions, &answers, 16, questions_auto); + questions, &answers, 16, questions_auto, static_ip_callback); if (results != RETURN_OK) return setup_network_interface(intf); @@ -362,11 +391,9 @@ static enum return_type setup_network_interface(struct interface_info * intf) gateway.s_addr = 0; /* keep an understandable state */ } - if (!strcmp(answers[3], "")) - guess_netmask(intf); - else if (!inet_aton(answers[3], &addr)) { + if (!inet_aton(answers[3], &addr)) { log_message("invalid netmask -- back to the guess"); - guess_netmask(intf); + inet_aton(guess_netmask(answers[0]), &addr); } else memcpy(&intf->netmask, &addr, sizeof(addr)); @@ -447,7 +474,7 @@ static enum return_type configure_network(struct interface_info * intf) 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); + questions, &answers, 32, questions_auto, NULL); if (results != RETURN_OK) return results; @@ -468,9 +495,6 @@ static enum return_type bringup_networking(struct interface_info * intf) my_insmod("af_packet", ANY_DRIVER_TYPE, NULL); -// if (intf->is_up == 1) -// log_message("interface already up (with IP %s)", inet_ntoa(intf->ip)); - while (results != RETURN_OK) { results = setup_network_interface(intf); if (results != RETURN_OK) @@ -593,7 +617,7 @@ enum return_type nfs_prepare(void) do { results = ask_from_entries_auto("Please enter the name or IP address of your NFS server, " "and the directory containing the " DISTRIB_NAME " Distribution.", - questions, &answers, 40, questions_auto); + questions, &answers, 40, questions_auto, NULL); if (results != RETURN_OK) return nfs_prepare(); @@ -659,7 +683,7 @@ enum return_type ftp_prepare(void) results = ask_from_entries_auto("Please enter the name or IP address of the FTP server, " "the directory containing the " DISTRIB_NAME " Distribution, " "and the login/pass if necessary (leave login blank for anonymous).", - questions, &answers, 40, questions_auto); + questions, &answers, 40, questions_auto, NULL); if (results != RETURN_OK) return ftp_prepare(); @@ -741,7 +765,7 @@ enum return_type http_prepare(void) results = ask_from_entries_auto("Please enter the name or IP address of the HTTP server, " "and the directory containing the " DISTRIB_NAME " Distribution.", - questions, &answers, 40, questions_auto); + questions, &answers, 40, questions_auto, NULL); if (results != RETURN_OK) return http_prepare(); diff --git a/mdk-stage1/network.h b/mdk-stage1/network.h index 8d6b2bb17..b832ea906 100644 --- a/mdk-stage1/network.h +++ b/mdk-stage1/network.h @@ -45,7 +45,7 @@ struct interface_info { /* these are to be used only by dhcp.c */ -void guess_netmask(struct interface_info * intf); +char * guess_netmask(char * ip_addr); int configure_net_device(struct interface_info * intf); extern char * hostname; diff --git a/mdk-stage1/newt-frontend.c b/mdk-stage1/newt-frontend.c index 6d55c9ab7..416d05ae6 100644 --- a/mdk-stage1/newt-frontend.c +++ b/mdk-stage1/newt-frontend.c @@ -237,7 +237,128 @@ enum return_type ask_yes_no(char *msg) else return RETURN_ERROR; } -enum return_type ask_from_entries(char *msg, char ** questions, char *** answers, int entry_size) + +static void (*callback_real_function)(char ** strings) = NULL; + +static void default_callback(newtComponent co, void * data) +{ + newtComponent * entries = data; + char * strings[50], ** ptr; + + if (!callback_real_function) + return; + + ptr = strings; + while (entries && *entries) { + *ptr = newtEntryGetValue(*entries); + entries++; + ptr++; + } + + callback_real_function(strings); + + ptr = strings; + entries = data; + while (entries && *entries) { + newtEntrySet(*entries, strdup(*ptr), 1); + entries++; + ptr++; + } +} + +/* only supports up to 50 buttons and entries -- shucks! */ +static int mynewtWinEntries(char * title, char * text, int suggestedWidth, int flexDown, + int flexUp, int dataWidth, void (*callback_func)(char ** strings), + struct newtWinEntry * items, char * button1, ...) { + newtComponent buttons[50], result, form, textw; + newtGrid grid, buttonBar, subgrid; + int numItems; + int rc, i; + int numButtons; + char * buttonName; + newtComponent entries[50]; + + va_list args; + + textw = newtTextboxReflowed(-1, -1, text, suggestedWidth, flexDown, + flexUp, 0); + + for (numItems = 0; items[numItems].text; numItems++); + + buttonName = button1, numButtons = 0; + va_start(args, button1); + while (buttonName) { + buttons[numButtons] = newtButton(-1, -1, buttonName); + numButtons++; + buttonName = va_arg(args, char *); + } + + va_end(args); + + buttonBar = newtCreateGrid(numButtons, 1); + for (i = 0; i < numButtons; i++) { + newtGridSetField(buttonBar, i, 0, NEWT_GRID_COMPONENT, + buttons[i], + i ? 1 : 0, 0, 0, 0, 0, 0); + } + + if (callback_func) { + callback_real_function = callback_func; + entries[numItems] = NULL; + } + else + callback_real_function = NULL; + + subgrid = newtCreateGrid(2, numItems); + for (i = 0; i < numItems; i++) { + newtComponent entr = newtEntry(-1, -1, items[i].value ? + *items[i].value : NULL, dataWidth, + items[i].value, items[i].flags); + + newtGridSetField(subgrid, 0, i, NEWT_GRID_COMPONENT, + newtLabel(-1, -1, items[i].text), + 0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0); + newtGridSetField(subgrid, 1, i, NEWT_GRID_COMPONENT, + entr, + 1, 0, 0, 0, 0, 0); + if (callback_func) { + entries[i] = entr; + newtComponentAddCallback(entr, default_callback, entries); + } + } + + + grid = newtCreateGrid(1, 3); + form = newtForm(NULL, 0, 0); + newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, textw, + 0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0); + newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, subgrid, + 0, 1, 0, 0, 0, 0); + newtGridSetField(grid, 0, 2, NEWT_GRID_SUBGRID, buttonBar, + 0, 1, 0, 0, 0, NEWT_GRID_FLAG_GROWX); + newtGridAddComponentsToForm(grid, form, 1); + newtGridWrappedWindow(grid, title); + newtGridFree(grid, 1); + + result = newtRunForm(form); + + for (rc = 0; rc < numItems; rc++) + *items[rc].value = strdup(*items[rc].value); + + for (rc = 0; result != buttons[rc] && rc < numButtons; rc++); + if (rc == numButtons) + rc = 0; /* F12 */ + else + rc++; + + newtFormDestroy(form); + newtPopWindow(); + + return rc; +} + + +enum return_type ask_from_entries(char *msg, char ** questions, char *** answers, int entry_size, void (*callback_func)(char ** strings)) { struct newtWinEntry entries[50]; int j, i = 0; @@ -267,7 +388,7 @@ enum return_type ask_from_entries(char *msg, char ** questions, char *** answers *(entries[j].value) = NULL; } - rc = newtWinEntries("Please fill entries...", msg, 52, 5, 5, entry_size, entries, "Ok", "Cancel", NULL); + rc = mynewtWinEntries("Please fill entries...", msg, 52, 5, 5, entry_size, callback_func, entries, "Ok", "Cancel", NULL); if (rc == 3) return RETURN_BACK; diff --git a/mdk-stage1/stdio-frontend.c b/mdk-stage1/stdio-frontend.c index 0eac0e173..dfef79f6c 100644 --- a/mdk-stage1/stdio-frontend.c +++ b/mdk-stage1/stdio-frontend.c @@ -287,7 +287,7 @@ enum return_type ask_yes_no(char *msg) } -enum return_type ask_from_entries(char *msg, char ** questions, char *** answers, int entry_size) +enum return_type ask_from_entries(char *msg, char ** questions, char *** answers, int entry_size, void (*callback_func)(char ** strings)) { int j, i = 0; char ** already_answers = NULL; -- cgit v1.2.1