]> git.donarmstrong.com Git - mothur.git/blob - systemcommand.cpp
sffinfo bug with flow grams right index when clipQualRight=0
[mothur.git] / systemcommand.cpp
1 /*
2  *  systemcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 7/8/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "systemcommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> SystemCommand::setParameters(){  
14         try {
15                 CommandParameter pcommand("command", "String", "", "", "", "", "","",false,false); parameters.push_back(pcommand);
16                                 
17                 vector<string> myArray;
18                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
19                 return myArray;
20         }
21         catch(exception& e) {
22                 m->errorOut(e, "SystemCommand", "setParameters");
23                 exit(1);
24         }
25 }
26 //**********************************************************************************************************************
27 SystemCommand::SystemCommand(string option)  {
28         try {
29                 abort = false; calledHelp = false;   
30                 
31                 //allow user to run help
32                 if(option == "help") { help(); abort = true; calledHelp = true; }
33                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
34                 
35                 else {
36                         vector<string> myArray = setParameters();
37                         
38                         OptionParser parser(option);
39                         map<string, string> parameters = parser.getParameters();
40                         map<string, string>::iterator it;
41                         
42                         ValidParameters validParameter;
43                         
44                         //check for optional parameter and set defaults
45                         // ...at some point should added some additional type checking...
46                         string commandOption = validParameter.validFile(parameters, "command", false);                  
47                         if (commandOption == "not found") { commandOption = ""; }
48                         else { command = commandOption; }
49                         
50                         if ((option == "") && (commandOption == "")) { m->mothurOut("You must enter a command to run."); m->mothurOutEndLine(); abort = true; }
51                         else if (commandOption == "") { 
52                                 //check for outputdir and inputdir parameters
53                                 int commaPos = option.find_first_of(',');
54                                 
55                                 //if there is a comma then grab string up to that pos
56                                 if (commaPos != option.npos) {
57                                         option = option.substr(0, commaPos);
58                                 }
59                         
60                                 command = option;
61                         }
62                 }       
63
64         }
65         catch(exception& e) {
66                 m->errorOut(e, "SystemCommand", "SystemCommand");
67                 exit(1);
68         }
69 }
70 //**********************************************************************************************************************
71
72 string SystemCommand::getHelpString(){
73         try {
74                 string helpString = "";
75                 helpString += "The system command allows you to execute a system command from within mothur.\n";
76                 helpString += "The system has no parameters.\n";
77                 helpString += "The system command should be in the following format: system(yourCommand).\n";
78                 helpString += "Example system(clear).\n";
79                 return helpString;
80         }
81         catch(exception& e) {
82                 m->errorOut(e, "SystemCommand", "help");
83                 exit(1);
84         }
85 }
86
87 //**********************************************************************************************************************
88
89 int SystemCommand::execute(){
90         try {
91                 
92                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
93                 
94                 //if command contains a redirect don't add the redirect
95                 bool usedRedirect = false;
96                 if ((command.find('>')) == string::npos) {
97                         command += " > ./commandScreen.output 2>&1";
98                         usedRedirect = true;
99                 }
100                 
101                 system(command.c_str());
102                 
103                 if (usedRedirect) {
104                         ifstream in;
105                         string filename = "./commandScreen.output";
106                         m->openInputFile(filename, in, "no error");
107                         
108                         string output = "";
109                         while(char c = in.get()){
110                                 if(in.eof())            {       break;                  }
111                                 else                            {       output += c;    }
112                         }
113                         in.close();
114                         
115                         m->mothurOut(output); m->mothurOutEndLine();
116                         m->mothurRemove(filename);
117                 }
118                 
119                 return 0;               
120         }
121
122         catch(exception& e) {
123                 m->errorOut(e, "SystemCommand", "execute");
124                 exit(1);
125         }
126 }
127
128 //**********************************************************************************************************************