/*
 * headerGen.c
 *
 * Generate Sapphire headers automatically
 *
 *  1994-1998 Straylight
 */

/*----- Licensing note ----------------------------------------------------*
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, write to the Free Software Foundation,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

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

#include "kernel.h"
#include "swis.h"

static void swi(int s,_kernel_swi_regs *r)
{
  _kernel_oserror *e=_kernel_swi(s,r,r);
  if (e)
  {
    fprintf(stderr,"Arrghh -- %s\n",e->errmess);
    exit(1);
  }
}

static char *getline(char **p,char *b)
{
  while (**p!=10)
    *b++=*((*p)++);
  *b=0;
  (*p)++;
  return (b);
}

static void headergen(char *buff,char *infile,char *outfile)
{
  FILE *fp;
  char b[256];
  char *p;
  char *leaf=strrchr(infile,'.');

  if (!leaf)
    leaf=outfile;
  else
    leaf++;

  if (fp=fopen(outfile,"w"),!fp)
  {
    fprintf(stderr,"Couldn't open output\n");
    exit(1);
  }

  /* --- Do the header (this bit's a little fragile) --- */

  getline(&buff,b);
  fprintf(fp,"%s\n",b);

  getline(&buff,b);
  fprintf(fp,"%sh\n",b);

  getline(&buff,b);
  fprintf(fp,"%s\n",b);

  p=getline(&buff,b);
  if (p[-1]==')' && p[-5]=='(')
    p[-6]=0;
  fprintf(fp,"%s\n",b);

  getline(&buff,b);
  fprintf(fp,"%s\n",b);

  getline(&buff,b);
  fprintf(fp,"%s\n",b);

  getline(&buff,b);
  fprintf(fp,"%s\n",b);

  getline(&buff,b);
  fprintf(fp,"%s\n",b);

  fprintf(fp,";----- Licensing note -------------------------------------------------------\n"
             ";\n"
             "; This file is part of Straylight's Sapphire library.\n"
             ";\n"
             "; Sapphire is free software; you can redistribute it and/or modify\n"
             "; it under the terms of the GNU General Public License as published by\n"
             "; the Free Software Foundation; either version 2, or (at your option)\n"
             "; any later version.\n"
             ";\n"
             "; Sapphire is distributed in the hope that it will be useful,\n"
             "; but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
             "; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
             "; GNU General Public License for more details.\n"
             ";\n"
             "; You should have received a copy of the GNU General Public License\n"
             "; along with Sapphire.  If not, write to the Free Software Foundation,\n"
             "; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n"
             "\n"
             ";----- Overview -------------------------------------------------------------\n"
             ";\n"
             "; Functions provided:\n"
             ";\n");

  /* --- Search for all EXPORTs and output them in the overview --- */

  {
    char *bb=buff;
    do
    {
      getline(&bb,b);
      if (!memcmp(b,"\t\tEXPORT",8))
      {
        p=b+8;
        while (!isalnum(*p) && !(*p=='_'))
          p++;
        fprintf(fp,";  ");
        while (isalnum(*p) || *p=='_')
          fputc(*p++,fp);
        fputc('\n',fp);
      }
    }
    while (memcmp(b,"\t\tEND",5));
  }
  fputc('\n',fp);

  fprintf(fp,"\t\t[\t:LNOT::DEF:%s__dfn\n"
             "\t\tGBLL\t%s__dfn\n"
             "\n",
             leaf,leaf);

  /* --- Now output the block comments etc. --- */

  {
    char *bb=buff;
    char *ob=bb;
    char *cm=0;
    char *ocm=(char *)0x80000000;
    do
    {
      getline(&bb,b);
      if (*b==';' && !cm)
      {
        cm=ob;
      }
      else if (*b!=';' && cm)
      {
        ocm=cm;
        cm=0;
      }
      if (!memcmp(b,"\t\tEXPORT",8))
      {
        for (p=ocm;p<ob;p++)
          fputc(*p,fp);
        fprintf(fp,"\t\tIMPORT\t%s\n\n",b+9);
      }
      ob=bb;
    }
    while (memcmp(b,"\t\tEND",5));
  }

  fprintf(fp,"\t\t]\n"
             "\n"
             ";----- That's all, folks ----------------------------------------------------\n"
             "\n"
             "\t\tEND\n");

  fclose(fp);
}

int main(int argc,char *argv[])
{
  if (argc!=3)
  {
    fprintf(stderr,"Syntax: headerGen <input> <output>\n");
    exit(1);
  }

  /* --- Load input file into mondo buffer --- */

  {
    _kernel_swi_regs r;
    char *fb;
    r.r[0]=17;
    r.r[1]=(int)argv[1];
    swi(OS_File,&r);
    if (fb=malloc(r.r[4]),!fb)
    {
      fprintf(stderr,"No memory -- buy some more\n");
      exit(1);
    }
    r.r[0]=16;
    r.r[1]=(int)argv[1];
    r.r[2]=(int)fb;
    r.r[3]=0;
    swi(OS_File,&r);
    headergen(fb,argv[1],argv[2]);
  }
  return (0);
}
