#include <gpxe/tables.h>
#include <gpxe/command.h>
#include <gpxe/settings.h>
+#include <gpxe/parse.h>
#include <lib.h>
-
-#define ENDQUOTES 0
-#define TABLE 1
-#define FUNC 2
-#define ENDTOK 3
-
/** @file
*
* Command execution
/* Hand off to command implementation */
for_each_table_entry ( cmd, COMMANDS ) {
if ( strcmp ( command, cmd->name ) == 0 ) {
- if ( branch_stack[branch_tos] || !strcmp ( cmd -> name, "if" ) || !strcmp ( cmd -> name, "fi" ) || !strcmp ( cmd -> name, "else" ) )
+ if ( branch_stack[branch_tos] || !strcmp ( cmd->name, "if" ) || !strcmp ( cmd->name, "fi" ) || !strcmp ( cmd->name, "else" ) )
return cmd->exec ( argc, ( char ** ) argv );
else
return 0;
struct argument *next;
};
-int parse_arith(char *inp_string, char **end, char **buffer);
-
char * dollar_expand ( char *inp, char **end ) {
char *expdollar;
char *name;
return exp;
}
-
-struct char_table {
- char token;
- int type;
- union {
- struct {
- struct char_table *ntable;
- int len;
- } next_table;
- char * ( *parse_func ) ( char *, char ** );
- }next;
-};
-
struct char_table dquote_table[3] = {
{ .token = '"', .type = ENDQUOTES },
{ .token = '$', .type = FUNC, .next.parse_func = dollar_expand },
{ .token = '\t', .type = ENDTOK }
};
-char * expand_string ( char * input, char **end, const struct char_table *table, int tlen, int in_quotes ) {
+char * expand_string ( char * input, char **end, const struct char_table *table, int tlen, int in_quotes, int *success ) {
char *expstr;
char *head;
char *nstr, *tmp;
int i;
int new_len;
+ *success = 0;
expstr = strdup ( input );
head = expstr;
}
if ( ! tline ) {
+ *success = 1;
head++;
} else {
- switch ( tline -> type ) {
+ 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;
//printf ( "return value: [%s]\n", expstr );
case TABLE: /* 1 for recursive call. Probably found quotes */
case FUNC: /* Call another function */
{
+ *success = 1;
*head = 0;
- nstr = ( tline -> type == TABLE ) ? ( expand_string ( head + 1, &head, tline->next.next_table.ntable, tline->next.next_table.len, 1 ) )
- : ( tline -> next.parse_func ( head, &head ) );
+ nstr = ( tline->type == TABLE ) ? ( expand_string ( head + 1, &head, tline->next.next_table.ntable, tline->next.next_table.len, 1, success ) )
+ : ( tline->next.parse_func ( head, &head ) );
tmp = expstr;
new_len = asprintf ( &expstr, "%s%s%s", expstr, nstr, head );
- if ( in_quotes || tline -> type == TABLE )
- head = expstr + strlen ( tmp ) + strlen ( nstr );
- else
- head = expstr + strlen ( tmp );
+ head = expstr + strlen ( tmp ) + strlen ( nstr );
+
free ( tmp );
free ( nstr );
if ( !nstr || new_len < 0 ) { /* if new_len < 0, expstr = NULL */
char *nstring;
struct argument *cur_arg = NULL;
int argc = 0;
+ int success;
/* Obtain temporary modifiable copy of command line */
expcmd = strdup ( command );
if ( *head == '#' && !*argv_start ) { /* Comment starts with # */
return 0;
}
- nstring = expand_string ( head, &end, table, 6, 0 );
+ nstring = expand_string ( head, &end, table, 6, 0, &success );
if ( !nstring ) {
while ( *argv_start ) {
cur_arg = *argv_start;
- *argv_start = ( *argv_start ) -> next;
+ *argv_start = ( *argv_start )->next;
free ( cur_arg );
}
+ free ( expcmd );
return -ENOMEM;
}
- if ( nstring != end ) {
+ if ( success ) {
argc++;
if ( !*argv_start ) {
*argv_start = calloc ( sizeof ( struct argument ), 1 );
cur_arg = *argv_start;
} else {
- cur_arg -> next = calloc ( sizeof ( struct argument ), 1 );
- cur_arg = cur_arg -> next;
+ cur_arg->next = calloc ( sizeof ( struct argument ), 1 );
+ cur_arg = cur_arg->next;
}
if ( !cur_arg ) {
while ( *argv_start ) {
cur_arg = *argv_start;
- *argv_start = ( *argv_start ) -> next;
+ *argv_start = ( *argv_start )->next;
free ( cur_arg );
}
+ free ( expcmd );
+ free ( nstring );
return -ENOMEM;
}
- cur_arg -> word = calloc ( end - nstring + 1, 1);
- strncpy ( cur_arg -> word, nstring, end - nstring );
+ cur_arg->word = calloc ( end - nstring + 1, 1);
+ strncpy ( cur_arg->word, nstring, end - nstring );
}
free ( expcmd );
expcmd = nstring;
for ( i = 0; i < argc; i++ ) {
struct argument *tmp;
tmp = arg;
- argv[i] = arg -> word;
- arg = arg -> next;
+ argv[i] = arg->word;
+ arg = arg->next;
free ( tmp );
//printf ( "[%s] ", argv[i] );
#include <stdio.h>
#include <errno.h>
-#ifndef __ARITH_TEST__
#include <lib.h>
-#endif
+#include <gpxe/parse.h>
#define NUM_OPS 20
#define MAX_PRIO 11
char *str_value;
}tok_value;
-struct char_table {
- char token;
- int type;
- union {
- struct {
- struct char_table *ntable;
- int len;
- } next_table;
- char * ( *parse_func ) ( char *, char ** );
- }next;
-};
-
-
/* Here is a table of the operators */
static const char op_table[NUM_OPS * 3 + 1] = { '!', SEP2 , '~', SEP2, '*', SEP2, '/', SEP2, '%', SEP2, '+', SEP2, '-', SEP2,
'<', SEP2, '<', '=', SEP1, '<', '<', SEP1, '>', SEP2, '>', '=', SEP1, '>', '>', SEP1, '&', SEP2,
'|', SEP2, '^', SEP2, '&', '&', SEP1, '|', '|', SEP1, '!', '=', SEP1, '=', '=', SEP1, '\0'
};
-//static const char *keyword_table = " \t\v()'\"$!~*/%+-<=>&|^"; /* Characters that cannot appear in a string */
static signed const char op_prio[NUM_OPS] = { 10, 10, 9, 9, 9, 8, 8, 6, 6, 7, 6, 6, 7, 4, 3, 2, 1, 0, 5, 5 };
static void ignore_whitespace ( void );
static int parse_expr ( char **buffer );
-char * expand_string ( char * input, char **end, const struct char_table *table, int tlen, int in_quotes );
-char * dollar_expand ( char *inp, char **end );
-char * parse_escape ( char *input, char **end );
-int isnum ( char *string, long *num );
-
-#define ENDQUOTES 0
-#define TABLE 1
-#define FUNC 2
-#define ENDTOK 3
-extern struct char_table dquote_table[3];
-extern struct char_table squote_table[1];
struct char_table table[21] = {
{ .token = '\\', .type = FUNC, .next.parse_func = parse_escape },
{ .token = '"', .type = TABLE, .next = { .next_table = { .ntable = dquote_table, .len = 3 } } },
char *p1, *p2;
char *strtmp = NULL, *tmp;
char *end;
+ int success;
if ( tok == -1 )
return;
}
tok = 0;
- tmp = expand_string ( inp, &end, table, 21, 1 );
+ tmp = expand_string ( inp, &end, table, 21, 1, &success );
inp = end;
free ( orig );
- orig = tmp;
- if ( tmp != end ) {
+ if ( !tmp ) {
+ tok = -1;
+ err_val = -ENOMEM;
+ return;
+ }
+
+ orig = tmp;
+ if ( success ) {
strtmp = calloc ( end - tmp + 1, 1 );
strncpy ( strtmp, tmp, end - tmp );
if ( isnum ( strtmp, &tok_value.num_value ) ) {
int bothints = 1;
long lhs, rhs;
- printf ( "lhs = %s, rhs = %s\n", op1, op2 );
-
if ( op1 ) {
if ( ! isnum ( op1, &lhs ) )
bothints = 0;