BCommand.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef APP_BCOMMAND_H
00014 #define APP_BCOMMAND_H
00015 
00016 #include <support/Binder.h>
00017 #include <support/Catalog.h>
00018 #include <support/IByteStream.h>
00019 #include <support/IInterface.h>
00020 #include <support/ITextStream.h>
00021 #include <support/KeyedVector.h>
00022 #include <support/Locker.h>
00023 #include <support/String.h>
00024 #include <support/Value.h>
00025 #include <support/Vector.h>
00026 
00027 #include <app/ICommand.h>
00028 
00029 #if _SUPPORTS_NAMESPACE
00030 namespace palmos {
00031 namespace app {
00032 using namespace palmos::support;
00033 #endif
00034 
00035 class BCommand : public BnCommand
00036 {
00037 public:
00038                             BCommand();
00039                             BCommand(const SContext& context);
00040     
00041     virtual SValue          Run(const ArgList& args) = 0;
00042     virtual sptr<ICommand>  Clone() const;
00043     virtual sptr<ICommand>  Spawn(const SString & command) const;
00044 
00045     virtual void            AddCommand(const SString& name, const sptr<ICommand>& command);
00046     virtual sptr<ICommand>  GetCommand(const SString& name) const;
00047     virtual void            RemoveCommand(const SString& name);
00048 
00049     virtual SValue          GetProperty(const SValue& key) const;   
00050     virtual void            SetProperty(const SValue& key, const SValue& value);
00051     virtual void            RemoveProperty(const SValue& key);
00052 
00053     virtual SValue          Environment() const;
00054     
00055     virtual void            SetEnvironment(const SValue& env);
00056     virtual void            SetFileDescriptors(const SValue& fds);
00057 
00058     virtual void            SetByteInput(const sptr<IByteInput>& input);
00059     virtual void            SetByteOutput(const sptr<IByteOutput>& output);
00060     virtual void            SetByteError(const sptr<IByteOutput>& error);
00061 
00062     virtual sptr<IByteInput>        ByteInput() const;
00063     virtual sptr<IByteOutput>       ByteOutput() const;
00064     virtual sptr<IByteOutput>       ByteError() const;
00065 
00066             sptr<ITextInput>        TextInput();
00067             sptr<ITextOutput>       TextOutput();
00068             sptr<ITextOutput>       TextError();
00069 
00070             sptr<ICommand>  SpawnCustom(const SString & command,
00071                                         const SContext& context,
00072                                         const sptr<IProcess>& team) const;
00073 
00075 
00077             SString         ArgToPath(const SValue& arg) const;
00078 
00080 
00083             sptr<IBinder>   ArgToBinder(const SValue& arg) const;
00084             
00085             // Returns a sequence number that is incremented each time
00086             // the command's environment properties change.
00087             int32_t         CurrentPropertySequence() const;
00088 
00089             // Conveniences for checking some standard environment variables.
00090             bool            GetEchoFlag() const;        // ECHO
00091             bool            GetTraceFlag() const;       // TRACE
00092             bool            GetColorFlag() const;       // BSH_COLOR
00093 
00094             SString         CurDir() const;             // PWD
00095             
00096             FILE *          __sF(int which) const;
00097             const char*     getenv(const char* key) const;
00098             int             putenv(const char* string);
00099             int             setenv(const char *name, const char *value, int rewrite);
00100             void            unsetenv(const char *name);
00101             int             system(const char *cmd);
00102 
00104 
00108     virtual SString         Documentation() const;
00109 
00110 protected:
00111     virtual                 ~BCommand();
00112 
00113 private:
00114 
00116     static  int             FILERead(void *cookie, char *buf, int n);
00117     static  int             FILEWrite(void *cookie, const char *buf, int n);
00118     static  int             FILEClose(void *cookie);
00119 
00120     mutable SLocker             m_lock;
00121 
00122             enum                { NUM_GETS = 4 };
00123     mutable int32_t             m_lastGet;
00124     mutable SString             m_gets[NUM_GETS];
00125 
00126             sptr<IByteInput>    m_in;
00127             sptr<IByteOutput>   m_out;
00128             sptr<IByteOutput>   m_err;
00129 
00130     mutable sptr<ITextInput>    m_textIn;
00131     mutable sptr<ITextOutput>   m_textOut;
00132     mutable sptr<ITextOutput>   m_textErr;
00133 
00134     mutable FILE*               m_stdStreams[3];
00135 
00136             SValue              m_env;
00137             SValue              m_fds;
00138 
00139             int32_t             m_propSeq;
00140 
00141     mutable int32_t             m_echoSeq;
00142     mutable int32_t             m_traceSeq;
00143     mutable int32_t             m_colorSeq;
00144 
00145     mutable bool                m_echoFlag : 1;
00146     mutable bool                m_traceFlag : 1;
00147     mutable bool                m_colorFlag : 1;
00148 
00149     SKeyedVector<SString, sptr<ICommand> > m_functions;
00150 };
00151 
00152 class BUnixCommand : public BCommand
00153 {
00154 public:
00155                             BUnixCommand(const SContext& context);
00156 
00157     virtual SValue          Run(const ArgList& args);
00158 
00159 protected:
00160     virtual                 ~BUnixCommand();
00161 
00162     virtual SValue          main(int argc, char** argv) = 0;
00163 
00164 private:
00165 
00166     // copy constructor not supported
00167     BUnixCommand( const BUnixCommand& rhs );
00168 
00169     // operator= not supported
00170     BUnixCommand& operator=( const BUnixCommand& rhs );
00171 };
00172 
00173 #if _SUPPORTS_NAMESPACE
00174 } } // namespace palmos::app
00175 #endif
00176 
00177 #endif // APP_BCOMMAND_H