4 * This file is part of the KDE project, module kdesu.
5 * Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
7 * This is free software; you can use this library under the GNU Library
8 * General Public License, version 2. See the file "COPYING.LIB" for the
9 * exact licensing terms.
12 #ifndef __Process_h_Included__
13 #define __Process_h_Included__
15 #include <QtCore/QByteArray>
16 #include <QtCore/QList>
26 typedef QList
<QByteArray
> QCStringList
;
29 * Synchronous communication with tty programs.
31 * PtyProcess provides synchronous communication with tty based programs.
32 * The communications channel used is a pseudo tty (as opposed to a pipe)
33 * This means that programs which require a terminal will work.
40 virtual ~MyPtyProcess();
43 * Fork off and execute a command. The command's standard in and output
44 * are connected to the pseudo tty. They are accessible with @ref #readLine
45 * and @ref #writeLine.
46 * @param command The command to execute.
47 * @param args The arguments to the command.
49 int exec(QByteArray command
, QCStringList args
);
52 * Read a line from the program's standard out. Depending on the @em block
53 * parameter, this call blocks until a single, full line is read.
54 * @param block Block until a full line is read?
55 * @return The output string.
57 QByteArray
readLine(bool block
= true)
59 { return readLineFrom(fd(), m_ptyBuf
, block
); }
61 { return readLineFrom(0, m_ptyBuf
, block
); }
64 QByteArray
readLineFromPty(bool block
= true)
66 { return readLineFrom(fd(), m_ptyBuf
, block
); }
68 { return readLineFrom(0, m_ptyBuf
, block
); }
71 QByteArray
readLineFromStdout(bool block
= true)
72 { return readLineFrom(m_stdinout
, m_stdoutBuf
, block
); }
74 QByteArray
readLineFromStderr(bool block
= true)
75 { return readLineFrom(m_err
, m_stderrBuf
, block
); }
78 * Write a line of text to the program's standard in.
79 * @param line The text to write.
80 * @param addNewline Adds a '\n' to the line.
82 void writeLine(QByteArray line
, bool addNewline
=true);
85 * Put back a line of input.
86 * @param line The line to put back.
87 * @param addNewline Adds a '\n' to the line.
90 void unreadLine(QByteArray line
, bool addNewline
= true)
91 { unreadLineFrom(m_ptyBuf
, line
, addNewline
); }
93 void unreadLineFromPty(QByteArray line
, bool addNewline
= true)
94 { unreadLineFrom(m_ptyBuf
, line
, addNewline
); }
96 void unreadLineFromStderr(QByteArray line
, bool addNewline
= true)
97 { unreadLineFrom(m_stderrBuf
, line
, addNewline
); }
99 void unreadLineFromStdout(QByteArray line
, bool addNewline
= true)
100 { unreadLineFrom(m_stdoutBuf
, line
, addNewline
); }
103 * Set exit string. If a line of program output matches this,
104 * @ref #waitForChild() will terminate the program and return.
106 void setExitString(QByteArray exit
) { m_Exit
= exit
; }
109 * Wait for the child to exit. See also @ref #setExitString.
114 * Wait until the pty has cleared the ECHO flag. This is useful
115 * when programs write a password prompt before they disable ECHO.
116 * Disabling it might flush any input that was written.
120 /** Enables/disables local echo on the pseudo tty. */
121 int enableLocalEcho(bool enable
=true);
123 /** Enable/disable terminal output. Relevant only to some subclasses. */
124 void setTerminal(bool terminal
) { m_bTerminal
= terminal
; }
126 /** Overwritte the password as soon as it is used. Relevant only to
127 * some subclasses. */
128 void setErase(bool erase
) { m_bErase
= erase
; }
130 /** Return the filedescriptor of the process. */
132 int fd() {return m_pPTY
? m_pPTY
->masterFd() : -1;}
134 KProcess
*fd() {return m_pPTY
;}
136 /** Return the pid of the process. */
137 int pid() {return m_Pid
;}
139 int stdioFd() {return m_stdinout
;}
141 int stderrFd() {return m_err
;}
144 bool m_bErase
, m_bTerminal
;
145 int m_Pid
, m_stdinout
, m_err
;
146 QByteArray m_Command
, m_Exit
;
157 QByteArray m_ptyBuf
, m_stderrBuf
, m_stdoutBuf
;
159 QByteArray
readLineFrom(int fd
, QByteArray
& inbuf
, bool block
);
160 void unreadLineFrom(QByteArray inbuf
, QByteArray line
, bool addnl
);
161 class PtyProcessPrivate
;
162 PtyProcessPrivate
*d
;