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()
24 iState = EPerlDestroying;
26 iConsole->Printf(_L("[Any key to continue]"));
30 (void)perl_destruct(iPerl);
41 iConsoleBuffer = NULL;
43 #ifdef PERL_GLOBAL_STRUCT
45 PerlInterpreter* my_perl = NULL;
46 free_global_struct(iVars);
52 CPerlBase::~CPerlBase()
57 EXPORT_C CPerlBase* CPerlBase::NewInterpreterL(TBool aCloseStdlib,
58 void (*aStdioInitFunc)(void*),
59 void *aStdioInitCookie)
62 CPerlBase::NewInterpreterLC(aCloseStdlib,
65 CleanupStack::Pop(self);
69 EXPORT_C CPerlBase* CPerlBase::NewInterpreterLC(TBool aCloseStdlib,
70 void (*aStdioInitFunc)(void*),
71 void *aStdioInitCookie)
73 CPerlBase* self = new (ELeave) CPerlBase;
74 CleanupStack::PushL(self);
75 self->iCloseStdlib = aCloseStdlib;
76 self->iStdioInitFunc = aStdioInitFunc;
77 self->iStdioInitCookie = aStdioInitCookie;
79 PERL_APPCTX_SET(self);
83 static int _console_stdin(void* cookie, char* buf, int n)
85 return ((CPerlBase*)cookie)->ConsoleRead(0, buf, n);
88 static int _console_stdout(void* cookie, const char* buf, int n)
90 return ((CPerlBase*)cookie)->ConsoleWrite(1, buf, n);
93 static int _console_stderr(void* cookie, const char* buf, int n)
95 return ((CPerlBase*)cookie)->ConsoleWrite(2, buf, n);
98 void CPerlBase::StdioRewire(void *arg) {
99 _REENT->_sf[0]._cookie = (void*)this;
100 _REENT->_sf[0]._read = &_console_stdin;
101 _REENT->_sf[0]._write = 0;
102 _REENT->_sf[0]._seek = 0;
103 _REENT->_sf[0]._close = 0;
105 _REENT->_sf[1]._cookie = (void*)this;
106 _REENT->_sf[1]._read = 0;
107 _REENT->_sf[1]._write = &_console_stdout;
108 _REENT->_sf[1]._seek = 0;
109 _REENT->_sf[1]._close = 0;
111 _REENT->_sf[2]._cookie = (void*)this;
112 _REENT->_sf[2]._read = 0;
113 _REENT->_sf[2]._write = &_console_stderr;
114 _REENT->_sf[2]._seek = 0;
115 _REENT->_sf[2]._close = 0;
118 void CPerlBase::ConstructL()
121 #ifdef PERL_GLOBAL_STRUCT
122 PerlInterpreter *my_perl = 0;
123 iVars = init_global_struct();
124 User::LeaveIfNull(iVars);
126 iPerl = perl_alloc();
127 User::LeaveIfNull(iPerl);
128 iState = EPerlAllocated;
129 perl_construct(iPerl); // returns void
130 if (!iStdioInitFunc) {
132 Console::NewL(_L("Perl Console"),
133 TSize(KConsFullScreen, KConsFullScreen));
135 (TUint16*)malloc(sizeof(TUint) *
136 KPerlConsoleBufferMaxTChars);
137 User::LeaveIfNull(iConsoleBuffer);
140 iStdioInitFunc = &StdioRewire;
144 iStdioInitFunc(iStdioInitCookie);
147 iState = EPerlConstructed;
150 EXPORT_C PerlInterpreter* CPerlBase::GetInterpreter()
152 return (PerlInterpreter*) iPerl;
156 static void boot_DynaLoader(pTHX_ CV* cv) { }
158 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
161 static void xs_init(pTHX)
164 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__);
167 EXPORT_C TInt CPerlBase::RunScriptL(const TDesC& aFileName,
171 TBuf8<KMaxFileName> scriptUtf8;
173 error = CnvUtfConverter::ConvertFromUnicodeToUtf8(scriptUtf8, aFileName);
174 User::LeaveIfError(error);
175 char *filename = (char*)scriptUtf8.PtrZ();
177 if (stat(filename, &st) == -1)
180 return KErrGeneral; /* Anything better? */
181 char **Argv = (char**)malloc(argc * sizeof(char*));
182 User::LeaveIfNull(Argv);
183 TCleanupItem ArgvCleanupItem = TCleanupItem(free, Argv);
184 CleanupStack::PushL(ArgvCleanupItem);
186 if (argv && argc > 2)
187 for (int i = 2; i < argc - 1; i++)
189 Argv[argc - 1] = filename;
190 error = this->ParseAndRun(argc, Argv, envp);
191 CleanupStack::PopAndDestroy(Argv);
193 return error == 0 ? KErrNone : KErrGeneral;
197 EXPORT_C int CPerlBase::Parse(int argc, char *argv[], char *envp[])
199 if (iState == EPerlConstructed) {
200 const char* const NullArgv[] = { "perl", "-e", "0" };
201 if (argc == 0 || argv == 0) {
203 argv = (char**) NullArgv;
205 PERL_SYS_INIT(&argc, &argv);
206 int parsed = perl_parse(iPerl, xs_init, argc, argv, envp);
208 iState = EPerlParsed;
214 EXPORT_C void CPerlBase::SetupExit()
216 if (iState == EPerlParsed) {
218 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
219 // PL_perl_destruct level of 2 would be nice but
220 // it causes "Unbalanced scopes" for some reason.
221 PL_perl_destruct_level = 1;
225 EXPORT_C int CPerlBase::Run()
227 if (iState == EPerlParsed) {
229 iState = EPerlRunning;
230 int ran = perl_run(iPerl);
231 iState = (ran == 0) ? EPerlSuccess : EPerlFailure;
237 EXPORT_C int CPerlBase::ParseAndRun(int argc, char *argv[], char *envp[])
239 int parsed = Parse(argc, argv, envp);
240 int ran = (parsed == 0) ? Run() : -1;
244 int CPerlBase::ConsoleReadLine()
249 TUint currX = KPerlConsoleNoPos;
250 TUint currY = KPerlConsoleNoPos;
251 TUint prevX = KPerlConsoleNoPos;
252 TUint prevY = KPerlConsoleNoPos;
253 TUint maxX = KPerlConsoleNoPos;
257 TKeyCode code = iConsole->Getch();
259 if (code == EKeyLineFeed || code == EKeyEnter) {
260 if (offset < KPerlConsoleBufferMaxTChars) {
261 iConsoleBuffer[offset++] = '\n';
262 iConsole->Printf(_L("\n"));
263 iConsoleBuffer[offset++] = 0;
268 TBool doBackward = EFalse;
269 TBool doBackspace = EFalse;
273 if (code == EKeyBackspace) {
275 iConsoleBuffer[--offset] = 0;
280 else if (offset < KPerlConsoleBufferMaxTChars) {
281 TChar ch = TChar(code);
284 iConsoleBuffer[offset++] = (unsigned short)code;
285 iConsole->Printf(_L("%c"), code);
288 currX = iConsole->WhereX();
289 currY = iConsole->WhereY();
290 if (maxX == KPerlConsoleNoPos && prevX != KPerlConsoleNoPos &&
291 prevY != KPerlConsoleNoPos && currY == prevY + 1)
295 iConsole->SetPos(currX - 1);
297 iConsole->SetPos(maxX, currY - 1);
299 TUint nowX = iConsole->WhereX();
300 TUint nowY = iConsole->WhereY();
301 iConsole->Printf(_L(" ")); /* scrub */
302 iConsole->SetPos(nowX, nowY);
311 int CPerlBase::ConsoleRead(const int fd, char* buf, int n)
314 return iReadFunc(fd, buf, n);
329 TBuf8<4 * KPerlConsoleBufferMaxTChars> aBufferUtf8;
330 TBuf16<KPerlConsoleBufferMaxTChars> aBufferUtf16;
331 int length = ConsoleReadLine();
334 iConsoleUsed += length;
336 aBufferUtf16.SetLength(length);
337 for (i = 0; i < length; i++)
338 aBufferUtf16[i] = iConsoleBuffer[i];
339 aBufferUtf8.SetLength(4 * length);
341 CnvUtfConverter::ConvertFromUnicodeToUtf8(aBufferUtf8, aBufferUtf16);
343 char *pUtf8 = (char*)aBufferUtf8.PtrZ();
344 int nUtf8 = aBufferUtf8.Size();
346 nUtf8 = n; /* Potential data loss. */
347 #ifdef PERL_SYMBIAN_CONSOLE_UTF8
348 for (i = 0; i < nUtf8; i++)
352 for (i = 0; i < nUtf8; i+= UTF8SKIP(pUtf8 + i)) {
353 unsigned long u = utf8_to_uvchr((U8*)(pUtf8 + i), 0);
355 iConsole->Printf(_L("(keycode > 0xFF)\n"));
367 int CPerlBase::ConsoleWrite(const int fd, const char* buf, int n)
370 return iWriteFunc(fd, buf, n);
386 #ifdef PERL_SYMBIAN_CONSOLE_UTF8
388 if (is_utf8_string((U8*)buf, n)) {
389 for (int i = 0; i < n; i += UTF8SKIP(buf + i)) {
390 TChar u = utf8_to_uvchr((U8*)(buf + i), 0);
391 iConsole->Printf(_L("%c"), u);
395 iConsole->Printf(_L("(malformed utf8: "));
396 for (int i = 0; i < n; i++)
397 iConsole->Printf(_L("%02x "), buf[i]);
398 iConsole->Printf(_L(")\n"));
401 for (int i = 0; i < n; i++) {
402 iConsole->Printf(_L("%c"), buf[i]);
406 iConsoleUsed += wrote;