Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 73106d6

Browse files
committed
system: Add support for accessing system error information.
Add the following LCB syntax: - `reset system error`: Reset system code to "none" (i.e. 0) - `the system error code`: The current numerical error code - `the system error message`: A string describing the current error code.
1 parent 70f336e commit 73106d6

File tree

7 files changed

+255
-3
lines changed

7 files changed

+255
-3
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# LiveCode Builder Standard Library
2+
## System error information
3+
4+
* Two new expressions have been added for accessing platform-specific
5+
system error status:
6+
7+
* `the system error code` evaluates to the current numerical system
8+
error code
9+
10+
* `the system error message` evaluates to a string describing the
11+
current system error
12+
13+
* The new `reset system error` statement clears the current system
14+
error.
15+

libfoundation/include/foundation-system.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* -*-c++-*-
2-
Copyright (C) 2015 LiveCode Ltd.
2+
Copyright (C) 2015-2016 LiveCode Ltd.
33
44
This file is part of LiveCode.
55
@@ -33,6 +33,7 @@ extern "C" {
3333
#include <system-library.h>
3434
#include <system-random.h>
3535
#include <system-stream.h>
36+
#include <system-error.h>
3637

3738
}
3839

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* -*-c++-*-
2+
Copyright (C) 2016 LiveCode Ltd.
3+
4+
This file is part of LiveCode.
5+
6+
LiveCode is free software; you can redistribute it and/or modify it under
7+
the terms of the GNU General Public License v3 as published by the Free
8+
Software Foundation.
9+
10+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
11+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
17+
18+
#if !defined(__MCS_SYSTEM_H_INSIDE__)
19+
# error "Only <foundation-system.h> can be included directly"
20+
#endif
21+
22+
/* ================================================================
23+
* System error handling
24+
* ================================================================ */
25+
26+
/* The MCSError library provides an API for accessing and resetting
27+
* the system error status. This is to support standardized use of
28+
* the POSIX and Win32 APIs.
29+
*/
30+
31+
/* Reset or clear the current system error state. */
32+
MC_DLLEXPORT void MCSErrorReset (void);
33+
34+
/* Get the platform-dependent numeric code corresponding to the
35+
* current system error. */
36+
MC_DLLEXPORT uint32_t MCSErrorGetCode (void);
37+
38+
/* Get a platform-dependent string describing a system error code. */
39+
MC_DLLEXPORT bool MCSErrorGetDescription (uint32_t p_code, MCStringRef& r_description);

libfoundation/libfoundation.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
'include/foundation-text.h',
6767
'include/foundation-unicode.h',
6868
'include/system-commandline.h',
69+
'include/system-error.h',
6970
'include/system-file.h',
7071
'include/system-init.h',
7172
'include/system-library.h',
@@ -112,6 +113,7 @@
112113
'src/foundation-objc.mm',
113114
'src/foundation-ffi-js.cpp',
114115
'src/system-commandline.cpp',
116+
'src/system-error.cpp',
115117
'src/system-file.cpp',
116118
'src/system-file-posix.cpp',
117119
'src/system-file-w32.cpp',

libfoundation/src/system-error.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* -*-c++-*-
2+
Copyright (C) 2016 LiveCode Ltd.
3+
4+
This file is part of LiveCode.
5+
6+
LiveCode is free software; you can redistribute it and/or modify it under
7+
the terms of the GNU General Public License v3 as published by the Free
8+
Software Foundation.
9+
10+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
11+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
17+
18+
#include "system-private.h"
19+
20+
#include <foundation-auto.h>
21+
22+
#if defined(__WINDOWS__)
23+
/* ================================================================
24+
* Windows implementations
25+
* ================================================================ */
26+
# include <windows.h>
27+
28+
MC_DLLEXPORT_DEF void
29+
MCSErrorReset ()
30+
{
31+
SetLastError (0);
32+
}
33+
34+
MC_DLLEXPORT_DEF uint32_t
35+
MCSErrorGetCode ()
36+
{
37+
return GetLastError ();
38+
}
39+
40+
MC_DLLEXPORT_DEF bool
41+
MCSErrorGetDescription (uint32_t p_code,
42+
MCStringRef& r_description)
43+
{
44+
DWORD t_code = GetLastError ();
45+
46+
/* No error has been recorded */
47+
if (0 == t_code)
48+
{
49+
return MCStringCopy (kMCEmptyString, r_description);
50+
}
51+
52+
LPWSTR t_message_buffer = nil;
53+
54+
DWORD t_message_len = FormatMessageW (
55+
(FORMAT_MESSAGE_ALLOCATE_BUFFER |
56+
FORMAT_MESSAGE_FROM_SYSTEM |
57+
FORMAT_MESSAGE_IGNORE_INSERTS), /* dwFlags */
58+
nil, /* lpSource */
59+
t_code, /* dwMessageId */
60+
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* dwLanguageId */
61+
reinterpret_cast<LPWSTR>(&t_message_buffer), /* lpBuffer */
62+
0, /* nSize */
63+
nil); /* arguments... */
64+
65+
bool t_success = true;
66+
MCAutoStringRef t_description;
67+
68+
if (t_success)
69+
t_success = MCStringCreateWithChars(t_message_buffer,
70+
t_message_len,
71+
&t_description);
72+
73+
LocalFree(t_message_buffer);
74+
75+
return MCStringCopy(*t_description, r_description);
76+
}
77+
78+
#else /* !__WINDOWS__ */
79+
/* ================================================================
80+
* POSIX implementations
81+
* ================================================================ */
82+
83+
#include <errno.h>
84+
85+
MC_DLLEXPORT_DEF void
86+
MCSErrorReset ()
87+
{
88+
errno = 0;
89+
}
90+
91+
MC_DLLEXPORT_DEF uint32_t
92+
MCSErrorGetCode ()
93+
{
94+
return errno;
95+
}
96+
97+
MC_DLLEXPORT_DEF bool
98+
MCSErrorGetDescription (uint32_t p_code,
99+
MCStringRef& r_description)
100+
{
101+
return MCStringCreateWithCString(strerror(p_code),
102+
r_description);
103+
}
104+
105+
#endif /* !__WINDOWS__ */

