#define MAX_TASK 8
#define MAX_FILE 16
#define MAX_NAME 16
#define MAX_CMD 64
#define MAX_CODE 512
#define FAT_SIZE 512
#define CODE_SIZE 512
#define STATE_STOP 0
#define STATE_RUN 1
#define STATE_WAIT 2 

struct Task {
   short id;
   short run_state;
   short exit_code;
   char name[MAX_NAME];
   char code[MAX_CODE];
};

struct File {
   short is_open;
   char name[MAX_NAME];
   short sector;
};

char fat_table[FAT_SIZE];

struct File * open();
short flenth();
short readfile();
short writefile();
void close();
short spawnl();
void reboot();
short print();
short gets();
short memset();
void init();
void fin();

typedef short (*pf_t)();

void
main()
{
   char cmd[MAX_CMD];

   init();

   print("Wellcome to PCOS...\r\n\r\n");
   print("Programmer: Matthew W. Coan\r\n");
   print("Email: matthewcoan1976@hotmail.com\n");
   print("Web: http://matthewcoan.com/\r\n\r\n");

   print("PCOS> ");
   memset(cmd,0,MAX_CMD);

   while(gets(cmd,MAX_CMD-1) < MAX_CMD) {
      spawnl(cmd);
      print("PCOS> ");
      memset(cmd,0,MAX_CMD);
   }

   fin();

   print("sutdown...\n");
   shutdown();
}


short 
print(message)
char * message;
{
   return 0;
}

void
init()
{
}

void 
fin()
{
}

void
shutdown()
{
}

short
gets(buffer,size)
char * buffer;
short size;
{
   short rc;
   rc = 0;
   return rc;
}

short
atoi(ptr)
char * ptr;
{
   short ret;
   ret = 0;
   return ret;
}

short
strcpy(ptr1,ptr2)
char * ptr1;
char * ptr2;
{
    return 0;
}

char * 
strstr(buffer,key)
char * buffer;
char * key;
{
   char * ptr;
   ptr = 0;
   return ptr;
}

short
memset(buffer,ch,size)
char * buffer;
short ch;
short size;
{
   short ret;
   ret = 0;
   return ret;
}

short
readfile(fp,buffer,size)
struct File * fp;
char * buffer;
short size;
{
   short rc;
   rc = 0;
   return rc;
}

short 
writefile(fp,buffer,size)
struct File * fp;
char * buffer;
short size;
{
    return 0;
}

short
spawnl(name)
char * name;
{
   struct File fp;
   short rc;
   char code[CODE_SIZE];
   pf_t pf;

   rc = 0;

   open(name,&fp);

   readfile(fp,code,CODE_SIZE);

   close(fp);

   pf = (pf_t)code;

   rc = pf();

   return rc;   
}

short
strlen(ptr)
char * ptr;
{
   return 0;
}

short
open(name,fp)
char * name;
struct File * fp;
{
   short i,j;
   char * ptr, ptr0;
   char buffer[10];
   fp->is_open = 1;
   strcpy(fp->name, name);
   ptr = strstr(fat_table, name);
   ptr = ptr + strlen(name) + 1;
   ptr0 = strstr(ptr,"-");
   memset(buffer, 0, 10);
   while(ptr != ptr0) {
      buffer[j] = *ptr;
      ptr = ptr + 1;
      j = j + 1;
   }
   fp->sector = atoi(buffer);
   return 0;
}

void close(fp) 
struct File * fp;
{
   fp->is_open = 0;
}
