1 /* Copyright (c) 2004-2005 Nokia. All rights reserved. */
3 /* The CPerlBase class is licensed under the same terms as Perl itself. */
5 /* See PerlBase.pod for documentation. */
15 const TUint KPerlConsoleBufferMaxTChars = 0x0200;
16 const TUint KPerlConsoleNoPos = 0xffff;
18 CPerlBase::CPerlBase()
22 EXPORT_C void CPerlBase::Destruct()
25 iState = EPerlDestroying;
27 iConsole->Printf(_L("[Any key to continue]"));
31 (void)perl_destruct(iPerl);
42 iConsoleBuffer = NULL;
44 #ifdef PERL_GLOBAL_STRUCT
46 PerlInterpreter* my_perl = NULL;
47 free_global_struct(iVars);
53 CPerlBase::~CPerlBase()
58 EXPORT_C CPerlBase* CPerlBase::NewInterpreterL(TBool aCloseStdlib,
59 void (*aStdioInitFunc)(void*),
60 void *aStdioInitCookie)
63 CPerlBase::NewInterpreterLC(aCloseStdlib,
66 CleanupStack::Pop(self);
70 EXPORT_C CPerlBase* CPerlBase::NewInterpreterLC(TBool aCloseStdlib,
71 void (*aStdioInitFunc)(void*),
72 void *aStdioInitCookie)
74 CPerlBase* self = new (ELeave) CPerlBase;
75 CleanupStack::PushL(self);
76 self->iCloseStdlib = aCloseStdlib;
77 self->iStdioInitFunc = aStdioInitFunc;
78 self->iStdioInitCookie = aStdioInitCookie;
80 PERL_APPCTX_SET(self);
84 static int _console_stdin(void* cookie, char* buf, int n)
86 return ((CPerlBase*)cookie)->ConsoleRead(0, buf, n);
89 static int _console_stdout(void* cookie, const char* buf, int n)
91 return ((CPerlBase*)cookie)->ConsoleWrite(1, buf, n);
94 static int _console_stderr(void* cookie, const char* buf, int n)
96 return ((CPerlBase*)cookie)->ConsoleWrite(2, buf, n);
99 void CPerlBase::StdioRewire(void *arg) {
100 _REENT->_sf[0]._cookie = (void*)this;
101 _REENT->_sf[0]._read = &_console_stdin;
102 _REENT->_sf[0]._write = 0;
103 _REENT->_sf[0]._seek = 0;
104 _REENT->_sf[0]._close = 0;
106 _REENT->_sf[1]._cookie = (void*)this;
107 _REENT->_sf[1]._read = 0;
108 _REENT->_sf[1]._write = &_console_stdout;
109 _REENT->_sf[1]._seek = 0;
110 _REENT->_sf[1]._close = 0;
112 _REENT->_sf[2]._cookie = (void*)this;
113 _REENT->_sf[2]._read = 0;
114 _REENT->_sf[2]._write = &_console_stderr;
115 _REENT->_sf[2]._seek = 0;
116 _REENT->_sf[2]._close = 0;
119 void CPerlBase::ConstructL()
122 #ifdef PERL_GLOBAL_STRUCT
123 PerlInterpreter *my_perl = 0;
124 iVars = init_global_struct();
125 User::LeaveIfNull(iVars);
127 iPerl = perl_alloc();
128 User::LeaveIfNull(iPerl);
129 iState = EPerlAllocated;
130 perl_construct(iPerl); // returns void
131 if (!iStdioInitFunc) {
133 Console::NewL(_L("Perl Console"),
134 TSize(KConsFullScreen, KConsFullScreen));
136 (TUint16*)malloc(sizeof(TUint) *
137 KPerlConsoleBufferMaxTChars);
138 User::LeaveIfNull(iConsoleBuffer);
141 iStdioInitFunc = &StdioRewire;
145 iStdioInitFunc(iStdioInitCookie);
148 iState = EPerlConstructed;
151 EXPORT_C PerlInterpreter* CPerlBase::GetInterpreter()
153 return (PerlInterpreter*) iPerl;
157 static void boot_DynaLoader(pTHX_ CV* cv) { }
159 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
162 static void xs_init(pTHX)
165 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__);
168 EXPORT_C TInt CPerlBase::RunScriptL(const TDesC& aFileName,
172 TBuf8<KMaxFileName> scriptUtf8;
174 error = CnvUtfConverter::ConvertFromUnicodeToUtf8(scriptUtf8, aFileName);
175 User::LeaveIfError(error);
176 char *filename = (char*)scriptUtf8.PtrZ();
178 if (stat(filename, &st) == -1)
181 return KErrGeneral; /* Anything better? */
182 char **Argv = (char**)malloc(argc * sizeof(char*));
183 User::LeaveIfNull(Argv);
184 TCleanupItem ArgvCleanupItem = TCleanupItem(free, Argv);
185 CleanupStack::PushL(ArgvCleanupItem);
187 if (argv && argc > 2)
188 for (int i = 2; i < argc - 1; i++)
190 Argv[argc - 1] = filename;
191 error = this->ParseAndRun(argc, Argv, envp);
192 CleanupStack::PopAndDestroy(Argv);
194 return error == 0 ? KErrNone : KErrGeneral;
198 EXPORT_C int CPerlBase::Parse(int argc, char *argv[], char *envp[])
200 if (iState == EPerlConstructed) {
201 const char* const NullArgv[] = { "perl", "-e", "0" };
202 if (argc == 0 || argv == 0) {
204 argv = (char**) NullArgv;
206 PERL_SYS_INIT(&argc, &argv);
207 int parsed = perl_parse(iPerl, xs_init, argc, argv, envp);
209 iState = EPerlParsed;
215 EXPORT_C void CPerlBase::SetupExit()
217 if (iState == EPerlParsed) {
219 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
220 // PL_perl_destruct level of 2 would be nice but
221 // it causes "Unbalanced scopes" for some reason.
222 PL_perl_destruct_level = 1;
226 EXPORT_C int CPerlBase::Run()
228 if (iState == EPerlParsed) {
230 iState = EPerlRunning;
231 int ran = perl_run(iPerl);
232 iState = (ran == 0) ? EPerlSuccess : EPerlFailure;
238 EXPORT_C int CPerlBase::ParseAndRun(int argc, char *argv[], char *envp[])
240 int parsed = Parse(argc, argv, envp);
241 int ran = (parsed == 0) ? Run() : -1;
245 int CPerlBase::ConsoleReadLine()
250 TUint currX = KPerlConsoleNoPos;
251 TUint currY = KPerlConsoleNoPos;
252 TUint prevX = KPerlConsoleNoPos;
253 TUint prevY = KPerlConsoleNoPos;
254 TUint maxX = KPerlConsoleNoPos;
258 TKeyCode code = iConsole->Getch();
260 if (code == EKeyLineFeed || code == EKeyEnter) {
261 if (offset < KPerlConsoleBufferMaxTChars) {
262 iConsoleBuffer[offset++] = '\n';
263 iConsole->Printf(_L("\n"));
264 iConsoleBuffer[offset++] = 0;
269 TBool doBackward = EFalse;
270 TBool doBackspace = EFalse;
274 if (code == EKeyBackspace) {
276 iConsoleBuffer[--offset] = 0;
281 else if (offset < KPerlConsoleBufferMaxTChars) {
282 TChar ch = TChar(code);
285 iConsoleBuffer[offset++] = (unsigned short)code;
286 iConsole->Printf(_L("%c"), code);
289 currX = iConsole->WhereX();
290 currY = iConsole->WhereY();
291 if (maxX == KPerlConsoleNoPos && prevX != KPerlConsoleNoPos &&
292 prevY != KPerlConsoleNoPos && currY == prevY + 1)
296 iConsole->SetPos(currX - 1);
298 iConsole->SetPos(maxX, currY - 1);
300 TUint nowX = iConsole->WhereX();
301 TUint nowY = iConsole->WhereY();
302 iConsole->Printf(_L(" ")); /* scrub */
303 iConsole->SetPos(nowX, nowY);
312 int CPerlBase::ConsoleRead(const int fd, char* buf, int n)
315 return iReadFunc(fd, buf, n);
330 TBuf8<4 * KPerlConsoleBufferMaxTChars> aBufferUtf8;
331 TBuf16<KPerlConsoleBufferMaxTChars> aBufferUtf16;
332 int length = ConsoleReadLine();
335 iConsoleUsed += length;
337 aBufferUtf16.SetLength(length);
338 for (i = 0; i < length; i++)
339 aBufferUtf16[i] = iConsoleBuffer[i];
340 aBufferUtf8.SetLength(4 * length);
342 CnvUtfConverter::ConvertFromUnicodeToUtf8(aBufferUtf8, aBufferUtf16);
344 char *pUtf8 = (char*)aBufferUtf8.PtrZ();
345 int nUtf8 = aBufferUtf8.Size();
347 nUtf8 = n; /* Potential data loss. */
348 #ifdef PERL_SYMBIAN_CONSOLE_UTF8
349 for (i = 0; i < nUtf8; i++)
353 for (i = 0; i < nUtf8; i+= UTF8SKIP(pUtf8 + i)) {
354 unsigned long u = utf8_to_uvchr((U8*)(pUtf8 + i), 0);
356 iConsole->Printf(_L("(keycode > 0xFF)\n"));
368 int CPerlBase::ConsoleWrite(const int fd, const char* buf, int n)
371 return iWriteFunc(fd, buf, n);
387 #ifdef PERL_SYMBIAN_CONSOLE_UTF8
389 if (is_utf8_string((U8*)buf, n)) {
390 for (int i = 0; i < n; i += UTF8SKIP(buf + i)) {
391 TChar u = utf8_to_uvchr((U8*)(buf + i), 0);
392 iConsole->Printf(_L("%c"), u);
396 iConsole->Printf(_L("(malformed utf8: "));
397 for (int i = 0; i < n; i++)
398 iConsole->Printf(_L("%02x "), buf[i]);
399 iConsole->Printf(_L(")\n"));
402 for (int i = 0; i < n; i++) {
403 iConsole->Printf(_L("%c"), buf[i]);
407 iConsoleUsed += wrote;