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();
57 mout->mothurOutEndLine();
59 if (mout->control_pressed) { input = "quit()"; }
61 //allow user to omit the () on the quit command
62 if (input == "quit") { input = "quit()"; }
64 CommandOptionParser parser(input);
65 commandName = parser.getCommandString();
67 options = parser.getOptionString();
69 if (commandName != "") {
70 mout->executing = true;
74 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
76 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
78 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
80 //executes valid command
81 Command* command = cFactory->getCommand(commandName, options);
82 quitCommandCalled = command->execute();
83 mout->control_pressed = 0;
84 mout->executing = false;
90 mout->mothurOut("Your input contains errors. Please try again.");
91 mout->mothurOutEndLine();
97 mout->errorOut(e, "InteractEngine", "getInput");
101 /***********************************************************************/
102 string Engine::getCommand() {
104 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
106 char* nextCommand = NULL;
107 nextCommand = readline("mothur > ");
109 if(nextCommand != NULL) { add_history(nextCommand); }
110 else{ //^D causes null string and we want it to quit mothur
111 nextCommand = "quit";
112 mout->mothurOut(nextCommand);
115 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
118 string nextCommand = "";
119 mout->mothurOut("mothur > ");
120 getline(cin, nextCommand);
121 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
126 string nextCommand = "";
128 mout->mothurOut("mothur > ");
129 getline(cin, nextCommand);
130 mout->mothurOutJustToLog(toString(nextCommand));
137 catch(exception& e) {
138 mout->errorOut(e, "Engine", "getCommand");
142 /***********************************************************************/
143 //This function opens the batchfile to be used by BatchEngine::getInput.
144 BatchEngine::BatchEngine(string path, string batchFileName){
146 globaldata = GlobalData::getInstance();
148 openedBatch = openInputFile(batchFileName, inputBatchFile);
149 globaldata->argv = path;
152 catch(exception& e) {
153 mout->errorOut(e, "BatchEngine", "BatchEngine");
158 /***********************************************************************/
160 BatchEngine::~BatchEngine(){ }
162 /***********************************************************************/
163 //This Function allows the user to run a batchfile containing several commands on Dotur
164 bool BatchEngine::getInput(){
166 //check if this is a valid batchfile
167 if (openedBatch == 1) {
168 mout->mothurOut("unable to open batchfile");
169 mout->mothurOutEndLine();
174 string commandName = "";
177 //CommandFactory cFactory;
178 int quitCommandCalled = 0;
180 while(quitCommandCalled == 0){
182 if (inputBatchFile.eof()) { input = "quit()"; }
183 else { input = getline(inputBatchFile); }
185 if (input[0] != '#') {
187 mout->mothurOutEndLine();
188 mout->mothurOut("mothur > " + input);
189 mout->mothurOutEndLine();
191 if (mout->control_pressed) { input = "quit()"; }
193 //allow user to omit the () on the quit command
194 if (input == "quit") { input = "quit()"; }
196 CommandOptionParser parser(input);
197 commandName = parser.getCommandString();
198 options = parser.getOptionString();
200 if (commandName != "") {
201 mout->executing = true;
204 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
205 cout << pid << " is waiting " << commandName << endl;
206 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
207 cout << pid << " is here " << commandName << endl;
208 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
210 //executes valid command
211 Command* command = cFactory->getCommand(commandName, options);
212 quitCommandCalled = command->execute();
213 mout->control_pressed = 0;
214 mout->executing = false;
220 mout->mothurOut("Invalid.");
221 mout->mothurOutEndLine();
225 gobble(inputBatchFile);
228 inputBatchFile.close();
231 catch(exception& e) {
232 mout->errorOut(e, "BatchEngine", "getInput");
238 /***********************************************************************/
239 /***********************************************************************/
240 //This function opens the batchfile to be used by BatchEngine::getInput.
241 ScriptEngine::ScriptEngine(string path, string commandString){
243 globaldata = GlobalData::getInstance();
246 listOfCommands = commandString.substr(1, (commandString.length()-1));
248 globaldata->argv = path;
251 catch(exception& e) {
252 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
257 /***********************************************************************/
259 ScriptEngine::~ScriptEngine(){ }
261 /***********************************************************************/
262 //This Function allows the user to run a batchfile containing several commands on mothur
263 bool ScriptEngine::getInput(){
267 string commandName = "";
271 //CommandFactory cFactory;
272 int quitCommandCalled = 0;
274 while(quitCommandCalled == 0){
276 input = getNextCommand(listOfCommands);
278 if (input == "") { input = "quit()"; }
280 mout->mothurOutEndLine();
281 mout->mothurOut("mothur > " + input);
282 mout->mothurOutEndLine();
284 if (mout->control_pressed) { input = "quit()"; }
286 //allow user to omit the () on the quit command
287 if (input == "quit") { input = "quit()"; }
289 CommandOptionParser parser(input);
290 commandName = parser.getCommandString();
291 options = parser.getOptionString();
293 if (commandName != "") {
294 mout->executing = true;
297 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
299 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
301 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
303 //executes valid command
304 Command* command = cFactory->getCommand(commandName, options);
305 quitCommandCalled = command->execute();
306 mout->control_pressed = 0;
307 mout->executing = false;
313 mout->mothurOut("Invalid.");
314 mout->mothurOutEndLine();
321 catch(exception& e) {
322 mout->errorOut(e, "ScriptEngine", "getInput");
326 /***********************************************************************/
327 string ScriptEngine::getNextCommand(string& commandString) {
329 string nextcommand = "";
332 //go through string until you reach ; or end
333 while (count < commandString.length()) {
335 if (commandString[count] == ';') { break; }
336 else { nextcommand += commandString[count]; }
341 //if you are not at the end
342 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
343 else { commandString = ""; }
346 //get rid of spaces in between commands if any
347 if (commandString.length() > 0) {
348 while (commandString[0] == ' ') {
349 commandString = commandString.substr(1,commandString.length());
350 if (commandString.length() == 0) { break; }
356 catch(exception& e) {
357 mout->errorOut(e, "ScriptEngine", "getNextCommand");
361 /***********************************************************************/