We need klibc-1.4.26 for getopt_long() to actually work.
[syslinux.git] / menu / complex.c
blob94627c4f79878ee996a83913c27958a3e24364c0
1 /* -*- c -*- ------------------------------------------------------------- *
3 * Copyright 2004-2005 Murali Krishnan Ganapathy - All Rights Reserved
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.
11 * ----------------------------------------------------------------------- */
13 #ifndef NULL
14 #define NULL ((void *) 0)
15 #endif
17 #include "menu.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>
25 /* Global variables */
26 char infoline[160];
27 char buffer[80];
29 // Different network options
30 static char nonet[] = "<n>etwork [none]";
31 static char dhcpnet[]="<n>etwork [dhcp]";
32 static char statnet[]="<n>etwork [static]";
34 static char loginstr[] = "<L>ogin ";
35 static char logoutstr[]= "<L>ogout ";
37 struct {
38 unsigned int baseurl : 1; // Do we need to specify by url
39 unsigned int mountcd : 1; // Should we mount the cd
40 unsigned int winrep : 1; // Want to repair windows?
41 unsigned int linrep : 1; // Want to repair linux?
42 } flags;
44 // Some menu options
45 t_menuitem *baseurl,*mountcd,*network,*runprep,*winrep,*linrep;
46 t_menuitem * stat,*dhcp,*none,*prepopt,*secret;
48 // all the menus we are going to declare
49 unsigned char TESTING,RESCUE,MAIN,PREPMENU,NETMENU,LONGMENU,SECRETMENU;
51 char username[12]; // Name of user currently using the system
53 /* End globals */
55 TIMEOUTCODE ontimeout()
57 beep();
58 return CODE_WAIT;
62 #define INFLINE 22
63 #define PWDLINE 3
64 #define PWDPROMPT 21
65 #define PWDCOLUMN 60
66 #define PWDATTR 0x74
67 #define EDITPROMPT 21
69 void keys_handler(t_menusystem *ms, t_menuitem *mi,unsigned int scancode)
71 char nc;
73 if ((scancode >> 8) == F1) { // If scancode of F1
74 runhelpsystem(mi->helpid);
76 // If user hit TAB, and item is an "executable" item
77 // and user has privileges to edit it, edit it in place.
78 if (((scancode & 0xFF) == 0x09) && (mi->action == OPT_RUN) &&
79 (isallowed(username,"editcmd") || isallowed(username,"root"))) {
80 nc = getnumcols();
81 // User typed TAB and has permissions to edit command line
82 gotoxy(EDITPROMPT,1,ms->menupage);
83 csprint("Command line:",0x07);
84 editstring(mi->data,ACTIONLEN);
85 gotoxy(EDITPROMPT,1,ms->menupage);
86 cprint(' ',0x07,nc-1,ms->menupage);
90 t_handler_return login_handler(t_menusystem *ms, t_menuitem *mi)
92 (void)mi; // Unused
93 char pwd[40];
94 char login[40];
95 char nc;
96 t_handler_return rv;
98 if (mi->item == loginstr) { /* User wants to login */
99 nc = getnumcols();
100 gotoxy(PWDPROMPT,1,ms->menupage);
101 csprint("Enter Username: ",0x07);
102 getstring(login, sizeof username);
103 gotoxy(PWDPROMPT,1,ms->menupage);
104 cprint(' ',0x07,nc,ms->menupage);
105 csprint("Enter Password: ",0x07);
106 getpwd(pwd, sizeof pwd);
107 gotoxy(PWDPROMPT,1,ms->menupage);
108 cprint(' ',0x07,nc,ms->menupage);
110 if (authenticate_user(login,pwd))
112 strcpy(username,login);
113 mi->item = logoutstr; // Change item to read "Logout"
115 else strcpy(username,GUEST_USER);
117 else // User needs to logout
119 strcpy(username,GUEST_USER);
120 mi->item = loginstr;
123 if (strcmp(username,GUEST_USER)==0)
125 prepopt->action = OPT_INACTIVE;
126 secret->action = OPT_INVISIBLE;
128 else
130 prepopt->action = OPT_SUBMENU;
131 prepopt->itemdata.radiomenunum = PREPMENU;
132 secret->action = OPT_SUBMENU;
133 secret->itemdata.submenunum = SECRETMENU;
135 rv.valid = 0;
136 rv.refresh = 1;
137 return rv;
140 void msys_handler(t_menusystem *ms, t_menuitem *mi)
142 char nc;
143 void *v;
144 nc = getnumcols(); // Get number of columns
146 gotoxy(PWDLINE,PWDCOLUMN,ms->menupage);
147 csprint("User: ",PWDATTR);
148 cprint(ms->fillchar,ms->fillattr,sizeof username,ms->menupage);
149 gotoxy(PWDLINE,PWDCOLUMN +6,ms->menupage);
150 csprint(username,PWDATTR);
152 if (mi->parindex != PREPMENU) // If we are not in the PREP MENU
154 gotoxy(INFLINE,0,ms->menupage);
155 cprint(' ',0x07,nc,ms->menupage);
156 gotoxy(INFLINE+1,0,ms->menupage);
157 cprint(' ',0x07,nc,ms->menupage);
158 return;
160 strcpy (infoline," ");
161 if (flags.baseurl) strcat(infoline,"baseurl=http://192.168.11.12/gui ");
162 if (flags.mountcd) strcat(infoline,"mountcd=yes ");
163 v = (void *)network->data;
164 if (v!=NULL) // Some network option specified
166 strcat(infoline,"network=");
167 strcat(infoline,(char *)(((t_menuitem *)v)->data));
169 if (flags.winrep) strcat(infoline,"repair=win ");
170 if (flags.linrep) strcat(infoline,"repair=lin ");
172 gotoxy(INFLINE,0,ms->menupage);
173 cprint(' ',0x07,nc,ms->menupage);
174 gotoxy(INFLINE+1,0,ms->menupage);
175 cprint(' ',0x07,nc,ms->menupage);
176 gotoxy(INFLINE,0,ms->menupage);
177 csprint("Kernel Arguments:",0x07);
178 gotoxy(INFLINE,17,ms->menupage);
179 csprint(infoline,0x07);
182 t_handler_return network_handler(t_menusystem *ms, t_menuitem *mi)
184 // mi=network since this is handler only for that.
185 (void)ms; // Unused
187 if (mi->data == (void *)none) mi->item = nonet;
188 if (mi->data == (void *)stat) mi->item = statnet;
189 if (mi->data == (void *)dhcp) mi->item = dhcpnet;
190 return ACTION_INVALID; // VALID or INVALID does not matter
193 t_handler_return checkbox_handler(t_menusystem *ms, t_menuitem *mi)
195 (void)ms; /* Unused */
197 if (mi->action != OPT_CHECKBOX) return ACTION_INVALID;
199 if (strcmp(mi->data,"baseurl") == 0) flags.baseurl = (mi->itemdata.checked ? 1 : 0);
200 if (strcmp(mi->data,"winrepair") == 0) {
201 if (mi->itemdata.checked)
203 flags.winrep = 1;
204 linrep->action = OPT_INACTIVE;
206 else
208 flags.winrep = 0;
209 linrep->action = OPT_CHECKBOX;
212 if (strcmp(mi->data,"linrepair") == 0) {
213 if (mi->itemdata.checked)
215 flags.linrep = 1;
216 winrep->action = OPT_INACTIVE;
218 else
220 flags.winrep = 0;
221 winrep->action = OPT_CHECKBOX;
224 if (strcmp(mi->data,"mountcd") == 0) flags.mountcd = (mi->itemdata.checked ? 1 : 0);
225 return ACTION_VALID;
229 Clears keyboard buffer and then
230 wait for stepsize*numsteps milliseconds for user to press any key
231 checks for keypress every stepsize milliseconds.
232 Returns: 1 if user pressed a key (not read from the buffer),
233 0 if time elapsed
235 int checkkeypress(int stepsize, int numsteps)
237 int i;
238 clearkbdbuf();
239 for (i=0; i < numsteps; i++)
241 if (checkkbdbuf()) return 1;
242 sleep(stepsize);
244 return 0;
247 int main()
249 t_menuitem * curr;
250 char cmd[160];
251 char ip[30];
253 // Set default username as guest
254 strcpy(username,GUEST_USER);
256 // Switch video mode here
257 // setvideomode(0x18); // or whatever mode you want
259 // Choose the default title and setup default values for all attributes....
260 init_passwords("/isolinux/password");
261 init_help("/isolinux/help");
262 init_menusystem(NULL);
263 set_window_size(1,1,20,78); // Leave some space around
265 // Choose the default values for all attributes and char's
266 // -1 means choose defaults (Actually the next 4 lines are not needed)
267 //set_normal_attr (-1,-1,-1,-1);
268 //set_status_info (-1,-1); // Display status on the last line
269 //set_title_info (-1,-1);
270 //set_misc_info(-1,-1,-1,-1);
272 // Register the menusystem handler
273 reg_handler(HDLR_SCREEN,&msys_handler);
274 reg_handler(HDLR_KEYS,&keys_handler);
275 // Register the ontimeout handler, with a time out of 10 seconds
276 reg_ontimeout(ontimeout,1000,0);
278 NETMENU = add_menu(" Init Network ",-1);
279 none = add_item("<N>one","Dont start network",OPT_RADIOITEM,"no ",0);
280 dhcp = add_item("<d>hcp","Use DHCP",OPT_RADIOITEM,"dhcp ",0);
281 stat = add_item("<s>tatic","Use static IP I will specify later",OPT_RADIOITEM,"static ",0);
283 TESTING = add_menu(" Testing ",-1);
284 set_menu_pos(5,55);
285 add_item("<M>emory Test","Perform extensive memory testing",OPT_RUN, "memtest",0);
286 add_item("<I>nvisible","You dont see this",OPT_INVISIBLE,"junk",0);
287 add_item("<E>xit this menu","Go one level up",OPT_EXITMENU,"exit",0);
289 RESCUE = add_menu(" Rescue Options ",-1);
290 add_item("<L>inux Rescue","linresc",OPT_RUN,"linresc",0);
291 add_item("<D>os Rescue","dosresc",OPT_RUN,"dosresc",0);
292 add_item("<W>indows Rescue","winresc",OPT_RUN,"winresc",0);
293 add_item("<E>xit this menu","Go one level up",OPT_EXITMENU,"exit",0);
295 PREPMENU = add_menu(" Prep options ",-1);
296 baseurl = add_item("<b>aseurl by IP?","Specify gui baseurl by IP address",OPT_CHECKBOX,"baseurl",0);
297 mountcd = add_item("<m>ountcd?","Mount the cdrom drive?",OPT_CHECKBOX,"mountcd",0);
298 network = add_item(dhcpnet,"How to initialise network device?",OPT_RADIOMENU,NULL,NETMENU);
299 add_sep();
300 winrep = add_item("Reinstall <w>indows","Re-install the windows side of a dual boot setup",OPT_CHECKBOX,"winrepair",0);
301 linrep = add_item("Reinstall <l>inux","Re-install the linux side of a dual boot setup",OPT_CHECKBOX,"linrepair",0);
302 add_sep();
303 runprep = add_item("<R>un prep now","Execute prep with the above options",OPT_RUN,"prep",0);
304 add_item("<E>xit this menu","Go up one level",OPT_EXITMENU,"exitmenu",0);
305 baseurl->handler = &checkbox_handler;
306 mountcd->handler = &checkbox_handler;
307 winrep->handler = &checkbox_handler;
308 linrep->handler = &checkbox_handler;
309 network->handler = &network_handler;
310 flags.baseurl = 0;
311 flags.mountcd = 0;
312 flags.winrep = 0;
313 flags.linrep = 0;
315 SECRETMENU = add_menu(" Secret Menu ",-1);
316 add_item("secret 1","Secret",OPT_RUN,"A",0);
317 add_item("secret 2","Secret",OPT_RUN,"A",0);
319 LONGMENU = add_menu(" Long Menu ",40); // Override default here
320 add_item("<A>a","Aa",OPT_RUN,"A",0);
321 add_item("<B>b","Ab",OPT_RUN,"A",0);
322 add_item("<C>","A",OPT_RUN,"A",0);
323 add_item("<D>","A",OPT_RUN,"A",0);
324 add_item("<E>","A",OPT_RUN,"A",0);
325 add_item("<F>","A",OPT_RUN,"A",0);
326 add_item("<G>","A",OPT_RUN,"A",0);
327 add_item("<H>","A",OPT_RUN,"A",0);
328 add_item("<I>","A",OPT_RUN,"A",0);
329 add_item("<J>","A",OPT_RUN,"A",0);
330 add_item("<K>","A",OPT_RUN,"A",0);
331 add_item("<L>","A",OPT_RUN,"A",0);
332 add_item("<J>","A",OPT_RUN,"A",0);
333 add_item("<K>","A",OPT_RUN,"A",0);
334 add_item("<L>","A",OPT_RUN,"A",0);
335 add_item("<M>","A",OPT_RUN,"A",0);
336 add_item("<N>","A",OPT_RUN,"A",0);
337 add_item("<O>","A",OPT_RUN,"A",0);
338 add_item("<P>","A",OPT_RUN,"A",0);
339 add_item("<Q>","A",OPT_RUN,"A",0);
340 add_item("<R>","A",OPT_RUN,"A",0);
341 add_item("<S>","A",OPT_RUN,"A",0);
342 add_item("<T>","A",OPT_RUN,"A",0);
343 add_item("<U>","A",OPT_RUN,"A",0);
344 add_item("<V>","A",OPT_RUN,"A",0);
345 add_item("<W>","A",OPT_RUN,"A",0);
346 add_item("<X>","A",OPT_RUN,"A",0);
347 add_item("<Y>","A",OPT_RUN,"A",0);
348 add_item("<Z>","A",OPT_RUN,"A",0);
349 add_item("<1>","A",OPT_RUN,"A",0);
350 add_item("<2>","A",OPT_RUN,"A",0);
351 add_item("<3>","A",OPT_RUN,"A",0);
352 add_item("<4>","A",OPT_RUN,"A",0);
353 add_item("<5>","A",OPT_RUN,"A",0);
354 add_item("<6>","A",OPT_RUN,"A",0);
355 add_item("<7>","A",OPT_RUN,"A",0);
356 add_item("<8>","A",OPT_RUN,"A",0);
357 add_item("<9>","A",OPT_RUN,"A",0);
359 MAIN = add_menu(" Main Menu ",8);
360 curr = add_item(loginstr,"Login as a privileged user",OPT_RUN,NULL,0);
361 set_item_options(-1,23);
362 curr->handler = &login_handler;
364 add_item("<P>repare","prep",OPT_RUN,"prep",0);
365 set_item_options(-1,24);
366 prepopt = add_item("<P>rep options...","Options for prep image: Requires authenticated user",OPT_INACTIVE,NULL,PREPMENU);
367 set_item_options(-1,25);
369 add_item("<R>escue options...","Troubleshoot a system",OPT_SUBMENU,NULL,RESCUE);
370 set_item_options(-1,26);
371 add_item("<T>esting...","Options to test hardware",OPT_SUBMENU,NULL,TESTING);
372 set_item_options(-1,27);
373 add_item("<L>ong Menu...","test menu system",OPT_SUBMENU,NULL,LONGMENU);
374 set_item_options(-1,28);
375 secret = add_item("<S>ecret Menu...","Secret menu",OPT_INVISIBLE,NULL,SECRETMENU);
376 set_item_options(-1,29);
377 add_item("<E>xit to prompt", "Exit the menu system", OPT_EXITMENU, "exit", 0);
378 set_item_options(-1,30);
379 csprint("Press any key within 5 seconds to show menu...",0x07);
380 if (!checkkeypress(100,50)) // Granularity of 100 milliseconds
382 csprint("Sorry! Time's up.\r\n",0x07);
383 return 1;
385 else clearkbdbuf(); // Just in case user pressed something important
386 curr = showmenus(MAIN);
387 if (curr)
389 if (curr->action == OPT_RUN)
391 strcpy(cmd,curr->data);
392 if (curr == runprep)
394 strcat(cmd,infoline);
395 if (network->data == (void *)stat) // We want static
397 csprint("Enter IP address (last two octets only): ",0x07);
398 strcpy(ip, "Junk");
399 editstring(ip, sizeof ip);
400 strcat(cmd,"ipaddr=192.168.");
401 strcat(cmd,ip);
404 if (issyslinux())
405 runsyslinuxcmd(cmd);
406 else csprint(cmd,0x07);
407 return 1; // Should not happen when run from SYSLINUX
410 // If user quits the menu system, control comes here
411 // If you want to execute some specific command uncomment the next two lines
413 // if (syslinux) runcommand(YOUR_COMMAND_HERE);
414 // else csprint(YOUR_COMMAND_HERE,0x07);
416 // Deallocate space used for these data structures
417 close_passwords();
418 close_help();
419 close_menusystem();
421 // Return to prompt
422 return 0;