persistentstorage/sqlite3api/TEST/TCL/src/tclSymbianInit.cpp
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Provides a Symbian version of the main program and Tcl_AppInit
       
    15 // procedure for Tcl applications (without Tk).
       
    16 // 
       
    17 //
       
    18 
       
    19 #include "tcl.h"
       
    20 #include "tclPort.h"
       
    21 #include "tclInt.h"
       
    22 #include "tclIntPlatDecls.h"
       
    23 #include <e32test.h>
       
    24 
       
    25 #ifdef __WINSCW__ 
       
    26 #include <e32std.h>     //RPointerArray
       
    27 
       
    28 #include <pls.h> // For emulator WSD API 
       
    29 const TUid KTCLDLLUid3 = {0}; // Must change
       
    30 const TInt KMaxDataKey = 10;
       
    31 #endif // __WINSCW__
       
    32 
       
    33 /*
       
    34  * The following macros convert between TclFile's and fd's.  The conversion
       
    35  * simple involves shifting fd's up by one to ensure that no valid fd is ever
       
    36  * the same as NULL.  Note that this code is duplicated from tclUnixPipe.c
       
    37  */
       
    38 
       
    39 #define MakeFile(fd) ((TclFile)((fd)+1))
       
    40 #define GetFd(file) (((int)file)-1)
       
    41 
       
    42 #ifdef __WINSCW__ 
       
    43 //The following code will run only on the emulator
       
    44 
       
    45 //Put the global count into a structure
       
    46 struct DLLData
       
    47 {
       
    48 	// TCL globals
       
    49 	char* tclExecutableName;
       
    50 	char* tclNativeExecutableName;
       
    51 
       
    52 	void* dataKey[KMaxDataKey];
       
    53 	int inFinalize;
       
    54 	int subsystemsInitialized;
       
    55 	void* allocHead;	
       
    56 	void* defaultEncoding;
       
    57 	void* systemEncoding;
       
    58 	Tcl_HashTable encodingTable;	
       
    59 	SyncObjRecord keyRecord;
       
    60 	Tcl_HashTable typeTable;
       
    61 	int typeTableInitialized;
       
    62 	int encodingsInitialized;	
       
    63 	char* tclDefaultEncodingDir;
       
    64 	char* tclLibraryPathStr;	
       
    65 	int opTableInitialized;
       
    66 	Tcl_HashTable opHashTable;
       
    67 	Tcl_HashTable auxDataTypeTable;
       
    68 	int auxDataTypeTableInitialized;
       
    69 	void* cwdPathPtr;
       
    70 	int cwdPathEpoch;
       
    71 	void* refArray;
       
    72 	int spaceAvl;
       
    73 	int inUse;
       
    74 	TclPlatformType tclPlatform;	
       
    75 	void* firstNotifierPtr;
       
    76 	
       
    77 	// Symbian globals
       
    78 	char fileNames[8][L_tmpnam + 9];
       
    79 };
       
    80 
       
    81 //Initialization function
       
    82 TInt InitializeGlobals(DLLData* aData)
       
    83 {
       
    84    memset(aData, 0, sizeof(DLLData));
       
    85    aData->tclPlatform = TCL_PLATFORM_UNIX;
       
    86    return KErrNone;
       
    87 }
       
    88 
       
    89 //Define a way to access the structure
       
    90 //On the first call to this function, memory will be allocated with the specified
       
    91 //Uid as an identifier and the Initialization function will be called
       
    92 //Subsequent calls to this function return the allocated memory
       
    93 struct DLLData* GetGlobals()
       
    94 {
       
    95    return Pls<DLLData>(KTCLDLLUid3, InitializeGlobals);
       
    96 }
       
    97 
       
    98 //Clean up memory allocated for PLS used for storing globals
       
    99 int CleanupGlobals(void)
       
   100 {
       
   101 	return FreePls(GetGlobals());
       
   102 }
       
   103 
       
   104 void* get_gFileName(int index)
       
   105 {
       
   106    return &(GetGlobals()->fileNames[index]);
       
   107 }
       
   108 
       
   109 char** get_tclExecutableName()
       
   110 {
       
   111    return &(GetGlobals()->tclExecutableName);
       
   112 }
       
   113 
       
   114 char** get_tclNativeExecutableName()
       
   115 {
       
   116    return &(GetGlobals()->tclNativeExecutableName);
       
   117 }
       
   118 
       
   119 void** get_dataKey(int index)
       
   120 {
       
   121    return &(GetGlobals()->dataKey[index]);
       
   122 }
       
   123 
       
   124 void* get_inFinalize()
       
   125 {
       
   126    return &(GetGlobals()->inFinalize);
       
   127 }
       
   128 
       
   129 void* get_subsystemsInitialized()
       
   130 {
       
   131    return &(GetGlobals()->subsystemsInitialized);
       
   132 }
       
   133 
       
   134 void** get_allocHead()
       
   135 {
       
   136    return &(GetGlobals()->allocHead);
       
   137 }
       
   138 
       
   139 void** get_defaultEncoding()
       
   140 {
       
   141    return &(GetGlobals()->defaultEncoding);
       
   142 }
       
   143 
       
   144 void** get_systemEncoding()
       
   145 {
       
   146    return &(GetGlobals()->systemEncoding);
       
   147 }
       
   148 
       
   149 void* get_encodingTable()
       
   150 {
       
   151    return &(GetGlobals()->encodingTable);
       
   152 }
       
   153 
       
   154 void* get_keyRecord()
       
   155 {
       
   156    return &(GetGlobals()->keyRecord);
       
   157 }
       
   158 
       
   159 void* get_typeTable()
       
   160 {
       
   161    return &(GetGlobals()->typeTable);
       
   162 }
       
   163 
       
   164 void* get_typeTableInitialized()
       
   165 {
       
   166    return &(GetGlobals()->typeTableInitialized);
       
   167 }
       
   168 
       
   169 void* get_encodingsInitialized()
       
   170 {
       
   171    return &(GetGlobals()->encodingsInitialized);
       
   172 }
       
   173 
       
   174 char** get_tclDefaultEncodingDir()
       
   175 {
       
   176    return &(GetGlobals()->tclDefaultEncodingDir);
       
   177 }
       
   178 
       
   179 char** get_tclLibraryPathStr()
       
   180 {
       
   181    return &(GetGlobals()->tclLibraryPathStr);
       
   182 }
       
   183 
       
   184 void* get_opTableInitialized()
       
   185 {
       
   186    return &(GetGlobals()->opTableInitialized);
       
   187 }
       
   188 
       
   189 void* get_opHashTable()
       
   190 {
       
   191    return &(GetGlobals()->opHashTable);
       
   192 }
       
   193 
       
   194 void* get_auxDataTypeTableInitialized()
       
   195 {
       
   196    return &(GetGlobals()->auxDataTypeTableInitialized);
       
   197 }
       
   198 
       
   199 void* get_auxDataTypeTable()
       
   200 {
       
   201    return &(GetGlobals()->auxDataTypeTable);
       
   202 }
       
   203 
       
   204 void** get_cwdPathPtr()
       
   205 {
       
   206    return &(GetGlobals()->cwdPathPtr);
       
   207 }
       
   208 
       
   209 void* get_cwdPathEpoch()
       
   210 {
       
   211    return &(GetGlobals()->cwdPathEpoch);
       
   212 }
       
   213 
       
   214 void** get_refArray()
       
   215 {
       
   216    return &(GetGlobals()->refArray);
       
   217 }
       
   218 
       
   219 void* get_spaceAvl()
       
   220 {
       
   221    return &(GetGlobals()->spaceAvl);
       
   222 }
       
   223 
       
   224 void* get_inUse()
       
   225 {
       
   226    return &(GetGlobals()->inUse);
       
   227 }
       
   228 
       
   229 /*
       
   230  *----------------------------------------------------------------------
       
   231  *
       
   232  * TclPlatformExit --
       
   233  *
       
   234  *	This procedure implements the Symbian specific exit routine.
       
   235  *  Modelled after Macintosh version. 
       
   236  *
       
   237  * Results:
       
   238  *	None.
       
   239  *
       
   240  * Side effects:
       
   241  *	We exit the process.
       
   242  *
       
   243  *----------------------------------------------------------------------
       
   244  */
       
   245 
       
   246 void
       
   247 TclpExit(
       
   248     int status)		/* Ignored. */
       
   249 {
       
   250     // Free the PLS
       
   251     CleanupGlobals();
       
   252 
       
   253     exit(status);
       
   254 }
       
   255 
       
   256 void* get_tclPlatform()
       
   257 {
       
   258    return &(GetGlobals()->tclPlatform);
       
   259 }
       
   260 
       
   261 void** get_firstNotifierPtr()
       
   262 {
       
   263    return &(GetGlobals()->firstNotifierPtr);
       
   264 }
       
   265 
       
   266 #else
       
   267 //Target device code
       
   268 char tmpFileName[L_tmpnam + 9];
       
   269 char fifoFileName[L_tmpnam + 9];
       
   270 char inFileName[L_tmpnam + 9];
       
   271 char outFileName[L_tmpnam + 9];
       
   272 char errFileName[L_tmpnam + 9];
       
   273 char inFileName1[L_tmpnam + 9];
       
   274 char outFileName1[L_tmpnam + 9];
       
   275 char errFileName1[L_tmpnam + 9];	
       
   276 
       
   277 #endif
       
   278 
       
   279 #include "tclSymbianGlobals.h"
       
   280 
       
   281 #ifdef __cplusplus
       
   282 extern "C" {
       
   283 #endif
       
   284 
       
   285 #define ADDPARAMTOCHILD 4
       
   286 
       
   287 EXPORT_C void ChildProcessCleanup(int isChildProcess, int argc, char **argv)
       
   288 {
       
   289 	RDebug::Print(_L("###TclSqlite3: Child process cleanup - begin. argc = %d.\r\n"), argc);
       
   290 	TBuf<256> buf;
       
   291 	for(TInt i=0;i<argc;++i)
       
   292 		{
       
   293 		TPtrC8 p((const unsigned char*)(argv[i]));
       
   294 		buf.Copy(p);
       
   295 	    RDebug::Print(_L("   ### arg %d, value \"%S\"\r\n"), i, &buf);
       
   296 		}
       
   297 		
       
   298     // add fifo close & unlink
       
   299     if (isChildProcess == 1)
       
   300     	{
       
   301         RDebug::Print(_L("  ### Unlink 0.\r\n"));
       
   302     	
       
   303     	TPtrC8 p1((const unsigned char*)tmpFileName);
       
   304     	buf.Copy(p1);
       
   305         RDebug::Print(_L("   ### tmp file name \"%S\"\r\n"), &buf);
       
   306     	
       
   307     	TPtrC8 p2((const unsigned char*)fifoFileName);
       
   308     	buf.Copy(p2);
       
   309         RDebug::Print(_L("   ### fifo file name \"%S\"\r\n"), &buf);
       
   310         
       
   311     	TPtrC8 p3((const unsigned char*)inFileName);
       
   312     	buf.Copy(p3);
       
   313         RDebug::Print(_L("   ### input file name \"%S\"\r\n"), &buf);
       
   314     	
       
   315     	TPtrC8 p4((const unsigned char*)outFileName);
       
   316     	buf.Copy(p4);
       
   317         RDebug::Print(_L("   ### output file name \"%S\"\r\n"), &buf);
       
   318     	
       
   319     	TPtrC8 p5((const unsigned char*)errFileName);
       
   320     	buf.Copy(p5);
       
   321         RDebug::Print(_L("   ### err file name \"%S\"\r\n"), &buf);
       
   322     	
       
   323     	RDebug::Print(_L("   ### Close stdin, stdout and stderr.\r\n"));
       
   324     	close (TCL_STDIN);
       
   325     	close (TCL_STDOUT);
       
   326     	close (TCL_STDERR);
       
   327 		for(TInt i=0, idx=argc-i-1; i<ADDPARAMTOCHILD && idx >= 0; ++i, --idx)
       
   328 			{
       
   329     		if(argv[idx])
       
   330     			{
       
   331         		TPtrC8 p((const unsigned char*)(argv[idx]));
       
   332         		buf.Copy(p);
       
   333     	    	RDebug::Print(_L("   ### Unlink. Arg %d. Value \"%S\".\r\n"), idx, &buf);
       
   334     	    	unlink(argv[idx]);
       
   335     			}
       
   336 			}
       
   337     	}
       
   338     else
       
   339     	{
       
   340         RDebug::Print(_L("  ### Unlink 1.\r\n"));
       
   341     	
       
   342     	TPtrC8 p1((const unsigned char*)inFileName1);
       
   343     	buf.Copy(p1);
       
   344         RDebug::Print(_L("   ### 1 input file name \"%S\"\r\n"), &buf);
       
   345     	
       
   346     	TPtrC8 p2((const unsigned char*)outFileName1);
       
   347     	buf.Copy(p2);
       
   348         RDebug::Print(_L("   ### 1 output file name \"%S\"\r\n"), &buf);
       
   349 
       
   350     	TPtrC8 p3((const unsigned char*)errFileName1);
       
   351     	buf.Copy(p3);
       
   352         RDebug::Print(_L("   ### 1 err file name \"%S\"\r\n"), &buf);
       
   353 
       
   354     	unlink(inFileName1);
       
   355     	unlink(outFileName1);
       
   356     	unlink(errFileName1);
       
   357     	}
       
   358 	RDebug::Print(_L("###TclSqlite3: Child process cleanup - end.\r\n"));
       
   359 	}
       
   360 
       
   361 // Symbian main hook for tclappinit
       
   362 EXPORT_C int ChildProcessInit (int *argc, char ***argv)
       
   363 {
       
   364     //set the stdin,stdout,stderr to the child process. the fds pass to the posix_spawn() in argv
       
   365     TclFile inputFile = NULL;
       
   366     TclFile outputFile= NULL;
       
   367     TclFile errorFile = NULL;
       
   368     int joinThisError;
       
   369     int fd[4] = {0, 0, 0, 0};
       
   370     char errSpace[200 + TCL_INTEGER_SPACE];
       
   371     int anerr = 0;
       
   372 	TBuf<256> buf;
       
   373 
       
   374     RDebug::Print(_L("###TclSqlite3: Child process init - begin. argc = %d.\r\n"), argc != NULL ? *argc : 0);
       
   375     if(argc)
       
   376     	{
       
   377     	for(TInt i=0;i<*argc;++i)
       
   378     		{
       
   379     		TPtrC8 p((const unsigned char*)((*argv)[i]));
       
   380     		buf.Copy(p);
       
   381     	    RDebug::Print(_L("   ### arg %d, value \"%S\"\r\n"), i, &buf);
       
   382     		}
       
   383     	}
       
   384    //set the stdin,stdout,stderr and pipeid to the child process. the fds pass to the posix_spawn() in argv
       
   385 	if (*argc >= 5)
       
   386 		{
       
   387 		// fifoFile
       
   388 		RDebug::Print(_L("  ### Fifo file. Arg %d.\r\n"), *argc-4);
       
   389 		if((*argv)[*argc-4])
       
   390 			{
       
   391 			fd[0] = open((*argv)[*argc-4],O_WRONLY);
       
   392 			if (fd[0] == -1)
       
   393 				{
       
   394 				RDebug::Print(_L("   ### fd[0](fifoFile) errno is %d\r\n"), errno);
       
   395 				}
       
   396 			else
       
   397 				{
       
   398 	    		TPtrC8 p((const unsigned char*)((*argv)[*argc-4]));
       
   399 	    		buf.Copy(p);
       
   400 				RDebug::Print(_L("   ### fifoFile is \"%S\", fd[0] is %d\r\n"), &buf, fd[0]);				
       
   401 				}
       
   402 		    //fd = atoi((*argv)[*argc-1]);
       
   403 			}
       
   404 		else
       
   405 			{
       
   406 			RDebug::Print(_L("   ### Fifo file - (*argv)[*argc-4] is 0.\r\n"));
       
   407 			//should add later
       
   408 			}
       
   409 		// inputFile
       
   410 		RDebug::Print(_L("  ### Input file. Arg %d.\r\n"), *argc-3);
       
   411 		if(((*argv)[*argc-3])&&(strcmp((*argv)[*argc-3],"STD")))
       
   412 			{
       
   413 			fd[3] = open((*argv)[*argc-3],O_RDONLY); 
       
   414 			inputFile = MakeFile(fd[3]);
       
   415 			if (fd[3] == -1)
       
   416 				{
       
   417 				RDebug::Print(_L("   ### fd[3](inputFile) errno is %d\r\n"), errno);
       
   418 				}
       
   419 			else
       
   420 				{
       
   421 	    		TPtrC8 p((const unsigned char*)((*argv)[*argc-3]));
       
   422 	    		buf.Copy(p);
       
   423 				RDebug::Print(_L("   ### inputFile is \"%S\", fd[3] is %d\r\n"), &buf, fd[3]);					
       
   424 				}
       
   425 			    //inputFile = (TclFile) (atoi((*argv)[*argc-4]));
       
   426 			}
       
   427 		else
       
   428 			{
       
   429 			RDebug::Print(_L("   ### Input file - ((*argv)[*argc-3])&&(strcmp((*argv)[*argc-3],\"STD\")) is 0.\r\n"));
       
   430 			//should add later
       
   431 			}
       
   432 		// outputFile
       
   433 		RDebug::Print(_L("  ### Output file. Arg %d\r\n"), *argc-2);
       
   434 		if(((*argv)[*argc-2])&&(strcmp((*argv)[*argc-2],"STD")))
       
   435 			{
       
   436 			fd[2] = open((*argv)[*argc-2],O_WRONLY);
       
   437 			outputFile = MakeFile(fd[2]);
       
   438 		    if (fd[2] == -1)
       
   439 		    	{
       
   440 		    	RDebug::Print(_L("   ### fd[2](outputFile) errno is %d\r\n"), errno);
       
   441 		    	}
       
   442 		    else
       
   443 				{
       
   444 	    		TPtrC8 p((const unsigned char*)((*argv)[*argc-2]));
       
   445 	    		buf.Copy(p);
       
   446 				RDebug::Print(_L("   ### outputFile is \"%S\", fd[2] is %d\r\n"), &buf, fd[2]);					
       
   447 				}
       
   448 		    
       
   449 			//outputFile = (TclFile) (atoi((*argv)[*argc-3]));
       
   450 			}
       
   451 		else
       
   452 			{
       
   453 			RDebug::Print(_L("   ### Output file - ((*argv)[*argc-2])&&(strcmp((*argv)[*argc-2],\"STD\")) is 0.\r\n"));
       
   454 			//should add later
       
   455 			//outputFile = MakeFile(1);
       
   456 			}
       
   457 		// errorFile
       
   458 		RDebug::Print(_L("  ### Error file. Arg %d\r\n"), *argc-1);
       
   459 		if(((*argv)[*argc-1])&&(strcmp((*argv)[*argc-1],"STD")))
       
   460 			{
       
   461 			fd[1] = open((*argv)[*argc-1],O_WRONLY);
       
   462 			errorFile = MakeFile(fd[1]);
       
   463 			if (fd[1] == -1)
       
   464 				{
       
   465 				RDebug::Print(_L("   ### fd[1] errorFile errno is %d\r\n"), errno);
       
   466 				}
       
   467 			else
       
   468 				{
       
   469 	    		TPtrC8 p((const unsigned char*)((*argv)[*argc-1]));
       
   470 	    		buf.Copy(p);
       
   471 				RDebug::Print(_L("   ### errorFile is \"%S\", fd[1] is %d\r\n"), &buf, fd[1]);
       
   472 				}
       
   473 		    //errorFile = (TclFile) (atoi((*argv)[*argc-2]));
       
   474 			}
       
   475 		else
       
   476 			{
       
   477 			RDebug::Print(_L("   ### Output file - ((*argv)[*argc-1])&&(strcmp((*argv)[*argc-1],\"STD\")) is 0.\r\n"));
       
   478 			//should add later
       
   479 			}
       
   480 		//*argc = *argc-4;
       
   481 		
       
   482 		joinThisError = errorFile && (errorFile == outputFile);
       
   483 
       
   484 		//fd = GetFd(errPipeOut);
       
   485     
       
   486 		//
       
   487 		// Set up stdio file handles for the child process.
       
   488 		//
       
   489 
       
   490 		if (!SetupStdFile(inputFile, TCL_STDIN)
       
   491 			|| !SetupStdFile(outputFile, TCL_STDOUT)
       
   492 			|| (!joinThisError && !SetupStdFile(errorFile, TCL_STDERR))
       
   493 			|| (joinThisError &&
       
   494 				((dup2(1,2) == -1) ||
       
   495 				 (fcntl(2, F_SETFD, 0) != 0)))) 
       
   496 			//if (!SetupStdFile(errorFile, TCL_STDERR))
       
   497 			{
       
   498 			RDebug::Print(_L("   ### child process couldn't set up input/output, error: %d\r\n"), errno);
       
   499 			sprintf(errSpace,"child process couldn't set up input/output, error: %d\r\n", errno);
       
   500 			write(fd[0], errSpace, (size_t) strlen(errSpace));
       
   501 			close(fd[0]);
       
   502 			unlink((*argv)[*argc-4]);
       
   503 		    RDebug::Print(_L("###TclSqlite3: Child process init - end 1.\r\n"));
       
   504 			_exit(1);
       
   505 			}
       
   506 
       
   507 		sprintf(errSpace,"OK\r\n");
       
   508 		write(fd[0], errSpace, (size_t) strlen(errSpace));
       
   509 		anerr = close(fd[0]);
       
   510 		anerr = unlink((*argv)[*argc-4]);
       
   511 	    RDebug::Print(_L("###TclSqlite3: Child process init - end 2. anerr=%d.\r\n"), anerr);
       
   512 	   	return 1;
       
   513 		}
       
   514     
       
   515     RDebug::Print(_L("###TclSqlite3: Child process init - end 3.\r\n"));
       
   516     return 0;			
       
   517 }
       
   518 
       
   519 void TclPrint1(const char* aFmt, const char* aStr)
       
   520 	{
       
   521 	TBuf<128> fmt;
       
   522 	fmt.Copy(TPtrC8((const TUint8*)aFmt));
       
   523 	TBuf<128> str;
       
   524 	str.Copy(TPtrC8((const TUint8*)aStr));
       
   525 	RDebug::Print(fmt, &str);	
       
   526 	}
       
   527 
       
   528 void TclPrint2(const char* aFmt, const char* aStr, int aNum)
       
   529 	{
       
   530 	TBuf<128> fmt;
       
   531 	fmt.Copy(TPtrC8((const TUint8*)aFmt));
       
   532 	TBuf<128> str;
       
   533 	str.Copy(TPtrC8((const TUint8*)aStr));
       
   534 	RDebug::Print(fmt, &str, aNum);	
       
   535 	}
       
   536 
       
   537 void TclPrint3(const char* aFmt)
       
   538 	{
       
   539 	TBuf<128> fmt;
       
   540 	fmt.Copy(TPtrC8((const TUint8*)aFmt));
       
   541 	RDebug::Print(fmt);	
       
   542 	}
       
   543 
       
   544 #ifdef __cplusplus
       
   545 }
       
   546 #endif