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));
135 catch(exception& e) {
136 mout->errorOut(e, "Engine", "getCommand");
140 /***********************************************************************/
141 //This function opens the batchfile to be used by BatchEngine::getInput.
142 BatchEngine::BatchEngine(string path, string batchFileName){
144 globaldata = GlobalData::getInstance();
146 openedBatch = openInputFile(batchFileName, inputBatchFile);
147 globaldata->argv = path;
150 catch(exception& e) {
151 mout->errorOut(e, "BatchEngine", "BatchEngine");
156 /***********************************************************************/
158 BatchEngine::~BatchEngine(){ }
160 /***********************************************************************/
161 //This Function allows the user to run a batchfile containing several commands on Dotur
162 bool BatchEngine::getInput(){
164 //check if this is a valid batchfile
165 if (openedBatch == 1) {
166 mout->mothurOut("unable to open batchfile");
167 mout->mothurOutEndLine();
172 string commandName = "";
175 //CommandFactory cFactory;
176 int quitCommandCalled = 0;
178 while(quitCommandCalled == 0){
180 if (inputBatchFile.eof()) { input = "quit()"; }
181 else { input = getline(inputBatchFile); }
183 if (input[0] != '#') {
185 mout->mothurOutEndLine();
186 mout->mothurOut("mothur > " + input);
187 mout->mothurOutEndLine();
189 if (mout->control_pressed) { input = "quit()"; }
191 //allow user to omit the () on the quit command
192 if (input == "quit") { input = "quit()"; }
194 CommandOptionParser parser(input);
195 commandName = parser.getCommandString();
196 options = parser.getOptionString();
198 if (commandName != "") {
199 mout->executing = true;
202 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
204 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
206 //executes valid command
207 Command* command = cFactory->getCommand(commandName, options);
208 quitCommandCalled = command->execute();
209 mout->control_pressed = 0;
210 mout->executing = false;
216 mout->mothurOut("Invalid.");
217 mout->mothurOutEndLine();
221 gobble(inputBatchFile);
224 inputBatchFile.close();
227 catch(exception& e) {
228 mout->errorOut(e, "BatchEngine", "getInput");
234 /***********************************************************************/
235 /***********************************************************************/
236 //This function opens the batchfile to be used by BatchEngine::getInput.
237 ScriptEngine::ScriptEngine(string path, string commandString){
239 globaldata = GlobalData::getInstance();
242 listOfCommands = commandString.substr(1, (commandString.length()-1));
244 globaldata->argv = path;
247 catch(exception& e) {
248 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
253 /***********************************************************************/
255 ScriptEngine::~ScriptEngine(){ }
257 /***********************************************************************/
258 //This Function allows the user to run a batchfile containing several commands on mothur
259 bool ScriptEngine::getInput(){
263 string commandName = "";
267 //CommandFactory cFactory;
268 int quitCommandCalled = 0;
270 while(quitCommandCalled == 0){
272 input = getNextCommand(listOfCommands);
274 if (input == "") { input = "quit()"; }
276 mout->mothurOutEndLine();
277 mout->mothurOut("mothur > " + input);
278 mout->mothurOutEndLine();
280 if (mout->control_pressed) { input = "quit()"; }
282 //allow user to omit the () on the quit command
283 if (input == "quit") { input = "quit()"; }
285 CommandOptionParser parser(input);
286 commandName = parser.getCommandString();
287 options = parser.getOptionString();
289 if (commandName != "") {
290 mout->executing = true;
293 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
295 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
297 //executes valid command
298 Command* command = cFactory->getCommand(commandName, options);
299 quitCommandCalled = command->execute();
300 mout->control_pressed = 0;
301 mout->executing = false;
307 mout->mothurOut("Invalid.");
308 mout->mothurOutEndLine();
315 catch(exception& e) {
316 mout->errorOut(e, "ScriptEngine", "getInput");
320 /***********************************************************************/
321 string ScriptEngine::getNextCommand(string& commandString) {
323 string nextcommand = "";
326 //go through string until you reach ; or end
327 while (count < commandString.length()) {
329 if (commandString[count] == ';') { break; }
330 else { nextcommand += commandString[count]; }
335 //if you are not at the end
336 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
337 else { commandString = ""; }
340 //get rid of spaces in between commands if any
341 if (commandString.length() > 0) {
342 while (commandString[0] == ' ') {
343 commandString = commandString.substr(1,commandString.length());
344 if (commandString.length() == 0) { break; }
350 catch(exception& e) {
351 mout->errorOut(e, "ScriptEngine", "getNextCommand");
355 /***********************************************************************/