2
0

PrintXErr.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  25. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  26. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  27. * (c) Copyright 1993, 1994 Novell, Inc. *
  28. */
  29. /* Copyright Massachusetts Institute of Technology 1985, 1986, 1987 */
  30. /*
  31. * $TOG: PrintXErr.c /main/7 1998/04/10 07:46:38 mgreess $
  32. */
  33. /* ** (c) Copyright Hewlett-Packard Company, 1990.*/
  34. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  35. /*Lifted from xlib code. How to print a reasonably complete message */
  36. /*and NOT exit.*/
  37. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  38. /*$TOG: PrintXErr.c /main/7 1998/04/10 07:46:38 mgreess $*/
  39. #include <stdio.h>
  40. #include <X11/Xlib.h>
  41. #include <X11/Xlibint.h>
  42. #include <Dt/UserMsg.h>
  43. int
  44. _DtPrintDefaultError(
  45. Display *dpy,
  46. XErrorEvent *event,
  47. char *msg )
  48. {
  49. _DtPrintDefaultErrorSafe(dpy, event, msg, BUFSIZ);
  50. /* XXX retval? */
  51. /* COV: 87468 nothing actually checks this return value */
  52. return(1);
  53. }
  54. #define _DTP_STRNCAT(s1, s2, nb, ec) \
  55. { \
  56. strncat((s1),(s2),(nb)); \
  57. (nb)-=strlen(s2); \
  58. if (0>=(nb)) return (ec); \
  59. }
  60. int
  61. _DtPrintDefaultErrorSafe(
  62. Display *dpy,
  63. XErrorEvent *event,
  64. char *msg,
  65. int bytes)
  66. {
  67. char buffer[BUFSIZ];
  68. char fpBuf[BUFSIZ];
  69. char *fp;
  70. char mesg[BUFSIZ];
  71. char number[32];
  72. char *mtype = "XlibMessage";
  73. int nbytes = bytes-1;
  74. _XExtension *ext = (_XExtension *)NULL;
  75. memset(msg, 0, bytes);
  76. {
  77. XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
  78. XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
  79. _DTP_STRNCAT(msg, mesg, nbytes, event->error_code);
  80. _DTP_STRNCAT(msg, ": ", nbytes, event->error_code);
  81. _DTP_STRNCAT(msg, buffer, nbytes, event->error_code);
  82. _DTP_STRNCAT(msg, "\n ", nbytes, event->error_code);
  83. }
  84. {
  85. XGetErrorDatabaseText(
  86. dpy, mtype, "MajorCode", "Request Major code %d", mesg, BUFSIZ);
  87. if (strlen(mesg) < BUFSIZ-10)
  88. fp = fpBuf;
  89. else
  90. fp = malloc(strlen(mesg) + 10);
  91. (void) sprintf(fp, mesg, event->request_code);
  92. _DTP_STRNCAT(msg, fp, nbytes, event->error_code);
  93. if (fp != fpBuf) free(fp);
  94. }
  95. {
  96. if (event->request_code < 128)
  97. {
  98. sprintf(number, "%d", event->request_code);
  99. XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
  100. }
  101. else
  102. {
  103. for (ext = dpy->ext_procs;
  104. ext && (ext->codes.major_opcode != event->request_code);
  105. ext = ext->next)
  106. ;
  107. if (ext)
  108. {
  109. strncpy(buffer, ext->name, BUFSIZ-1);
  110. buffer[BUFSIZ-1] = '\0';
  111. }
  112. else
  113. buffer[0] = '\0';
  114. }
  115. _DTP_STRNCAT(msg, " (", nbytes, event->error_code);
  116. _DTP_STRNCAT(msg, buffer, nbytes, event->error_code);
  117. _DTP_STRNCAT(msg, ")\n ", nbytes, event->error_code);
  118. }
  119. {
  120. if (event->request_code >= 128)
  121. {
  122. XGetErrorDatabaseText(
  123. dpy, mtype, "MinorCode", "Request Minor code %d", mesg, BUFSIZ);
  124. if (strlen(mesg) < BUFSIZ-10)
  125. fp = fpBuf;
  126. else
  127. fp = malloc(strlen(mesg) + 10);
  128. (void) sprintf(fp, mesg, event->minor_code);
  129. _DTP_STRNCAT(msg, fp, nbytes, event->error_code);
  130. if (fp != fpBuf) free(fp);
  131. if (ext)
  132. {
  133. if (strlen(ext->name) < BUFSIZ-10)
  134. fp = fpBuf;
  135. else
  136. fp = malloc(strlen(ext->name) + 10);
  137. sprintf(fp, "%s.%d", ext->name, event->minor_code);
  138. XGetErrorDatabaseText(dpy, "XRequest", fp, "", buffer, BUFSIZ);
  139. if (fp != fpBuf) free(fp);
  140. }
  141. _DTP_STRNCAT(msg, "\n (", nbytes, event->error_code);
  142. _DTP_STRNCAT(msg, buffer, nbytes, event->error_code);
  143. _DTP_STRNCAT(msg, ")", nbytes, event->error_code);
  144. }
  145. }
  146. if (event->error_code >= 128)
  147. {
  148. /* kludge, try to find the extension that caused it */
  149. buffer[0] = '\0';
  150. for (ext = dpy->ext_procs; ext; ext = ext->next)
  151. {
  152. if (ext->error_string)
  153. (*ext->error_string)(dpy, event->error_code, &ext->codes,
  154. buffer, BUFSIZ);
  155. if (buffer[0])
  156. break;
  157. }
  158. if (buffer[0])
  159. {
  160. if (strlen(buffer) < BUFSIZ-10)
  161. sprintf(buffer,
  162. "%s.%d",
  163. ext->name,
  164. event->error_code - ext->codes.first_error);
  165. }
  166. else
  167. strcpy(buffer, "Value");
  168. XGetErrorDatabaseText(dpy, mtype, buffer, "Value 0x%x", mesg, BUFSIZ);
  169. if (*mesg)
  170. {
  171. if (strlen(mesg) < BUFSIZ-10)
  172. fp = fpBuf;
  173. else
  174. fp = malloc(strlen(mesg) + 10);
  175. (void) sprintf(fp, mesg, event->resourceid);
  176. _DTP_STRNCAT(msg, fp, nbytes, event->error_code);
  177. _DTP_STRNCAT(msg, "\n ", nbytes, event->error_code);
  178. if (fp != fpBuf) free(fp);
  179. }
  180. }
  181. else if ((event->error_code == BadWindow) ||
  182. (event->error_code == BadPixmap) ||
  183. (event->error_code == BadCursor) ||
  184. (event->error_code == BadFont) ||
  185. (event->error_code == BadDrawable) ||
  186. (event->error_code == BadColor) ||
  187. (event->error_code == BadGC) ||
  188. (event->error_code == BadIDChoice) ||
  189. (event->error_code == BadValue) ||
  190. (event->error_code == BadAtom))
  191. {
  192. if (event->error_code == BadValue)
  193. XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
  194. mesg, BUFSIZ);
  195. else if (event->error_code == BadAtom)
  196. XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
  197. mesg, BUFSIZ);
  198. else
  199. XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
  200. mesg, BUFSIZ);
  201. if (strlen(mesg) < BUFSIZ-10)
  202. fp = fpBuf;
  203. else
  204. fp = malloc(strlen(mesg) + 10);
  205. (void) sprintf(fp, mesg, event->resourceid);
  206. _DTP_STRNCAT(msg, fp, nbytes, event->error_code);
  207. _DTP_STRNCAT(msg, "\n ", nbytes, event->error_code);
  208. if (fp != fpBuf) free(fp);
  209. }
  210. {
  211. XGetErrorDatabaseText(
  212. dpy, mtype, "ErrorSerial", "Error Serial #%d", mesg, BUFSIZ);
  213. if (strlen(mesg) < BUFSIZ-10)
  214. fp = fpBuf;
  215. else
  216. fp = malloc(strlen(mesg) + 10);
  217. (void) sprintf(fp, mesg, event->serial);
  218. _DTP_STRNCAT(msg, fp, nbytes, event->error_code);
  219. _DTP_STRNCAT(msg, "\n ", nbytes, event->error_code);
  220. if (fp != fpBuf) free(fp);
  221. }
  222. {
  223. XGetErrorDatabaseText(
  224. dpy, mtype, "CurrentSerial", "Current Serial #%d", mesg, BUFSIZ);
  225. if (strlen(mesg) < BUFSIZ-10)
  226. fp = fpBuf;
  227. else
  228. fp = malloc(strlen(mesg) + 10);
  229. (void) sprintf(fp, mesg, dpy->request);
  230. _DTP_STRNCAT(msg, fp, nbytes, event->error_code);
  231. _DTP_STRNCAT(msg, "\n ", nbytes, event->error_code);
  232. if (fp != fpBuf) free(fp);
  233. }
  234. if (event->error_code == BadImplementation) return 0;
  235. return 1;
  236. }