5 * Created by Pat Schloss on 8/15/08.
6 * Copyright 2008 Patrick D. Schloss. All rights reserved.
8 * There's a TON of duplicated code between InteractEngine and BatchEngine
9 * I couldn't figure out how to transition between ifstream (batch) and cin (interact)
10 * Fix later, don't have time now.
17 /***********************************************************************/
20 cFactory = CommandFactory::getInstance();
21 mout = MothurOut::getInstance();
24 mout->errorOut(e, "Engine", "Engine");
29 /***********************************************************************/
31 InteractEngine::InteractEngine(string path){
33 globaldata = GlobalData::getInstance();
34 globaldata->argv = path;
38 /***********************************************************************/
40 InteractEngine::~InteractEngine(){}
42 /***********************************************************************/
43 //This function allows the user to input commands one line at a time until they quit.
44 //If the command is garbage it does nothing.
45 bool InteractEngine::getInput(){
48 string commandName = "";
50 int quitCommandCalled = 0;
52 while(quitCommandCalled != 1){
55 mout->mothurOutEndLine();
58 mout->mothurOutEndLine();
60 if (mout->control_pressed) { input = "quit()"; }
62 //allow user to omit the () on the quit command
63 if (input == "quit") { input = "quit()"; }
65 CommandOptionParser parser(input);
66 commandName = parser.getCommandString();
68 options = parser.getOptionString();
70 if (commandName != "") {
71 mout->executing = true;
74 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
76 //cout << pid << " is here " << commandName << endl;
77 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
79 //executes valid command
80 Command* command = cFactory->getCommand(commandName, options);
81 quitCommandCalled = command->execute();
82 mout->control_pressed = 0;
83 mout->executing = false;
89 mout->mothurOut("Invalid.");
90 mout->mothurOutEndLine();
96 mout->errorOut(e, "InteractEngine", "getInput");
100 /***********************************************************************/
101 string Engine::getCommand() {
104 MPI_Barrier(MPI_COMM_WORLD);
107 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
109 char* nextCommand = NULL;
110 nextCommand = readline("mothur > ");
112 if(nextCommand != NULL) { add_history(nextCommand); }
113 else{ //^D causes null string and we want it to quit mothur
114 nextCommand = "quit";
115 mout->mothurOut(nextCommand);
118 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
121 string nextCommand = "";
122 mout->mothurOut("mothur > ");
123 getline(cin, nextCommand);
124 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
129 string nextCommand = "";
131 mout->mothurOut("mothur > ");
132 getline(cin, nextCommand);
133 mout->mothurOutJustToLog(toString(nextCommand));
140 catch(exception& e) {
141 mout->errorOut(e, "Engine", "getCommand");
145 /***********************************************************************/
146 //This function opens the batchfile to be used by BatchEngine::getInput.
147 BatchEngine::BatchEngine(string path, string batchFileName){
149 globaldata = GlobalData::getInstance();
151 openedBatch = openInputFile(batchFileName, inputBatchFile);
152 globaldata->argv = path;
155 catch(exception& e) {
156 mout->errorOut(e, "BatchEngine", "BatchEngine");
161 /***********************************************************************/
163 BatchEngine::~BatchEngine(){ }
165 /***********************************************************************/
166 //This Function allows the user to run a batchfile containing several commands on Dotur
167 bool BatchEngine::getInput(){
169 //check if this is a valid batchfile
170 if (openedBatch == 1) {
171 mout->mothurOut("unable to open batchfile");
172 mout->mothurOutEndLine();
177 string commandName = "";
180 //CommandFactory cFactory;
181 int quitCommandCalled = 0;
183 while(quitCommandCalled == 0){
185 input = getNextCommand(inputBatchFile);
188 if (input[0] != '#') {
190 mout->mothurOutEndLine();
191 mout->mothurOut("mothur > " + input);
192 mout->mothurOutEndLine();
194 if (mout->control_pressed) { input = "quit()"; }
196 //allow user to omit the () on the quit command
197 if (input == "quit") { input = "quit()"; }
199 CommandOptionParser parser(input);
200 commandName = parser.getCommandString();
201 options = parser.getOptionString();
203 if (commandName != "") {
204 mout->executing = true;
207 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
209 //cout << pid << " is here " << commandName << '\t' << count << endl;
210 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
212 //executes valid command
213 Command* command = cFactory->getCommand(commandName, options);
214 quitCommandCalled = command->execute();
215 mout->control_pressed = 0;
216 mout->executing = false;
222 mout->mothurOut("Invalid.");
223 mout->mothurOutEndLine();
227 gobble(inputBatchFile);
230 inputBatchFile.close();
233 catch(exception& e) {
234 mout->errorOut(e, "BatchEngine", "getInput");
238 /***********************************************************************/
239 string BatchEngine::getNextCommand(ifstream& inputBatchFile) {
243 int err = MPI_Barrier(MPI_COMM_WORLD);
244 //cout << "barrier = " << err << '\t' << MPI_SUCCESS << endl;
247 string nextcommand = "";
249 if (inputBatchFile.eof()) { nextcommand = "quit()"; }
250 else { nextcommand = getline(inputBatchFile); }
254 catch(exception& e) {
255 mout->errorOut(e, "BatchEngine", "getNextCommand");
260 /***********************************************************************/
261 /***********************************************************************/
262 //This function opens the batchfile to be used by BatchEngine::getInput.
263 ScriptEngine::ScriptEngine(string path, string commandString){
265 globaldata = GlobalData::getInstance();
268 listOfCommands = commandString.substr(1, (commandString.length()-1));
270 globaldata->argv = path;
273 catch(exception& e) {
274 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
279 /***********************************************************************/
281 ScriptEngine::~ScriptEngine(){ }
283 /***********************************************************************/
284 //This Function allows the user to run a batchfile containing several commands on mothur
285 bool ScriptEngine::getInput(){
289 string commandName = "";
293 //CommandFactory cFactory;
294 int quitCommandCalled = 0;
296 while(quitCommandCalled == 0){
298 input = getNextCommand(listOfCommands);
300 if (input == "") { input = "quit()"; }
302 mout->mothurOutEndLine();
303 mout->mothurOut("mothur > " + input);
304 mout->mothurOutEndLine();
306 if (mout->control_pressed) { input = "quit()"; }
308 //allow user to omit the () on the quit command
309 if (input == "quit") { input = "quit()"; }
311 CommandOptionParser parser(input);
312 commandName = parser.getCommandString();
313 options = parser.getOptionString();
315 if (commandName != "") {
316 mout->executing = true;
318 int pid, numProcesses;
321 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
322 MPI_Comm_size(MPI_COMM_WORLD, &numProcesses);
324 //cout << pid << " is here " << commandName << endl;
325 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
326 //cout << pid << " is in execute" << endl;
328 //executes valid command
329 Command* command = cFactory->getCommand(commandName, options);
330 quitCommandCalled = command->execute();
331 mout->control_pressed = 0;
332 mout->executing = false;
335 //cout << pid << " is done in execute" << endl;
339 mout->mothurOut("Invalid.");
340 mout->mothurOutEndLine();
348 catch(exception& e) {
349 mout->errorOut(e, "ScriptEngine", "getInput");
353 /***********************************************************************/
354 string ScriptEngine::getNextCommand(string& commandString) {
358 MPI_Barrier(MPI_COMM_WORLD);
361 string nextcommand = "";
364 //go through string until you reach ; or end
365 while (count < commandString.length()) {
367 if (commandString[count] == ';') { break; }
368 else { nextcommand += commandString[count]; }
373 //if you are not at the end
374 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
375 else { commandString = ""; }
378 //get rid of spaces in between commands if any
379 if (commandString.length() > 0) {
380 while (commandString[0] == ' ') {
381 commandString = commandString.substr(1,commandString.length());
382 if (commandString.length() == 0) { break; }
388 catch(exception& e) {
389 mout->errorOut(e, "ScriptEngine", "getNextCommand");
393 /***********************************************************************/