systemc.c

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------------*/
00002 /* Interface with system C function */
00003 /* Copyright (Allan CORNET) INRIA 2005 */
00004 /*-----------------------------------------------------------------------------------*/
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include "MALLOC.h" /* MALLOC */
00008 #include "machine.h"
00009 #include "systemc.h"
00010 /*-----------------------------------------------------------------------------------*/
00011 #ifdef _MSC_VER
00012 extern BOOL IsAFile(char *chainefichier);
00013 BOOL CallWindowsShell(char *command,BOOL WaitInput);
00014 #endif
00015 /*-----------------------------------------------------------------------------------*/
00016 int C2F(systemc)(char *command, integer *stat)
00017 {
00018 #ifdef _MSC_VER
00019         {
00020                 BOOL Status=FALSE;
00021                 Status=CallWindowsShell(command,FALSE);
00022                 if (Status)
00023                 {
00024                         *stat=(integer)0;
00025                 }
00026                 else
00027                 {
00028                         *stat=(integer)1;
00029                 }
00030         }
00031 #else
00032         {
00033                 int status;
00034                 status=system(command);
00035                 *stat=(integer)status;
00036         }
00037 #endif
00038 
00039         return(0);
00040 }
00041 /*-----------------------------------------------------------------------------------*/
00042 #ifdef _MSC_VER
00043 BOOL CallWindowsShell(char *command,BOOL WaitInput)
00044 {
00045         BOOL bReturn=FALSE;
00046         char shellCmd[_MAX_PATH];
00047         char *CmdLine=NULL;
00048 
00049         PROCESS_INFORMATION piProcInfo; 
00050         STARTUPINFO siStartInfo;
00051         SECURITY_ATTRIBUTES saAttr; 
00052 
00053         DWORD ExitCode;
00054 
00055         char *TMPDir=NULL;
00056         char FileTMPDir[MAX_PATH];
00057 
00058         saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
00059         saAttr.bInheritHandle = TRUE; 
00060         saAttr.lpSecurityDescriptor = NULL; 
00061 
00062         ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
00063 
00064         ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
00065         siStartInfo.cb = sizeof(STARTUPINFO); 
00066         siStartInfo.dwFlags      = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
00067         siStartInfo.wShowWindow  = SW_HIDE;
00068         
00069         if (WaitInput)
00070         {
00071                 siStartInfo.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
00072         }
00073         else
00074         {
00075                 siStartInfo.hStdInput=NULL;
00076         }
00077 
00078         siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
00079         siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
00080 
00081         GetEnvironmentVariable("ComSpec", shellCmd, _MAX_PATH);
00082         TMPDir=getenv("TMPDIR");
00083         sprintf(FileTMPDir,"%s\\DOS.OK",TMPDir);
00084 
00085         CmdLine=(char*)MALLOC( (strlen(shellCmd)+strlen(command)+strlen(FileTMPDir)+strlen("%s /a /c %s && echo DOS>%s")+1)*sizeof(char) );
00086         sprintf(CmdLine,"%s /a /c %s && echo DOS>%s",shellCmd,command,FileTMPDir);
00087         if (IsAFile(FileTMPDir)) DeleteFile(FileTMPDir);
00088 
00089         if (CreateProcess(NULL, CmdLine, NULL, NULL, TRUE,0, NULL, NULL, &siStartInfo, &piProcInfo))
00090         {
00091                 WaitForSingleObject(piProcInfo.hProcess,INFINITE);
00092 
00093                 if ( GetExitCodeProcess(piProcInfo.hProcess,&ExitCode) == STILL_ACTIVE )
00094                 {
00095                         TerminateProcess(piProcInfo.hProcess,0);
00096                 }
00097 
00098                 CloseHandle(piProcInfo.hProcess);
00099 
00100                 if (CmdLine) {FREE(CmdLine);CmdLine=NULL;}
00101 
00102                 if (IsAFile(FileTMPDir))
00103                 {
00104                         DeleteFile(FileTMPDir);
00105                         bReturn=TRUE;
00106                 }
00107                 else
00108                 {
00109                         bReturn=FALSE;
00110                 }
00111         }
00112         else
00113         {
00114                 CloseHandle(piProcInfo.hProcess);
00115                 if (CmdLine) {FREE(CmdLine);CmdLine=NULL;}
00116                 bReturn=FALSE;
00117         }
00118         return bReturn;
00119 }
00120 #endif
00121 /*-----------------------------------------------------------------------------------*/

Generated on Sun Mar 4 15:03:58 2007 for Scilab [trunk] by  doxygen 1.5.1