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 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("Your input contains errors. Please try again.");
89 mout->mothurOutEndLine();
95 mout->errorOut(e, "InteractEngine", "getInput");
99 /***********************************************************************/
100 string Engine::getCommand() {
102 #ifdef USE_MPI //mpirun doesn't work with readline
103 string nextCommand = "";
105 mout->mothurOut("mothur > ");
106 getline(cin, nextCommand);
107 mout->mothurOutJustToLog(toString(nextCommand));
111 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
113 char* nextCommand = NULL;
114 nextCommand = readline("mothur > ");
116 if(nextCommand != NULL) { add_history(nextCommand); }
117 else{ //^D causes null string and we want it to quit mothur
118 nextCommand = "quit";
119 mout->mothurOut(nextCommand);
122 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
125 string nextCommand = "";
126 mout->mothurOut("mothur > ");
127 getline(cin, nextCommand);
128 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
133 string nextCommand = "";
135 mout->mothurOut("mothur > ");
136 getline(cin, nextCommand);
137 mout->mothurOutJustToLog(toString(nextCommand));
144 catch(exception& e) {
145 mout->errorOut(e, "Engine", "getCommand");
149 /***********************************************************************/
150 //This function opens the batchfile to be used by BatchEngine::getInput.
151 BatchEngine::BatchEngine(string path, string batchFileName){
153 globaldata = GlobalData::getInstance();
155 openedBatch = openInputFile(batchFileName, inputBatchFile);
156 globaldata->argv = path;
159 catch(exception& e) {
160 mout->errorOut(e, "BatchEngine", "BatchEngine");
165 /***********************************************************************/
167 BatchEngine::~BatchEngine(){ }
169 /***********************************************************************/
170 //This Function allows the user to run a batchfile containing several commands on Dotur
171 bool BatchEngine::getInput(){
173 //check if this is a valid batchfile
174 if (openedBatch == 1) {
175 mout->mothurOut("unable to open batchfile");
176 mout->mothurOutEndLine();
181 string commandName = "";
184 //CommandFactory cFactory;
185 int quitCommandCalled = 0;
187 while(quitCommandCalled == 0){
189 if (inputBatchFile.eof()) { input = "quit()"; }
190 else { input = getline(inputBatchFile); }
192 if (input[0] != '#') {
194 mout->mothurOutEndLine();
195 mout->mothurOut("mothur > " + input);
196 mout->mothurOutEndLine();
198 if (mout->control_pressed) { input = "quit()"; }
200 //allow user to omit the () on the quit command
201 if (input == "quit") { input = "quit()"; }
203 CommandOptionParser parser(input);
204 commandName = parser.getCommandString();
205 options = parser.getOptionString();
207 if (commandName != "") {
208 mout->executing = true;
211 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
213 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
215 //executes valid command
216 Command* command = cFactory->getCommand(commandName, options);
217 quitCommandCalled = command->execute();
218 mout->control_pressed = 0;
219 mout->executing = false;
225 mout->mothurOut("Invalid.");
226 mout->mothurOutEndLine();
230 gobble(inputBatchFile);
233 inputBatchFile.close();
236 catch(exception& e) {
237 mout->errorOut(e, "BatchEngine", "getInput");
243 /***********************************************************************/
244 /***********************************************************************/
245 //This function opens the batchfile to be used by BatchEngine::getInput.
246 ScriptEngine::ScriptEngine(string path, string commandString){
248 globaldata = GlobalData::getInstance();
251 listOfCommands = commandString.substr(1, (commandString.length()-1));
253 globaldata->argv = path;
256 catch(exception& e) {
257 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
262 /***********************************************************************/
264 ScriptEngine::~ScriptEngine(){ }
266 /***********************************************************************/
267 //This Function allows the user to run a batchfile containing several commands on mothur
268 bool ScriptEngine::getInput(){
272 string commandName = "";
276 //CommandFactory cFactory;
277 int quitCommandCalled = 0;
279 while(quitCommandCalled == 0){
281 input = getNextCommand(listOfCommands);
283 if (input == "") { input = "quit()"; }
285 mout->mothurOutEndLine();
286 mout->mothurOut("mothur > " + input);
287 mout->mothurOutEndLine();
289 if (mout->control_pressed) { input = "quit()"; }
291 //allow user to omit the () on the quit command
292 if (input == "quit") { input = "quit()"; }
294 CommandOptionParser parser(input);
295 commandName = parser.getCommandString();
296 options = parser.getOptionString();
298 if (commandName != "") {
299 mout->executing = true;
302 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
304 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
306 //executes valid command
307 Command* command = cFactory->getCommand(commandName, options);
308 quitCommandCalled = command->execute();
309 mout->control_pressed = 0;
310 mout->executing = false;
316 mout->mothurOut("Invalid.");
317 mout->mothurOutEndLine();
324 catch(exception& e) {
325 mout->errorOut(e, "ScriptEngine", "getInput");
329 /***********************************************************************/
330 string ScriptEngine::getNextCommand(string& commandString) {
332 string nextcommand = "";
335 //go through string until you reach ; or end
336 while (count < commandString.length()) {
338 if (commandString[count] == ';') { break; }
339 else { nextcommand += commandString[count]; }
344 //if you are not at the end
345 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
346 else { commandString = ""; }
349 //get rid of spaces in between commands if any
350 if (commandString.length() > 0) {
351 while (commandString[0] == ' ') {
352 commandString = commandString.substr(1,commandString.length());
353 if (commandString.length() == 0) { break; }
359 catch(exception& e) {
360 mout->errorOut(e, "ScriptEngine", "getNextCommand");
364 /***********************************************************************/