1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/src/tclSymbianInit.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,546 @@
1.4 +// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// Provides a Symbian version of the main program and Tcl_AppInit
1.18 +// procedure for Tcl applications (without Tk).
1.19 +//
1.20 +//
1.21 +
1.22 +#include "tcl.h"
1.23 +#include "tclPort.h"
1.24 +#include "tclInt.h"
1.25 +#include "tclIntPlatDecls.h"
1.26 +#include <e32test.h>
1.27 +
1.28 +#ifdef __WINSCW__
1.29 +#include <e32std.h> //RPointerArray
1.30 +
1.31 +#include <pls.h> // For emulator WSD API
1.32 +const TUid KTCLDLLUid3 = {0}; // Must change
1.33 +const TInt KMaxDataKey = 10;
1.34 +#endif // __WINSCW__
1.35 +
1.36 +/*
1.37 + * The following macros convert between TclFile's and fd's. The conversion
1.38 + * simple involves shifting fd's up by one to ensure that no valid fd is ever
1.39 + * the same as NULL. Note that this code is duplicated from tclUnixPipe.c
1.40 + */
1.41 +
1.42 +#define MakeFile(fd) ((TclFile)((fd)+1))
1.43 +#define GetFd(file) (((int)file)-1)
1.44 +
1.45 +#ifdef __WINSCW__
1.46 +//The following code will run only on the emulator
1.47 +
1.48 +//Put the global count into a structure
1.49 +struct DLLData
1.50 +{
1.51 + // TCL globals
1.52 + char* tclExecutableName;
1.53 + char* tclNativeExecutableName;
1.54 +
1.55 + void* dataKey[KMaxDataKey];
1.56 + int inFinalize;
1.57 + int subsystemsInitialized;
1.58 + void* allocHead;
1.59 + void* defaultEncoding;
1.60 + void* systemEncoding;
1.61 + Tcl_HashTable encodingTable;
1.62 + SyncObjRecord keyRecord;
1.63 + Tcl_HashTable typeTable;
1.64 + int typeTableInitialized;
1.65 + int encodingsInitialized;
1.66 + char* tclDefaultEncodingDir;
1.67 + char* tclLibraryPathStr;
1.68 + int opTableInitialized;
1.69 + Tcl_HashTable opHashTable;
1.70 + Tcl_HashTable auxDataTypeTable;
1.71 + int auxDataTypeTableInitialized;
1.72 + void* cwdPathPtr;
1.73 + int cwdPathEpoch;
1.74 + void* refArray;
1.75 + int spaceAvl;
1.76 + int inUse;
1.77 + TclPlatformType tclPlatform;
1.78 + void* firstNotifierPtr;
1.79 +
1.80 + // Symbian globals
1.81 + char fileNames[8][L_tmpnam + 9];
1.82 +};
1.83 +
1.84 +//Initialization function
1.85 +TInt InitializeGlobals(DLLData* aData)
1.86 +{
1.87 + memset(aData, 0, sizeof(DLLData));
1.88 + aData->tclPlatform = TCL_PLATFORM_UNIX;
1.89 + return KErrNone;
1.90 +}
1.91 +
1.92 +//Define a way to access the structure
1.93 +//On the first call to this function, memory will be allocated with the specified
1.94 +//Uid as an identifier and the Initialization function will be called
1.95 +//Subsequent calls to this function return the allocated memory
1.96 +struct DLLData* GetGlobals()
1.97 +{
1.98 + return Pls<DLLData>(KTCLDLLUid3, InitializeGlobals);
1.99 +}
1.100 +
1.101 +//Clean up memory allocated for PLS used for storing globals
1.102 +int CleanupGlobals(void)
1.103 +{
1.104 + return FreePls(GetGlobals());
1.105 +}
1.106 +
1.107 +void* get_gFileName(int index)
1.108 +{
1.109 + return &(GetGlobals()->fileNames[index]);
1.110 +}
1.111 +
1.112 +char** get_tclExecutableName()
1.113 +{
1.114 + return &(GetGlobals()->tclExecutableName);
1.115 +}
1.116 +
1.117 +char** get_tclNativeExecutableName()
1.118 +{
1.119 + return &(GetGlobals()->tclNativeExecutableName);
1.120 +}
1.121 +
1.122 +void** get_dataKey(int index)
1.123 +{
1.124 + return &(GetGlobals()->dataKey[index]);
1.125 +}
1.126 +
1.127 +void* get_inFinalize()
1.128 +{
1.129 + return &(GetGlobals()->inFinalize);
1.130 +}
1.131 +
1.132 +void* get_subsystemsInitialized()
1.133 +{
1.134 + return &(GetGlobals()->subsystemsInitialized);
1.135 +}
1.136 +
1.137 +void** get_allocHead()
1.138 +{
1.139 + return &(GetGlobals()->allocHead);
1.140 +}
1.141 +
1.142 +void** get_defaultEncoding()
1.143 +{
1.144 + return &(GetGlobals()->defaultEncoding);
1.145 +}
1.146 +
1.147 +void** get_systemEncoding()
1.148 +{
1.149 + return &(GetGlobals()->systemEncoding);
1.150 +}
1.151 +
1.152 +void* get_encodingTable()
1.153 +{
1.154 + return &(GetGlobals()->encodingTable);
1.155 +}
1.156 +
1.157 +void* get_keyRecord()
1.158 +{
1.159 + return &(GetGlobals()->keyRecord);
1.160 +}
1.161 +
1.162 +void* get_typeTable()
1.163 +{
1.164 + return &(GetGlobals()->typeTable);
1.165 +}
1.166 +
1.167 +void* get_typeTableInitialized()
1.168 +{
1.169 + return &(GetGlobals()->typeTableInitialized);
1.170 +}
1.171 +
1.172 +void* get_encodingsInitialized()
1.173 +{
1.174 + return &(GetGlobals()->encodingsInitialized);
1.175 +}
1.176 +
1.177 +char** get_tclDefaultEncodingDir()
1.178 +{
1.179 + return &(GetGlobals()->tclDefaultEncodingDir);
1.180 +}
1.181 +
1.182 +char** get_tclLibraryPathStr()
1.183 +{
1.184 + return &(GetGlobals()->tclLibraryPathStr);
1.185 +}
1.186 +
1.187 +void* get_opTableInitialized()
1.188 +{
1.189 + return &(GetGlobals()->opTableInitialized);
1.190 +}
1.191 +
1.192 +void* get_opHashTable()
1.193 +{
1.194 + return &(GetGlobals()->opHashTable);
1.195 +}
1.196 +
1.197 +void* get_auxDataTypeTableInitialized()
1.198 +{
1.199 + return &(GetGlobals()->auxDataTypeTableInitialized);
1.200 +}
1.201 +
1.202 +void* get_auxDataTypeTable()
1.203 +{
1.204 + return &(GetGlobals()->auxDataTypeTable);
1.205 +}
1.206 +
1.207 +void** get_cwdPathPtr()
1.208 +{
1.209 + return &(GetGlobals()->cwdPathPtr);
1.210 +}
1.211 +
1.212 +void* get_cwdPathEpoch()
1.213 +{
1.214 + return &(GetGlobals()->cwdPathEpoch);
1.215 +}
1.216 +
1.217 +void** get_refArray()
1.218 +{
1.219 + return &(GetGlobals()->refArray);
1.220 +}
1.221 +
1.222 +void* get_spaceAvl()
1.223 +{
1.224 + return &(GetGlobals()->spaceAvl);
1.225 +}
1.226 +
1.227 +void* get_inUse()
1.228 +{
1.229 + return &(GetGlobals()->inUse);
1.230 +}
1.231 +
1.232 +/*
1.233 + *----------------------------------------------------------------------
1.234 + *
1.235 + * TclPlatformExit --
1.236 + *
1.237 + * This procedure implements the Symbian specific exit routine.
1.238 + * Modelled after Macintosh version.
1.239 + *
1.240 + * Results:
1.241 + * None.
1.242 + *
1.243 + * Side effects:
1.244 + * We exit the process.
1.245 + *
1.246 + *----------------------------------------------------------------------
1.247 + */
1.248 +
1.249 +void
1.250 +TclpExit(
1.251 + int status) /* Ignored. */
1.252 +{
1.253 + // Free the PLS
1.254 + CleanupGlobals();
1.255 +
1.256 + exit(status);
1.257 +}
1.258 +
1.259 +void* get_tclPlatform()
1.260 +{
1.261 + return &(GetGlobals()->tclPlatform);
1.262 +}
1.263 +
1.264 +void** get_firstNotifierPtr()
1.265 +{
1.266 + return &(GetGlobals()->firstNotifierPtr);
1.267 +}
1.268 +
1.269 +#else
1.270 +//Target device code
1.271 +char tmpFileName[L_tmpnam + 9];
1.272 +char fifoFileName[L_tmpnam + 9];
1.273 +char inFileName[L_tmpnam + 9];
1.274 +char outFileName[L_tmpnam + 9];
1.275 +char errFileName[L_tmpnam + 9];
1.276 +char inFileName1[L_tmpnam + 9];
1.277 +char outFileName1[L_tmpnam + 9];
1.278 +char errFileName1[L_tmpnam + 9];
1.279 +
1.280 +#endif
1.281 +
1.282 +#include "tclSymbianGlobals.h"
1.283 +
1.284 +#ifdef __cplusplus
1.285 +extern "C" {
1.286 +#endif
1.287 +
1.288 +#define ADDPARAMTOCHILD 4
1.289 +
1.290 +EXPORT_C void ChildProcessCleanup(int isChildProcess, int argc, char **argv)
1.291 +{
1.292 + RDebug::Print(_L("###TclSqlite3: Child process cleanup - begin. argc = %d.\r\n"), argc);
1.293 + TBuf<256> buf;
1.294 + for(TInt i=0;i<argc;++i)
1.295 + {
1.296 + TPtrC8 p((const unsigned char*)(argv[i]));
1.297 + buf.Copy(p);
1.298 + RDebug::Print(_L(" ### arg %d, value \"%S\"\r\n"), i, &buf);
1.299 + }
1.300 +
1.301 + // add fifo close & unlink
1.302 + if (isChildProcess == 1)
1.303 + {
1.304 + RDebug::Print(_L(" ### Unlink 0.\r\n"));
1.305 +
1.306 + TPtrC8 p1((const unsigned char*)tmpFileName);
1.307 + buf.Copy(p1);
1.308 + RDebug::Print(_L(" ### tmp file name \"%S\"\r\n"), &buf);
1.309 +
1.310 + TPtrC8 p2((const unsigned char*)fifoFileName);
1.311 + buf.Copy(p2);
1.312 + RDebug::Print(_L(" ### fifo file name \"%S\"\r\n"), &buf);
1.313 +
1.314 + TPtrC8 p3((const unsigned char*)inFileName);
1.315 + buf.Copy(p3);
1.316 + RDebug::Print(_L(" ### input file name \"%S\"\r\n"), &buf);
1.317 +
1.318 + TPtrC8 p4((const unsigned char*)outFileName);
1.319 + buf.Copy(p4);
1.320 + RDebug::Print(_L(" ### output file name \"%S\"\r\n"), &buf);
1.321 +
1.322 + TPtrC8 p5((const unsigned char*)errFileName);
1.323 + buf.Copy(p5);
1.324 + RDebug::Print(_L(" ### err file name \"%S\"\r\n"), &buf);
1.325 +
1.326 + RDebug::Print(_L(" ### Close stdin, stdout and stderr.\r\n"));
1.327 + close (TCL_STDIN);
1.328 + close (TCL_STDOUT);
1.329 + close (TCL_STDERR);
1.330 + for(TInt i=0, idx=argc-i-1; i<ADDPARAMTOCHILD && idx >= 0; ++i, --idx)
1.331 + {
1.332 + if(argv[idx])
1.333 + {
1.334 + TPtrC8 p((const unsigned char*)(argv[idx]));
1.335 + buf.Copy(p);
1.336 + RDebug::Print(_L(" ### Unlink. Arg %d. Value \"%S\".\r\n"), idx, &buf);
1.337 + unlink(argv[idx]);
1.338 + }
1.339 + }
1.340 + }
1.341 + else
1.342 + {
1.343 + RDebug::Print(_L(" ### Unlink 1.\r\n"));
1.344 +
1.345 + TPtrC8 p1((const unsigned char*)inFileName1);
1.346 + buf.Copy(p1);
1.347 + RDebug::Print(_L(" ### 1 input file name \"%S\"\r\n"), &buf);
1.348 +
1.349 + TPtrC8 p2((const unsigned char*)outFileName1);
1.350 + buf.Copy(p2);
1.351 + RDebug::Print(_L(" ### 1 output file name \"%S\"\r\n"), &buf);
1.352 +
1.353 + TPtrC8 p3((const unsigned char*)errFileName1);
1.354 + buf.Copy(p3);
1.355 + RDebug::Print(_L(" ### 1 err file name \"%S\"\r\n"), &buf);
1.356 +
1.357 + unlink(inFileName1);
1.358 + unlink(outFileName1);
1.359 + unlink(errFileName1);
1.360 + }
1.361 + RDebug::Print(_L("###TclSqlite3: Child process cleanup - end.\r\n"));
1.362 + }
1.363 +
1.364 +// Symbian main hook for tclappinit
1.365 +EXPORT_C int ChildProcessInit (int *argc, char ***argv)
1.366 +{
1.367 + //set the stdin,stdout,stderr to the child process. the fds pass to the posix_spawn() in argv
1.368 + TclFile inputFile = NULL;
1.369 + TclFile outputFile= NULL;
1.370 + TclFile errorFile = NULL;
1.371 + int joinThisError;
1.372 + int fd[4] = {0, 0, 0, 0};
1.373 + char errSpace[200 + TCL_INTEGER_SPACE];
1.374 + int anerr = 0;
1.375 + TBuf<256> buf;
1.376 +
1.377 + RDebug::Print(_L("###TclSqlite3: Child process init - begin. argc = %d.\r\n"), argc != NULL ? *argc : 0);
1.378 + if(argc)
1.379 + {
1.380 + for(TInt i=0;i<*argc;++i)
1.381 + {
1.382 + TPtrC8 p((const unsigned char*)((*argv)[i]));
1.383 + buf.Copy(p);
1.384 + RDebug::Print(_L(" ### arg %d, value \"%S\"\r\n"), i, &buf);
1.385 + }
1.386 + }
1.387 + //set the stdin,stdout,stderr and pipeid to the child process. the fds pass to the posix_spawn() in argv
1.388 + if (*argc >= 5)
1.389 + {
1.390 + // fifoFile
1.391 + RDebug::Print(_L(" ### Fifo file. Arg %d.\r\n"), *argc-4);
1.392 + if((*argv)[*argc-4])
1.393 + {
1.394 + fd[0] = open((*argv)[*argc-4],O_WRONLY);
1.395 + if (fd[0] == -1)
1.396 + {
1.397 + RDebug::Print(_L(" ### fd[0](fifoFile) errno is %d\r\n"), errno);
1.398 + }
1.399 + else
1.400 + {
1.401 + TPtrC8 p((const unsigned char*)((*argv)[*argc-4]));
1.402 + buf.Copy(p);
1.403 + RDebug::Print(_L(" ### fifoFile is \"%S\", fd[0] is %d\r\n"), &buf, fd[0]);
1.404 + }
1.405 + //fd = atoi((*argv)[*argc-1]);
1.406 + }
1.407 + else
1.408 + {
1.409 + RDebug::Print(_L(" ### Fifo file - (*argv)[*argc-4] is 0.\r\n"));
1.410 + //should add later
1.411 + }
1.412 + // inputFile
1.413 + RDebug::Print(_L(" ### Input file. Arg %d.\r\n"), *argc-3);
1.414 + if(((*argv)[*argc-3])&&(strcmp((*argv)[*argc-3],"STD")))
1.415 + {
1.416 + fd[3] = open((*argv)[*argc-3],O_RDONLY);
1.417 + inputFile = MakeFile(fd[3]);
1.418 + if (fd[3] == -1)
1.419 + {
1.420 + RDebug::Print(_L(" ### fd[3](inputFile) errno is %d\r\n"), errno);
1.421 + }
1.422 + else
1.423 + {
1.424 + TPtrC8 p((const unsigned char*)((*argv)[*argc-3]));
1.425 + buf.Copy(p);
1.426 + RDebug::Print(_L(" ### inputFile is \"%S\", fd[3] is %d\r\n"), &buf, fd[3]);
1.427 + }
1.428 + //inputFile = (TclFile) (atoi((*argv)[*argc-4]));
1.429 + }
1.430 + else
1.431 + {
1.432 + RDebug::Print(_L(" ### Input file - ((*argv)[*argc-3])&&(strcmp((*argv)[*argc-3],\"STD\")) is 0.\r\n"));
1.433 + //should add later
1.434 + }
1.435 + // outputFile
1.436 + RDebug::Print(_L(" ### Output file. Arg %d\r\n"), *argc-2);
1.437 + if(((*argv)[*argc-2])&&(strcmp((*argv)[*argc-2],"STD")))
1.438 + {
1.439 + fd[2] = open((*argv)[*argc-2],O_WRONLY);
1.440 + outputFile = MakeFile(fd[2]);
1.441 + if (fd[2] == -1)
1.442 + {
1.443 + RDebug::Print(_L(" ### fd[2](outputFile) errno is %d\r\n"), errno);
1.444 + }
1.445 + else
1.446 + {
1.447 + TPtrC8 p((const unsigned char*)((*argv)[*argc-2]));
1.448 + buf.Copy(p);
1.449 + RDebug::Print(_L(" ### outputFile is \"%S\", fd[2] is %d\r\n"), &buf, fd[2]);
1.450 + }
1.451 +
1.452 + //outputFile = (TclFile) (atoi((*argv)[*argc-3]));
1.453 + }
1.454 + else
1.455 + {
1.456 + RDebug::Print(_L(" ### Output file - ((*argv)[*argc-2])&&(strcmp((*argv)[*argc-2],\"STD\")) is 0.\r\n"));
1.457 + //should add later
1.458 + //outputFile = MakeFile(1);
1.459 + }
1.460 + // errorFile
1.461 + RDebug::Print(_L(" ### Error file. Arg %d\r\n"), *argc-1);
1.462 + if(((*argv)[*argc-1])&&(strcmp((*argv)[*argc-1],"STD")))
1.463 + {
1.464 + fd[1] = open((*argv)[*argc-1],O_WRONLY);
1.465 + errorFile = MakeFile(fd[1]);
1.466 + if (fd[1] == -1)
1.467 + {
1.468 + RDebug::Print(_L(" ### fd[1] errorFile errno is %d\r\n"), errno);
1.469 + }
1.470 + else
1.471 + {
1.472 + TPtrC8 p((const unsigned char*)((*argv)[*argc-1]));
1.473 + buf.Copy(p);
1.474 + RDebug::Print(_L(" ### errorFile is \"%S\", fd[1] is %d\r\n"), &buf, fd[1]);
1.475 + }
1.476 + //errorFile = (TclFile) (atoi((*argv)[*argc-2]));
1.477 + }
1.478 + else
1.479 + {
1.480 + RDebug::Print(_L(" ### Output file - ((*argv)[*argc-1])&&(strcmp((*argv)[*argc-1],\"STD\")) is 0.\r\n"));
1.481 + //should add later
1.482 + }
1.483 + //*argc = *argc-4;
1.484 +
1.485 + joinThisError = errorFile && (errorFile == outputFile);
1.486 +
1.487 + //fd = GetFd(errPipeOut);
1.488 +
1.489 + //
1.490 + // Set up stdio file handles for the child process.
1.491 + //
1.492 +
1.493 + if (!SetupStdFile(inputFile, TCL_STDIN)
1.494 + || !SetupStdFile(outputFile, TCL_STDOUT)
1.495 + || (!joinThisError && !SetupStdFile(errorFile, TCL_STDERR))
1.496 + || (joinThisError &&
1.497 + ((dup2(1,2) == -1) ||
1.498 + (fcntl(2, F_SETFD, 0) != 0))))
1.499 + //if (!SetupStdFile(errorFile, TCL_STDERR))
1.500 + {
1.501 + RDebug::Print(_L(" ### child process couldn't set up input/output, error: %d\r\n"), errno);
1.502 + sprintf(errSpace,"child process couldn't set up input/output, error: %d\r\n", errno);
1.503 + write(fd[0], errSpace, (size_t) strlen(errSpace));
1.504 + close(fd[0]);
1.505 + unlink((*argv)[*argc-4]);
1.506 + RDebug::Print(_L("###TclSqlite3: Child process init - end 1.\r\n"));
1.507 + _exit(1);
1.508 + }
1.509 +
1.510 + sprintf(errSpace,"OK\r\n");
1.511 + write(fd[0], errSpace, (size_t) strlen(errSpace));
1.512 + anerr = close(fd[0]);
1.513 + anerr = unlink((*argv)[*argc-4]);
1.514 + RDebug::Print(_L("###TclSqlite3: Child process init - end 2. anerr=%d.\r\n"), anerr);
1.515 + return 1;
1.516 + }
1.517 +
1.518 + RDebug::Print(_L("###TclSqlite3: Child process init - end 3.\r\n"));
1.519 + return 0;
1.520 +}
1.521 +
1.522 +void TclPrint1(const char* aFmt, const char* aStr)
1.523 + {
1.524 + TBuf<128> fmt;
1.525 + fmt.Copy(TPtrC8((const TUint8*)aFmt));
1.526 + TBuf<128> str;
1.527 + str.Copy(TPtrC8((const TUint8*)aStr));
1.528 + RDebug::Print(fmt, &str);
1.529 + }
1.530 +
1.531 +void TclPrint2(const char* aFmt, const char* aStr, int aNum)
1.532 + {
1.533 + TBuf<128> fmt;
1.534 + fmt.Copy(TPtrC8((const TUint8*)aFmt));
1.535 + TBuf<128> str;
1.536 + str.Copy(TPtrC8((const TUint8*)aStr));
1.537 + RDebug::Print(fmt, &str, aNum);
1.538 + }
1.539 +
1.540 +void TclPrint3(const char* aFmt)
1.541 + {
1.542 + TBuf<128> fmt;
1.543 + fmt.Copy(TPtrC8((const TUint8*)aFmt));
1.544 + RDebug::Print(fmt);
1.545 + }
1.546 +
1.547 +#ifdef __cplusplus
1.548 +}
1.549 +#endif