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 mout->runParse = true;
183 mout->Groups.clear();
184 mout->namesOfGroups.clear();
185 mout->Treenames.clear();
187 mout->saveNextLabel = "";
189 Command* command = cFactory->getCommand(commandName, options);
190 quitCommandCalled = command->execute();
192 //if we aborted command
193 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
195 mout->control_pressed = 0;
196 mout->executing = false;
202 mout->mothurOut("Invalid.");
203 mout->mothurOutEndLine();
208 catch(exception& e) {
209 mout->errorOut(e, "InteractEngine", "getInput");
213 /***********************************************************************/
214 string Engine::getCommand() {
217 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
219 char* nextCommand = NULL;
220 nextCommand = readline("mothur > ");
222 if(nextCommand != NULL) { add_history(nextCommand); }
223 else{ //^D causes null string and we want it to quit mothur
224 strcpy(nextCommand, "quit");
225 mout->mothurOut(nextCommand);
228 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
231 string nextCommand = "";
232 mout->mothurOut("mothur > ");
233 getline(cin, nextCommand);
234 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
239 string nextCommand = "";
241 mout->mothurOut("mothur > ");
242 getline(cin, nextCommand);
243 mout->mothurOutJustToLog(toString(nextCommand));
250 catch(exception& e) {
251 mout->errorOut(e, "Engine", "getCommand");
255 /***********************************************************************/
256 //This function opens the batchfile to be used by BatchEngine::getInput.
257 BatchEngine::BatchEngine(string path, string batchFileName){
260 openedBatch = mout->openInputFile(batchFileName, inputBatchFile);
262 string temppath = path.substr(0, (path.find_last_of("othur")-5));
264 //this will happen if you set the path variable to contain mothur's exe location
265 if (temppath == "") { path = findMothursPath(); }
270 catch(exception& e) {
271 mout->errorOut(e, "BatchEngine", "BatchEngine");
276 /***********************************************************************/
278 BatchEngine::~BatchEngine(){ }
280 /***********************************************************************/
281 //This Function allows the user to run a batchfile containing several commands on Dotur
282 bool BatchEngine::getInput(){
284 //check if this is a valid batchfile
285 if (openedBatch == 1) {
286 mout->mothurOut("unable to open batchfile");
287 mout->mothurOutEndLine();
292 string commandName = "";
295 //CommandFactory cFactory;
296 int quitCommandCalled = 0;
298 while(quitCommandCalled != 1){
303 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
304 MPI_Comm_size(MPI_COMM_WORLD, &processors);
310 input = getNextCommand(inputBatchFile);
315 for(int i = 1; i < processors; i++) {
316 int length = input.length();
317 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
318 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
323 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
325 char* tempBuf = new char[length];
326 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
329 if (input.length() > length) { input = input.substr(0, length); }
338 if (input[0] != '#') {
340 mout->mothurOutEndLine();
341 mout->mothurOut("mothur > " + input);
342 mout->mothurOutEndLine();
344 if (mout->control_pressed) { input = "quit()"; }
346 //allow user to omit the () on the quit command
347 if (input == "quit") { input = "quit()"; }
349 CommandOptionParser parser(input);
350 commandName = parser.getCommandString();
351 options = parser.getOptionString();
353 if (commandName != "") {
354 mout->executing = true;
357 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
359 //cout << pid << " is here " << commandName << '\t' << count << endl;
360 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
362 //executes valid command
363 mout->runParse = true;
364 mout->Groups.clear();
365 mout->namesOfGroups.clear();
366 mout->Treenames.clear();
368 mout->saveNextLabel = "";
370 Command* command = cFactory->getCommand(commandName, options);
371 quitCommandCalled = command->execute();
373 //if we aborted command
374 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
376 mout->control_pressed = 0;
377 mout->executing = false;
383 mout->mothurOut("Invalid.");
384 mout->mothurOutEndLine();
388 mout->gobble(inputBatchFile);
391 inputBatchFile.close();
394 catch(exception& e) {
395 mout->errorOut(e, "BatchEngine", "getInput");
399 /***********************************************************************/
400 string BatchEngine::getNextCommand(ifstream& inputBatchFile) {
403 string nextcommand = "";
405 if (inputBatchFile.eof()) { nextcommand = "quit()"; }
406 else { nextcommand = mout->getline(inputBatchFile); }
410 catch(exception& e) {
411 mout->errorOut(e, "BatchEngine", "getNextCommand");
416 /***********************************************************************/
417 /***********************************************************************/
418 //This function opens the batchfile to be used by BatchEngine::getInput.
419 ScriptEngine::ScriptEngine(string path, string commandString){
423 listOfCommands = commandString.substr(1, (commandString.length()-1));
425 string temppath = path.substr(0, (path.find_last_of("othur")-5));
427 //this will happen if you set the path variable to contain mothur's exe location
428 if (temppath == "") { path = findMothursPath(); }
433 catch(exception& e) {
434 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
439 /***********************************************************************/
441 ScriptEngine::~ScriptEngine(){ }
443 /***********************************************************************/
444 //This Function allows the user to run a batchfile containing several commands on mothur
445 bool ScriptEngine::getInput(){
449 string commandName = "";
453 //CommandFactory cFactory;
454 int quitCommandCalled = 0;
456 while(quitCommandCalled != 1){
461 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
462 MPI_Comm_size(MPI_COMM_WORLD, &processors);
468 input = getNextCommand(listOfCommands);
470 if (input == "") { input = "quit()"; }
473 if ((input.find("quit") != string::npos) || (input.find("set.logfile") != string::npos)) {}
474 else if ((input.find("get.current") != string::npos) && (!mout->hasCurrentFiles())) {}
475 else { mout->mothurOutEndLine(); mout->mothurOut("mothur > " + input); mout->mothurOutEndLine(); }
477 mout->mothurOutEndLine(); mout->mothurOut("mothur > " + input); mout->mothurOutEndLine();
482 for(int i = 1; i < processors; i++) {
483 int length = input.length();
484 MPI_Send(&length, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
485 MPI_Send(&input[0], length, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
490 MPI_Recv(&length, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
492 char* tempBuf = new char[length];
493 MPI_Recv(&tempBuf[0], length, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
496 if (input.length() > length) { input = input.substr(0, length); }
504 if (mout->control_pressed) { input = "quit()"; }
506 //allow user to omit the () on the quit command
507 if (input == "quit") { input = "quit()"; }
509 CommandOptionParser parser(input);
510 commandName = parser.getCommandString();
511 options = parser.getOptionString();
513 if (commandName != "") {
514 mout->executing = true;
516 int pid, numProcesses;
518 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
519 MPI_Comm_size(MPI_COMM_WORLD, &numProcesses);
521 //cout << pid << " is here " << commandName << endl;
522 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
523 //cout << pid << " is in execute" << endl;
525 //executes valid command
526 mout->runParse = true;
527 mout->Groups.clear();
528 mout->namesOfGroups.clear();
529 mout->Treenames.clear();
531 mout->saveNextLabel = "";
533 Command* command = cFactory->getCommand(commandName, options);
534 quitCommandCalled = command->execute();
536 //if we aborted command
537 if (quitCommandCalled == 2) { mout->mothurOut("[ERROR]: did not complete " + commandName + "."); mout->mothurOutEndLine(); }
539 mout->control_pressed = 0;
540 mout->executing = false;
543 //cout << pid << " is done in execute" << endl;
547 mout->mothurOut("Invalid.");
548 mout->mothurOutEndLine();
556 catch(exception& e) {
557 mout->errorOut(e, "ScriptEngine", "getInput");
561 /***********************************************************************/
562 string ScriptEngine::getNextCommand(string& commandString) {
565 string nextcommand = "";
567 bool ignoreSemiColons = false;
569 //go through string until you reach ; or end
570 while (count < commandString.length()) {
572 //you want to ignore any ; until you reach the next '
573 if ((commandString[count] == '\'') && (!ignoreSemiColons)) { ignoreSemiColons = true; }
574 else if ((commandString[count] == '\'') && (ignoreSemiColons)) { ignoreSemiColons = false; }
576 if ((commandString[count] == ';') && (!ignoreSemiColons)) { break; }
577 else { nextcommand += commandString[count]; }
582 //if you are not at the end
583 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
584 else { commandString = ""; }
587 //get rid of spaces in between commands if any
588 if (commandString.length() > 0) {
589 while (commandString[0] == ' ') {
590 commandString = commandString.substr(1,commandString.length());
591 if (commandString.length() == 0) { break; }
597 catch(exception& e) {
598 mout->errorOut(e, "ScriptEngine", "getNextCommand");
602 /***********************************************************************/