#include "kernel.h"
#include "proc.h"

#include <stdio.h>

/*
 * This is a small source that contains the registered variables
 * and functions. It shows how to perform the registration
*/

//functions registered in the shell
//you need to add here the functions that you want to register
extern	void    test_zero( );
extern	void    test_one( int test_var );

//variables registered in the shell
//this is the parameter to test_one
//you need to add here the variables that you want to register
int	test_var = 1;

//the rest of the functions from this source
void	init_func_table( );
void	init_var_table( );
int		printProcessState( );

//test function with no argument
void    test_zero( ) {
	fprintf( stderr, "Process test_zero starts!\n" );
	fprintf( stderr, "test_zero ends ...\n" );
}

//test function with one argument
void    test_one( int a ) {
	fprintf( stderr, "Process test_one starts with %d!\n", a );
	fprintf( stderr, "test_one rescheduling ...\n" );
	xinu_resched( );

	fprintf( stderr, "test_one ends ...\n" );
}

//initialize the function table by registering all the functions
void    init_func_table( ) {

	function_register( "test_zero", &test_zero );
	function_register( "test_one", &test_one );
}

//initialize the variable table by registering all the needed variables
//for now, only xinu_create accepts variables as last argument
void    init_var_table( ) {
        
	variable_register( "a", &test_var );
}


//help function for printing the current process table and waiting queue
int	printProcessState( )
{
	int i;
	char proc_state[ 50 ];

	printf( "\n####################################################################\n" );
	printf(" \t\t\tPROCESS TABLE" );

	for( i = 0 ; i < NPROC ; i++ )
	{
		if( proctab[ i ].pstate != PRFREE )
		{
			printf( "\n------------------------------------------------\n" );
			printf( "\tProcess Name:   %s\n\n", proctab[ i ].pname );
			switch( proctab[ i ].pstate )
			{
				case PRCURR:
					sprintf( proc_state,"%s","CURRENT" );
					break;
				case PRREADY:
					sprintf( proc_state,"%s","READY" );
					break;
				case PRSUSP:
					sprintf( proc_state,"%s","SUSPENDED" );
					break;
				default:
					sprintf( proc_state,"%s","UNKNOWN" );
					break;
			}

			printf( "\tState\t\t\t%s\n", proc_state );
			printf( "\tPriority\t\t%d\n",proctab[ i ].pprio );
			printf( "\tStack Length\t\t%d\n", proctab[ i ].pstklen );
			printf( "\tESP\t\t\t%d\n", proctab[ i ].pesp );
			printf( "\tTOP STACK\t\t%d\n", proctab[ i ].pbase );
			printf( "\tBOTTOM STACK\t\t%d\n", proctab[ i ].plimit );
		}
	}

	printf( "\n#####################################################################\n\n" );

	printf( "\n#####################################################################\n" );
	printf( "\t\t\tREADY QUEUE" );
	printqueue( rdyhead );
	printf( "\n#####################################################################\n" );

	return( OK );
}

