Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
5.4 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #ifndef IPROCESSUTILS_H
  7. #define IPROCESSUTILS_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "appframework/iappsystem.h"
  12. #include "tier1/utlstring.h"
  13. #include "tier1/utlbuffer.h"
  14. //-----------------------------------------------------------------------------
  15. // Handle to a process. This is only for b/w compatibility.
  16. //-----------------------------------------------------------------------------
  17. class IProcess;
  18. //-----------------------------------------------------------------------------
  19. // Interface version
  20. //-----------------------------------------------------------------------------
  21. #define PIPEREAD_INFINITE INT32_MAX
  22. abstract_class IPipeRead
  23. {
  24. public:
  25. // NONBLOCKING FUNCTIONS
  26. // Returns how much you can Read() without blocking.
  27. virtual int GetNumBytesAvailable() = 0;
  28. // Read whatever is available without blocking.
  29. // This is the same as Read( sStr, GetNumBytesAvailable() ).
  30. virtual void ReadAvailable( CUtlString &sStr, int32 nMaxBytes=PIPEREAD_INFINITE ) = 0;
  31. virtual void ReadAvailable( CUtlBuffer* pOutBuffer, int32 nMaxBytes=PIPEREAD_INFINITE ) = 0;
  32. // (POTENTIALLY) BLOCKING FUNCTIONS
  33. // Read one line of output (also returns when the process quits).
  34. // sStr will not include the \n (or \r\n) at the end of the line.
  35. virtual void ReadLine( CUtlString &sStr ) = 0;
  36. // This will block the calling thread until it gets the number of bytes specified
  37. // or until the process exits. If sStr.Length() != nBytes, then you know the process exited.
  38. //
  39. // The returned string will always be null-terminated.
  40. // If you call with nBytes=PIPEREAD_INFINITE, it'll read until the process exits.
  41. virtual void Read( CUtlString &sStr, int32 nBytes=PIPEREAD_INFINITE ) = 0;
  42. };
  43. abstract_class IProcess
  44. {
  45. public:
  46. // Note: If the process is still running, this will auto kill it unless you started the process with
  47. // STARTPROCESS_NOAUTOKILL.
  48. virtual void Release() = 0;
  49. // Kill the running process. You still must call IProcess::Release to free the resources.
  50. virtual void Abort() = 0;
  51. // Returns true if a process is complete
  52. virtual bool IsComplete() = 0;
  53. // Waits until a process is complete.
  54. // Returns the return value from the process.
  55. virtual int WaitUntilComplete() = 0;
  56. // Write to the process' stdin.
  57. // This blocks until the process has read it.
  58. virtual int WriteStdin( char *pBuf, int nBufLen ) = 0;
  59. // Get stuff to read the outputs.
  60. virtual IPipeRead* GetStdout() = 0;
  61. virtual IPipeRead* GetStderr() = 0; // NOTE: Only valid if you used STARTPROCESS_SEPARATE_STDERR.
  62. // Returns the exit code for the process. Doesn't work unless the process is complete.
  63. // Returns -1 on error or if the process isn't complete.
  64. virtual int GetExitCode() = 0;
  65. };
  66. // Flags to IProcessUtils::StartProcess.
  67. #define STARTPROCESS_CONNECTSTDPIPES 0x01 // Necessary to use the stdin/stdout/stderr io functions.
  68. #define STARTPROCESS_SHARE_CONSOLE 0x02 // The process writes directly to your console. The pipe objects returned by
  69. // IProcess::GetStdout and GetStderr won't do anything.
  70. #define STARTPROCESS_SEPARATE_STDERR 0x04 // Rather than having to read stdout and stderr to get the output, the default is to put the stderr output into stdout.
  71. // This flag can change that behavior so you can get that output separately.
  72. // Warning: There may be deadlock problems with this, specifically in CProcessPipeRead::GetActualProcessOutput if
  73. // it's blocked reading stdout's pipe but the process is blocked waiting for us to flush stderr's pipe first.
  74. // To fully support that case, we'd need threads, overlapped IO, or a more careful (and slower) GetActualProcessOutput call
  75. // that bounces between the two pipes and never stalls.
  76. //
  77. // You can also get around this on the client side by reading the pipes from threads.
  78. #define STARTPROCESS_NOAUTOKILL 0x08 // Prevents the process from being auto-terminated in IProcess::Release()
  79. // or when IProcessUtils' Shutdown function is called.
  80. #define STARTPROCESS_FATPIPES 0x10 // Use I/O pipes larger than the default size for processes that do lots of stdio
  81. // (Only works with STARTPROCESS_CONNECTSTDPIPES)
  82. //-----------------------------------------------------------------------------
  83. // Interface for makefiles to build differently depending on where they are run from
  84. //-----------------------------------------------------------------------------
  85. abstract_class IProcessUtils : public IAppSystem
  86. {
  87. public:
  88. // Starts, stops a process.
  89. // If pWorkingDir is left at NULL, it'll use this process' working directory.
  90. virtual IProcess* StartProcess( const char *pCommandLine, int fFlags, const char *pWorkingDir=NULL )= 0;
  91. virtual IProcess* StartProcess( int argc, const char **argv, int fFlags, const char *pWorkingDir=NULL ) = 0;
  92. // Run a process and get its output.
  93. // If pStdout is set, then stdout AND stderr are put into pStdout.
  94. // If not, then the text output is ignored.
  95. //
  96. // Returns -1 if it was unable to run the process. Otherwise, returns the exit code from the process.
  97. virtual int SimpleRunProcess( const char *pCommandLine, const char *pWorkingDir=NULL, CUtlString *pStdout=NULL ) = 0;
  98. };
  99. DECLARE_TIER1_INTERFACE( IProcessUtils, g_pProcessUtils );
  100. #endif // IPROCESSUTILS_H