




extern void k_print();
extern short k_getch();
extern short k_putc();
extern void k_fgets();
extern short k_strlen();
extern char * k_strstr();
extern short k_strcmp();
extern short k_printf();
extern void readcmd();
extern void search();
extern void load();
extern void findsect();
extern short k_get_fat();
extern short is_first();
extern void inc_charcount();
extern void reset_charcount();
extern short load_file_table();
extern short find_sector();
extern short k_exec();


extern short sys_open();
extern void sys_read();
extern void sys_write();
extern void sys_close();
extern char * sys_malloc();
extern void sys_free();
extern short sys_exec();
extern void sys_exit();
extern short sys_getpid();



void
k_printf(fmt)
char * p;
{
   short * arg;
   char * sval;
   short ival;

   arg = &p + 2;

   while(*p) {
      if(*p == '%') {
         p++;
         if(*p == 's') {
            sval = *arg;
            k_print(sval);
            arg += 2;
            p++;
         }
         else if(*p == 'd') {
            ival = *arg;
            arg += 2;        
            p++;
         }
         else if(*p == '%') {
            k_putc('%');
            p++;
         }
      }
      else {
         k_putc(*p);
         p++;
      }
   }
}

short
k_atoi(str)
char * str ;
{
   short ret;
   short i;
   short n;
   short off;
   short num[6];

   num[4] = 10000;
   num[3] = 1000;
   num[2] = 100;
   num[1] = 10;
   num[0] = 1;

   ret = 0;
   n = k_strlen(str);

   off = n - 1;

   for(i = 0; i < n; i++) {
      switch(str[i]) {
      case '0':
      break;
      case '1':
         ret = ret + 1 * num[off-i];
      break;
      case '2':
         ret = ret + 2 * num[off-i];
      break;
      case '3':
         ret = ret + 3 * num[off-i];
      break;
      case '4':
         ret = ret + 4 * num[off-i];
      break;
      case '5':
         ret = ret + 5 * num[off-i];
      break;
      case '6':
         ret = ret + 6 * num[off-i];
      break;
      case '7':
         ret = ret + 7 * num[off-i];
      break;
      case '8':
         ret = ret + 8 * num[off-i];
      break;
      case '9':
         ret = ret + 9 * num[off-i];
      break;
      }
   }
   return ret;
}

short
k_strcpy(buffer,ptr)
char * buffer;
char * ptr;
{
   short i;
   for(i = 0; ptr[i] != '\0'; i++) {
      buffer[i] = ptr[i];
   }
   buffer[i] = ptr[i];
   return i;
}

void
k_bzero(ptr,len)
char * ptr;
short len;
{
   short i;
   for(i = 0; i < len; i++) {
       ptr[i] = '\0';
   }
}

char *
k_strstr(buf,str)
char * buf;
char * str;
{
   char * ptr;
   short i,j;
   short count;

   count = 0;
   ptr = 0;

   for(i = 0; i < k_strlen(buf); i++) {
      count = 0;
      for(j = 0; j < k_strlen(str) && (i+j) < k_strlen(buf); j++) {
         if(buf[i+j] == str[j]) {
            count++;
         }
         else {
            break;
         }
      }
      if(count == k_strlen(str)) {
         ptr = buf + i;
         break;
      }
   }

   return ptr;
}

short
sys_exec(program)
char * program;
{
    short size;
    char * ptr;
    char * name;
    char * sector_str;
    char buffer[2000];
    short isector;
    short n;
    short temp;

    n = 25;

    ptr = 0;

    k_bzero(buffer, 2000);

    size = load_file_table(&buffer[0]);

    name = ptr = k_strstr(buffer, program);

    if(ptr != 0) {
       while(*ptr != ',') {
          ptr++;
       }
       *ptr = '\0';

       ptr = name;
       while(*ptr != '-') {
          ptr++;
       }
       ptr++;
       sector_str = ptr; 

k_printf("program=%s\r\n", program);
k_printf("sector_str=%s\r\n", sector_str);

       isector = k_atoi(sector_str);

       temp = 0;

       k_exec(isector, n, temp);

k_print("program was run...\r\n");
    }
    else {
       k_print("program not found...");
       k_print("\r\n");
    }

    return 0;
}


void 
k_fgets(buf, size)
char * buf;
short size;
{
   short ch;
   char * p;
   short count;

   p = buf;

   count = 0;

   ch = k_getch();

   while(ch != '\r' && ch != '\n') {
      if(ch == '\b') {
         ch = k_getch();

         if(count > 0) {
            k_putc('\b');
            k_putc(' ');
            k_putc('\b');
 
            p--;

            *p = '\0';

            count--;
         }

         continue;
      }

      k_putc(ch);

      *p = ch;

      p++;

      count++;

      if(count >= size-1) 
         break;

      ch = k_getch();
   } 

   *p = '\0';
}

void
k_init()
{
}

void
k_puts(msg)
char * msg;
{
   char * p;
   for(p = msg; *p; p++) {
      k_putc((short)*p);
   } 
}

short
k_strlen(str)
char * str;
{
   short len;
   short i;

   len = 0;

   for(i = 0; str[i] != '\0'; i++) {
      len++;
   }

   return len;
}

short 
k_strcmp(arg1,arg2)
char * arg1;
char * arg2;
{
   short ret;
   short i;
   ret = 0;
   for(i = 0; arg1[i] && arg2[i]; i++) {
      if(arg1[i] == arg2[i]) {
      }
      else if(arg1[i] < arg2[i]) {
         ret = -1;
         break;
      }
      else if(arg1[i] > arg2[i]) {
         ret = 1;
         break;
      }
   }
   return ret;
}


short
main()
{
   char query[30];
  
   if(is_first()) {
      k_print(">>>PCOS: Version: 1.0.0a\r\n");
      k_print("PC Operating System...\r\n");
      k_print("Author: Matthew W. Coan\r\n");
      k_print("EMail: mcoan@slack.net\r\n");
      k_print("\r\n");
      k_init();
   }

   k_bzero(query, 30);

   while(1) {
      k_print("PCOS> ");

      k_fgets(query, 30);

      k_print("\r\n");

      if(k_strlen(query) != 0)
         sys_exec(query);

      k_bzero(query, 30);
   }

   k_print("system shutdown...\r\n");

   return 0;
}


