home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples1.exe / Debugger / shell.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.3 KB  |  215 lines

  1. /* ------------------------------------------------------------------------- *
  2.  * debug\shell.h: generic shell class
  3.  * ------------------------------------------------------------------------- */
  4.  
  5. #ifndef SHELL_H_
  6. #define SHELL_H_
  7.  
  8. #include <string.h>
  9.  
  10. /* ------------------------------------------------------------------------- *
  11.  * Class forward declations
  12.  * ------------------------------------------------------------------------- */
  13.  
  14. class Shell;
  15. class ShellCommand;
  16. class ShellCommandTable;
  17. class HelpShellCommand;
  18.  
  19. /* ------------------------------------------------------------------------- *
  20.  * Abstract ShellCommand class
  21.  *
  22.  * All commands that the shell will support must be derived from this class.
  23.  * ------------------------------------------------------------------------- */
  24.  
  25. class ShellCommand
  26. {
  27. protected:
  28.     // The name of the command (what the user types in)
  29.     const WCHAR *m_pName;
  30.  
  31.     // The minimum subset of name that must be typed in
  32.     int m_minMatchLength;
  33.  
  34. public:
  35.     ShellCommand(const WCHAR *name, int MatchLength = 0);
  36.  
  37.     virtual ~ShellCommand()
  38.     {
  39.     }
  40.  
  41.     /*********************************************************************
  42.      * Methods
  43.      */
  44.  
  45.     /*
  46.      * Executes the shell command
  47.      */
  48.     virtual void Do(Shell *shell, const WCHAR *args) = 0;
  49.  
  50.     /*
  51.      * Displays a help message to the user
  52.      */
  53.     virtual void Help(Shell *shell);
  54.  
  55.     /*
  56.      * Returns a short help message for the user
  57.      */
  58.     virtual const WCHAR *ShortHelp(Shell *shell)
  59.     {
  60.         // Name is a good default.
  61.         return m_pName;
  62.     }
  63.  
  64.     /*
  65.      * Returns the name of the command
  66.      */
  67.     const WCHAR *GetName()
  68.     {
  69.         return m_pName;
  70.     }
  71.  
  72.     /*
  73.      * Returns the minimum match length
  74.      */
  75.     int GetMinMatchLength()
  76.     {
  77.         return m_minMatchLength;
  78.     }
  79. };
  80.  
  81. /* ------------------------------------------------------------------------- *
  82.  * Abstract Shell class
  83.  *
  84.  * A basic outline of a command shell, used by the debugger.
  85.  * ------------------------------------------------------------------------- */
  86.  
  87. const int BUFFER_SIZE = 1024;
  88.  
  89. class Shell
  90. {
  91. private:
  92.     // The collection of the available commands
  93.     ShellCommandTable *m_pCommands;
  94.  
  95.     // The last command executed
  96.     WCHAR m_lastCommand[BUFFER_SIZE];
  97.  
  98.     // A buffer for reading input
  99.     WCHAR m_buffer[BUFFER_SIZE];
  100.  
  101. protected:
  102.     // The input prompt
  103.     WCHAR *m_pPrompt;
  104.  
  105. public:
  106.     Shell();
  107.     ~Shell();
  108.  
  109.     /*********************************************************************
  110.      * Shell I/O routines
  111.      */
  112.  
  113.     /*
  114.      * Read a line of input from the user, getting a maximum of maxCount chars
  115.      */
  116.     virtual bool ReadLine(WCHAR *buffer, int maxCount) = 0;
  117.  
  118.     /*
  119.      * Write a line of output to the shell
  120.      */
  121.     virtual void Write(const WCHAR *buffer, ...) = 0;
  122.  
  123.     /*
  124.      * Write an error to the shell
  125.      */
  126.     virtual void Error(const WCHAR *buffer, ...) = 0;
  127.  
  128.     void ReportError(long res);
  129.     void SystemError();
  130.  
  131.     /*********************************************************************
  132.      * Shell functionality
  133.      */
  134.  
  135.     /*
  136.      * This will add a command to the collection of available commands
  137.      */
  138.     void AddCommand(ShellCommand *command);
  139.  
  140.     /*
  141.      * This will get a command from the available commands by matching the
  142.      * command name with string.
  143.      */
  144.     ShellCommand *GetCommand(const WCHAR *string, size_t length);
  145.  
  146.     /*
  147.      * This will get a command from the available commands by matching the
  148.      * command name with string.
  149.      */
  150.     void PutCommand(FILE *f);
  151.  
  152.     /*
  153.      * This will read a command from the user
  154.      */
  155.     void ReadCommand();
  156.  
  157.     /*
  158.      * This will attempt to match string with a command and execute it with
  159.      * the arguments following the command string.
  160.      */
  161.     void DoCommand(const WCHAR *string);
  162.  
  163.     /*
  164.      * This will provide a listing of the commands available to the shell.
  165.      */
  166.     void Help();
  167.  
  168.     // utility methods: 
  169.     bool GetStringArg(const WCHAR * &string, const WCHAR * &result);
  170.     bool GetIntArg(const WCHAR * &string, int &result);
  171.     bool GetInt64Arg(const WCHAR * &string, unsigned __int64 &result);
  172.  
  173.     size_t GetArgArray(WCHAR *argString, const WCHAR **argArray, size_t argMax);
  174.  
  175.     const WCHAR *GetPrompt()
  176.     {
  177.         return m_pPrompt;
  178.     }
  179.  
  180.     void SetPrompt(const WCHAR *prompt)
  181.     {
  182.         m_pPrompt = new WCHAR[wcslen(prompt)];
  183.         wcscpy(m_pPrompt, prompt);
  184.     }
  185. };
  186.  
  187. /* ------------------------------------------------------------------------- *
  188.  * Predefined command classes
  189.  * ------------------------------------------------------------------------- */
  190.  
  191. class HelpShellCommand : public ShellCommand
  192. {
  193. public:
  194.     HelpShellCommand(const WCHAR *name, int minMatchLength = 0)
  195.     : ShellCommand(name, minMatchLength)
  196.     {
  197.         
  198.     }
  199.  
  200.     /*
  201.      * This will display help for the command given in args, or help on the
  202.      * help command if args is empty.
  203.      */
  204.     void Do(Shell *shell, const WCHAR *args);
  205.  
  206.     /*
  207.      * This will provide help for the help command.
  208.      */
  209.     void Help(Shell *shell);
  210.  
  211.     const WCHAR *ShortHelp(Shell *shell);
  212. };
  213.  
  214. #endif /* SHELL_H_ */
  215.