summaryrefslogtreecommitdiff
path: root/i486-stage2/syslinux/syslinux-6.03-precomputed-python-menues.patch
blob: e6c1db3951a3d60c9973b595403cc9717c40ad81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
diff -rauN syslinux/com32/cmenu/Makefile syslinux-precomputed-python-menues-patch/com32/cmenu/Makefile
--- syslinux/com32/cmenu/Makefile	2018-02-24 08:57:21.870445499 +0100
+++ syslinux-precomputed-python-menues-patch/com32/cmenu/Makefile	2018-02-24 09:44:46.464893504 +0100
@@ -41,7 +41,8 @@
 
 .PRECIOUS: %.c
 %.c: %.menu adv_menu.tpl
-	$(PYTHON) $(SRC)/menugen.py --input=$< --output=$@ --template=$(SRC)/adv_menu.tpl
+	cp $(basename $@).c $@
+#	$(PYTHON) $(SRC)/menugen.py --input=$< --output=$@ --template=$(SRC)/adv_menu.tpl
 
 all:	makeoutputdirs menus
 
diff -rauN syslinux/com32/cmenu/test.c syslinux-precomputed-python-menues-patch/com32/cmenu/test.c
--- syslinux/com32/cmenu/test.c	1970-01-01 01:00:00.000000000 +0100
+++ syslinux-precomputed-python-menues-patch/com32/cmenu/test.c	2018-02-24 09:17:50.483434027 +0100
@@ -0,0 +1,477 @@
+/* -*- c -*- ------------------------------------------------------------- *
+ *
+ *   Copyright 2004-2006 Murali Krishnan Ganapathy - All Rights Reserved
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
+ *   Boston MA 02111-1307, USA; either version 2 of the License, or
+ *   (at your option) any later version; incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
+#ifndef NULL
+#define NULL ((void *) 0)
+#endif
+
+#include "cmenu.h"
+#include "help.h"
+#include "passwords.h"
+#include "com32io.h"
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define MAX_CMD_LINE_LENGTH 514
+
+typedef struct s_xtra {
+  long ipappend; // Stores the ipappend flag to send (useful for PXELINUX only)
+  char *argsmenu; // Stores the name of menu which contains options for the given RUN item
+  char *perms; // stores the permissions required to activate the item
+} t_xtra;
+
+typedef t_xtra *pt_xtra; // Pointer to extra datastructure
+
+// Types of dot commands for which caller takes responsibility of handling
+// In some case some commands may not make sense, it is up to the caller
+// to handle cases which do not make sense
+typedef enum {QUIT_CMD, REPEAT_CMD, ENTER_CMD, ESCAPE_CMD} t_dotcmd;
+
+
+/*----------------- Global Variables */
+
+// default user
+#define GUEST_USER "guest"
+
+// for local commands. return value of execdotcmd
+#define QUIT_CMD 0
+#define RPT_CMD 1
+
+char username[12]; // Name of user currently using the system
+
+int PWD_ROW; // Line number where user authentication happens
+int EDIT_ROW; // row where User Tab
+
+char loginstr[] = "<L>ogin  ";
+char logoutstr[30];
+
+int vmode; // The video mode we want to be in
+char timeoutcmd[MAX_CMD_LINE_LENGTH]; // Command to execute on timeout
+char totaltimeoutcmd[MAX_CMD_LINE_LENGTH]; // Command to execute on totaltimeout
+
+char QUITSTR[] = ".quit"; // same as exit
+char IGNORESTR[]=".ignore"; // same as repeat, wait
+
+/*----------------  End globals */
+
+// returns pointer to first non-space char
+// and advances end of str by removing trailing spaces
+char * strip(char *str)
+{
+  char *p,*s,*e;
+  if (!str) return NULL;
+  p = str;
+  s = NULL;
+  e = NULL;
+  while (*p) {
+    if (*p != ' ') {
+       // mark start of string or record the last visited non-space char
+       if (!s) s=p; else e=p;
+    }
+    p++;
+  }
+  *(++e)='\0'; // kill string earlier
+  return s;
+}
+
+// executes a list of % separated commands
+// non-dot commands are assumed to be syslinux commands
+// All syslinux commands are appended with the contents of kerargs
+// If it fails (kernel not found) then the next one is tried in the
+// list
+// returns QUIT_CMD or RPT_CMD
+t_dotcmd execdotcmd(const char *cmd, char *defcmd, const char *kerargs)
+{
+   char cmdline[MAX_CMD_LINE_LENGTH];
+   char dotcmd[MAX_CMD_LINE_LENGTH];
+   char *curr,*next,*p,*args;
+   char ctr;
+
+   strcpy(dotcmd,cmd);
+   next = dotcmd;
+   cmdline[0] = '\0';
+   while (*next) { // if something to do
+      curr = next;
+      p = strchr(next,'%');
+      if (p) {
+         *p--='\0'; next=p+2;
+         while (*p == ' ') p--;
+         *(++p)='\0'; // remove trailing spaces
+      } else {
+        if (*defcmd) { // execute defcmd next
+            next=defcmd;
+            defcmd=NULL; // exec def cmd only once
+        } else next=NULL;
+      }
+      // now we just need to execute the command "curr"
+      curr = strip(curr);
+      if (curr[0] != '.') { // just run the kernel
+         strcpy(cmdline,curr);
+         if (kerargs) strcat(cmdline,kerargs);
+         runsyslinuximage(cmdline,0); // No IPAppend
+      } else { // We have a DOT command
+        // split command into command and args (may be empty)
+        args = curr;
+        while ( (*args != ' ') && (*args != '\0') ) args++;
+        if (*args) { // found a space
+           *args++ = '\0';
+           while (*args == ' ') args++; // skip over spaces
+        }
+        if ( (strcmp(curr,".exit")==0) ||
+             (strcmp(curr,".quit")==0)
+           )
+           return QUIT_CMD;
+        if ( (strcmp(curr,".repeat")==0) ||
+             (strcmp(curr,".ignore")==0) ||
+             (strcmp(curr,".wait")==0)
+           )
+           return RPT_CMD;
+        if (strcmp(curr,".beep")==0) {
+           if ((args) && ('0' <= args[0]) && (args[0] <= '9'))
+              ctr = args[0]-'0';
+           else ctr=1;
+           for (;ctr>0; ctr--) beep();
+        }
+        if (strcmp(curr,".help")==0) runhelp(args);
+      }
+   }
+   return RPT_CMD; // by default we do not quit
+}
+
+
+TIMEOUTCODE timeout(const char *cmd)
+{
+  t_dotcmd c;
+  c = execdotcmd(cmd,".wait",NULL);
+  switch(c) {
+    case ENTER_CMD:
+         return CODE_ENTER;
+    case ESCAPE_CMD:
+         return CODE_ESCAPE;
+    default:
+         return CODE_WAIT;
+  }
+}
+
+TIMEOUTCODE ontimeout(void)
+{
+   return timeout(timeoutcmd);
+}
+
+TIMEOUTCODE ontotaltimeout(void)
+{
+   return timeout(totaltimeoutcmd);
+}
+
+void keys_handler(t_menusystem * ms __attribute__ (( unused )), t_menuitem * mi, int scancode)
+{
+   int nc, nr;
+
+   if (getscreensize(1, &nr, &nc)) {
+       /* Unknown screen size? */
+       nc = 80;
+       nr = 24;
+   }
+
+   if ( (scancode == KEY_F1) && (mi->helpid != 0xFFFF) ) { // If scancode of F1 and non-trivial helpid
+      runhelpsystem(mi->helpid);
+   }
+
+   // If user hit TAB, and item is an "executable" item
+   // and user has privileges to edit it, edit it in place.
+   if ((scancode == KEY_TAB) && (mi->action == OPT_RUN) &&
+       (EDIT_ROW < nr) && (EDIT_ROW > 0) &&
+       (isallowed(username,"editcmd") || isallowed(username,"root"))) {
+     // User typed TAB and has permissions to edit command line
+     gotoxy(EDIT_ROW,1);
+     csprint("Command line:",0x07);
+     editstring(mi->data,ACTIONLEN);
+     gotoxy(EDIT_ROW,1);
+     cprint(' ',0x07,nc-1);
+   }
+}
+
+t_handler_return login_handler(t_menusystem *ms, t_menuitem *mi)
+{
+  (void)mi; // Unused
+  char pwd[40];
+  char login[40];
+  int nc, nr;
+  t_handler_return rv;
+
+  (void)ms;
+
+  rv = ACTION_INVALID;
+  if (PWD_ROW < 0) return rv; // No need to authenticate
+
+  if (mi->item == loginstr) { /* User wants to login */
+    if (getscreensize(1, &nr, &nc)) {
+        /* Unknown screen size? */
+        nc = 80;
+        nr = 24;
+    }
+
+    gotoxy(PWD_ROW,1);
+    csprint("Enter Username: ",0x07);
+    getstring(login, sizeof username);
+    gotoxy(PWD_ROW,1);
+    cprint(' ',0x07,nc);
+    csprint("Enter Password: ",0x07);
+    getpwd(pwd, sizeof pwd);
+    gotoxy(PWD_ROW,1);
+    cprint(' ',0x07,nc);
+
+    if (authenticate_user(login,pwd))
+    {
+      strcpy(username,login);
+      strcpy(logoutstr,"<L>ogout ");
+      strcat(logoutstr,username);
+      mi->item = logoutstr; // Change item to read "Logout"
+      rv.refresh = 1; // refresh the screen (as item contents changed)
+    }
+    else strcpy(username,GUEST_USER);
+  }
+  else // User needs to logout
+  {
+    strcpy(username,GUEST_USER);
+    strcpy(logoutstr,"");
+    mi->item = loginstr;
+  }
+
+  return rv;
+}
+
+t_handler_return check_perms(t_menusystem *ms, t_menuitem *mi)
+{
+   char *perms;
+   pt_xtra x;
+
+   (void) ms; // To keep compiler happy
+
+   x = (pt_xtra) mi->extra_data;
+   perms = ( x ? x->perms : NULL);
+   if (!perms) return ACTION_VALID;
+
+   if (isallowed(username,"root") || isallowed(username,perms)) // If allowed
+      return ACTION_VALID;
+   else return ACTION_INVALID;
+}
+
+// Compute the full command line to add and if non-trivial
+// prepend the string prepend to final command line
+// Assume cmdline points to buffer long enough to hold answer
+void gencommand(pt_menuitem mi, char *cmdline)
+{
+   pt_xtra x;
+   cmdline[0] = '\0';
+   strcat(cmdline,mi->data);
+   x = (pt_xtra) mi->extra_data;
+   if ( (x) && (x->argsmenu)) gen_append_line(x->argsmenu,cmdline);
+}
+
+
+// run the given command together with additional options which may need passing
+void runcommand(pt_menuitem mi)
+{
+   char *line;
+   pt_xtra x;
+   long ipappend;
+
+   line = (char *)malloc(sizeof(char)*MAX_CMD_LINE_LENGTH);
+   gencommand(mi,line);
+   x = (pt_xtra) mi->extra_data;
+   ipappend = (x ? x->ipappend : 0);
+
+   runsyslinuximage(line,ipappend);
+   free(line);
+}
+
+// set the extra info for the specified menu item.
+void set_xtra(pt_menuitem mi, const char *argsmenu, const char *perms, unsigned int helpid, long ipappend)
+{
+   pt_xtra xtra;
+   int bad_argsmenu, bad_perms, bad_ipappend;
+
+   mi->extra_data = NULL; // initalize
+   mi->helpid = helpid; // set help id
+
+   if (mi->action != OPT_RUN) return;
+
+   bad_argsmenu = bad_perms = bad_ipappend = 0;
+   if ( (argsmenu==NULL) || (strlen(argsmenu)==0)) bad_argsmenu = 1;
+   if ( (perms==NULL) || (strlen(perms)==0)) bad_perms = 1;
+   if ( ipappend==0) bad_ipappend = 1;
+
+   if (bad_argsmenu && bad_perms && bad_ipappend) return;
+
+   xtra = (pt_xtra) malloc(sizeof(t_xtra));
+   mi->extra_data = (void *) xtra;
+   xtra->argsmenu = xtra->perms = NULL;
+   xtra->ipappend = ipappend;
+   if (!bad_argsmenu) {
+      xtra->argsmenu = (char *) malloc(sizeof(char)*(strlen(argsmenu)+1));
+      strcpy(xtra->argsmenu,argsmenu);
+   }
+   if (!bad_perms) {
+      xtra->perms = (char *) malloc(sizeof(char)*(strlen(perms)+1));
+      strcpy(xtra->perms,perms);
+      mi->handler = &check_perms;
+   }
+}
+
+int main(void)
+{
+  pt_menuitem curr;
+  char quit;
+  char exitcmd[MAX_CMD_LINE_LENGTH];
+  char exitcmdroot[MAX_CMD_LINE_LENGTH];
+  char onerrcmd[MAX_CMD_LINE_LENGTH];
+  char startfile[MAX_CMD_LINE_LENGTH];
+  char *ecmd; // effective exit command or onerrorcmd
+  char skipbits;
+  char skipcmd[MAX_CMD_LINE_LENGTH];
+  int timeout; // time in multiples of 0.1 seconds
+  int totaltimeout; // time in multiples of 0.1 seconds
+  t_dotcmd dotrv; // to store the return value of execdotcmd
+  int temp;
+
+  strcpy(username,GUEST_USER);
+/* ---- Initializing menu system parameters --- */
+  vmode = 0xFF;
+  skipbits = 0;
+  PWD_ROW = 23;
+  EDIT_ROW = 23;
+  strcpy(onerrcmd,".repeat");
+  strcpy(exitcmd,".exit");
+  strcpy(exitcmdroot,"");
+  // If not specified exitcmdroot = exitcmd
+  if (exitcmdroot[0] == '\0') strcpy(exitcmdroot,exitcmd);
+  // Timeout stuff
+  timeout = 600;
+  strcpy(timeoutcmd,".beep");
+  totaltimeout = 0;
+  strcpy(totaltimeoutcmd,".wait");
+  strcpy(startfile,"");
+
+  init_help("/isolinux/help");
+  init_passwords("");
+  init_menusystem("A test of the test.menu file");
+  set_window_size(1,1,23,78);
+
+  // Register the ontimeout handler, with a time out of 10 seconds
+  reg_ontimeout(ontimeout,timeout*10,0);
+  reg_ontotaltimeout(ontotaltimeout,totaltimeout*10);
+
+  // Register menusystem handlers
+  reg_handler(HDLR_KEYS,&keys_handler);
+/* ---- End of initialization --- */
+
+/* ------- MENU testing ----- */
+  add_named_menu("testing"," Testing ",-1);
+
+  curr = add_item("Self Loop","Go to Testing",OPT_SUBMENU,"testing",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("Memory Test","Perform extensive memory testing",OPT_RUN,"memtest",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("Exit this menu","Go one level up",OPT_EXITMENU,"",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+/* ------- MENU rescue ----- */
+  add_named_menu("rescue"," Rescue Options ",-1);
+  set_menu_pos(10,10);
+
+  curr = add_item("Linux Rescue","linresc",OPT_RUN,"linresc",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("Dos Rescue","dosresc",OPT_RUN,"dosresc",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("Windows Rescue","winresc",OPT_RUN,"winresc",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("Exit this menu","Go one level up",OPT_EXITMENU,"",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+/* ------- MENU main ----- */
+  add_named_menu("main"," Main Menu ",-1);
+
+  curr = add_item("Prepare","prep",OPT_RUN,"prep",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("Rescue options...","Troubleshoot a system",OPT_SUBMENU,"rescue",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("Testing...","Options to test hardware",OPT_SUBMENU,"testing",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("Exit this menu","Go one level up",OPT_EXITMENU,"",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+/* ------- END OF MENU declarations ----- */
+
+// Check if we should skip the menu altogether
+  quit = 0; // Dont skip the menu
+  if (getshiftflags() & skipbits) { // we must skip the menu altogther and execute skipcmd
+    dotrv = execdotcmd(skipcmd,".beep%.exit",NULL); // Worst case we beep and exit
+    if (dotrv == QUIT_CMD) quit = 1;
+  }
+
+// Switch vide mode if required
+   if (vmode != 0xFF) setvideomode(vmode);
+
+// Do we have a startfile to display?
+   if (startfile[0] != '\0') runhelp(startfile);
+
+// The main loop
+  while (quit == 0) { // As long as quit is zero repeat
+     curr = showmenus(find_menu_num("main")); // Initial menu is the one called "main"
+
+     if (curr) {
+        if (curr->action == OPT_RUN) {
+           ecmd = (char *)malloc(sizeof(char)*MAX_CMD_LINE_LENGTH);
+           gencommand(curr,ecmd);
+           temp = (curr->extra_data ? ((pt_xtra)curr->extra_data)->ipappend : 0);
+           runsyslinuximage(ecmd,temp);
+           // kernel not found so execute the appropriate dot command
+           dotrv = execdotcmd(onerrcmd,".quit",ecmd); // pass bad cmdline as arg
+           if (dotrv== QUIT_CMD) quit = 1;
+           free(ecmd); ecmd = NULL;
+        }
+        else csprint("Error in programming!",0x07);
+     } else {
+        // find effective exit command
+        ecmd = ( isallowed(username,"root") ? exitcmdroot : exitcmd);
+        dotrv = execdotcmd(ecmd,".repeat",NULL);
+        quit = (dotrv == QUIT_CMD ? 1 : 0); // should we exit now
+     }
+  }
+
+// Deallocate space used and quit
+  close_passwords();
+  close_help();
+  close_menusystem();
+  return 0;
+}
+
diff -rauN syslinux/com32/cmenu/test2.c syslinux-precomputed-python-menues-patch/com32/cmenu/test2.c
--- syslinux/com32/cmenu/test2.c	1970-01-01 01:00:00.000000000 +0100
+++ syslinux-precomputed-python-menues-patch/com32/cmenu/test2.c	2018-02-24 09:19:06.739180583 +0100
@@ -0,0 +1,534 @@
+/* -*- c -*- ------------------------------------------------------------- *
+ *
+ *   Copyright 2004-2006 Murali Krishnan Ganapathy - All Rights Reserved
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
+ *   Boston MA 02111-1307, USA; either version 2 of the License, or
+ *   (at your option) any later version; incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
+#ifndef NULL
+#define NULL ((void *) 0)
+#endif
+
+#include "cmenu.h"
+#include "help.h"
+#include "passwords.h"
+#include "com32io.h"
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define MAX_CMD_LINE_LENGTH 514
+
+typedef struct s_xtra {
+  long ipappend; // Stores the ipappend flag to send (useful for PXELINUX only)
+  char *argsmenu; // Stores the name of menu which contains options for the given RUN item
+  char *perms; // stores the permissions required to activate the item
+} t_xtra;
+
+typedef t_xtra *pt_xtra; // Pointer to extra datastructure
+
+// Types of dot commands for which caller takes responsibility of handling
+// In some case some commands may not make sense, it is up to the caller
+// to handle cases which do not make sense
+typedef enum {QUIT_CMD, REPEAT_CMD, ENTER_CMD, ESCAPE_CMD} t_dotcmd;
+
+
+/*----------------- Global Variables */
+
+// default user
+#define GUEST_USER "guest"
+
+// for local commands. return value of execdotcmd
+#define QUIT_CMD 0
+#define RPT_CMD 1
+
+char username[12]; // Name of user currently using the system
+
+int PWD_ROW; // Line number where user authentication happens
+int EDIT_ROW; // row where User Tab
+
+char loginstr[] = "<L>ogin  ";
+char logoutstr[30];
+
+int vmode; // The video mode we want to be in
+char timeoutcmd[MAX_CMD_LINE_LENGTH]; // Command to execute on timeout
+char totaltimeoutcmd[MAX_CMD_LINE_LENGTH]; // Command to execute on totaltimeout
+
+char QUITSTR[] = ".quit"; // same as exit
+char IGNORESTR[]=".ignore"; // same as repeat, wait
+
+/*----------------  End globals */
+
+// returns pointer to first non-space char
+// and advances end of str by removing trailing spaces
+char * strip(char *str)
+{
+  char *p,*s,*e;
+  if (!str) return NULL;
+  p = str;
+  s = NULL;
+  e = NULL;
+  while (*p) {
+    if (*p != ' ') {
+       // mark start of string or record the last visited non-space char
+       if (!s) s=p; else e=p;
+    }
+    p++;
+  }
+  *(++e)='\0'; // kill string earlier
+  return s;
+}
+
+// executes a list of % separated commands
+// non-dot commands are assumed to be syslinux commands
+// All syslinux commands are appended with the contents of kerargs
+// If it fails (kernel not found) then the next one is tried in the
+// list
+// returns QUIT_CMD or RPT_CMD
+t_dotcmd execdotcmd(const char *cmd, char *defcmd, const char *kerargs)
+{
+   char cmdline[MAX_CMD_LINE_LENGTH];
+   char dotcmd[MAX_CMD_LINE_LENGTH];
+   char *curr,*next,*p,*args;
+   char ctr;
+
+   strcpy(dotcmd,cmd);
+   next = dotcmd;
+   cmdline[0] = '\0';
+   while (*next) { // if something to do
+      curr = next;
+      p = strchr(next,'%');
+      if (p) {
+         *p--='\0'; next=p+2;
+         while (*p == ' ') p--;
+         *(++p)='\0'; // remove trailing spaces
+      } else {
+        if (*defcmd) { // execute defcmd next
+            next=defcmd;
+            defcmd=NULL; // exec def cmd only once
+        } else next=NULL;
+      }
+      // now we just need to execute the command "curr"
+      curr = strip(curr);
+      if (curr[0] != '.') { // just run the kernel
+         strcpy(cmdline,curr);
+         if (kerargs) strcat(cmdline,kerargs);
+         runsyslinuximage(cmdline,0); // No IPAppend
+      } else { // We have a DOT command
+        // split command into command and args (may be empty)
+        args = curr;
+        while ( (*args != ' ') && (*args != '\0') ) args++;
+        if (*args) { // found a space
+           *args++ = '\0';
+           while (*args == ' ') args++; // skip over spaces
+        }
+        if ( (strcmp(curr,".exit")==0) ||
+             (strcmp(curr,".quit")==0)
+           )
+           return QUIT_CMD;
+        if ( (strcmp(curr,".repeat")==0) ||
+             (strcmp(curr,".ignore")==0) ||
+             (strcmp(curr,".wait")==0)
+           )
+           return RPT_CMD;
+        if (strcmp(curr,".beep")==0) {
+           if ((args) && ('0' <= args[0]) && (args[0] <= '9'))
+              ctr = args[0]-'0';
+           else ctr=1;
+           for (;ctr>0; ctr--) beep();
+        }
+        if (strcmp(curr,".help")==0) runhelp(args);
+      }
+   }
+   return RPT_CMD; // by default we do not quit
+}
+
+
+TIMEOUTCODE timeout(const char *cmd)
+{
+  t_dotcmd c;
+  c = execdotcmd(cmd,".wait",NULL);
+  switch(c) {
+    case ENTER_CMD:
+         return CODE_ENTER;
+    case ESCAPE_CMD:
+         return CODE_ESCAPE;
+    default:
+         return CODE_WAIT;
+  }
+}
+
+TIMEOUTCODE ontimeout(void)
+{
+   return timeout(timeoutcmd);
+}
+
+TIMEOUTCODE ontotaltimeout(void)
+{
+   return timeout(totaltimeoutcmd);
+}
+
+void keys_handler(t_menusystem * ms __attribute__ (( unused )), t_menuitem * mi, int scancode)
+{
+   int nc, nr;
+
+   if (getscreensize(1, &nr, &nc)) {
+       /* Unknown screen size? */
+       nc = 80;
+       nr = 24;
+   }
+
+   if ( (scancode == KEY_F1) && (mi->helpid != 0xFFFF) ) { // If scancode of F1 and non-trivial helpid
+      runhelpsystem(mi->helpid);
+   }
+
+   // If user hit TAB, and item is an "executable" item
+   // and user has privileges to edit it, edit it in place.
+   if ((scancode == KEY_TAB) && (mi->action == OPT_RUN) &&
+       (EDIT_ROW < nr) && (EDIT_ROW > 0) &&
+       (isallowed(username,"editcmd") || isallowed(username,"root"))) {
+     // User typed TAB and has permissions to edit command line
+     gotoxy(EDIT_ROW,1);
+     csprint("Command line:",0x07);
+     editstring(mi->data,ACTIONLEN);
+     gotoxy(EDIT_ROW,1);
+     cprint(' ',0x07,nc-1);
+   }
+}
+
+t_handler_return login_handler(t_menusystem *ms, t_menuitem *mi)
+{
+  (void)mi; // Unused
+  char pwd[40];
+  char login[40];
+  int nc, nr;
+  t_handler_return rv;
+
+  (void)ms;
+
+  rv = ACTION_INVALID;
+  if (PWD_ROW < 0) return rv; // No need to authenticate
+
+  if (mi->item == loginstr) { /* User wants to login */
+    if (getscreensize(1, &nr, &nc)) {
+        /* Unknown screen size? */
+        nc = 80;
+        nr = 24;
+    }
+
+    gotoxy(PWD_ROW,1);
+    csprint("Enter Username: ",0x07);
+    getstring(login, sizeof username);
+    gotoxy(PWD_ROW,1);
+    cprint(' ',0x07,nc);
+    csprint("Enter Password: ",0x07);
+    getpwd(pwd, sizeof pwd);
+    gotoxy(PWD_ROW,1);
+    cprint(' ',0x07,nc);
+
+    if (authenticate_user(login,pwd))
+    {
+      strcpy(username,login);
+      strcpy(logoutstr,"<L>ogout ");
+      strcat(logoutstr,username);
+      mi->item = logoutstr; // Change item to read "Logout"
+      rv.refresh = 1; // refresh the screen (as item contents changed)
+    }
+    else strcpy(username,GUEST_USER);
+  }
+  else // User needs to logout
+  {
+    strcpy(username,GUEST_USER);
+    strcpy(logoutstr,"");
+    mi->item = loginstr;
+  }
+
+  return rv;
+}
+
+t_handler_return check_perms(t_menusystem *ms, t_menuitem *mi)
+{
+   char *perms;
+   pt_xtra x;
+
+   (void) ms; // To keep compiler happy
+
+   x = (pt_xtra) mi->extra_data;
+   perms = ( x ? x->perms : NULL);
+   if (!perms) return ACTION_VALID;
+
+   if (isallowed(username,"root") || isallowed(username,perms)) // If allowed
+      return ACTION_VALID;
+   else return ACTION_INVALID;
+}
+
+// Compute the full command line to add and if non-trivial
+// prepend the string prepend to final command line
+// Assume cmdline points to buffer long enough to hold answer
+void gencommand(pt_menuitem mi, char *cmdline)
+{
+   pt_xtra x;
+   cmdline[0] = '\0';
+   strcat(cmdline,mi->data);
+   x = (pt_xtra) mi->extra_data;
+   if ( (x) && (x->argsmenu)) gen_append_line(x->argsmenu,cmdline);
+}
+
+
+// run the given command together with additional options which may need passing
+void runcommand(pt_menuitem mi)
+{
+   char *line;
+   pt_xtra x;
+   long ipappend;
+
+   line = (char *)malloc(sizeof(char)*MAX_CMD_LINE_LENGTH);
+   gencommand(mi,line);
+   x = (pt_xtra) mi->extra_data;
+   ipappend = (x ? x->ipappend : 0);
+
+   runsyslinuximage(line,ipappend);
+   free(line);
+}
+
+// set the extra info for the specified menu item.
+void set_xtra(pt_menuitem mi, const char *argsmenu, const char *perms, unsigned int helpid, long ipappend)
+{
+   pt_xtra xtra;
+   int bad_argsmenu, bad_perms, bad_ipappend;
+
+   mi->extra_data = NULL; // initalize
+   mi->helpid = helpid; // set help id
+
+   if (mi->action != OPT_RUN) return;
+
+   bad_argsmenu = bad_perms = bad_ipappend = 0;
+   if ( (argsmenu==NULL) || (strlen(argsmenu)==0)) bad_argsmenu = 1;
+   if ( (perms==NULL) || (strlen(perms)==0)) bad_perms = 1;
+   if ( ipappend==0) bad_ipappend = 1;
+
+   if (bad_argsmenu && bad_perms && bad_ipappend) return;
+
+   xtra = (pt_xtra) malloc(sizeof(t_xtra));
+   mi->extra_data = (void *) xtra;
+   xtra->argsmenu = xtra->perms = NULL;
+   xtra->ipappend = ipappend;
+   if (!bad_argsmenu) {
+      xtra->argsmenu = (char *) malloc(sizeof(char)*(strlen(argsmenu)+1));
+      strcpy(xtra->argsmenu,argsmenu);
+   }
+   if (!bad_perms) {
+      xtra->perms = (char *) malloc(sizeof(char)*(strlen(perms)+1));
+      strcpy(xtra->perms,perms);
+      mi->handler = &check_perms;
+   }
+}
+
+int main(void)
+{
+  pt_menuitem curr;
+  char quit;
+  char exitcmd[MAX_CMD_LINE_LENGTH];
+  char exitcmdroot[MAX_CMD_LINE_LENGTH];
+  char onerrcmd[MAX_CMD_LINE_LENGTH];
+  char startfile[MAX_CMD_LINE_LENGTH];
+  char *ecmd; // effective exit command or onerrorcmd
+  char skipbits;
+  char skipcmd[MAX_CMD_LINE_LENGTH];
+  int timeout; // time in multiples of 0.1 seconds
+  int totaltimeout; // time in multiples of 0.1 seconds
+  t_dotcmd dotrv; // to store the return value of execdotcmd
+  int temp;
+
+  strcpy(username,GUEST_USER);
+/* ---- Initializing menu system parameters --- */
+  vmode = 0xFF;
+  skipbits = SHIFT_PRESSED | CAPSLOCK_ON;
+  PWD_ROW = 23;
+  EDIT_ROW = 23;
+  strcpy(onerrcmd,".beep 2 % % .help hlp00025.txt % .exit");
+  strcpy(exitcmd,".exit");
+  strcpy(exitcmdroot,"");
+  // If not specified exitcmdroot = exitcmd
+  if (exitcmdroot[0] == '\0') strcpy(exitcmdroot,exitcmd);
+  // Timeout stuff
+  timeout = 600;
+  strcpy(timeoutcmd,".wait");
+  totaltimeout = 0;
+  strcpy(totaltimeoutcmd,"chain.c32 hd 0");
+  strcpy(startfile,"hlp00026.txt");
+
+  init_help("/isolinux/help");
+  init_passwords("/isolinux/password");
+  init_menusystem(" COMBOOT Menu System ");
+  set_window_size(1,1,21,79);
+
+  // Register the ontimeout handler, with a time out of 10 seconds
+  reg_ontimeout(ontimeout,timeout*10,0);
+  reg_ontotaltimeout(ontotaltimeout,totaltimeout*10);
+
+  // Register menusystem handlers
+  reg_handler(HDLR_KEYS,&keys_handler);
+/* ---- End of initialization --- */
+
+/* ------- MENU netmenu ----- */
+  add_named_menu("netmenu"," Init Network ",-1);
+
+  curr = add_item("<N>one","Dont start network",OPT_RADIOITEM,"network=no",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("<d>hcp","Use DHCP",OPT_RADIOITEM,"network=dhcp",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+/* ------- MENU testing ----- */
+  add_named_menu("testing"," Testing ",-1);
+
+  curr = add_item("<M>emory Test","Perform extensive memory testing",OPT_RUN,"memtest",0);
+  set_xtra(curr,"","",25,3); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("<I>nvisible","You dont see this",OPT_INVISIBLE,"",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("<E>xit menu","Go one level up",OPT_EXITMENU,"",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+/* ------- MENU rescue ----- */
+  add_named_menu("rescue"," Rescue Options ",-1);
+
+  curr = add_item("<L>inux Rescue","Run linresc",OPT_RUN,"linresc",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("<D>os Rescue","dosresc",OPT_RUN,"dosresc",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("<W>indows Rescue","winresc",OPT_RUN,"winresc",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("<E>xit this menu","Go one level up",OPT_EXITMENU,"",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+/* ------- MENU prep ----- */
+  add_named_menu("prep"," Prep options ",-1);
+
+  curr = add_item("<b>aseurl by IP?","Specify gui baseurl by IP address",OPT_CHECKBOX,"baseurl=http://192.168.0.1",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("<m>ountcd?","Mount the cdrom drive?",OPT_CHECKBOX,"mountcd",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("Network Initialization","How to initialise network device?",OPT_RADIOMENU,"netmenu",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("","",OPT_SEP,"",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("Reinstall <w>indows","Re-install the windows side of a dual boot setup",OPT_CHECKBOX,"repair=win",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("Reinstall <l>inux","Re-install the linux side of a dual boot setup",OPT_CHECKBOX,"repair=lin",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("","",OPT_SEP,"",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("<R>un prep now","Execute prep with the above options",OPT_RUN,"prep",0);
+  set_xtra(curr,"prep","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("<E>xit this menu","Go up one level",OPT_EXITMENU,"",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+/* ------- MENU main ----- */
+  add_named_menu("main"," Main Menu ",-1);
+
+  curr = add_item(loginstr,"Login/Logout of authentication system",OPT_RUN,NULL,0);
+  curr->helpid = 65535;
+  curr->handler = &login_handler;
+
+  curr = add_item("<P>repare","prep",OPT_RUN,"prep",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("<P>rep options...","Options for prep",OPT_SUBMENU,"prep",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("<R>escue options...","Troubleshoot a system",OPT_SUBMENU,"rescue",0);
+  set_xtra(curr,"","",26,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("<T>esting...","Options to test hardware",OPT_SUBMENU,"testing",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+
+  curr = add_item("<E>xit to prompt","Exit the menu system",OPT_EXITMENU,"",0);
+  set_xtra(curr,"","",65535,0); // Set associated extra info
+  set_shortcut(-1);
+/* ------- END OF MENU declarations ----- */
+
+// Check if we should skip the menu altogether
+  quit = 0; // Dont skip the menu
+  if (getshiftflags() & skipbits) { // we must skip the menu altogther and execute skipcmd
+    dotrv = execdotcmd(skipcmd,".beep%.exit",NULL); // Worst case we beep and exit
+    if (dotrv == QUIT_CMD) quit = 1;
+  }
+
+// Switch vide mode if required
+   if (vmode != 0xFF) setvideomode(vmode);
+
+// Do we have a startfile to display?
+   if (startfile[0] != '\0') runhelp(startfile);
+
+// The main loop
+  while (quit == 0) { // As long as quit is zero repeat
+     curr = showmenus(find_menu_num("main")); // Initial menu is the one called "main"
+
+     if (curr) {
+        if (curr->action == OPT_RUN) {
+           ecmd = (char *)malloc(sizeof(char)*MAX_CMD_LINE_LENGTH);
+           gencommand(curr,ecmd);
+           temp = (curr->extra_data ? ((pt_xtra)curr->extra_data)->ipappend : 0);
+           runsyslinuximage(ecmd,temp);
+           // kernel not found so execute the appropriate dot command
+           dotrv = execdotcmd(onerrcmd,".quit",ecmd); // pass bad cmdline as arg
+           if (dotrv== QUIT_CMD) quit = 1;
+           free(ecmd); ecmd = NULL;
+        }
+        else csprint("Error in programming!",0x07);
+     } else {
+        // find effective exit command
+        ecmd = ( isallowed(username,"root") ? exitcmdroot : exitcmd);
+        dotrv = execdotcmd(ecmd,".repeat",NULL);
+        quit = (dotrv == QUIT_CMD ? 1 : 0); // should we exit now
+     }
+  }
+
+// Deallocate space used and quit
+  close_passwords();
+  close_help();
+  close_menusystem();
+  return 0;
+}
+