Cosmetic refactoring in fmt_new.c.
authormarkus schnalke <meillo@marmaro.de>
Thu, 9 Feb 2012 17:06:58 +0000 (18:06 +0100)
committermarkus schnalke <meillo@marmaro.de>
Thu, 9 Feb 2012 17:06:58 +0000 (18:06 +0100)
sbr/fmt_new.c

index c7c519f..9d81874 100644 (file)
@@ -9,9 +9,7 @@
 #include <h/mh.h>
 #include <h/utils.h>
 
-#define QUOTE '\\'
-
-static char *formats = 0;
+static char *formats = NULL;
 
 /*
 ** static prototypes
@@ -20,37 +18,39 @@ static void normalize(char *);
 
 
 /*
-** Get new format string
+** Copy format string (selected by precedence) and store in static memory.
+** Normalize it.
 */
-
 char *
 new_fs(char *form, char *format, char *default_fs)
 {
        struct stat st;
        register FILE *fp;
 
-       if (formats)
+       if (formats) {
                free(formats);
+       }
 
        if (form) {
-               if ((fp = fopen(etcpath(form), "r")) == NULL)
+               if ((fp = fopen(etcpath(form), "r")) == NULL) {
                        adios(form, "unable to open format file");
-
-               if (fstat(fileno(fp), &st) == -1)
+               }
+               if (fstat(fileno(fp), &st) == -1) {
                        adios(form, "unable to stat format file");
-
+               }
                formats = mh_xmalloc((size_t) st.st_size + 1);
-
-               if (read(fileno(fp), formats, (int) st.st_size) != st.st_size)
+               if (read(fileno(fp), formats, (int)st.st_size) != st.st_size) {
                        adios(form, "error reading format file");
-
+               }
                formats[st.st_size] = '\0';
-
                fclose(fp);
+
+       } else if (format) {
+               formats = getcpy(format);
+
        } else {
-               formats = getcpy(format ? format : default_fs);
+               formats = getcpy(default_fs);
        }
-
        normalize(formats);  /* expand escapes */
 
        return formats;
@@ -60,46 +60,41 @@ new_fs(char *form, char *format, char *default_fs)
 /*
 ** Expand escapes in format strings
 */
-
 static void
 normalize(char *cp)
 {
        char *dp;
 
        for (dp = cp; *cp; cp++) {
-               if (*cp != QUOTE) {
+               if (*cp != '\\') {
                        *dp++ = *cp;
-               } else {
-                       switch (*++cp) {
-                       case 'b':
-                               *dp++ = '\b';
-                               break;
-
-                       case 'f':
-                               *dp++ = '\f';
-                               break;
-
-                       case 'n':
-                               *dp++ = '\n';
-                               break;
-
-                       case 'r':
-                               *dp++ = '\r';
-                               break;
-
-                       case 't':
-                               *dp++ = '\t';
-                               break;
-
-                       case '\n':
-                               break;
+                       continue;
+               }
 
-                       case 0:
-                               cp--;  /* fall */
-                       default:
-                               *dp++ = *cp;
-                               break;
-                       }
+               switch (*++cp) {
+               case 'b':
+                       *dp++ = '\b';
+                       break;
+               case 'f':
+                       *dp++ = '\f';
+                       break;
+               case 'n':
+                       *dp++ = '\n';
+                       break;
+               case 'r':
+                       *dp++ = '\r';
+                       break;
+               case 't':
+                       *dp++ = '\t';
+                       break;
+               case '\n':
+                       break;
+               case '\0':
+                       cp--;
+                       /* fall */
+               default:
+                       *dp++ = *cp;
+                       break;
                }
        }
        *dp = '\0';