This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathsystem-commandline.cpp
More file actions
294 lines (240 loc) · 7.51 KB
/
system-commandline.cpp
File metadata and controls
294 lines (240 loc) · 7.51 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* -*-c++-*-
Copyright (C) 2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode 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 General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "system-private.h"
#include <foundation-auto.h>
#if defined(__WINDOWS__)
# include <windows.h>
#endif
/* ---------------------------------------------------------------- */
/* The current command-line arguments. */
MCProperListRef s_arguments = NULL;
/* The current command name. */
MCStringRef s_name = NULL;
/* The current command filename. */
MCStringRef s_filename = NULL;
/* ================================================================
* File-local declarations
* ================================================================ */
#if defined(__WINDOWS__)
/* Windows-specific functions for getting the command line arguments
* encoded as UTF-16 rather than using the system codepage. */
static bool __MCSWindowsCommandLineGet (uindex_t &, unichar_t **&);
static void __MCSWindowsCommandLineFree (uindex_t &, unichar_t **& );
#endif /* __WINDOWS__ */
/* ================================================================
* Setters and getters
* ================================================================ */
MC_DLLEXPORT_DEF bool
MCSCommandLineGetArguments (MCProperListRef & r_arg_list)
{
if (NULL != s_arguments)
return MCProperListCopy (s_arguments, r_arg_list);
r_arg_list = MCValueRetain (kMCEmptyProperList);
return true;
}
MC_DLLEXPORT_DEF bool
MCSCommandLineSetArguments (MCProperListRef p_arg_list)
{
MCAssert (NULL != p_arg_list);
MCValueRelease (s_arguments);
s_arguments = MCValueRetain (p_arg_list);
return true;
}
MC_DLLEXPORT_DEF bool
MCSCommandLineGetName (MCStringRef & r_name)
{
if (NULL != s_name)
return MCStringCopy (s_name, r_name);
r_name = MCValueRetain (kMCEmptyString);
return true;
}
MC_DLLEXPORT_DEF bool
MCSCommandLineSetName (MCStringRef p_name)
{
MCAssert (NULL != p_name);
MCValueRelease (s_name);
s_name = MCValueRetain (p_name);
return true;
}
MC_DLLEXPORT_DEF bool
MCSCommandLineGetFilename (MCStringRef & r_filename)
{
if (NULL != s_filename)
return MCStringCopy (s_filename, r_filename);
r_filename = MCValueRetain (kMCEmptyString);
return true;
}
MC_DLLEXPORT_DEF bool
MCSCommandLineSetFilename (MCStringRef p_filename)
{
MCAssert (NULL != p_filename);
MCValueRelease (s_filename);
s_filename = MCValueRetain (p_filename);
return true;
}
/* ================================================================
* Set up initial values by capturing command line
* ================================================================ */
/* These __MCSCommandLineStringCreate() functions are used to decode
* the command line arguments into LiveCode native strings correctly
* for the platform. The assumptions are:
*
* - Windows: UTF-16
* - Linux: locale-dependent
* - All other platforms: UTF-8
*/
static inline bool
__MCSCommandLineStringCreate(const char *in, MCStringRef &out)
{
#if defined(__LINUX__)
return MCStringCreateWithSysString (in, out);
#else
return MCStringCreateWithBytes ((const byte_t *)in, strlen(in), kMCStringEncodingUTF8,
false, out);
#endif
}
static inline bool
__MCSCommandLineStringCreate(const unichar_t *in, MCStringRef &out)
{
return MCStringCreateWithWString (in, out);
}
/* The bulk of the command-line capture code is identical for all
* platforms: iterate over an array of nul-terminated strings and
* convert them into a proper list, differing only in the array type.
* This template is used to allow maximum commonality between
* platforms. */
template<typename T>
static bool
__MCSCommandLineCaptureNameAndArguments (uindex_t p_arg_count,
const T p_args[])
{
/* ---------- Command name */
MCAutoStringRef t_name;
if (0 < p_arg_count)
{
if (!__MCSCommandLineStringCreate (p_args[0], &t_name))
return false;
}
else
{
t_name = kMCEmptyString;
}
if (!MCSCommandLineSetName (*t_name))
return false;
/* ---------- Command arguments */
MCAutoProperListRef t_arg_list;
if (1 < p_arg_count)
{
const T *t_args_nonname = p_args + 1;
uindex_t t_arg_count_nonname = p_arg_count - 1;
MCAutoStringRefArray t_arg_strings;
if (!t_arg_strings.New (t_arg_count_nonname))
return false;
for (uindex_t i = 0; i < t_arg_count_nonname; ++i)
{
if (!__MCSCommandLineStringCreate (t_args_nonname[i],
t_arg_strings[i]))
return false;
}
if (!t_arg_strings.TakeAsProperList (&t_arg_list))
return false;
}
else
{
t_arg_list = kMCEmptyProperList;
}
if (!MCSCommandLineSetArguments (*t_arg_list))
return false;
return true;
}
/* ----------------------------------------------------------------
* Capture command information using C argument array
* ---------------------------------------------------------------- */
MC_DLLEXPORT_DEF bool
MCSCommandLineCapture (uindex_t p_arg_count, const char *p_arg_array[])
{
return
__MCSCommandLineCaptureNameAndArguments<>(p_arg_count, p_arg_array);
}
/* ================================================================
* Initialization
* ================================================================ */
bool
__MCSCommandLineInitialize (void)
{
s_arguments = NULL;
s_name = NULL;
return true;
}
void
__MCSCommandLineFinalize (void)
{
if (NULL != s_arguments)
{
MCValueRelease (s_arguments);
s_arguments = NULL;
}
if (NULL != s_name)
{
MCValueRelease (s_name);
s_name = NULL;
}
}
#if defined(__WINDOWS__)
/* ================================================================
* Windows-specific functions
* ================================================================ */
/* This is needed for CommandLineToArgvW */
#pragma comment(lib, "shell32.lib")
MC_DLLEXPORT_DEF bool
MCSCommandLineCaptureWindows (void)
{
bool t_success = true;
unichar_t **t_arg_array_w32 = NULL;
uindex_t t_arg_count_w32;
/* ---------- Get command name and arguments */
if (t_success)
t_success = __MCSWindowsCommandLineGet (t_arg_count_w32,
t_arg_array_w32);
if (t_success)
t_success = __MCSCommandLineCaptureNameAndArguments<>(t_arg_count_w32,
t_arg_array_w32);
__MCSWindowsCommandLineFree (t_arg_count_w32, t_arg_array_w32);
return t_success;
}
/* ----------------------------------------------------------------
* Get Unicode argument array on Windows
* ---------------------------------------------------------------- */
static bool
__MCSWindowsCommandLineGet (uindex_t & r_argc,
unichar_t **& r_argv)
{
LPCWSTR t_args_w32;
int t_arg_count_w32;
LPWSTR *t_arg_array_w32;
t_args_w32 = GetCommandLineW ();
t_arg_array_w32 = CommandLineToArgvW (t_args_w32, &t_arg_count_w32);
if (NULL == t_arg_array_w32)
return false; /* FIXME proper error */
r_argc = t_arg_count_w32;
r_argv = t_arg_array_w32;
return true;
}
static void
__MCSWindowsCommandLineFree (uindex_t& p_argc,
unichar_t **&p_argv)
{
if (p_argv != NULL)
LocalFree (p_argv);
}
#endif /* __WINDOWS__ */