/* CliUMain.C : modification de UMain.C fonction _main()        */
/* Utilisation exclusive des programmes linkés avec CliUMain.O  */
/* sous CLI, pas de fonctionnement avec le WorkBench            */
/* Remplace "_main.c"                                           */

#include <stdio.h>
#include <fcntl.h>
#include <ios1.h>
#include <string.h>
#include <stdlib.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <proto/dos.h>
#include <proto/exec.h>

#define MAXARG 32              /* maximum command line arguments */
#define QUOTE  '"'

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

#ifndef TINY
extern int _fmode,_iomode;
extern int (*_ONBREAK)();
extern int CXBRK();
#endif

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

/**
*
* 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(line)
register char *line;
{
 register char **pargv;
 register int x;


/*
 *
 * Build argument pointer list
 *
 */
 while (argc < MAXARG)
       {
        while(isspace(*line)) line++;

        if(*line == '\0')     break;

        pargv = &argv[argc++];

        if(*line == QUOTE)
            {
             *pargv = ++line;  /* ptr inside quoted string */

             while ((*line) && (*line != QUOTE)) line++;

             if (*line == '\0') _exit(1);
             else               *line++ = 0;  /* terminate arg */

            }
        else{           /* non-quoted arg */
             *pargv = line;

             while ((*line) && (!isspace(*line))) line++;

             if (*line == 0) break;
             else            *line++ = 0;  /* terminate arg */
            }
        }  /* while */

 targv = (char **)&argv[0];

/*
 *
 * Open standard files
 *
 */
#ifndef TINY

 if(argc == 0) exit(0); /* running under workbench      */
 else{                  /* running under CLI            */
      _ufbs[0].ufbfh = Input();
      _ufbs[1].ufbfh = Output();
      _ufbs[2].ufbfh = Output(); /* 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;
*/
 _ufbs[2].ufbflg = _ufbs[0].ufbflg;

 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;
*/
 stderr->_file = stdin->_file;
 stderr->_flag = stdin->_flag;

/*      establish control-c handler */

 _ONBREAK = CXBRK;

#endif

/*
 *
 * Call user's main program
 *
 */

 main(argc,targv);              /* call main function */

 #ifndef TINY
 exit(0);
 #else
 _exit(0);
 #endif
}
