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){
54 mout->mothurOutEndLine();
59 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
63 mout->mothurOutEndLine();
69 if (mout->control_pressed) { input = "quit()"; }
71 //allow user to omit the () on the quit command
72 if (input == "quit") { input = "quit()"; }
74 CommandOptionParser parser(input);
75 commandName = parser.getCommandString();
77 options = parser.getOptionString();
79 if (commandName != "") {
80 mout->executing = true;
84 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
86 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
88 //executes valid command
89 Command* command = cFactory->getCommand(commandName, options);
90 quitCommandCalled = command->execute();
91 mout->control_pressed = 0;
92 mout->executing = false;
98 mout->mothurOut("Your input contains errors. Please try again.");
99 mout->mothurOutEndLine();
104 catch(exception& e) {
105 mout->errorOut(e, "InteractEngine", "getInput");
109 /***********************************************************************/
110 string Engine::getCommand() {
112 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
114 char* nextCommand = NULL;
115 nextCommand = readline("mothur > ");
117 if(nextCommand != NULL) { add_history(nextCommand); }
118 else{ //^D causes null string and we want it to quit mothur
119 nextCommand = "quit";
120 cout << nextCommand << endl;
125 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
127 if (pid == 0) { //only one process should output to screen
130 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
138 string nextCommand = "";
141 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
143 if (pid == 0) { //only one process should output to screen
146 mout->mothurOut("mothur > ");
152 getline(cin, nextCommand);
156 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
158 if (pid == 0) { //only one process should output to screen
161 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
170 string nextCommand = "";
173 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
175 if (pid == 0) { //only one process should output to screen
178 mout->mothurOut("mothur > ");
184 getline(cin, nextCommand);
188 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
190 if (pid == 0) { //only one process should output to screen
193 mout->mothurOutJustToLog(toString(nextCommand));
202 mout->mothurOutEndLine();
205 catch(exception& e) {
206 mout->errorOut(e, "Engine", "getCommand");
210 /***********************************************************************/
211 //This function opens the batchfile to be used by BatchEngine::getInput.
212 BatchEngine::BatchEngine(string path, string batchFileName){
214 globaldata = GlobalData::getInstance();
216 openedBatch = openInputFile(batchFileName, inputBatchFile);
217 globaldata->argv = path;
220 catch(exception& e) {
221 mout->errorOut(e, "BatchEngine", "BatchEngine");
226 /***********************************************************************/
228 BatchEngine::~BatchEngine(){ }
230 /***********************************************************************/
231 //This Function allows the user to run a batchfile containing several commands on Dotur
232 bool BatchEngine::getInput(){
234 //check if this is a valid batchfile
235 if (openedBatch == 1) {
236 mout->mothurOut("unable to open batchfile");
237 mout->mothurOutEndLine();
242 string commandName = "";
245 //CommandFactory cFactory;
246 int quitCommandCalled = 0;
248 while(quitCommandCalled == 0){
250 if (inputBatchFile.eof()) { input = "quit()"; }
251 else { input = getline(inputBatchFile); }
253 if (input[0] != '#') {
257 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
259 if (pid == 0) { //only one process should output to screen
262 mout->mothurOutEndLine();
263 mout->mothurOut("mothur > " + input);
264 mout->mothurOutEndLine();
270 if (mout->control_pressed) { input = "quit()"; }
272 //allow user to omit the () on the quit command
273 if (input == "quit") { input = "quit()"; }
275 CommandOptionParser parser(input);
276 commandName = parser.getCommandString();
277 options = parser.getOptionString();
279 if (commandName != "") {
280 mout->executing = true;
283 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
285 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
287 //executes valid command
288 Command* command = cFactory->getCommand(commandName, options);
289 quitCommandCalled = command->execute();
290 mout->control_pressed = 0;
291 mout->executing = false;
297 mout->mothurOut("Invalid.");
298 mout->mothurOutEndLine();
302 gobble(inputBatchFile);
305 inputBatchFile.close();
308 catch(exception& e) {
309 mout->errorOut(e, "BatchEngine", "getInput");
315 /***********************************************************************/
316 /***********************************************************************/
317 //This function opens the batchfile to be used by BatchEngine::getInput.
318 ScriptEngine::ScriptEngine(string path, string commandString){
320 globaldata = GlobalData::getInstance();
323 listOfCommands = commandString.substr(1, (commandString.length()-1));
325 globaldata->argv = path;
328 catch(exception& e) {
329 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
334 /***********************************************************************/
336 ScriptEngine::~ScriptEngine(){ }
338 /***********************************************************************/
339 //This Function allows the user to run a batchfile containing several commands on mothur
340 bool ScriptEngine::getInput(){
344 string commandName = "";
348 //CommandFactory cFactory;
349 int quitCommandCalled = 0;
351 while(quitCommandCalled == 0){
353 input = getNextCommand(listOfCommands);
355 if (input == "") { input = "quit()"; }
359 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
364 mout->mothurOutEndLine();
365 mout->mothurOut("mothur > " + input);
366 mout->mothurOutEndLine();
372 if (mout->control_pressed) { input = "quit()"; }
374 //allow user to omit the () on the quit command
375 if (input == "quit") { input = "quit()"; }
377 CommandOptionParser parser(input);
378 commandName = parser.getCommandString();
379 options = parser.getOptionString();
381 if (commandName != "") {
382 mout->executing = true;
385 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
387 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
389 //executes valid command
390 Command* command = cFactory->getCommand(commandName, options);
391 quitCommandCalled = command->execute();
392 mout->control_pressed = 0;
393 mout->executing = false;
399 mout->mothurOut("Invalid.");
400 mout->mothurOutEndLine();
407 catch(exception& e) {
408 mout->errorOut(e, "ScriptEngine", "getInput");
412 /***********************************************************************/
413 string ScriptEngine::getNextCommand(string& commandString) {
415 string nextcommand = "";
418 //go through string until you reach ; or end
419 while (count < commandString.length()) {
421 if (commandString[count] == ';') { break; }
422 else { nextcommand += commandString[count]; }
427 //if you are not at the end
428 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
429 else { commandString = ""; }
432 //get rid of spaces in between commands if any
433 if (commandString.length() > 0) {
434 while (commandString[0] == ' ') {
435 commandString = commandString.substr(1,commandString.length());
436 if (commandString.length() == 0) { break; }
442 catch(exception& e) {
443 mout->errorOut(e, "ScriptEngine", "getNextCommand");
447 /***********************************************************************/