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

Function cbm_shell_quote_word

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

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

Calls

no outgoing calls

Tested by

no test coverage detected