add more spacing
[personal-kdebase.git] / runtime / kioslave / sftp / process.h
blob250adad57ec928e796c3816db3c059a4fe304dc1
1 /* vi: ts=8 sts=4 sw=4
4 * This file is part of the KDE project, module kdesu.
5 * Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
6 *
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>
18 #ifndef Q_WS_WIN
19 #include <kpty.h>
20 #else
21 #include <kprocess.h>
22 #endif
24 #define PTYPROC 7120
26 typedef QList<QByteArray> QCStringList;
28 /**
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.
36 class MyPtyProcess
38 public:
39 MyPtyProcess();
40 virtual ~MyPtyProcess();
42 /**
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);
51 /**
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)
58 #ifndef Q_WS_WIN
59 { return readLineFrom(fd(), m_ptyBuf, block); }
60 #else
61 { return readLineFrom(0, m_ptyBuf, block); }
62 #endif
64 QByteArray readLineFromPty(bool block = true)
65 #ifndef Q_WS_WIN
66 { return readLineFrom(fd(), m_ptyBuf, block); }
67 #else
68 { return readLineFrom(0, m_ptyBuf, block); }
69 #endif
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); }
77 /**
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);
84 /**
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.
111 int waitForChild();
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.
118 int WaitSlave();
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. */
131 #ifndef Q_WS_WIN
132 int fd() {return m_pPTY ? m_pPTY->masterFd() : -1;}
133 #else
134 KProcess *fd() {return m_pPTY;}
135 #endif
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;}
143 protected:
144 bool m_bErase, m_bTerminal;
145 int m_Pid, m_stdinout, m_err;
146 QByteArray m_Command, m_Exit;
148 private:
149 int init();
150 int setupTTY();
152 #ifndef Q_WS_WIN
153 KPty *m_pPTY;
154 #else
155 KProcess *m_pPTY;
156 #endif
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;
165 #endif