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::NewInterpreter(TBool aCloseStdlib,
59 void (*aStdioInitFunc)(void*),
60 void *aStdioInitCookie)
62 CPerlBase* self = new (ELeave) CPerlBase;
63 self->iCloseStdlib = aCloseStdlib;
64 self->iStdioInitFunc = aStdioInitFunc;
65 self->iStdioInitCookie = aStdioInitCookie;
67 PERL_APPCTX_SET(self);
71 EXPORT_C CPerlBase* CPerlBase::NewInterpreterL(TBool aCloseStdlib,
72 void (*aStdioInitFunc)(void*),
73 void *aStdioInitCookie)
76 CPerlBase::NewInterpreterLC(aCloseStdlib,
79 CleanupStack::Pop(self);
83 EXPORT_C CPerlBase* CPerlBase::NewInterpreterLC(TBool aCloseStdlib,
84 void (*aStdioInitFunc)(void*),
85 void *aStdioInitCookie)
87 CPerlBase* self = new (ELeave) CPerlBase;
88 CleanupStack::PushL(self);
89 self->iCloseStdlib = aCloseStdlib;
90 self->iStdioInitFunc = aStdioInitFunc;
91 self->iStdioInitCookie = aStdioInitCookie;
93 PERL_APPCTX_SET(self);
97 static int _console_stdin(void* cookie, char* buf, int n)
99 return ((CPerlBase*)cookie)->ConsoleRead(0, buf, n);
102 static int _console_stdout(void* cookie, const char* buf, int n)
104 return ((CPerlBase*)cookie)->ConsoleWrite(1, buf, n);
107 static int _console_stderr(void* cookie, const char* buf, int n)
109 return ((CPerlBase*)cookie)->ConsoleWrite(2, buf, n);
112 void CPerlBase::StdioRewire(void *arg) {
113 _REENT->_sf[0]._cookie = (void*)this;
114 _REENT->_sf[0]._read = &_console_stdin;
115 _REENT->_sf[0]._write = 0;
116 _REENT->_sf[0]._seek = 0;
117 _REENT->_sf[0]._close = 0;
119 _REENT->_sf[1]._cookie = (void*)this;
120 _REENT->_sf[1]._read = 0;
121 _REENT->_sf[1]._write = &_console_stdout;
122 _REENT->_sf[1]._seek = 0;
123 _REENT->_sf[1]._close = 0;
125 _REENT->_sf[2]._cookie = (void*)this;
126 _REENT->_sf[2]._read = 0;
127 _REENT->_sf[2]._write = &_console_stderr;
128 _REENT->_sf[2]._seek = 0;
129 _REENT->_sf[2]._close = 0;
132 void CPerlBase::ConstructL()
135 #ifdef PERL_GLOBAL_STRUCT
136 PerlInterpreter *my_perl = 0;
137 iVars = init_global_struct();
138 User::LeaveIfNull(iVars);
140 iPerl = perl_alloc();
141 User::LeaveIfNull(iPerl);
142 iState = EPerlAllocated;
143 perl_construct(iPerl); // returns void
145 PerlInterpreter *my_perl = iPerl;
146 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
148 if (!iStdioInitFunc) {
150 Console::NewL(_L("Perl Console"),
151 TSize(KConsFullScreen, KConsFullScreen));
153 (TUint16*)malloc(sizeof(TUint) *
154 KPerlConsoleBufferMaxTChars);
155 User::LeaveIfNull(iConsoleBuffer);
158 iStdioInitFunc = &StdioRewire;
162 iStdioInitFunc(iStdioInitCookie);
165 iState = EPerlConstructed;
168 EXPORT_C PerlInterpreter* CPerlBase::GetInterpreter()
170 return (PerlInterpreter*) iPerl;
174 static void boot_DynaLoader(pTHX_ CV* cv) { }
176 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
179 static void xs_init(pTHX)
182 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__);
185 EXPORT_C TInt CPerlBase::RunScriptL(const TDesC& aFileName,
189 TBuf8<KMaxFileName> scriptUtf8;
191 error = CnvUtfConverter::ConvertFromUnicodeToUtf8(scriptUtf8, aFileName);
192 User::LeaveIfError(error);
193 char *filename = (char*)scriptUtf8.PtrZ();
195 if (stat(filename, &st) == -1)
198 return KErrGeneral; /* Anything better? */
199 char **Argv = (char**)malloc(argc * sizeof(char*));
200 User::LeaveIfNull(Argv);
201 TCleanupItem ArgvCleanupItem = TCleanupItem(free, Argv);
202 CleanupStack::PushL(ArgvCleanupItem);
204 if (argv && argc > 2)
205 for (int i = 2; i < argc - 1; i++)
207 Argv[argc - 1] = filename;
208 error = this->ParseAndRun(argc, Argv, envp);
209 CleanupStack::PopAndDestroy(Argv);
211 return error == 0 ? KErrNone : KErrGeneral;
215 EXPORT_C int CPerlBase::Parse(int argc, char *argv[], char *envp[])
217 if (iState == EPerlConstructed) {
218 const char* const NullArgv[] = { "perl", "-e", "0" };
219 if (argc == 0 || argv == 0) {
221 argv = (char**) NullArgv;
223 PERL_SYS_INIT(&argc, &argv);
224 int parsed = perl_parse(iPerl, xs_init, argc, argv, envp);
226 iState = EPerlParsed;
232 EXPORT_C void CPerlBase::SetupExit()
234 if (iState == EPerlParsed) {
236 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
237 // PL_perl_destruct level of 2 would be nice but
238 // it causes "Unbalanced scopes" for some reason.
239 PL_perl_destruct_level = 1;
243 EXPORT_C int CPerlBase::Run()
245 if (iState == EPerlParsed) {
247 iState = EPerlRunning;
248 int ran = perl_run(iPerl);
249 iState = (ran == 0) ? EPerlSuccess : EPerlFailure;
255 EXPORT_C int CPerlBase::ParseAndRun(int argc, char *argv[], char *envp[])
257 int parsed = Parse(argc, argv, envp);
258 int ran = (parsed == 0) ? Run() : -1;
262 int CPerlBase::ConsoleReadLine()
267 TUint currX = KPerlConsoleNoPos;
268 TUint currY = KPerlConsoleNoPos;
269 TUint prevX = KPerlConsoleNoPos;
270 TUint prevY = KPerlConsoleNoPos;
271 TUint maxX = KPerlConsoleNoPos;
275 TKeyCode code = iConsole->Getch();
277 if (code == EKeyLineFeed || code == EKeyEnter) {
278 if (offset < KPerlConsoleBufferMaxTChars) {
279 iConsoleBuffer[offset++] = '\n';
280 iConsole->Printf(_L("\n"));
281 iConsoleBuffer[offset++] = 0;
286 TBool doBackward = EFalse;
287 TBool doBackspace = EFalse;
291 if (code == EKeyBackspace) {
293 iConsoleBuffer[--offset] = 0;
298 else if (offset < KPerlConsoleBufferMaxTChars) {
299 TChar ch = TChar(code);
302 iConsoleBuffer[offset++] = (unsigned short)code;
303 iConsole->Printf(_L("%c"), code);
306 currX = iConsole->WhereX();
307 currY = iConsole->WhereY();
308 if (maxX == KPerlConsoleNoPos && prevX != KPerlConsoleNoPos &&
309 prevY != KPerlConsoleNoPos && currY == prevY + 1)
313 iConsole->SetPos(currX - 1);
315 iConsole->SetPos(maxX, currY - 1);
317 TUint nowX = iConsole->WhereX();
318 TUint nowY = iConsole->WhereY();
319 iConsole->Printf(_L(" ")); /* scrub */
320 iConsole->SetPos(nowX, nowY);
329 int CPerlBase::ConsoleRead(const int fd, char* buf, int n)
332 return iReadFunc(fd, buf, n);
347 TBuf8<4 * KPerlConsoleBufferMaxTChars> aBufferUtf8;
348 TBuf16<KPerlConsoleBufferMaxTChars> aBufferUtf16;
349 int length = ConsoleReadLine();
352 iConsoleUsed += length;
354 aBufferUtf16.SetLength(length);
355 for (i = 0; i < length; i++)
356 aBufferUtf16[i] = iConsoleBuffer[i];
357 aBufferUtf8.SetLength(4 * length);
359 CnvUtfConverter::ConvertFromUnicodeToUtf8(aBufferUtf8, aBufferUtf16);
361 char *pUtf8 = (char*)aBufferUtf8.PtrZ();
362 int nUtf8 = aBufferUtf8.Size();
364 nUtf8 = n; /* Potential data loss. */
365 #ifdef PERL_SYMBIAN_CONSOLE_UTF8
366 for (i = 0; i < nUtf8; i++)
370 for (i = 0; i < nUtf8; i+= UTF8SKIP(pUtf8 + i)) {
371 unsigned long u = utf8_to_uvchr_buf((U8*)(pUtf8 + i),
372 (U8*)(pUtf8 + nUtf8),
375 iConsole->Printf(_L("(keycode > 0xFF)\n"));
387 int CPerlBase::ConsoleWrite(const int fd, const char* buf, int n)
390 return iWriteFunc(fd, buf, n);
406 #ifdef PERL_SYMBIAN_CONSOLE_UTF8
408 if (is_utf8_string((U8*)buf, n)) {
409 for (int i = 0; i < n; i += UTF8SKIP(buf + i)) {
410 TChar u = valid_utf8_to_uvchr((U8*)(buf + i), 0);
411 iConsole->Printf(_L("%c"), u);
415 iConsole->Printf(_L("(malformed utf8: "));
416 for (int i = 0; i < n; i++)
417 iConsole->Printf(_L("%02x "), buf[i]);
418 iConsole->Printf(_L(")\n"));
421 for (int i = 0; i < n; i++) {
422 iConsole->Printf(_L("%c"), buf[i]);
426 iConsoleUsed += wrote;