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

Function cbm_cmdline_append_arg

src/foundation/subprocess.c:244–281  ·  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

242 * non-zero at JSON-arg parse, misattributed to the last-marked file). POSIX is
243 * unaffected: the POSIX spawn path passes the argv array straight to execv. */
244static size_t cbm_cmdline_append_arg(char *buf, size_t cap, size_t pos, const char *arg, bool first,
245 bool *ovf) {
246 if (!first) {
247 pos = cbm_cmdline_put(buf, cap, pos, ' ', ovf);
248 }
249 pos = cbm_cmdline_put(buf, cap, pos, '"', ovf);
250 for (const char *p = arg; *p;) {
251 size_t nbs = 0;
252 while (*p == '\\') {
253 nbs++;
254 p++;
255 }
256 if (*p == '\0') {
257 /* Trailing backslashes precede the closing quote: double them so the
258 * quote stays a delimiter, not an escaped literal. */
259 for (size_t k = 0; k < nbs * 2; k++) {
260 pos = cbm_cmdline_put(buf, cap, pos, '\\', ovf);
261 }
262 break;
263 }
264 if (*p == '"') {
265 /* N backslashes then a quote -> 2N+1 backslashes then an escaped quote. */
266 for (size_t k = 0; k < nbs * 2 + 1; k++) {
267 pos = cbm_cmdline_put(buf, cap, pos, '\\', ovf);
268 }
269 pos = cbm_cmdline_put(buf, cap, pos, '"', ovf);
270 p++;
271 } else {
272 for (size_t k = 0; k < nbs; k++) {
273 pos = cbm_cmdline_put(buf, cap, pos, '\\', ovf);
274 }
275 pos = cbm_cmdline_put(buf, cap, pos, *p, ovf);
276 p++;
277 }
278 }
279 pos = cbm_cmdline_put(buf, cap, pos, '"', ovf);
280 return pos;
281}
282
283/* Build a full Windows CreateProcess command line from a NULL-terminated argv,
284 * 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