[scripting] Modified parse.c functions.
authorLynus Vaz <lynus@Arch.localdomain>
Fri, 26 Jun 2009 11:14:31 +0000 (16:44 +0530)
committerLynus Vaz <lynus@Arch.localdomain>
Fri, 26 Jun 2009 11:14:31 +0000 (16:44 +0530)
src/core/exec.c
src/hci/arith.c
src/hci/commands/if_cmd.c
src/hci/parse.c
src/include/gpxe/gen_stack.h
src/include/gpxe/parse.h

index 25b31f1..0175a26 100644 (file)
@@ -93,7 +93,6 @@ int execv ( const char *command, char * const argv[] ) {
 
 static int expand_command ( const char *command, struct generic_stack *argv_stack ) {
        char *head, *end;
-       char *nstring;
        int success;
        int argc;
        
@@ -122,13 +121,16 @@ static int expand_command ( const char *command, struct generic_stack *argv_stac
                        break;
                }
                head = expcmd.value;
-               nstring = expand_string ( &expcmd, &head, &end, table, 6, 0, &success );
-               if ( nstring ) {
+               end = expand_string ( &expcmd, head, table, 6, 0, &success );
+               if ( end ) {
                        if ( success ) {
+                               char *argv = expcmd.value;
                                argc++;
-                               push_generic_stack ( argv_stack, &expcmd.value, 0 );
                                expcmd.value = NULL;
-                               stringcpy ( &expcmd, end );
+                               if ( push_generic_stack ( argv_stack, &argv, 0 ) < 0 || !stringcpy ( &expcmd, end ) ) {
+                                       argc = -ENOMEM;
+                                       break;
+                               }
                                *end = 0;
                                /*
                                So if the command is: word1 word2 word3
index 1f321d7..5618e06 100644 (file)
@@ -61,9 +61,10 @@ static int parse_expr ( char **buffer );
 static void input ( void ) {
        char t_op[3] = { '\0', '\0', '\0'};
        char *p1, *p2;
-       char *tmp, *strtmp = NULL;
+       char *strtmp = NULL;
        char *end;
        int success;
+       int start;
        
        if ( tok == -1 )
                return;
@@ -76,15 +77,16 @@ static void input ( void ) {
        }
        tok = 0;
        
-       tmp = expand_string ( input_str, &inp_ptr, &end, arith_table, 21, 0, &success );
-       if ( !tmp ) {
+       start = inp_ptr - input_str->value;
+       end = expand_string ( input_str, inp_ptr, arith_table, 21, 0, &success );
+       if ( !end ) {
                tok = -1;
                err_val = -ENOMEM;
                return;
        }
        
        if ( success ) {
-               strtmp = strndup ( inp_ptr, end - inp_ptr );
+               strtmp = strndup ( input_str->value + start, ( end - input_str->value ) - start );
                if ( isnum ( strtmp, &tok_value.num_value ) ) {
                        free ( strtmp );
                        tok = TOK_NUMBER;
@@ -356,9 +358,10 @@ static int parse_expr ( char **buffer ) {
        return parse_prio ( -1, buffer );
 }
 
-int parse_arith ( struct string *inp, char *orig, char **end ) {
+char * parse_arith ( struct string *inp, char *orig ) {
        char *buffer = NULL;
        int start;
+       char *end = NULL;
        
        start = orig - inp->value;
        
@@ -378,9 +381,9 @@ int parse_arith ( struct string *inp, char *orig, char **end ) {
                } else {
                        orig = inp->value + start;
                        *orig = 0;
-                       *end = inp_ptr;
-                       orig = string3cat ( inp, buffer, *end );
-                       *end = orig + start + strlen ( buffer );
+                       end = inp_ptr;
+                       orig = string3cat ( inp, buffer, end );
+                       end = orig + start + strlen ( buffer );
                }
        }
                
@@ -405,9 +408,9 @@ int parse_arith ( struct string *inp, char *orig, char **end ) {
                                break;
                }
                free_string ( inp );
-               return err_val;
+               return end;
        }
        
-       return 0;
+       return end;
 }
 
index 884d53b..c329ec7 100644 (file)
@@ -22,8 +22,8 @@ static int if_exec ( int argc, char **argv ) {
        }
        cond = TOP_GEN_STACK_INT ( &if_stack ) ? ( cond ? 1 : 0 ) : 0;
        if ( ( push_generic_stack ( &else_stack, &zero, 0 ) < 0 ) || ( push_generic_stack ( &if_stack, &cond, 0 ) < 0 ) ) {
-               free_generic_stack ( &if_stack );
-               free_generic_stack ( &else_stack );
+               free_generic_stack ( &if_stack, 0 );
+               free_generic_stack ( &else_stack, 0 );
                return 1;
        }
 
index 0a5b448..5d827b3 100644 (file)
@@ -83,10 +83,11 @@ Before: [start]$<exp>[end]
 End: [start]<expanded>[end]
 and *end points to [end]
 */
-char * dollar_expand ( struct string *s, char *inp, char ** end ) {
+char * dollar_expand ( struct string *s, char *inp ) {
        char *name;
        int setting_len;
        int len;
+       char *end;
        
        len = inp - s->value;
        
@@ -95,14 +96,14 @@ char * dollar_expand ( struct string *s, char *inp, char ** end ) {
                name = ( inp + 2 );
 
                /* Locate closer */
-               *end = strstr ( name, "}" );
-               if ( ! *end ) {
+               end = strstr ( name, "}" );
+               if ( ! end ) {
                        printf ( "can't find ending }\n" );
                        free_string ( s );
-                       return NULL;
+                       return end;
                }
-               **end = 0;
-               *end += 1;
+               *end = 0;
+               end += 1;
                
                /* Determine setting length */
                setting_len = fetchf_named_setting ( name, NULL, 0 );
@@ -115,59 +116,58 @@ char * dollar_expand ( struct string *s, char *inp, char ** end ) {
                        expdollar[0] = '\0';
                        fetchf_named_setting ( name, expdollar,
                                                        setting_len + 1 );
-                       if ( string3cat ( s, expdollar, *end ) )
-                               *end = s->value + len + strlen ( expdollar );
+                       if ( string3cat ( s, expdollar, end ) )
+                               end = s->value + len + strlen ( expdollar );
                }
-               return s->value;
+               return end;
        } else if ( inp[1] == '(' ) {
                name = inp;
                {
-                       int ret;
-                       ret = parse_arith ( s, name, end );
-                       return s->value;                /* if ret < 0, s->value = NULL */
+                       end = parse_arith ( s, name );
+                       return end;
                }
        }
        /* Can't find { or (, so preserve the $ */
-       *end = inp + 1;
-       return s->value;
+       end = inp + 1;
+       return end;
 }
 
-char * parse_escape ( struct string *s, char *input, char **end ) {
+char * parse_escape ( struct string *s, char *input ) {
        char *exp;
+       char *end;
+       
        if ( ! input[1] ) {
                printf ( "stray \\\n" );
-               return s->value;
+               return input + 1;
        }
        *input = 0;
-       *end = input + 2;
+       end = input + 2;
        if ( input[1] == '\n' ) {
                int len = input - s->value;
-               exp = stringcat ( s, *end );
-               *end = exp + len;
+               exp = stringcat ( s, end );
+               end = exp + len;
        } else {
                int len = input - s->value;
-               *end = input + 1;
-               exp = stringcat ( s, *end );
-               *end = exp + len + 1;
+               end = input + 1;
+               exp = stringcat ( s, end );
+               end = exp + len + 1;
        }
-       return exp;
+       return end;
 }
 
-/* Both *head and *end point somewhere within s */
-char * expand_string ( struct string *s, char **head, char **end, const struct char_table *table, int tlen, int in_quotes, int *success ) {
+/* Return a pointer to the first unconsumed character */
+char * expand_string ( struct string *s, char *head, const struct char_table *table, int tlen, int in_quotes, int *success ) {
        int i;
-       char *cur_pos;
-       int start;
+       int cur_pos;    /* s->value may be reallocated, so this seems better */
        
        *success = 0;
-       start = *head - s->value;
-       cur_pos = *head;
+       cur_pos = head - s->value;
        
-       while ( *cur_pos ) {
+       while ( s->value[cur_pos] ) {
                const struct char_table * tline = NULL;
                
                for ( i = 0; i < tlen; i++ ) {
-                       if ( table[i].token == *cur_pos ) {
+                       if ( table[i].token == s->value[cur_pos] ) {
                                tline = table + i;
                                break;
                        }
@@ -180,51 +180,42 @@ char * expand_string ( struct string *s, char **head, char **end, const struct c
                        switch ( tline->type ) {
                                case ENDQUOTES: /* 0 for end of input, where next char is to be discarded. Used for ending ' or " */
                                {
-                                       int pos = cur_pos - s->value;
-                                       char *t;
                                        *success = 1;
-                                       *cur_pos = 0;
-                                       if ( ( t = stringcat ( s, cur_pos + 1 ) ) ) {
-                                               *end = t + pos;
-                                               *head = s->value + start;
-                                       }
-                                       return s->value;
+                                       s->value[cur_pos] = 0;
+                                       if ( ! stringcat ( s, s->value + cur_pos + 1 ) )
+                                               return NULL;
+                                       return s->value + cur_pos;
                                }
                                        break;
                                case TABLE: /* 1 for recursive call. Probably found quotes */
                                        {
                                                int s2;
-                                               char *end;
                                                char *tmp;
                                                
                                                *success = 1;
-                                               *cur_pos = 0;
-                                               tmp = s->value;
-                                               
-                                               if ( !stringcat ( s, cur_pos + 1 ) ) {
-                                                       printf ( "stringcat failed\n" );
+                                               s->value[cur_pos] = 0;
+                                               /* Remove the current character and call recursively */
+                                               if ( !stringcat ( s, s->value + cur_pos + 1 ) )
                                                        return NULL;
-                                               }
-                                               /* tmp is now invalid. Should not be dereferenced */
-                                               cur_pos = s->value + ( cur_pos - tmp );
-                                               if ( !expand_string ( s, &cur_pos, &end, tline->next.next_table.ntable, tline->next.next_table.len, 1, &s2 ) ) {
+                                               
+                                               tmp = expand_string ( s, s->value + cur_pos, tline->next.next_table.ntable, tline->next.next_table.len, 1, &s2 );
+                                               if ( ! tmp )
                                                        return NULL;
-                                               }
-                                               cur_pos = end;
+                                               cur_pos = tmp - s->value;
                                        }
                                        break;
                                case FUNC: /* Call another function */
                                        {
+                                               char *tmp;
                                                *success = 1;
-                                               if ( ! tline->next.parse_func ( s, cur_pos, &cur_pos ) )
+                                               if ( ! ( tmp = tline->next.parse_func ( s, s->value + cur_pos ) ) )
                                                        return NULL;
+                                               cur_pos = tmp - s->value;
                                        }
                                        break;
                                        
                                case ENDTOK: /* End of input, and we also want next character */
-                                       *end = cur_pos;
-                                       *head = s->value + start;
-                                       return s->value;
+                                       return s->value + cur_pos;
                                        break;
                        }
                }
@@ -235,7 +226,5 @@ char * expand_string ( struct string *s, char **head, char **end, const struct c
                free_string ( s );
                return NULL;
        }
-       *end = cur_pos;
-       *head = s->value + start;
-       return s->value;
+       return s->value + cur_pos;
 }
index 4d82a92..620dd77 100644 (file)
@@ -12,9 +12,9 @@ struct generic_stack {
 };
 
 void init_generic_stack ( struct generic_stack *stack, size_t size );
-int push_generic_stack ( struct generic_stack *stack, void *str, int is_string );
+int push_generic_stack ( struct generic_stack *stack, void *str, int is_string );      /* Use is_string = 1 to allocate a new string on the heap */
 int pop_generic_stack ( struct generic_stack *stack, void *ptr );
-void free_generic_stack ( struct generic_stack *stack, int on_stack );
+void free_generic_stack ( struct generic_stack *stack, int on_stack );                 /* Use on_stack = 1 to free stack values on the heap */
 
 /* convenience macros */
 #define TOP_GEN_STACK_INT( stack ) ( ( ( int * ) ( stack )->ptr )[( stack )->tos] )
index 0fdd952..b718e13 100644 (file)
@@ -3,10 +3,10 @@
 
 #include <stdint.h>
 
-#define                        ENDQUOTES       0
-#define                        TABLE           1
-#define                        FUNC            2
-#define                        ENDTOK          3
+#define ENDQUOTES 0
+#define TABLE 1
+#define FUNC   2
+#define ENDTOK 3
 
 struct string {
        char *value;
@@ -20,15 +20,15 @@ struct char_table {
                        const struct char_table *ntable;
                        int len;
                } next_table;
-               char * ( *parse_func ) ( struct string *, char *, char ** );
-       }next;
+               char * ( *parse_func ) ( struct string *, char * );
+       }next; 
 };
 
-int parse_arith ( struct string *inp, char *inp_str, char **end );
+char * parse_arith ( struct string *inp, char *inp_str );
 
-char * expand_string ( struct string *s, char **head, char **end, const struct char_table *table, int tlen, int in_quotes, int *success );
-char * dollar_expand ( struct string *s, char *inp, char ** end );
-char * parse_escape ( struct string *s, char *input, char **end );
+char * expand_string ( struct string *s, char *head, const struct char_table *table, int tlen, int in_quotes, int *success );
+char * dollar_expand ( struct string *s, char *inp );
+char * parse_escape ( struct string *s, char *input );
 int isnum ( char *string, long *num );
 
 void free_string ( struct string *s );