libscript/src/module-system.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* -*-c++-*-
2-
Copyright (C) 2015 LiveCode Ltd.
2+
Copyright (C) 2015-2016 LiveCode Ltd.
33
44
This file is part of LiveCode.
55
@@ -62,6 +62,22 @@ MCSystemExecGetCommandArguments (MCProperListRef & r_list)
6262
/* UNCHECKED */ MCSCommandLineGetArguments (r_list);
6363
}
6464

65+
/* ================================================================
66+
* System error information
67+
* ================================================================ */
68+
69+
extern "C" MC_DLLEXPORT_DEF void
70+
MCSystemEvalErrorCode (uint32_t & r_code)
71+
{
72+
r_code = MCSErrorGetCode ();
73+
}
74+
75+
extern "C" MC_DLLEXPORT_DEF void
76+
MCSystemEvalErrorDescription (MCStringRef & r_string)
77+
{
78+
/* UNCHECKED */ MCSErrorGetDescription (MCSErrorGetCode (), r_string);
79+
}
80+
6581
////////////////////////////////////////////////////////////////
6682

6783
extern "C" bool com_livecode_system_Initialize (void)

libscript/src/system.lcb

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2015 LiveCode Ltd.
2+
Copyright (C) 2015-2016 LiveCode Ltd.
33

44
This file is part of LiveCode.
55

@@ -161,6 +161,80 @@ begin
161161
MCSystemExecGetCommandArguments(output)
162162
end syntax
163163

164+
----------------------------------------------------------------
165+
-- System error information
166+
----------------------------------------------------------------
167+
168+
public foreign handler MCSErrorReset() returns nothing binds to "<builtin>"
169+
public foreign handler MCSystemEvalErrorCode(out Code as UInt32) returns nothing binds to "<builtin>"
170+
public foreign handler MCSystemEvalErrorDescription(out Description as String) returns nothing binds to "<builtin>"
171+
172+
173+
/**
174+
Summary: Clear the system error code.
175+
176+
Description:
177+
178+
Reset the system error code to its platform-dependent default ("no
179+
error") value.
180+
181+
Tags: System
182+
*/
183+
syntax ResetSystemError is statement
184+
"reset" "system" "error"
185+
begin
186+
MCSErrorReset()
187+
end syntax
188+
189+
/**
190+
Summary: The system error code.
191+
192+
Description:
193+
194+
Evaluates to the current platform-dependent system error code.
195+
196+
- On Windows, returns the result of `GetLastError()`.
197+
198+
- On other platforms, returns the current value of `errno`.
199+
200+
> **Note:** The system error code may be modified or cleared by any
201+
> syntax that interacts with the operating system (e.g. by performing
202+
> input or output). You should check the system error code as soon as
203+
> possible after any platform operation that might fail.
204+
205+
Related: SystemErrorDescription
206+
207+
Tags: System
208+
*/
209+
syntax SystemErrorCode is expression
210+
"the" "system" "error" "code"
211+
begin
212+
MCSystemEvalErrorCode(output)
213+
end syntax
214+
215+
/**
216+
Summary: The system error description.
217+
218+
Description:
219+
220+
Evaluates to a string describing the current platform-dependent system
221+
error code.
222+
223+
> **Note:** The system error code may be modified or cleared by any
224+
> syntax that interacts with the operating system (e.g. by performing
225+
> input or output). You should check the system error code as soon as
226+
> possible after any platform operation that might fail.
227+
228+
Related: SystemErrorCode
229+
230+
Tags: System
231+
*/
232+
syntax SystemErrorDescription is expression
233+
"the" "system" "error" "message"
234+
begin
235+
MCSystemEvalErrorDescription(output)
236+
end syntax
237+
164238
--
165239

166240
end module

0 commit comments

Comments
 (0)