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

Function cbm_shell_quote_word

src/cli/cli.c:4626–4660  ·  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

4624/* Encode one shell word without permitting expansion or command substitution.
4625 * POSIX single-quoted strings represent an apostrophe as: '\'' */
4626static int cbm_shell_quote_word(const char *value, char *out, size_t out_size) {
4627 if (!value || !out || out_size < CLI_PAIR_LEN) {
4628 return CLI_ERR;
4629 }
4630 size_t used = 0;
4631 out[used++] = '\'';
4632 for (const unsigned char *cursor = (const unsigned char *)value; *cursor; cursor++) {
4633 if (*cursor < 0x20 || *cursor == 0x7f) {
4634 out[0] = '\0';
4635 return CLI_ERR;
4636 }
4637 if (*cursor == '\'') {
4638 static const char escaped_quote[] = "'\\''";
4639 if (sizeof(escaped_quote) - CLI_SKIP_ONE > out_size - used - CLI_PAIR_LEN) {
4640 out[0] = '\0';
4641 return CLI_ERR;
4642 }
4643 memcpy(out + used, escaped_quote, sizeof(escaped_quote) - CLI_SKIP_ONE);
4644 used += sizeof(escaped_quote) - CLI_SKIP_ONE;
4645 } else {
4646 if (used >= out_size - CLI_PAIR_LEN) {
4647 out[0] = '\0';
4648 return CLI_ERR;
4649 }
4650 out[used++] = (char)*cursor;
4651 }
4652 }
4653 if (used >= out_size - CLI_SKIP_ONE) {
4654 out[0] = '\0';
4655 return CLI_ERR;
4656 }
4657 out[used++] = '\'';
4658 out[used] = '\0';
4659 return CLI_OK;
4660}
4661
4662/* PowerShell single-quoted words are literal; an apostrophe is represented by
4663 * two apostrophes. This prevents $env expansion and command substitution. */

Calls

no outgoing calls

Tested by

no test coverage detected