/****************************************************************************** * * Title : commandFunc_runExternalProgram.c * Version 0.0 * * Description: Runs external program. * * * Author: Lukas Tomasek, tomasekl@fzu.cz * ******************************************************************************/ /****************************************************************************** * Header files * ******************************************************************************/ #include #include #include "commandListDefinitions.h" #include "commandFunc_runExternalProgram.h" #include "commandParamsUir.h" #include "globalDefinitions.h" #include "uirUtility.h" #include "commandStatusMessage.h" #include "RWlists.h" /****************************************************************************** * Global functions * ******************************************************************************/ /*============================================================================= * commandFunction_compareFiles() *============================================================================= * * * */ ERROR_ID commandFunction_runExternalProgram(COMMAND_FUNC_OPTION funcOption, struct COMMAND *command, UINT8 slotNumber, FILE *file){ struct HOST *host; const int panel=global.panel.commandEdit[RUN_EXTERNAL_PROGRAM_ID]; struct RUN_EXTERNAL_PROGRAM_PARAMS *runExtPrg=&command->params.runExternalProgram; int status; ERROR_ID errorId=SUCCESS; char rodFileName[PATHNAME_LENGTH]; char commandLine[500]; char inputText[1000]; unsigned int time; int programHandle; switch(funcOption){ case COMMAND_TO_LIST: GetCtrlVal(panel, RUN_EXTPRG_EXT_PRG, runExtPrg->externalProgram); GetCtrlVal(panel, RUN_EXTPRG_ROD_FILE, runExtPrg->rodDataFile); GetCtrlVal(panel, RUN_EXTPRG_WAIT_FOR_TERM, &runExtPrg->waitForTermination); GetCtrlVal(panel, RUN_EXTPRG_TIMEOUT, &runExtPrg->timeoutInSeconds); GetCtrlVal(panel, RUN_EXTPRG_DATA_FILE_OPT, &runExtPrg->rodDataFileOption); if(runExtPrg->externalProgram[0]==0) { status = MessagePopup ("WARNING", "No ExternalProgram selected!!"); UIR_STATUS_CHECK(status, MessagePopup()); return(PROGRAM_WARNING); } break; case LIST_TO_COMMAND: SetCtrlVal(panel, RUN_EXTPRG_EXT_PRG, runExtPrg->externalProgram); SetCtrlVal(panel, RUN_EXTPRG_ROD_FILE, runExtPrg->rodDataFile); SetCtrlVal(panel, RUN_EXTPRG_WAIT_FOR_TERM, runExtPrg->waitForTermination); SetCtrlVal(panel, RUN_EXTPRG_TIMEOUT, runExtPrg->timeoutInSeconds); SetCtrlVal(panel, RUN_EXTPRG_DATA_FILE_OPT, runExtPrg->rodDataFileOption); status=SetInputMode(panel, RUN_EXTPRG_ROD_FILE, runExtPrg->rodDataFileOption); UIR_STATUS_CHECK(status, SetInputMode()); break; case COMMAND_EXECUTION: host=global.host[HOST_INDEX(slotNumber)]; strcpy(commandLine, runExtPrg->externalProgram); if(runExtPrg->rodDataFileOption){ if(slotNumber==TIM_SLOT_NUMBER){ strcpy(rodFileName, global.rodDataDir[HOST_INDEX(slotNumber)]); }else{ strcpy(rodFileName, host->dataDir); } strcat(rodFileName, runExtPrg->rodDataFile); strcat(commandLine, " "); /* add space */ strcat(commandLine, rodFileName); } /* run resource manager */ status=LaunchExecutableEx(commandLine, LE_SHOWNORMAL, &programHandle); if(status!=0){ sprintf(inputText,"##RUN_EXT_PROGRAM - LaunchExecutableEx(%s) error!!\n", commandLine); if(slotNumber==TIM_SLOT_NUMBER){ programError(__FILE__, __LINE__, COMMAND_ERROR, "LaunchExecutableEx()", errorId, inputText); }else{ errorId=commandStatusMessage(host, host->commandStatusFile, inputText); HOST_ERROR_CHECK(errorId, host, commandStatusMessage()); } return(COMMAND_ERROR); } if(slotNumber!=TIM_SLOT_NUMBER){ sprintf(inputText,"LaunchExecutable(%s)\n", commandLine); errorId=commandStatusMessage(host, host->commandStatusFile, inputText); HOST_ERROR_CHECK(errorId, host, commandStatusMessage()); } if(!runExtPrg->waitForTermination) break; /* don't wait for termination !!!!*/ /* wait for termination or timeout */ for(time=0; ; ++time){ status=ExecutableHasTerminated(programHandle); if(status==1){ /* executable has terminated */ errorId=commandStatusMessage(host, host->commandStatusFile, "...terminated\n"); HOST_ERROR_CHECK(errorId, host, commandStatusMessage()); break; } if(time>(runExtPrg->timeoutInSeconds)){ /* timeout error */ sprintf(inputText,"##RUN_EXT_PROGRAM - timeout error, (%s) not terminated!!\n", commandLine); if(slotNumber==TIM_SLOT_NUMBER){ programError(__FILE__, __LINE__, COMMAND_ERROR, "ExecutableHasTerminated()", errorId, inputText); }else{ errorId=commandStatusMessage(host, host->commandStatusFile, inputText); HOST_ERROR_CHECK(errorId, host, commandStatusMessage()); } return(COMMAND_ERROR); } Sleep(1000); } break; case SAVE_CMD_TO_FILE: fprintf(file, "waitForTermination= %d, timeoutInSeconds= %d, rodDataFileOption= %d\n", runExtPrg->waitForTermination, runExtPrg->timeoutInSeconds, runExtPrg->rodDataFileOption); fprintf(file, "externalProgram[]= %s\n", runExtPrg->externalProgram); fprintf(file, "rodDataFile[]= %s\n", runExtPrg->rodDataFile); break; case LOAD_CMD_FROM_FILE: status=fscanf(file, "waitForTermination= %d, timeoutInSeconds= %d, rodDataFileOption= %d\n", &runExtPrg->waitForTermination, &runExtPrg->timeoutInSeconds, &runExtPrg->rodDataFileOption); if(status==-1) {ERROR_CHECK(FATAL_ERROR, Read from cmd file error!); return(FATAL_ERROR);} status=readLineFromIndex(file, strlen("externalProgram[]= "), PATHNAME_LENGTH, runExtPrg->externalProgram); if(status!=0) {ERROR_CHECK(FATAL_ERROR, Read from cmd file error!); return(FATAL_ERROR);} status=readLineFromIndex(file, strlen("rodDataFile[]= "), PATHNAME_LENGTH, runExtPrg->rodDataFile); if(status!=0) {ERROR_CHECK(FATAL_ERROR, Read from cmd file error!); return(FATAL_ERROR);} break; default: ; } return(SUCCESS); } /******************************************************************************/