[scripting] Moved scripting functions to a new file
authorLynus Vaz <lynus@Arch.localdomain>
Thu, 25 Jun 2009 06:03:25 +0000 (11:33 +0530)
committerLynus Vaz <lynus@Arch.localdomain>
Thu, 25 Jun 2009 07:54:55 +0000 (13:24 +0530)
src/core/exec.c
src/hci/arith.c
src/hci/commands/if_cmd.c
src/hci/parse.c [new file with mode: 0644]
src/include/gpxe/parse.h

index fb52679..0b109ff 100644 (file)
@@ -94,74 +94,6 @@ int execv ( const char *command, char * const argv[] ) {
        return -ENOEXEC;
 }
 
-
-
-char * dollar_expand ( char *inp, char **end ) {
-       char *expdollar;
-       char *name;
-       int setting_len;
-       
-       *inp = 0;
-       if ( inp[1] == '{' ) {
-               name = ( inp + 2 );
-
-               /* Locate closer */
-               *end = strstr ( name, "}" );
-               if ( ! *end ) {
-                       printf ( "can't find ending }\n" );
-                       return NULL;
-               }
-               **end = '\0';
-               *end += 1;
-               
-               /* Determine setting length */
-               setting_len = fetchf_named_setting ( name, NULL, 0 );
-               if ( setting_len < 0 )
-                       setting_len = 0; /* Treat error as empty setting */
-
-               /* Read setting into temporary buffer */
-               {
-                       expdollar = malloc ( setting_len + 1 );
-
-                       expdollar[0] = '\0';
-                       fetchf_named_setting ( name, expdollar,
-                                              setting_len + 1 );
-                       return expdollar;
-               }
-       } else if ( inp[1] == '(' ) {
-               name = ( inp + 1 );
-               {
-                       int ret;
-                       ret = parse_arith ( name, end, &expdollar );
-                       
-                       if( ret < 0 ) {
-                               return NULL;
-                       }
-                       
-                       return expdollar;
-               }
-       }
-       /* Can't find { or (, so preserve the $ (current behaviour) */
-       *end = inp + 1;
-       asprintf ( &expdollar, "%c", '$' );
-       return expdollar;
-}
-
-char * parse_escape ( char *input, char **end ) {
-       char *exp;
-       *input = 0;
-       if ( input[1] == '\n' ) {
-               *end = input + 2;
-               exp = malloc ( 1 );
-               if ( exp )
-                       *exp = 0;
-       } else {
-               *end = input + 2;
-               asprintf ( &exp, "%c", input[1] );
-       }
-       return exp;
-}
-
 struct char_table dquote_table[3] = {
        { .token = '"', .type = ENDQUOTES },
        { .token = '$', .type = FUNC, .next.parse_func = dollar_expand },
@@ -180,78 +112,6 @@ static struct char_table table[6] = {
        { .token = '\t', .type = ENDTOK }
 };
 
-char * expand_string ( const char * input, char **end, const struct char_table *table, int tlen, int in_quotes, int *success ) {
-       char *expstr;
-       char *head;
-       char *tmp;
-       int i;
-       int new_len;
-       
-       *success = 0;
-       expstr = strdup ( input );
-       head = expstr;
-       
-       while ( *head ) {
-               const struct char_table * tline = NULL;
-               
-               for ( i = 0; i < tlen; i++ ) {
-                       if ( table[i].token == *head ) {
-                               tline = table + i;
-                               break;
-                       }
-               }
-               
-               if ( ! tline ) {
-                       *success = 1;
-                       head++;
-               } else {
-                       switch ( tline->type ) {
-                               case ENDQUOTES: /* 0 for end of input, where next char is to be discarded. Used for ending ' or " */
-                                       *success = 1;
-                                       *head = 0;
-                                       *end = head + 1;
-                                       return expstr;
-                                       break;
-                               case TABLE: /* 1 for recursive call. Probably found quotes */
-                               case FUNC: /* Call another function */
-                                       {
-                                               char *nstr;
-                                               int s2;
-                                               
-                                               *success = 1;
-                                               *head = 0;
-                                               nstr = ( tline->type == TABLE ) ? ( expand_string ( head + 1, &head, tline->next.next_table.ntable, tline->next.next_table.len, 1, &s2 ) ) 
-                                                                                                       : ( tline->next.parse_func ( head, &head ) );
-                                               tmp = expstr;
-                                               
-                                               new_len = asprintf ( &expstr, "%s%s%s", expstr, nstr, head );
-                                               head = expstr + strlen ( tmp ) + strlen ( nstr );
-                                               
-                                               free ( tmp );
-                                               free ( nstr );
-                                               if ( !nstr || new_len < 0 ) { /* if new_len < 0, expstr = NULL */
-                                                       free ( expstr );
-                                                       return NULL;
-                                               }
-                                       }
-                                       break;
-                               case ENDTOK: /* End of input, and we also want next character */
-                                       *end = head;
-                                       return expstr;
-                                       break;
-                       }
-               }
-               
-       }
-       if ( in_quotes ) {
-               printf ( "can't find closing '%c'\n", table[0].token );
-               free ( expstr );
-               return NULL;
-       }
-       *end = head;
-       return expstr;
-}
-
 /**
  * Expand variables within command line
  *
index 1602e82..cc4d17b 100644 (file)
@@ -391,7 +391,7 @@ static int parse_expr ( char **buffer ) {
        return parse_prio ( -1, buffer );
 }
 
-int parse_arith ( char *inp_string, char **end, char **buffer ) {
+int parse_arith ( const char *inp_string, char **end, char **buffer ) {
        err_val = tok = 0;
        orig = strdup ( inp_string );
        inp = orig;
index 50e6c21..e0443a0 100644 (file)
@@ -24,7 +24,7 @@ static int if_exec ( int argc, char **argv ) {
        push_generic_stack ( &else_stack, &zero, 0 );
        push_generic_stack ( &if_stack, &cond, 0 );
        
-       if ( ( ( int * ) if_stack.ptr )[if_tos] != 0 )
+       if ( ( if_tos == -1 || ( ( int * ) if_stack.ptr )[if_tos] ) && if_stack.tos - if_tos == 1 )             /* If the top of logical stack == 1 and the current command has been pushed over it */
                if_tos++;
        
        //printf ( "Condition is %ld\n", cond );
diff --git a/src/hci/parse.c b/src/hci/parse.c
new file mode 100644 (file)
index 0000000..ad10d1f
--- /dev/null
@@ -0,0 +1,144 @@
+#include <gpxe/parse.h>
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <gpxe/settings.h>
+
+/* The returned string is allocated on the heap and the calling function must free it */
+char * dollar_expand ( char *inp, char ** end ) {
+       char *expdollar;
+       const char *name;
+       int setting_len;
+       
+       if ( inp[1] == '{' ) {
+               name = ( inp + 2 );
+
+               /* Locate closer */
+               *end = strstr ( name, "}" );
+               if ( ! *end ) {
+                       printf ( "can't find ending }\n" );
+                       return NULL;
+               }
+               **end = 0;
+               *end += 1;
+               
+               /* Determine setting length */
+               setting_len = fetchf_named_setting ( name, NULL, 0 );
+               if ( setting_len < 0 )
+                       setting_len = 0; /* Treat error as empty setting */
+
+               /* Read setting into buffer */
+               expdollar = malloc ( setting_len + 1 );
+               if ( expdollar ) {
+                       expdollar[0] = '\0';
+                       fetchf_named_setting ( name, expdollar,
+                                                       setting_len + 1 );
+               }
+               return expdollar;
+               
+       } else if ( inp[1] == '(' ) {
+               name = ( inp + 1 );
+               {
+                       int ret;
+                       ret = parse_arith ( name, end, &expdollar );
+                       
+                       if( ret < 0 ) {
+                               return NULL;
+                       }
+                       
+                       return expdollar;
+               }
+       }
+       /* Can't find { or (, so preserve the $ */
+       *end = inp + 1;
+       asprintf ( &expdollar, "%c", '$' );
+       return expdollar;
+}
+
+/* The returned string is allocated on the heap and the calling function must free it */
+char * parse_escape ( char *input, char **end ) {
+       char *exp;
+       *end = input + 2;
+       if ( input[1] == '\n' ) {
+               exp = malloc ( 1 );
+               if ( exp )
+                       *exp = 0;
+       } else {
+               asprintf ( &exp, "%c", input[1] );
+       }
+       return exp;
+}
+
+/* The returned string is allocated on the heap and the calling function must free it */
+char * expand_string ( const char * input, char **end, const struct char_table *table, int tlen, int in_quotes, int *success ) {
+       char *expstr;
+       char *head;
+       char *tmp;
+       int i;
+       int new_len;
+       
+       *success = 0;
+       expstr = strdup ( input );
+       
+       head = expstr;
+       
+       while ( *head ) {
+               const struct char_table * tline = NULL;
+               
+               for ( i = 0; i < tlen; i++ ) {
+                       if ( table[i].token == *head ) {
+                               tline = table + i;
+                               break;
+                       }
+               }
+               
+               if ( ! tline ) {
+                       *success = 1;
+                       head++;
+               } else {
+                       switch ( tline->type ) {
+                               case ENDQUOTES: /* 0 for end of input, where next char is to be discarded. Used for ending ' or " */
+                                       *success = 1;
+                                       *head = 0;
+                                       *end = head + 1;
+                                       return expstr;
+                                       break;
+                               case TABLE: /* 1 for recursive call. Probably found quotes */
+                               case FUNC: /* Call another function */
+                                       {
+                                               char *nstr;
+                                               int s2;
+                                               
+                                               *success = 1;
+                                               *head = 0;
+                                               nstr = ( tline->type == TABLE ) ? ( expand_string ( head + 1, &head, tline->next.next_table.ntable, tline->next.next_table.len, 1, &s2 ) ) 
+                                                                                                       : ( tline->next.parse_func ( head, &head ) );
+                                               tmp = expstr;
+                                               new_len = asprintf ( &expstr, "%s%s%s", expstr, nstr, head );
+                                               head = expstr + strlen ( tmp ) + strlen ( nstr );
+                                               
+                                               free ( tmp );
+                                               free ( nstr );
+                                               if ( !nstr || new_len < 0 ) { /* if new_len < 0, expstr = NULL */
+                                                       free ( expstr );
+                                                       return NULL;
+                                               }
+                                       }
+                                       break;
+                               case ENDTOK: /* End of input, and we also want next character */
+                                       *end = head;
+                                       return expstr;
+                                       break;
+                       }
+               }
+               
+       }
+       if ( in_quotes ) {
+               printf ( "can't find closing '%c'\n", table[0].token );
+               free ( expstr );
+               return NULL;
+       }
+       *end = head;
+       return expstr;
+}
index 8e8ae51..2d6a99a 100644 (file)
@@ -18,7 +18,7 @@ struct char_table {
        }next;
 };
 
-int parse_arith ( char *inp_string, char **end, char **buffer );
+int parse_arith ( const char *inp_string, char **end, char **buffer );
 
 char * expand_string ( const char * input, char **end, const struct char_table *table, int tlen, int in_quotes, int *success );
 char * dollar_expand ( char *inp, char **end );