/**************************************************************************/
/*                                                                        */
/*                                 Sources                                */
/*                                from  the                               */
/*                           XSC SOFTWARE PACKAGE                         */
/*                                                                        */
/*                Copyright (c) 1997 University of Karlsruhe              */
/*                     Institute for Appl. Mathematics                    */
/*                                                                        */
/* This program is free software for non-commercial use. For details, see */
/* the file LICENSE.XSC, distributed with this software package.          */
/*                                                                        */
/* This program is distributed WITHOUT ANY WARRANTY. For details, see the */
/* file LICENSE.XSC, distributed with this software package.              */
/*                                                                        */
/**************************************************************************/

/* RCS $Id: f_popt.c,v 1.2 1997/09/25 17:19:48 ae59 Exp $ */
/*
$Log: f_popt.c,v $
 * Revision 1.2  1997/09/25  17:19:48  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:22:28  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : f_popt.c                              */
/*                                                              */
/*      Entry           : void f_popt(d)                        */
/*                        s_trng *d;                            */
/*                                                              */
/*      Arguments       : d - string for command line argument  */
/*                                                              */
/*      Description     : Copy command line argument string to  */
/*                        string variable.                      */
/*                        Only those arguments are returned     */
/*                        that are not processed by PASCAL-xsc. */
/*                                                              */
/*      Date            : 1993-09-16                            */
/*      1993-09-16 : cast to char* in malloc =b=                */
/*      1991-12-17 : handle empty string correctly              */
/*      1991-12-03 : do not return file name                    */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
extern int f_argc;
extern char **f_argv;
#endif

#ifdef LINT_ARGS
local void f_popt(s_trng *d)
#else
local void f_popt(d)

s_trng *d;
#endif
        {
        size_t arglen;
        static int index = 1;
 
        E_TPUSH("f_popt")

        if (index>=f_argc)
           {
           d->clen = 0;
      
           E_TPOPP("f_popt")
           return; 
           }

        /* argument is an empty string */
        if ((arglen = strlen(f_argv[index]))==0)
           {
           d->clen = 0;
           }

        /* destination is smaller than source */
        else if (d->alen<arglen || d->suba)
           {

           /* destination is dynamic */
           if (d->fix==FALSE)
              {
              s_free(d);
              d->alen = d->clen = arglen;

              /* one more allocated for direct use of '\0'   */
              if ((d->ptr = (char*) malloc(arglen+1))==NULL)
                 e_trap(ALLOCATION,2,E_TMSG,54);
              else
                 {
#ifdef HEAP_CHECK
b_geth((a_char *)&d->ptr,(a_char *)d->ptr,(a_char *)"s_asgn");
#endif
                 strcpy(d->ptr,f_argv[index]);
                 if (d->suba)
                    {
                    d->suba = FALSE;
                    d->tmp = TRUE;
                    }
                 }
              }

           /* destination is not dynamic == chopping */
           else
              {
              memcpy(d->ptr,f_argv[index],d->alen);
              d->clen = d->alen;
              }
           }

        /* destination large enough to hold source */
        else
           {
           d->clen = arglen;
           strcpy(d->ptr,f_argv[index]);
           }

        index++;

        E_TPOPP("f_popt")
        return;
        }
