/*      _main.c         Copyright (C) 1985  Lattice, Inc.
*       Hacked by MLO, for PF2.c
*/

#define OUT_WINDOW_MAX_HT   150         /* Maximum allowed window height */

#include <stdio.h>                      /* Standard library */
#include <fcntl.h>
#include <ios1.h>
#include <stdlib.h>
#include <exec/types.h>                 /* Amiga specific */
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <workbench/startup.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <devices/printer.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/dos_protos.h>
#include "pf2.h"                        /* Program specific */

#define MAXARG 32                       /* maximum command line arguments */
#define QUOTE  '"'
#define ESCAPE '*'
#define NL '\n'

#define isspace(c)            ((c == ' ') || (c == '\t') || (c == '\n'))

extern int _fmode;
extern int (*_ONBREAK)();
extern UWORD ScreenFontHeight;

int CXBRK(void);

extern struct UFB _ufbs[];
static int argc = 0;                    /* arg count */
static char **targv, *argv[MAXARG];     /* arg pointers */

#define MAXWINDOW 40
extern struct WBStartup *WBenchMsg;
extern struct IntuitionBase *IntuitionBase;

/*
*
* name         _main - process command line, open files, and call "main"
*
* synopsis     _main(line);
*              char *line;     ptr to command line that caused execution
*
* description   This function performs the standard pre-processing for
*               the main module of a C program.  It accepts a command
*               line of the form
*
*                       pgmname arg1 arg2 ...
*
*               and builds a list of pointers to each argument.  The first
*               pointer is to the program name.  For some environments, the
*               standard I/O files are also opened, using file names that
*               were set up by the OS interface module XCMAIN.
*
*/

void _main(
  register char *line
){
  register char **pargv;
  register int x;
  struct Process *process;
  struct FileHandle *handle;
  static char window[MAXWINDOW+18];
  char *argbuf;

/*
* Build argument pointer list
*/

  while (argc < MAXARG) {
    while (isspace(*line))      line++;
    if (*line == '\0')          break;
    pargv = &argv[argc++];
    if (*line == QUOTE) {
      argbuf = *pargv = ++line;       /* ptr inside quoted string */
      while (*line != QUOTE) {
        if (*line == ESCAPE) {
          line++;
          switch (*line) {
            case 'E':
              *argbuf++ = ESC;
              break;
            case 'N':
              *argbuf++ = NL;
              break;
            default:
              *argbuf++ = *line;
          }
          line++;
        } else {
          *argbuf++ = *line++;
        }
      }
      line++;
      *argbuf++ = '\0';               /* terminate arg */
    } else {
      *pargv = line;                  /* non-quoted arg */
      while ((*line != '\0')   &&   (!isspace(*line)))      line++;
      if (*line == '\0')  break;
         else *line++ = '\0';         /* terminate arg */
    }
  }  /* while */
  targv = (argc == 0) ? (char **) WBenchMsg : (char **) &argv[0];


/*
* Open standard files
*/

  if (argc == 0) {                    /* running under workbench */
    long lock;
    int sWidth, sHeight;

/*
* Open intuition library
*/

    IntuitionBase = LibOpen("intuition.library", REVISION, LO_ABORT);
    lock = LockIBase(0);
    sWidth = IntuitionBase->ActiveScreen->Width;
    sHeight = IntuitionBase->ActiveScreen->Height;
    ScreenFontHeight = IntuitionBase->ActiveScreen->Font->ta_YSize;
    UnlockIBase(lock);

    if (sHeight > OUT_WINDOW_MAX_HT)  sHeight = OUT_WINDOW_MAX_HT;
    sprintf(window, "Con:0/0/%d/%d/\"PF2\" - v%s - MLO %s ",
            sWidth, sHeight, VERSION, LAST_CHANGE);

    _ufbs[0].ufbfh = Open(window,MODE_NEWFILE);
    _ufbs[1].ufbfh = _ufbs[0].ufbfh;
    _ufbs[1].ufbflg = UFB_NC;
    _ufbs[2].ufbfh = _ufbs[0].ufbfh;
    _ufbs[2].ufbflg = UFB_NC;
    handle = (struct FileHandle *) (_ufbs[0].ufbfh << 2);
    process = (struct Process *) FindTask(NULL);
    process->pr_ConsoleTask = (APTR) handle->fh_Type;
    x = 0;
  } else {
    _ufbs[0].ufbfh = Input();         /* running under CLI */
    _ufbs[1].ufbfh = Output();
    _ufbs[2].ufbfh = Open("*", MODE_OLDFILE);
    x = UFB_NC;                       /* do not close CLI defaults */
  }

  _ufbs[0].ufbflg |= UFB_RA | O_RAW | x;
  _ufbs[1].ufbflg |= UFB_WA | O_RAW | x;
  _ufbs[2].ufbflg |= UFB_RA | UFB_WA | O_RAW;

  x = (_fmode) ? 0 : _IOXLAT;
  stdin->_file = 0;
  stdin->_flag = _IOREAD | x;
  stdout->_file = 1;
  stdout->_flag = _IOWRT | x;
  stderr->_file = 2;
  stderr->_flag = _IORW | x;

/*
* Establish control-c handler
*/

  _ONBREAK = CXBRK;

/*
* Print program header (if from CLI)
**/

  if (argc) fprintf(stdout, "\n\t\"PF2\" - v%s - MLO %s\n\n",
            VERSION, LAST_CHANGE);
     else   fprintf(stdout, "\n");

/*
* Call user's main program
*/

  main(argc, targv);                  /* call main function */
  exit(0);
}

/*
* Disable SAS Control-C built-in handling.
*/

int CXBRK(void)           { return 0; }
