1 /* buildcmd.[ch] -- build command lines from a stream of arguments 2 Copyright (C) 2005-2021 Free Software Foundation, Inc. 3 4 This program is free software: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation, either version 3 of the License, or 7 (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 /* 18 * Written by James Youngman. 19 */ 20 #ifndef INC_BUILDCMD_H 21 # define INC_BUILDCMD_H 1 22 23 # include <stdbool.h> 24 25 struct buildcmd_state 26 { 27 /* Number of valid elements in `cmd_argv', including terminating NULL. */ 28 size_t cmd_argc; /* 0 */ 29 30 /* The list of args being built. */ 31 char **cmd_argv; /* NULL */ 32 33 /* Number of elements allocated for `cmd_argv'. */ 34 size_t cmd_argv_alloc; 35 36 /* Storage for elements of `cmd_argv'. */ 37 char *argbuf; 38 39 /* Number of chars being used in `cmd_argv'. */ 40 size_t cmd_argv_chars; 41 42 /* Number of chars being used in `cmd_argv' for the initial args.. */ 43 size_t cmd_initial_argv_chars; 44 45 /* User context information. */ 46 void *usercontext; 47 48 /* to-do flag. */ 49 int todo; 50 51 /* Directory in which to perform the exec. */ 52 int dir_fd; 53 54 /* Summary of what we think the argv limits are. */ 55 size_t largest_successful_arg_count; 56 size_t smallest_failed_arg_count; 57 }; 58 59 struct buildcmd_control 60 { 61 /* If true, exit if lines_per_exec or args_per_exec is exceeded. */ 62 int exit_if_size_exceeded; /* false */ 63 64 /* POSIX limits on the argument length. */ 65 size_t posix_arg_size_max; 66 size_t posix_arg_size_min; 67 68 /* The maximum number of characters that can be used per command line. */ 69 size_t arg_max; 70 71 /* max_arg_count: the maximum number of arguments that can be used. 72 * 73 * Many systems include the size of the pointers in ARG_MAX. 74 * Linux on PPC fails if we just subtract 1 here. 75 * 76 * However, not all systems define ARG_MAX. Our bc_get_arg_max() 77 * function returns a useful value even if ARG_MAX is not defined. 78 * However, sometimes, max_arg_count is LONG_MAX! 79 */ 80 size_t max_arg_count; 81 82 83 /* The length of `replace_pat'. */ 84 size_t rplen; 85 86 /* If nonzero, then instead of putting the args from stdin at 87 the end of the command argument list, they are each stuck into the 88 initial args, replacing each occurrence of the `replace_pat' in the 89 initial args. */ 90 const char *replace_pat; 91 92 /* Number of initial arguments given on the command line. */ 93 size_t initial_argc; /* 0 */ 94 95 /* exec callback. */ 96 int (*exec_callback)(struct buildcmd_control *, void *usercontext, int argc, char **argv); 97 98 /* If nonzero, the maximum number of nonblank lines from stdin to use 99 per command line. */ 100 unsigned long lines_per_exec; /* 0 */ 101 102 /* The maximum number of arguments to use per command line. */ 103 size_t args_per_exec; 104 }; 105 106 enum BC_INIT_STATUS 107 { 108 BC_INIT_OK = 0, 109 BC_INIT_ENV_TOO_BIG, 110 BC_INIT_CANNOT_ACCOMODATE_HEADROOM 111 }; 112 113 extern size_t bc_size_of_environment (void); 114 115 116 extern void bc_do_insert (struct buildcmd_control *ctl, 117 struct buildcmd_state *state, 118 char *arg, size_t arglen, 119 const char *prefix, size_t pfxlen, 120 const char *linebuf, size_t lblen, 121 int initial_args); 122 123 extern void bc_do_exec (struct buildcmd_control *ctl, 124 struct buildcmd_state *state); 125 126 extern void bc_push_arg (struct buildcmd_control *ctl, 127 struct buildcmd_state *state, 128 const char *arg, size_t len, 129 const char *prefix, size_t pfxlen, 130 int initial_args); 131 132 extern void bc_init_state(const struct buildcmd_control *ctl, 133 struct buildcmd_state *state, 134 void *usercontext); 135 extern enum BC_INIT_STATUS bc_init_controlinfo(struct buildcmd_control *ctl, 136 size_t arglen_headroom); 137 extern size_t bc_get_arg_max(void); 138 extern void bc_use_sensible_arg_max(struct buildcmd_control *ctl); 139 extern void bc_clear_args(const struct buildcmd_control *ctl, 140 struct buildcmd_state *state); 141 bool bc_args_exceed_testing_limit(char **argv); 142 143 144 #endif 145