Commits

Armin Ronacher committed b2a4697

Added support for defaults

Comments (0)

Files changed (3)

     free(options->format);
     free(options->default_branch);
     free(options->default_revision);
-    free(options->default_unknown);
-    free(options->default_modified);
     free(options);
 }
 
     int show_modified;                  /* show ! if local changes? */
     char* default_branch;
     char* default_revision;
-    char* default_unknown;
-    char* default_modified;
 } options_t;
 
 typedef struct {
     return 0;
 }
 
+static size_t read_default_format(const char* format, size_t i, char** out)
+{
+    size_t start = i + 1;
+    while (format[i] && format[i] != ']')
+        i++;
+    if (format[i])
+        i++;
+    *out = malloc(i - start + 1);
+    strncpy(*out, format + start, i - start);
+    return i;
+}
+
 void parse_format(options_t* options)
 {
     size_t i;
     options->show_modified = 0;
 
     char* format = options->format;
-    const char* next_default = NULL;
+    char* new_format = malloc(strlen(format) + 1);
+    char* fmtp = new_format;
+    char* next_default = NULL;
     size_t len = strlen(format);
     for (i = 0; i < len; i++) {
         if (format[i] == '%') {
-            i++;
-            switch (format[i]) {
+            *fmtp++ = format[i++];
+test:       switch (format[i]) {
                 case '\0':              /* at end of string: ignore */
                     break;
+                case '[':               /* default section */
+                    i = read_default_format(format, i, &next_default);
+                    goto test;          /* consumed default, try again */
                 case 'b':
                     options->show_branch = 1;
-                    break;
+                    options->default_branch = next_default;
+                    next_default = NULL;
+                    goto end_of_loop;
                 case 'r':
                     options->show_revision = 1;
-                    break;
+                    options->default_revision = next_default;
+                    next_default = NULL;
+                    goto end_of_loop;
                 case 'u':
                     options->show_unknown = 1;
-                    break;
+                    goto end_of_loop;
                 case 'm':
                     options->show_modified = 1;
-                    break;
+                    goto end_of_loop;
                 case 'n':               /* name of VC system */
                 case '%':
-                    break;
+                    goto end_of_loop;
                 default:
                     fprintf(stderr,
                             "error: invalid format string: %%%c\n",
                     break;
             }
         }
+        next_default = NULL;
+end_of_loop:
+        *fmtp++ = format[i];
     }
+
+    *fmtp = '\0';
+    free(options->format);
+    options->format = new_format;
 }
 
 void print_result(vccontext_t* context, options_t* options, result_t* result)