/**************************************************************************/
/*                                                                        */
/*                                 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_rset.c,v 1.2 1997/09/25 17:21:58 ae59 Exp $ */
/*
$Log: f_rset.c,v $
 * Revision 1.2  1997/09/25  17:21:58  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:22:35  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : f_rset.c                              */
/*                                                              */
/*      Entry           : void f_rset(desc,name,device)         */
/*                        f_text *desc;                         */
/*                        a_char *name;                         */
/*                        a_char *device;                       */
/*                                                              */
/*      Arguments       : desc   - descriptor of device         */
/*                        name   - PASCAL file variable name    */
/*                        device - name of device               */
/*                          NULL = reuse file descriptor        */
/*                          ""   = standard input (text only)   */
/*                          file = filename used for fopen      */
/*                                                              */
/*      Description     : reset PASCAL device.                  */
/*                                                              */
/*      Date            : 1992-01-16                            */
/*      1992-01-16 : use global o_text instead of constants     */
/*      1991-07-09 : put final '\n' to open output textfile     */
/*      1991-07-09 : prompting requested for program parameters */
/*                   prompting optional for local file variable */
/*      1991-05-16 : repeat prompting for file name             */
/*      1991-04-09 : avoid loops without body                   */
/*      1991-04-08 : argument type of name changed to a_char *  */
/*      1991-02-13 : open for binary access if required         */
/*      1991-02-01 : f_getc() used instead of fgetc()           */
/*      1990-11-29 : (a_char *),(char *) applied                */
/*      1990-11-20 : last argument of type a_char *             */
/*      1990-11-15 : E_TPUSH+E_TPOPP                            */
/*      1990-07-06 : temporary filenames used if f_pptf         */
/*      1990-07-04 : LINT_ARGS prototype                        */
/*                   f_pmti, f_pmto replace stdin, stderr       */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
extern f_text f_pmti;
extern f_text f_pmto;
extern a_bool f_pptf;
extern char *o_text[];
#endif

#ifdef LINT_ARGS
local void f_rset(f_text *desc,a_char *name,a_char *device)
#else
local void f_rset(desc,name,device)

f_text *desc;
a_char *name;
a_char *device;
#endif
        {
        char *p;
        int i,ch;       /* !!! must be int !!! */

        E_TPUSH("f_rset")

        /* put final newline character to textfile if opened for output */
        if (desc->asgd==TRUE && desc->fp!=NULL && desc->text==TRUE &&
            desc->outf==TRUE && desc->err==FALSE && desc->eoln==FALSE)
           f_putc((a_char)'\n',desc);

        if (desc->stdo==FALSE && desc->stdi==FALSE && desc->fp!=NULL)
           fclose(desc->fp);
        desc->fp = NULL;

        desc->infl = TRUE;
        desc->outf = desc->err = FALSE;

        /* reuse descriptor                                     */
        if (device==NULL)
           {

           /* file assigned to descriptor                       */
           if (desc->asgd)
              {
              if (desc->stdo)
                 desc->stdi = TRUE;
              else if (desc->stdi==FALSE)
                 device = (a_char *)&desc->name[0];
              }

           /* no device assigned to descriptor                  */

           /* prompt for filename if program parameter or       */
           /* requested by runtime flag f_pptf                  */
           else if (desc->pp || f_pptf)
              {
new_file_name:
              fprintf(f_pmto.fp,"(PASCAL file variable %s) %s",
                      (char *)name,o_text[9]);
              device = (a_char *)&desc->name[0];

              /* length of input must be less than f_fnsz       */
              for (i=1;i<f_fnsz;i++)
                 if ((ch = fgetc(f_pmti.fp))=='\n') break;
                 else *device++ = ch;
              *device = '\0';

              desc->asgd = TRUE;
              device = (a_char *)&desc->name[0];

              if (i==1)
                 {
                 if (desc->text==TRUE)
                    {
                    desc->stdi = TRUE;
                    desc->name[0] = '\0';
                    }
                 else
                    {
                    e_trap(NO_ERROR+E_EMSG+E_ECNT,6,E_TMSG,68,E_TMSG,33,
                           E_TSTR+E_TEXT(9),name);
                    goto new_file_name;
                    }
                 }
              else if (i>=f_fnsz)
                 {
                 while ((ch = fgetc(f_pmti.fp))!='\n') { /* empty */ }
                 e_trap(I_O_BUFFER,6,E_TMSG,30,
                        E_TSTR+E_TEXT(9),name,E_TSTR+E_TEXT(8),device);
                 E_TPOPP("f_rset")
                 return;
                 }
              }

           /* using temporary file causes error                 */
           else
              {
              e_trap(I_O_ERROR,4,E_TMSG,38,E_TSTR+E_TEXT(9),name);
              E_TPOPP("f_rset")
              return;
              }
           }

        /* open standard input                          */
        else if (*device=='\0')
           {
           if (desc->temp)
              {
              remove(desc->name);
              desc->temp = FALSE;
              }

           if (desc->text==TRUE)
              {
              desc->stdi = TRUE;
              desc->name[0] = '\0';
              }
           else
              {
              e_trap(I_O_ERROR,4,E_TMSG,33,E_TSTR+E_TEXT(9),name);
              E_TPOPP("f_rset")
              return;
              }
           }

        /* use filename                                 */
        else
           {
           if (desc->temp)
              {
              remove(desc->name);
              desc->temp = FALSE;
              }

           p = &desc->name[0];
           for (i=1;(*p++ = device[i-1])!='\0' && i<f_fnsz;i++)
              { /* empty */ }
           *p = '\0';

           desc->stdi = FALSE;

           if (i>=f_fnsz)
              {
              e_trap(I_O_BUFFER,6,E_TMSG,30,
                     E_TSTR+E_TEXT(9),name,E_TSTR+E_TEXT(8),device);
              E_TPOPP("f_rset")
              return;
              }
           }

        desc->eof = desc->eoln = desc->stdo = FALSE;
        desc->asgd = TRUE;

        if (desc->stdi)
           {
           desc->fp = stdin;
           desc->eoln = TRUE;
           desc->win.ch[0] = ' ';
           }
        else if ((desc->fp =
                  fopen((char *)device,((desc->text) ? "r" : "rb")))==NULL)
           {
           e_trap(I_O_ERROR,6,E_TMSG,31,
                  E_TSTR+E_TEXT(9),name,E_TSTR+E_TEXT(8),device);
           desc->err = TRUE;
           }
        else
           f_getc(desc);

        E_TPOPP("f_rset")
        return;
        }
