5 * Created by Sarah Westcott on 7/8/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "systemcommand.h"
12 //**********************************************************************************************************************
13 vector<string> SystemCommand::setParameters(){
15 CommandParameter pcommand("command", "String", "", "", "", "", "","",false,false); parameters.push_back(pcommand);
17 vector<string> myArray;
18 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
22 m->errorOut(e, "SystemCommand", "setParameters");
26 //**********************************************************************************************************************
27 SystemCommand::SystemCommand(string option) {
29 abort = false; calledHelp = false;
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;}
36 vector<string> myArray = setParameters();
38 OptionParser parser(option);
39 map<string, string> parameters = parser.getParameters();
40 map<string, string>::iterator it;
42 ValidParameters validParameter;
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; }
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(',');
55 //if there is a comma then grab string up to that pos
56 if (commaPos != option.npos) {
57 option = option.substr(0, commaPos);
66 m->errorOut(e, "SystemCommand", "SystemCommand");
70 //**********************************************************************************************************************
72 string SystemCommand::getHelpString(){
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";
82 m->errorOut(e, "SystemCommand", "help");
87 //**********************************************************************************************************************
89 int SystemCommand::execute(){
92 if (abort == true) { if (calledHelp) { return 0; } return 2; }
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";
101 system(command.c_str());
105 string filename = "./commandScreen.output";
106 m->openInputFile(filename, in, "no error");
109 while(char c = in.get()){
110 if(in.eof()) { break; }
111 else { output += c; }
115 m->mothurOut(output); m->mothurOutEndLine();
116 m->mothurRemove(filename);
122 catch(exception& e) {
123 m->errorOut(e, "SystemCommand", "execute");
128 //**********************************************************************************************************************