initial load of upstream version 1.06.32
[xmlrpc-c] / lib / libutil / error.c
1 /* Copyright information is at end of file */
2
3 #include "xmlrpc_config.h"
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdarg.h>
9
10 #include "xmlrpc-c/util_int.h"
11 #include "xmlrpc-c/util.h"
12
13
14 #define ERROR_BUFFER_SZ (256)
15
16 void
17 xmlrpc_assertion_failed(const char * const fileName,
18                         int          const lineNumber) {
19
20     fprintf(stderr, "%s:%d: assertion failed\n", fileName, lineNumber);
21     abort();
22 }
23
24
25
26 static const char * const default_fault_string =
27     "Not enough memory for error message";
28
29 void xmlrpc_env_init (xmlrpc_env* env)
30 {
31     XMLRPC_ASSERT(env != NULL);
32
33     env->fault_occurred = 0;
34     env->fault_code     = 0;
35     env->fault_string   = NULL;
36 }
37
38 void
39 xmlrpc_env_clean(xmlrpc_env * const envP) {
40
41     XMLRPC_ASSERT(envP != NULL);
42     XMLRPC_ASSERT(envP->fault_string != XMLRPC_BAD_POINTER);
43
44     /* env->fault_string may be one of three things:
45     **   1) a NULL pointer
46     **   2) a pointer to the default_fault_string
47     **   3) a pointer to a malloc'd fault string
48     ** If we have case (3), we'll need to free it. */
49     if (envP->fault_string && envP->fault_string != default_fault_string)
50         free(envP->fault_string);
51     envP->fault_string = XMLRPC_BAD_POINTER;
52 }
53
54
55
56 void 
57 xmlrpc_env_set_fault(xmlrpc_env * const envP,
58                      int          const faultCode, 
59                      const char * const faultDescription) {
60
61     XMLRPC_ASSERT(envP != NULL); 
62     XMLRPC_ASSERT(faultDescription != NULL);
63
64     /* Clean up any leftover pointers. */
65     xmlrpc_env_clean(envP);
66
67     envP->fault_occurred = 1;
68     envP->fault_code     = faultCode;
69
70     /* Try to copy the fault string. If this fails, use a default. */
71     envP->fault_string = strdup(faultDescription);
72     if (envP->fault_string == NULL)
73         envP->fault_string = (char *)default_fault_string;
74 }
75
76
77
78 static void
79 set_fault_formatted_v(xmlrpc_env * const envP,
80                       int          const code,
81                       const char * const format,
82                       va_list      const args) {
83
84     char buffer[ERROR_BUFFER_SZ];
85
86     vsnprintf(buffer, ERROR_BUFFER_SZ, format, args);
87
88     /* vsnprintf is guaranteed to terminate the buffer, but we're paranoid. */
89     buffer[ERROR_BUFFER_SZ - 1] = '\0';
90
91     /* Set the fault. */
92     xmlrpc_env_set_fault(envP, code, buffer);
93 }
94
95
96
97 void 
98 xmlrpc_env_set_fault_formatted(xmlrpc_env * const envP, 
99                                int          const code,
100                                const char * const format, 
101                                ...) {
102     va_list args;
103
104     XMLRPC_ASSERT(envP != NULL);
105     XMLRPC_ASSERT(format != NULL);
106
107     /* Print our error message to the buffer. */
108     va_start(args, format);
109     set_fault_formatted_v(envP, code, format, args);
110     va_end(args);
111 }
112
113
114
115 void
116 xmlrpc_faultf(xmlrpc_env * const envP,
117               const char * const format,
118               ...) {
119
120     va_list args;
121
122     XMLRPC_ASSERT(envP != NULL);
123     XMLRPC_ASSERT(format != NULL);
124
125     /* Print our error message to the buffer. */
126     va_start(args, format);
127     set_fault_formatted_v(envP, XMLRPC_INTERNAL_ERROR, format, args);
128     va_end(args);
129
130 }
131
132
133
134 /* Copyright (C) 2001 by First Peer, Inc. All rights reserved.
135 **
136 ** Redistribution and use in source and binary forms, with or without
137 ** modification, are permitted provided that the following conditions
138 ** are met:
139 ** 1. Redistributions of source code must retain the above copyright
140 **    notice, this list of conditions and the following disclaimer.
141 ** 2. Redistributions in binary form must reproduce the above copyright
142 **    notice, this list of conditions and the following disclaimer in the
143 **    documentation and/or other materials provided with the distribution.
144 ** 3. The name of the author may not be used to endorse or promote products
145 **    derived from this software without specific prior written permission. 
146 **  
147 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
148 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
149 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
150 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
151 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
152 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
153 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
154 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
155 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
156 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
157 ** SUCH DAMAGE. */
158