# HG changeset patch # User Matti Hamalainen # Date 1530900575 -10800 # Node ID 2e890e3d568495e8292fa55244b59325500fb5ee # Parent 3d578c0043ae3bb07addae445ede3fdf948f5a27 Add functionality for escaping the separator characters (-l option) in printed out string data. diff -r 3d578c0043ae -r 2e890e3d5684 sidinfo.c --- a/sidinfo.c Mon Jul 02 00:03:45 2018 +0300 +++ b/sidinfo.c Fri Jul 06 21:09:35 2018 +0300 @@ -6,6 +6,7 @@ #include "th_args.h" #include "th_string.h" #include "th_file.h" +#include "th_datastruct.h" #include "sidlib.h" #ifdef HAVE_ICONV # include @@ -725,6 +726,43 @@ } +static char * siEscapeString(const char *str, const char *esc) +{ + if (str == NULL) + return NULL; + + if (esc == NULL) + return th_strdup(str); + + size_t len = 0, size = strlen(str) + 1; + char *buf = th_malloc(size); + if (buf == NULL) + return NULL; + + while (*str) + { + if (strchr(esc, *str) != NULL || *str == '\\') + { + if (!th_strbuf_putch(&buf, &size, &len, '\\')) + goto err; + } + if (!th_strbuf_putch(&buf, &size, &len, *str)) + goto err; + + str++; + } + + if (!th_strbuf_putch(&buf, &size, &len, 0)) + goto err; + + return buf; + +err: + th_free(buf); + return NULL; +} + + static void siPrintStrEscapes(FILE *outFile, const char *str) { while (*str) @@ -764,7 +802,7 @@ static void siPrintPSIDInfoLine(FILE *outFile, BOOL *shown, const PSFStackItem *item, const char *d_str, const int d_int, const BOOL useConv) { const PSFOption *opt = &optPSOptions[item->cmd]; - char *fmt, *str; + char *fmt, *str, *tmp; switch (opt->type) { @@ -789,28 +827,25 @@ siPrintFieldPrefix(outFile, opt); #ifdef HAVE_ICONV - char *tmp; - if (setUseChConv && d_str != NULL && useConv) - tmp = siConvertCharset(setChConv, d_str); + { + char *tmp2 = siConvertCharset(setChConv, d_str); + tmp = siEscapeString(tmp2, optFieldSep); + th_free(tmp2); + } else - tmp = th_strdup(d_str); - - str = siItemFormatStrPrint(fmt, opt, tmp, d_int); + tmp = siEscapeString(d_str, optFieldSep); #else (void) useConv; - str = siItemFormatStrPrint(fmt, opt, d_str, d_int); + tmp = siEscapeString(d_str, optFieldSep); #endif - if (str != NULL) + if ((str = siItemFormatStrPrint(fmt, opt, tmp, d_int)) != NULL) fputs(str, outFile); siPrintFieldSeparator(outFile); th_free(str); - -#ifdef HAVE_ICONV th_free(tmp); -#endif *shown = TRUE; }