3 #include <gpxe/command.h>
4 #include <gpxe/gen_stack.h>
7 struct generic_stack if_stack;
8 static struct generic_stack else_stack;
9 int isnum ( char *string, long *num );
11 static int if_exec ( int argc, char **argv ) {
15 printf ( "Syntax: if <condition>\n" );
19 if ( !isnum ( argv[1], &cond ) ) {
20 printf ( "non-numeric condition: %s\n", argv[1] );
23 cond = TOP_GEN_STACK_INT ( &if_stack ) ? ( cond ? 1 : 0 ) : 0;
24 if ( ( push_generic_stack ( &else_stack, &zero, 0 ) < 0 ) || ( push_generic_stack ( &if_stack, &cond, 0 ) < 0 ) ) {
25 free_generic_stack ( &if_stack );
26 free_generic_stack ( &else_stack );
33 struct command if_command __command = {
38 static int fi_exec ( int argc, char **argv ) {
41 printf ( "Syntax: %s\n", argv[0] );
45 if ( if_stack.tos > 0 ) {
46 if ( pop_generic_stack ( &if_stack, &cond ) < 0 )
49 printf ( "fi without if\n" );
56 struct command fi_command __command = {
61 static int else_exec ( int argc, char **argv ) {
63 printf ( "Syntax: %s\n", argv[0] );
67 if ( TOP_GEN_STACK_INT ( &else_stack ) != 0 ) {
68 printf ( "else without if\n" );
72 if ( ELEMENT_GEN_STACK_INT ( &if_stack, if_stack.tos - 1 ) )
73 TOP_GEN_STACK_INT ( &if_stack ) = !TOP_GEN_STACK_INT ( &if_stack );
78 struct command else_command __command = {
83 void init_if ( void ) {
85 init_generic_stack ( &if_stack, sizeof ( int ) );
86 init_generic_stack ( &else_stack, sizeof ( int ) );
87 push_generic_stack ( &if_stack, &one, 0 );
88 push_generic_stack ( &else_stack, &one, 0 );
92 struct init_fn initialise_if __init_fn ( INIT_NORMAL ) = {
93 .initialise = init_if,