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 /***********************************************************************/
18 inline void terminateCommand(int dummy) {
20 //mothurOut("Stopping command....");
21 //CommandFactory* cFactory = CommandFactory::getInstance();
22 //cFactory->getCommand(); //deletes old command and makes new no command.
23 //this may cause memory leak if old commands execute function allocated memory
24 //that is freed in the execute function and not the deconstructor
25 //mothurOut("DONE."); mothurOutEndLine();
27 /***********************************************************************/
30 cFactory = CommandFactory::getInstance();
33 errorOut(e, "Engine", "Engine");
38 /***********************************************************************/
40 InteractEngine::InteractEngine(string path){
42 globaldata = GlobalData::getInstance();
43 globaldata->argv = path;
46 /***********************************************************************/
48 InteractEngine::~InteractEngine(){}
50 /***********************************************************************/
51 //This function allows the user to input commands one line at a time until they quit.
52 //If the command is garbage it does nothing.
53 bool InteractEngine::getInput(){
56 string commandName = "";
58 int quitCommandCalled = 0;
60 while(quitCommandCalled != 1){
66 //allow user to omit the () on the quit command
67 if (input == "quit") { input = "quit()"; }
69 CommandOptionParser parser(input);
70 commandName = parser.getCommandString();
72 options = parser.getOptionString();
74 if (commandName != "") {
76 //executes valid command
77 Command* command = cFactory->getCommand(commandName, options);
78 quitCommandCalled = command->execute();
81 mothurOut("Your input contains errors. Please try again.");
88 errorOut(e, "InteractEngine", "getInput");
92 /***********************************************************************/
93 string Engine::getCommand() {
95 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
97 char* nextCommand = NULL;
98 nextCommand = readline("mothur > ");
99 if(nextCommand != NULL) { add_history(nextCommand); }
100 mothurOutJustToLog("mothur > " + toString(nextCommand));
103 string nextCommand = "";
104 mothurOut("mothur > ");
105 getline(cin, nextCommand);
109 string nextCommand = "";
110 mothurOut("mothur > ");
111 getline(cin, nextCommand);
118 catch(exception& e) {
119 errorOut(e, "Engine", "getCommand");
123 /***********************************************************************/
124 //This function opens the batchfile to be used by BatchEngine::getInput.
125 BatchEngine::BatchEngine(string path, string batchFileName){
127 globaldata = GlobalData::getInstance();
129 openedBatch = openInputFile(batchFileName, inputBatchFile);
130 globaldata->argv = path;
133 catch(exception& e) {
134 errorOut(e, "BatchEngine", "BatchEngine");
139 /***********************************************************************/
141 BatchEngine::~BatchEngine(){ }
143 /***********************************************************************/
144 //This Function allows the user to run a batchfile containing several commands on Dotur
145 bool BatchEngine::getInput(){
147 //check if this is a valid batchfile
148 if (openedBatch == 1) {
149 mothurOut("unable to open batchfile");
155 string commandName = "";
158 //CommandFactory cFactory;
159 int quitCommandCalled = 0;
161 while(quitCommandCalled == 0){
163 if (inputBatchFile.eof()) { input = "quit()"; }
164 else { input = getline(inputBatchFile); }
166 if (input[0] != '#') {
169 mothurOut("mothur > " + input);
173 //allow user to omit the () on the quit command
174 if (input == "quit") { input = "quit()"; }
176 CommandOptionParser parser(input);
177 commandName = parser.getCommandString();
178 options = parser.getOptionString();
180 if (commandName != "") {
182 //executes valid command
183 Command* command = cFactory->getCommand(commandName, options);
184 quitCommandCalled = command->execute();
186 mothurOut("Invalid.");
191 gobble(inputBatchFile);
194 inputBatchFile.close();
197 catch(exception& e) {
198 errorOut(e, "BatchEngine", "getInput");
204 /***********************************************************************/
205 /***********************************************************************/
206 //This function opens the batchfile to be used by BatchEngine::getInput.
207 ScriptEngine::ScriptEngine(string path, string commandString){
209 globaldata = GlobalData::getInstance();
212 listOfCommands = commandString.substr(1, (commandString.length()-1));
214 globaldata->argv = path;
217 catch(exception& e) {
218 errorOut(e, "ScriptEngine", "ScriptEngine");
223 /***********************************************************************/
225 ScriptEngine::~ScriptEngine(){ }
227 /***********************************************************************/
228 //This Function allows the user to run a batchfile containing several commands on mothur
229 bool ScriptEngine::getInput(){
233 string commandName = "";
237 //CommandFactory cFactory;
238 int quitCommandCalled = 0;
240 while(quitCommandCalled == 0){
242 input = getNextCommand(listOfCommands);
244 if (input == "") { input = "quit()"; }
248 mothurOut("mothur > " + input);
252 //allow user to omit the () on the quit command
253 if (input == "quit") { input = "quit()"; }
255 CommandOptionParser parser(input);
256 commandName = parser.getCommandString();
257 options = parser.getOptionString();
259 if (commandName != "") {
261 //executes valid command
262 Command* command = cFactory->getCommand(commandName, options);
263 quitCommandCalled = command->execute();
265 mothurOut("Invalid.");
273 catch(exception& e) {
274 errorOut(e, "ScriptEngine", "getInput");
278 /***********************************************************************/
279 string ScriptEngine::getNextCommand(string& commandString) {
281 string nextcommand = "";
284 //go through string until you reach ; or end
285 while (count < commandString.length()) {
287 if (commandString[count] == ';') { break; }
288 else { nextcommand += commandString[count]; }
293 //if you are not at the end
294 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
295 else { commandString = ""; }
298 //get rid of spaces in between commands if any
299 if (commandString.length() > 0) {
300 while (commandString[0] == ' ') {
301 commandString = commandString.substr(1,commandString.length());
302 if (commandString.length() == 0) { break; }
308 catch(exception& e) {
309 errorOut(e, "ScriptEngine", "getNextCommand");
313 /***********************************************************************/