/******************************************************/
/*                                                    */
/*    FILE : AIM.C  version 2.51                      */
/*                                                    */
/*    updated :  5 - 7 - 87                           */
/*              by : R. de Vries                      */
/*                                                    */
/*    updated : 25 - 1 - 87                           */
/*              by : F. Groen                         */
/*                                                    */
/*    updated : 16 - 8 - 87                           */
/*              by : F. Groen                         */
/*                                                    */
/*    updated :  2 - 2 - 88                           */
/*              by : F. Groen                         */
/*              P. Verbeek filters                    */
/*    updated : 2 - 10 - 88                           */
/*              by : R. de Vries                      */
/*              added : error handling                */
/*                                                    */
/******************************************************/

#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <stdlib.h>
#include "errors.h"
#include "window.h"
#include "parse.h"
#include "bblib.h"

#define CTL(c)  (c - 0x40)

  int             contrl[12], intin[128], intout[128], ptsin[128], ptsout[128];
  int             work_out[57];
  int             work_in[12];
  int             end_macro;
  int             xlen, ylen, ngrey;
  char           *LstCmd;
  char *Image;
  char *InpIm;
  char *Inp1Im;
  char *Inp2Im;
  char *Inp3Im;
  char *OutIm;
  char *GenIm;
  char *TmpIm;
  char           *wi_title = "COMMAND WINDOW";
  char  signal_dir[100] ,         
        setup_file[100] ,
        macro_dir[100] ,
        help_file[100]  ,
        log_file[100]   ;
  char *root;      
  char old_disk_name[20];

/* save old palette and restore on exit */ 
  int   greypal[16][3] = {{0,0,0},{16,16,16},{32,32,32},{48,48,48},{64,64,64},
                         {80,80,80},{96,96,96},{112,112,112},{128,128,128},
                         {144,144,144},{160,160,160},{176,176,176},{192,192,192},                                  {208,208,208},{224,224,224},{240,240,240}};
  jmp_buf mainenv;
  int resolution;
  int disp_on = 1;
  int time_on = FALSE,
      logflag = FALSE, 
      aspect = FALSE,
      align = FALSE,
      formflag = TRUE,
      prompt_flag = FALSE;
  BOOL dbox_semaphor=FALSE;
  FILE *flog;
  FILE *fp,*macro[16],*mp;
  char _argbreak = ' ';/* last character for escape sequence  */
  jmp_buf main_loop;
  VENSTER *vp[MAXSIG];
  int i_macro;
  int rep_macro[17];
 
  extern int curhist, /* alloc_ptr,*/alloc_top;

  extern BOOL   trace_on;
  unsigned int  queue_end,
                queuesize; /* vvv queue parameters   */
  short         Cdisp_Type = 0;
  short         gamma = 6; /* 10*gamma of low resolution display monitor */
  short         Con_Perc = 2; /* promille for display */
  short         itab[512];


 void bb_close(void)
  {  
 /*  os_error *SetFileType(char *filename,int filetype); 
 int i;
   Dpg
   for (i = 0; i < MAXIM; i++)
      if (wp[i] != NULL)
         CloseImage( wp[i] );   */


   if (logflag)
    {
      fclose(flog);
 /*     set filetype of just written file 
 Dpg
      osfileblok->action = 18;
      osfileblok->name = log_file;
      osfileblok->loadaddr = MACRO;
      err1 = osfile(osfileblok);

      wimpt_complain(SetFileType(log_file,MACRO));  */
    }
  }


/*-----------------------------------------------------------------------------------------*/

 void wimpstuff_close(void)
  {
/*
   void clean_up(void);
   clean_up();
*/
  }

