home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xt / Error.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  14.2 KB  |  561 lines

  1. /* $XConsortium: Error.c,v 1.33 91/11/09 15:38:01 keith 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. #if NeedFunctionPrototypes
  333. XtErrorMsgHandler XtAppSetErrorMsgHandler(
  334.     XtAppContext app,
  335.     XtErrorMsgHandler handler)
  336. #else
  337. XtErrorMsgHandler XtAppSetErrorMsgHandler(app,handler)
  338.     XtAppContext app;
  339.     XtErrorMsgHandler handler;
  340. #endif
  341. {
  342.     XtErrorMsgHandler old;
  343. #if GLOBALERRORS
  344.     old = errorMsgHandler;
  345.     if (handler != NULL) errorMsgHandler = handler;
  346.     else errorMsgHandler  = _XtDefaultErrorMsg;
  347. #else
  348.     old = app->errorMsgHandler;
  349.     if (handler != NULL) app->errorMsgHandler = handler;
  350.     else app->errorMsgHandler  = _XtDefaultErrorMsg;
  351. #endif /* GLOBALERRORS */
  352.     return old;
  353. }
  354.  
  355. void XtSetWarningMsgHandler(handler)
  356.     XtErrorMsgHandler handler;
  357. {
  358. #if GLOBALERRORS
  359.     if (handler != NULL) warningMsgHandler  = handler;
  360.     else warningMsgHandler = _XtDefaultWarningMsg;
  361. #else
  362.     XtAppSetWarningMsgHandler(_XtDefaultAppContext(),handler);
  363. #endif /* GLOBALERRORS */
  364. }
  365.  
  366. #if NeedFunctionPrototypes
  367. XtErrorMsgHandler XtAppSetWarningMsgHandler(
  368.     XtAppContext app,
  369.     XtErrorMsgHandler handler)
  370. #else
  371. XtErrorMsgHandler XtAppSetWarningMsgHandler(app,handler)
  372.     XtAppContext app;
  373.     XtErrorMsgHandler handler;
  374. #endif
  375. {
  376.     XtErrorMsgHandler old;
  377. #if GLOBALERRORS
  378.     old = warningMsgHandler;
  379.     if (handler != NULL) warningMsgHandler  = handler;
  380.     else warningMsgHandler = _XtDefaultWarningMsg;
  381. #else
  382.     old = app->warningMsgHandler;
  383.     if (handler != NULL) app->warningMsgHandler  = handler;
  384.     else app->warningMsgHandler = _XtDefaultWarningMsg;
  385. #endif /* GLOBALERRORS */
  386.     return old;
  387. }
  388.  
  389. void _XtDefaultError(message)
  390.     String message;
  391. {
  392.     extern void exit();
  393.  
  394.     (void)fprintf(stderr, "%sError: %s\n", XTERROR_PREFIX, message);
  395.     exit(1);
  396. }
  397.  
  398. void _XtDefaultWarning(message)
  399.     String message;
  400. {
  401.     if (message && *message)
  402.        (void)fprintf(stderr, "%sWarning: %s\n", XTWARNING_PREFIX, message); 
  403.     return;
  404. }
  405.  
  406. #if NeedFunctionPrototypes
  407. void XtError(
  408.     _Xconst char* message
  409.     )
  410. #else
  411. void XtError(message)
  412.     String message;
  413. #endif
  414. {
  415. #if GLOBALERRORS
  416.     (*errorHandler)((String)message);
  417. #else
  418.     XtAppError(_XtDefaultAppContext(),message);
  419. #endif /* GLOBALERRORS */
  420. }
  421.  
  422. #if NeedFunctionPrototypes
  423. void XtAppError(
  424.     XtAppContext app,
  425.     _Xconst char* message
  426.     )
  427. #else
  428. void XtAppError(app,message)
  429.     XtAppContext app;
  430.     String message;
  431. #endif
  432. {
  433. #if GLOBALERRORS
  434.     (*errorHandler)((String)message);
  435. #else
  436.     (*app->errorHandler)(message);
  437. #endif /* GLOBALERRORS */
  438. }
  439.  
  440. #if NeedFunctionPrototypes
  441. void XtWarning(
  442.     _Xconst char* message
  443.     )
  444. #else
  445. void XtWarning(message)
  446.     String message;
  447. #endif
  448. {
  449. #if GLOBALERRORS
  450.     (*warningHandler)((String)message);
  451. #else
  452.     XtAppWarning(_XtDefaultAppContext(),message);
  453. #endif /* GLOBALERRORS */
  454. }
  455.  
  456. #if NeedFunctionPrototypes
  457. void XtAppWarning(
  458.     XtAppContext app,
  459.     _Xconst char* message
  460.     )
  461. #else
  462. void XtAppWarning(app,message)
  463.     XtAppContext app;
  464.     String message;
  465. #endif
  466. {
  467. #if GLOBALERRORS
  468.     (*warningHandler)((String)message);
  469. #else
  470.     (*app->warningHandler)(message);
  471. #endif /* GLOBALERRORS */
  472. }
  473.  
  474. #if NeedFunctionPrototypes
  475. void XtSetErrorHandler(XtErrorHandler handler)
  476. #else
  477. void XtSetErrorHandler(handler)
  478.     XtErrorHandler handler;
  479. #endif
  480. {
  481. #if GLOBALERRORS
  482.     if (handler != NULL) errorHandler = handler;
  483.     else errorHandler  = _XtDefaultError;
  484. #else
  485.     XtAppSetErrorHandler(_XtDefaultAppContext(),handler);
  486. #endif /* GLOBALERRORS */
  487. }
  488.  
  489. #if NeedFunctionPrototypes
  490. XtErrorHandler XtAppSetErrorHandler(
  491.     XtAppContext app,
  492.     XtErrorHandler handler)
  493. #else
  494. XtErrorHandler XtAppSetErrorHandler(app,handler)
  495.     XtAppContext app;
  496.     XtErrorHandler handler;
  497. #endif
  498. {
  499.     XtErrorHandler old;
  500. #if GLOBALERRORS
  501.     old = errorHandler;
  502.     if (handler != NULL) errorHandler = handler;
  503.     else errorHandler  = _XtDefaultError;
  504. #else
  505.     old = app->errorHandler;
  506.     if (handler != NULL) app->errorHandler = handler;
  507.     else app->errorHandler  = _XtDefaultError;
  508. #endif /* GLOBALERRORS */
  509.     return old;
  510. }
  511.  
  512. #if NeedFunctionPrototypes
  513. void XtSetWarningHandler(XtErrorHandler handler)
  514. #else
  515. void XtSetWarningHandler(handler)
  516.     XtErrorHandler handler;
  517. #endif
  518. {
  519. #if GLOBALERRORS
  520.     if (handler != NULL) warningHandler = handler;
  521.     else warningHandler = _XtDefaultWarning;
  522. #else
  523.     XtAppSetWarningHandler(_XtDefaultAppContext(),handler);
  524. #endif /* GLOBALERRORS */
  525. }
  526.  
  527. #if NeedFunctionPrototypes
  528. XtErrorHandler XtAppSetWarningHandler(
  529.     XtAppContext app,
  530.     XtErrorHandler handler)
  531. #else
  532. XtErrorHandler XtAppSetWarningHandler(app,handler)
  533.     XtAppContext app;
  534.     XtErrorHandler handler;
  535. #endif
  536. {
  537.     XtErrorHandler old;
  538. #if GLOBALERRORS
  539.     old = warningHandler;
  540.     if (handler != NULL) warningHandler  = handler;
  541.     else warningHandler = _XtDefaultWarning;
  542. #else
  543.     old = app->warningHandler;
  544.     if (handler != NULL) app->warningHandler  = handler;
  545.     else app->warningHandler = _XtDefaultWarning;
  546. #endif /* GLOBALERRORS */
  547.     return old;
  548. }
  549.  
  550. void _XtSetDefaultErrorHandlers(errMsg, warnMsg, err, warn)
  551.     XtErrorMsgHandler *errMsg, *warnMsg;
  552.     XtErrorHandler *err, *warn;
  553. {
  554. #ifndef GLOBALERRORS
  555.     *errMsg = _XtDefaultErrorMsg;
  556.     *warnMsg = _XtDefaultWarningMsg;
  557.     *err = _XtDefaultError;
  558.     *warn = _XtDefaultWarning;
  559. #endif /* GLOBALERRORS */
  560. }
  561.