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;
73 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
75 cout << pid << " is here " << commandName << endl;
76 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
78 //executes valid command
79 Command* command = cFactory->getCommand(commandName, options);
80 quitCommandCalled = command->execute();
81 mout->control_pressed = 0;
82 mout->executing = false;
88 mout->mothurOut("Invalid.");
89 mout->mothurOutEndLine();
95 mout->errorOut(e, "InteractEngine", "getInput");
99 /***********************************************************************/
100 string Engine::getCommand() {
103 MPI_Barrier(MPI_COMM_WORLD);
106 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
108 char* nextCommand = NULL;
109 nextCommand = readline("mothur > ");
111 if(nextCommand != NULL) { add_history(nextCommand); }
112 else{ //^D causes null string and we want it to quit mothur
113 nextCommand = "quit";
114 mout->mothurOut(nextCommand);
117 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
120 string nextCommand = "";
121 mout->mothurOut("mothur > ");
122 getline(cin, nextCommand);
123 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
128 string nextCommand = "";
130 mout->mothurOut("mothur > ");
131 getline(cin, nextCommand);
132 mout->mothurOutJustToLog(toString(nextCommand));
139 catch(exception& e) {
140 mout->errorOut(e, "Engine", "getCommand");
144 /***********************************************************************/
145 //This function opens the batchfile to be used by BatchEngine::getInput.
146 BatchEngine::BatchEngine(string path, string batchFileName){
148 globaldata = GlobalData::getInstance();
150 openedBatch = openInputFile(batchFileName, inputBatchFile);
151 globaldata->argv = path;
154 catch(exception& e) {
155 mout->errorOut(e, "BatchEngine", "BatchEngine");
160 /***********************************************************************/
162 BatchEngine::~BatchEngine(){ }
164 /***********************************************************************/
165 //This Function allows the user to run a batchfile containing several commands on Dotur
166 bool BatchEngine::getInput(){
168 //check if this is a valid batchfile
169 if (openedBatch == 1) {
170 mout->mothurOut("unable to open batchfile");
171 mout->mothurOutEndLine();
176 string commandName = "";
179 //CommandFactory cFactory;
180 int quitCommandCalled = 0;
182 while(quitCommandCalled == 0){
184 input = getNextCommand(inputBatchFile);
187 if (input[0] != '#') {
189 mout->mothurOutEndLine();
190 mout->mothurOut("mothur > " + input);
191 mout->mothurOutEndLine();
193 if (mout->control_pressed) { input = "quit()"; }
195 //allow user to omit the () on the quit command
196 if (input == "quit") { input = "quit()"; }
198 CommandOptionParser parser(input);
199 commandName = parser.getCommandString();
200 options = parser.getOptionString();
202 if (commandName != "") {
203 mout->executing = true;
206 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
208 cout << pid << " is here " << commandName << '\t' << count << endl;
209 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
211 //executes valid command
212 Command* command = cFactory->getCommand(commandName, options);
213 quitCommandCalled = command->execute();
214 mout->control_pressed = 0;
215 mout->executing = false;
221 mout->mothurOut("Invalid.");
222 mout->mothurOutEndLine();
226 gobble(inputBatchFile);
229 inputBatchFile.close();
232 catch(exception& e) {
233 mout->errorOut(e, "BatchEngine", "getInput");
237 /***********************************************************************/
238 string BatchEngine::getNextCommand(ifstream& inputBatchFile) {
242 int err = MPI_Barrier(MPI_COMM_WORLD);
243 //cout << "barrier = " << err << '\t' << MPI_SUCCESS << endl;
246 string nextcommand = "";
248 if (inputBatchFile.eof()) { nextcommand = "quit()"; }
249 else { nextcommand = getline(inputBatchFile); }
253 catch(exception& e) {
254 mout->errorOut(e, "BatchEngine", "getNextCommand");
259 /***********************************************************************/
260 /***********************************************************************/
261 //This function opens the batchfile to be used by BatchEngine::getInput.
262 ScriptEngine::ScriptEngine(string path, string commandString){
264 globaldata = GlobalData::getInstance();
267 listOfCommands = commandString.substr(1, (commandString.length()-1));
269 globaldata->argv = path;
272 catch(exception& e) {
273 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
278 /***********************************************************************/
280 ScriptEngine::~ScriptEngine(){ }
282 /***********************************************************************/
283 //This Function allows the user to run a batchfile containing several commands on mothur
284 bool ScriptEngine::getInput(){
288 string commandName = "";
292 //CommandFactory cFactory;
293 int quitCommandCalled = 0;
295 while(quitCommandCalled == 0){
297 input = getNextCommand(listOfCommands);
299 if (input == "") { input = "quit()"; }
301 mout->mothurOutEndLine();
302 mout->mothurOut("mothur > " + input);
303 mout->mothurOutEndLine();
305 if (mout->control_pressed) { input = "quit()"; }
307 //allow user to omit the () on the quit command
308 if (input == "quit") { input = "quit()"; }
310 CommandOptionParser parser(input);
311 commandName = parser.getCommandString();
312 options = parser.getOptionString();
314 if (commandName != "") {
315 mout->executing = true;
317 int pid, numProcesses;
320 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
321 MPI_Comm_size(MPI_COMM_WORLD, &numProcesses);
323 cout << pid << " is here " << commandName << endl;
324 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
325 cout << pid << " is in execute" << endl;
327 //executes valid command
328 Command* command = cFactory->getCommand(commandName, options);
329 quitCommandCalled = command->execute();
330 mout->control_pressed = 0;
331 mout->executing = false;
334 cout << pid << " is done in execute" << endl;
338 mout->mothurOut("Invalid.");
339 mout->mothurOutEndLine();
347 catch(exception& e) {
348 mout->errorOut(e, "ScriptEngine", "getInput");
352 /***********************************************************************/
353 string ScriptEngine::getNextCommand(string& commandString) {
357 MPI_Barrier(MPI_COMM_WORLD);
360 string nextcommand = "";
363 //go through string until you reach ; or end
364 while (count < commandString.length()) {
366 if (commandString[count] == ';') { break; }
367 else { nextcommand += commandString[count]; }
372 //if you are not at the end
373 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
374 else { commandString = ""; }
377 //get rid of spaces in between commands if any
378 if (commandString.length() > 0) {
379 while (commandString[0] == ' ') {
380 commandString = commandString.substr(1,commandString.length());
381 if (commandString.length() == 0) { break; }
387 catch(exception& e) {
388 mout->errorOut(e, "ScriptEngine", "getNextCommand");
392 /***********************************************************************/