tControl and quote characters in string configuration variables are now properly escaped to avoid writing out broken config files. - vaccinewars - be a doctor and try to vaccinate the world
 (HTM) git clone git://src.adamsgaard.dk/vaccinewars
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 5cd6140008fa710b14d0c86e3a5553df065cacdb
 (DIR) parent bf1956fb9811da51db35f13b89f643347538b9c8
 (HTM) Author: Ben Webb <ben@salilab.org>
       Date:   Sat,  2 Mar 2002 19:36:18 +0000
       
       Control and quote characters in string configuration variables are now
       properly escaped to avoid writing out broken config files.
       
       
       Diffstat:
         M src/dopewars.c                      |      50 +++++++++++++++++++++++++++++--
       
       1 file changed, 47 insertions(+), 3 deletions(-)
       ---
 (DIR) diff --git a/src/dopewars.c b/src/dopewars.c
       t@@ -1967,6 +1967,47 @@ gboolean CheckMaxIndex(GScanner *scanner, int GlobalIndex, int StructIndex,
        }
        
        /*
       + * Prints the given string to a file, converting control characters
       + * and escaping other special characters.
       + */
       +static void PrintEscaped(FILE *fp, gchar *str)
       +{
       +  int i;
       +
       +  for (i = 0; i < strlen(str); i++) {
       +    switch(str[i]) {
       +    case '"':
       +    case '\'':
       +    case '\\':
       +      fputc('\\', fp);
       +      fputc(str[i], fp);
       +      break;
       +    case '\n':
       +      fputs("\\n", fp);
       +      break;
       +    case '\t':
       +      fputs("\\t", fp);
       +      break;
       +    case '\r':
       +      fputs("\\r", fp);
       +      break;
       +    case '\b':
       +      fputs("\\b", fp);
       +      break;
       +    case '\f':
       +      fputs("\\f", fp);
       +      break;
       +    default:
       +      if (isprint(str[i])) {
       +        fputc(str[i], fp);
       +      } else {
       +        fprintf(fp, "\\%o", str[i]);
       +      }
       +    }
       +  }
       +}
       +
       +/*
         * Writes a single configuration file variable (identified by GlobalIndex
         * and StructIndex) to the specified file, in a format suitable for reading
         * back in (via. ParseNextConfig and friends).
       t@@ -1996,8 +2037,9 @@ static void WriteConfigValue(FILE *fp, int GlobalIndex, int StructIndex)
            fprintf(fp, "%s = %s\n", GlobalName, prstr);
            g_free(prstr);
          } else if (Globals[GlobalIndex].StringVal) {
       -    fprintf(fp, "%s = \"%s\"\n", GlobalName,
       -            *GetGlobalString(GlobalIndex, StructIndex));
       +    fprintf(fp, "%s = \"", GlobalName);
       +    PrintEscaped(fp, *GetGlobalString(GlobalIndex, StructIndex));
       +    fprintf(fp, "\"\n");
          } else if (Globals[GlobalIndex].StringList) {
            int i;
        
       t@@ -2005,7 +2047,9 @@ static void WriteConfigValue(FILE *fp, int GlobalIndex, int StructIndex)
            for (i = 0; i < *Globals[GlobalIndex].MaxIndex; i++) {
              if (i > 0)
                fprintf(fp, ", ");
       -      fprintf(fp, "\"%s\"", (*Globals[GlobalIndex].StringList)[i]);
       +      fputc('"', fp);
       +      PrintEscaped(fp, (*Globals[GlobalIndex].StringList)[i]);
       +      fputc('"', fp);
            }
            fprintf(fp, " }\n");
          }