fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kdesu / process.h
blobf2b916d24bc277a1b59aa5faf7f922b1f0660de1
1 ///////// XXX migrate it to kprocess /////////////////
3 /* vi: ts=8 sts=4 sw=4
5 * This file is part of the KDE project, module kdesu.
6 * Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
7 *
8 * This is free software; you can use this library under the GNU Library
9 * General Public License, version 2. See the file "COPYING.LIB" for the
10 * exact licensing terms.
13 #ifndef __Process_h_Included__
14 #define __Process_h_Included__
16 #include <sys/types.h>
18 #include <QtCore/QByteRef>
19 #include <QtCore/QString>
20 #include <QtCore/QStringList>
21 #include <QtCore/QList>
23 #include <kdesu/kdesu_export.h>
25 #include <kpty.h>
27 namespace KDESu {
29 /** \class PtyProcess process.h kdesu/process.h
30 * Synchronous communication with tty programs.
32 * PtyProcess provides synchronous communication with tty based programs.
33 * The communications channel used is a pseudo tty (as opposed to a pipe)
34 * This means that programs which require a terminal will work.
37 class KDESU_EXPORT PtyProcess
39 public:
40 PtyProcess();
41 virtual ~PtyProcess();
43 /**
44 * Forks off and execute a command. The command's standard in and output
45 * are connected to the pseudo tty. They are accessible with readLine
46 * and writeLine.
47 * @param command The command to execute.
48 * @param args The arguments to the command.
49 * @return 0 on success, -1 on error. errno might give more information then.
51 int exec(const QByteArray &command, const QList<QByteArray> &args);
53 /**
54 * Reads a line from the program's standard out. Depending on the @em block
55 * parameter, this call blocks until something was read.
56 * Note that in some situations this function will return less than a full
57 * line of output, but never more. Newline characters are stripped.
58 * @param block Block until a full line is read?
59 * @return The output string.
61 QByteArray readLine(bool block=true);
63 /**
64 * Read all available output from the program's standard out.
65 * @param block If no output is in the buffer, should the function block
66 * (else it will return an empty QByteArray)?
67 * @return The output.
69 QByteArray readAll(bool block=true);
71 /**
72 * Writes a line of text to the program's standard in.
73 * @param line The text to write.
74 * @param addNewline Adds a '\n' to the line.
76 void writeLine(const QByteArray &line, bool addNewline=true);
78 /**
79 * Puts back a line of input.
80 * @param line The line to put back.
81 * @param addNewline Adds a '\n' to the line.
83 void unreadLine(const QByteArray &line, bool addNewline=true);
85 /**
86 * Sets the exit string. If a line of program output matches this,
87 * waitForChild() will terminate the program and return.
89 void setExitString(const QByteArray &exit);
91 /**
92 * Waits for the child to exit. See also setExitString.
94 int waitForChild();
96 /**
97 * Waits until the pty has cleared the ECHO flag. This is useful
98 * when programs write a password prompt before they disable ECHO.
99 * Disabling it might flush any input that was written.
101 int WaitSlave();
104 * Enables/disables local echo on the pseudo tty.
106 int enableLocalEcho(bool enable=true);
109 * Enables/disables terminal output. Relevant only to some subclasses.
111 void setTerminal(bool terminal);
114 * Overwrites the password as soon as it is used. Relevant only to
115 * some subclasses.
117 void setErase(bool erase);
120 * Set additinal environment variables.
122 void setEnvironment( const QList<QByteArray> &env );
125 * Returns the filedescriptor of the process.
127 int fd() const;
130 * Returns the pid of the process.
132 int pid() const;
134 public /* static */:
136 ** This is a collection of static functions that can be
137 ** used for process control inside kdesu. I'd suggest
138 ** against using this publicly. There are probably
139 ** nicer Qt based ways to do what you want.
143 ** Wait @p ms miliseconds (ie. 1/10th of a second is 100ms),
144 ** using @p fd as a filedescriptor to wait on. Returns
145 ** select(2)'s result, which is -1 on error, 0 on timeout,
146 ** or positive if there is data on one of the selected fd's.
148 ** @p ms must be in the range 0..999 (i.e. the maximum wait
149 ** duration is 999ms, almost one second).
151 static int waitMS(int fd,int ms);
155 ** Basic check for the existence of @p pid.
156 ** Returns true iff @p pid is an extant process,
157 ** (one you could kill - see man kill(2) for signal 0).
159 static bool checkPid(pid_t pid);
162 /** Error return values for checkPidExited() */
163 enum checkPidStatus { Error=-1, /**< No child */
164 NotExited=-2, /**< Child hasn't exited */
165 Killed=-3 /**< Child terminated by signal */
169 ** Check process exit status for process @p pid.
170 ** If child @p pid has exited, return its exit status,
171 ** (which may be zero).
172 ** On error (no child, no exit), return -1.
173 ** If child @p has not exited, return -2.
175 static int checkPidExited(pid_t pid);
178 protected:
179 QList<QByteArray> environment() const;
181 bool m_bErase, /**< @see setErase() */
182 m_bTerminal; /**< Indicates running in a terminal, causes additional
183 newlines to be printed after output. Set to @c false
184 in constructor. @see setTerminal() */
185 int m_Pid; /**< PID of child process */
186 QByteArray m_Command, /**< Unused */
187 m_Exit; /**< String to scan for in output that indicates
188 child has exited. */
190 private:
191 int init();
192 int setupTTY();
194 protected:
195 /** Standard hack to add virtual methods in a BC way. Unused. */
196 virtual void virtual_hook( int id, void* data );
197 private:
198 class PtyProcessPrivate;
199 PtyProcessPrivate* const d;
204 #endif