00001 /* Part of the culibs project, <http://www.eideticdew.org/culibs/>. 00002 * Copyright (C) 2003--2007 Petter Urkedal <urkedal@nbi.dk> 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #ifndef CUOS_PROCESS_H 00019 #define CUOS_PROCESS_H 00020 00021 #include <stdio.h> 00022 #include <sys/types.h> 00023 #include <cucon/fwd.h> 00024 00025 CU_BEGIN_DECLARATIONS 00026 /*!\defgroup cuos_process cuos/process.h: Running external processes 00027 * @{ \ingroup cuos_mod */ 00028 00029 /*!Fork the current process and call \code execvp(prg, argv)\endcode in the 00030 * subprocess. If \a fd_arr is not NULL, redirect file descriptor \e i 00031 * to \a fd_arr[i] iff \a fd_arr[i] ≠ -1. The given file 00032 * descriptors will be closed by this function. The process id is 00033 * returned. 00034 * 00035 * \arg fd_arr[0] file descriptor for \c stdin in the subprocess, or -1 00036 * \arg fd_arr[1] file descriptor for \c stdout in the subprocess, or -1 00037 * \arg fd_arr[2] file descriptor for \c stderr in the subprocess, or -1 */ 00038 pid_t cuos_start_prog_fd(char const *prg, char const *const *argv, 00039 char *const *envp, int *fd_arr); 00040 00041 /*!Fork the current process and call \code execvp(prg, argv)\endcode in the 00042 * subprocess. For each \a in, \a out, \a err which is non-\c NULL, return 00043 * an open file to the \c stdin, \c stdout, \c stderr of the subprocess. 00044 * Note that \c stdin is opened for writing and the others are opened for 00045 * reading. */ 00046 pid_t cuos_start_prog_io(char const *prg, char const *const *argv, 00047 char *const *envp, 00048 FILE **in, FILE **out, FILE **err); 00049 00050 /*!Shortcut for \ref cuos_start_prog_fd when no redirections are needed. */ 00051 CU_SINLINE pid_t cuos_start_prog(char const *prg, char const *const *argv, 00052 char *const *envp) 00053 { return cuos_start_prog_fd(prg, argv, envp, NULL); } 00054 00055 /*!Wait for the process with process id \a pid as retured by 00056 * \ref cuos_start_prog_fd, \ref cuos_start_prog_io, or 00057 * \ref cuos_start_prog. \a prg should be the same as the first argument 00058 * of the corresponding start-function. */ 00059 int cuos_wait_for_prog(char const *prg, pid_t pid); 00060 00061 /*!Start in a sub-process and wait for the program \a prg with arguments 00062 * \a argv and environment \a envp. See \ref cuos_start_prog for details. */ 00063 int cuos_call_prog(char const *prg, char const *const *argv, 00064 char *const *envp); 00065 00066 /*!Start in a sub-process and wait for the program \a prg with arguments 00067 * \a argv, environment \a envp and redirections \a fd_arr. See 00068 * \ref cuos_start_prog_fd for details. */ 00069 int cuos_call_prog_fd(char const *prg, char const *const *argv, 00070 char *const *envp, int *fd_arr); 00071 00072 /*!Construct a \c NULL terminated argv from a \c cucon_list_t of 00073 * \c cu_str_t. \a argv must have at least 00074 * <tt>\ref cucon_list_count (strlist) + 1</tt> 00075 * elements. */ 00076 void cuos_argv_cct_strlist(char const **argv, cucon_list_t strlist); 00077 00078 /*!Returns 'cstr' with shell special chars (including space) escaped. 00079 * May quote more than strictly needed. */ 00080 cu_str_t cuos_shell_escape_cstr(char const *cstr); 00081 00082 /*!Print out \a argv to \a out. */ 00083 void cuos_argv_fprint(char const **argv, FILE *out); 00084 00085 /*!@} */ 00086 00087 /* Backward compat. */ 00088 #define cuos_argv_cct_strlist cuos_argv_init_strlist 00089 00090 CU_END_DECLARATIONS 00091 00092 #endif