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");
28 /***********************************************************************/
29 string Engine::findMothursPath(){
32 string envPath = getenv("PATH");
33 string mothurPath = "";
35 //delimiting path char
37 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
43 //break apart path variable by ':'
45 mout->splitAtChar(envPath, dirs, delim);
47 //get path related to mothur
48 for (int i = 0; i < dirs.size(); i++) {
49 //to lower so we can find it
50 string tempLower = "";
51 for (int j = 0; j < dirs[i].length(); j++) { tempLower += tolower(dirs[i][j]); }
53 //is this mothurs path?
54 if (tempLower.find("mothur") != -1) { mothurPath = dirs[i]; break; }
57 if (mothurPath != "") {
58 //add mothur so it looks like what argv would look like
59 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
60 mothurPath += "/mothur";
62 mothurPath += "\\mothur";
65 //okay mothur is not in the path, so the folder mothur is in must be in the path
66 //lets find out which one
68 //get path related to mothur
69 for (int i = 0; i < dirs.size(); i++) {
71 //is this mothurs path?
73 string tempIn = dirs[i];
74 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
79 mout->openInputFile(tempIn, in, "");
82 if (in) { in.close(); mothurPath = tempIn; break; }
90 mout->errorOut(e, "Engine", "findMothursPath");
94 /***********************************************************************/
96 InteractEngine::InteractEngine(string path){
99 string temppath = path.substr(0, (path.find_last_of("othur")-5));
101 //this will happen if you set the path variable to contain mothur's exe location
102 if (temppath == "") { path = findMothursPath(); }
107 /***********************************************************************/
109 InteractEngine::~InteractEngine(){}
111 /***********************************************************************/
112 //This function allows the user to input commands one line at a time until they quit.
113 //If the command is garbage it does nothing.
114 bool InteractEngine::getInput(){
117 string commandName = "";
119 int quitCommandCalled = 0;
121 while(quitCommandCalled != 1){
126 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
127 MPI_Comm_size(MPI_COMM_WORLD, &processors);
133 mout->mothurOutEndLine();
135 input = getCommand();
136 mout->mothurOutEndLine();
138 if (mout->control_pressed) { input = "quit()"; }
140 //allow user to omit the () on the quit command
141 if (input == "quit") { input = "quit()"; }
146 for(int i = 1; i < processors; i++) {
147 int length = input.length();
148 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
149 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
154 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
156 char* tempBuf = new char[length];
157 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
160 if (input.length() > length) { input = input.substr(0, length); }
167 CommandOptionParser parser(input);
168 commandName = parser.getCommandString();
170 options = parser.getOptionString();
172 if (commandName != "") {
173 mout->executing = true;
176 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
178 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
179 //cout << pid << " is in execute " << commandName << endl;
181 //executes valid command
182 Command* command = cFactory->getCommand(commandName, options);
183 quitCommandCalled = command->execute();
185 //if we aborted command
186 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
188 mout->control_pressed = 0;
189 mout->executing = false;
195 mout->mothurOut("Invalid.");
196 mout->mothurOutEndLine();
201 catch(exception& e) {
202 mout->errorOut(e, "InteractEngine", "getInput");
206 /***********************************************************************/
207 string Engine::getCommand() {
210 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
212 char* nextCommand = NULL;
213 nextCommand = readline("mothur > ");
215 if(nextCommand != NULL) { add_history(nextCommand); }
216 else{ //^D causes null string and we want it to quit mothur
217 strcpy(nextCommand, "quit");
218 mout->mothurOut(nextCommand);
221 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
224 string nextCommand = "";
225 mout->mothurOut("mothur > ");
226 getline(cin, nextCommand);
227 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
232 string nextCommand = "";
234 mout->mothurOut("mothur > ");
235 getline(cin, nextCommand);
236 mout->mothurOutJustToLog(toString(nextCommand));
243 catch(exception& e) {
244 mout->errorOut(e, "Engine", "getCommand");
248 /***********************************************************************/
249 //This function opens the batchfile to be used by BatchEngine::getInput.
250 BatchEngine::BatchEngine(string path, string batchFileName){
253 openedBatch = mout->openInputFile(batchFileName, inputBatchFile);
255 string temppath = path.substr(0, (path.find_last_of("othur")-5));
257 //this will happen if you set the path variable to contain mothur's exe location
258 if (temppath == "") { path = findMothursPath(); }
263 catch(exception& e) {
264 mout->errorOut(e, "BatchEngine", "BatchEngine");
269 /***********************************************************************/
271 BatchEngine::~BatchEngine(){ }
273 /***********************************************************************/
274 //This Function allows the user to run a batchfile containing several commands on Dotur
275 bool BatchEngine::getInput(){
277 //check if this is a valid batchfile
278 if (openedBatch == 1) {
279 mout->mothurOut("unable to open batchfile");
280 mout->mothurOutEndLine();
285 string commandName = "";
288 //CommandFactory cFactory;
289 int quitCommandCalled = 0;
291 while(quitCommandCalled != 1){
296 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
297 MPI_Comm_size(MPI_COMM_WORLD, &processors);
303 input = getNextCommand(inputBatchFile);
308 for(int i = 1; i < processors; i++) {
309 int length = input.length();
310 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
311 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
316 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
318 char* tempBuf = new char[length];
319 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
322 if (input.length() > length) { input = input.substr(0, length); }
331 if (input[0] != '#') {
333 mout->mothurOutEndLine();
334 mout->mothurOut("mothur > " + input);
335 mout->mothurOutEndLine();
337 if (mout->control_pressed) { input = "quit()"; }
339 //allow user to omit the () on the quit command
340 if (input == "quit") { input = "quit()"; }
342 CommandOptionParser parser(input);
343 commandName = parser.getCommandString();
344 options = parser.getOptionString();
346 if (commandName != "") {
347 mout->executing = true;
350 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
352 //cout << pid << " is here " << commandName << '\t' << count << endl;
353 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
355 //executes valid command
356 Command* command = cFactory->getCommand(commandName, options);
357 quitCommandCalled = command->execute();
359 //if we aborted command
360 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
362 mout->control_pressed = 0;
363 mout->executing = false;
369 mout->mothurOut("Invalid.");
370 mout->mothurOutEndLine();
374 mout->gobble(inputBatchFile);
377 inputBatchFile.close();
380 catch(exception& e) {
381 mout->errorOut(e, "BatchEngine", "getInput");
385 /***********************************************************************/
386 string BatchEngine::getNextCommand(ifstream& inputBatchFile) {
389 string nextcommand = "";
391 if (inputBatchFile.eof()) { nextcommand = "quit()"; }
392 else { nextcommand = mout->getline(inputBatchFile); }
396 catch(exception& e) {
397 mout->errorOut(e, "BatchEngine", "getNextCommand");
402 /***********************************************************************/
403 /***********************************************************************/
404 //This function opens the batchfile to be used by BatchEngine::getInput.
405 ScriptEngine::ScriptEngine(string path, string commandString){
409 listOfCommands = commandString.substr(1, (commandString.length()-1));
411 string temppath = path.substr(0, (path.find_last_of("othur")-5));
413 //this will happen if you set the path variable to contain mothur's exe location
414 if (temppath == "") { path = findMothursPath(); }
419 catch(exception& e) {
420 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
425 /***********************************************************************/
427 ScriptEngine::~ScriptEngine(){ }
429 /***********************************************************************/
430 //This Function allows the user to run a batchfile containing several commands on mothur
431 bool ScriptEngine::getInput(){
435 string commandName = "";
439 //CommandFactory cFactory;
440 int quitCommandCalled = 0;
442 while(quitCommandCalled != 1){
447 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
448 MPI_Comm_size(MPI_COMM_WORLD, &processors);
454 input = getNextCommand(listOfCommands);
456 if (input == "") { input = "quit()"; }
458 mout->mothurOutEndLine();
459 mout->mothurOut("mothur > " + input);
460 mout->mothurOutEndLine();
464 for(int i = 1; i < processors; i++) {
465 int length = input.length();
466 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
467 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
472 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
474 char* tempBuf = new char[length];
475 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
478 if (input.length() > length) { input = input.substr(0, length); }
486 if (mout->control_pressed) { input = "quit()"; }
488 //allow user to omit the () on the quit command
489 if (input == "quit") { input = "quit()"; }
491 CommandOptionParser parser(input);
492 commandName = parser.getCommandString();
493 options = parser.getOptionString();
495 if (commandName != "") {
496 mout->executing = true;
498 int pid, numProcesses;
500 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
501 MPI_Comm_size(MPI_COMM_WORLD, &numProcesses);
503 //cout << pid << " is here " << commandName << endl;
504 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
505 //cout << pid << " is in execute" << endl;
507 //executes valid command
508 Command* command = cFactory->getCommand(commandName, options);
509 quitCommandCalled = command->execute();
511 //if we aborted command
512 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
514 mout->control_pressed = 0;
515 mout->executing = false;
518 //cout << pid << " is done in execute" << endl;
522 mout->mothurOut("Invalid.");
523 mout->mothurOutEndLine();
531 catch(exception& e) {
532 mout->errorOut(e, "ScriptEngine", "getInput");
536 /***********************************************************************/
537 string ScriptEngine::getNextCommand(string& commandString) {
540 string nextcommand = "";
542 bool ignoreSemiColons = false;
544 //go through string until you reach ; or end
545 while (count < commandString.length()) {
547 //you want to ignore any ; until you reach the next '
548 if ((commandString[count] == '\'') && (!ignoreSemiColons)) { ignoreSemiColons = true; }
549 else if ((commandString[count] == '\'') && (ignoreSemiColons)) { ignoreSemiColons = false; }
551 if ((commandString[count] == ';') && (!ignoreSemiColons)) { break; }
552 else { nextcommand += commandString[count]; }
557 //if you are not at the end
558 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
559 else { commandString = ""; }
562 //get rid of spaces in between commands if any
563 if (commandString.length() > 0) {
564 while (commandString[0] == ' ') {
565 commandString = commandString.substr(1,commandString.length());
566 if (commandString.length() == 0) { break; }
572 catch(exception& e) {
573 mout->errorOut(e, "ScriptEngine", "getNextCommand");
577 /***********************************************************************/