File indexing completed on 2024-04-28 05:46:50

0001 /*****************************************************************************
0002  *   Copyright 2014 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
0003  *                                                                           *
0004  *   This program is free software; you can redistribute it and/or modify    *
0005  *   it under the terms of the GNU Lesser General Public License as          *
0006  *   published by the Free Software Foundation; either version 2.1 of the    *
0007  *   License, or (at your option) version 3, or any later version accepted   *
0008  *   by the membership of KDE e.V. (or its successor approved by the         *
0009  *   membership of KDE e.V.), which shall act as a proxy defined in          *
0010  *   Section 6 of version 3 of the license.                                  *
0011  *                                                                           *
0012  *   This program is distributed in the hope that it will be useful,         *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
0015  *   Lesser General Public License for more details.                         *
0016  *                                                                           *
0017  *   You should have received a copy of the GNU Lesser General Public        *
0018  *   License along with this library. If not,                                *
0019  *   see <http://www.gnu.org/licenses/>.                                     *
0020  *****************************************************************************/
0021 
0022 #ifndef _QTC_UTILS_PROCESS_H_
0023 #define _QTC_UTILS_PROCESS_H_
0024 
0025 #include "utils.h"
0026 
0027 bool qtcForkBackground(QtcCallback cb, void *data, QtcCallback fail_cb=nullptr);
0028 bool qtcSpawn(const char *file, const char *const *argv,
0029               QtcCallback cb, void *cb_data, QtcCallback fail_cb=nullptr);
0030 typedef enum {
0031     QTC_POPEN_NONE = 0,
0032     QTC_POPEN_READ = 1 << 0,
0033     QTC_POPEN_WRITE = 1 << 1,
0034     QTC_POPEN_RDWR = QTC_POPEN_READ | QTC_POPEN_WRITE
0035 } QtcPopenFDMode;
0036 typedef struct {
0037     int orig;
0038     int replace;
0039     QtcPopenFDMode mode;
0040 } QtcPopenFD;
0041 bool qtcPopen(const char *file, const char *const *argv,
0042               unsigned fd_num, QtcPopenFD *fds);
0043 typedef struct {
0044     int orig;
0045     QtcPopenFDMode mode;
0046     char *buff;
0047     size_t len;
0048 } QtcPopenBuff;
0049 bool qtcPopenBuff(const char *file, const char *const *argv, unsigned buff_num,
0050                   QtcPopenBuff *buffs, int timeout);
0051 QTC_ALWAYS_INLINE static inline char*
0052 qtcPopenStdout(const char *file, const char *const *argv,
0053                int timeout, size_t *len)
0054 {
0055     QtcPopenBuff popen_buff = {1, QTC_POPEN_READ, nullptr, 0};
0056     bool res = qtcPopenBuff(file, argv, 1, &popen_buff, timeout);
0057     qtcAssign(len, popen_buff.len);
0058     QTC_RET_IF_FAIL(res, nullptr);
0059     if (!popen_buff.len) {
0060         free(popen_buff.buff);
0061         return nullptr;
0062     }
0063     popen_buff.buff[popen_buff.len] = '\0';
0064     return popen_buff.buff;
0065 }
0066 
0067 #endif