Home | History | Annotate | Download | only in cmenu
      1 /* -*- c -*- ------------------------------------------------------------- *
      2  *
      3  *   Copyright 2004-2005 Murali Krishnan Ganapathy - All Rights Reserved
      4  *
      5  *   This program is free software; you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
      8  *   Boston MA 02111-1307, USA; either version 2 of the License, or
      9  *   (at your option) any later version; incorporated herein by reference.
     10  *
     11  * ----------------------------------------------------------------------- */
     12 
     13 #ifndef NULL
     14 #define NULL ((void *) 0)
     15 #endif
     16 
     17 #include "cmenu.h"
     18 #include "com32io.h"
     19 #include "help.h"
     20 #include "passwords.h"
     21 #include "des.h"
     22 #include <stdlib.h>
     23 #include <stdio.h>
     24 #include <unistd.h>
     25 #include <getkey.h>
     26 
     27 /* Global variables */
     28 char infoline[160];
     29 char buffer[80];
     30 
     31 // Different network options
     32 static char nonet[] = "<n>etwork [none]";
     33 static char dhcpnet[] = "<n>etwork [dhcp]";
     34 static char statnet[] = "<n>etwork [static]";
     35 
     36 static char loginstr[] = "<L>ogin  ";
     37 static char logoutstr[] = "<L>ogout ";
     38 
     39 struct {
     40     unsigned int baseurl:1;	// Do we need to specify by url
     41     unsigned int mountcd:1;	// Should we mount the cd
     42     unsigned int winrep:1;	// Want to repair windows?
     43     unsigned int linrep:1;	// Want to repair linux?
     44 } flags;
     45 
     46 // Some menu options
     47 t_menuitem *baseurl, *mountcd, *network, *runprep, *winrep, *linrep;
     48 t_menuitem *stat, *dhcp, *none, *prepopt, *secret;
     49 
     50 // all the menus we are going to declare
     51 unsigned char TESTING, RESCUE, MAIN, PREPMENU, NETMENU, LONGMENU, SECRETMENU;
     52 
     53 char username[12];		// Name of user currently using the system
     54 
     55 /* End globals */
     56 
     57 TIMEOUTCODE ontimeout(void)
     58 {
     59     beep();
     60     return CODE_WAIT;
     61 }
     62 
     63 #define INFLINE 22
     64 #define PWDLINE 3
     65 #define PWDPROMPT 21
     66 #define PWDCOLUMN 60
     67 #define PWDATTR 0x74
     68 #define EDITPROMPT 21
     69 
     70 void keys_handler(t_menusystem * ms __attribute__ (( unused )), t_menuitem * mi, int scancode)
     71 {
     72     int nc, nr;
     73 
     74     if ((scancode) == KEY_F1 && mi->helpid != 0xFFFF) {	// If scancode of F1
     75 	runhelpsystem(mi->helpid);
     76     }
     77     // If user hit TAB, and item is an "executable" item
     78     // and user has privileges to edit it, edit it in place.
     79     if ((scancode == KEY_TAB) && (mi->action == OPT_RUN) &&
     80 	(isallowed(username, "editcmd") || isallowed(username, "root"))) {
     81     if (getscreensize(1, &nr, &nc)) {
     82         /* Unknown screen size? */
     83         nc = 80;
     84         nr = 24;
     85     }
     86 	// User typed TAB and has permissions to edit command line
     87 	gotoxy(EDITPROMPT, 1);
     88 	csprint("Command line:", 0x07);
     89 	editstring(mi->data, ACTIONLEN);
     90 	gotoxy(EDITPROMPT, 1);
     91     clear_line();
     92     }
     93 }
     94 
     95 t_handler_return login_handler(t_menusystem * ms, t_menuitem * mi)
     96 {
     97     (void)mi;			// Unused
     98     char pwd[40];
     99     char login[40];
    100     int nc, nr;
    101     t_handler_return rv;
    102 
    103     (void)ms;
    104 
    105     if (mi->item == loginstr) {	/* User wants to login */
    106     if (getscreensize(1, &nr, &nc)) {
    107         /* Unknown screen size? */
    108         nc = 80;
    109         nr = 24;
    110     }
    111 	gotoxy(PWDPROMPT, 1);
    112 	csprint("Enter Username: ", 0x07);
    113 	getstring(login, sizeof username);
    114 	gotoxy(PWDPROMPT, 1);
    115     clear_line();
    116 	csprint("Enter Password: ", 0x07);
    117 	getpwd(pwd, sizeof pwd);
    118 	gotoxy(PWDPROMPT, 1);
    119     clear_line();
    120 
    121 	if (authenticate_user(login, pwd)) {
    122 	    strcpy(username, login);
    123 	    mi->item = logoutstr;	// Change item to read "Logout"
    124 	} else
    125 	    strcpy(username, GUEST_USER);
    126     } else			// User needs to logout
    127     {
    128 	strcpy(username, GUEST_USER);
    129 	mi->item = loginstr;
    130     }
    131 
    132     if (strcmp(username, GUEST_USER) == 0) {
    133 	prepopt->action = OPT_INACTIVE;
    134 	secret->action = OPT_INVISIBLE;
    135     } else {
    136 	prepopt->action = OPT_SUBMENU;
    137 	prepopt->itemdata.radiomenunum = PREPMENU;
    138 	secret->action = OPT_SUBMENU;
    139 	secret->itemdata.submenunum = SECRETMENU;
    140     }
    141     rv.valid = 0;
    142     rv.refresh = 1;
    143     rv.reserved = 0;
    144     return rv;
    145 }
    146 
    147 void msys_handler(t_menusystem * ms, t_menuitem * mi)
    148 {
    149     int nc, nr;
    150     void *v;
    151 
    152     if (getscreensize(1, &nr, &nc)) {
    153         /* Unknown screen size? */
    154         nc = 80;
    155         nr = 24;
    156     }
    157     gotoxy(PWDLINE, PWDCOLUMN);
    158     csprint("User: ", PWDATTR);
    159     cprint(ms->fillchar, ms->fillattr, sizeof username);
    160     gotoxy(PWDLINE, PWDCOLUMN + 6);
    161     csprint(username, PWDATTR);
    162 
    163     if (mi->parindex != PREPMENU)	// If we are not in the PREP MENU
    164     {
    165 	gotoxy(INFLINE, 0);
    166     reset_colors();
    167     clear_line();
    168 	gotoxy(INFLINE + 1, 0);
    169     clear_line();
    170 	return;
    171     }
    172     strcpy(infoline, " ");
    173     if (flags.baseurl)
    174 	strcat(infoline, "baseurl=http://192.168.11.12/gui ");
    175     if (flags.mountcd)
    176 	strcat(infoline, "mountcd=yes ");
    177     v = (void *)network->data;
    178     if (v != NULL)		// Some network option specified
    179     {
    180 	strcat(infoline, "network=");
    181 	strcat(infoline, (char *)(((t_menuitem *) v)->data));
    182     }
    183     if (flags.winrep)
    184 	strcat(infoline, "repair=win ");
    185     if (flags.linrep)
    186 	strcat(infoline, "repair=lin ");
    187 
    188     gotoxy(INFLINE, 0);
    189     reset_colors();
    190     clear_line();
    191     gotoxy(INFLINE + 1, 0);
    192     clear_line();
    193     gotoxy(INFLINE, 0);
    194     csprint("Kernel Arguments:", 0x07);
    195     gotoxy(INFLINE, 17);
    196     csprint(infoline, 0x07);
    197 }
    198 
    199 t_handler_return network_handler(t_menusystem * ms, t_menuitem * mi)
    200 {
    201     // mi=network since this is handler only for that.
    202     (void)ms;			// Unused
    203 
    204     if (mi->data == (void *)none)
    205 	mi->item = nonet;
    206     if (mi->data == (void *)stat)
    207 	mi->item = statnet;
    208     if (mi->data == (void *)dhcp)
    209 	mi->item = dhcpnet;
    210     return ACTION_INVALID;	// VALID or INVALID does not matter
    211 }
    212 
    213 t_handler_return checkbox_handler(t_menusystem * ms, t_menuitem * mi)
    214 {
    215     (void)ms;			/* Unused */
    216 
    217     t_handler_return rv;
    218 
    219     if (mi->action != OPT_CHECKBOX)
    220 	return ACTION_INVALID;
    221 
    222     if (strcmp(mi->data, "baseurl") == 0)
    223 	flags.baseurl = (mi->itemdata.checked ? 1 : 0);
    224     if (strcmp(mi->data, "winrepair") == 0) {
    225 	if (mi->itemdata.checked) {
    226 	    flags.winrep = 1;
    227 	    linrep->action = OPT_INACTIVE;
    228 	} else {
    229 	    flags.winrep = 0;
    230 	    linrep->action = OPT_CHECKBOX;
    231 	}
    232     }
    233     if (strcmp(mi->data, "linrepair") == 0) {
    234 	if (mi->itemdata.checked) {
    235 	    flags.linrep = 1;
    236 	    winrep->action = OPT_INACTIVE;
    237 	} else {
    238 	    flags.winrep = 0;
    239 	    winrep->action = OPT_CHECKBOX;
    240 	}
    241     }
    242     if (strcmp(mi->data, "mountcd") == 0)
    243 	flags.mountcd = (mi->itemdata.checked ? 1 : 0);
    244 
    245     rv.valid = 0;
    246     rv.refresh = 1;
    247     rv.reserved = 0;
    248     return rv;
    249 }
    250 
    251 int main(void)
    252 {
    253     t_menuitem *curr;
    254     char cmd[160];
    255     char ip[30];
    256 
    257     // Set default username as guest
    258     strcpy(username, GUEST_USER);
    259 
    260     // Switch video mode here
    261     // setvideomode(0x18); // or whatever mode you want
    262 
    263     // Choose the default title and setup default values for all attributes....
    264     init_passwords("/isolinux/password");
    265     init_help("/isolinux/help");
    266     init_menusystem(NULL);
    267     set_window_size(1, 1, 20, 78);	// Leave some space around
    268 
    269     // Choose the default values for all attributes and char's
    270     // -1 means choose defaults (Actually the next 4 lines are not needed)
    271     //set_normal_attr (-1,-1,-1,-1);
    272     //set_status_info (-1,-1); // Display status on the last line
    273     //set_title_info  (-1,-1);
    274     //set_misc_info(-1,-1,-1,-1);
    275 
    276     // Register the menusystem handler
    277     reg_handler(HDLR_SCREEN, &msys_handler);
    278     reg_handler(HDLR_KEYS, &keys_handler);
    279     // Register the ontimeout handler, with a time out of 10 seconds
    280     reg_ontimeout(ontimeout, 10, 0);
    281 
    282     NETMENU = add_menu(" Init Network ", -1);
    283     none = add_item("<N>one", "Dont start network", OPT_RADIOITEM, "no ", 0);
    284     dhcp = add_item("<d>hcp", "Use DHCP", OPT_RADIOITEM, "dhcp ", 0);
    285     stat =
    286 	add_item("<s>tatic", "Use static IP I will specify later",
    287 		 OPT_RADIOITEM, "static ", 0);
    288 
    289     TESTING = add_menu(" Testing ", -1);
    290     set_menu_pos(5, 55);
    291     add_item("<M>emory Test", "Perform extensive memory testing", OPT_RUN,
    292 	     "memtest", 0);
    293     add_item("<I>nvisible", "You dont see this", OPT_INVISIBLE, "junk", 0);
    294     add_item("<E>xit this menu", "Go one level up", OPT_EXITMENU, "exit", 0);
    295 
    296     RESCUE = add_menu(" Rescue Options ", -1);
    297     add_item("<L>inux Rescue", "linresc", OPT_RUN, "linresc", 0);
    298     add_item("<D>os Rescue", "dosresc", OPT_RUN, "dosresc", 0);
    299     add_item("<W>indows Rescue", "winresc", OPT_RUN, "winresc", 0);
    300     add_item("<E>xit this menu", "Go one level up", OPT_EXITMENU, "exit", 0);
    301 
    302     PREPMENU = add_menu(" Prep options ", -1);
    303     baseurl =
    304 	add_item("<b>aseurl by IP?", "Specify gui baseurl by IP address",
    305 		 OPT_CHECKBOX, "baseurl", 0);
    306     mountcd =
    307 	add_item("<m>ountcd?", "Mount the cdrom drive?", OPT_CHECKBOX,
    308 		 "mountcd", 0);
    309     network =
    310 	add_item(dhcpnet, "How to initialise network device?", OPT_RADIOMENU,
    311 		 NULL, NETMENU);
    312     add_sep();
    313     winrep =
    314 	add_item("Reinstall <w>indows",
    315 		 "Re-install the windows side of a dual boot setup",
    316 		 OPT_CHECKBOX, "winrepair", 0);
    317     linrep =
    318 	add_item("Reinstall <l>inux",
    319 		 "Re-install the linux side of a dual boot setup", OPT_CHECKBOX,
    320 		 "linrepair", 0);
    321     add_sep();
    322     runprep =
    323 	add_item("<R>un prep now", "Execute prep with the above options",
    324 		 OPT_RUN, "prep", 0);
    325     add_item("<E>xit this menu", "Go up one level", OPT_EXITMENU, "exitmenu",
    326 	     0);
    327     baseurl->handler = &checkbox_handler;
    328     mountcd->handler = &checkbox_handler;
    329     winrep->handler = &checkbox_handler;
    330     linrep->handler = &checkbox_handler;
    331     network->handler = &network_handler;
    332     flags.baseurl = 0;
    333     flags.mountcd = 0;
    334     flags.winrep = 0;
    335     flags.linrep = 0;
    336 
    337     SECRETMENU = add_menu(" Secret Menu ", -1);
    338     add_item("secret 1", "Secret", OPT_RUN, "A", 0);
    339     add_item("secret 2", "Secret", OPT_RUN, "A", 0);
    340 
    341     LONGMENU = add_menu(" Long Menu ", 40);	// Override default here
    342     add_item("<A>a", "Aa", OPT_RUN, "A", 0);
    343     add_item("<B>b", "Ab", OPT_RUN, "A", 0);
    344     add_item("<C>", "A", OPT_RUN, "A", 0);
    345     add_item("<D>", "A", OPT_RUN, "A", 0);
    346     add_item("<E>", "A", OPT_RUN, "A", 0);
    347     add_item("<F>", "A", OPT_RUN, "A", 0);
    348     add_item("<G>", "A", OPT_RUN, "A", 0);
    349     add_item("<H>", "A", OPT_RUN, "A", 0);
    350     add_item("<I>", "A", OPT_RUN, "A", 0);
    351     add_item("<J>", "A", OPT_RUN, "A", 0);
    352     add_item("<K>", "A", OPT_RUN, "A", 0);
    353     add_item("<L>", "A", OPT_RUN, "A", 0);
    354     add_item("<J>", "A", OPT_RUN, "A", 0);
    355     add_item("<K>", "A", OPT_RUN, "A", 0);
    356     add_item("<L>", "A", OPT_RUN, "A", 0);
    357     add_item("<M>", "A", OPT_RUN, "A", 0);
    358     add_item("<N>", "A", OPT_RUN, "A", 0);
    359     add_item("<O>", "A", OPT_RUN, "A", 0);
    360     add_item("<P>", "A", OPT_RUN, "A", 0);
    361     add_item("<Q>", "A", OPT_RUN, "A", 0);
    362     add_item("<R>", "A", OPT_RUN, "A", 0);
    363     add_item("<S>", "A", OPT_RUN, "A", 0);
    364     add_item("<T>", "A", OPT_RUN, "A", 0);
    365     add_item("<U>", "A", OPT_RUN, "A", 0);
    366     add_item("<V>", "A", OPT_RUN, "A", 0);
    367     add_item("<W>", "A", OPT_RUN, "A", 0);
    368     add_item("<X>", "A", OPT_RUN, "A", 0);
    369     add_item("<Y>", "A", OPT_RUN, "A", 0);
    370     add_item("<Z>", "A", OPT_RUN, "A", 0);
    371     add_item("<1>", "A", OPT_RUN, "A", 0);
    372     add_item("<2>", "A", OPT_RUN, "A", 0);
    373     add_item("<3>", "A", OPT_RUN, "A", 0);
    374     add_item("<4>", "A", OPT_RUN, "A", 0);
    375     add_item("<5>", "A", OPT_RUN, "A", 0);
    376     add_item("<6>", "A", OPT_RUN, "A", 0);
    377     add_item("<7>", "A", OPT_RUN, "A", 0);
    378     add_item("<8>", "A", OPT_RUN, "A", 0);
    379     add_item("<9>", "A", OPT_RUN, "A", 0);
    380 
    381     MAIN = add_menu(" Main Menu ", 8);
    382     curr = add_item(loginstr, "Login as a privileged user", OPT_RUN, NULL, 0);
    383     set_item_options(-1, 23);
    384     curr->handler = &login_handler;
    385 
    386     add_item("<P>repare", "prep", OPT_RUN, "prep", 0);
    387     set_item_options(-1, 24);
    388     prepopt =
    389 	add_item("<P>rep options...",
    390 		 "Options for prep image: Requires authenticated user",
    391 		 OPT_INACTIVE, NULL, PREPMENU);
    392     set_item_options(-1, 25);
    393 
    394     add_item("<R>escue options...", "Troubleshoot a system", OPT_SUBMENU, NULL,
    395 	     RESCUE);
    396     set_item_options(-1, 26);
    397     add_item("<T>esting...", "Options to test hardware", OPT_SUBMENU, NULL,
    398 	     TESTING);
    399     set_item_options(-1, 27);
    400     add_item("<L>ong Menu...", "test menu system", OPT_SUBMENU, NULL, LONGMENU);
    401     set_item_options(-1, 28);
    402     secret =
    403 	add_item("<S>ecret Menu...", "Secret menu", OPT_INVISIBLE, NULL,
    404 		 SECRETMENU);
    405     set_item_options(-1, 29);
    406     add_item("<E>xit to prompt", "Exit the menu system", OPT_EXITMENU, "exit",
    407 	     0);
    408     set_item_options(-1, 30);
    409     csprint("Press any key within 5 seconds to show menu...", 0x07);
    410     if (get_key(stdin, 50) == KEY_NONE)	// Granularity of 100 milliseconds
    411     {
    412 	csprint("Sorry! Time's up.\r\n", 0x07);
    413 	return 1;
    414     }
    415     curr = showmenus(MAIN);
    416     if (curr) {
    417 	if (curr->action == OPT_RUN) {
    418 	    strcpy(cmd, curr->data);
    419 	    if (curr == runprep) {
    420 		strcat(cmd, infoline);
    421 		if (network->data == (void *)stat)	// We want static
    422 		{
    423 		    csprint("Enter IP address (last two octets only): ", 0x07);
    424 		    strcpy(ip, "Junk");
    425 		    editstring(ip, sizeof ip);
    426 		    strcat(cmd, "ipaddr=192.168.");
    427 		    strcat(cmd, ip);
    428 		}
    429 	    }
    430 	    if (issyslinux())
    431 		runsyslinuxcmd(cmd);
    432 	    else
    433 		csprint(cmd, 0x07);
    434 	    return 1;		// Should not happen when run from SYSLINUX
    435 	}
    436     }
    437     // If user quits the menu system, control comes here
    438     // If you want to execute some specific command uncomment the next two lines
    439 
    440     // if (syslinux) runcommand(YOUR_COMMAND_HERE);
    441     // else csprint(YOUR_COMMAND_HERE,0x07);
    442 
    443     // Deallocate space used for these data structures
    444     close_passwords();
    445     close_help();
    446     close_menusystem();
    447 
    448     // Return to prompt
    449     return 0;
    450 }
    451