#include "IntrinsicI.h"
#include <stdio.h>
#include <stdlib.h>
#ifndef GLOBALERRORS
#define GLOBALERRORS 1
#endif
static void InitErrorHandling();
#if GLOBALERRORS
static XrmDatabase errorDB = NULL;
static Boolean error_inited = FALSE;
void _XtDefaultErrorMsg(), _XtDefaultWarningMsg(),
_XtDefaultError(), _XtDefaultWarning();
static XtErrorMsgHandler errorMsgHandler = _XtDefaultErrorMsg;
static XtErrorMsgHandler warningMsgHandler = _XtDefaultWarningMsg;
static XtErrorHandler errorHandler = _XtDefaultError;
static XtErrorHandler warningHandler = _XtDefaultWarning;
#endif
XrmDatabase *XtGetErrorDatabase(void)
{
XrmDatabase* retval;
#if GLOBALERRORS
LOCK_PROCESS;
retval = &errorDB;
UNLOCK_PROCESS;
#else
retval = XtAppGetErrorDatabase(_XtDefaultAppContext());
#endif
return retval;
}
XrmDatabase *XtAppGetErrorDatabase(app)
XtAppContext app;
{
XrmDatabase* retval;
#if GLOBALERRORS
LOCK_PROCESS;
retval = &errorDB;
UNLOCK_PROCESS;
#else
LOCK_APP(app);
retval= &app->errorDB;
UNLOCK_APP(app);
#endif
return retval;
}
void XtGetErrorDatabaseText(
register _Xconst char* name,
register _Xconst char* type,
register _Xconst char* class,
_Xconst char* defaultp,
String buffer,
int nbytes)
{
#if GLOBALERRORS
XtAppGetErrorDatabaseText(NULL,
name,type,class,defaultp, buffer, nbytes, NULL);
#else
XtAppGetErrorDatabaseText(_XtDefaultAppContext(),
name,type,class,defaultp, buffer, nbytes, NULL);
#endif
}
void XtAppGetErrorDatabaseText(
XtAppContext app,
register _Xconst char* name,
register _Xconst char* type,
register _Xconst char* class,
_Xconst char* defaultp,
String buffer,
int nbytes,
XrmDatabase db)
{
String str_class;
String type_str;
XrmValue result;
char *str_name = NULL;
char *temp = NULL;
#if GLOBALERRORS
LOCK_PROCESS;
if (error_inited == FALSE) {
InitErrorHandling (&errorDB);
error_inited = TRUE;
}
#else
LOCK_APP(app);
if (app->error_inited == FALSE) {
InitErrorHandling (&app->errorDB);
app->error_inited = TRUE;
}
#endif
if (!(str_name = ALLOCATE_LOCAL(strlen(name) + strlen(type) + 2)))
_XtAllocError(NULL);
(void) sprintf(str_name, "%s.%s", name, type);
str_class = (char *)class;
if (! strchr(class, '.')) {
if (!(temp = ALLOCATE_LOCAL(2 * strlen(class) + 2)))
_XtAllocError(NULL);
(void) sprintf(temp, "%s.%s", class, class);
str_class = temp;
}
if (db == NULL) {
#if GLOBALERRORS
(void) XrmGetResource(errorDB, str_name, str_class, &type_str,
&result);
#else
(void) XrmGetResource(app->errorDB, str_name, str_class, &type_str,
&result);
#endif
} else (void) XrmGetResource(db, str_name, str_class, &type_str, &result);
if (result.addr) {
(void) strncpy (buffer, result.addr, nbytes);
if (result.size > nbytes) buffer[nbytes-1] = 0;
} else {
int len = strlen(defaultp);
if (len >= nbytes) len = nbytes-1;
(void) memmove(buffer, defaultp, len);
buffer[len] = '\0';
}
if (str_name)
DEALLOCATE_LOCAL(str_name);
if (temp)
DEALLOCATE_LOCAL(temp);
#if GLOBALERRORS
UNLOCK_PROCESS;
#else
UNLOCK_APP(app);
#endif
}
static void InitErrorHandling (db)
XrmDatabase *db;
{
XrmDatabase errordb;
errordb = XrmGetFileDatabase(ERRORDB);
XrmMergeDatabases(errordb, db);
}
static void DefaultMsg (name,type,class,defaultp,params,num_params,error,fn)
String name,type,class,defaultp;
String* params;
Cardinal* num_params;
Bool error;
void (*fn)(_Xconst _XtString);
{
#define BIGBUF 1024
#ifdef notyet
#if defined(__linux__) || defined(CSRG_BASED)
#define USE_SNPRINTF
#endif
#endif
char buffer[BIGBUF];
char* message;
XtGetErrorDatabaseText(name,type,class,defaultp, buffer, BIGBUF);
if (params == NULL || num_params == NULL || *num_params == 0)
(*fn)(buffer);
#ifndef WIN32
else if ((getuid () != geteuid ()) || getuid() == 0) {
if ((error && errorHandler == _XtDefaultError) ||
(!error && warningHandler == _XtDefaultWarning)) {
int i = *num_params;
String par[10];
if (i > 10) i = 10;
(void) memmove((char*)par, (char*)params, i * sizeof(String) );
bzero( &par[i], (10-i) * sizeof(String) );
(void) fprintf (stderr, "%s%s",
error ? XTERROR_PREFIX : XTWARNING_PREFIX,
error ? "Error: " : "Warning: ");
(void) fprintf (stderr, buffer,
par[0], par[1], par[2], par[3], par[4],
par[5], par[6], par[7], par[8], par[9]);
(void) fprintf (stderr, "%c", '\n');
if (i != *num_params)
(*fn) ( "Some arguments in previous message were lost" );
else if (error) exit (1);
} else {
XtWarning ("\
This program is an suid-root program or is being run by the root user.\n\
The full text of the error or warning message cannot be safely formatted\n\
in this environment. You may get a more descriptive message by running the\n\
program as a non-root user or by removing the suid bit on the executable.");
(*fn)(buffer);
}
}
#endif
else {
int i = *num_params;
String par[10];
if (i > 10) i = 10;
(void) memmove((char*)par, (char*)params, i * sizeof(String) );
bzero( &par[i], (10-i) * sizeof(String) );
if (i != *num_params)
XtWarning( "Some arguments in following message were lost" );
if ((message = __XtMalloc (BIGBUF))) {
#ifndef USE_SNPRINTF
message[BIGBUF-1] = 0;
(void) sprintf (message, buffer,
#else
(void) snprintf (message, BIGBUF, buffer,
#endif
par[0], par[1], par[2], par[3], par[4],
par[5], par[6], par[7], par[8], par[9]);
#ifndef USE_SNPRINTF
if (message[BIGBUF-1] != '\0')
XtWarning ("Possible heap corruption in Xt{Error,Warning}MsgHandler");
#endif
(*fn)(message);
XtFree(message);
} else {
XtWarning ("Memory allocation failed, arguments in the following message were lost");
(*fn)(buffer);
}
}
}
void _XtDefaultErrorMsg (name,type,class,defaultp,params,num_params)
String name,type,class,defaultp;
String* params;
Cardinal* num_params;
{
DefaultMsg (name,type,class,defaultp,params,num_params,True,XtError);
}
void _XtDefaultWarningMsg (name,type,class,defaultp,params,num_params)
String name,type,class,defaultp;
String* params;
Cardinal* num_params;
{
DefaultMsg (name,type,class,defaultp,params,num_params,False,XtWarning);
}
void XtErrorMsg(
_Xconst char* name,
_Xconst char* type,
_Xconst char* class,
_Xconst char* defaultp,
String* params,
Cardinal* num_params)
{
#if GLOBALERRORS
LOCK_PROCESS;
(*errorMsgHandler)((String)name,(String)type,(String)class,
(String)defaultp,params,num_params);
UNLOCK_PROCESS;
#else
XtAppErrorMsg(_XtDefaultAppContext(),name,type,class,
defaultp,params,num_params);
#endif
}
void XtAppErrorMsg(
XtAppContext app,
_Xconst char* name,
_Xconst char* type,
_Xconst char* class,
_Xconst char* defaultp,
String* params,
Cardinal* num_params)
{
#if GLOBALERRORS
LOCK_PROCESS;
(*errorMsgHandler)((String)name,(String)type,(String)class,
(String)defaultp,params,num_params);
UNLOCK_PROCESS;
#else
LOCK_APP(app);
(*app->errorMsgHandler)(name,type,class,defaultp,params,num_params);
UNLOCK_APP(app);
#endif
}
void XtWarningMsg(
_Xconst char* name,
_Xconst char* type,
_Xconst char* class,
_Xconst char* defaultp,
String* params,
Cardinal* num_params)
{
#if GLOBALERRORS
LOCK_PROCESS;
(*warningMsgHandler)((String)name,(String)type,(String)class,
(String)defaultp,params,num_params);
UNLOCK_PROCESS;
#else
XtAppWarningMsg(_XtDefaultAppContext(),name,type,class,
defaultp,params,num_params);
#endif
}
void XtAppWarningMsg(
XtAppContext app,
_Xconst char* name,
_Xconst char* type,
_Xconst char* class,
_Xconst char* defaultp,
String* params,
Cardinal* num_params)
{
#if GLOBALERRORS
LOCK_PROCESS;
(*warningMsgHandler)((String)name,(String)type,(String)class,
(String)defaultp,params,num_params);
UNLOCK_PROCESS;
#else
LOCK_APP(app);
(*app->warningMsgHandler)(name,type,class,defaultp,params,num_params);
UNLOCK_APP(app);
#endif
}
void XtSetErrorMsgHandler(handler)
XtErrorMsgHandler handler;
{
#if GLOBALERRORS
LOCK_PROCESS;
if (handler != NULL) errorMsgHandler = handler;
else errorMsgHandler = _XtDefaultErrorMsg;
UNLOCK_PROCESS;
#else
XtAppSetErrorMsgHandler(_XtDefaultAppContext(), handler);
#endif
}
XtErrorMsgHandler XtAppSetErrorMsgHandler(
XtAppContext app,
XtErrorMsgHandler handler)
{
XtErrorMsgHandler old;
#if GLOBALERRORS
LOCK_PROCESS;
old = errorMsgHandler;
if (handler != NULL) errorMsgHandler = handler;
else errorMsgHandler = _XtDefaultErrorMsg;
UNLOCK_PROCESS;
#else
LOCK_APP(app);
old = app->errorMsgHandler;
if (handler != NULL) app->errorMsgHandler = handler;
else app->errorMsgHandler = _XtDefaultErrorMsg;
UNLOCK_APP(app);
#endif
return old;
}
void XtSetWarningMsgHandler(handler)
XtErrorMsgHandler handler;
{
#if GLOBALERRORS
LOCK_PROCESS;
if (handler != NULL) warningMsgHandler = handler;
else warningMsgHandler = _XtDefaultWarningMsg;
UNLOCK_PROCESS;
#else
XtAppSetWarningMsgHandler(_XtDefaultAppContext(),handler);
#endif
}
XtErrorMsgHandler XtAppSetWarningMsgHandler(
XtAppContext app,
XtErrorMsgHandler handler)
{
XtErrorMsgHandler old;
#if GLOBALERRORS
LOCK_PROCESS;
old = warningMsgHandler;
if (handler != NULL) warningMsgHandler = handler;
else warningMsgHandler = _XtDefaultWarningMsg;
UNLOCK_PROCESS;
#else
LOCK_APP(app);
old = app->warningMsgHandler;
if (handler != NULL) app->warningMsgHandler = handler;
else app->warningMsgHandler = _XtDefaultWarningMsg;
UNLOCK_APP(app);
#endif
return old;
}
void _XtDefaultError(message)
String message;
{
if (message && *message)
(void)fprintf(stderr, "%sError: %s\n", XTERROR_PREFIX, message);
exit(1);
}
void _XtDefaultWarning(message)
String message;
{
if (message && *message)
(void)fprintf(stderr, "%sWarning: %s\n", XTWARNING_PREFIX, message);
return;
}
void XtError(
_Xconst char* message)
{
#if GLOBALERRORS
LOCK_PROCESS;
(*errorHandler)((String)message);
UNLOCK_PROCESS;
#else
XtAppError(_XtDefaultAppContext(),message);
#endif
}
void XtAppError(
XtAppContext app,
_Xconst char* message)
{
#if GLOBALERRORS
LOCK_PROCESS;
(*errorHandler)((String)message);
UNLOCK_PROCESS;
#else
LOCK_APP(app);
(*app->errorHandler)(message);
UNLOCK_APP(app);
#endif
}
void XtWarning(
_Xconst char* message)
{
#if GLOBALERRORS
LOCK_PROCESS;
(*warningHandler)((String)message);
UNLOCK_PROCESS;
#else
XtAppWarning(_XtDefaultAppContext(),message);
#endif
}
void XtAppWarning(
XtAppContext app,
_Xconst char* message)
{
#if GLOBALERRORS
LOCK_PROCESS;
(*warningHandler)((String)message);
UNLOCK_PROCESS;
#else
LOCK_APP(app);
(*app->warningHandler)(message);
UNLOCK_APP(app);
#endif
}
void XtSetErrorHandler(XtErrorHandler handler)
{
#if GLOBALERRORS
LOCK_PROCESS;
if (handler != NULL) errorHandler = handler;
else errorHandler = _XtDefaultError;
UNLOCK_PROCESS;
#else
XtAppSetErrorHandler(_XtDefaultAppContext(),handler);
#endif
}
XtErrorHandler XtAppSetErrorHandler(
XtAppContext app,
XtErrorHandler handler)
{
XtErrorHandler old;
#if GLOBALERRORS
LOCK_PROCESS;
old = errorHandler;
if (handler != NULL) errorHandler = handler;
else errorHandler = _XtDefaultError;
UNLOCK_PROCESS;
#else
LOCK_APP(app);
old = app->errorHandler;
if (handler != NULL) app->errorHandler = handler;
else app->errorHandler = _XtDefaultError;
UNLOCK_APP(app);
#endif
return old;
}
void XtSetWarningHandler(XtErrorHandler handler)
{
#if GLOBALERRORS
LOCK_PROCESS;
if (handler != NULL) warningHandler = handler;
else warningHandler = _XtDefaultWarning;
UNLOCK_PROCESS;
#else
XtAppSetWarningHandler(_XtDefaultAppContext(),handler);
#endif
}
XtErrorHandler XtAppSetWarningHandler(
XtAppContext app,
XtErrorHandler handler)
{
XtErrorHandler old;
#if GLOBALERRORS
LOCK_PROCESS;
old = warningHandler;
if (handler != NULL) warningHandler = handler;
else warningHandler = _XtDefaultWarning;
UNLOCK_PROCESS;
#else
LOCK_APP(app);
old = app->warningHandler;
if (handler != NULL) app->warningHandler = handler;
else app->warningHandler = _XtDefaultWarning;
UNLOCK_APP(app);
#endif
return old;
}
void _XtSetDefaultErrorHandlers(errMsg, warnMsg, err, warn)
XtErrorMsgHandler *errMsg, *warnMsg;
XtErrorHandler *err, *warn;
{
#ifndef GLOBALERRORS
LOCK_PROCESS;
*errMsg = _XtDefaultErrorMsg;
*warnMsg = _XtDefaultWarningMsg;
*err = _XtDefaultError;
*warn = _XtDefaultWarning;
UNLOCK_PROCESS;
#endif
}