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"
45 #define MAX_CMD_LINE_LENGTH 514
47 typedef struct s_xtra {
48 long ipappend; // Stores the ipappend flag to send (useful for PXELINUX only)
49 char *argsmenu; // Stores the name of menu which contains options for the given RUN item
50 char *perms; // stores the permissions required to activate the item
53 typedef t_xtra *pt_xtra; // Pointer to extra datastructure
55 // Types of dot commands for which caller takes responsibility of handling
56 // In some case some commands may not make sense, it is up to the caller
57 // to handle cases which do not make sense
58 typedef enum {QUIT_CMD, REPEAT_CMD, ENTER_CMD, ESCAPE_CMD} t_dotcmd;
61 /*----------------- Global Variables */
64 #define GUEST_USER "guest"
66 // for local commands. return value of execdotcmd
70 char username[12]; // Name of user currently using the system
72 int PWD_ROW; // Line number where user authentication happens
73 int EDIT_ROW; // row where User Tab
75 char loginstr[] = "<L>ogin ";
78 int vmode; // The video mode we want to be in
79 char timeoutcmd[MAX_CMD_LINE_LENGTH]; // Command to execute on timeout
80 char totaltimeoutcmd[MAX_CMD_LINE_LENGTH]; // Command to execute on totaltimeout
82 char QUITSTR[] = ".quit"; // same as exit
83 char IGNORESTR[]=".ignore"; // same as repeat, wait
85 /*---------------- End globals */
87 // returns pointer to first non-space char
88 // and advances end of str by removing trailing spaces
89 char * strip(char *str)
92 if (!str) return NULL;
98 // mark start of string or record the last visited non-space char
99 if (!s) s=p; else e=p;
103 *(++e)='\0'; // kill string earlier
107 // executes a list of % separated commands
108 // non-dot commands are assumed to be syslinux commands
109 // All syslinux commands are appended with the contents of kerargs
110 // If it fails (kernel not found) then the next one is tried in the
112 // returns QUIT_CMD or RPT_CMD
113 t_dotcmd execdotcmd(const char *cmd, char *defcmd, const char *kerargs)
115 char cmdline[MAX_CMD_LINE_LENGTH];
116 char dotcmd[MAX_CMD_LINE_LENGTH];
117 char *curr,*next,*p,*args;
123 while (*next) { // if something to do
125 p = strchr(next,'%');
128 while (*p == ' ') p--;
129 *(++p)='\0'; // remove trailing spaces
131 if (*defcmd) { // execute defcmd next
133 defcmd=NULL; // exec def cmd only once
136 // now we just need to execute the command "curr"
138 if (curr[0] != '.') { // just run the kernel
139 strcpy(cmdline,curr);
140 if (kerargs) strcat(cmdline,kerargs);
141 runsyslinuximage(cmdline,0); // No IPAppend
142 } else { // We have a DOT command
143 // split command into command and args (may be empty)
145 while ( (*args != ' ') && (*args != '\0') ) args++;
146 if (*args) { // found a space
148 while (*args == ' ') args++; // skip over spaces
150 if ( (strcmp(curr,".exit")==0) ||
151 (strcmp(curr,".quit")==0)
154 if ( (strcmp(curr,".repeat")==0) ||
155 (strcmp(curr,".ignore")==0) ||
156 (strcmp(curr,".wait")==0)
159 if (strcmp(curr,".beep")==0) {
160 if ((args) && ('0' <= args[0]) && (args[0] <= '9'))
163 for (;ctr>0; ctr--) beep();
165 if (strcmp(curr,".help")==0) runhelp(args);
168 return RPT_CMD; // by default we do not quit
172 TIMEOUTCODE timeout(const char *cmd)
175 c = execdotcmd(cmd,".wait",NULL);
186 TIMEOUTCODE ontimeout(void)
188 return timeout(timeoutcmd);
191 TIMEOUTCODE ontotaltimeout(void)
193 return timeout(totaltimeoutcmd);
196 void keys_handler(t_menusystem * ms __attribute__ (( unused )), t_menuitem * mi, int scancode)
200 if (getscreensize(1, &nr, &nc)) {
201 /* Unknown screen size? */
206 if ( (scancode == KEY_F1) && (mi->helpid != 0xFFFF) ) { // If scancode of F1 and non-trivial helpid
207 runhelpsystem(mi->helpid);
210 // If user hit TAB, and item is an "executable" item
211 // and user has privileges to edit it, edit it in place.
212 if ((scancode == KEY_TAB) && (mi->action == OPT_RUN) &&
213 (EDIT_ROW < nr) && (EDIT_ROW > 0) &&
214 (isallowed(username,"editcmd") || isallowed(username,"root"))) {
215 // User typed TAB and has permissions to edit command line
217 csprint("Command line:",0x07);
218 editstring(mi->data,ACTIONLEN);
220 cprint(' ',0x07,nc-1);
224 t_handler_return login_handler(t_menusystem *ms, t_menuitem *mi)
235 if (PWD_ROW < 0) return rv; // No need to authenticate
237 if (mi->item == loginstr) { /* User wants to login */
238 if (getscreensize(1, &nr, &nc)) {
239 /* Unknown screen size? */
245 csprint("Enter Username: ",0x07);
246 getstring(login, sizeof username);
249 csprint("Enter Password: ",0x07);
250 getpwd(pwd, sizeof pwd);
254 if (authenticate_user(login,pwd))
256 strcpy(username,login);
257 strcpy(logoutstr,"<L>ogout ");
258 strcat(logoutstr,username);
259 mi->item = logoutstr; // Change item to read "Logout"
260 rv.refresh = 1; // refresh the screen (as item contents changed)
262 else strcpy(username,GUEST_USER);
264 else // User needs to logout
266 strcpy(username,GUEST_USER);
267 strcpy(logoutstr,"");
274 t_handler_return check_perms(t_menusystem *ms, t_menuitem *mi)
279 (void) ms; // To keep compiler happy
281 x = (pt_xtra) mi->extra_data;
282 perms = ( x ? x->perms : NULL);
283 if (!perms) return ACTION_VALID;
285 if (isallowed(username,"root") || isallowed(username,perms)) // If allowed
287 else return ACTION_INVALID;
290 // Compute the full command line to add and if non-trivial
291 // prepend the string prepend to final command line
292 // Assume cmdline points to buffer long enough to hold answer
293 void gencommand(pt_menuitem mi, char *cmdline)
297 strcat(cmdline,mi->data);
298 x = (pt_xtra) mi->extra_data;
299 if ( (x) && (x->argsmenu)) gen_append_line(x->argsmenu,cmdline);
303 // run the given command together with additional options which may need passing
304 void runcommand(pt_menuitem mi)
310 line = (char *)malloc(sizeof(char)*MAX_CMD_LINE_LENGTH);
312 x = (pt_xtra) mi->extra_data;
313 ipappend = (x ? x->ipappend : 0);
315 runsyslinuximage(line,ipappend);
319 // set the extra info for the specified menu item.
320 void set_xtra(pt_menuitem mi, const char *argsmenu, const char *perms, unsigned int helpid, long ipappend)
323 int bad_argsmenu, bad_perms, bad_ipappend;
325 mi->extra_data = NULL; // initalize
326 mi->helpid = helpid; // set help id
328 if (mi->action != OPT_RUN) return;
330 bad_argsmenu = bad_perms = bad_ipappend = 0;
331 if ( (argsmenu==NULL) || (strlen(argsmenu)==0)) bad_argsmenu = 1;
332 if ( (perms==NULL) || (strlen(perms)==0)) bad_perms = 1;
333 if ( ipappend==0) bad_ipappend = 1;
335 if (bad_argsmenu && bad_perms && bad_ipappend) return;
337 xtra = (pt_xtra) malloc(sizeof(t_xtra));
338 mi->extra_data = (void *) xtra;
339 xtra->argsmenu = xtra->perms = NULL;
340 xtra->ipappend = ipappend;
342 xtra->argsmenu = (char *) malloc(sizeof(char)*(strlen(argsmenu)+1));
343 strcpy(xtra->argsmenu,argsmenu);
346 xtra->perms = (char *) malloc(sizeof(char)*(strlen(perms)+1));
347 strcpy(xtra->perms,perms);
348 mi->handler = &check_perms;
356 char exitcmd[MAX_CMD_LINE_LENGTH];
357 char exitcmdroot[MAX_CMD_LINE_LENGTH];
358 char onerrcmd[MAX_CMD_LINE_LENGTH];
359 char startfile[MAX_CMD_LINE_LENGTH];
360 char *ecmd; // effective exit command or onerrorcmd
362 char skipcmd[MAX_CMD_LINE_LENGTH];
363 int timeout; // time in multiples of 0.1 seconds
364 int totaltimeout; // time in multiples of 0.1 seconds
365 t_dotcmd dotrv; // to store the return value of execdotcmd
368 strcpy(username,GUEST_USER);
371 /* ---- Initializing menu system parameters --- */
372 vmode = %(videomode)s;
373 skipbits = %(skipcondn)s;
374 PWD_ROW = %(pwdrow)s;
375 EDIT_ROW = %(editrow)s;
376 strcpy(onerrcmd,"%(onerrorcmd)s");
377 strcpy(exitcmd,"%(exitcmd)s");
378 strcpy(exitcmdroot,"%(exitcmdroot)s");
379 // If not specified exitcmdroot = exitcmd
380 if (exitcmdroot[0] == '\0') strcpy(exitcmdroot,exitcmd);
382 timeout = %(timeout)s;
383 strcpy(timeoutcmd,"%(timeoutcmd)s");
384 totaltimeout = %(totaltimeout)s;
385 strcpy(totaltimeoutcmd,"%(totaltimeoutcmd)s");
386 strcpy(startfile,"%(startfile)s");
388 init_help("%(helpdir)s");
389 init_passwords("%(pwdfile)s");
390 init_menusystem("%(title)s");
391 set_window_size(%(top)s,%(left)s,%(bot)s,%(right)s);
393 // Register the ontimeout handler, with a time out of 10 seconds
394 reg_ontimeout(ontimeout,timeout*10,0);
395 reg_ontotaltimeout(ontotaltimeout,totaltimeout*10);
397 // Register menusystem handlers
398 reg_handler(HDLR_KEYS,&keys_handler);
399 /* ---- End of initialization --- */
403 curr = add_item("%(item)s","%(info)s",%(type)s,"%(data)s",%(state)d);
404 set_xtra(curr,"%(argsmenu)s","%(perms)s",%(helpid)d,%(ipappend)d); // Set associated extra info
405 set_shortcut(%(shortcut)s);
409 curr = add_item(loginstr,"Login/Logout of authentication system",OPT_RUN,NULL,0);
410 curr->helpid = %(helpid)d;
411 curr->handler = &login_handler;
415 /* ------- MENU %(name)s ----- */
416 add_named_menu("%(name)s","%(title)s",-1);
419 /* ------- END OF MENU declarations ----- */
421 // Check if we should skip the menu altogether
422 quit = 0; // Dont skip the menu
423 if (getshiftflags() & skipbits) { // we must skip the menu altogther and execute skipcmd
424 dotrv = execdotcmd(skipcmd,".beep%.exit",NULL); // Worst case we beep and exit
425 if (dotrv == QUIT_CMD) quit = 1;
428 // Switch vide mode if required
429 if (vmode != 0xFF) setvideomode(vmode);
431 // Do we have a startfile to display?
432 if (startfile[0] != '\0') runhelp(startfile);
435 while (quit == 0) { // As long as quit is zero repeat
436 curr = showmenus(find_menu_num("main")); // Initial menu is the one called "main"
439 if (curr->action == OPT_RUN) {
440 ecmd = (char *)malloc(sizeof(char)*MAX_CMD_LINE_LENGTH);
441 gencommand(curr,ecmd);
442 temp = (curr->extra_data ? ((pt_xtra)curr->extra_data)->ipappend : 0);
443 runsyslinuximage(ecmd,temp);
444 // kernel not found so execute the appropriate dot command
445 dotrv = execdotcmd(onerrcmd,".quit",ecmd); // pass bad cmdline as arg
446 if (dotrv== QUIT_CMD) quit = 1;
447 free(ecmd); ecmd = NULL;
449 else csprint("Error in programming!",0x07);
451 // find effective exit command
452 ecmd = ( isallowed(username,"root") ? exitcmdroot : exitcmd);
453 dotrv = execdotcmd(ecmd,".repeat",NULL);
454 quit = (dotrv == QUIT_CMD ? 1 : 0); // should we exit now
458 // Deallocate space used and quit