home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / FACETV.ZIP / DOSSHELL.CPP < prev    next >
C/C++ Source or Header  |  1994-01-04  |  3KB  |  97 lines

  1. /************************************************************************
  2. **
  3. ** @(#)dosshell.cpp    04/01/93    Chris Ahlstrom
  4. **
  5. **  ------------------------
  6. **  73340.26!compuserve.com
  7. **  ------------------------
  8. **
  9. **    Implements a simple shell to DOS.
  10. **
  11. *************************************************************************/
  12.  
  13. #define DOSSHELL_cpp
  14.  
  15. #include <stdlib.h>             // for exit(), random()
  16. #include <string.h>             // for strlen etc
  17. #include <iostream.h>        // for cout
  18.  
  19. #include "dosshell.h"
  20.  
  21.  
  22. /************************************************************************
  23. ** DosShell::DosShell
  24. *************************************************************************/
  25.  
  26. DosShell::DosShell                // DOS Shell Command
  27. (
  28.     char *newp                // the prompt for new COMMAND.COM
  29. )
  30. {
  31.     newP = newp;            // NULL can be used if desired
  32. }
  33.  
  34.  
  35. /************************************************************************
  36. ** DosShell::shell
  37. **
  38. **    This routine provides a nice way to get out to DOS to look at
  39. ** files and things.  I'm not sure this is robust against a lack of
  40. ** memory to shell out.
  41. **
  42. **    The basic method is to turn off the mouse, get the old DOS
  43. ** prompt and save it, put out the new DOS prompt, test for a command
  44. ** processor, then clear the screen, and shell out to a new copy of
  45. ** the command-line interpreter in the environment variable COMSPEC.
  46. ** (Might need to handle cases where COMSPEC (and SHELL) are not
  47. ** specified).
  48. **
  49. **    Also, note the lack of use of malloc() and free().  As shown in
  50. ** that excellent book "Effective C++" by Scott Meyers (Addison-Wesley
  51. ** Publishing Company), these should be always be converted to new and
  52. ** delete.
  53. **
  54. **    The caller must redraw() itself, if necessary.
  55. **
  56. *************************************************************************/
  57.  
  58. #define PROMPT_NAME    "PROMPT"            // DOS variable
  59. #define CLEAR_SCREEN    "cls"                // DOS clear-screen
  60. #define FIRST_PROMPT    "Type EXIT to return..."    // initial message
  61. #define COMMAND_SPEC    "COMSPEC"            // the shell's name
  62.  
  63. void
  64. DosShell::shell ()            // DOS Shell Command
  65. {
  66.     // Get and save the old value of the DOS PROMPT environment variable
  67.  
  68.     suspend();                // turn off the mouse
  69.  
  70.     P = getenv(PROMPT_NAME);        // get location of old prompt
  71.     oldP = new char [strlen(P) + 1];    // allocate a place for the prompt
  72.     if (oldP)
  73.     {
  74.     strcpy(oldP, P);        // store the prompt there for awhile
  75.     if (newP)            // if a prompt was provided, then
  76.         putenv(newP);        // change PROMPT to remind the user
  77.  
  78.     shIs = system(NULL);        // test for a command processor
  79.     if (shIs != 0)            // can we get a command processor?
  80.     {
  81.         shIs = system(CLEAR_SCREEN);    // clear the screen
  82.         cout << FIRST_PROMPT;        // make clear what's happening
  83.         shIs = system(getenv(COMMAND_SPEC));// spawn out a command shell
  84.         putenv(oldP);            // restore the old prompt
  85.         if (shIs == -1)
  86.         {
  87.         // error processing goes here for errno = one of...
  88.         //
  89.         //    ENOENT   ENOMEM   E2BIG   ENOEXEC
  90.         }
  91.     }
  92.     delete [] oldP;            // free up the scratch space
  93.     }
  94.     resume();                // turn on the mouse
  95. }
  96.  
  97.