home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_06 / cmenu13.exe / RMENU3.C < prev    next >
C/C++ Source or Header  |  1992-05-22  |  5KB  |  220 lines

  1. /************************************************************
  2.  *    Program: RMENU Menu Interpreter
  3.  *  Module: rmenu3.c
  4.  *        Execute a Menu Item
  5.  *    Written by: Leor Zolman, 7/91
  6.  ************************************************************/
  7.  
  8. #include "cmenu.h"
  9. #include "rcmenu.h"
  10.  
  11. #if __STDC__
  12. #pragma hdrstop
  13. #endif
  14.  
  15. /************************************************************
  16.  * do_item():
  17.  *    Perform the action associated with a particular item.
  18.  *    Default path is supplied as "path":
  19.  ************************************************************/
  20.  
  21. int do_item(M2p, curr, path)
  22. MENU2 *M2p;
  23. int curr;
  24. char *path;
  25. {
  26.     ITEM *Ip = M2p -> Items[curr];
  27.     char newpath[MAX_PATH];
  28.     int i, j;
  29.  
  30.     strcpy(newpath, make_path(path, Ip -> path));
  31.  
  32.     switch (Ip -> acttyp)
  33.     {
  34.         case ACT_CMND:
  35.             do_cmnd(Ip, newpath);
  36.             break;
  37.  
  38.         case ACT_LMENU:
  39.             if (sub_menu(Ip->lmenunum, newpath) == EXITALL)
  40.                 return EXITALL;
  41.             break;
  42.                 
  43.         case ACT_EMENU:
  44.             if (do_emenu(Ip, newpath) == EXITALL)
  45.                 return EXITALL;
  46.             break;
  47.     }
  48.     return OK;
  49. }
  50.  
  51.  
  52. /************************************************************
  53.  * show_item():
  54.  *    Display the action associated with a particular item.
  55.  *    Default path is supplied as "path":
  56.  ************************************************************/
  57.  
  58. int show_item(M2p, curr, path)
  59. MENU2 *M2p;
  60. int curr;
  61. char *path;
  62. {
  63.     ITEM *Ip = M2p -> Items[curr];
  64.     char newpath[MAX_PATH];
  65.     int i, j;
  66.     char c;
  67.     
  68.     strcpy(newpath, make_path(path, Ip -> path));
  69.  
  70.     switch (Ip -> acttyp)
  71.     {
  72.         case ACT_CMND:
  73.             return show_cmnd(Ip, newpath);
  74.  
  75.         case ACT_LMENU:
  76.             c = put_msg(0, "This is a local menu. Press any key to continue: ");
  77.             if (tolower(c) == 'x')
  78.                 return EXITALL;
  79.             break;
  80.                         
  81.         case ACT_EMENU:
  82.             c = put_msg(0,
  83.              " Path: %s; Emenu spec: %s. Press any key to continue: ",
  84.                            newpath, Ip -> action);
  85.             if (tolower(c) == 'x')
  86.                 return EXITALL;
  87.             break;
  88.     }
  89.     return OK;
  90. }
  91.  
  92.  
  93. /************************************************************
  94.  * do_cmnd():
  95.  *    Run a directly executable item.
  96.  ************************************************************/
  97.  
  98. Void do_cmnd(Ip, path)
  99. ITEM *Ip;
  100. char *path;
  101. {
  102.     char *cmd_line;
  103.     int retval;
  104.  
  105.     cmd_line = make_cmd(path, Ip -> action);
  106.  
  107.     if (Ip -> pre_clear == YES ||
  108.         (Ip -> pre_clear == DEFAULT && DEF_PRECLEAR == YES))
  109.             clear();
  110.  
  111.     move(0, 0);
  112.     refresh();
  113.  
  114.     if (debug)
  115.         put_msg(0, "About to exec: %s", cmd_line);
  116.  
  117.     pre_shell();                /* set up for shell call    */
  118.     retval = system0(cmd_line);    /* make the shell call        */
  119.  
  120.     switch (Ip -> prompt)        /* decide whether to prompt */
  121.     {                            /* or not after the command */
  122.         case NO:                /* explicit no is clear        */
  123.             break;
  124.  
  125.         case DEFAULT:            /* if unspecified, then...    */
  126.  
  127. #if (DEF_PROMPT == NO)            /* if default is NO,        */
  128.                 break;            /* then don't prompt        */
  129. #endif
  130.  
  131. #if (DEF_PROMPT == ON_ERROR)    /* if prompting on errors,    */
  132.             if (!retval)        /* but there wasn't any        */
  133.                 break;            /* error, then don't prompt    */
  134. #endif
  135.                                 /* else fall through to YES */
  136.         case YES:
  137.             puts("\nPress RETURN to continue . . .");
  138.             (Void) getchar();
  139.     }
  140.                                 /* Upon return from shell,    */
  141.     post_shell();                /* restore everything        */
  142. }
  143.  
  144.  
  145. /************************************************************
  146.  * show_cmnd():
  147.  *    Show a directly executable action string.
  148.  ************************************************************/
  149.  
  150. int show_cmnd(Ip, path)
  151. ITEM *Ip;
  152. char *path;
  153. {
  154.     char c, *cmd_line;
  155.     int retval;
  156.  
  157.     cmd_line = make_cmd(path, Ip -> action);
  158.     c = put_msg(0, " Action: %s. Press any key (x to exit): ", cmd_line);
  159.     if (tolower(c) == 'x')
  160.     {                                /* If user presses 'x' while */
  161.         strcpy(sav_cmd, cmd_line);    /* viewing an action text,   */
  162.         return EXITALL;                /* exit and show the text.     */
  163.     }
  164.     return OK;
  165. }
  166.  
  167.  
  168. /**************************************************************
  169.  * do_emenu():
  170.  *    Run an external menu specification.
  171.  **************************************************************/
  172.  
  173. int do_emenu(Ip, path)
  174. ITEM *Ip;
  175. char *path;
  176. {
  177.     char filename[MAX_PATH];
  178.     int retval;
  179.     
  180.     if (nestlev == MAX_NEST - 1)
  181.         return fatal("Too many nested external menus (increase MAX_NEST)");
  182.  
  183.     strcpy(filename, make_path(path, Ip -> action));
  184.  
  185.     nestlev++;
  186.     retval = do_menu("", filename);
  187.     nestlev--;
  188.  
  189.     return retval;
  190. }
  191.  
  192.  
  193. /************************************************************
  194.  * pre_shell()
  195.  *    Set tty mode for a shell call
  196.  *    DOS only: save drive and path for later restoration
  197.  ************************************************************/
  198.  
  199.  
  200. Void pre_shell()
  201. {
  202.     push_path();
  203.     tty_shell();
  204. }
  205.  
  206.  
  207. /************************************************************
  208.  * post_shell()
  209.  *    Set tty mode for curses
  210.  *    DOS only: restore drive and path from before shell call
  211.  ************************************************************/
  212.  
  213. Void post_shell()
  214. {
  215.     pop_path();
  216.     tty_curses();
  217. }
  218.  
  219.  
  220.