AcePerl

 view release on metacpan or  search on metacpan

acelib/messubs.c  view on Meta::CPAN

 *              all functions.  mess*() versions continue to centralise
 *              handling of ... via stdarg.
 * * Aug 20 17:10 1998 (rd): moved memory handling to memsubs.c
 * * Jul  9 11:54 1998 (edgrif): 
 *              Fixed problem with SunOS not having strerror function, system
 *              is too old to have standard C libraries, have reverted to
 *              referencing sys_errlist for SunOS only.
 *              Also fixed problem with getpwuid in getLogin function, code
 *              did not check return value from getpwuid function.
 * * Jul  7 10:36 1998 (edgrif):
 *      -       Replaced reference to sys_errlist with strerror function.
 * * DON'T KNOW WHO MADE THESE CHANGES...NO RECORD IN HEADER....(edgrif)
 *      -       newformat added for the log file on mess dump.
 *      -       Time, host and pid are now always the first things written.
 *      -       This is for easier checking og the log.wrm with scripts etc.
 *      -       Messquery added for > 50 minor errors to ask if user wants to crash.
 *      -       Made user,pid and host static in messdump.
 * * Dec  3 15:52 1997 (rd)
 * 	-	messout(): defined(_WINDOW) =>!defined(NON_GRAPHIC)
 * * Dec 16 17:26 1996 (srk)
 * * Aug 15 13:29 1996 (srk)
 *	-	WIN32 and MACINTOSH: seteuid() etc. are stub functions
 * * Jun 6 10:50 1996 (rbrusk): compile error fixes
 * * Jun  4 23:31 1996 (rd)
 * Created: Mon Jun 29 14:15:56 1992 (rd)
 *-------------------------------------------------------------------
 */

/* $Id: messubs.c,v 1.1 2002/11/14 20:00:06 lstein Exp $ */

#include <assert.h>
#include <errno.h>
#include "regular.h"
#include "freeout.h"				  /* messbeep uses freeOutF */



/* This is horrible...a hack for sunos which is not standard C compliant.    */
/* to allow accessing system library error messages, will disappear....      */
#ifdef SUN
extern const char *sys_errlist[] ;
#endif


/* Mac has its own routine for crashing, see messcrash for usage.            */
#if !defined(MACINTOSH) 
extern void crashOut (char* text) ;
#endif



/* This buffer is used only by the routines that OUTPUT a message. Routines  */
/* that format messages into buffers (e.g. messprintf, messSysErrorText)     */
/* have their own buffers. Note that there is a problem here in that this    */
/* buffer can be overflowed, unfortunately because we use vsprintf to do     */
/* our formatting, this can only be detected after the event.                */
/*                                                                           */
/* Constraints on message buffer size - applicable to ALL routines that      */
/* format externally supplied strings.                                       */
/*                                                                           */
/* BUFSIZE:  size of message buffers (messbuf, a global buffer for general   */
/*           message stuff and a private ones in messdump & messprintf).     */
/* PREFIX:   length of message prefix (used to report details such as the    */
/*           file/line info. for where the error occurred.                   */
/* MAINTEXT: space left in buffer is the rest after the prefix and string    */
/*           terminator (NULL) are subtracted.                               */
/* Is there an argument for putting this buffer size in regular.h ??         */
/*                                                                           */
enum {BUFSIZE = 32768, PREFIXSIZE = 1024, MAINTEXTSIZE = BUFSIZE - PREFIXSIZE - 1} ;

static char messbuf[BUFSIZE] ;



/* Macro to format strings using va_xx calls, it calls uMessFormat whose     */
/* prototype is given below.                                                 */
/*                                                                           */
/* Arguments to the macro must have the following types:                     */
/*                                                                           */
/*   FORMAT_ARGS:   va_list used to get the variable argument list.          */
/*        FORMAT:   char *  to a string containing the printf format string. */
/*    TARGET_PTR:   char *  the formatted string will be returned in this    */
/*                          string pointer, N.B. do not put &TARGET_PTR      */
/*        PREFIX:   char *  to a string to be used as a prefix to the rest   */
/*                          of the string, or NULL.                          */
/*        BUFFER:   char *  the buffer where the formatting will take place, */
/*                          if NULL then the global messbuf buffer will be   */
/*                          used.                                            */
/*        BUFLEN:   unsigned                                                 */
/*                     int  the length of the buffer given by BUFFER (ignored*/
/*                          if BUFFER is NULL.                               */
/*                                                                           */
#define ACEFORMATSTRING(FORMAT_ARGS, FORMAT, TARGET_PTR, PREFIX, BUFFER, BUFLEN)  \
va_start(FORMAT_ARGS, FORMAT) ;                                                   \
TARGET_PTR = uMessFormat(FORMAT_ARGS, FORMAT, PREFIX, BUFFER, BUFLEN) ;           \
va_end(FORMAT_ARGS) ;

static char *uMessFormat(va_list args, char *format, char *prefix,
			 char *buffer, unsigned int buflen) ;


/* Some standard defines for titles/text for messages:                       */
/*                                                                           */
#define ERROR_PREFIX "ERROR: "
#define EXIT_PREFIX "EXIT: "
#define CRASH_PREFIX_FORMAT "FATAL ERROR reported by %s at line %d: "
#define FULL_CRASH_PREFIX_FORMAT "FATAL ERROR reported by program %s, in file %s, at line %d: "
#if defined(MACINTOSH)
#define SYSERR_FORMAT "system error %d"
#else
#define SYSERR_FORMAT "system error %d - %s"
#endif
#define PROGNAME "The program"

