File: PyrUnixPrim.cpp

package info (click to toggle)
supercollider 1%3A3.7.0~repack-4%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 34,364 kB
  • sloc: cpp: 197,140; ansic: 72,013; lisp: 63,505; sh: 14,009; python: 1,992; perl: 766; makefile: 679; java: 677; xml: 326; yacc: 309; lex: 175; ruby: 173; objc: 65
file content (393 lines) | stat: -rw-r--r-- 9,535 bytes parent folder | download
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
/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
*/
/*

Primitives for Unix.

*/

#include <cstring>
#include <errno.h>
#include <signal.h>

#include "PyrPrimitive.h"
#include "PyrObject.h"
#include "PyrKernel.h"
#include "PyrSched.h"
#include "VMGlobals.h"
#include "GC.h"
#include "SC_RGen.h"
#include "SC_DirUtils.h"
#include "sc_popen.h"
#include "SCBase.h"

#include "SC_Lock.h"

#ifdef _WIN32
#include "SC_Win32Utils.h"
#else
#include <libgen.h>
#endif

extern bool compiledOK;
PyrSymbol* s_unixCmdAction;

int prString_System(struct VMGlobals *g, int numArgsPushed);
int prString_System(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a = g->sp;

	char cmdline[1024];
	int err = slotStrVal(a, cmdline, 1023);
	if (err) return err;

	int res = system(cmdline);
	SetInt(a, res);

	return errNone;
}

int prString_Basename(struct VMGlobals *g, int numArgsPushed);
int prString_Basename(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a = g->sp;

	char path[PATH_MAX];
	int err = slotStrVal(a, path, PATH_MAX);
	if (err) return err;

	char *basename0 = basename(path);

	int size = strlen(basename0);
	PyrString *strobj = newPyrStringN(g->gc, size, 0, true);
	memcpy(strobj->s, basename0, size);

	SetObject(a, strobj);

	return errNone;
}

int prString_Dirname(struct VMGlobals *g, int numArgsPushed);
int prString_Dirname(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a = g->sp;

	char path[PATH_MAX];
	int err = slotStrVal(a, path, PATH_MAX);
	if (err) return err;

	char *dirname0 = dirname(path);

	int size = strlen(dirname0);
	PyrString *strobj = newPyrStringN(g->gc, size, 0, true);
	memcpy(strobj->s, dirname0, size);

	SetObject(a, strobj);

	return errNone;
}

struct sc_process {
	pid_t pid;
	FILE *stream;
	bool postOutput;
};

static void string_popen_thread_func(struct sc_process *process)
{
	FILE *stream = process->stream;
	pid_t pid = process->pid;
	char buf[1024];

	while (process->postOutput) {
		char *string = fgets(buf, 1024, stream);
		if (!string) break;
		postText(string, strlen(string));
	}

	int res;
	res = sc_pclose(stream, pid);
	res = WEXITSTATUS(res);

	if(process->postOutput)
		postfl("RESULT = %d\n", res);

	free(process);

	gLangMutex.lock();
	if(compiledOK) {
		VMGlobals *g = gMainVMGlobals;
		g->canCallOS = true;
		++g->sp;  SetObject(g->sp, class_string);
		++g->sp; SetInt(g->sp, res);
		++g->sp; SetInt(g->sp, pid);
		runInterpreter(g, s_unixCmdAction, 3);
		g->canCallOS = false;
	}
	gLangMutex.unlock();
}

int prString_POpen(struct VMGlobals *g, int numArgsPushed);
int prString_POpen(struct VMGlobals *g, int numArgsPushed)
{
	struct sc_process *process;
	PyrSlot *a = g->sp - 1;
	PyrSlot *b = g->sp;
	int err;

	if (!isKindOfSlot(a, class_string)) return errWrongType;

	char *cmdline = (char*)malloc(slotRawObject(a)->size + 1);
	err = slotStrVal(a, cmdline, slotRawObject(a)->size + 1);
	if(err) {
		free(cmdline);
		return errFailed;
	}

#ifdef SC_IPHONE
	SetInt(a, 0);
	return errNone;
#endif

	process = (struct sc_process *)malloc(sizeof(struct sc_process));
	process->stream = sc_popen(cmdline, &process->pid, "r");
	setvbuf(process->stream, 0, _IONBF, 0);

	process->postOutput = IsTrue(b);

	free(cmdline);

	if(process->stream == NULL) {
		free(process);
		return errFailed;
	}

	thread thread(std::bind(string_popen_thread_func, process));
	thread.detach();

	SetInt(a, process->pid);
	return errNone;
}

int prPidRunning(VMGlobals *g, int numArgsPushed);
int prPidRunning(VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a;

	a = g->sp;

#ifdef _WIN32
	HANDLE handle;

	handle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, slotRawInt(a));
	if(handle) {
		unsigned long exitCode;

		if(GetExitCodeProcess(handle, &exitCode) == 0)
			SetFalse(a);
		else if(exitCode == STILL_ACTIVE)
			SetTrue(a);

		CloseHandle(handle);
	}
	else
		SetFalse(a);
#else
	if(kill(slotRawInt(a), 0) == 0)
		SetTrue(a);
	else
		SetFalse(a);
#endif

	return errNone;
}

int prUnix_Errno(struct VMGlobals *g, int numArgsPushed);
int prUnix_Errno(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a = g->sp;

	SetInt(a, errno);

	return errNone;
}

#include <time.h>

