1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
47 #include <sys/types.h>
57 //using namespace std;
64 PROCESS_INFORMATION pi;
66 HANDLE g_hReadPipe = NULL;
67 HANDLE g_hWritePipe = NULL;
68 HANDLE g_hErrReadPipe = NULL;
69 HANDLE g_hErrWritePipe = NULL;
70 HANDLE g_hInputReadPipe = NULL;
71 HANDLE g_hInputWritePipe = NULL;
74 static char home_path[200];
78 char* g_pchBuf = NULL;
80 void MainEx(int argc, char **argv, Tcl_AppInitProc *pfappInitProc, Tcl_Interp *pintrp,
82 int Tcl_AppInit(Tcl_Interp *pintrp);
83 void Panic (Tcl_Interp * pintrp, const char * pch);
84 void WishPanic TCL_VARARGS_DEF(char *,arg1);
89 //string g_sCfgFile = "colors.cfg";
91 bool g_bNonStop = true;
95 *----------------------------------------------------------------------
100 * TCL_OK - if sucsess, or TCL_ERROR
102 *----------------------------------------------------------------------
105 static char* module_path;
107 static char* GetPathFromModuleName( char* modulename )
113 /* if module is link find real path */
116 strcpy( real, modulename );
117 while( (len = readlink( real, real, 1000 )) > 0 )
119 char* path = (char*)malloc( strlen( real ) + 10 );
120 strcpy( path, real );
122 char* path = (char*)malloc( strlen( modulename ) + 10 );
123 strcpy( path, modulename );
126 for( i = strlen( path ) - 1;
127 i > 0 && path[i] != '\\' && path[i] != '/';
140 for( i = 0; i < (int)strlen( path ); i++ )
141 if( path[i] == '\\' )
148 int _LoadColors (ClientData cd, Tcl_Interp *pintrp,
149 int argc, char **argv)
154 strcat( strcpy( fn, module_path ), "/colors.tcl" );
156 strcat( strcpy( fn, home_path ), "/.cvenv/colors.tcl" );
158 LoadColors( pintrp, fn );
162 int _SaveColors (ClientData cd, Tcl_Interp *pintrp,
163 int argc, char **argv)
167 strcat( strcpy( fn, module_path ), "/colors.tcl" );
169 strcat( strcpy( fn, home_path ), "/.cvenv" );
171 strcat( fn, "/colors.tcl" );
173 SaveColors( pintrp, fn );
177 static int finished = 0;
178 static int started = 0;
179 static char* buffer = 0;
180 static char* errbuffer = 0;
183 CRITICAL_SECTION mutex;
185 DWORD WINAPI ErrReaderFun (LPVOID pvParam)
187 DWORD cchReadBuffer,cchErrReadBuffer,counter = 0; /* number of bytes read or to be written */
189 char * chErrReadBuffer = (char*)malloc( PIPESIZE + 1 ); /* pipe read buffer */
191 while( ReadFile( g_hErrReadPipe, chErrReadBuffer, PIPESIZE, &cchErrReadBuffer, 0 ) )
194 chErrReadBuffer[cchErrReadBuffer] = 0;
195 EnterCriticalSection( &mutex );
198 errbuffer = (char*)malloc( strlen( chErrReadBuffer ) + 1 );
199 strcpy( errbuffer, chErrReadBuffer );
203 errbuffer = (char*)realloc( errbuffer, strlen( errbuffer ) + strlen( chErrReadBuffer ) + 2 );
204 strcat( errbuffer, chErrReadBuffer );
206 LeaveCriticalSection( &mutex );
209 free(chErrReadBuffer);
213 DWORD WINAPI ReaderFun (LPVOID pvParam)
215 char* comline = (char*)pvParam;
217 //-----------------------------------------------------------------------
218 BOOL bOk; /* BOOL return code for APIs */
220 DWORD cchReadBuffer,cchErrReadBuffer,counter = 0; /* number of bytes read or to be written */
221 STARTUPINFO si; /* for CreateProcess call */
222 SECURITY_ATTRIBUTES saPipe,saErrPipe; /* security for anonymous pipe */
223 DWORD retCode; // Used to trap return codes.
224 //-----------------------------------------------------------------------
226 //-----------------------------------------------------------------------
227 /* set up the security attributes for the anonymous pipe */
228 saPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
229 saPipe.lpSecurityDescriptor = NULL;
230 saErrPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
231 saErrPipe.lpSecurityDescriptor = NULL;
233 /* In order for the child to be able to write to the pipe, the handle */
234 /* must be marked as inheritable by setting this flag: */
235 saPipe.bInheritHandle = TRUE;
236 saErrPipe.bInheritHandle = TRUE;
237 //-----------------------------------------------------------------------
239 bOk = CreatePipe(&g_hReadPipe,
246 retCode = GetLastError();
247 printf("Reader: Can't create STDOUT pipe, Error #%d\n",GetLastError());
251 bOk = CreatePipe(&g_hErrReadPipe,
258 retCode = GetLastError();
259 printf("Reader: Can't create STDERR pipe, Error #%d\n",GetLastError());
263 bOk = CreatePipe(&g_hInputReadPipe,
270 retCode = GetLastError();
271 printf("Reader: Can't create STDIN pipe, Error #%d\n",GetLastError());
276 /* Set up the STARTUPINFO structure for the CreateProcess() call */
277 memset(&si, 0, sizeof(si));
279 //si.hStdError = si.hStdOutput = g_hWritePipe;
280 //si.dwFlags = STARTF_USESTDHANDLES;
283 //si.hStdOutput = g_hWritePipe; /* write end of the pipe */
284 //si.dwFlags = STARTF_USESTDHANDLES;
288 strcpy( buf, module_path );
289 strcat( buf, comline );
290 sprintf(ln," %u",g_hWritePipe);
292 sprintf(ln," %u",g_hErrWritePipe);
294 sprintf(ln," %u",g_hInputReadPipe);
298 /* Now create the child process, inheriting handles */
299 bOk = CreateProcess(NULL, /* filename */
300 buf, /* full command line for child */
301 NULL, /* process security descriptor */
302 NULL, /* thread security descriptor */
303 TRUE, /* inherit handles? Also use if STARTF_USESTDHANDLES */
304 /*DETACHED_PROCESS*/0, /* creation flags */ //Out// 0
305 NULL, /* inherited environment address */
306 NULL, /* startup dir; NULL = start in current */
307 &si, /* pointer to startup info (input) */
308 &pi); /* pointer to process info (output) */
310 CloseHandle( g_hWritePipe ); g_hWritePipe = NULL;
311 CloseHandle( g_hErrWritePipe ); g_hErrWritePipe = NULL;
312 CloseHandle( g_hInputReadPipe ); g_hInputReadPipe = NULL;
316 char * chReadBuffer = (char*)malloc( PIPESIZE + 1 ); /* pipe read buffer */
319 CloseHandle(CreateThread(NULL, 0, ErrReaderFun,
322 while( ReadFile( g_hReadPipe, chReadBuffer, PIPESIZE, &cchReadBuffer, 0 ) )
324 chReadBuffer[cchReadBuffer] = 0;
325 EnterCriticalSection( &mutex );
328 buffer = (char*)malloc( strlen( chReadBuffer ) + 1 );
329 strcpy( buffer, chReadBuffer );
333 buffer = (char*)realloc( buffer, strlen( buffer ) + strlen( chReadBuffer ) + 2 );
334 strcat( buffer, chReadBuffer );
336 LeaveCriticalSection( &mutex );
341 //--------------------------------------------------
342 printf("ReaderID: I finish myself\n");
343 //--------------------------------------------------
351 int main_seance( int argc, char** argv );
354 static pthread_mutex_t mutex;
359 static void* errrun_process( void* a )
363 while( (nbytes = read( err[0], buff, sizeof(errbuffer) )) > 0 )
366 pthread_mutex_lock( &mutex );
369 errbuffer = (char*)malloc( strlen( buff ) + 1 );
373 errbuffer = (char*)realloc( errbuffer, strlen( errbuffer ) + strlen( buff ) + 2 );
374 strcat( errbuffer, buff );
375 pthread_mutex_unlock( &mutex );
383 static void* run_process( void* a )
385 char* argv = (char*)a;
386 // creating pipe for interproc communications
391 // creating child process
392 if( (child = fork()) == -1 )
394 printf( "can't fork\n" );
400 /* we are in child process */
404 sprintf( pip, "%d", fd[1] );
405 sprintf( errp, "%d", err[1] );
406 sprintf( pipi, "%d", fdi[0] );
410 char* _argv[] = {global_argv[0], argv, pip, errp, pipi,0};
411 main_seance( 5, _argv );
419 // we are in parent process
426 pthread_create( &thread, 0, errrun_process, 0 );
429 while( (nbytes = read( fd[0], buff, sizeof(buffer) )) > 0 )
432 pthread_mutex_lock( &mutex );
435 buffer = (char*)malloc( strlen( buff ) + 1 );
439 buffer = (char*)realloc( buffer, strlen( buffer ) + strlen( buff ) + 2 );
440 strcat( buffer, buff );
441 pthread_mutex_unlock( &mutex );
453 static char filename[1000];
455 int InputData (ClientData, Tcl_Interp *interp,
460 if (g_hInputWritePipe)
465 char* data = Tcl_GetVar(interp, "CVEnv::inputdata", TCL_GLOBAL_ONLY);
469 size_t len = strlen( data );
473 WriteFile(g_hInputWritePipe,
479 write( fdi[1], data, len );
487 int Execute_Command (ClientData, Tcl_Interp *interp,
491 "$CVEnv::tb.bbox3.b0 configure -state disable -relief link\n"
492 "$CVEnv::tb.bbox3.b1 configure -state active -relief link\n"
493 //"$CVEnv::tb.bbox3.b1 _leave\n"
498 strcpy(cmd , "seance ");
499 strcat(cmd, argv[1] );
500 CloseHandle(CreateThread(NULL, 0, ReaderFun,
503 strcpy( filename, argv[1] );
505 pthread_create( &thread, 0, run_process, filename );
512 int Stop_Command (ClientData, Tcl_Interp *interp, int, char**)
515 "$CVEnv::tb.bbox3.b1 configure -state disable -relief link\n"
516 "$CVEnv::tb.bbox3.b0 configure -state active -relief link\n"
517 //"$CVEnv::tb.bbox3.b0 _leave"
521 if (TerminateProcess(pi.hProcess,1))
523 else return TCL_ERROR;
525 if( kill( child, 9 ) )
527 printf( "kill error\n" );
533 ////////////////////////////////////////////////////////////////////////
536 *----------------------------------------------------------------------
538 * Commands_Init -- Commands initialisation routine.
541 * TCL_OK - if sucsess, or TCL_ERROR
543 *----------------------------------------------------------------------
546 Commands_Init(Tcl_Interp *interp)
548 Tcl_CreateCommand (interp, "pExecute", Execute_Command,
549 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
550 Tcl_CreateCommand (interp, "pStop", Stop_Command,
551 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
552 Tcl_CreateCommand (interp, "pLoadColors", _LoadColors,
553 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
554 Tcl_CreateCommand (interp, "pSaveColors", _SaveColors,
555 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
556 Tcl_CreateCommand (interp, "pParsebegin", ParseTextBegin,
557 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
558 Tcl_CreateCommand (interp, "pParseend", ParseTextEnd,
559 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
560 Tcl_CreateCommand (interp, "pInput", InputData,
561 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
568 *----------------------------------------------------------------------
570 * MainEx -- Main program for Tk-based applications.
572 *----------------------------------------------------------------------
575 MainEx( int argc, char** argv, Tcl_AppInitProc *appInitProc,
576 Tcl_Interp *pintrp, char *fileName )
585 (*appInitProc)(pintrp);
587 if ( Commands_Init(pintrp) != TCL_OK)
588 Panic (pintrp,"Can't initialise commands!");
590 //if (!LoadColors(pintrp,string("colors.tcl")))
591 // Panic (pintrp,"Can't load config file!");
594 strcat( strcat( strcpy( set_path, "set ::image_path \"" ), module_path ), "\"" );
595 code = Tcl_Eval( pintrp, set_path );
597 if (fileName != NULL)
600 strcat( strcat( strcpy( script, module_path ), "" ), fileName );
601 code = Tcl_EvalFile(pintrp, script);
603 Panic (pintrp,"Evaluate file error!");
605 else Tcl_SourceRCFile(pintrp);
607 Tcl_ResetResult(pintrp);
610 InitializeCriticalSection( &mutex );
612 pthread_mutex_init( &mutex, 0 );
615 while( Tk_GetNumMainWindows() > 0 )
617 while( Tcl_DoOneEvent( TCL_DONT_WAIT ) );
620 EnterCriticalSection( &mutex );
622 pthread_mutex_lock( &mutex );
626 char* pchcom = (char*)malloc( strlen( buffer ) + 1000 );
627 strcpy(pchcom , "set outputText \"");
628 strcat(pchcom , (const char*)buffer);
629 strcat(pchcom , "\"\n");
630 //strcat(pchcom , "$CVEnv::outtx configure -state normal\n");
631 strcat(pchcom , "$CVEnv::outtx insert end $outputText\n");
632 //strcat(pchcom , "$CVEnv::outtx configure -state disabled\n");
633 strcat(pchcom , "$CVEnv::outtx see \"end -1 lines\"\n");
634 strcat(pchcom , "unset outputText\n");
635 Tcl_Eval( pintrp, pchcom );
644 char* pchcom = (char*)malloc( strlen( errbuffer ) + 1000 );
645 strcpy(pchcom , "set errText \"");
646 strcat(pchcom , (const char*)errbuffer);
647 strcat(pchcom , "\"\n");
648 //strcat(pchcom , "$CVEnv::outtx configure -state normal\n");
649 strcat(pchcom , "$CVEnv::parsetx insert end $errText\n");
650 //strcat(pchcom , "$CVEnv::outtx configure -state disabled\n");
651 strcat(pchcom , "$CVEnv::parsetx see \"end -1 lines\"\n");
652 strcat(pchcom , "unset errText\n");
653 Tcl_Eval( pintrp, pchcom );
664 "$CVEnv::tb.bbox3.b1 configure -state disable -relief link\n"
665 "$CVEnv::tb.bbox3.b0 configure -state active -relief link\n"
666 //"$CVEnv::tb.bbox3.b0 _leave"
670 CloseHandle( g_hReadPipe ); g_hReadPipe = NULL;
671 CloseHandle( g_hErrReadPipe ); g_hErrReadPipe = NULL;
672 CloseHandle( g_hInputWritePipe ); g_hInputWritePipe = NULL;
677 LeaveCriticalSection( &mutex );
680 pthread_mutex_unlock( &mutex );
684 Tcl_DeleteInterp(pintrp);
690 *----------------------------------------------------------------------
692 * WishPanic -- escape function.
694 *----------------------------------------------------------------------
697 void WishPanic TCL_VARARGS_DEF(char *,arg1)
703 format = TCL_VARARGS_START(char *,arg1,argList);
704 printf(buf, format, argList);
708 *----------------------------------------------------------------------
710 * Panic -- error output & exit function.
712 *----------------------------------------------------------------------
715 void Panic (Tcl_Interp * pintrp, const char * pch)
717 printf("Thread %P:",Tcl_GetCurrentThread());
719 printf("\n Reason:");
720 printf(pintrp->result);
723 Tcl_DeleteInterp(pintrp);
728 *----------------------------------------------------------------------
730 * Tcl_AppInit -- Initialisation function.
732 *----------------------------------------------------------------------
735 int Tcl_AppInit(Tcl_Interp *pintrp)
738 if (Tcl_InitStubs(pintrp, TCL_VERSION, 1) == NULL)
739 Panic (pintrp,"Tcl stub's initialisation failed!");
741 if (Tcl_Init(pintrp) == TCL_ERROR)
742 Panic (pintrp,"Tcl's initialisation failed!");
744 if (Tk_Init(pintrp) == TCL_ERROR)
745 Panic (pintrp,"Tk's initialisation failed!");
746 // in unix disable multythread support
750 //-----------------------------------------------------------------------------
752 int main(int argc, char* argv[])
754 module_path = GetPathFromModuleName( argv[0] );
758 main_seance( argc, argv );
760 char* hdir = getenv( "HOME" );
762 strcpy( home_path, hdir );
765 Tcl_Interp* g_pInterp = Tcl_CreateInterp();
767 Tcl_SetPanicProc(WishPanic);
768 Tcl_FindExecutable(argv[0]);
770 MainEx(argc, argv, Tcl_AppInit, g_pInterp, "cvenv.tcl");
772 Tcl_DeleteInterp(g_pInterp);