/*
 * memory/startup.c - __memory_startup(), __memory_shutdown()

    This file is part of libRILc, a standard C library for GCC on Amiga OS.
    Copyright © 1998  Rask Ingemann Lambertsen

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    As a special exception, if you link this library with files compiled
    with a GNU compiler to produce an executable, this does not cause the
    resulting executable to be covered by the GNU General Public License.
    This exception does not however invalidate any other reasons why the
    executable file might be covered by the GNU General Public License.
*/

#include <exec/types.h>
#include <exec/lists.h>
#include <exec/memory.h>
#include <stdio.h>
#include "memory/globals.h"
#include "stdio/globals.h"
#include "stdio/startup.h"

#ifdef __PPC__
#include <powerup/gcclib/powerup_protos.h>
#define NewList(a) PPCNewList(a)
#else
#include <proto/exec.h>
#endif

#ifdef __PPC__
#include <ppcproto/dos.h>
#else
#include <proto/dos.h>
#endif

/* Globals for file tracking. */
struct MinList __file_list;
static BOOL close_stderr_on_exit;

FILE __stdio[3];    /* File pointers for stdin, stdout and stderr. */
BPTR __stdio_fh[3]; /* File handles  for stdin, stdout and stderr. */

/* Public stdio globals. */
FILE *stdin = &__stdio[0], *stdout = &__stdio[1], *stderr = &__stdio[2];

BOOL __stdio_startup (BPTR std_in, BPTR std_out, BPTR std_err)
{
    /* Find a file handle for stderr. */
    if (! std_err)
    {
      std_err = Open ("CONSOLE:", MODE_OLDFILE);
      close_stderr_on_exit = TRUE;
    }
    if (! std_err)
    {
      std_err = std_out;
      close_stderr_on_exit = FALSE;
    }

    /* Save the file handles for later (std(in|out|err) might change). */
    __stdio_fh[0] = std_in; __stdio_fh[1] = std_out; __stdio_fh[2] = std_err;

    /* Maybe this will get rid of the spurious linefeed on stdin? */
    /* (well, it looks like it does. Weird. 
     * My guess is a ReadArgs()/UnGetC() bug.) */
    Flush (std_in);

    /* Initialise stdin, stdout and stderr. */
    /* FIXME Changing the buffering causes problems with NIL:
     * when the program exits. */
    if (IsInteractive (std_in))
      __fopen (stdin, std_in, -1, 0, BUF_NONE /* BUFSIZ, BUF_LINE */);
    else
      __fopen (stdin, std_in, -1, 0, BUF_NONE /* BUFSIZ, BUF_FULL */);

    if (IsInteractive (std_out))
      __fopen (stdout, std_out, -1, 0, BUF_NONE /* BUFSIZ, BUF_LINE */);
    else
      __fopen (stdout, std_out, -1, 0, BUF_NONE /* BUFSIZ, BUF_FULL */);

    if (std_out != std_err)
    {
      if (IsInteractive (std_err))
        __fopen (stderr, std_err, -1, 0, BUF_NONE /* BUFSIZ, BUF_LINE */);
      else
        __fopen (stderr, std_err, -1, 0, BUF_NONE /* BUFSIZ, BUF_FULL */);
    }
    else
      __fopen (stderr, std_err, -1, 0, BUF_NONE);

    /* Prevent the inherited file handles from being closed. */
    stdin->closable = stdout->closable = stderr->closable = 0;

    /* Prevent stdin/stdou/stderr from being freed. */
    stdin->freeable = stdout->freeable = stderr->freeable = 0;

    NewList ((struct List *) &__file_list);

    return (TRUE);
}

VOID __stdio_shutdown (VOID)
{
    struct MinNode *node, *next;

    /* FPuts (stderr->fh, "Closing files...\n"); */
    for (node = __file_list.mlh_Head; (next = node->mln_Succ); node = next)
    {
      /* FIXME Insert lots of warning requesters here.
      ULONG args[2];
      args[0] = (ULONG) node;
      args[1] = ((FILE *) node)->fh;
      VFPrintf (stderr->fh, "Program didn't close file 0x%lx (file handle 0x%lx)!\n", args);
      */
      /* Close files here. */
      fclose ((FILE *) node);
    }

    /* Return stdin/stdout/stderr buffering to a normal (?) state. */
#if 0
    SetVBuf (__stdio_fh[0], NULL, BUF_NONE, -1);
    SetVBuf (__stdio_fh[1], NULL, BUF_NONE, -1);
    if (__stdio_fh[1] != __stdio_fh[2])
      SetVBuf (__stdio_fh[2], NULL, BUF_NONE, -1);
#endif

    /* If a file handle was opened for stderr, close it. */
    if (close_stderr_on_exit)
      Close (__stdio_fh[2]);
}
