/*      _main.c         Copyright (C) 1985  Lattice, Inc.       */

/*                      Butchered by DPVC to remove unwanted    *
 *                      I/O stuff - for small utility programs  *
 *                      that do their simple I/O through the    *
 *                      AmigaDOS Output() and Write() commands  */

#include <exec/types.h>

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

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

int argc;                       /* arg count */
char *targv, *argv[MAXARG];     /* arg pointers */


/*
 *  _main()
 *
 *  c.o passes us the command line as a single character string.
 *  While there is still room in the argument array,
 *    Skip any blanks and quit if we're at the end of the line.
 *    Get a pointer to the current arguement string location.
 *    If we have a quoted string,
 *      Skip the quote, and save the start of the string.
 *      Look for the closeing quote or the end of the line.
 *      If it's the end of the line, exit with error, otherwise erase the quote.
 *    Otherwise,
 *      Save the start of the string and find the first space in the string
 *      If we're at the end of the line, quit,
 *        Otherwise, end the parameter at the space and go on to the next one.
 *  If there were no arguments passed, set argv to NULL, otherwise point it
 *    to the first argv pointer.
 *  Do the main C routine.
 *  Exit with no error.
 */

void _main(line)
register char *line;
{
   register char **pargv;

   while (argc < MAXARG)
   {
      while (isspace(*line)) line++;
      if (*line == '\0') break;
      pargv = &argv[argc++];
      if (*line == QUOTE)
      {
         *pargv = ++line;
         while ((*line != '\0') && (*line != QUOTE)) line++;
         if (*line == '\0') _exit(1); else *line++ = '\0';
      } else {
         *pargv = line;
         while ((*line != '\0') && (!isspace(*line))) line++;
         if (*line == '\0')  break; else *line++ = '\0';
      }
   }
   targv = (argc == 0) ? NULL : (char *)&argv[0];

   main(argc,targv);

   _exit(0);
}
