Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSvc / DtUtil2 / UErrNoBMS.c
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 librararies 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
45 #include <stdio.h>
46 #include <errno.h>
47 #include <string.h>
48 #include <Dt/MsgLog.h>
49 #include <Dt/MsgLogI.h>
50 #include <Dt/UserMsg.h>
51
52 #define MESSAGE_BUFFER          2048
53
54
55 static char * errno_string( 
56                         unsigned int errn, char *buff) ;
57 static void log_message( 
58                         char *progName,
59                         char *help,
60                         char *message,
61                         DtSeverity severity,
62                         int errnoset);
63
64
65 static char * 
66 errno_string(
67         unsigned int errn, char *buff)
68 {
69     char * s;
70     
71     switch (errn) {
72         
73     /* These are all in POSIX 1003.1 and/or X/Open XPG3     */
74     /* ---------------------------------------------------- */
75     case EPERM          : s = "EPERM";  break;
76     case ENOENT         : s = "ENOENT"; break;
77     case ESRCH          : s = "ESRCH";  break;
78     case EINTR          : s = "EINTR";  break;
79     case EIO            : s = "EIO";    break;
80     case ENXIO          : s = "ENXIO";  break;
81     case E2BIG          : s = "E2BIG";  break;
82     case ENOEXEC        : s = "ENOEXEC";break;
83     case EBADF          : s = "EBADF";  break;
84     case ECHILD         : s = "ECHILD"; break;
85     case EAGAIN         : s = "EAGAIN"; break;
86     case ENOMEM         : s = "ENOMEM"; break;
87     case EACCES         : s = "EACCES"; break;
88     case EFAULT         : s = "EFAULT"; break;
89     case ENOTBLK        : s = "ENOTBLK";break;
90     case EBUSY          : s = "EBUSY";  break;
91     case EEXIST         : s = "EEXIST"; break;
92     case EXDEV          : s = "EXDEV";  break;
93     case ENODEV         : s = "ENODEV"; break;
94     case ENOTDIR        : s = "ENOTDIR";break;
95     case EISDIR         : s = "EISDIR"; break;
96     case EINVAL         : s = "EINVAL"; break;
97     case ENFILE         : s = "ENFILE"; break;
98     case EMFILE         : s = "EMFILE"; break;
99     case ENOTTY         : s = "ENOTTY"; break;
100     case ETXTBSY        : s = "ETXTBSY";break;
101     case EFBIG          : s = "EFBIG";  break;
102     case ENOSPC         : s = "ENOSPC"; break;
103     case ESPIPE         : s = "ESPIPE"; break;
104     case EROFS          : s = "EROFS";  break;
105     case EMLINK         : s = "EMLINK"; break;
106     case EPIPE          : s = "EPIPE";  break;
107     case ENOMSG         : s = "ENOMSG"; break;
108     case EIDRM          : s = "EIDRM";  break;
109     case EDEADLK        : s = "EDEADLK";break;
110     case ENOLCK         : s = "ENOLCK"; break;
111 #ifndef _AIX
112     case ENOTEMPTY      : s = "ENOTEMPTY"; break;
113 #endif
114     case ENAMETOOLONG   : s = "ENAMETOOLONG"; break;
115     case ENOSYS         : s = "ENOSYS"; break;
116     default             : s = NULL;     break;
117     }
118     
119     if (s)
120         sprintf(buff, "%s (%d)",s,errn);
121     else
122         sprintf(buff, "(%d)",errn);
123     
124     return (buff);
125 }
126
127
128 /*
129  * This function calls the CDE message logging service.
130  */
131 static void 
132 log_message(
133         char *progName,
134         char *help,
135         char *message,
136         DtSeverity severity,
137         int errnoset )
138 {
139    char                 * errmsg = NULL;
140    char                 * errname = NULL;
141    char                 format[25];
142    DtMsgLogType         msg_type;
143    char                 buff[40];
144    
145    (void) strcpy (format, "%s");
146
147    if (help)
148       (void) strcat (format, "\n  %s");
149
150    if (errnoset) {
151       unsigned int      errn;
152
153       if (errnoset == TRUE)             /* Use "errno" from <errno.h> ? */
154          errn = errno;                  /* --- yep.                     */
155       else
156          errn = errnoset;               /* No, not the magic value, use parm */
157
158       errname = errno_string(errn, buff);
159
160       if (!(errmsg = strerror(errn)))
161          errmsg = "unknown";
162    } 
163
164    /*
165     * Must map the old message types to the new
166     */
167    switch (severity) {
168       case DtError:
169            DtFatalError:
170            DtInternalError: 
171                 msg_type = DtMsgLogError; break;
172       case DtIgnore:
173            DtInformation:
174                 msg_type = DtMsgLogInformation; break;
175       case DtWarning:
176                 msg_type = DtMsgLogWarning; break;
177       default: 
178                 msg_type = DtMsgLogError;
179    }
180
181    if (errmsg)
182       (void) strcat (format, "\n  [%s] %s");
183
184    if (help) {
185       if (errmsg)
186          DtMsgLogMessage (progName, msg_type, format, message, help, 
187                           errname, errmsg);
188       else
189          DtMsgLogMessage (progName, msg_type, format, message, help);
190    } else {
191       if (errmsg)
192          DtMsgLogMessage (progName, msg_type, format, message, errname, errmsg);
193       else
194          DtMsgLogMessage (progName, msg_type, format, message);
195    }
196 }
197
198
199 void 
200 _DtSimpleError(
201         char *progName,
202         DtSeverity severity,
203         char *help,
204         char *format,
205         ... )
206 {
207    va_list         args;
208    char            *message = (char*) malloc(MESSAGE_BUFFER);
209
210    if (NULL == message) return;
211
212    Va_start(args, format);
213 #if defined(USE_SNPRINTF)
214    (void) vsnprintf(message, MESSAGE_BUFFER, format, args);
215 #else
216    (void) vsprintf(message, format, args);
217 #endif
218    va_end(args);
219
220    log_message(progName, help, message, severity, FALSE);
221    if (message) free(message);
222 }
223
224 void 
225 _DtSimpleErrnoError(
226         char *progName,
227         DtSeverity severity,
228         char *help,
229         char *format,
230         ... )
231 {
232    va_list         args;
233    char            *message = (char*) malloc(MESSAGE_BUFFER);
234
235    if (NULL == message) return;
236
237    Va_start(args, format);
238 #if defined(USE_SNPRINTF)
239    (void) vsnprintf(message, MESSAGE_BUFFER, format, args);
240 #else
241    (void) vsprintf(message, format, args);
242 #endif
243    va_end(args);
244
245    log_message(progName, help, message, severity, TRUE);
246    if (message) free(message);
247 }