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
|
/* Copyright (c) 2004-2005 Nokia. All rights reserved. */
/* The CPerlBase class is licensed under the same terms as Perl itself. */
/* See PerlBase.pod for documentation. */
#define PERLBASE_CPP
#include <e32cons.h>
#include <e32keys.h>
#include <utf.h>
#include "PerlBase.h"
const TUint KPerlConsoleBufferMaxTChars = 0x0200;
const TUint KPerlConsoleNoPos = 0xffff;
CPerlBase::CPerlBase()
{
}
EXPORT_C void CPerlBase::Destruct()
{
dTHX;
iState = EPerlDestroying;
if (iConsole) {
iConsole->Printf(_L("[Any key to continue]"));
iConsole->Getch();
}
if (iPerl) {
(void)perl_destruct(iPerl);
perl_free(iPerl);
iPerl = NULL;
PERL_SYS_TERM();
}
if (iConsole) {
delete iConsole;
iConsole = NULL;
}
if (iConsoleBuffer) {
free(iConsoleBuffer);
iConsoleBuffer = NULL;
}
#ifdef PERL_GLOBAL_STRUCT
if (iVars) {
PerlInterpreter* my_perl = NULL;
free_global_struct(iVars);
iVars = NULL;
}
#endif
}
CPerlBase::~CPerlBase()
{
Destruct();
}
EXPORT_C CPerlBase* CPerlBase::NewInterpreter(TBool aCloseStdlib,
void (*aStdioInitFunc)(void*),
void *aStdioInitCookie)
{
CPerlBase* self = new (ELeave) CPerlBase;
self->iCloseStdlib = aCloseStdlib;
self->iStdioInitFunc = aStdioInitFunc;
self->iStdioInitCookie = aStdioInitCookie;
self->ConstructL();
PERL_APPCTX_SET(self);
return self;
}
EXPORT_C CPerlBase* CPerlBase::NewInterpreterL(TBool aCloseStdlib,
void (*aStdioInitFunc)(void*),
void *aStdioInitCookie)
{
CPerlBase* self =
CPerlBase::NewInterpreterLC(aCloseStdlib,
aStdioInitFunc,
aStdioInitCookie);
CleanupStack::Pop(self);
return self;
}
EXPORT_C CPerlBase* CPerlBase::NewInterpreterLC(TBool aCloseStdlib,
void (*aStdioInitFunc)(void*),
void *aStdioInitCookie)
{
CPerlBase* self = new (ELeave) CPerlBase;
CleanupStack::PushL(self);
self->iCloseStdlib = aCloseStdlib;
self->iStdioInitFunc = aStdioInitFunc;
self->iStdioInitCookie = aStdioInitCookie;
self->ConstructL();
PERL_APPCTX_SET(self);
return self;
}
static int _console_stdin(void* cookie, char* buf, int n)
{
return ((CPerlBase*)cookie)->ConsoleRead(0, buf, n);
}
static int _console_stdout(void* cookie, const char* buf, int n)
{
return ((CPerlBase*)cookie)->ConsoleWrite(1, buf, n);
}
static int _console_stderr(void* cookie, const char* buf, int n)
{
return ((CPerlBase*)cookie)->ConsoleWrite(2, buf, n);
}
void CPerlBase::StdioRewire(void *arg) {
_REENT->_sf[0]._cookie = (void*)this;
_REENT->_sf[0]._read = &_console_stdin;
_REENT->_sf[0]._write = 0;
_REENT->_sf[0]._seek = 0;
_REENT->_sf[0]._close = 0;
_REENT->_sf[1]._cookie = (void*)this;
_REENT->_sf[1]._read = 0;
_REENT->_sf[1]._write = &_console_stdout;
_REENT->_sf[1]._seek = 0;
_REENT->_sf[1]._close = 0;
_REENT->_sf[2]._cookie = (void*)this;
_REENT->_sf[2]._read = 0;
_REENT->_sf[2]._write = &_console_stderr;
_REENT->_sf[2]._seek = 0;
_REENT->_sf[2]._close = 0;
}
void CPerlBase::ConstructL()
{
iState = EPerlNone;
#ifdef PERL_GLOBAL_STRUCT
PerlInterpreter *my_perl = 0;
iVars = init_global_struct();
User::LeaveIfNull(iVars);
#endif
iPerl = perl_alloc();
User::LeaveIfNull(iPerl);
iState = EPerlAllocated;
perl_construct(iPerl); // returns void
{
PerlInterpreter *my_perl = iPerl;
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
}
if (!iStdioInitFunc) {
iConsole =
Console::NewL(_L("Perl Console"),
TSize(KConsFullScreen, KConsFullScreen));
iConsoleBuffer =
(TUint16*)malloc(sizeof(TUint) *
KPerlConsoleBufferMaxTChars);
User::LeaveIfNull(iConsoleBuffer);
iConsoleUsed = 0;
#ifndef USE_PERLIO
iStdioInitFunc = &StdioRewire;
#endif
}
if (iStdioInitFunc)
iStdioInitFunc(iStdioInitCookie);
iReadFunc = NULL;
iWriteFunc = NULL;
iState = EPerlConstructed;
}
EXPORT_C PerlInterpreter* CPerlBase::GetInterpreter()
{
return (PerlInterpreter*) iPerl;
}
#ifdef PERL_MINIPERL
static void boot_DynaLoader(pTHX_ CV* cv) { }
#else
EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
#endif
static void xs_init(pTHX)
{
dXSUB_SYS;
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__);
}
EXPORT_C TInt CPerlBase::RunScriptL(const TDesC& aFileName,
int argc,
char **argv,
char *envp[]) {
TBuf8<KMaxFileName> scriptUtf8;
TInt error;
error = CnvUtfConverter::ConvertFromUnicodeToUtf8(scriptUtf8, aFileName);
User::LeaveIfError(error);
char *filename = (char*)scriptUtf8.PtrZ();
struct stat st;
if (stat(filename, &st) == -1)
return KErrNotFound;
if (argc < 2)
return KErrGeneral; /* Anything better? */
char **Argv = (char**)malloc(argc * sizeof(char*));
User::LeaveIfNull(Argv);
TCleanupItem ArgvCleanupItem = TCleanupItem(free, Argv);
CleanupStack::PushL(ArgvCleanupItem);
Argv[0] = "perl";
if (argv && argc > 2)
for (int i = 2; i < argc - 1; i++)
Argv[i] = argv[i];
Argv[argc - 1] = filename;
error = this->ParseAndRun(argc, Argv, envp);
CleanupStack::PopAndDestroy(Argv);
Argv = 0;
return error == 0 ? KErrNone : KErrGeneral;
}
EXPORT_C int CPerlBase::Parse(int argc, char *argv[], char *envp[])
{
if (iState == EPerlConstructed) {
const char* const NullArgv[] = { "perl", "-e", "0" };
if (argc == 0 || argv == 0) {
argc = 3;
argv = (char**) NullArgv;
}
PERL_SYS_INIT(&argc, &argv);
int parsed = perl_parse(iPerl, xs_init, argc, argv, envp);
if (parsed == 0)
iState = EPerlParsed;
return parsed;
} else
return -1;
}
EXPORT_C void CPerlBase::SetupExit()
{
if (iState == EPerlParsed) {
diTHX;
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
// PL_perl_destruct level of 2 would be nice but
// it causes "Unbalanced scopes" for some reason.
PL_perl_destruct_level = 1;
}
}
EXPORT_C int CPerlBase::Run()
{
if (iState == EPerlParsed) {
SetupExit();
iState = EPerlRunning;
int ran = perl_run(iPerl);
iState = (ran == 0) ? EPerlSuccess : EPerlFailure;
return ran;
} else
return -1;
}
EXPORT_C int CPerlBase::ParseAndRun(int argc, char *argv[], char *envp[])
{
int parsed = Parse(argc, argv, envp);
int ran = (parsed == 0) ? Run() : -1;
return ran;
}
int CPerlBase::ConsoleReadLine()
{
if (!iConsole)
return -EIO;
TUint currX = KPerlConsoleNoPos;
TUint currY = KPerlConsoleNoPos;
TUint prevX = KPerlConsoleNoPos;
TUint prevY = KPerlConsoleNoPos;
TUint maxX = KPerlConsoleNoPos;
TUint offset = 0;
for (;;) {
TKeyCode code = iConsole->Getch();
if (code == EKeyLineFeed || code == EKeyEnter) {
if (offset < KPerlConsoleBufferMaxTChars) {
iConsoleBuffer[offset++] = '\n';
iConsole->Printf(_L("\n"));
iConsoleBuffer[offset++] = 0;
}
break;
}
else {
TBool doBackward = EFalse;
TBool doBackspace = EFalse;
prevX = currX;
prevY = currY;
if (code == EKeyBackspace) {
if (offset > 0) {
iConsoleBuffer[--offset] = 0;
doBackward = ETrue;
doBackspace = ETrue;
}
}
else if (offset < KPerlConsoleBufferMaxTChars) {
TChar ch = TChar(code);
if (ch.IsPrint()) {
iConsoleBuffer[offset++] = (unsigned short)code;
iConsole->Printf(_L("%c"), code);
}
}
currX = iConsole->WhereX();
currY = iConsole->WhereY();
if (maxX == KPerlConsoleNoPos && prevX != KPerlConsoleNoPos &&
prevY != KPerlConsoleNoPos && currY == prevY + 1)
maxX = prevX;
if (doBackward) {
if (currX > 0)
iConsole->SetPos(currX - 1);
else if (currY > 0)
iConsole->SetPos(maxX, currY - 1);
if (doBackspace) {
TUint nowX = iConsole->WhereX();
TUint nowY = iConsole->WhereY();
iConsole->Printf(_L(" ")); /* scrub */
iConsole->SetPos(nowX, nowY);
}
}
}
}
return offset;
}
int CPerlBase::ConsoleRead(const int fd, char* buf, int n)
{
if (iReadFunc)
return iReadFunc(fd, buf, n);
if (!iConsole) {
errno = EIO;
return -1;
}
if (n < 0) {
errno = EINVAL;
return -1;
}
if (n == 0)
return 0;
TBuf8<4 * KPerlConsoleBufferMaxTChars> aBufferUtf8;
TBuf16<KPerlConsoleBufferMaxTChars> aBufferUtf16;
int length = ConsoleReadLine();
int i;
iConsoleUsed += length;
aBufferUtf16.SetLength(length);
for (i = 0; i < length; i++)
aBufferUtf16[i] = iConsoleBuffer[i];
aBufferUtf8.SetLength(4 * length);
CnvUtfConverter::ConvertFromUnicodeToUtf8(aBufferUtf8, aBufferUtf16);
char *pUtf8 = (char*)aBufferUtf8.PtrZ();
int nUtf8 = aBufferUtf8.Size();
if (nUtf8 > n)
nUtf8 = n; /* Potential data loss. */
#ifdef PERL_SYMBIAN_CONSOLE_UTF8
for (i = 0; i < nUtf8; i++)
buf[i] = pUtf8[i];
#else
dTHX;
for (i = 0; i < nUtf8; i+= UTF8SKIP(pUtf8 + i)) {
unsigned long u = utf8_to_uvchr_buf((U8*)(pUtf8 + i),
(U8*)(pUtf8 + nUtf8),
0);
if (u > 0xFF) {
iConsole->Printf(_L("(keycode > 0xFF)\n"));
buf[i] = 0;
return -1;
}
buf[i] = u;
}
#endif
if (nUtf8 < n)
buf[nUtf8] = 0;
return nUtf8;
}
int CPerlBase::ConsoleWrite(const int fd, const char* buf, int n)
{
if (iWriteFunc)
return iWriteFunc(fd, buf, n);
if (!iConsole) {
errno = EIO;
return -1;
}
if (n < 0) {
errno = EINVAL;
return -1;
}
if (n == 0)
return 0;
int wrote = 0;
#ifdef PERL_SYMBIAN_CONSOLE_UTF8
dTHX;
if (is_utf8_string((U8*)buf, n)) {
for (int i = 0; i < n; i += UTF8SKIP(buf + i)) {
TChar u = valid_utf8_to_uvchr((U8*)(buf + i), 0);
iConsole->Printf(_L("%c"), u);
wrote++;
}
} else {
iConsole->Printf(_L("(malformed utf8: "));
for (int i = 0; i < n; i++)
iConsole->Printf(_L("%02x "), buf[i]);
iConsole->Printf(_L(")\n"));
}
#else
for (int i = 0; i < n; i++) {
iConsole->Printf(_L("%c"), buf[i]);
}
wrote = n;
#endif
iConsoleUsed += wrote;
return n;
}
|