/
IoSystem.c
530 lines (435 loc) · 13.5 KB
/
IoSystem.c
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// metadoc System category Core
// metadoc System copyright Steve Dekorte, 2002
// metadoc System license BSD revised
/*metadoc System description
Contains methods related to the IoVM.
*/
#include "IoSystem.h"
#include "IoNumber.h"
#include "IoMessage_parser.h"
#include "IoVersion.h"
#ifndef WIN32
#include "IoInstallPrefix.h"
#endif
#if defined(linux) || defined(__MINGW64__)
#include <unistd.h>
#endif
#if defined(unix) || defined(__APPLE__) || defined(__NetBSD__) || \
defined(__OpenBSD__)
#include <sys/utsname.h>
#if defined(__NetBSD__) || defined(__OpenBSD__)
#include <sys/param.h>
#endif
#if defined(HAVE_SYS_SYSCTL_H) && !defined(__GLIBC__)
#include <sys/sysctl.h>
#endif
#endif
//#define WIN32
#if defined(__CYGWIN__) || defined(_WIN32)
#include <windows.h>
#endif
#ifdef WIN32
#include <windows.h>
#define _fullpath(res, path, size) \
(GetFullPathName((path), (size), (res), NULL) ? (res) : NULL)
#ifndef __CYGWIN__
static void setenv(const char *varName, const char *value, int force) {
const char *safeValue;
char *buf;
if (!varName) {
return;
}
if (!value) {
safeValue = "";
} else {
safeValue = value;
}
// buffer for var and value plus '=' and the \0
buf = (char *)io_calloc(1, strlen(varName) + strlen(safeValue) + 2);
if (!buf) {
return;
}
strcpy(buf, varName);
strcat(buf, "=");
strcat(buf, safeValue);
_putenv(buf);
io_free(buf);
}
//#define setenv(k, v, o) SetEnvironmentVariable((k), (v)) // removed by james
// burgess #define getpid GetCurrentProcessId // removed by james burgess
//#define setenv(k, v, o) SetEnvironmentVariable((k), (v))
#endif
IO_METHOD(IoObject, installPrefix) {
char acPath[256];
char root[256];
if (GetModuleFileName(NULL, acPath, 256) != 0) {
// guaranteed file name of at least one character after path
strcpy(strrchr(acPath, '\\'), "\\.");
_fullpath(root, acPath, 256);
}
return IoState_symbolWithCString_(IOSTATE, root);
}
#endif
IoObject *IoSystem_proto(void *state) {
IoMethodTable methodTable[] = {
#ifdef WIN32
{"shellExecute", IoObject_shellExecute},
{"installPrefix", IoObject_installPrefix},
#else
{"daemon", IoObject_daemon},
#endif
{"errorNumber", IoObject_errorNumberDescription},
{"exit", IoObject_exit},
{"getEnvironmentVariable", IoObject_getEnvironmentVariable},
{"setEnvironmentVariable", IoObject_setEnvironmentVariable},
{"system", IoObject_system},
//{"memorySizeOfState", IoObject_memorySizeOfState},
//{"compactState", IoObject_compactState},
{"platform", IoObject_platform},
{"platformVersion", IoObject_platformVersion},
{"sleep", IoObject_sleep},
{"activeCpus", IoObject_activeCpus},
{"recycledObjectCount", IoObject_recycledObjectCount},
{"maxRecycledObjects", IoObject_maxRecycledObjects},
{"setMaxRecycledObjects", IoObject_setMaxRecycledObjects},
{"symbols", IoObject_symbols},
{"setLobby", IoObject_setLobby},
{"thisProcessPid", IoObject_thisProcessPid},
{NULL, NULL},
};
IoObject *self = IoObject_new(state);
IoObject_addMethodTable_(self, methodTable);
/*doc System version
Returns the Io version number.
*/
IoObject_setSlot_to_(self, IOSYMBOL("version"),
IOSYMBOL(IO_VERSION_STRING));
// IoObject_setSlot_to_(self, IOSYMBOL("distribution"), IOSYMBOL("Io"));
IoObject_setSlot_to_(self, IOSYMBOL("type"), IOSYMBOL("System"));
// IoState_registerProtoWithId_((IoState *)state, self, protoId);
/*doc System installPrefix
Returns the root path where io was installed. The default is /usr/local.
*/
#ifndef WIN32
IoObject_setSlot_to_(self, IOSYMBOL("installPrefix"),
IOSYMBOL(INSTALL_PREFIX));
#endif
return self;
}
/*doc System args
Returns the list of command line argument strings the program was run
with.
*/
/*
IO_METHOD(IoObject, errorNumber)
{
return IONUMBER(errno);
}
*/
#include <stdio.h>
#include <errno.h>
#ifdef WIN32
#include <shellapi.h>
#include "IoError.h"
#include <stdint.h>
IO_METHOD(IoObject, shellExecute) {
LPCTSTR operation;
LPCTSTR file;
LPCTSTR parameters;
LPCTSTR directory;
int displayFlag;
int64_t result;
operation = CSTRING(IoMessage_locals_symbolArgAt_(m, locals, 0));
file = CSTRING(IoMessage_locals_symbolArgAt_(m, locals, 1));
parameters = IoMessage_argCount(m) > 2
? CSTRING(IoMessage_locals_symbolArgAt_(m, locals, 2))
: NULL;
directory = IoMessage_argCount(m) > 3
? CSTRING(IoMessage_locals_symbolArgAt_(m, locals, 3))
: NULL;
displayFlag = IoMessage_argCount(m) > 4
? IoMessage_locals_intArgAt_(m, locals, 4)
: SW_SHOWNORMAL;
result = (int64_t)ShellExecute(NULL, operation, file, parameters, directory,
displayFlag);
if (result > 32) {
return self;
} else {
return (IoObject *)IoError_newWithMessageFormat_(
IOSTATE, "ShellExecute Error %i", result);
}
}
#else
#ifdef daemon
IO_METHOD(IoObject, daemon) {
/*doc System daemon(dontChroot, dontRedirectOutputStreams)
Become a daemon process. If dontChroot is false, the process will change
its directory to /. If dontRedirectOutputStreams is false, stdout and
stderr are redirected to /dev/null.
*/
if (daemon(IoMessage_locals_boolArgAt_(m, locals, 0),
IoMessage_locals_boolArgAt_(m, locals, 1))) {
IoState_error_(IOSTATE, self,
"Daemonize failed. See System errorNumberDescription.");
}
return self;
}
#else
IO_METHOD(IoObject, daemon) {
IoState_error_(IOSTATE, self, "no daemon function found on this platform.");
return self;
}
#endif
#endif
IO_METHOD(IoObject, errorNumberDescription) {
/*doc System errorNumber
Returns the C errno string.
*/
return errno ? IOSYMBOL(strerror(errno)) : IONIL(self);
}
IO_METHOD(IoObject, exit) {
/*doc System exit(optionalReturnCodeNumber)
Shutdown the IoState (io_free all objects) and return
control to the calling program (if any).
*/
int returnCode = 0;
if (IoMessage_argCount(m)) {
returnCode = IoMessage_locals_intArgAt_(m, locals, 0);
}
IoState_exit(IOSTATE, returnCode);
return self;
}
IO_METHOD(IoObject, getEnvironmentVariable) {
/*doc System getEnvironmentVariable(nameString)
Returns a string with the value of the environment
variable whose name is specified by nameString.
*/
IoSymbol *key = IoMessage_locals_symbolArgAt_(m, locals, 0);
char *s = getenv(CSTRING(key));
if (!s) {
return ((IoState *)IOSTATE)->ioNil;
}
return IoState_symbolWithCString_(IOSTATE, s);
}
IO_METHOD(IoObject, system) {
/*doc System system(aString)
Makes a system call and returns a Number for the return value.
*/
IoSymbol *s = IoMessage_locals_symbolArgAt_(m, locals, 0);
char *buf = NULL;
int result = 0;
buf = (char *)getcwd(buf, 1024);
// printf("CURDIR: [%s]\n", buf);
// printf("SYSTEM: [%s]\n", CSTRING(s));
result = system(CSTRING(s));
// printf("system result = %i\n", result);
#if !defined(_WIN32) || defined(__CYGWIN__)
result /= 256;
#endif
return IONUMBER(result);
}
IO_METHOD(IoObject, memorySizeOfState) {
/*doc Object memorySizeOfState
Returns the number of bytes in the IoState
(this may not include memory allocated by C libraries).
*/
return IONUMBER(0);
// return IONUMBER(IoState_memorySize(IOSTATE));
}
IO_METHOD(IoObject, compactState) {
/*doc Object compactState
Attempt to compact the memory of the IoState if possible.
*/
// IoState_compact(IOSTATE);
return self;
}
IO_METHOD(IoObject, setEnvironmentVariable) {
/*doc System setEnvironmentVariable(keyString, valueString)
Sets the environment variable keyString to the value valueString.
*/
// setenv() takes different args in different implementations
IoSymbol *key = IoMessage_locals_symbolArgAt_(m, locals, 0);
IoSymbol *value = IoMessage_locals_symbolArgAt_(m, locals, 1);
setenv(CSTRING(key), CSTRING(value), 1);
return self;
}
IO_METHOD(IoObject, platform) {
/*doc System platform
Returns a string description of the platform.
*/
char *platform = "Unknown";
#if defined(__CYGWIN__)
platform = "cygwin";
#elif defined(__MINGW32__)
platform = "mingw";
#elif defined(_WIN32)
OSVERSIONINFO os;
os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&os);
switch (os.dwPlatformId) {
case VER_PLATFORM_WIN32_WINDOWS:
switch (os.dwMinorVersion) {
case 0:
platform = "Windows 95";
break;
case 10:
platform = "Windows 98";
break;
case 90:
platform = "Windows ME";
break;
default:
platform = "Windows 9X";
break;
}
break;
case VER_PLATFORM_WIN32_NT:
if (os.dwMajorVersion == 3 || os.dwMajorVersion == 4) {
platform = "Windows NT";
} else if (os.dwMajorVersion == 5) {
switch (os.dwMinorVersion) {
case 0:
platform = "Windows 2000";
break;
case 1:
platform = "Windows XP";
break;
default:
platform = "Windows";
break;
}
} else {
platform = "Windows";
}
break;
default:
platform = "Windows";
}
#elif defined(unix) || defined(__APPLE__) || defined(__NetBSD__) || \
defined(__OpenBSD__)
/* Why Apple and NetBSD don't define 'unix' I'll never know. */
struct utsname os;
int ret = uname(&os);
if (ret == 0) {
platform = os.sysname;
}
#endif
return IoState_symbolWithCString_(IOSTATE, platform);
}
IO_METHOD(IoObject, platformVersion) {
char platformVersion[256];
/*doc System platformVersion
Returns the version id of the OS.
*/
#if defined(_WIN32)
OSVERSIONINFO os;
os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&os);
snprintf(platformVersion, sizeof(platformVersion) - 1, "%d.%d",
os.dwMajorVersion, os.dwMinorVersion);
#elif defined(unix) || defined(__APPLE__) || defined(__NetBSD__) || \
defined(__OpenBSD__)
/* Why Apple and NetBSD don't define 'unix' I'll never know. */
struct utsname os;
int ret = uname(&os);
if (ret == 0) {
snprintf(platformVersion, 256, "%s.%s", os.version, os.release);
// sprintf(platformVersion, (size_t)sizeof(platformVersion) - 1, (const
// char *)os.release);
}
#endif
return IoState_symbolWithCString_(IOSTATE, platformVersion);
}
IO_METHOD(IoObject, activeCpus) {
/*doc System activeCpus
Returns the number of active CPUs.
*/
int cpus = 1;
#if defined(CTL_HW)
int mib[2];
size_t len = sizeof(cpus);
mib[0] = CTL_HW;
#if defined(HW_AVAILCPU)
mib[1] = HW_AVAILCPU;
#elif defined(HW_NCPU)
mib[1] = HW_NCPU;
#else
#error
#endif
sysctl(mib, 2, &cpus, &len, NULL, 0);
#elif defined(_SC_NPROCESSORS_ONLN)
cpus = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined(_SC_NPROC_ONLN)
cpus = sysconf(_SC_NPROC_ONLN);
#elif defined(WIN32)
SYSTEM_INFO si;
GetSystemInfo(&si);
cpus = si.dwNumberOfProcessors;
#else
#error
#endif
return IONUMBER(cpus);
}
#include "PortableUsleep.h"
IO_METHOD(IoObject, sleep) {
/*doc System sleep(secondsNumber)
Performs a *blocking* sleep call for specified number of seconds.
*/
double seconds = IoMessage_locals_doubleArgAt_(m, locals, 0);
unsigned int microseconds = (seconds * 1000000);
while (microseconds > 999999) {
usleep(999999);
microseconds -= 999999;
}
usleep(microseconds);
return self;
}
IO_METHOD(IoObject, maxRecycledObjects) {
/*doc System maxRecycledObjects
Returns the max number of recycled objects used.
*/
return IONUMBER(IOSTATE->maxRecycledObjects);
}
IO_METHOD(IoObject, setMaxRecycledObjects) {
/*doc System setMaxRecycledObjects(aNumber)
Sets the max number of recycled objects used.
*/
size_t max = IoMessage_locals_sizetArgAt_(m, locals, 0);
IOSTATE->maxRecycledObjects = max;
return self;
}
IO_METHOD(IoObject, recycledObjectCount) {
/*doc System recycledObjectCount
Returns the current number of objects being held for recycling.
*/
return IONUMBER(List_size(IOSTATE->recycledObjects));
}
#include "IoList.h"
IO_METHOD(IoObject, symbols) {
/*doc System symbols
Returns a List containing all Symbols currently in the system.
*/
IoList *list = IoList_new(IOSTATE);
CHASH_FOREACH(IOSTATE->symbols, i, v, IoList_rawAppend_(list, v));
return list;
}
IO_METHOD(IoObject, setLobby) {
/*doc System setLobby(anObject)
Sets the root object of the garbage collector.
*/
IoObject *v = IoMessage_locals_valueArgAt_(m, locals, 0);
IoState_setLobby_(IOSTATE, v);
return self;
}
IO_METHOD(IoObject, thisProcessPid) {
/*doc System thisProcessPid()
Return the process id (pid) for this Io process.
*/
return IONUMBER(getpid());
}
/*doc System version
Returns a version number for Io.
*/
/*doc System distribution
Returns the Io distribution name as a string.
*/