home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CMENU.ZIP / CMENU.C
Encoding:
C/C++ Source or Header  |  1988-04-19  |  3.8 KB  |  203 lines

  1. /************************************************
  2. *                        *
  3. *    CMENU -- MENU DRIVEN PROGRAM LOADER    *
  4. *    BY WELCOM H. WATSON, JR.        *
  5. *                        *
  6. *    DOCUMENTED IN CMENU.DOC            *
  7. *                        *
  8. ************************************************/
  9. #include "stdio.h"
  10. #define MAXPROGS 51    /* number of selectable programs = 51 (0 - 50) */
  11. #define B_SIZE 3    /* buffer size for selection = 2 */
  12. #define C_SIZE 81    /* command line size = 80 */
  13.  
  14. int _stack = 5120;
  15.  
  16. main(argc, argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.     FILE *fp, *fopen();
  21.     int c, fgetc();
  22.     char progs[MAXPROGS][C_SIZE];
  23.     char *p, *stpbrk(), *stpblk();
  24.  
  25.     extern int _oserr;
  26.     int error;
  27.     static char breaks[] = {' ', '\t', '\n', '\0'};
  28.     int i, n;
  29.  
  30.     char menu[31];
  31.  
  32.     static char *dos_err[] = {
  33.         " ",
  34.         "INVALID FUNCTION NUMBER",
  35.         "FILE NOT FOUND",
  36.         "PATH NOT FOUND",
  37.         "TOO MANY OPEN FILES (NO HANDLES LEFT)",
  38.         "ACCESS DENIED",
  39.         "INVALID HANDLE",
  40.         "MEMORY CONTROL BLOCKS DESTROYED",
  41.         "INSUFFICIENT MEMORY",
  42.         "INVALID MEMORY BLOCK ADDRESS",
  43.         "INVALID ENVIRONMENT",
  44.         "INVALID FORMAT",
  45.         "INVALID ACCESS CODE",
  46.         "INVALID DATA",
  47.         " ",
  48.         "INVALID DRIVE WAS SPECIFIED",
  49.         "ATTEMPTED TO REMOVE THE CURRENT DIRECTORY",
  50.         "NOT SAME DEVICE",
  51.         "NO MORE FILES"
  52.     };
  53.  
  54. /*
  55. If no arguments exit.
  56. */
  57.  
  58.     if (argc == 1) {
  59.         exit();
  60.     }
  61.  
  62. /*
  63. Open and read menu control file 
  64. all characters before '~' are output to
  65. console.  Character string beginning after
  66. first occurrence of '~' and ending with
  67. '\n' is the program if '0' or CR only is entered
  68. from the console.  If nothing is between
  69. '~` and '\n' '0' or CR will cause the program
  70. to terminate with exit.
  71.  
  72. Strings following are the program options for
  73. the fork, with each program name terminated
  74. with '\n'.  The first program name will be loaded
  75. and executed if '1' is entered from the console.
  76. */
  77.     strcpy(menu, argv[1]);
  78.  
  79.  
  80.  
  81. loop:
  82.     fp = fopen(menu, "ra");
  83.     if (fp == NULL) {
  84.         cprintf("\nCANNOT OPEN [%s]", menu);
  85.         putch(7);
  86.         waitsec(2);
  87.         exit();
  88.     }
  89.  
  90.     while ( (c = fgetc(fp)) != '~' ) {
  91.         putch(c);
  92.     }
  93.  
  94.  
  95.     /* get the program options */
  96.     i = n = 0;
  97.     while ((c = fgetc(fp)) != EOF && i < MAXPROGS) {
  98.         switch (c) {
  99.             case '\n':
  100.                 progs[i][n] = '\0';
  101.                 ++i;
  102.                 n = 0;
  103.                 break;
  104.             default:
  105.                 if (n < C_SIZE) {
  106.                     progs[i][n] = c;
  107.                     ++n;
  108.                 }
  109.                 break;
  110.         }
  111.     }
  112.  
  113.     fclose(fp);
  114.  
  115.     --i;
  116.  
  117.     n = enterir(i);
  118.  
  119.  
  120.     if (*progs[n] == '\0') {
  121.         exit();
  122.     }
  123.  
  124.  
  125.     p = progs[n];
  126.  
  127.     for (i=0; *p != '\0'; ++i) {
  128.  
  129.         argv[i] = p;
  130.         p = stpbrk(p, breaks);
  131.         if (*p != '\0') {
  132.             *p = '\0';
  133.             ++p;
  134.         }
  135.  
  136.         p = stpblk(p);            
  137.  
  138.     }
  139.  
  140.     argv[i] = NULL;
  141.  
  142.  
  143.     error = forkvp(argv[0], argv);
  144.     if (error != 0) {
  145.         if (_oserr > 0) {
  146.             cprintf("\nMS-DOS ERROR [%d] IN LOADING '%s'\n%s", _oserr, argv[0], dos_err[_oserr]);
  147.         }
  148.         else {
  149.             error = wait();
  150.             cprintf("\nMS-DOS ERROR [%d] IN EXECUTING '%s'\n%s", error, argv[0], dos_err[error]);
  151.         }
  152.         putch(7);
  153.         waitsec(2);
  154.     }
  155.     goto loop;
  156. }
  157.  
  158. /*function for entry of non negative integer characters*/
  159.  
  160. enteri(charcnt)
  161. int *charcnt;    /*pointer to integer which keeps track of # of chars entered*/
  162. {
  163.     char c, buffer[B_SIZE];
  164.     int i, x;
  165.     for (i = 0; (c = getch()) != '\r'; ) {
  166.         if ((c >= '0' && c <= '9') && i < B_SIZE - 1) {
  167.             buffer[i] = c;
  168.             putch(c);
  169.             ++i;
  170.         }
  171.         else if (c == '\b' && i > 0) {
  172.             putch('\b');
  173.             putch(' ');
  174.             putch('\b');
  175.             --i;
  176.         }
  177.         else {
  178.             putch(7);
  179.         }
  180.     }
  181.     buffer[i] = '\0';
  182.     *charcnt = i;
  183.     x = atoi(buffer);
  184.     return(x);
  185. }
  186.  
  187. /*entry of int >= 0 but not > maximum selection*/
  188. enterir(maxsel)
  189. int maxsel;
  190. {
  191.     int x, charcnt;
  192.     while ((x = enteri(&charcnt)) > maxsel) {
  193.         putch(7);
  194.         while (charcnt > 0) {
  195.             putch('\b');
  196.             putch(' ');
  197.             putch('\b');
  198.             --charcnt;
  199.         }
  200.     }
  201.     return(x);
  202. }
  203.