edelib 2.0.0

edelib/PtyProcess.h

00001 /*
00002  * $Id: PtyProcess.h 2839 2009-09-28 11:36:20Z karijes $
00003  *
00004  * This class enables to "chat" with terminal programs synchronously
00005  * Copyright (c) 2006-2009 edelib authors
00006  *
00007  * This file was a part of the KDE project, module kdesu.
00008  * Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public License
00021  * along with this library. If not, see <http://www.gnu.org/licenses/>.
00022  */
00023 
00024 #ifndef __EDELIB_PTYPROCESS_H__
00025 #define __EDELIB_PTYPROCESS_H__
00026 
00027 #include "edelib-global.h"
00028 #include <sys/types.h>
00029 
00030 EDELIB_NS_BEGIN
00031 
00032 class PTY;
00033 
00042 class EDELIB_API PtyProcess {
00043 private:
00044         int init();
00045         int setup_tty(int fd);
00046 
00047         PTY *m_pPTY;
00048         char *m_Inbuf, *m_TTY;
00049 
00050         class PtyProcessPrivate;
00051         PtyProcessPrivate *d;
00052 
00053         E_DISABLE_CLASS_COPY(PtyProcess)
00054 protected:
00055 #ifndef SKIP_DOCS
00056         char **environment() const;
00057 
00058         bool m_bErase, m_bTerminal;
00059         int m_Pid, m_Fd;
00060         char *m_Command, *m_Exit;
00061 #endif
00062 
00063 public:
00067         PtyProcess();
00068 
00072         virtual ~PtyProcess();
00073 
00081         int exec(const char *command, const char **args);
00082 
00089         char *read_line(bool block=true);
00090 
00096         void write_line(const char *line, bool addNewline=true);
00097 
00103         void unread_line(const char *line, bool addNewline=true);
00104 
00109         void set_exit_string(char *exit) { m_Exit = exit; }
00110 
00114         int wait_for_child();
00115 
00121         int wait_slave();
00122 
00126         int enable_local_echo(bool enable=true);
00127 
00131         void set_terminal(bool terminal) { m_bTerminal = terminal; }
00132 
00137         void set_erase(bool erase) { m_bErase = erase; }
00138 
00142         void set_environment( const char **env );
00143 
00147         inline int fd() const { return m_Fd; }
00148 
00152         inline int pid() const { return m_Pid; }
00153 
00157         void terminate_child();
00158 
00168         static int wait_ms(int fd,int ms);
00169 
00174         static bool check_pid(pid_t pid);
00175 
00182         enum CheckPidStatus { Error=-1, NotExited=-2, Killed=-3, Crashed=-4 } ;
00183 
00191         static int check_pid_exited(pid_t pid);
00192 
00193 };
00194 
00195 EDELIB_NS_END
00196 #endif