home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cmdline.lha / cmdline / src / cmd / shells.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-03  |  11.7 KB  |  513 lines

  1. //------------------------------------------------------------------------
  2. // ^FILE: shells.h - define classes for the various Unix shells
  3. //
  4. // ^DESCRIPTION:
  5. //     This file encapsulates all the information that cmdparse(1) needs
  6. //     to know about each of the various shells that it will support.
  7. //
  8. //     To add a new shell to the list of shells supported here:
  9. //        1) Add its class definition in this file.
  10. //
  11. //        2) Implement its member functions in "shells.h"
  12. //           (dont forget the NAME data-member to hold the name).
  13. //
  14. //        3) Add an "else if" statement for the new shell into
  15. //           the virtual constructor UnixShell::UnixShell(const char *).
  16. //
  17. // ^HISTORY:
  18. //    04/19/92    Brad Appleton    <brad@ssd.csd.harris.com>    Created
  19. //-^^---------------------------------------------------------------------
  20.  
  21. #ifndef _shells_h
  22. #define _shells_h
  23.  
  24.    // A ShellVariable object houses the name and value of a shell
  25.    // environment variable.
  26.    //
  27. class  ShellVariable {
  28. public:
  29.    ShellVariable(const char * name);
  30.  
  31.    virtual ~ShellVariable(void);
  32.  
  33.       // Return the name of this variable
  34.    const char *
  35.    name(void) const { return  var_name ; }
  36.  
  37.       // Set the value of this variable
  38.    void
  39.    set(const char * value) { var_value = value; }
  40.  
  41.       // Return the value of this variable
  42.    const char *
  43.    value(void) const { return  var_value; }
  44.  
  45. protected:
  46.    const char * var_name ;
  47.    const char * var_value ;
  48. };
  49.  
  50.  
  51.    // A ShellArray object houses the name and values of a shell array.
  52.    //
  53. struct  ShellArrayValues;
  54. class  ShellArray {
  55. public:
  56.    ShellArray(const char * name);
  57.  
  58.    virtual ~ShellArray(void);
  59.  
  60.       // Return the name of this array
  61.    const char *
  62.    name(void) const { return  array_name; }
  63.  
  64.       // Append to the list of values in this array
  65.    void
  66.    append(const char * value);
  67.  
  68.       // Return the number of items in this array.
  69.    unsigned
  70.    count(void) const;
  71.  
  72.       // Return the desired element of an array
  73.       //
  74.       // NOTE: the elements range in index from 0 .. count-1,
  75.       //       an out-of-range index will result in a run-time
  76.       //       NULL-ptr dereferencing error!
  77.       //
  78.    const char *
  79.    operator[](unsigned  index) const;
  80.  
  81. protected:
  82.    const char       * array_name ;
  83.    ShellArrayValues * array_value ;
  84. } ;
  85.  
  86.  
  87.    // AbstractUnixShell is an abstract class for an arbitrary Unix shell
  88.    // program.  It represents all the functionality that cmdparse(1)
  89.    // requires of a command-interpreter.
  90.    //
  91. class  AbstractUnixShell {
  92. public:
  93.    virtual
  94.    ~AbstractUnixShell(void);
  95.  
  96.       // Return the name of this shell
  97.    virtual  const char *
  98.    name(void) const = 0;
  99.  
  100.       // Does "name" correspond to the positional-parameters for this shell?
  101.    virtual  int
  102.    is_positionals(const char * name) const = 0;
  103.  
  104.       // Unset the positional parameters of this shell.
  105.       //
  106.       // The parameter "name" is the name of a shell variable
  107.       // for which is_positionals() returns TRUE.
  108.       //
  109.    virtual  void
  110.    unset_args(const char * name) const = 0;
  111.  
  112.       // Set the given variable name to the given value
  113.    virtual  void
  114.    set(const ShellVariable & variable) const = 0;
  115.  
  116.       // Set the given array name to the given values.
  117.       // Some shells have more than one way to set an array.
  118.       // Such shells should label these varying methods as
  119.       // variant0 .. variantN, the desired variant method to use
  120.       // (which defaults to zero), should be indicated by the
  121.       // last parameter.
  122.       //
  123.       // This member function is responsible for checking to see
  124.       // if the array name corresponds to the positional-parameters
  125.       // (and for behaving accordingly if this is the case).
  126.       //
  127.    virtual  void
  128.    set(const ShellArray & array, int variant) const = 0;
  129.  
  130. protected:
  131.    AbstractUnixShell(void) {};
  132.  
  133. } ;
  134.  
  135.  
  136.    // UnixShell is used as an envelope class (using its siblings as
  137.    // letter classes). It is a "shell" that does not decide what
  138.    // type of shell it is until runtime.
  139.    // 
  140. class  UnixShell {
  141. public:
  142.       // This is a virtual constructor that constructs a Unix shell object
  143.       // that is the appropriate derived class of AbstractUnixShell.
  144.       //
  145.    UnixShell(const char * shell_name);
  146.  
  147.    virtual
  148.    ~UnixShell(void);
  149.  
  150.       // See if this shell is valid
  151.    int
  152.    is_valid(void) const { return  (valid) ? 1 : 0; }
  153.  
  154.       // Return the name of this shell
  155.    virtual  const char *
  156.    name(void) const;
  157.  
  158.    virtual  void
  159.    unset_args(const char * name) const;
  160.  
  161.    virtual  int
  162.    is_positionals(const char * name) const;
  163.  
  164.    virtual  void
  165.    set(const ShellVariable & variable) const;
  166.  
  167.    virtual  void
  168.    set(const ShellArray & array, int variant) const;
  169.  
  170. private:
  171.    unsigned    valid : 1 ;
  172.    AbstractUnixShell * shell;
  173.  
  174. } ;
  175.  
  176.  
  177.    // BourneShell (sh) - the most common of the Unix Shells - implemented
  178.    //                    by Stephen R. Bourne
  179.    //
  180.    // Variables are set using:
  181.    //      name='value';
  182.    //
  183.    // Arrays (by default) are set using:
  184.    //      name='value1 value2 value3 ...';
  185.    //
  186.    // but if requested, the following array-variant will be used instead:
  187.    //      name_count=N;
  188.    //      name1='value1';
  189.    //      name2='value2';
  190.    //          ...
  191.    //      nameN='valueN';
  192.    //
  193.    // If a variable name matches one of "@", "*", "-", or "--", then the 
  194.    // variable is assumed to refer to the positional-parameters of the
  195.    // shell-script and the following syntax will be used:
  196.    //      set -- 'value1' 'value2' 'value3' ...
  197.    //
  198. class  BourneShell : public  AbstractUnixShell {
  199. public:
  200.    BourneShell(void);
  201.  
  202.    virtual  ~BourneShell(void);
  203.  
  204.    virtual  const char *
  205.    name(void) const;
  206.  
  207.    virtual  void
  208.    unset_args(const char * name) const;
  209.  
  210.    virtual  int
  211.    is_positionals(const char * name) const;
  212.  
  213.    virtual  void
  214.    set(const ShellVariable & variable) const;
  215.  
  216.    virtual  void
  217.    set(const ShellArray & array, int variant) const;
  218.  
  219.    static const char * NAME ;
  220.  
  221. protected:
  222.    void
  223.    escape_value(const char * value) const;
  224.  
  225. private:
  226.  
  227. } ;
  228.  
  229.  
  230.    // KornShell (ksh) -- David G. Korn's reimplementation of the Bourne shell
  231.    //
  232.    // Variables are set using the same syntax as in the Bourne Shell.
  233.    //
  234.    // Arrays (by default) are set using:
  235.    //      set -A name 'value1' 'value2' 'value3' ...;
  236.    //
  237.    // but if requested, the following array-variant will be used instead:
  238.    //      set +A name 'value1' 'value2' 'value3' ...;
  239.    //
  240.    // If a variable name matches one of "@", "*", "-", or "--", then the 
  241.    // variable is assumed to refer to the positional-parameters of the
  242.    // shell-script and the following syntax will be used:
  243.    //      set -- 'value1' 'value2' 'value3' ...
  244.    //
  245. class  KornShell : public BourneShell {
  246. public:
  247.    KornShell(void);
  248.  
  249.    virtual  ~KornShell(void);
  250.  
  251.    virtual  const char *
  252.    name(void) const;
  253.  
  254.    virtual  void
  255.    unset_args(const char * name) const;
  256.  
  257.    virtual  void
  258.    set(const ShellVariable & variable) const;
  259.  
  260.    virtual  void
  261.    set(const ShellArray & array, int variant) const;
  262.  
  263.    static const char * NAME ;
  264.  
  265. protected:
  266.  
  267. private:
  268.  
  269. } ;
  270.  
  271.  
  272.    // BourneAgainShell (bash) -- The Free Software Foundation's answer to ksh
  273.    //
  274.    // bash is treated exactlt like the Bourne Shell, this will change when
  275.    // bash supports arrays.
  276.    //
  277. class  BourneAgainShell : public BourneShell {
  278. public:
  279.    BourneAgainShell(void);
  280.  
  281.    virtual  ~BourneAgainShell(void);
  282.  
  283.    virtual  const char *
  284.    name(void) const;
  285.  
  286.    virtual  void
  287.    set(const ShellVariable & variable) const;
  288.  
  289.    virtual  void
  290.    set(const ShellArray & array, int variant) const;
  291.  
  292.    static const char * NAME ;
  293.  
  294. protected:
  295.  
  296. private:
  297.  
  298. } ;
  299.  
  300.  
  301.    // CShell (csh) -- Bill Joy's rewrite of "sh" with C like syntax.
  302.    //                 this will work for tcsh and itcsh as well.
  303.    //
  304.    // Variables are set using:
  305.    //      set name='value';
  306.    //
  307.    // Arrays (by default) are set using:
  308.    //      set name=('value1' 'value2' 'value3' ...);
  309.    //
  310. class  CShell : public AbstractUnixShell {
  311. public:
  312.    CShell(void);
  313.  
  314.    virtual  ~CShell(void);
  315.  
  316.    virtual  const char *
  317.    name(void) const;
  318.  
  319.    virtual  void
  320.    unset_args(const char * name) const;
  321.  
  322.    virtual  int
  323.    is_positionals(const char * name) const;
  324.  
  325.    virtual  void
  326.    set(const ShellVariable & variable) const;
  327.  
  328.    virtual  void
  329.    set(const ShellArray & array, int variant) const;
  330.  
  331.    static const char * NAME ;
  332.  
  333. protected:
  334.    void
  335.    escape_value(const char * value) const;
  336.  
  337. private:
  338.  
  339. } ;
  340.  
  341.  
  342.    // ZShell (zsh) -- Paul Falstad's shell combining lots of stuff from
  343.    //                 csh and ksh and some stuff of his own.
  344.    //
  345.    // Variables are set using:
  346.    //    name='value';
  347.    //
  348.    // Arrays are set using:
  349.    //    name=('value1' 'value2' 'value3' ...);
  350.    //
  351.    // If a variable name matches one of "@", "*", "-", "--", or "argv" then
  352.    // the variable is assumed to refer to the positional-parameters of the
  353.    // shell-script and the following syntax will be used:
  354.    //    argv=('value1' 'value2' 'value3' ...);
  355.    //
  356. class  ZShell : public AbstractUnixShell {
  357. public:
  358.    ZShell(void);
  359.  
  360.    virtual  ~ZShell(void);
  361.  
  362.    virtual  const char *
  363.    name(void) const;
  364.  
  365.    virtual  void
  366.    unset_args(const char * name) const;
  367.  
  368.    virtual  int
  369.    is_positionals(const char * name) const;
  370.  
  371.    virtual  void
  372.    set(const ShellVariable & variable) const;
  373.  
  374.    virtual  void
  375.    set(const ShellArray & array, int variant) const;
  376.  
  377.    static const char * NAME ;
  378.  
  379. protected:
  380.    void
  381.    escape_value(const char * value) const;
  382.  
  383. private:
  384.  
  385. } ;
  386.  
  387.  
  388.    // Plan9Shell (rc) -- Tom Duff's shell from the Plan 9 papers.
  389.    //        A public domain version (with some enhancements) was
  390.    //        written by Byron Rakitzis.
  391.    //
  392.    // Variables are set using:
  393.    //    name='value';
  394.    //
  395.    // Arrays are set using:
  396.    //    name=('value1' 'value2' 'value3' ...);
  397.    //
  398. class  Plan9Shell : public AbstractUnixShell {
  399. public:
  400.    Plan9Shell(void);
  401.  
  402.    virtual  ~Plan9Shell(void);
  403.  
  404.    virtual  const char *
  405.    name(void) const;
  406.  
  407.    virtual  void
  408.    unset_args(const char * name) const;
  409.  
  410.    virtual  int
  411.    is_positionals(const char * name) const;
  412.  
  413.    virtual  void
  414.    set(const ShellVariable & variable) const;
  415.  
  416.    virtual  void
  417.    set(const ShellArray & array, int variant) const;
  418.  
  419.    static const char * NAME ;
  420.  
  421. protected:
  422.    void
  423.    escape_value(const char * value) const;
  424.  
  425. private:
  426.  
  427. } ;
  428.  
  429.  
  430.    // Perl (perl) -- Larry Wall's Practical Extraction and Report Generation
  431.    //                utility.
  432.    //
  433.    // Variables are set using:
  434.    //    $name = 'value';
  435.    //
  436.    // Arrays are set using:
  437.    //    @name = ('value1', 'value2', 'value3', ...);
  438.    //
  439. class  PerlShell : public AbstractUnixShell {
  440. public:
  441.    PerlShell(void);
  442.  
  443.    virtual  ~PerlShell(void);
  444.  
  445.    virtual  const char *
  446.    name(void) const;
  447.  
  448.    virtual  void
  449.    unset_args(const char * name) const;
  450.  
  451.    virtual  int
  452.    is_positionals(const char * name) const;
  453.  
  454.    virtual  void
  455.    set(const ShellVariable & variable) const;
  456.  
  457.    virtual  void
  458.    set(const ShellArray & array, int variant) const;
  459.  
  460.    static const char * NAME ;
  461.  
  462. protected:
  463.    void
  464.    escape_value(const char * value) const;
  465.  
  466. private:
  467.  
  468. } ;
  469.  
  470.  
  471.    // Tcl -- Karl Lehenbauer and friends implementation of a shell based
  472.    //        on John K. Ousterhout's Tool Command Language
  473.    //
  474.    // Variables are set using:
  475.    //    set name "value";
  476.    //
  477.    // Arrays are set using:
  478.    //     set name [list "value1" "value2" "value3" ...];
  479.    //
  480. class  TclShell : public AbstractUnixShell {
  481. public:
  482.    TclShell(void);
  483.  
  484.    virtual  ~TclShell(void);
  485.  
  486.    virtual  const char *
  487.    name(void) const;
  488.  
  489.    virtual  void
  490.    unset_args(const char * name) const;
  491.  
  492.    virtual  int
  493.    is_positionals(const char * name) const;
  494.  
  495.    virtual  void
  496.    set(const ShellVariable & variable) const;
  497.  
  498.    virtual  void
  499.    set(const ShellArray & array, int variant) const;
  500.  
  501.    static const char * NAME ;
  502.  
  503. protected:
  504.    void
  505.    escape_value(const char * value) const;
  506.  
  507. private:
  508.  
  509. } ;
  510.  
  511.  
  512. #endif  /* _shells_h */
  513.