]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
added checks for ^C to quit command instead of program
[mothur.git] / engine.cpp
1 /*
2  *  engine.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 8/15/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
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.
11  *
12  */
13
14
15 #include "engine.hpp"
16
17 /***********************************************************************/
18 Engine::Engine(){
19         try {
20                 cFactory = CommandFactory::getInstance();
21                 mout = MothurOut::getInstance();
22         }
23         catch(exception& e) {
24                 mout->errorOut(e, "Engine", "Engine");
25                 exit(1);
26         }
27 }
28
29 /***********************************************************************/
30
31 InteractEngine::InteractEngine(string path){
32
33         globaldata = GlobalData::getInstance();
34         globaldata->argv = path;
35         
36 }
37
38 /***********************************************************************/
39
40 InteractEngine::~InteractEngine(){}
41
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(){
46         try {
47                 string input = "";
48                 string commandName = "";
49                 string options = "";
50                 int quitCommandCalled = 0;
51                 
52                 while(quitCommandCalled != 1){
53
54                         mout->mothurOutEndLine();
55                         
56                         input = getCommand();   
57                         mout->mothurOutEndLine();       
58                         
59                         if (mout->control_pressed) { input = "quit()"; }
60                         
61                         //allow user to omit the () on the quit command
62                         if (input == "quit") { input = "quit()"; }
63                         
64                         CommandOptionParser parser(input);
65                         commandName = parser.getCommandString();
66         
67                         options = parser.getOptionString();
68                         
69                         if (commandName != "") {
70                                 mout->executing = true;
71                                 //executes valid command
72                                 Command* command = cFactory->getCommand(commandName, options);
73                                 quitCommandCalled = command->execute();
74                                 mout->control_pressed = 0;
75                                 mout->executing = false;
76                         }else {
77                                 mout->mothurOut("Your input contains errors. Please try again."); 
78                                 mout->mothurOutEndLine();
79                         }
80                 }       
81                 return 1;
82         }
83         catch(exception& e) {
84                 mout->errorOut(e, "InteractEngine", "getInput");
85                 exit(1);
86         }
87 }
88 /***********************************************************************/
89 string Engine::getCommand()  {
90         try {
91                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
92                         #ifdef USE_READLINE
93                                 char* nextCommand = NULL;
94                                 nextCommand = readline("mothur > ");
95                                 
96                                 if(nextCommand != NULL) {  add_history(nextCommand);  } 
97                                 else{ //^D causes null string and we want it to quit mothur
98                                         nextCommand = "quit"; 
99                                         cout << nextCommand << endl;
100                                 }       
101                                 
102                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
103                                 return nextCommand;
104                         #else
105                                 string nextCommand = "";
106                                 mout->mothurOut("mothur > ");
107                                 getline(cin, nextCommand);
108                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
109                                 return nextCommand;
110                         #endif
111                 #else
112                         string nextCommand = "";
113                         mout->mothurOut("mothur > ");
114                         getline(cin, nextCommand);
115                         mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
116                         return nextCommand;
117                 #endif
118                 
119                 mout->mothurOutEndLine();
120                                                 
121         }
122         catch(exception& e) {
123                 mout->errorOut(e, "Engine", "getCommand");
124                 exit(1);
125         }
126 }
127 /***********************************************************************/
128 //This function opens the batchfile to be used by BatchEngine::getInput.
129 BatchEngine::BatchEngine(string path, string batchFileName){
130         try {
131                 globaldata = GlobalData::getInstance();
132         
133                 openedBatch = openInputFile(batchFileName, inputBatchFile);
134                 globaldata->argv = path;
135                                 
136         }
137         catch(exception& e) {
138                 mout->errorOut(e, "BatchEngine", "BatchEngine");
139                 exit(1);
140         }
141 }
142
143 /***********************************************************************/
144
145 BatchEngine::~BatchEngine(){    }
146
147 /***********************************************************************/
148 //This Function allows the user to run a batchfile containing several commands on Dotur
149 bool BatchEngine::getInput(){
150         try {
151                 //check if this is a valid batchfile
152                 if (openedBatch == 1) {  
153                         mout->mothurOut("unable to open batchfile");  
154                         mout->mothurOutEndLine();
155                         return 1; 
156                 }
157         
158                 string input = "";
159                 string commandName = "";
160                 string options = "";
161                 
162                 //CommandFactory cFactory;
163                 int quitCommandCalled = 0;
164         
165                 while(quitCommandCalled == 0){
166         
167                         if (inputBatchFile.eof()) { input = "quit()"; }
168                         else { input = getline(inputBatchFile); }
169                         
170                         if (input[0] != '#') {
171                                 
172                                 mout->mothurOutEndLine();
173                                 mout->mothurOut("mothur > " + input);
174                                 mout->mothurOutEndLine();
175                                 
176                                 if (mout->control_pressed) { input = "quit()"; }
177                                 
178                                 //allow user to omit the () on the quit command
179                                 if (input == "quit") { input = "quit()"; }
180
181                                 CommandOptionParser parser(input);
182                                 commandName = parser.getCommandString();
183                                 options = parser.getOptionString();
184                                                                                 
185                                 if (commandName != "") {
186                                         mout->executing = true;
187                                         //executes valid command
188                                         Command* command = cFactory->getCommand(commandName, options);
189                                         quitCommandCalled = command->execute();
190                                         mout->control_pressed = 0;
191                                         mout->executing = false;
192                                 }else {         
193                                         mout->mothurOut("Invalid."); 
194                                         mout->mothurOutEndLine();
195                                 }
196                                 
197                         }
198                         gobble(inputBatchFile);
199                 }
200                 
201                 inputBatchFile.close();
202                 return 1;
203         }
204         catch(exception& e) {
205                 mout->errorOut(e, "BatchEngine", "getInput");
206                 exit(1);
207         }
208 }
209
210
211 /***********************************************************************/
212 /***********************************************************************/
213 //This function opens the batchfile to be used by BatchEngine::getInput.
214 ScriptEngine::ScriptEngine(string path, string commandString){
215         try {
216                 globaldata = GlobalData::getInstance();
217                 
218                 //remove quotes
219                 listOfCommands = commandString.substr(1, (commandString.length()-1));
220
221                 globaldata->argv = path;
222                                 
223         }
224         catch(exception& e) {
225                 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
226                 exit(1);
227         }
228 }
229
230 /***********************************************************************/
231
232 ScriptEngine::~ScriptEngine(){  }
233
234 /***********************************************************************/
235 //This Function allows the user to run a batchfile containing several commands on mothur
236 bool ScriptEngine::getInput(){
237         try {
238                         
239                 string input = "";
240                 string commandName = "";
241                 string options = "";
242                 
243                 
244                 //CommandFactory cFactory;
245                 int quitCommandCalled = 0;
246         
247                 while(quitCommandCalled == 0){
248                 
249                         input = getNextCommand(listOfCommands); 
250                         
251                         if (input == "") { input = "quit()"; }
252                         
253                         
254                         mout->mothurOutEndLine();
255                         mout->mothurOut("mothur > " + input);
256                         mout->mothurOutEndLine();
257
258                         if (mout->control_pressed) { input = "quit()"; }
259                                 
260                         //allow user to omit the () on the quit command
261                         if (input == "quit") { input = "quit()"; }
262
263                         CommandOptionParser parser(input);
264                         commandName = parser.getCommandString();
265                         options = parser.getOptionString();
266                                                                                 
267                         if (commandName != "") {
268                                 mout->executing = true;
269                                 //executes valid command
270                                 Command* command = cFactory->getCommand(commandName, options);
271                                 quitCommandCalled = command->execute();
272                                 mout->control_pressed = 0;
273                                 mout->executing = false;
274                         }else {         
275                                 mout->mothurOut("Invalid."); 
276                                 mout->mothurOutEndLine();
277                         }
278                         
279                 }
280                 
281                 return 1;
282         }
283         catch(exception& e) {
284                 mout->errorOut(e, "ScriptEngine", "getInput");
285                 exit(1);
286         }
287 }
288 /***********************************************************************/
289 string ScriptEngine::getNextCommand(string& commandString) {
290         try {
291                 string nextcommand = "";
292                 int count = 0;
293                 
294                 //go through string until you reach ; or end
295                 while (count < commandString.length()) { 
296                         
297                         if (commandString[count] == ';') {  break;   }
298                         else {          nextcommand += commandString[count];    }
299                         
300                         count++;
301                 }
302                 
303                 //if you are not at the end
304                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
305                 else { commandString = ""; }
306                                 
307                 
308                 //get rid of spaces in between commands if any
309                 if (commandString.length() > 0) {
310                         while (commandString[0] == ' ') {  
311                                 commandString = commandString.substr(1,commandString.length());
312                                 if (commandString.length() == 0) {  break;  }
313                         }
314                 }
315                                         
316                 return nextcommand;
317         }
318         catch(exception& e) {
319                 mout->errorOut(e, "ScriptEngine", "getNextCommand");
320                 exit(1);
321         }
322 }
323 /***********************************************************************/