prof_parse.c (parse_std_line, parse_quoted_string, need_double_quotes,
authorTheodore Tso <tytso@mit.edu>
Sat, 24 Jan 1998 02:02:30 +0000 (02:02 +0000)
committerTheodore Tso <tytso@mit.edu>
Sat, 24 Jan 1998 02:02:30 +0000 (02:02 +0000)
output_quoted_string, dump_profile, dump_profile_to_file): Vastly
improved the profile parsing; whitespace at the end of lines are now
ignored.  Added quoted string parsing, complete with backquote
processing.  Strings which need to be quoted are properly quoted on
output.

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@10373 dc483132-0cff-0310-8789-dd5450dbe970

src/util/profile/ChangeLog
src/util/profile/prof_parse.c
src/util/profile/test.ini

index 834a44949b9e693ff64190569e2e2089a02fc557..0a1bdf04f15f5a18ad92bff8cd266029a3371f2e 100644 (file)
@@ -1,3 +1,13 @@
+Fri Jan 23 20:55:06 1998  Theodore Ts'o  <tytso@rsts-11.mit.edu>
+
+       * prof_parse.c (parse_std_line, parse_quoted_string,
+               need_double_quotes, output_quoted_string, dump_profile,
+               dump_profile_to_file): Vastly improved the profile
+               parsing; whitespace at the end of lines are now ignored.
+               Added quoted string parsing, complete with backquote
+               processing.  Strings which need to be quoted are properly
+               quoted on output.
+
 Sat Feb 22 18:33:17 1997  Richard Basch  <basch@lehman.com>
 
        * Makefile.in: Move list file construction to win-post.in
index 941cc33f7c97eb65108313d82def1589fc8af624..e7e72b8cda1f9fe558944a68057be8f34a2c6896 100644 (file)
@@ -43,6 +43,36 @@ static void strip_line(line)
        }
 }
 
+static void parse_quoted_string(char *str)
+{
+       char *to, *from;
+
+       to = from = str;
+
+       for (to = from = str; *from && *from != '"'; to++, from++) {
+               if (*from == '\\') {
+                       from++;
+                       switch (*from) {
+                       case 'n':
+                               *to = '\n';
+                               break;
+                       case 't':
+                               *to = '\t';
+                               break;
+                       case 'b':
+                               *to = '\b';
+                               break;
+                       default:
+                               *to = *from;
+                       }
+                       continue;
+               }
+               *to = *from;
+       }
+       *to = '\0';
+}
+
+
 static errcode_t parse_init_state(state)
        struct parse_state *state;
 {
@@ -126,12 +156,19 @@ static errcode_t parse_std_line(line, state)
        }
        cp = skip_over_blanks(cp+1);
        value = cp;
-       if (value[0] == 0) {
+       if (value[0] == '"') {
+               value++;
+               parse_quoted_string(value);
+       } else if (value[0] == 0) {
                do_subsection++;
                state->state = STATE_GET_OBRACE;
-       }
-       if (value[0] == '{' && value[1] == 0) 
+       } else if (value[0] == '{' && value[1] == 0) 
                do_subsection++;
+       else {
+               cp = value + strlen(value) - 1;
+               while ((cp > value) && isspace(*cp))
+                       *cp-- = 0;
+       }
        if (do_subsection) {
                retval = profile_add_node(state->current_section,
                                          tag, 0, &state->current_section);
@@ -200,6 +237,60 @@ errcode_t profile_parse_file(f, root)
        return 0;
 }
 
+/*
+ * Return TRUE if the string begins or ends with whitespace
+ */
+static int need_double_quotes(str)
+       char *str;
+{
+       if (!str || !*str)
+               return 0;
+       if (isspace(*str) ||isspace(*(str + strlen(str) - 1)))
+               return 1;
+       if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b'))
+               return 1;
+       return 0;
+}
+
+/*
+ * Output a string with double quotes, doing appropriate backquoting
+ * of characters as necessary.
+ */
+static void output_quoted_string(str, f)
+       char    *str;
+       FILE    *f;
+{
+       char    ch;
+       
+       fputc('"', f);
+       if (!str) {
+               fputc('"', f);
+               return;
+       }
+       while (ch = *str++) {
+               switch (ch) {
+               case '\\':
+                       fputs("\\\\", f);
+                       break;
+               case '\n':
+                       fputs("\\n", f);
+                       break;
+               case '\t':
+                       fputs("\\t", f);
+                       break;
+               case '\b':
+                       fputs("\\b", f);
+                       break;
+               default:
+                       fputc(ch, f);
+                       break;
+               }
+       }
+       fputc('"', f);
+}
+
+
+
 #if defined(_MSDOS) || defined(_WIN32)
 #define EOL "\r\n"
 #endif
@@ -232,7 +323,13 @@ void dump_profile(root, level)
                        break;
                for (i=0; i < level; i++)
                        printf("   ");
-               printf("%s = '%s'%s", name, value, EOL);
+               if (need_double_quotes(value)) {
+                       fputs(name, stdout);
+                       fputs(" = ", stdout);
+                       output_quoted_string(value, stdout);
+                       fputs(EOL, stdout);
+               } else
+                       printf("%s = '%s'%s", name, value, EOL);
        } while (iter != 0);
 
        iter = 0;
@@ -269,7 +366,13 @@ void dump_profile_to_file(root, level, dstfile)
                        break;
                for (i=0; i < level; i++)
                        fprintf(dstfile, "\t");
-               fprintf(dstfile, "%s = %s%s", name, value, EOL);
+               if (need_double_quotes(value)) {
+                       fputs(name, dstfile);
+                       fputs(" = ", dstfile);
+                       output_quoted_string(value, dstfile);
+                       fputs(EOL, dstfile);
+               } else
+                       fprintf(dstfile, "%s = %s%s", name, value, EOL);
        } while (iter != 0);
 
        iter = 0;
index 505c4a1d2f0c3ae7dd6c5c0f67b03c1602b61a72..c1c8830aa37bc59dfd876aad53a5768374f9bf09 100644 (file)
@@ -1,11 +1,12 @@
 this is a comment.  Everything up to the first square brace is ignored.
 
 [test section 2]
-       child_section2 = one
+       test_child = "foo\nbar"
+       child_section2 = "one"
        child_section2 = {
                child = slick
                child = harry
-               child = john
+               child = "john\tb "
        }
        child_section2 = foo
 
@@ -21,7 +22,7 @@ ATHENA.MIT.EDU = {
 
 
 [test section 1]
-    foo = "bar"
+    foo = "bar "
 
 [test section 2]
        quux = "bar"