static void fillSlotsFromTime(PyrSlot * result, struct tm* tm, std::chrono::system_clock::time_point const & now)
{
	PyrSlot *slots = slotRawObject(result)->slots;

	SetInt(slots+0, tm->tm_year + 1900);
	SetInt(slots+1, tm->tm_mon + 1); // 0 based month ??
	SetInt(slots+2, tm->tm_mday);
	SetInt(slots+3, tm->tm_hour);
	SetInt(slots+4, tm->tm_min);
	SetInt(slots+5, tm->tm_sec);
	SetInt(slots+6, tm->tm_wday);
	SetFloat(slots+7, std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count() * 1.0e-9);
}

int prLocalTime(struct VMGlobals *g, int numArgsPushed)
{
	using namespace std::chrono;
	system_clock::time_point now = system_clock::now();
	time_t now_time_t = system_clock::to_time_t(now);
	struct tm* tm = localtime(&now_time_t);

	fillSlotsFromTime(g->sp, tm,  now);

	return errNone;
}

int prGMTime(struct VMGlobals *g, int numArgsPushed)
{
	using namespace std::chrono;
	system_clock::time_point now = system_clock::now();
	time_t now_time_t = system_clock::to_time_t(now);
	struct tm* tm = gmtime(&now_time_t);

	fillSlotsFromTime(g->sp, tm, now);
	return errNone;
}

int prAscTime(struct VMGlobals *g, int numArgsPushed);
int prAscTime(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a = g->sp;
	PyrSlot *slots = slotRawObject(a)->slots;

	if (IsNil(slots + 0)) {
		SetNil(a);
		return errNone;
	}

	struct tm tm0;

	if (slotIntVal(slots+0, &tm0.tm_year)) return errWrongType;
	tm0.tm_year -= 1900;
	if (slotIntVal(slots+1, &tm0.tm_mon)) return errWrongType;
	tm0.tm_mon -- ;
	if (slotIntVal(slots+2, &tm0.tm_mday)) return errWrongType;
	if (slotIntVal(slots+3, &tm0.tm_hour)) return errWrongType;
	if (slotIntVal(slots+4, &tm0.tm_min)) return errWrongType;
	if (slotIntVal(slots+5, &tm0.tm_sec)) return errWrongType;
	if (slotIntVal(slots+6, &tm0.tm_wday)) return errWrongType;

	const char *text = asctime(&tm0);

	int size = strlen(text) - 1; // Discard trailing newline
	PyrString *strobj = newPyrStringN(g->gc, size, 0, true);
	memcpy(strobj->s, text, size);

	SetObject(a, strobj);

	return errNone;
}

int prStrFTime(struct VMGlobals *g, int numArgsPushed);
int prStrFTime(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a = g->sp - 1;
	PyrSlot *b = g->sp;

	PyrSlot *slots = slotRawObject(a)->slots;

	if (IsNil(slots + 0)) {
		SetNil(a);
		return errNone;
	}

	struct tm tm0;

	if (slotIntVal(slots+0, &tm0.tm_year)) return errWrongType;
	tm0.tm_year -= 1900;
	if (slotIntVal(slots+1, &tm0.tm_mon)) return errWrongType;
	tm0.tm_mon --;
	if (slotIntVal(slots+2, &tm0.tm_mday)) return errWrongType;
	if (slotIntVal(slots+3, &tm0.tm_hour)) return errWrongType;
	if (slotIntVal(slots+4, &tm0.tm_min)) return errWrongType;
	if (slotIntVal(slots+5, &tm0.tm_sec)) return errWrongType;
	if (slotIntVal(slots+6, &tm0.tm_wday)) return errWrongType;

	char format[1024];
	if (slotStrVal(b, format, 1024)) return errWrongType;

	char buffer[1024];
	if (strftime(buffer, 1024, format, &tm0) != 0) {
		int size = strlen(buffer);
		PyrString *strobj = newPyrStringN(g->gc, size, 0, true);
		memcpy(strobj->s, buffer, size);

		SetObject(a, strobj);
	} else {
		error("could not convert the date to string with the give format");
		return errFailed;
	}
	return errNone;
}

int32 timeseed();

int prTimeSeed(struct VMGlobals *g, int numArgsPushed);
int prTimeSeed(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a = g->sp;
	SetInt(a, timeseed());
	return errNone;
}

int prGetPid(VMGlobals *g, int numArgsPushed);
int prGetPid(VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a = g->sp;
	SetInt(a,
#ifndef _WIN32
		getpid()
#else
		GetCurrentProcessId()
#endif
		);
	return errNone;
}


void initUnixPrimitives();
void initUnixPrimitives()
{
	int base, index = 0;

	base = nextPrimitiveIndex();

	s_unixCmdAction = getsym("doUnixCmdAction");

	definePrimitive(base, index++, "_String_System", prString_System, 1, 0);
	definePrimitive(base, index++, "_String_Basename", prString_Basename, 1, 0);
	definePrimitive(base, index++, "_String_Dirname", prString_Dirname, 1, 0);
	definePrimitive(base, index++, "_String_POpen", prString_POpen, 2, 0);
	definePrimitive(base, index++, "_Unix_Errno", prUnix_Errno, 1, 0);
	definePrimitive(base, index++, "_LocalTime", prLocalTime, 1, 0);
	definePrimitive(base, index++, "_GMTime", prGMTime, 1, 0);
	definePrimitive(base, index++, "_AscTime", prAscTime, 1, 0);
	definePrimitive(base, index++, "_prStrFTime", prStrFTime, 2, 0);
	definePrimitive(base, index++, "_TimeSeed", prTimeSeed, 1, 0);
	definePrimitive(base, index++, "_PidRunning", prPidRunning, 1, 0);
	definePrimitive(base, index++, "_GetPid", prGetPid, 1, 0);
}