| 1 |
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 |
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */ |
| 3 |
#include "kwsysPrivate.h" |
| 4 |
#include KWSYS_HEADER(System.h) |
| 5 |
|
| 6 |
/* Work-around CMake dependency scanning limitation. This must |
| 7 |
duplicate the above list of headers. */ |
| 8 |
#if 0 |
| 9 |
# include "System.h.in" |
| 10 |
#endif |
| 11 |
|
| 12 |
#include <ctype.h> /* isspace */ |
| 13 |
#include <stddef.h> /* ptrdiff_t */ |
| 14 |
#include <stdlib.h> /* malloc, free */ |
| 15 |
#include <string.h> /* memcpy */ |
| 16 |
|
| 17 |
#include <stdio.h> |
| 18 |
|
| 19 |
#if defined(KWSYS_C_HAS_PTRDIFF_T) && KWSYS_C_HAS_PTRDIFF_T |
| 20 |
typedef ptrdiff_t kwsysSystem_ptrdiff_t; |
| 21 |
#else |
| 22 |
typedef int kwsysSystem_ptrdiff_t; |
| 23 |
#endif |
| 24 |
|
| 25 |
static int kwsysSystem__AppendByte(const char* local, char** begin, char** end, |
| 26 |
int* size, char c) |
| 27 |
{ |
| 28 |
/* Allocate space for the character. */ |
| 29 |
if ((*end - *begin) >= *size) { |
| 30 |
kwsysSystem_ptrdiff_t length = *end - *begin; |
| 31 |
char* newBuffer = (char*)malloc((size_t)(*size * 2)); |
| 32 |
if (!newBuffer) { |
| 33 |
return 0; |
| 34 |
} |
| 35 |
memcpy(newBuffer, *begin, (size_t)(length) * sizeof(char)); |
| 36 |
if (*begin != local) { |
| 37 |
free(*begin); |
| 38 |
} |
| 39 |
*begin = newBuffer; |
| 40 |
*end = *begin + length; |
| 41 |
*size *= 2; |
| 42 |
} |
| 43 |
|
| 44 |
/* Store the character. */ |
| 45 |
*(*end)++ = c; |
| 46 |
return 1; |
| 47 |
} |
| 48 |
|
| 49 |
static int kwsysSystem__AppendArgument(char** local, char*** begin, |
| 50 |
char*** end, int* size, char* arg_local, |
| 51 |
char** arg_begin, char** arg_end, |
| 52 |
int* arg_size) |
| 53 |
{ |
| 54 |
/* Append a null-terminator to the argument string. */ |
| 55 |
if (!kwsysSystem__AppendByte(arg_local, arg_begin, arg_end, arg_size, |
| 56 |
'\0')) { |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
/* Allocate space for the argument pointer. */ |
| 61 |
if ((*end - *begin) >= *size) { |
| 62 |
kwsysSystem_ptrdiff_t length = *end - *begin; |
| 63 |
char** newPointers = (char**)malloc((size_t)(*size) * 2 * sizeof(char*)); |
| 64 |
if (!newPointers) { |
| 65 |
return 0; |
| 66 |
} |
| 67 |
memcpy(newPointers, *begin, (size_t)(length) * sizeof(char*)); |
| 68 |
if (*begin != local) { |
| 69 |
free(*begin); |
| 70 |
} |
| 71 |
*begin = newPointers; |
| 72 |
*end = *begin + length; |
| 73 |
*size *= 2; |
| 74 |
} |
| 75 |
|
| 76 |
/* Allocate space for the argument string. */ |
| 77 |
**end = (char*)malloc((size_t)(*arg_end - *arg_begin)); |
| 78 |
if (!**end) { |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
/* Store the argument in the command array. */ |
| 83 |
memcpy(**end, *arg_begin, (size_t)(*arg_end - *arg_begin)); |
| 84 |
++(*end); |
| 85 |
|
| 86 |
/* Reset the argument to be empty. */ |
| 87 |
*arg_end = *arg_begin; |
| 88 |
|
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
#define KWSYSPE_LOCAL_BYTE_COUNT 1024 |
| 93 |
#define KWSYSPE_LOCAL_ARGS_COUNT 32 |
| 94 |
static char** kwsysSystem__ParseUnixCommand(const char* command, int flags) |
| 95 |
{ |
| 96 |
/* Create a buffer for argument pointers during parsing. */ |
| 97 |
char* local_pointers[KWSYSPE_LOCAL_ARGS_COUNT]; |
| 98 |
int pointers_size = KWSYSPE_LOCAL_ARGS_COUNT; |
| 99 |
char** pointer_begin = local_pointers; |
| 100 |
char** pointer_end = pointer_begin; |
| 101 |
|
| 102 |
/* Create a buffer for argument strings during parsing. */ |
| 103 |
char local_buffer[KWSYSPE_LOCAL_BYTE_COUNT]; |
| 104 |
int buffer_size = KWSYSPE_LOCAL_BYTE_COUNT; |
| 105 |
char* buffer_begin = local_buffer; |
| 106 |
char* buffer_end = buffer_begin; |
| 107 |
|
| 108 |
/* Parse the command string. Try to behave like a UNIX shell. */ |
| 109 |
char** newCommand = 0; |
| 110 |
const char* c = command; |
| 111 |
int in_argument = 0; |
| 112 |
int in_escape = 0; |
| 113 |
int in_single = 0; |
| 114 |
int in_double = 0; |
| 115 |
int failed = 0; |
| 116 |
for (; *c; ++c) { |
| 117 |
if (in_escape) { |
| 118 |
/* This character is escaped so do no special handling. */ |
| 119 |
if (!in_argument) { |
| 120 |
in_argument = 1; |
| 121 |
} |
| 122 |
if (!kwsysSystem__AppendByte(local_buffer, &buffer_begin, &buffer_end, |
| 123 |
&buffer_size, *c)) { |
| 124 |
failed = 1; |
| 125 |
break; |
| 126 |
} |
| 127 |
in_escape = 0; |
| 128 |
} else if (*c == '\\') { |
| 129 |
/* The next character should be escaped. */ |
| 130 |
in_escape = 1; |
| 131 |
} else if (*c == '\'' && !in_double) { |
| 132 |
/* Enter or exit single-quote state. */ |
| 133 |
if (in_single) { |
| 134 |
in_single = 0; |
| 135 |
} else { |
| 136 |
in_single = 1; |
| 137 |
if (!in_argument) { |
| 138 |
in_argument = 1; |
| 139 |
} |
| 140 |
} |
| 141 |
} else if (*c == '"' && !in_single) { |
| 142 |
/* Enter or exit double-quote state. */ |
| 143 |
if (in_double) { |
| 144 |
in_double = 0; |
| 145 |
} else { |
| 146 |
in_double = 1; |
| 147 |
if (!in_argument) { |
| 148 |
in_argument = 1; |
| 149 |
} |
| 150 |
} |
| 151 |
} else if (isspace((unsigned char)*c)) { |
| 152 |
if (in_argument) { |
| 153 |
if (in_single || in_double) { |
| 154 |
/* This space belongs to a quoted argument. */ |
| 155 |
if (!kwsysSystem__AppendByte(local_buffer, &buffer_begin, |
| 156 |
&buffer_end, &buffer_size, *c)) { |
| 157 |
failed = 1; |
| 158 |
break; |
| 159 |
} |
| 160 |
} else { |
| 161 |
/* This argument has been terminated by whitespace. */ |
| 162 |
if (!kwsysSystem__AppendArgument( |
| 163 |
local_pointers, &pointer_begin, &pointer_end, &pointers_size, |
| 164 |
local_buffer, &buffer_begin, &buffer_end, &buffer_size)) { |
| 165 |
failed = 1; |
| 166 |
break; |
| 167 |
} |
| 168 |
in_argument = 0; |
| 169 |
} |
| 170 |
} |
| 171 |
} else { |
| 172 |
/* This character belong to an argument. */ |
| 173 |
if (!in_argument) { |
| 174 |
in_argument = 1; |
| 175 |
} |
| 176 |
if (!kwsysSystem__AppendByte(local_buffer, &buffer_begin, &buffer_end, |
| 177 |
&buffer_size, *c)) { |
| 178 |
failed = 1; |
| 179 |
break; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/* Finish the last argument. */ |
| 185 |
if (in_argument) { |
| 186 |
if (!kwsysSystem__AppendArgument( |
| 187 |
local_pointers, &pointer_begin, &pointer_end, &pointers_size, |
| 188 |
local_buffer, &buffer_begin, &buffer_end, &buffer_size)) { |
| 189 |
failed = 1; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/* If we still have memory allocate space for the new command |
| 194 |
buffer. */ |
| 195 |
if (!failed) { |
| 196 |
kwsysSystem_ptrdiff_t n = pointer_end - pointer_begin; |
| 197 |
newCommand = (char**)malloc((size_t)(n + 1) * sizeof(char*)); |
| 198 |
} |
| 199 |
|
| 200 |
if (newCommand) { |
| 201 |
/* Copy the arguments into the new command buffer. */ |
| 202 |
kwsysSystem_ptrdiff_t n = pointer_end - pointer_begin; |
| 203 |
memcpy(newCommand, pointer_begin, sizeof(char*) * (size_t)(n)); |
| 204 |
newCommand[n] = 0; |
| 205 |
} else { |
| 206 |
/* Free arguments already allocated. */ |
| 207 |
while (pointer_end != pointer_begin) { |
| 208 |
free(*(--pointer_end)); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/* Free temporary buffers. */ |
| 213 |
if (pointer_begin != local_pointers) { |
| 214 |
free(pointer_begin); |
| 215 |
} |
| 216 |
if (buffer_begin != local_buffer) { |
| 217 |
free(buffer_begin); |
| 218 |
} |
| 219 |
|
| 220 |
/* The flags argument is currently unused. */ |
| 221 |
(void)flags; |
| 222 |
|
| 223 |
/* Return the final command buffer. */ |
| 224 |
return newCommand; |
| 225 |
} |
| 226 |
|
| 227 |
char** kwsysSystem_Parse_CommandForUnix(const char* command, int flags) |
| 228 |
{ |
| 229 |
/* Validate the flags. */ |
| 230 |
if (flags != 0) { |
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
/* Forward to our internal implementation. */ |
| 235 |
return kwsysSystem__ParseUnixCommand(command, flags); |
| 236 |
} |