PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/syslinux/menu/complex.c

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