1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/*
* Copyright (c) 2002-2011 by XMLVM.org
*
* Project Info: http://www.xmlvm.org
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include "xmlvm.h"
#include "xmlvm-hy.h"
const char* errorMessage (I_32 errorCode)
{
PortlibPTBuffers_t ptBuffers;
ptBuffers = hyport_tls_peek ();
if (0 == ptBuffers->errorMessageBufferSize)
{
ptBuffers->errorMessageBuffer = XMLVM_ATOMIC_MALLOC(HYERROR_DEFAULT_BUFFER_SIZE);
if (NULL == ptBuffers->errorMessageBuffer)
{
return "";
}
ptBuffers->errorMessageBufferSize = HYERROR_DEFAULT_BUFFER_SIZE;
}
/* Copy from OS to ptBuffers */
#if !defined(ZOS)
strerror_r(errorCode,
ptBuffers->errorMessageBuffer, ptBuffers->errorMessageBufferSize);
#else
/* Do not have strerror_r on z/OS so use port library function instead */
portLibrary->str_printf(portLibrary, ptBuffers->errorMessageBuffer, ptBuffers->errorMessageBufferSize, strerror(errorCode));
#endif /* ZOS */
ptBuffers->errorMessageBuffer[ptBuffers->errorMessageBufferSize - 1] = '\0';
return ptBuffers->errorMessageBuffer;
}
static const char* swapMessageBuffer (PortlibPTBuffers_t ptBuffers, const char *message)
{
char *tempBuffer = ptBuffers->reportedMessageBuffer;
U_32 tempBufferSize = ptBuffers->reportedMessageBufferSize;
if (message == NULL)
{
return "";
}
/* Can't swap unknown message buffer */
if (message != ptBuffers->errorMessageBuffer)
{
return message;
}
/* Save reported information */
ptBuffers->reportedErrorCode = ptBuffers->portableErrorCode;
ptBuffers->reportedMessageBuffer = ptBuffers->errorMessageBuffer;
ptBuffers->reportedMessageBufferSize = ptBuffers->errorMessageBufferSize;
if (tempBufferSize > 0)
{
tempBuffer[0] = '\0';
}
/* Clear pending fields ready for next error */
ptBuffers->portableErrorCode = 0;
ptBuffers->errorMessageBuffer = tempBuffer;
ptBuffers->errorMessageBufferSize = tempBufferSize;
return ptBuffers->reportedMessageBuffer;
}
void* hyport_tls_get ()
{
java_lang_Thread* curThread = (java_lang_Thread*) java_lang_Thread_currentThread__();
if (curThread->fields.java_lang_Thread.ptBuffers_ == JAVA_NULL) {
curThread->fields.java_lang_Thread.ptBuffers_ = XMLVM_MALLOC(sizeof(PortlibPTBuffers_struct));
XMLVM_BZERO(curThread->fields.java_lang_Thread.ptBuffers_, sizeof(PortlibPTBuffers_struct));
}
return curThread->fields.java_lang_Thread.ptBuffers_;
}
void* hyport_tls_peek ()
{
java_lang_Thread* curThread = (java_lang_Thread*) java_lang_Thread_currentThread__();
return curThread->fields.java_lang_Thread.ptBuffers_;
}
const char* hyerror_last_error_message ()
{
PortlibPTBuffers_t ptBuffers;
/* Was an error saved ? */
ptBuffers = hyport_tls_peek ();
if (NULL == ptBuffers)
{
return "";
}
/* New error ? */
if (ptBuffers->portableErrorCode != 0)
{
const char *message = NULL;
/* Customized message stored ? */
if (ptBuffers->errorMessageBufferSize > 0)
{
if ('\0' != ptBuffers->errorMessageBuffer[0])
{
message = ptBuffers->errorMessageBuffer;
}
}
/* Call a helper to get the last message from the OS. */
if (message == NULL)
{
message = errorMessage (ptBuffers->platformErrorCode);
}
/* Avoid overwrite by internal portlib errors */
return swapMessageBuffer (ptBuffers, message);
}
/* Previous message stored ? */
if (ptBuffers->reportedMessageBufferSize > 0)
{
if ('\0' != ptBuffers->reportedMessageBuffer[0])
{
return ptBuffers->reportedMessageBuffer;
}
}
/* No error. */
return "";
}
I_32 hyerror_last_error_number ()
{
PortlibPTBuffers_t ptBuffers;
/* get the buffers, return failure if not present */
ptBuffers = hyport_tls_peek ();
if (NULL == ptBuffers)
{
return 0;
}
/* New error ? */
if (ptBuffers->portableErrorCode != 0)
{
return ptBuffers->portableErrorCode;
}
else
{
return ptBuffers->reportedErrorCode;
}
}
I_32 hyerror_set_last_error (I_32 platformCode, I_32 portableCode)
{
PortlibPTBuffers_t ptBuffers;
/* get the buffers, allocate if necessary.
* Silently return if not present, what else would the caller do anyway?
*/
ptBuffers = hyport_tls_get ();
if (NULL == ptBuffers)
{
return portableCode;
}
/* Save the last error */
ptBuffers->platformErrorCode = platformCode;
ptBuffers->portableErrorCode = portableCode;
/* Overwrite any customized messages stored */
if (ptBuffers->errorMessageBufferSize > 0)
{
ptBuffers->errorMessageBuffer[0] = '\0';
}
return portableCode;
}
I_32 hyerror_set_last_error_with_message (I_32 portableCode, const char *errorMessage)
{
PortlibPTBuffers_t ptBuffers;
U_32 requiredSize;
/* get the buffers, allocate if necessary.
* Silently return if not present, what else would the caller do anyway?
*/
ptBuffers = hyport_tls_get ();
if (NULL == ptBuffers)
{
return portableCode;
}
/* Save the last error */
ptBuffers->platformErrorCode = -1;
ptBuffers->portableErrorCode = portableCode;
/* Store the message, allocate a bigger buffer if required. Keep the old buffer around
* just in case memory can not be allocated
*/
requiredSize = strlen (errorMessage) + 1;
requiredSize =
requiredSize <
HYERROR_DEFAULT_BUFFER_SIZE ? HYERROR_DEFAULT_BUFFER_SIZE : requiredSize;
if (requiredSize > ptBuffers->errorMessageBufferSize)
{
char *newBuffer = XMLVM_ATOMIC_MALLOC(requiredSize);
if (NULL != newBuffer)
{
if (ptBuffers->errorMessageBuffer != NULL)
{
XMLVM_FREE(ptBuffers->errorMessageBuffer);
}
ptBuffers->errorMessageBuffer = newBuffer;
ptBuffers->errorMessageBufferSize = requiredSize;
}
}
/* Save the message */
if (ptBuffers->errorMessageBufferSize > 0)
{
sprintf(ptBuffers->errorMessageBuffer, "%s", errorMessage);
ptBuffers->errorMessageBuffer[ptBuffers->errorMessageBufferSize - 1] = '\0';
}
return portableCode;
}
|