/*-----------------------------------------------------------------------------------------*/

 void wimpstuff_init(void)
  {
   char filename[100];
/*   FILE *fp;               */
   root=getenv("ASM$dir");
   strncpy(signal_dir,root,80);
   strcat(signal_dir,".signals.");            
   strncpy(setup_file,root,80);
   strcat(setup_file,".resources.");
   strncpy(macro_dir,root,80);
   strcat(macro_dir,".macros.");
   strncpy(log_file,root,80);
   strcat(log_file,".macros.log1.");
   strncpy(help_file,root,80);
   strcat(help_file,".help.");
   strcpy(filename,setup_file);
   strcat(filename,"setup");
/*   
   fp = fopen(filename,"r");
   if (fp == NULL)
   {
      werr(1,"Couldn't load setup file, using default paths.");
      return;
   }
   fgets(signal_dir,100,fp);
   fgets(setup_file,100,fp);
   fgets(macro_dir,100,fp);
   fgets(help_file,100,fp);
   fgets(log_file,100,fp);
   fclose(fp);
   signal_dir[strlen(signal_dir) - 1] = '\0';
   setup_file[strlen(setup_file) - 1] = '\0';
   macro_dir[strlen(macro_dir) - 1] = '\0';
   help_file[strlen(help_file) - 1] = '\0';
   log_file[strlen(log_file) - 1] = '\0';
*/
}

 static void bb_init(void)
  {
/*   int i;
   char text[50];        */

   xlen = 256;
   ylen = 256;
  }

/*-----------------------------------------------------------------------------------------*/

 static void aim_init(void)
 {
  int  i;   /*, x_out, y_out, w_out, h_out, dum; */        

  Image=(char *)heap_alloc(8);
  InpIm=(char *)heap_alloc(40);
  Inp1Im=(char *)heap_alloc(40);
  Inp2Im=(char *)heap_alloc(40);
  Inp3Im=(char *)heap_alloc(40);
  OutIm=(char *)heap_alloc(40);
  GenIm=(char *)heap_alloc(40);
  TmpIm=(char *)heap_alloc(40);
  
    strcpy(Image,"ABCD");     
    strcpy(InpIm,"Input  Image <A,B,C,D> ->");   
    strcpy(Inp1Im,"Input  Image 1 <A,B,C,D> ->");  
    strcpy(Inp2Im,"Input  Image 2 <A,B,C,D> ->");  
    strcpy(Inp3Im,"Input  Image 3 <A,B,C,D> ->");  
    strcpy(OutIm,"Output Image <A,B,C,D> ->");   
    strcpy(GenIm,"Image <A,B,C,D> ->");    
    strcpy(TmpIm,"Temporary Image <A,B,C,D> ->");  
    
    /*    load_setup(setup_file, FALSE); */
    /*
     * initialize command list (alphabetic sort) and load indices from help
     * file
     */
  if ((i=init_cmds()) !=1)
   {
     werr(0, "Unable to open helpfile 'aimhelp'");
     wprintf("init=%d\n",i);            
   }
 }


 static void prompt(void)
 {
 /*    printw((resolution == HIGHRES) ? "ASM%d>" : "%d>", curhist); */
 }

 /*******************************************************************************/

 static void print_error(int error)
 
 {
 static char    *error_messages[] = {
 "",   /* NO_ERROR */
 "Illegal parameter(s).",/* ERR_PARAM */
 "Unable to allocate memory.", /* ERR_ALLOC */
 "Operation cannot work on complex image.", /* ERR_COMPLEX */
 "Input not a complex-signal.", /* ERR_NO_COMPLEX */
 "Inputfile is not an ASM or TCL file.", /*ERR_FILETYPE*/
 "Cannot open file.", /* ERR_OPENFILE */
 "Cannot close file.", /* ERR_CLOSEFILE */
 "Cannot read file.", /* ERR_READFILE */
 "Cannot write file.", /* ERR_WRITEFILE */
 "Cannot access printer.", /* ERR_PRINTER */
 "No printer installed.",/* ERR_NO_PRINTER_INSTALL */
 "Queue too small.", /* ERR_QUEUE */
 "Unknown command.",           /* ERR_UNKNOWN      */
 "Ambiguous command.",         /* ERR_AMBIGUOUS    */
 "Illegal transformation. ", /* ERR_TRANSFORM    */
 "No shell given.",            /* ERR_NOSHELL      */
 "Could not start shell.",     /* ERR_EXEC         */
 "Command aborted.",           /* ERR_ABORT        */
 "Signal not declared.",       /* ERR_DECL         */
 "Too many displaywindows.",   /* ERR_WINDOWS      */
 "Input does not exist.",      /*ERR_SIGNAL        */
 "Wrong domain or domains do not match.",      /* ERR_DOMAIN       */ 
 "No imaginary part present.", /* ERR_NO_IMAG      */
 "No real part present.",      /* ERR_NO_REAL      */
 "Signal too long.",            /* ERR_LONG        */
 "Signals have different size.",/* ERR_SIZE       */
 "Not implemented on complex data.",   /*ERR_IMPL  */   
 "Signal already exists."       /* ERR_EXIST       */
  };                                     

   if (error == 0) return;
/*    emergency_free(0);    */
   if ((error <= ERR_EXIST) && (error > 0))
    {
    if (error != ERR_EXIST)
      printw("Error: %s\n", error_messages[error]);
    else
      printw("%s\n", error_messages[error]);
      return;
    } 
    printw("Unknown error %d\n", error); 
 }  

