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 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
104 char* nextCommand = NULL;
105 nextCommand = readline("mothur > ");
107 if(nextCommand != NULL) { add_history(nextCommand); }
108 else{ //^D causes null string and we want it to quit mothur
109 nextCommand = "quit";
110 mout->mothurOut(nextCommand);
113 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
116 string nextCommand = "";
117 mout->mothurOut("mothur > ");
118 getline(cin, nextCommand);
119 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
124 string nextCommand = "";
126 mout->mothurOut("mothur > ");
127 getline(cin, nextCommand);
128 mout->mothurOutJustToLog(toString(nextCommand));
133 mout->mothurOutEndLine();
136 catch(exception& e) {
137 mout->errorOut(e, "Engine", "getCommand");
141 /***********************************************************************/
142 //This function opens the batchfile to be used by BatchEngine::getInput.
143 BatchEngine::BatchEngine(string path, string batchFileName){
145 globaldata = GlobalData::getInstance();
147 openedBatch = openInputFile(batchFileName, inputBatchFile);
148 globaldata->argv = path;
151 catch(exception& e) {
152 mout->errorOut(e, "BatchEngine", "BatchEngine");
157 /***********************************************************************/
159 BatchEngine::~BatchEngine(){ }
161 /***********************************************************************/
162 //This Function allows the user to run a batchfile containing several commands on Dotur
163 bool BatchEngine::getInput(){
165 //check if this is a valid batchfile
166 if (openedBatch == 1) {
167 mout->mothurOut("unable to open batchfile");
168 mout->mothurOutEndLine();
173 string commandName = "";
176 //CommandFactory cFactory;
177 int quitCommandCalled = 0;
179 while(quitCommandCalled == 0){
181 if (inputBatchFile.eof()) { input = "quit()"; }
182 else { input = getline(inputBatchFile); }
184 if (input[0] != '#') {
186 mout->mothurOutEndLine();
187 mout->mothurOut("mothur > " + input);
188 mout->mothurOutEndLine();
190 if (mout->control_pressed) { input = "quit()"; }
192 //allow user to omit the () on the quit command
193 if (input == "quit") { input = "quit()"; }
195 CommandOptionParser parser(input);
196 commandName = parser.getCommandString();
197 options = parser.getOptionString();
199 if (commandName != "") {
200 mout->executing = true;
203 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
205 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
207 //executes valid command
208 Command* command = cFactory->getCommand(commandName, options);
209 quitCommandCalled = command->execute();
210 mout->control_pressed = 0;
211 mout->executing = false;
217 mout->mothurOut("Invalid.");
218 mout->mothurOutEndLine();
222 gobble(inputBatchFile);
225 inputBatchFile.close();
228 catch(exception& e) {
229 mout->errorOut(e, "BatchEngine", "getInput");
235 /***********************************************************************/
236 /***********************************************************************/
237 //This function opens the batchfile to be used by BatchEngine::getInput.
238 ScriptEngine::ScriptEngine(string path, string commandString){
240 globaldata = GlobalData::getInstance();
243 listOfCommands = commandString.substr(1, (commandString.length()-1));
245 globaldata->argv = path;
248 catch(exception& e) {
249 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
254 /***********************************************************************/
256 ScriptEngine::~ScriptEngine(){ }
258 /***********************************************************************/
259 //This Function allows the user to run a batchfile containing several commands on mothur
260 bool ScriptEngine::getInput(){
264 string commandName = "";
268 //CommandFactory cFactory;
269 int quitCommandCalled = 0;
271 while(quitCommandCalled == 0){
273 input = getNextCommand(listOfCommands);
275 if (input == "") { input = "quit()"; }
277 mout->mothurOutEndLine();
278 mout->mothurOut("mothur > " + input);
279 mout->mothurOutEndLine();
281 if (mout->control_pressed) { input = "quit()"; }
283 //allow user to omit the () on the quit command
284 if (input == "quit") { input = "quit()"; }
286 CommandOptionParser parser(input);
287 commandName = parser.getCommandString();
288 options = parser.getOptionString();
290 if (commandName != "") {
291 mout->executing = true;
294 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
296 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
298 //executes valid command
299 Command* command = cFactory->getCommand(commandName, options);
300 quitCommandCalled = command->execute();
301 mout->control_pressed = 0;
302 mout->executing = false;
308 mout->mothurOut("Invalid.");
309 mout->mothurOutEndLine();
316 catch(exception& e) {
317 mout->errorOut(e, "ScriptEngine", "getInput");
321 /***********************************************************************/
322 string ScriptEngine::getNextCommand(string& commandString) {
324 string nextcommand = "";
327 //go through string until you reach ; or end
328 while (count < commandString.length()) {
330 if (commandString[count] == ';') { break; }
331 else { nextcommand += commandString[count]; }
336 //if you are not at the end
337 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
338 else { commandString = ""; }
341 //get rid of spaces in between commands if any
342 if (commandString.length() > 0) {
343 while (commandString[0] == ' ') {
344 commandString = commandString.substr(1,commandString.length());
345 if (commandString.length() == 0) { break; }
351 catch(exception& e) {
352 mout->errorOut(e, "ScriptEngine", "getNextCommand");
356 /***********************************************************************/