int optind;
int nextchar;
-extern int branch_stack[10];
-extern int branch_tos;
+extern struct generic_stack if_stack;
+extern int if_tos;
/**
* Execute command
/* 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 ( !if_stack.ptr || ( ( int * ) if_stack.ptr )[if_tos] || !strcmp ( cmd->name, "if" ) || !strcmp ( cmd->name, "fi" ) || !strcmp ( cmd->name, "else" ) )
return cmd->exec ( argc, ( char ** ) argv );
else
return 0;
#include <stdio.h>
+#include <unistd.h>
#include <gpxe/command.h>
+#include <gpxe/gen_stack.h>
-int branch_stack[10] = { 1 };
-int branch_tos = 0;
-static int logical_tos = 0;
-static int in_else[10];
+#include <lib.h>
+
+struct generic_stack if_stack = { .ptr = NULL, .tos = -1, .size = sizeof ( int ) };
+static struct generic_stack else_stack = { .ptr = NULL, .tos = -1, .size = sizeof ( int ) };
+int if_tos = 0;
int isnum ( char *string, long *num );
static int if_exec ( int argc, char **argv ) {
long cond;
+ int zero = 0;
if ( argc != 2 ) {
printf ( "Syntax: if <condition>\n" );
return -1;
printf ( "non-numeric condition\n" );
return -1;
}
+ cond = cond ? 1 : 0;
+ push_generic_stack ( &else_stack, &zero, 0 );
+ push_generic_stack ( &if_stack, &cond, 0 );
- in_else[logical_tos+1] = 0;
- if ( logical_tos > branch_tos || branch_stack[branch_tos] == 0 ) {
- logical_tos++;
- return 0;
- }
+ if ( ( ( int * ) if_stack.ptr )[if_tos] != 0 )
+ if_tos++;
//printf ( "Condition is %ld\n", cond );
- logical_tos = ++branch_tos;
- branch_stack[branch_tos] = cond ? 1 : 0;
return 0;
}
};
static int fi_exec ( int argc, char **argv ) {
+ int cond;
if ( argc != 1 ) {
printf ( "Syntax: %s\n", argv[0] );
return -1;
}
- if ( logical_tos > branch_tos ) {
- logical_tos--;
- return 0;
- }
-
- if ( branch_tos )
- logical_tos = --branch_tos;
- else {
+ if ( pop_generic_stack ( &if_stack, &cond ) == 0 ) {
+ if ( if_tos - 1 == if_stack.tos ) {
+ if_tos = if_stack.tos;
+ }
+ } else {
printf ( "fi without if\n" );
return -1;
}
printf ( "Syntax: %s\n", argv[0] );
}
- //printf ( "logical_tos = %d, branch_tos = %d\n", logical_tos, branch_tos );
- if ( ( logical_tos == 0 ) || ( in_else[logical_tos] != 0 ) ) {
+ if ( ( ( int * ) else_stack.ptr )[else_stack.tos] != 0 ) {
printf ( "else without if\n" );
return -1;
}
- in_else[logical_tos] = 1;
- if ( logical_tos > branch_tos ) {
- return 0;
+ if ( if_tos == if_stack.tos ) {
+ ( ( int * ) if_stack.ptr )[if_stack.tos] = !( ( ( int * ) if_stack.ptr )[if_stack.tos] );
}
-
- branch_stack[branch_tos] = !branch_stack[branch_tos];
return 0;
}