/*************************************************************************/

 static void interp(void)
 {
    char           *cmd, *nxt, *cmmnt;
    char            s[256];
    int             i,error;
    char            filename[256];


    i_macro = 0;
    setjmp(main_loop); 
/*    if (alloc_ptr >0) emergency_free();   */
    SignalInitialize();     
    visdelay_end();
    pointer_reset_shape();
 /* set jump buffer for ESC action */
    for(i=i_macro;i>=0;i--) 
      {
         mp = macro[i];
         if (mp !=NULL)fclose(mp);
      }
    i_macro=0;
    prompt_flag = FALSE;
    dbox_semaphor=FALSE;
    fp = stdin;
    while (*(cmd = getline2(s, fp)) != CTL('C'))
    {
       /* add command to the history or transform it to real command */
   /*     cmdtimer=timer(); */
        visdelay_begin();
  /*      upd_history(s);    */
        if (trace_on && fp != stdin) wprintf("%s\n",s);

        /* commands from macro-files are not logged only the macro-calls */
        if (logflag && (fp == stdin))fprintf(flog, "%s\n", s);
        LstCmd = s;
        end_macro = FALSE;
/*        ltimer=timer();  */
        switch (*cmd)
             {
              case '@':  /* macro */
                nxt = nxtarg (&cmd,NULL) + 1;
                /* get filename */
                if (strrchr(nxt,'.') == NULL)
                    { /* Only leaf name, create full path name */
                        strcpy(filename,macro_dir);
                        strcat(filename,nxt);
                    }
                else strcpy(filename,nxt); /* Full path name is given */
                if (i_macro < 16)
                    {
                        macro[i_macro++] = fp;
                        if ((fp = fopen(filename, "r")) == NULL)
                           {
                                wprintf("Unable to open command file %s\n", nxt);
                                fp = macro[--i_macro];
                           }
                        else
                           {
                           }

                        rep_macro[i_macro] = intarg(&cmd,NULL,"infinite = -1",-1,0,0);

                    }
                else
                        wprintf("Only 16 macro levels atmitted! OK \n");
              break;

              case '\0':
                if (i_macro > 0)
                   {
                        if (rep_macro[i_macro] == -1)  rewind(fp);
                        else
                           {
                                fclose(fp);

                                end_macro = TRUE;
                                fp = macro[--i_macro];
                           }
                   }
              break;

              case '?':  /* Command list */
                prstab();
              break;

              case '*':  /* Comment  */
              break;

              default:

                /* skip comment ('*--------') from parsing by making it 0 */
                if ((cmmnt = strchr(cmd, '*')) != NULL) *cmmnt = '\0';
                nxt = nxtarg(&cmd, NULL);
                switch (i = stablk(nxt, 0))
                     {
                      case -1:
                        error = ERR_UNKNOWN;  
                      break;
                      case -2:
                        error = ERR_AMBIGUOUS; 
                      break;
                      default:
                        nxt = stpblk(cmd);
                        if (*nxt == '?')
                          if (*stpblk(nxt + 1) == 0) prompt_flag = TRUE;
                        error = cmd_exec(i, cmd);
                        prompt_flag = FALSE;
                      break;
                     }
                print_error(error);
              break;
             }
        if (!end_macro)  prompt();
        visdelay_end();
        dbox_semaphor=FALSE;
    }
}

 int aim_kern()
 {
    SignalInitialize();  
    wimpstuff_init();
    bb_init();
    aim_init();
  /*  initclp(itab);  */
    interp();
    bb_close();  
    wimpstuff_close();  
    return 0;
}

