
#import <objc/Object.h>
#import "Shell.h"
#import "TreeNode.h"
#import "ListNode.h"

#import <stdlib.h>
#import <stdio.h>
#import <strings.h>

#ifndef NULL
#define NULL 0
#endif

#define COMMAND_QUIT 0
#define COMMAND_MOVE 1
#define COMMAND_SWAP 2
#define COMMAND_KILL 3
#define COMMAND_PRNT 4

@implementation Shell : Object

static Shell *instance;

+ new
{
   if ( instance == NULL ) instance = [[self alloc] init];
   return instance;
}

- run
{
/* read a line from stdin */
/* tokenize line */
/* look up first token get int equivalent */
/* find command block */
/* execute command with arguments */
/* repeat */

    BOOL done;
    char *buf;
    char *p;
    int success;

    buf = malloc( sizeof( char ) * 1024 );

    done = NO;
    while ( ! done ) 
    {
       printf(": "); /* prompt */
       fgets( buf, 1023, stdin ); /* warning: memory leaks/corruption */

       p = strtok( buf, " " );
       if ( p != NULL && strlen( p ) > 0 )
       {
          ListNode *list = [ListNode create: p];
          p = strtok( buf, NULL );
          while ( p != NULL )
          {
             [list append: p];
             p = strtok( buf, NULL );
          }
       }
       printf("%s\n", buf);
    }

    return self;
}

@end
