1 //
2 //  interactengine.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 10/21/19.
6 //  Copyright © 2019 Schloss Lab. All rights reserved.
7 //
8 
9 #include "interactengine.hpp"
10 #include "batchengine.hpp"
11 
12 /***********************************************************************/
13 
InteractEngine(string tpath,map<string,string> ev)14 InteractEngine::InteractEngine(string tpath, map<string, string> ev) : Engine(tpath) {
15 
16     if (m->getLogFileName() == "") {
17         time_t ltime = time(NULL); /* calendar time */
18         string outputPath = current->getOutputDir();
19         //if (outputPath == "") { outputPath = current->getDefaultPath();  }
20         string logFileName = outputPath + "mothur." + toString(ltime) + ".logfile";
21         m->setLogFileName(logFileName, false);
22         m->mothurOut("\n");
23     }
24     setEnvironmentVariables(ev);
25 }
26 
27 /***********************************************************************/
28 
~InteractEngine()29 InteractEngine::~InteractEngine(){}
30 
31 /***********************************************************************/
32 //This function allows the user to input commands one line at a time until they quit.
33 //If the command is garbage it does nothing.
getInput()34 bool InteractEngine::getInput(){
35     try {
36         string input = "";
37         string commandName = "";
38         string options = "";
39         int quitCommandCalled = 0;
40 
41         while(quitCommandCalled != 1){
42 
43             input = getCommand();
44 
45             if (m->getControl_pressed()) { input = "quit()"; }
46 
47             //allow user to omit the () on the quit command
48             if (input == "quit") { input = "quit()"; }
49             if (input == "help") { input = "help()"; }
50 
51             CommandOptionParser parser(input);
52             commandName = parser.getCommandString();
53 
54             options = parser.getOptionString();
55 
56             if (commandName != "") {
57                 numCommandsRun++;
58                 m->setExecuting(true);
59                 m->resetCommandErrors();
60 
61                 //executes valid command
62                 m->setChangedSeqNames(true);
63 
64                 Command* command = cFactory->getCommand(commandName, options);
65                 quitCommandCalled = command->execute();
66                 delete command;
67 
68                 //if we aborted command
69                 if (quitCommandCalled == 2) {  m->mothurOut("[ERROR]: did not complete " + commandName + ".\n");  }
70 
71                 m->setControl_pressed(false);
72                 m->setExecuting(false);
73 
74             }else { m->mothurOut("[ERROR]: Invalid.\n"); }
75         }
76         return true;
77     }
78     catch(exception& e) {
79         m->errorOut(e, "InteractEngine", "getInput");
80         exit(1);
81     }
82 }
83 /***********************************************************************/
getCommand()84 string InteractEngine::getCommand()  {
85     try {
86         string returnCommand = "";
87         #if defined NON_WINDOWS
88             #ifdef USE_READLINE
89                 char* nextCommand = NULL;
90                 nextCommand = readline("\nmothur > ");
91 
92                 if(nextCommand != NULL) {  add_history(nextCommand);  }
93                 else{ //^D causes null string and we want it to quit mothur
94                     nextCommand = strdup("quit()");
95                 }
96 
97                 m->mothurOutJustToLog("\nmothur > " + toString(nextCommand) + "\n");
98                 returnCommand = nextCommand;
99                 free(nextCommand);
100 
101             #else
102                 m->mothurOut("\nmothur > ");
103                 getline(cin, returnCommand);
104                 m->mothurOut("\n");
105                 m->mothurOutJustToLog("\nmothur > " + toString(returnCommand) + "\n");
106             #endif
107         #else
108                 m->mothurOut("\nmothur > ");
109                 getline(cin, returnCommand);
110                 m->mothurOut("\n");
111                 m->mothurOutJustToLog(toString(returnCommand) + "\n");
112         #endif
113 
114         //allow user to omit the () on the help and quit commands
115         if (returnCommand == "quit") { returnCommand = "quit()"; }
116         if (returnCommand == "help") { returnCommand = "help()"; }
117         if (returnCommand == "")     { return returnCommand; }
118 
119         string type = findType(returnCommand);
120 
121         if (type == "environment") {
122             //set environmental variables
123             string key, value; value = returnCommand;
124             util.splitAtEquals(key, value);
125 
126             map<string, string>::iterator it = environmentalVariables.find(key);
127             if (it == environmentalVariables.end())     { environmentalVariables[key] = value;  }
128             else                                        { it->second = value;                   }
129 
130             m->mothurOut("Setting environment variable " + key + " to " + value + "\n");
131 
132             returnCommand = getCommand();
133 
134         }else if (type == "batch") {
135             m->mothurOutClearBuffer();
136             m->mothurOut("/*****************************************************************************/\n");
137 
138             BatchEngine newBatchEngine(path, returnCommand, environmentalVariables);
139 
140             if (newBatchEngine.getOpenedBatch()) {
141                 bool bail = false;
142                 while(!bail)    {    bail = newBatchEngine.getInput();    }
143             }
144             m->mothurOutClearBuffer();
145             m->mothurOut("/*****************************************************************************/\n");
146 
147             returnCommand = getCommand();
148         }else { //assume command, look for environmental variables to replace
149 
150             int evPos = returnCommand.find_first_of('$');
151             if (evPos == string::npos) { }//no '$' , nothing to do
152             else { replaceVariables(returnCommand); }
153         }
154 
155         if (m->getDebug()) {
156             double ramUsed, total;
157             ramUsed = util.getRAMUsed(); total = util.getTotalRAM();
158             m->mothurOut("RAM used: " + toString(ramUsed/(double)GIG) + " Gigabytes. Total Ram: " + toString(total/(double)GIG) + " Gigabytes.\n\n");
159         }
160 
161         return returnCommand;
162     }
163     catch(exception& e) {
164         m->errorOut(e, "InteractEngine", "getCommand");
165         exit(1);
166     }
167 }
168 /***********************************************************************/
169