diff options
Diffstat (limited to 'src/lib/common.c')
-rw-r--r-- | src/lib/common.c | 99 |
1 files changed, 51 insertions, 48 deletions
diff --git a/src/lib/common.c b/src/lib/common.c index 338e9316c..44ff2ab98 100644 --- a/src/lib/common.c +++ b/src/lib/common.c | |||
@@ -404,59 +404,62 @@ char *replace_cntrl_chars(const char *str, char c) { | |||
404 | return rv; | 404 | return rv; |
405 | } | 405 | } |
406 | 406 | ||
407 | // Replaces each control character in str with an escape sequence, such as by | ||
408 | // replacing '\n' (0x0a) with "\\n" (0x5c6e). | ||
407 | char *escape_cntrl_chars(const char *str) { | 409 | char *escape_cntrl_chars(const char *str) { |
408 | if (str) { | 410 | if (str == NULL) |
409 | unsigned int cntrl_chars = 0; | 411 | return NULL; |
410 | const char *c = str; | 412 | |
411 | while (*c) { | 413 | unsigned int cntrl_chars = 0; |
412 | switch (*c++) { | 414 | const char *c = str; |
413 | case '\b': | 415 | while (*c) { |
414 | case '\a': | 416 | switch (*c++) { |
415 | case '\e': | 417 | case '\b': |
416 | case '\f': | 418 | case '\a': |
417 | case '\n': | 419 | case '\e': |
418 | case '\r': | 420 | case '\f': |
419 | case '\t': | 421 | case '\n': |
420 | case '\v': | 422 | case '\r': |
421 | case '\"': | 423 | case '\t': |
422 | case '\'': | 424 | case '\v': |
423 | case '\?': | 425 | case '\"': |
424 | case '\\': | 426 | case '\'': |
425 | ++cntrl_chars; | 427 | case '\?': |
426 | default: break; | 428 | case '\\': |
427 | } | 429 | ++cntrl_chars; |
430 | default: | ||
431 | break; | ||
428 | } | 432 | } |
429 | char *rv = malloc(strlen(str) + cntrl_chars + 1); | 433 | } |
430 | char *ptr = rv; | 434 | char *ptr, *rv = malloc(strlen(str) + cntrl_chars + 1); |
431 | if (!rv) | 435 | if (!rv) |
432 | errExit("malloc"); | 436 | errExit("malloc"); |
433 | c = str; | 437 | ptr = rv; |
434 | while (*c) { | 438 | c = str; |
435 | if (iscntrl(*c)) { | 439 | while (*c) { |
436 | *ptr++ = '\\'; | 440 | if (iscntrl(*c)) { |
437 | switch (*c) { | 441 | *ptr++ = '\\'; |
438 | case '\b': *ptr++ = 'b'; break; | 442 | switch (*c) { |
439 | case '\a': *ptr++ = 'a'; break; | 443 | case '\b': *ptr++ = 'b'; break; |
440 | case '\e': *ptr++ = 'e'; break; | 444 | case '\a': *ptr++ = 'a'; break; |
441 | case '\f': *ptr++ = 'f'; break; | 445 | case '\e': *ptr++ = 'e'; break; |
442 | case '\n': *ptr++ = 'n'; break; | 446 | case '\f': *ptr++ = 'f'; break; |
443 | case '\r': *ptr++ = 'r'; break; | 447 | case '\n': *ptr++ = 'n'; break; |
444 | case '\t': *ptr++ = 't'; break; | 448 | case '\r': *ptr++ = 'r'; break; |
445 | case '\v': *ptr++ = 'v'; break; | 449 | case '\t': *ptr++ = 't'; break; |
446 | case '\"': *ptr++ = '\"'; break; | 450 | case '\v': *ptr++ = 'v'; break; |
447 | case '\'': *ptr++ = '\''; break; | 451 | case '\"': *ptr++ = '\"'; break; |
448 | case '\?': *ptr++ = '?'; break; | 452 | case '\'': *ptr++ = '\''; break; |
449 | case '\\': *ptr++ = '\\'; break; | 453 | case '\?': *ptr++ = '?'; break; |
450 | } | 454 | case '\\': *ptr++ = '\\'; break; |
451 | } else { | ||
452 | *ptr++ = *c; | ||
453 | } | 455 | } |
454 | c++; | 456 | } else { |
457 | *ptr++ = *c; | ||
455 | } | 458 | } |
456 | *ptr = '\0'; | 459 | c++; |
457 | return rv; | ||
458 | } | 460 | } |
459 | return NULL; | 461 | *ptr = '\0'; |
462 | return rv; | ||
460 | } | 463 | } |
461 | 464 | ||
462 | int has_cntrl_chars(const char *str) { | 465 | int has_cntrl_chars(const char *str) { |