// -*-C++-*- 

/*  src/platform/POSIXPlatform.cpp  */

/*
 * Author: Philogelos A. <Philogelos@yahoo.com>
 * Maintainer: Philogelos A.
 * Keywords: C++, library, containers
 *
 * Copyright (C) 1998 Philogelos A.
 *
 * This file is part of Quercus Robusta.
 *
 * Quercus Robusta 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, or (at your option)
 * any later version.
 *
 * This software 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 software; see the file COPYING.LIB.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */



/* $Id$ */
#if !defined(_INLINE)
static char cvsid[] = "@(#)$Id$";
static char debugFileId[] = __FILE__;
#endif

#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>

#include "platform/POSIXPlatform.hpp"
#include "String.hpp"
#include "Debug.hpp"

extern "C" int kill(pid_t pid, int sig);

POSIXPlatform::POSIXPlatform()
{
  setImageName( IMAGE_NAME );
}

POSIXPlatform::~POSIXPlatform()
{}

char *POSIXPlatform::getErrorDescription( int errCode )
{
  ++callsNum;
  return strerror( errCode );
}

void POSIXPlatform::exitImmediate( long status )
{
  ++callsNum;
  _exit( status );
}
  
void POSIXPlatform::suicide()
{
  ++callsNum;
  kill( getpid(), SIGABRT );
  while( true )
	{}
}

void POSIXPlatform::attachDebugger()
{
  int result;
  const char *errorMessage;
  boolean MelTheProgrammer;

  ++callsNum;
  errorMessage = "Fork failed in attempt to attach debugger";
  result = fork();
  if( result == 0 )
	{
	  char *pidBuffer;

	  pidBuffer = new char[ ( 3 * sizeof( pid_t ) ) + 1 ];
	  this -> sprintf( pidBuffer, "%i", getppid() );
	  /* XXX how to get to the argv[0]? */
	  result = execlp
		( DEBUGGER_COMMAND, DEBUGGER_COMMAND, 
		  getImageName(), pidBuffer, NULL );
	  delete []pidBuffer;
	  errorMessage = "Exec failed in attempt to attach debugger";
	}
  if( result < 0 )
	{
	  rawErrorPrintf( errorMessage );
	  suicide();
	}
  /* you can set this from debugger to break out of loop */
  MelTheProgrammer = true;
  while( MelTheProgrammer )
	{}
}

void POSIXPlatform::setImageName( const char *aName )
{
  preC_( aName != NULL );
  
  imageName = aName;
}

const char *POSIXPlatform::getImageName() const
{
  return imageName;
}

char *POSIXPlatform::getTimeStampString()
{
  static time_t instant;
  static char buffer[ 30 ];

  if( time( &instant ) == -1 )
	{
	  strcpy( buffer, "UNKNOWN TIME" );
	}
  else
	{
	  static struct tm *t;

	  /* XXX _r version  localtime_r( &instant, &t ); */
	  t = localtime( &instant );
	  sprintf( buffer, "%4.4i/%2.2i/%2.2i %2.2i:%2.2i:%2.2i",
			   t -> tm_year + 1900, t -> tm_mon + 1, 
			   t -> tm_mday, t -> tm_hour, t -> tm_min, 
			   t -> tm_sec );
	}
  return buffer;
}

void *POSIXPlatform::allocateMemoryBootstrapPOSIX( Index aSize )
{
  return allocateMemoryBootstrapANSI( aSize );
}

void  POSIXPlatform::deallocateMemoryBootstrapPOSIX( void *aPointer )
{
  deallocateMemoryBootstrapANSI( aPointer );
}

boolean POSIXPlatform::equals( const Top *anOther ) const
{
  return( DCAST( anOther, POSIXPlatform ) == this );
}

Top *POSIXPlatform::clone() const
{
  return ( Top * ) this;
}

String  POSIXPlatform::toString() const
{
  return Object::toString() + " calls: " + String( callsNum );
}

String  POSIXPlatform::getClassName() const
{
  return "POSIXPlatform";
}

#if defined(_INLINE)
#include "../src/Debug.ipp"
#include "../src/String.ipp"
#endif


/* $Log$ */