/// TKS_Process.h
///
/// (c) 2018 Bastian Spiegel <bs@tkscript.de>
///     - distributed under terms of the GNU general public license (GPL).
///
#ifndef __TKS_PROCESS_H__
#define __TKS_PROCESS_H__


#ifdef YAC_WIN32
#include <strsafe.h>
#include <intsafe.h>
#include <wchar.h>
#include <windows.h>
#endif // YAC_WIN32

#ifdef YAC_LINUX
#include <unistd.h>
#include <sys/socket.h>

#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <wait.h>
#include <sys/ioctl.h>
#endif // YAC_LINUX


class TKS_Process : public YAC_StreamBase {
public:
#ifdef YAC_LINUX
   int fd1[2];
   int fd2[2];
   pid_t pid;
#endif // YAC_LINUX

#ifdef YAC_WIN32
   HANDLE child_in_rd;
   HANDLE child_in_wr;
   HANDLE child_out_rd;
   HANDLE child_out_wr;
   PROCESS_INFORMATION piProcInfo; 
#endif // YAC_WIN32
   
public:
   TKS_Process(void);
   ~TKS_Process();
   
   sUI   YAC_VCALL yacStreamGetSize      (void);
   sU8   YAC_VCALL yacStreamReadI8       (void); 
   sSI   YAC_VCALL yacStreamReadString   (YAC_String *, sUI _maxlen);
   sSI   YAC_VCALL yacStreamReadBuffer   (YAC_Buffer *_b, sUI _off, sUI _max, sBool _resize); 
   sSI   YAC_VCALL yacStreamWrite        (sU8 *, sUI _num);
   void  YAC_VCALL yacStreamWriteI8      (sU8); 
   sBool YAC_VCALL yacIsStream           (void);

  public:
   // Create new process and execute command
   //  - cmd: full command pathname (e.g. "/bin/ls")
   //  - args: StringArray or null
   sBool exec (YAC_String *_cmd, YAC_Object *_args);

   // Query number of available bytes. Wait up to <timeoutMillisec> for new data to arrive.
   // 
   sUI waitAvail (sUI _timeoutMillisec);

   // Send SIGTERM, wait 1sec, then send SIGKILL
   void kill (void);
 
};


#endif // __YAC_BUFFER_H__
