/*
    Console.h, Copyright (c) by Silvio Dragone & David Reist,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public Licence.
 
    A full copy of the GNU GPL is provided in the file COPYING found in the 
    development root of Topsy.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*****************************************************************************
* This is a quick and dirty SegMap header file generater for the TopsyIXP port 
*/

static void putString(char[], FILE*);

int main(int argc, char *argv[])
{
  char s[2048], ksize[5], w[200];
  FILE *segmap_file, *ksize_file, *usize_file;
  int i = 0, j = 0, k = 0;

  if (argc != 4)
  {
    fprintf(stderr,
            "Usage:\n"
            "\tSegMap_gen <kernel.elf.size> <user.elf.size> <SegMap.h>\n\n");
    return EXIT_FAILURE;
  }

  /* Create files. */
  ksize_file = fopen(argv[1], "r");
  if (ksize_file == NULL)
  {
    perror(argv[1]);
    return EXIT_FAILURE;
  }
  usize_file = fopen(argv[2], "r");
  if (usize_file == NULL)
  {
    perror(argv[2]);
    return EXIT_FAILURE;
  }
  segmap_file = fopen(argv[3], "w+");
  if (segmap_file == NULL)
  {
    perror(argv[3]);
    return EXIT_FAILURE;
  }

  strcpy(&w[0], "/* SegMap.h, Copyright (C) by Silvio Dragone & David Reist */\n");
  w[62] = '\0';
  putString(w, segmap_file);

  strcpy(&w[0], "#define KCSIZE  ");
  j = 16;

  while(fgets(s, sizeof(s), ksize_file))
  {

    if (s[0] == '.' && s[1] == 't')
    {
      i = 5;
      while (s[i] == ' ')
      {
        i++;
      }

      while (s[i] != ' ')
      {
        w[j]=s[i];
        ksize[k]=s[i];
        i++;
        j++;
        k++;
      }

      w[j++] = '\n';

      strcpy(&w[j++], "#define KCSTART ");
      j += 15;

      while (s[i] == ' ')
      {
        i++;
      }

      while (s[i] != '\0')
      {

        w[j]=s[i];
        i++;
        j++;
      }

      w[j] = '\n';
    }

    if (s[0] == '.' && s[1] == 'd')
    {
      strcpy(&w[j], "#define KDSTART ");
      j += 16;

      i = 5;
      while (s[i] == ' ')
      {
        i++;
      }

      while (s[i] != ' ')
      {
        i++;
      }

      while (s[i] == ' ')
      {
        i++;
      }

      while (s[i] != '\0')
      {
        w[j]=s[i];
        i++;
        j++;
      }

      w[j] = '\n';
    }

    if (s[0] == 'T' && s[1] == 'o')
    {
      strcpy(&w[j++], "#define KDSIZE (");
      j += 15;

      i = 5;
      while (s[i] == ' ')
      {
        i++;
      }

      while (s[i] != '\0')
      {
        w[j]=s[i];
        i++;
        j++;
      }
      w[j--] = ' ';
      w[j++] = '-';
      strcpy(&w[j++], (char*) ksize);

      j+=4;

      w[j++] = ')';
      w[j++] = '\n';
    }
  }

  w[j] = '\0';
  putString(w, segmap_file);

  /*------*/

  i=0;
  k=0;

  strcpy(&w[0], "#define UCSIZE  ");
  j = 16;

  while(fgets(s, sizeof(s), usize_file))
  {

    if (s[0] == '.' && s[1] == 't')
    {
      i = 5;
      while (s[i] == ' ')
      {
        i++;
      }

      while (s[i] != ' ')
      {
        w[j]=s[i];
        ksize[k]=s[i];
        i++;
        j++;
        k++;
      }

      w[j++] = '\n';

      strcpy(&w[j++], "#define UCSTART ");
      j += 15;

      while (s[i] == ' ')
      {
        i++;
      }

      while (s[i] != '\0')
      {

        w[j]=s[i];
        i++;
        j++;
      }

      w[j] = '\n';
    }

    if (s[0] == '.' && s[1] == 'd')
    {
      strcpy(&w[j++], "#define UDSTART ");
      j += 15;

      i = 5;
      while (s[i] == ' ')
      {
        i++;
      }

      while (s[i] != ' ')
      {
        i++;
      }

      while (s[i] == ' ')
      {
        i++;
      }

      while (s[i] != '\0')
      {
        w[j]=s[i];
        i++;
        j++;
      }

      w[j] = '\n';
    }

    if (s[0] == 'T' && s[1] == 'o')
    {
      strcpy(&w[j++], "#define UDSIZE (");
      j += 15;

      i = 5;
      while (s[i] == ' ')
      {
        i++;
      }

      while (s[i] != '\0')
      {
        w[j]=s[i];
        i++;
        j++;
      }
      w[j--] = ' ';
      w[j++] = '-';
      strcpy(&w[j++], (char*) ksize);

      j+=4;

      w[j++] = ')';
      w[j++] = '\n';
    }
  }
  w[j] = '\0';
  putString(w, segmap_file);

  strcpy(&w[0], "#define UJUMPADR UCSTART\n");
  w[26] = '\0';
  putString(w, segmap_file);

  fclose(segmap_file);
  fclose(ksize_file);
  fclose(usize_file);

  return 0;
}

void putString(char str[], FILE *fp)
{
  int i = 0;
  while (str[i] != '\0' )
  {
    fputc(str[i], fp);
    i++;
  }
  fputc('\n', fp);
}
