MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_shell_quote_word

Function cbm_shell_quote_word

src/cli/cli.c:5316–5350  ·  view source on GitHub ↗

Encode one shell word without permitting expansion or command substitution. * POSIX single-quoted strings represent an apostrophe as: '\'' */

Source from the content-addressed store, hash-verified

5314/* Encode one shell word without permitting expansion or command substitution.
5315 * POSIX single-quoted strings represent an apostrophe as: '\'' */
5316static int cbm_shell_quote_word(const char *value, char *out, size_t out_size) {
5317 if (!value || !out || out_size < CLI_PAIR_LEN) {
5318 return CLI_ERR;
5319 }
5320 size_t used = 0;
5321 out[used++] = '\'';
5322 for (const unsigned char *cursor = (const unsigned char *)value; *cursor; cursor++) {
5323 if (*cursor < 0x20 || *cursor == 0x7f) {
5324 out[0] = '\0';
5325 return CLI_ERR;
5326 }
5327 if (*cursor == '\'') {
5328 static const char escaped_quote[] = "'\\''";
5329 if (sizeof(escaped_quote) - CLI_SKIP_ONE > out_size - used - CLI_PAIR_LEN) {
5330 out[0] = '\0';
5331 return CLI_ERR;
5332 }
5333 memcpy(out + used, escaped_quote, sizeof(escaped_quote) - CLI_SKIP_ONE);
5334 used += sizeof(escaped_quote) - CLI_SKIP_ONE;
5335 } else {
5336 if (used >= out_size - CLI_PAIR_LEN) {
5337 out[0] = '\0';
5338 return CLI_ERR;
5339 }
5340 out[used++] = (char)*cursor;
5341 }
5342 }
5343 if (used >= out_size - CLI_SKIP_ONE) {
5344 out[0] = '\0';
5345 return CLI_ERR;
5346 }
5347 out[used++] = '\'';
5348 out[used] = '\0';
5349 return CLI_OK;
5350}
5351
5352/* PowerShell single-quoted words are literal; an apostrophe is represented by
5353 * two apostrophes. This prevents $env expansion and command substitution. */

Calls

no outgoing calls

Tested by

no test coverage detected