/*{{{  about this file*/
/*
 * xarg.1     V 1.0 for gcc V1.37
 * 28.04.1990
 * Header for xArg command line passing protocol.
 * #include in the file before main(), and it will do the rest.
 * Warning : main is #defined in this module, so take care if you #define
 * it yourself.
 * Alternatively, you might omit the #define main line and rename your old
 * main() to xarg_main().
 *
 * This program is closely modeled on XARG.C , which was distributed
 * with GEMINI.
 *
 * Bug reports to :
 * Thomas Koenig
 * (UI0T@DKAUNI2.BITNET or UI0T@IBM3090.RZ.UNI-KARLSRUHE.DE)
 * slightly modified for TurboC by M. Schwingen
*/
/*}}}  */

#ifndef _XARG_1
/*{{{  #includes*/
#include <tos.h>
#include <string.h>
#include <stdlib.h>
/*}}}  */

/*{{{  typedef struct xArg*/
typedef struct
{
    char        xarg_magic[4];  /* "xArg" == 0x78417267L         */
    short       xargc;          /* Like argc in main()           */
    char        **xargv;        /* Like argv in main()           */
    char        *xiovector;     /* Unused                        */
    BASPAG      *xparent;       /* Points to basepage of parent, */
                                /* definded in basepage.h        */
} xArg;
/*}}}  */

/*{{{  int main(int argc, char **argv, char *envp)*/
int main(int argc, char **argv, char *envp)
{
  extern BASPAG *_BasPag;
  xArg  *xarg;
  char  *xenv;
  unsigned long x;

  if ((xenv=getenv("xArg"))!=NULL)
  {
    /*{{{  try it*/
    x = strtoul(xenv,NULL,16);
    if ((x!=0) && (x%2==0))
    {
      /*{{{  at least, the xArg address seems to be valid*/
      xarg = (xArg *)x;
      if (!strncmp(xarg->xarg_magic,"xArg",4))
      /*{{{  even the magic number is found*/
      {
        if (xarg->xparent == _BasPag->p_parent)
        /*{{{  parent process is ok - use xArg parameters*/
        {
          /* o.k. Call xarg_main. */
          return xarg_main(xarg->xargc,xarg->xargv,envp);
        }
        /*}}}  */
        else
          ; /* xArg parent != my parent */
      }
      /*}}}  */
      else
        ; /* xArg magic number not found */
      /*}}}  */
    }
    /*}}}  */
    else
      ; /* illegal xArg address (0 or odd) */
  }
  else
    ;  /* xArg not in environment */
  /* Well, we'll have to make do with normal argument passing. */
  return xarg_main(argc,argv,envp);
}

/*}}}  */
#define main xarg_main
#endif
