155 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			155 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
| // fv 6/10/93                       
 | |
| 
 | |
| #include <stdio.h>
 | |
| #include <xvt.h>
 | |
| 
 | |
| #if XVT_OS == XVT_OS_SCOUNIX
 | |
| #include <sys/fcntl.h>
 | |
| #include <sys/wait.h>
 | |
| #endif
 | |
| 
 | |
| #if XVT_OS == XVT_OS_WIN
 | |
| #include <windows.h>
 | |
| #include <toolhelp.h>
 | |
| #endif
 | |
| 
 | |
| #include <applicat.h>
 | |
| #include <execp.h> 
 | |
| 
 | |
| bool TExternal_app::can_run() const
 | |
| {                    
 | |
| #if XVT_OS == XVT_OS_WIN
 | |
|   const TFixed_string p(_path);           
 | |
|   const bool big = p.find("cg0") == 0 && p.right(2) == "-1";
 | |
|   const int richieste = big ? 50 : 15;
 | |
|   const int libere = GetFreeSystemResources(GFSR_SYSTEMRESOURCES);
 | |
|   return libere >= richieste;
 | |
| #else
 | |
|   return TRUE;
 | |
| #endif
 | |
| }
 | |
| 
 | |
| 
 | |
| int TExternal_app::run(bool async, bool user)
 | |
| {
 | |
|   TString256 path(_path);
 | |
|   
 | |
|   if (user)
 | |
|     path << " -u" << main_app().user();
 | |
|   
 | |
|   _error = 0;
 | |
|   _exitcode =  0;
 | |
| 
 | |
|   // save cwd
 | |
|   save_dir();
 | |
| 
 | |
| #if XVT_OS == XVT_OS_WIN
 | |
|   if (can_run())
 | |
|   { 
 | |
|     main_app().begin_wait();
 | |
|     _exitcode = WinExec((char*)(const char*)path, SW_SHOW);
 | |
|     
 | |
|     if (_exitcode >= 32)
 | |
|     {        
 | |
|       if (!async)
 | |
|       {                            
 | |
|         TTemp_window tw(TASK_WIN);
 | |
|         tw.iconize();
 | |
|         
 | |
|         HTASK child = NULL;
 | |
|         TASKENTRY te; te.dwSize = sizeof(TASKENTRY); 
 | |
|         for (bool ok = TaskFirst(&te); ok; ok = TaskNext(&te))
 | |
|           if (te.hInst == (HINSTANCE)_exitcode) child = te.hTask;
 | |
|         CHECK(child, "Can't find child task");
 | |
|         
 | |
|         main_app().wait_for(child); 
 | |
|         for (byte i = 0; main_app().waiting(); i++)
 | |
|         {
 | |
|           if (i == 0 && TaskFindHandle(&te, child) == FALSE) 
 | |
|             break;
 | |
|           process_events();
 | |
|         }
 | |
|         
 | |
|         tw.maximize();
 | |
|       }
 | |
|       xvt_statbar_refresh();
 | |
|     }  
 | |
|     main_app().end_wait();
 | |
|   } else _exitcode = 8;
 | |
| 
 | |
|   switch (_exitcode)
 | |
|   {
 | |
|   case 0:          
 | |
|   case 8:
 | |
|     error_box("Risorse insufficienti per eseguire '%s'", (const char*)_path); break;
 | |
|   case 2:
 | |
|   case 3:
 | |
|     error_box("Impossibile trovare '%s'", (const char*)_path); break;
 | |
|   case 16:
 | |
|     error_box("'%s' e' gia' in esecuzione", (const char*)_path); break;
 | |
|   default:
 | |
|     if (_exitcode < 32)
 | |
|       error_box("Impossibile eseguire '%s':\nErrore %d", (const char*)_path, _exitcode); 
 | |
|     else
 | |
|       _exitcode = 0;  
 | |
|     break;
 | |
|   }  
 | |
| #else
 | |
| 
 | |
|   switch (fork())
 | |
|   {
 | |
|   case -1:
 | |
|     _error = errno;
 | |
|     _exitcode = -1;
 | |
|     break;
 | |
|   case 0:
 | |
|     const char* s = strdup(path);
 | |
|     char* p = strchr(s, ' ');
 | |
|     if (p) *p = '\0';
 | |
|     const char* pathn = strdup(s);
 | |
|     const char* args[21];
 | |
|     int i = 0;
 | |
|     args[i++] = pathn;
 | |
|     while ((i < 20) && (p))
 | |
|     {
 | |
|       s = p + 1;
 | |
|       p = strchr(s, ' ');
 | |
|       if (p) *p = '\0';
 | |
|       args[i++] = strdup(s);
 | |
|     }
 | |
|     args[i] = NULL;
 | |
|     for (i = 3; i < _NFILE; i++) fcntl(i,F_SETFD,1);
 | |
|     // execvp( path, NULL);
 | |
|     execvp ( pathn , args );
 | |
|     exit ( -1 );
 | |
|   default:
 | |
|     if(wait(&_exitcode) == -1)
 | |
|     {
 | |
|       error_box("Impossibile eseguire '%s':\nErrore %d", (const char*)_path, _exitcode);
 | |
|       _exitcode = -1;
 | |
|     }
 | |
|     else _exitcode = _exitcode >> 8;
 | |
|     break;
 | |
|   }
 | |
|   _error = errno;
 | |
|   xvt_escape(XVT_ESC_CH_REFRESH);
 | |
| 
 | |
| #endif
 | |
| 
 | |
|   // restore cwd
 | |
|   restore_dir();
 | |
| 
 | |
|   // update counts
 | |
|   if (_exitcode == 0)
 | |
|     _count++;
 | |
|   return _exitcode;
 | |
| }
 | |
| 
 | |
| TExternal_app::TExternal_app(const char* p)
 | |
| {
 | |
|   _path =  p;
 | |
|   _count = 0;
 | |
|   _error = 0;
 | |
|   _exitcode =  0;
 | |
| }
 |