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

Function cbm_shell_quote_word

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

4686/* Encode one shell word without permitting expansion or command substitution.
4687 * POSIX single-quoted strings represent an apostrophe as: '\'' */
4688static int cbm_shell_quote_word(const char *value, char *out, size_t out_size) {
4689 if (!value || !out || out_size < CLI_PAIR_LEN) {
4690 return CLI_ERR;
4691 }
4692 size_t used = 0;
4693 out[used++] = '\'';
4694 for (const unsigned char *cursor = (const unsigned char *)value; *cursor; cursor++) {
4695 if (*cursor < 0x20 || *cursor == 0x7f) {
4696 out[0] = '\0';
4697 return CLI_ERR;
4698 }
4699 if (*cursor == '\'') {
4700 static const char escaped_quote[] = "'\\''";
4701 if (sizeof(escaped_quote) - CLI_SKIP_ONE > out_size - used - CLI_PAIR_LEN) {
4702 out[0] = '\0';
4703 return CLI_ERR;
4704 }
4705 memcpy(out + used, escaped_quote, sizeof(escaped_quote) - CLI_SKIP_ONE);
4706 used += sizeof(escaped_quote) - CLI_SKIP_ONE;
4707 } else {
4708 if (used >= out_size - CLI_PAIR_LEN) {
4709 out[0] = '\0';
4710 return CLI_ERR;
4711 }
4712 out[used++] = (char)*cursor;
4713 }
4714 }
4715 if (used >= out_size - CLI_SKIP_ONE) {
4716 out[0] = '\0';
4717 return CLI_ERR;
4718 }
4719 out[used++] = '\'';
4720 out[used] = '\0';
4721 return CLI_OK;
4722}
4723
4724/* PowerShell single-quoted words are literal; an apostrophe is represented by
4725 * two apostrophes. This prevents $env expansion and command substitution. */

Calls

no outgoing calls

Tested by

no test coverage detected