Branch data Line data Source code
1 : : #ifndef bqtForkPTYHH 2 : : #define bqtForkPTYHH 3 : : /** @file tty/forkpty.hh 4 : : * @brief Physical frontend to the underlying process (shell). 5 : : */ 6 : : 7 : : #include <string> 8 : : #include <csignal> 9 : : 10 : : /** ForkPTY is a communications end point between the terminal emulator 11 : : * and the underlying operating system. 12 : : * As the name suggests, it is a wrapper over the forkpty() call. 13 : : * It launches a shell in the host system. 14 : : */ 15 : : class ForkPTY 16 : : { 17 : : public: 18 : : /** Constructor. Calls Open() with its parameters. @see Open() */ 19 : 4 : ForkPTY(std::size_t width, std::size_t height) { Open(width,height); } 20 : : 21 : : /** Destructor. Terminates the subprocess if it is still active. */ 22 : : ~ForkPTY(); 23 : : 24 : : /** Create a subprocess in a virtual terminal with given dimensions. 25 : : * If the opening is successful, the file descriptor is set to non-blocking mode. 26 : : */ 27 : : void Open(std::size_t width, std::size_t height); 28 : : 29 : : /** Terminate the subprocess. */ 30 : : void Close(); 31 : : 32 : : /** @returns true if the subprocess was launched successfully. */ 33 : : bool Active() const; 34 : : 35 : : /** @returns the underlying file descriptor. */ 36 : : int getfd() const; 37 : : 38 : : /** Try to send the string as input to the subprocess in non-blocking mode. 39 : : * This function is typically called when the user types something. 40 : : * @param buffer The string to send to the subprocess; typically whatever the user typed. 41 : : * @returns the number of bytes written. 42 : : */ 43 : : int Send(std::string_view buffer); 44 : : 45 : : /** Try to receive output from the subprocess in non-blcoking mode. 46 : : * @returns The string containing raw data that was read. 47 : : * The second member contains the length of the string if successful, 48 : : * or -1 if read was unsuccesful. 49 : : */ 50 : : std::pair<std::string,int> Recv(); 51 : : 52 : : /** Send a signal to the subprocess. */ 53 : : void Kill(int signal); 54 : : 55 : : /** Inform the subprocess (or its virtual terminal) that 56 : : * the terminal has been resized to the given dimensions. 57 : : */ 58 : : void Resize(unsigned xsize, unsigned ysize); 59 : : 60 : : private: 61 : : int fd = -1, pid = -1; 62 : : }; 63 : : 64 : : #endif