Files
harbour-core/contrib/xhb/hboutdbg.c
Przemysław Czerpak bf146ab5ee 2025-01-24 12:32 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl)
* contrib/hbwin/olecore.c
    * pacified false MSVC warning. It's very seldom situation that I'm adding
      code to pacify false compiler warnings anyhow in this case it cost in
      practice nothing so I decided to do that.

  * contrib/xhb/hboutdbg.c
  * src/rtl/gtcrs/gtcrs.c
    * NULL pointer casting to pacify warning

  * src/pp/hbpp.c
    % do not created new iLen variable but reuse the one previously declared

  * src/vm/classes.c
    % allocate puiMsgIdx as array of HB_USHORT items instead of HB_SYMCNT.
      This modification only reduces the memory usage and has no effect on
      number of accepted symbols.
    * changed 'HB_SYMCNT uiHashKey' to 'HB_SIZE nHashKey' to avoid explicit
      casting in the code
2025-01-24 12:32:32 +01:00

241 lines
6.4 KiB
C

/*
* Debug to separate console string output
*
* Copyright 2003 Giancarlo Niccolai <gian@niccolai.ws>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file LICENSE.txt. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA (or visit https://www.gnu.org/licenses/).
*
* As a special exception, the Harbour Project gives permission for
* additional uses of the text contained in its release of Harbour.
*
* The exception is that, if you link the Harbour libraries with other
* files to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the Harbour library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the Harbour
* Project under the name Harbour. If you copy code from other
* Harbour Project or Free Software Foundation releases into a copy of
* Harbour, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for Harbour, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice.
*
*/
/* JC1: Caution. This code is NOT multithreaded as it is meant
to be managed from the main thread. Every thread may use it,
but only the main thread should be allowed to activate the
debug window.
*/
#include "hbapi.h"
#include "hbapiitm.h"
#include "hbapifs.h"
#include "xhb.h"
#if defined( HB_OS_WIN )
#include <windows.h>
#include "hbwinuni.h"
#endif
#if defined( HB_OS_UNIX ) && ! defined( HB_OS_VXWORKS )
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
static int s_iDebugFd = 0;
static char s_szDebugName[ 128 ];
static int s_iUseDebugName = 0;
static int s_iXtermPid = 0;
static void debugInit( void )
{
int iFifoResult;
PHB_FNAME pFileName = NULL;
char szDebugName[ 128 ];
if( ! s_iUseDebugName )
{
int iRand = ( int ) ( hb_random_num() * 1000000 );
pFileName = hb_fsFNameSplit( hb_cmdargARGVN( 0 ) );
hb_snprintf( szDebugName, sizeof( szDebugName ) - 1, "/tmp/%s%d_dbg", pFileName->szName, iRand );
}
else
{
hb_snprintf( szDebugName, sizeof( szDebugName ), "/tmp/%s_dbg", s_szDebugName );
pFileName = hb_fsFNameSplit( szDebugName );
}
iFifoResult = mkfifo( szDebugName, 0666 );
if( iFifoResult == -1 )
iFifoResult = errno;
if( iFifoResult == 0 || iFifoResult == EEXIST )
{
char szDebugTitle[ 30 ];
int iPid;
hb_snprintf( szDebugTitle, sizeof( szDebugTitle ), "%s - Debug", pFileName->szName );
iPid = fork();
if( iPid != 0 )
{
s_iDebugFd = open( szDebugName, O_WRONLY );
s_iXtermPid = iPid;
}
else
{
if( iFifoResult != EEXIST )
{
s_iXtermPid = execlp( "xterm", "xterm", "-T", szDebugTitle, "-e",
"cat", szDebugName, ( char * ) NULL );
if( s_iXtermPid <= 0 )
{
int lastresort = open( szDebugName, O_RDONLY );
if( lastresort >= 0 )
close( lastresort );
}
}
}
}
hb_xfree( pFileName );
}
#endif
HB_BOOL hb_OutDebugName( PHB_ITEM pName )
{
HB_BOOL bRet;
#if defined( HB_OS_UNIX ) && ! defined( HB_OS_VXWORKS )
if( s_iDebugFd == 0 && pName != NULL )
{
hb_strncpy( s_szDebugName, hb_itemGetCPtr( pName ), sizeof( s_szDebugName ) - 1 );
s_iUseDebugName = 1;
bRet = HB_TRUE;
}
else if( pName == NULL )
{
s_iUseDebugName = 0;
bRet = HB_TRUE;
}
else
bRet = HB_FALSE;
#else
HB_SYMBOL_UNUSED( pName );
bRet = HB_FALSE;
#endif
return bRet;
}
void hb_OutDebug( const char * szMsg, HB_SIZE nMsgLen )
{
#if defined( HB_OS_UNIX ) && ! defined( HB_OS_VXWORKS )
int iStatus;
/* Are we under X? */
if( getenv( "DISPLAY" ) != NULL )
{
if( s_iDebugFd <= 0 || s_iXtermPid == 0 )
debugInit();
/* On error, drop it */
if( s_iDebugFd <= 0 || s_iXtermPid == 0 )
return;
/* Check if display process has terminated in the meanwhile */
if( ! s_iUseDebugName )
{
int iPid = waitpid( s_iXtermPid, &iStatus, WNOHANG );
if( iPid == s_iXtermPid || iPid == -1 )
{
s_iXtermPid = 0;
#if 0
close( s_iDebugFd );
#endif
s_iDebugFd = 0;
return;
}
}
if( s_iDebugFd > 0 && HB_ISCHAR( 1 ) )
{
if( hb_fsCanWrite( s_iDebugFd, 100 ) > 0 ) /* wait each time a tenth of second */
{
if( ( HB_SIZE ) write( s_iDebugFd, szMsg, nMsgLen ) == nMsgLen )
{
if( hb_fsCanWrite( s_iDebugFd, 100 ) > 0 )
{
if( write( s_iDebugFd, "\n", 1 ) != 1 )
{
}
}
}
}
}
}
#elif defined( HB_OS_WIN )
{
LPTSTR lpMsg = HB_CHARDUPN( szMsg, nMsgLen );
OutputDebugString( lpMsg );
hb_xfree( lpMsg );
}
#else
HB_SYMBOL_UNUSED( szMsg );
HB_SYMBOL_UNUSED( nMsgLen );
#endif
}
HB_FUNC( HB_OUTDEBUGNAME )
{
PHB_ITEM pName = hb_param( 1, HB_IT_STRING );
hb_retl( hb_OutDebugName( pName ) );
}
HB_FUNC( HB_OUTDEBUG )
{
if( HB_ISCHAR( 1 ) )
hb_OutDebug( hb_parcx( 1 ), hb_parclen( 1 ) );
}