1 All the lines in this file not in between --something BEGINS-- and --something ENDS--
2 is ignored completely and will not make it into the generated C file
4 This file has sections of C code each section delimited by --secname BEGINS--
5 and --secname ENDS--. In the generated C code certain section may be used multiple
6 times. Currently the different section which must be defined are
8 header, system, item, login and footer
10 Any additional sections you define will be processed but will probably not make it
11 to the C code if you do not modify menugen.py to use it.
13 header and footer go through unmolested. The remaining are % substituted using
14 python rules. Basically it means %(var)s gets replaced by the value of the variable
15 "var" which is a processed form of what is read from the .menu file
17 NOTE: There is absolutely no C code in the python script, so you are free to
18 modify this template to suit your needs
21 /* -*- c -*- ------------------------------------------------------------- *
23 * Copyright 2004-2006 Murali Krishnan Ganapathy - All Rights Reserved
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
28 * Boston MA 02111-1307, USA; either version 2 of the License, or
29 * (at your option) any later version; incorporated herein by reference.
31 * ----------------------------------------------------------------------- */
34 #define NULL ((void *) 0)
39 #include "passwords.h"
44 #define MAX_CMD_LINE_LENGTH 514
46 typedef struct s_xtra {
47 long ipappend; // Stores the ipappend flag to send (useful for PXELINUX only)
48 char *argsmenu; // Stores the name of menu which contains options for the given RUN item
49 char *perms; // stores the permissions required to activate the item
52 typedef t_xtra *pt_xtra; // Pointer to extra datastructure
54 // Types of dot commands for which caller takes responsibility of handling
55 // In some case some commands may not make sense, it is up to the caller
56 // to handle cases which do not make sense
57 typedef enum {QUIT_CMD, REPEAT_CMD, ENTER_CMD, ESCAPE_CMD} t_dotcmd;
60 /*----------------- Global Variables */
63 #define GUEST_USER "guest"
65 // for local commands. return value of execdotcmd
69 char username[12]; // Name of user currently using the system
71 int PWD_ROW; // Line number where user authentication happens
72 int EDIT_ROW; // row where User Tab
74 char loginstr[] = "<L>ogin ";
77 int vmode; // The video mode we want to be in
78 char timeoutcmd[MAX_CMD_LINE_LENGTH]; // Command to execute on timeout
79 char totaltimeoutcmd[MAX_CMD_LINE_LENGTH]; // Command to execute on totaltimeout
81 char QUITSTR[] = ".quit"; // same as exit
82 char IGNORESTR[]=".ignore"; // same as repeat, wait
84 /*---------------- End globals */
86 // returns pointer to first non-space char
87 // and advances end of str by removing trailing spaces
88 char * strip(char *str)
91 if (!str) return NULL;
97 // mark start of string or record the last visited non-space char
98 if (!s) s=p; else e=p;
102 *(++e)='\0'; // kill string earlier
106 // executes a list of % separated commands
107 // non-dot commands are assumed to be syslinux commands
108 // All syslinux commands are appended with the contents of kerargs
109 // If it fails (kernel not found) then the next one is tried in the
111 // returns QUIT_CMD or RPT_CMD
112 t_dotcmd execdotcmd(const char *cmd, char *defcmd, const char *kerargs)
114 char cmdline[MAX_CMD_LINE_LENGTH];
115 char dotcmd[MAX_CMD_LINE_LENGTH];
116 char *curr,*next,*p,*args;
122 while (*next) { // if something to do
124 p = strchr(next,'%');
127 while (*p == ' ') p--;
128 *(++p)='\0'; // remove trailing spaces
130 if (*defcmd) { // execute defcmd next
132 defcmd=NULL; // exec def cmd only once
135 // now we just need to execute the command "curr"
137 if (curr[0] != '.') { // just run the kernel
138 strcpy(cmdline,curr);
139 if (kerargs) strcat(cmdline,kerargs);
140 runsyslinuximage(cmdline,0); // No IPAppend
141 } else { // We have a DOT command
142 // split command into command and args (may be empty)
144 while ( (*args != ' ') && (*args != '\0') ) args++;
145 if (*args) { // found a space
147 while (*args == ' ') args++; // skip over spaces
149 if ( (strcmp(curr,".exit")==0) ||
150 (strcmp(curr,".quit")==0)
153 if ( (strcmp(curr,".repeat")==0) ||
154 (strcmp(curr,".ignore")==0) ||
155 (strcmp(curr,".wait")==0)
158 if (strcmp(curr,".beep")==0) {
159 if ((args) && ('0' <= args[0]) && (args[0] <= '9'))
162 for (;ctr>0; ctr--) beep();
164 if (strcmp(curr,".help")==0) runhelp(args);
167 return RPT_CMD; // by default we do not quit
171 TIMEOUTCODE timeout(const char *cmd)
174 c = execdotcmd(cmd,".wait",NULL);
185 TIMEOUTCODE ontimeout()
187 return timeout(timeoutcmd);
190 TIMEOUTCODE ontotaltimeout()
192 return timeout(totaltimeoutcmd);
195 void keys_handler(t_menusystem *ms, t_menuitem *mi,unsigned int scancode)
199 if ( ((scancode >> 8) == F1) && (mi->helpid != 0xFFFF) ) { // If scancode of F1 and non-trivial helpid
200 runhelpsystem(mi->helpid);
203 // If user hit TAB, and item is an "executable" item
204 // and user has privileges to edit it, edit it in place.
205 if (((scancode & 0xFF) == 0x09) && (mi->action == OPT_RUN) &&
206 (EDIT_ROW < getnumrows()) && (EDIT_ROW > 0) &&
207 (isallowed(username,"editcmd") || isallowed(username,"root"))) {
209 // User typed TAB and has permissions to edit command line
210 gotoxy(EDIT_ROW,1,ms->menupage);
211 csprint("Command line:",0x07);
212 editstring(mi->data,ACTIONLEN);
213 gotoxy(EDIT_ROW,1,ms->menupage);
214 cprint(' ',0x07,nc-1,ms->menupage);
218 t_handler_return login_handler(t_menusystem *ms, t_menuitem *mi)
227 if (PWD_ROW < 0) return rv; // No need to authenticate
229 if (mi->item == loginstr) { /* User wants to login */
231 gotoxy(PWD_ROW,1,ms->menupage);
232 csprint("Enter Username: ",0x07);
233 getstring(login, sizeof username);
234 gotoxy(PWD_ROW,1,ms->menupage);
235 cprint(' ',0x07,nc,ms->menupage);
236 csprint("Enter Password: ",0x07);
237 getpwd(pwd, sizeof pwd);
238 gotoxy(PWD_ROW,1,ms->menupage);
239 cprint(' ',0x07,nc,ms->menupage);
241 if (authenticate_user(login,pwd))
243 strcpy(username,login);
244 strcpy(logoutstr,"<L>ogout ");
245 strcat(logoutstr,username);
246 mi->item = logoutstr; // Change item to read "Logout"
247 rv.refresh = 1; // refresh the screen (as item contents changed)
249 else strcpy(username,GUEST_USER);
251 else // User needs to logout
253 strcpy(username,GUEST_USER);
254 strcpy(logoutstr,"");
261 t_handler_return check_perms(t_menusystem *ms, t_menuitem *mi)
266 (void) ms; // To keep compiler happy
268 x = (pt_xtra) mi->extra_data;
269 perms = ( x ? x->perms : NULL);
270 if (!perms) return ACTION_VALID;
272 if (isallowed(username,"root") || isallowed(username,perms)) // If allowed
274 else return ACTION_INVALID;
277 // Compute the full command line to add and if non-trivial
278 // prepend the string prepend to final command line
279 // Assume cmdline points to buffer long enough to hold answer
280 void gencommand(pt_menuitem mi, char *cmdline)
284 strcat(cmdline,mi->data);
285 x = (pt_xtra) mi->extra_data;
286 if ( (x) && (x->argsmenu)) gen_append_line(x->argsmenu,cmdline);
290 // run the given command together with additional options which may need passing
291 void runcommand(pt_menuitem mi)
297 line = (char *)malloc(sizeof(char)*MAX_CMD_LINE_LENGTH);
299 x = (pt_xtra) mi->extra_data;
300 ipappend = (x ? x->ipappend : 0);
302 runsyslinuximage(line,x->ipappend);
306 // set the extra info for the specified menu item.
307 void set_xtra(pt_menuitem mi, const char *argsmenu, const char *perms, unsigned int helpid, long ipappend)
310 int bad_argsmenu, bad_perms, bad_ipappend;
312 mi->extra_data = NULL; // initalize
313 mi->helpid = helpid; // set help id
315 if (mi->action != OPT_RUN) return;
317 bad_argsmenu = bad_perms = bad_ipappend = 0;
318 if ( (argsmenu==NULL) || (strlen(argsmenu)==0)) bad_argsmenu = 1;
319 if ( (perms==NULL) || (strlen(perms)==0)) bad_perms = 1;
320 if ( ipappend==0) bad_ipappend = 1;
322 if (bad_argsmenu && bad_perms && bad_ipappend) return;
324 xtra = (pt_xtra) malloc(sizeof(t_xtra));
325 mi->extra_data = (void *) xtra;
326 xtra->argsmenu = xtra->perms = NULL;
327 xtra->ipappend = ipappend;
329 xtra->argsmenu = (char *) malloc(sizeof(char)*(strlen(argsmenu)+1));
330 strcpy(xtra->argsmenu,argsmenu);
333 xtra->perms = (char *) malloc(sizeof(char)*(strlen(perms)+1));
334 strcpy(xtra->perms,perms);
335 mi->handler = &check_perms;
343 char exitcmd[MAX_CMD_LINE_LENGTH];
344 char exitcmdroot[MAX_CMD_LINE_LENGTH];
345 char onerrcmd[MAX_CMD_LINE_LENGTH];
346 char startfile[MAX_CMD_LINE_LENGTH];
347 char *ecmd; // effective exit command or onerrorcmd
349 char skipcmd[MAX_CMD_LINE_LENGTH];
350 int timeout; // time in multiples of 0.1 seconds
351 int totaltimeout; // time in multiples of 0.1 seconds
352 t_dotcmd dotrv; // to store the return value of execdotcmd
355 strcpy(username,GUEST_USER);
358 /* ---- Initializing menu system parameters --- */
359 vmode = %(videomode)s;
360 skipbits = %(skipcondn)s;
361 PWD_ROW = %(pwdrow)s;
362 EDIT_ROW = %(editrow)s;
363 strcpy(onerrcmd,"%(onerrorcmd)s");
364 strcpy(exitcmd,"%(exitcmd)s");
365 strcpy(exitcmdroot,"%(exitcmdroot)s");
366 // If not specified exitcmdroot = exitcmd
367 if (exitcmdroot[0] == '\0') strcpy(exitcmdroot,exitcmd);
369 timeout = %(timeout)s;
370 strcpy(timeoutcmd,"%(timeoutcmd)s");
371 totaltimeout = %(totaltimeout)s;
372 strcpy(totaltimeoutcmd,"%(totaltimeoutcmd)s");
373 strcpy(startfile,"%(startfile)s");
375 init_help("%(helpdir)s");
376 init_passwords("%(pwdfile)s");
377 init_menusystem("%(title)s");
378 set_window_size(%(top)s,%(left)s,%(bot)s,%(right)s);
380 // Register the ontimeout handler, with a time out of 10 seconds
381 reg_ontimeout(ontimeout,timeout*10,0);
382 reg_ontotaltimeout(ontotaltimeout,totaltimeout*10);
384 // Register menusystem handlers
385 reg_handler(HDLR_KEYS,&keys_handler);
386 /* ---- End of initialization --- */
390 curr = add_item("%(item)s","%(info)s",%(type)s,"%(data)s",%(state)d);
391 set_xtra(curr,"%(argsmenu)s","%(perms)s",%(helpid)d,%(ipappend)d); // Set associated extra info
392 set_shortcut(%(shortcut)s);
396 curr = add_item(loginstr,"Login/Logout of authentication system",OPT_RUN,NULL,0);
397 curr->helpid = %(helpid)d;
398 curr->handler = &login_handler;
402 /* ------- MENU %(name)s ----- */
403 add_named_menu("%(name)s","%(title)s",-1);
406 /* ------- END OF MENU declarations ----- */
408 // Check if we should skip the menu altogether
409 quit = 0; // Dont skip the menu
410 if (getshiftflags() & skipbits) { // we must skip the menu altogther and execute skipcmd
411 dotrv = execdotcmd(skipcmd,".beep%.exit",NULL); // Worst case we beep and exit
412 if (dotrv == QUIT_CMD) quit = 1;
415 // Switch vide mode if required
416 if (vmode != 0xFF) setvideomode(vmode);
418 // Do we have a startfile to display?
419 if (startfile[0] != '\0') runhelp(startfile);
422 while (quit == 0) { // As long as quit is zero repeat
423 curr = showmenus(find_menu_num("main")); // Initial menu is the one called "main"
426 if (curr->action == OPT_RUN) {
427 ecmd = (char *)malloc(sizeof(char)*MAX_CMD_LINE_LENGTH);
428 gencommand(curr,ecmd);
429 temp = (curr->extra_data ? ((pt_xtra)curr->extra_data)->ipappend : 0);
430 runsyslinuximage(ecmd,temp);
431 // kernel not found so execute the appropriate dot command
432 dotrv = execdotcmd(onerrcmd,".quit",ecmd); // pass bad cmdline as arg
433 if (dotrv== QUIT_CMD) quit = 1;
434 free(ecmd); ecmd = NULL;
436 else csprint("Error in programming!",0x07);
438 // find effective exit command
439 ecmd = ( isallowed(username,"root") ? exitcmdroot : exitcmd);
440 dotrv = execdotcmd(ecmd,".repeat",NULL);
441 quit = (dotrv == QUIT_CMD ? 1 : 0); // should we exit now
445 // Deallocate space used and quit