/*
 * c.PascalDT
 * ~~~~~~~~~~
 * Desktop interface to Acorn's ISO Pascal (Release 2)
 * see ReadMe file for notes on how it works
 *
 * by Carol Haynes
 *
 * modified (slightly) by John West, June 1992
 */

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

int w_count=0, e_count=0, s_count=0;

int throwback=0; /* Flag to note if -throwback is command line option */
int logopt   =0; /* Flag to note if -LO is a command line option      */
int firsttime=1; /* flag for checking whether to initialise throwback */
int errorcode=0; /* Provide exit code from PascalDT
                  * values are 0 - no error, 1 - errors, 2 - severe errors
                  */

void throwback_senderror(char *filename,int line_number,
                         char *message,int how_severe)
/* errorcode can't be set here as it won't *
 * be set if the throwback option is off!  */
{
  if (throwback==0) return; /* i.e. only react if throwback is requested */

  if (firsttime==1)
   { os_swi0(0x42587);
     (void *) os_swi6(0x42588,0,0,(int) filename,0,0,0);
     firsttime=0;
   }
  (void *) os_swi6(0x42588,1,0,(int) filename,line_number,
                                how_severe,(int) message);
}

BOOL throwback_ok(void) /* Returns TRUE if throwback is possible */
{                                  
  const x_bit = 1 << 17;
  os_error *err;

  err = os_swi0(0x42587 + x_bit);  /* Check throwback can be done */
  os_swi0(0x42589 + x_bit);        /* Shutdown throwback if started */
  if (err) printf("$$$$ Error from PascalDT - %s $$$$\n\n",err->errmess);
  return (err)? FALSE : TRUE;
}

BOOL log_on(char *arg) /* Returns TRUE if arg is a valid -log flag */
{                               
  const char *s[] = {"-lo","-Lo","-lO","-LO","-log","-Log",
                     "-lOg","-loG","-LOg","-LoG","-lOG","-LOG"};
  int p;
  for (p=0;p<12;p++) if (strcmp(s[p],arg) == 0) return TRUE;
  return FALSE;
}

int main(int argc,char **argv)
{
  int  lnum, i;
  char cmdline[4096], m0[4096], m1[4096], m2[4096]; /* Various buffers */
  FILE *f;

  printf("ISO Pascal Desktop & Throwback Interface\n\n");
/* Moved up so -throwback error message can come in a sensible place */

  strcpy(cmdline,"Pascal"); /* Invoke the real compiler */ 
  for (i=1 ; i<argc ; i++)  /* Build command line */
    {
      if (strcmp(argv[i],"-throwback")==0) throwback = throwback_ok();
      else 
       {
         if (log_on(argv[i])) logopt=1; /* I've deleted the 'else' */
         strcat(cmdline," ");
         strcat(cmdline,argv[i]);
       }
    }
                       
   strcat(cmdline," { >> <Wimp$ScrapDir>.PascalDTMP }");/* Append to scrap file */
   printf("%s\n\n",cmdline);

/* Start scrap file with an empty line */

   f = fopen("<Wimp$ScrapDir>.PascalDTMP","w");
   fputc(0x0a,f);  /* compiler lines have LF, CR at the end */
   fputc(0x0d,f);
   fclose(f);

   system(cmdline);                  /* run compiler */
   
   f = fopen("<Wimp$ScrapDir>.PascalDTMP","r");

   while (!feof(f))
     {
      fgets(m2,4096,f);
      printf("%s",m2); /* display text in window */
 
      switch (m2[1])   /* m2[0] = CR */
        { 
         case '*' : 
          switch (m2[2]) 
                      /* Adjust errorcode in this switch */
           { case 'E' :if (m1[6]!=' ') sscanf(m1,"%d",&lnum);
                       else sscanf(m0,"%d",&lnum);
                       throwback_senderror(argv[1],lnum,m2+8,1);
                       e_count++;
                       if (errorcode<1) errorcode=1;
                       break;
             case 'F' :if (m1[6]!=' ') sscanf(m1,"%d",&lnum);
                       else sscanf(m0,"%d",&lnum);
                       throwback_senderror(argv[1],lnum,m2+14,2);
                       s_count++;
                       errorcode=2;
                       break;
             default  :break;
           }
           break;
         case 'W' : if (m1[6]!=' ') sscanf(m1,"%d",&lnum);
                    else sscanf(m0,"%d",&lnum);
                    throwback_senderror(argv[1],lnum,m2+9,0);
                    w_count++;
                    break;
         default  : break;
        }
        strcpy(m0,m1);
        strcpy(m1,m2);
     }
   fclose(f);

   remove("<Wimp$ScrapDir>.PascalDTMP");

  if (firsttime==0) os_swi0(0x42589); /* Shutdown throwback if started */

  if (logopt==1)
   {
     printf("\nCompilation Log:\n~~~~~~~~~~~~~~~~\n");
     printf("%3d Warning(s)\n%3d Error(s)\n%3d Fatal Error(s)\n\n",
              w_count,e_count,s_count); /* extra \n to space from linker O/P */
   }

  if ( (f=fopen(argv[argc-1],"r"))!=NULL )
    {
      fclose(f);
      strcpy(cmdline,"Stamp ");
      strcat(cmdline,argv[argc-1]);
      system(cmdline);
    }
  return errorcode;
}
