00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
00086
00087 int32_t CurrentPropertySequence() const;
00088
00089
00090 bool GetEchoFlag() const;
00091 bool GetTraceFlag() const;
00092 bool GetColorFlag() const;
00093
00094 SString CurDir() const;
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
00167 BUnixCommand( const BUnixCommand& rhs );
00168
00169
00170 BUnixCommand& operator=( const BUnixCommand& rhs );
00171 };
00172
00173 #if _SUPPORTS_NAMESPACE
00174 } }
00175 #endif
00176
00177 #endif // APP_BCOMMAND_H