File indexing completed on 2024-04-21 03:56:41
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2007 Oswald Buddenhagen <ossi@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef KPTYPROCESS_H 0009 #define KPTYPROCESS_H 0010 0011 #include <KProcess> 0012 0013 #include "kpty_export.h" 0014 0015 #include <memory> 0016 0017 class KPtyDevice; 0018 0019 class KPtyProcessPrivate; 0020 0021 /** 0022 * This class extends KProcess by support for PTYs (pseudo TTYs). 0023 * 0024 * The PTY is opened as soon as the class is instantiated. Verify that 0025 * it was opened successfully by checking that pty()->masterFd() is not -1. 0026 * 0027 * The PTY is always made the process' controlling TTY. 0028 * Utmp registration and connecting the stdio handles to the PTY are optional. 0029 * 0030 * No attempt to integrate with QProcess' waitFor*() functions was made, 0031 * for it is impossible. Note that execute() does not work with the PTY, too. 0032 * Use the PTY device's waitFor*() functions or use it asynchronously. 0033 * 0034 * @note If you inherit from this class and use setChildProcessModifier() in 0035 * the derived class, you must call the childProcessModifier() of KPtyProcess 0036 * first (using setChildProcessModifier() in the derived class will "overwrite" 0037 * the childProcessModifier() std::function that was previously set by KPtyProcess). 0038 * For example: 0039 * @code 0040 * class MyProcess : public KPtyProcess 0041 * { 0042 * MyProcess() 0043 * { 0044 * auto parentChildProcModifier = KPtyProcess::childProcessModifier(); 0045 * setChildProcessModifier([parentChildProcModifier]() { 0046 * // First call the parent class modifier function 0047 * if (parentChildProcModifier) { 0048 * parentChildProcModifier(); 0049 * } 0050 * // Then whatever extra code you need to run 0051 * .... 0052 * .... 0053 * }); 0054 * } 0055 * @endcode 0056 * 0057 * @author Oswald Buddenhagen <ossi@kde.org> 0058 */ 0059 class KPTY_EXPORT KPtyProcess : public KProcess 0060 { 0061 Q_OBJECT 0062 Q_DECLARE_PRIVATE(KPtyProcess) 0063 0064 public: 0065 /** 0066 * @see PtyChannels 0067 */ 0068 enum PtyChannelFlag { 0069 NoChannels = 0, /**< The PTY is not connected to any channel. */ 0070 StdinChannel = 1, /**< Connect PTY to stdin. */ 0071 StdoutChannel = 2, /**< Connect PTY to stdout. */ 0072 StderrChannel = 4, /**< Connect PTY to stderr. */ 0073 AllOutputChannels = 6, /**< Connect PTY to all output channels. */ 0074 AllChannels = 7, /**< Connect PTY to all channels. */ 0075 }; 0076 0077 /** 0078 * Stores a combination of #PtyChannelFlag values. 0079 */ 0080 Q_DECLARE_FLAGS(PtyChannels, PtyChannelFlag) 0081 0082 /** 0083 * Constructor 0084 */ 0085 explicit KPtyProcess(QObject *parent = nullptr); 0086 0087 /** 0088 * Construct a process using an open pty master. 0089 * 0090 * @param ptyMasterFd an open pty master file descriptor. 0091 * The process does not take ownership of the descriptor; 0092 * it will not be automatically closed at any point. 0093 */ 0094 KPtyProcess(int ptyMasterFd, QObject *parent = nullptr); 0095 0096 /** 0097 * Destructor 0098 */ 0099 ~KPtyProcess() override; 0100 0101 /** 0102 * Set to which channels the PTY should be assigned. 0103 * 0104 * This function must be called before starting the process. 0105 * 0106 * @param channels the output channel handling mode 0107 */ 0108 void setPtyChannels(PtyChannels channels); 0109 0110 /** 0111 * Query to which channels the PTY is assigned. 0112 * 0113 * @return the output channel handling mode 0114 */ 0115 PtyChannels ptyChannels() const; 0116 0117 /** 0118 * Set whether to register the process as a TTY login in utmp. 0119 * 0120 * Utmp is disabled by default. 0121 * It should enabled for interactively fed processes, like terminal 0122 * emulations. 0123 * 0124 * This function must be called before starting the process. 0125 * 0126 * @param value whether to register in utmp. 0127 */ 0128 void setUseUtmp(bool value); 0129 0130 /** 0131 * Get whether to register the process as a TTY login in utmp. 0132 * 0133 * @return whether to register in utmp 0134 */ 0135 bool isUseUtmp() const; 0136 0137 /** 0138 * Get the PTY device of this process. 0139 * 0140 * @return the PTY device 0141 */ 0142 KPtyDevice *pty() const; 0143 0144 private: 0145 std::unique_ptr<KPtyProcessPrivate> const d_ptr; 0146 }; 0147 0148 Q_DECLARE_OPERATORS_FOR_FLAGS(KPtyProcess::PtyChannels) 0149 0150 #endif