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

Function cbm_cmdline_append_arg

src/foundation/subprocess.c:236–273  ·  view source on GitHub ↗

Append one argv element to the command line using the Microsoft C runtime * quoting rules (see MS "Parsing C Command-Line Arguments"). CreateProcess takes * a SINGLE string that the child re-parses back into argv, so any element with a * space, tab or double-quote must be wrapped in quotes and its embedded quotes / * preceding backslashes escaped. Without this a JSON argument like * {"repo_pa

Source from the content-addressed store, hash-verified

234 * non-zero at JSON-arg parse, misattributed to the last-marked file). POSIX is
235 * unaffected: the POSIX spawn path passes the argv array straight to execv. */
236static size_t cbm_cmdline_append_arg(char *buf, size_t cap, size_t pos, const char *arg, bool first,
237 bool *ovf) {
238 if (!first) {
239 pos = cbm_cmdline_put(buf, cap, pos, ' ', ovf);
240 }
241 pos = cbm_cmdline_put(buf, cap, pos, '"', ovf);
242 for (const char *p = arg; *p;) {
243 size_t nbs = 0;
244 while (*p == '\\') {
245 nbs++;
246 p++;
247 }
248 if (*p == '\0') {
249 /* Trailing backslashes precede the closing quote: double them so the
250 * quote stays a delimiter, not an escaped literal. */
251 for (size_t k = 0; k < nbs * 2; k++) {
252 pos = cbm_cmdline_put(buf, cap, pos, '\\', ovf);
253 }
254 break;
255 }
256 if (*p == '"') {
257 /* N backslashes then a quote -> 2N+1 backslashes then an escaped quote. */
258 for (size_t k = 0; k < nbs * 2 + 1; k++) {
259 pos = cbm_cmdline_put(buf, cap, pos, '\\', ovf);
260 }
261 pos = cbm_cmdline_put(buf, cap, pos, '"', ovf);
262 p++;
263 } else {
264 for (size_t k = 0; k < nbs; k++) {
265 pos = cbm_cmdline_put(buf, cap, pos, '\\', ovf);
266 }
267 pos = cbm_cmdline_put(buf, cap, pos, *p, ovf);
268 p++;
269 }
270 }
271 pos = cbm_cmdline_put(buf, cap, pos, '"', ovf);
272 return pos;
273}
274
275/* Build a full Windows CreateProcess command line from a NULL-terminated argv,
276 * applying the MS C runtime quoting rules so the child re-parses byte-identical

Callers 1

cbm_build_win_cmdlineFunction · 0.85

Calls 1

cbm_cmdline_putFunction · 0.85

Tested by

no test coverage detected