/* messcrash now reports the file/line no. where the messcrash was issued    */
/* as an aid to debugging. We do this using a static structure which holds   */
/* the information and a macro version of messcrash (see regular.h), the     */
/* structure elements are retrieved using access functions.                  */
typedef struct _MessErrorInfo
  {
  char *progname ;				  /* Name of executable reporting error. */
  char *filename ;				  /* Filename where error reported */
  int line_num ;				  /* Line number of file where error
						     reported. */
  } MessErrorInfo ;

static MessErrorInfo messageG = {NULL, NULL, 0} ;

static int messGetErrorLine() ;
static char *messGetErrorFile() ;


/* Keeps a running total of errors so far (incremented whenever messerror is */
/* called).                                                                  */
static int errorCount_G = 0 ;


/* Function pointers for application supplied routines that are called when  */
/* ever messerror or messcrash are called, enables application to take       */
/* action on all such errors.                                                */
static jmp_buf *errorJmpBuf = 0 ;
static jmp_buf *crashJmpBuf = 0 ;



/***************************************************************/
/********* call backs and functions to register them ***********/

acelib/messubs.c  view on Meta::CPAN

  char *mess ;

#ifdef SUN
  /* horrible hack for Sunos/Macs(?) which are not standard C compliant */
  mess = printToBuf(&errmess[0], ERRBUFSIZE, SYSERR_FORMAT, errno, sys_errlist[errno]) ;
#elif defined(MACINTOSH)
  mess = printToBuf(&errmess[0], ERRBUFSIZE, SYSERR_FORMAT, errno) ;
#else
  mess = printToBuf(&errmess[0], ERRBUFSIZE, SYSERR_FORMAT, errno, strerror(errno)) ;
#endif

  return mess ;
  }


/************************* message formatting ********************************/
/* This routine does the formatting of the message string using vsprintf,    */
/* it copes with the format string accidentally being our internal buffer.   */
/*                                                                           */
/* This routine does its best to check that the vsprintf is successful, if   */
/* not the routine bombs out with an error message. Note that num_bytes is   */
/* the return value from vsprintf.                                           */
/* Failures trapped:                                                         */
/*              num_bytes < 0  =>  vsprintf failed, reason is reported.      */
/*    num_bytes + 1 > BUFSIZE  =>  our internal buffer size was exceeded.    */
/*                                 (vsprintf returns number of bytes written */
/*                                  _minus_ terminating NULL)                */
/*                                                                           */
static char *uMessFormat(va_list args, char *format, char *prefix,
			 char *buffer, unsigned int buflen)
{
  char *buf_ptr ;
  unsigned int buf_len ;
  int prefix_len ;


  /* Check arguments.                                                        */
  if (format == NULL)
    { 
      fprintf(stderr, "uMessFormat() : "
	      "invalid call, no format string.\n") ;
      invokeDebugger();
      exit (EXIT_FAILURE);
    }

  if (prefix == NULL) 
    prefix_len = 0 ;
  else
    {
      prefix_len = strlen(prefix) ;
      if ((prefix_len + 1) > PREFIXSIZE) 
	{
	  fprintf (stderr, "uMessFormat() : "
		   "prefix string is too long.\n") ;
	  invokeDebugger();
	  exit (EXIT_FAILURE);
	}
    }

  /* If they supply their own buffer to receive the formatted 
     message then use this, otherwise use the global messbuf buffer. */
  if (buffer != NULL)
    {
      buf_ptr = buffer ;
      buf_len = buflen ;
      if (buf_len == 0) 
	{
	  fprintf (stderr, "uMessFormat() : "
		   "zero length buffer supplied for message format.\n") ;
	  invokeDebugger();
	  exit (EXIT_FAILURE);
	}
    }
  else
    {
      buf_ptr = &messbuf[0] ;
      buf_len = BUFSIZE ;
    }

  /* Add the prefix if there is one. */
  if (prefix != NULL)
    {
      if (strcpy (buf_ptr, prefix) == NULL) 
	{
	  fprintf (stderr, "uMessFormat() : strcpy failed\n") ;
	  invokeDebugger();
	  exit (EXIT_FAILURE);
	}
    }
  

  /* CHECK PERFORMANCE ISSUES....how is database dumped/logged.              */

  /* Fred has suggested that we could do a vprintf to /dev/null and see how  */
  /* many bytes that is then we could get away from a fixed internal buffer  */
  /* at all....but watch out, if messdump say is in a tight loop then this   */
  /* will kill performance...                                                */
  /* We could add a #define to allow a check to be included for debug code.  */
  /*                                                                         */


  /* Do the format. */

#ifdef SUN
  {
    char *return_str;

    /* NOTE, that SUNs vsprintf returns a char* */
    return_str = vsprintf((buf_ptr + prefix_len), format, args) + prefix_len + 1 ;
    
    /* Check the result. */
    if (!return_str)
      {
	fprintf(stderr, "uMessFormat() : "
		"vsprintf failed: %s\n", messSysErrorText()) ;
	invokeDebugger();
	exit (EXIT_FAILURE);
      }
    else if (strlen(return_str) > buf_len)
      {
	fprintf (stderr, "uMessFormat() : "



( run in 0.570 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )