home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xt / Error.c.orig < prev    next >
Encoding:
Text File  |  1991-04-12  |  13.6 KB  |  529 lines

  1. /* $XConsortium: Error.c,v 1.32 91/04/12 11:37:18 rws Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  5. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  6.  
  7.                         All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its 
  10. documentation for any purpose and without fee is hereby granted, 
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in 
  13. supporting documentation, and that the names of Digital or MIT not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.  
  16.  
  17. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  19. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27. #include "IntrinsicI.h"
  28. #include <stdio.h>
  29.  
  30. /* The error handlers in the application context aren't used since we can't
  31.    come up with a uniform way of using them.  If you can, define
  32.    GLOBALERRORS to be FALSE (or 0). */
  33.  
  34. #ifndef GLOBALERRORS
  35. #define GLOBALERRORS 1
  36. #endif
  37.  
  38. #if GLOBALERRORS
  39. static XrmDatabase errorDB = NULL;
  40. static Boolean error_inited = FALSE;
  41. void _XtDefaultErrorMsg(), _XtDefaultWarningMsg(), 
  42.     _XtDefaultError(), _XtDefaultWarning();
  43. static XtErrorMsgHandler errorMsgHandler = _XtDefaultErrorMsg;
  44. static XtErrorMsgHandler warningMsgHandler = _XtDefaultWarningMsg;
  45. static XtErrorHandler errorHandler = _XtDefaultError;
  46. static XtErrorHandler warningHandler = _XtDefaultWarning;
  47. #endif /* GLOBALERRORS */
  48.  
  49. XrmDatabase *XtGetErrorDatabase()
  50. {
  51. #if GLOBALERRORS
  52.     return &errorDB;
  53. #else
  54.     return XtAppGetErrorDatabase(_XtDefaultAppContext());
  55. #endif /* GLOBALERRORS */
  56. }
  57.  
  58. XrmDatabase *XtAppGetErrorDatabase(app)
  59.     XtAppContext app;
  60. {
  61. #if GLOBALERRORS
  62.     return &errorDB;
  63. #else
  64.     return &app->errorDB;
  65. #endif /* GLOBALERRORS */
  66. }
  67.  
  68. #if NeedFunctionPrototypes
  69. void XtGetErrorDatabaseText(
  70.     register _Xconst char* name,
  71.     register _Xconst char* type,
  72.     register _Xconst char* class,
  73.     _Xconst char* defaultp,
  74.     String buffer,
  75.     int nbytes
  76.     )
  77. #else
  78. void XtGetErrorDatabaseText(name,type,class,defaultp, buffer, nbytes)
  79.     register String name, type, class;
  80.     String defaultp;
  81.     String buffer;
  82.     int nbytes;
  83. #endif
  84. {
  85. #if GLOBALERRORS
  86.     XtAppGetErrorDatabaseText(NULL,
  87.         name,type,class,defaultp, buffer, nbytes, NULL);
  88. #else
  89.     XtAppGetErrorDatabaseText(_XtDefaultAppContext(),
  90.         name,type,class,defaultp, buffer, nbytes, NULL);
  91. #endif /* GLOBALERRORS */
  92. }
  93.  
  94. #if NeedFunctionPrototypes
  95. void XtAppGetErrorDatabaseText(
  96.     XtAppContext app,
  97.     register _Xconst char* name,
  98.     register _Xconst char* type,
  99.     register _Xconst char* class,
  100.     _Xconst char* defaultp,
  101.     String buffer,
  102.     int nbytes,
  103.     XrmDatabase db
  104.     )
  105. #else
  106. void XtAppGetErrorDatabaseText(app, name,type,class,defaultp,
  107.     buffer, nbytes, db)
  108.     XtAppContext app;
  109.     register String name, type, class;
  110.     String defaultp;
  111.     String buffer;
  112.     int nbytes;
  113.     XrmDatabase db;
  114. #endif
  115. {
  116.     String str_class;
  117.     String type_str;
  118.     XrmValue result;
  119.     char str_name[BUFSIZ];
  120.     char temp[BUFSIZ];
  121.  
  122. #if GLOBALERRORS
  123.     if (error_inited == FALSE) {
  124.         _XtInitErrorHandling (&errorDB);
  125.         error_inited = TRUE;
  126.     }
  127. #else
  128.     if (app->error_inited == FALSE) {
  129.         _XtInitErrorHandling (&app->errorDB);
  130.         app->error_inited = TRUE;
  131.     }
  132. #endif /* GLOBALERRORS */
  133.     (void) sprintf(str_name, "%s.%s", name, type);
  134.     /* XrmGetResource requires the name and class to be fully qualified
  135.      * and to have the same number of components. */
  136.     str_class = (char *)class;
  137.     if (! strchr(class, '.')) {
  138.     (void) sprintf(temp, "%s.%s", class, class);
  139.     str_class = temp;
  140.     }
  141.     if (db == NULL) {
  142. #if GLOBALERRORS
  143.     (void) XrmGetResource(errorDB, str_name, str_class, &type_str,
  144.                   &result);
  145. #else
  146.     (void) XrmGetResource(app->errorDB, str_name, str_class, &type_str,
  147.                   &result);
  148. #endif /* GLOBALERRORS */
  149.     } else (void) XrmGetResource(db, str_name, str_class, &type_str, &result);
  150.     if (result.addr) {
  151.         (void) strncpy (buffer, result.addr, nbytes);
  152.         if (result.size > nbytes) buffer[nbytes-1] = 0;
  153.     } else {
  154.     int len = strlen(defaultp);
  155.     if (len >= nbytes) len = nbytes-1;
  156.     bcopy(defaultp, buffer, len);
  157.     buffer[len] = '\0';
  158.     }
  159. }
  160.  
  161. _XtInitErrorHandling (db)
  162.     XrmDatabase *db;
  163. {
  164.     XrmDatabase errordb;
  165.  
  166.     errordb = XrmGetFileDatabase(ERRORDB);
  167.     XrmMergeDatabases(errordb, db);
  168. }
  169.  
  170. void _XtDefaultErrorMsg (name,type,class,defaultp,params,num_params)
  171.     String name,type,class,defaultp;
  172.     String* params;
  173.     Cardinal* num_params;
  174. {
  175.     char buffer[1000], message[1000];
  176.     XtGetErrorDatabaseText(name,type,class,defaultp, buffer, 1000);
  177. /*need better solution here, perhaps use lower level printf primitives? */
  178.     if (params == NULL || num_params == NULL || *num_params == 0)
  179.     XtError(buffer);
  180.     else {
  181.     int i = *num_params;
  182.     String par[10];
  183.     if (i > 10) i = 10;
  184.     bcopy( (char*)params, (char*)par, i * sizeof(String) );
  185.     bzero( &par[i], (10-i) * sizeof(String) );
  186.         (void) sprintf(message, buffer, par[0], par[1], par[2], par[3],
  187.                par[4], par[5], par[6], par[7], par[8], par[9]);
  188.     XtError(message);
  189.     if (i != *num_params)
  190.         XtWarning( "some arguments in previous message were lost" );
  191.     }
  192. }
  193.  
  194. void _XtDefaultWarningMsg (name,type,class,defaultp,params,num_params)
  195.     String name,type,class,defaultp;
  196.     String* params;
  197.     Cardinal* num_params;
  198. {
  199.  
  200.     char buffer[1000], message[1000];
  201.     XtGetErrorDatabaseText(name,type,class,defaultp, buffer, 1000);
  202. /*need better solution here*/
  203.     if (params == NULL || num_params == NULL || *num_params == 0)
  204.     XtWarning(buffer);
  205.     else {
  206.     int i = *num_params;
  207.     String par[10];
  208.     if (i > 10) i = 10;
  209.     bcopy( (char*)params, (char*)par, i * sizeof(String) );
  210.     bzero( &par[i], (10-i) * sizeof(String) );
  211.         (void) sprintf(message, buffer, par[0], par[1], par[2], par[3],
  212.                par[4], par[5], par[6], par[7], par[8], par[9]);
  213.     XtWarning(message); 
  214.     if (i != *num_params)
  215.         XtWarning( "some arguments in previous message were lost" );
  216.    }
  217. }
  218.  
  219. #if NeedFunctionPrototypes
  220. void XtErrorMsg(
  221.     _Xconst char* name,
  222.     _Xconst char* type,
  223.     _Xconst char* class,
  224.     _Xconst char* defaultp,
  225.     String* params,
  226.     Cardinal* num_params
  227.     )
  228. #else
  229. void XtErrorMsg(name,type,class,defaultp,params,num_params)
  230.     String name,type,class,defaultp;
  231.     String* params;
  232.     Cardinal* num_params;
  233. #endif
  234. {
  235. #if GLOBALERRORS
  236.     (*errorMsgHandler)((String)name,(String)type,(String)class,
  237.                (String)defaultp,params,num_params);
  238. #else
  239.     XtAppErrorMsg(_XtDefaultAppContext(),name,type,class,
  240.         defaultp,params,num_params);
  241. #endif /* GLOBALERRORS */
  242. }
  243.  
  244. #if NeedFunctionPrototypes
  245. void XtAppErrorMsg(
  246.     XtAppContext app,
  247.     _Xconst char* name,
  248.     _Xconst char* type,
  249.     _Xconst char* class,
  250.     _Xconst char* defaultp,
  251.     String* params,
  252.     Cardinal* num_params
  253.     )
  254. #else
  255. void XtAppErrorMsg(app, name,type,class,defaultp,params,num_params)
  256.     XtAppContext app;
  257.     String name,type,class,defaultp;
  258.     String* params;
  259.     Cardinal* num_params;
  260. #endif
  261. {
  262. #if GLOBALERRORS
  263.     (*errorMsgHandler)((String)name,(String)type,(String)class,
  264.                (String)defaultp,params,num_params);
  265. #else
  266.     (*app->errorMsgHandler)(name,type,class,defaultp,params,num_params);
  267. #endif /* GLOBALERRORS */
  268. }
  269.  
  270. #if NeedFunctionPrototypes
  271. void XtWarningMsg(
  272.     _Xconst char* name,
  273.     _Xconst char* type,
  274.     _Xconst char* class,
  275.     _Xconst char* defaultp,
  276.     String* params,
  277.     Cardinal* num_params
  278.     )
  279. #else
  280. void XtWarningMsg(name,type,class,defaultp,params,num_params)
  281.     String name,type,class,defaultp;
  282.     String* params;
  283.     Cardinal* num_params;
  284. #endif
  285. {
  286. #if GLOBALERRORS
  287.     (*warningMsgHandler)((String)name,(String)type,(String)class,
  288.              (String)defaultp,params,num_params);
  289. #else
  290.     XtAppWarningMsg(_XtDefaultAppContext(),name,type,class,
  291.         defaultp,params,num_params);
  292. #endif /* GLOBALERRORS */
  293. }
  294.  
  295. #if NeedFunctionPrototypes
  296. void XtAppWarningMsg(
  297.     XtAppContext app,
  298.     _Xconst char* name,
  299.     _Xconst char* type,
  300.     _Xconst char* class,
  301.     _Xconst char* defaultp,
  302.     String* params,
  303.     Cardinal* num_params
  304.     )
  305. #else
  306. void XtAppWarningMsg(app,name,type,class,defaultp,params,num_params)
  307.     XtAppContext app;
  308.     String name,type,class,defaultp;
  309.     String* params;
  310.     Cardinal* num_params;
  311. #endif
  312. {
  313. #if GLOBALERRORS
  314.     (*warningMsgHandler)((String)name,(String)type,(String)class,
  315.              (String)defaultp,params,num_params);
  316. #else
  317.     (*app->warningMsgHandler)(name,type,class,defaultp,params,num_params);
  318. #endif /* GLOBALERRORS */
  319. }
  320.  
  321. void XtSetErrorMsgHandler(handler)
  322.     XtErrorMsgHandler handler;
  323. {
  324. #if GLOBALERRORS
  325.     if (handler != NULL) errorMsgHandler = handler;
  326.     else errorMsgHandler  = _XtDefaultErrorMsg;
  327. #else
  328.     XtAppSetErrorMsgHandler(_XtDefaultAppContext(), handler);
  329. #endif /* GLOBALERRORS */
  330. }
  331.  
  332. XtErrorMsgHandler XtAppSetErrorMsgHandler(app,handler)
  333.     XtAppContext app;
  334.     XtErrorMsgHandler handler;
  335. {
  336.     XtErrorMsgHandler old;
  337. #if GLOBALERRORS
  338.     old = errorMsgHandler;
  339.     if (handler != NULL) errorMsgHandler = handler;
  340.     else errorMsgHandler  = _XtDefaultErrorMsg;
  341. #else
  342.     old = app->errorMsgHandler;
  343.     if (handler != NULL) app->errorMsgHandler = handler;
  344.     else app->errorMsgHandler  = _XtDefaultErrorMsg;
  345. #endif /* GLOBALERRORS */
  346.     return old;
  347. }
  348.  
  349. void XtSetWarningMsgHandler(handler)
  350.     XtErrorMsgHandler handler;
  351. {
  352. #if GLOBALERRORS
  353.     if (handler != NULL) warningMsgHandler  = handler;
  354.     else warningMsgHandler = _XtDefaultWarningMsg;
  355. #else
  356.     XtAppSetWarningMsgHandler(_XtDefaultAppContext(),handler);
  357. #endif /* GLOBALERRORS */
  358. }
  359.  
  360. XtErrorMsgHandler XtAppSetWarningMsgHandler(app,handler)
  361.     XtAppContext app;
  362.     XtErrorMsgHandler handler;
  363. {
  364.     XtErrorMsgHandler old;
  365. #if GLOBALERRORS
  366.     old = warningMsgHandler;
  367.     if (handler != NULL) warningMsgHandler  = handler;
  368.     else warningMsgHandler = _XtDefaultWarningMsg;
  369. #else
  370.     old = app->warningMsgHandler;
  371.     if (handler != NULL) app->warningMsgHandler  = handler;
  372.     else app->warningMsgHandler = _XtDefaultWarningMsg;
  373. #endif /* GLOBALERRORS */
  374.     return old;
  375. }
  376.  
  377. void _XtDefaultError(message)
  378.     String message;
  379. {
  380.     extern void exit();
  381.  
  382.     (void)fprintf(stderr, "%sError: %s\n", XTERROR_PREFIX, message);
  383.     exit(1);
  384. }
  385.  
  386. void _XtDefaultWarning(message)
  387.     String message;
  388. {
  389.     if (message && *message)
  390.        (void)fprintf(stderr, "%sWarning: %s\n", XTWARNING_PREFIX, message); 
  391.     return;
  392. }
  393.  
  394. #if NeedFunctionPrototypes
  395. void XtError(
  396.     _Xconst char* message
  397.     )
  398. #else
  399. void XtError(message)
  400.     String message;
  401. #endif
  402. {
  403. #if GLOBALERRORS
  404.     (*errorHandler)((String)message);
  405. #else
  406.     XtAppError(_XtDefaultAppContext(),message);
  407. #endif /* GLOBALERRORS */
  408. }
  409.  
  410. #if NeedFunctionPrototypes
  411. void XtAppError(
  412.     XtAppContext app,
  413.     _Xconst char* message
  414.     )
  415. #else
  416. void XtAppError(app,message)
  417.     XtAppContext app;
  418.     String message;
  419. #endif
  420. {
  421. #if GLOBALERRORS
  422.     (*errorHandler)((String)message);
  423. #else
  424.     (*app->errorHandler)(message);
  425. #endif /* GLOBALERRORS */
  426. }
  427.  
  428. #if NeedFunctionPrototypes
  429. void XtWarning(
  430.     _Xconst char* message
  431.     )
  432. #else
  433. void XtWarning(message)
  434.     String message;
  435. #endif
  436. {
  437. #if GLOBALERRORS
  438.     (*warningHandler)((String)message);
  439. #else
  440.     XtAppWarning(_XtDefaultAppContext(),message);
  441. #endif /* GLOBALERRORS */
  442. }
  443.  
  444. #if NeedFunctionPrototypes
  445. void XtAppWarning(
  446.     XtAppContext app,
  447.     _Xconst char* message
  448.     )
  449. #else
  450. void XtAppWarning(app,message)
  451.     XtAppContext app;
  452.     String message;
  453. #endif
  454. {
  455. #if GLOBALERRORS
  456.     (*warningHandler)((String)message);
  457. #else
  458.     (*app->warningHandler)(message);
  459. #endif /* GLOBALERRORS */
  460. }
  461.  
  462. void XtSetErrorHandler(handler)
  463.     XtErrorHandler handler;
  464. {
  465. #if GLOBALERRORS
  466.     if (handler != NULL) errorHandler = handler;
  467.     else errorHandler  = _XtDefaultError;
  468. #else
  469.     XtAppSetErrorHandler(_XtDefaultAppContext(),handler);
  470. #endif /* GLOBALERRORS */
  471. }
  472.  
  473. XtErrorHandler XtAppSetErrorHandler(app,handler)
  474.     XtAppContext app;
  475.     XtErrorHandler handler;
  476. {
  477.     XtErrorHandler old;
  478. #if GLOBALERRORS
  479.     old = errorHandler;
  480.     if (handler != NULL) errorHandler = handler;
  481.     else errorHandler  = _XtDefaultError;
  482. #else
  483.     old = app->errorHandler;
  484.     if (handler != NULL) app->errorHandler = handler;
  485.     else app->errorHandler  = _XtDefaultError;
  486. #endif /* GLOBALERRORS */
  487.     return old;
  488. }
  489.  
  490. void XtSetWarningHandler(handler)
  491.     XtErrorHandler handler;
  492. {
  493. #if GLOBALERRORS
  494.     if (handler != NULL) warningHandler = handler;
  495.     else warningHandler = _XtDefaultWarning;
  496. #else
  497.     XtAppSetWarningHandler(_XtDefaultAppContext(),handler);
  498. #endif /* GLOBALERRORS */
  499. }
  500.  
  501. XtErrorHandler XtAppSetWarningHandler(app,handler)
  502.     XtAppContext app;
  503.     XtErrorHandler handler;
  504. {
  505.     XtErrorHandler old;
  506. #if GLOBALERRORS
  507.     old = warningHandler;
  508.     if (handler != NULL) warningHandler  = handler;
  509.     else warningHandler = _XtDefaultWarning;
  510. #else
  511.     old = app->warningHandler;
  512.     if (handler != NULL) app->warningHandler  = handler;
  513.     else app->warningHandler = _XtDefaultWarning;
  514. #endif /* GLOBALERRORS */
  515.     return old;
  516. }
  517.  
  518. void _XtSetDefaultErrorHandlers(errMsg, warnMsg, err, warn)
  519.     XtErrorMsgHandler *errMsg, *warnMsg;
  520.     XtErrorHandler *err, *warn;
  521. {
  522. #ifndef GLOBALERRORS
  523.     *errMsg = _XtDefaultErrorMsg;
  524.     *warnMsg = _XtDefaultWarningMsg;
  525.     *err = _XtDefaultError;
  526.     *warn = _XtDefaultWarning;
  527. #endif /* GLOBALERRORS */
  528. }
  529.