/**************************************************************************/
/*                                                                        */
/*                                 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_rstn.c,v 1.2 1997/09/25 17:22:06 ae59 Exp $ */
/*
$Log: f_rstn.c,v $
 * Revision 1.2  1997/09/25  17:22:06  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:22:36  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : f_rstn.c                              */
/*                                                              */
/*      Entry           : void f_rstn(desc,spec)                */
/*                        f_text *desc;                         */
/*                        a_intg spec;                          */
/*                                                              */
/*      Arguments       : desc   - descriptor of device         */
/*                        spec   - specification                */
/*                           0 = stdin                          */
/*                           1 = stdout                         */
/*                           2 = stderr                         */
/*                           3 = stdcon                         */
/*                           4 = stdprn                         */
/*                           5 = stdrdr                         */
/*                           6 = stdpun                         */
/*                           8 = stdtmp                         */
/*                           9 = stdorg                         */
/*                                                              */
/*      Description     : reset PASCAL device according to      */
/*                        specification                         */
/*                                                              */
/*      Date            : 1992-07-21                            */
/*      1991-07-21 : SUN4_cpp_C added =b=                       */
/*      1992-07-03 : added rs600 switch =b=                     */
/*      1991-12-12 : HP_9000_C,SUN4_OS4_C added                 */
/*      1991-07-09 : put final '\n' to open output textfile     */
/*      1991-02-13 : open for binary access if required         */
/*      1990-07-23 : option 9 introduced                        */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
#endif

#ifdef LINT_ARGS
local void f_rstn(f_text *desc,a_intg spec)
#else
local void f_rstn(desc,spec)

f_text *desc;
a_intg spec;
#endif
        {
        E_TPUSH("f_rstn")

        /* 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;

        switch((int)spec)
           {
           case 0 :     /* assign stdin to TEXT file            */

                        if (desc->text==FALSE)
                           {
                           e_trap(I_O_ERROR,2,E_TMSG,33);
                           E_TPOPP("f_rstn")
                           return;
                           }

                        if (desc->asgd && desc->temp)
                           {
                           remove(desc->name);
                           desc->temp = FALSE;
                           }
                        desc->stdi = TRUE;
                        break;

#if IBM_RT_C+IBM_RS6000_C+IBM_PS2_C+SUN4_OS4_C+SUN4_CPP_C+HP_9000_C
           case 3 :     /* assign TTYIN    to TEXT file         */

                        if (desc->text==FALSE)
                           {
                           e_trap(I_O_ERROR,2,E_TMSG,33);
                           E_TPOPP("f_rstn")
                           return;
                           }

                        if (desc->asgd && desc->temp)
                           {
                           remove(desc->name);
                           desc->temp = FALSE;
                           }

                        /* get path name of controlling terminal*/
                        /* length of path name is defined by    */
                        /* L_ctermid in <stdio.h>.              */
                        if ((desc->fp = fopen(ctermid(desc->name),"r"))==
                            NULL)
                           {
                           e_trap(I_O_ERROR,4,E_TMSG,31,
                                              E_TSTR+E_TEXT(8),desc->name);
                           E_TPOPP("f_rstn")
                           return;
                           }

                        desc->stdi = FALSE;
                        break;
#endif

           case 9 :     /* assign command line association      */

                        if (desc->asgd && desc->temp)
                           {
                           remove(desc->name);
                           desc->temp = FALSE;
                           }

                        if (desc->org==NULL || *desc->org=='\0')
                           {
                           if (desc->text==FALSE)
                              {
                              e_trap(I_O_ERROR,2,E_TMSG,33);
                              E_TPOPP("f_rstn")
                              return;
                              }
                           desc->stdi = TRUE;
                           }
                        else
                           {
                           (void)strcpy(desc->name,desc->org);
                           desc->stdi = FALSE;
                           if ((desc->fp =
                                fopen(desc->org,((desc->text) ? "r" : "rb"))
                               )==NULL)
                              {
                              e_trap(I_O_ERROR,4,E_TMSG,31,
                                     E_TSTR+E_TEXT(8),desc->org);
                              E_TPOPP("f_rstn")
                              return;
                              }
                           }

                        break;

#if ATARI_TURBO_C+IBM_AT_MS_C+IBM_AT_TURBO_C
           case 3 :     /* assign TTYIN    to TEXT file         */
#endif
           case 1 :     /* assign stdout to TEXT file           */
           case 2 :     /* assign stderr to TEXT file           */
           case 4 :     /* assign printer to TEXT file          */
           case 5 :     /* assign reader to file                */
           case 6 :     /* assign puncher to file               */
           case 8 :     /* assign temporary attribute to file   */
           default :    e_trap(I_O_ERROR,6,E_TMSG,43,
                               E_TINT,&spec,E_TSTR+E_TEXT(8),desc->name);
                        E_TPOPP("f_rstn")
                        return;
           }

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

        if (desc->stdi)
           {
           desc->fp = stdin;
           desc->eoln = TRUE;
           desc->win.ch[0] = ' ';
           desc->name[0] = '\0';
           }
        else
           f_getc(desc);

        E_TPOPP("f_rstn")
        return;
        }
