UErrNoBMS.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /* $TOG: UErrNoBMS.c /main/9 1998/04/09 17:50:11 mgreess $ */
  24. /*
  25. * (c) Copyright 1995 Digital Equipment Corporation.
  26. * (c) Copyright 1987, 1988, 1989, 1990, 1993, 1994, 1995 Hewlett-Packard Company
  27. * (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
  28. * (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
  29. * (c) Copyright 1993, 1994, 1995 Novell, Inc.
  30. * (c) Copyright 1995 FUJITSU LIMITED.
  31. * (c) Copyright 1995 Hitachi.
  32. */
  33. /*************************************<+>*************************************
  34. *****************************************************************************
  35. **
  36. ** File: UErrNoBMS.c
  37. **
  38. ** Project: DT
  39. **
  40. ** Description: This file contains the CDE 1.0 message logging functions
  41. **
  42. ****************************************************************************
  43. ************************************<+>*************************************/
  44. #include <stdio.h>
  45. #include <errno.h>
  46. #include <string.h>
  47. #include <Dt/MsgLog.h>
  48. #include <Dt/MsgLogI.h>
  49. #include <Dt/UserMsg.h>
  50. #define MESSAGE_BUFFER 2048
  51. static char * errno_string(
  52. unsigned int errn, char *buff) ;
  53. static void log_message(
  54. char *progName,
  55. char *help,
  56. char *message,
  57. DtSeverity severity,
  58. int errnoset);
  59. static char *
  60. errno_string(
  61. unsigned int errn, char *buff)
  62. {
  63. char * s;
  64. switch (errn) {
  65. /* These are all in POSIX 1003.1 and/or X/Open XPG3 */
  66. /* ---------------------------------------------------- */
  67. case EPERM : s = "EPERM"; break;
  68. case ENOENT : s = "ENOENT"; break;
  69. case ESRCH : s = "ESRCH"; break;
  70. case EINTR : s = "EINTR"; break;
  71. case EIO : s = "EIO"; break;
  72. case ENXIO : s = "ENXIO"; break;
  73. case E2BIG : s = "E2BIG"; break;
  74. case ENOEXEC : s = "ENOEXEC";break;
  75. case EBADF : s = "EBADF"; break;
  76. case ECHILD : s = "ECHILD"; break;
  77. case EAGAIN : s = "EAGAIN"; break;
  78. case ENOMEM : s = "ENOMEM"; break;
  79. case EACCES : s = "EACCES"; break;
  80. case EFAULT : s = "EFAULT"; break;
  81. case ENOTBLK : s = "ENOTBLK";break;
  82. case EBUSY : s = "EBUSY"; break;
  83. case EEXIST : s = "EEXIST"; break;
  84. case EXDEV : s = "EXDEV"; break;
  85. case ENODEV : s = "ENODEV"; break;
  86. case ENOTDIR : s = "ENOTDIR";break;
  87. case EISDIR : s = "EISDIR"; break;
  88. case EINVAL : s = "EINVAL"; break;
  89. case ENFILE : s = "ENFILE"; break;
  90. case EMFILE : s = "EMFILE"; break;
  91. case ENOTTY : s = "ENOTTY"; break;
  92. case ETXTBSY : s = "ETXTBSY";break;
  93. case EFBIG : s = "EFBIG"; break;
  94. case ENOSPC : s = "ENOSPC"; break;
  95. case ESPIPE : s = "ESPIPE"; break;
  96. case EROFS : s = "EROFS"; break;
  97. case EMLINK : s = "EMLINK"; break;
  98. case EPIPE : s = "EPIPE"; break;
  99. case ENOMSG : s = "ENOMSG"; break;
  100. case EIDRM : s = "EIDRM"; break;
  101. case EDEADLK : s = "EDEADLK";break;
  102. case ENOLCK : s = "ENOLCK"; break;
  103. #ifndef _AIX
  104. case ENOTEMPTY : s = "ENOTEMPTY"; break;
  105. #endif
  106. case ENAMETOOLONG : s = "ENAMETOOLONG"; break;
  107. case ENOSYS : s = "ENOSYS"; break;
  108. default : s = NULL; break;
  109. }
  110. if (s)
  111. sprintf(buff, "%s (%d)",s,errn);
  112. else
  113. sprintf(buff, "(%d)",errn);
  114. return (buff);
  115. }
  116. /*
  117. * This function calls the CDE message logging service.
  118. */
  119. static void
  120. log_message(
  121. char *progName,
  122. char *help,
  123. char *message,
  124. DtSeverity severity,
  125. int errnoset )
  126. {
  127. char * errmsg = NULL;
  128. char * errname = NULL;
  129. char format[25];
  130. DtMsgLogType msg_type;
  131. char buff[40];
  132. (void) strcpy (format, "%s");
  133. if (help)
  134. (void) strcat (format, "\n %s");
  135. if (errnoset) {
  136. unsigned int errn;
  137. if (errnoset == TRUE) /* Use "errno" from <errno.h> ? */
  138. errn = errno; /* --- yep. */
  139. else
  140. errn = errnoset; /* No, not the magic value, use parm */
  141. errname = errno_string(errn, buff);
  142. if (!(errmsg = strerror(errn)))
  143. errmsg = "unknown";
  144. }
  145. /*
  146. * Must map the old message types to the new
  147. */
  148. switch (severity) {
  149. case DtError:
  150. DtFatalError:
  151. DtInternalError:
  152. msg_type = DtMsgLogError; break;
  153. case DtIgnore:
  154. DtInformation:
  155. msg_type = DtMsgLogInformation; break;
  156. case DtWarning:
  157. msg_type = DtMsgLogWarning; break;
  158. default:
  159. msg_type = DtMsgLogError;
  160. }
  161. if (errmsg)
  162. (void) strcat (format, "\n [%s] %s");
  163. if (help) {
  164. if (errmsg)
  165. DtMsgLogMessage (progName, msg_type, format, message, help,
  166. errname, errmsg);
  167. else
  168. DtMsgLogMessage (progName, msg_type, format, message, help);
  169. } else {
  170. if (errmsg)
  171. DtMsgLogMessage (progName, msg_type, format, message, errname, errmsg);
  172. else
  173. DtMsgLogMessage (progName, msg_type, format, message);
  174. }
  175. }
  176. void
  177. _DtSimpleError(
  178. char *progName,
  179. DtSeverity severity,
  180. char *help,
  181. char *format,
  182. ... )
  183. {
  184. va_list args;
  185. char *message = (char*) malloc(MESSAGE_BUFFER);
  186. if (NULL == message) return;
  187. Va_start(args, format);
  188. (void) vsnprintf(message, MESSAGE_BUFFER, format, args);
  189. va_end(args);
  190. log_message(progName, help, message, severity, FALSE);
  191. if (message) free(message);
  192. }
  193. void
  194. _DtSimpleErrnoError(
  195. char *progName,
  196. DtSeverity severity,
  197. char *help,
  198. char *format,
  199. ... )
  200. {
  201. va_list args;
  202. char *message = (char*) malloc(MESSAGE_BUFFER);
  203. if (NULL == message) return;
  204. Va_start(args, format);
  205. (void) vsnprintf(message, MESSAGE_BUFFER, format, args);
  206. va_end(args);
  207. log_message(progName, help, message, severity, TRUE);
  208. if (message) free(message);
  209. }