Changeset 11326 for trunk/FACT++
- Timestamp:
- 07/10/11 17:55:23 (13 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/Readline.cc
r11131 r11326 861 861 // -------------------------------------------------------------------------- 862 862 // 863 //! Execute a shell command through a pipe. Write stdout to rl_outstream 864 //! 865 //! @param cmd 866 //! Command to be executed 867 //! 868 //! @returns 869 //! always true 870 // 871 bool Readline::ExecuteShellCommand(const string &cmd) 872 { 873 FILE *pipe = popen(cmd.c_str(), "r"); 874 if (!pipe) 875 { 876 fprintf(rl_outstream, "ERROR - Could not create pipe '%s': %m\n", cmd.c_str()); 877 return true; 878 } 879 880 while (1) 881 { 882 char buf[1024]; 883 884 const size_t sz = fread(buf, 1, 1024, pipe); 885 886 fwrite(buf, 1, sz, rl_outstream); 887 888 if (feof(pipe) || ferror(pipe)) 889 break; 890 } 891 892 if (ferror(pipe)) 893 fprintf(rl_outstream, "ERROR - Reading from pipe '%s': %m\n", cmd.c_str()); 894 895 pclose(pipe); 896 897 fprintf(rl_outstream, "\n"); 898 899 return true; 900 } 901 902 // -------------------------------------------------------------------------- 903 // 863 904 //! Print the available commands. This is intended for being overwritten 864 905 //! by deriving classes. … … 903 944 fprintf(rl_outstream, " c,commands Dump available commands\n"); 904 945 fprintf(rl_outstream, " k,keylist Dump key bindings\n"); 946 fprintf(rl_outstream, " .! command Execute a shell command\n"); 905 947 fprintf(rl_outstream, " .w n Sleep n milliseconds\n"); 906 948 fprintf(rl_outstream, " .x filename Execute a script of commands\n"); … … 961 1003 } 962 1004 1005 if (str.substr(0, 2)==".!") 1006 { 1007 ExecuteShellCommand(str.substr(2)); 1008 return true; 1009 } 1010 963 1011 // ----------- Readline static ------------- 964 1012 -
trunk/FACT++/src/Readline.h
r11247 r11326 90 90 virtual void Run(const char *prompt=0); 91 91 static void Stop(); 92 virtual bool ExecuteShellCommand(const std::string &cmd); 92 93 int Execute(const std::string &fname); 93 94 bool IsStopped() const; -
trunk/FACT++/src/ReadlineColor.cc
r11044 r11326 153 153 out << kBold << " k,keylist " << kReset << "Dump key bindings" << endl; 154 154 out << kBold << " a,attrs " << kReset << "Dump available stream attributes" << endl; 155 out << kBold << " .! command " << kReset << "Execute a shell command"; 155 156 out << kBold << " .w n " << kReset << "Sleep n milliseconds"; 156 157 out << kBold << " .x filename " << kReset << "Execute a script of commands" << endl; … … 161 162 } 162 163 164 // -------------------------------------------------------------------------- 165 // 166 //! Execute a shell command through a pipe. Write stdout to rl_outstream 167 //! 168 //! @param cmd 169 //! Command to be executed 170 //! 171 //! @returns 172 //! always true 173 // 174 bool ReadlineColor::ExecuteShellCommand(ostream &out, const string &cmd) 175 { 176 FILE *pipe = popen(cmd.c_str(), "r"); 177 if (!pipe) 178 { 179 out << kRed << "ERROR - Could not create pipe '" << cmd << "': " << strerror(errno) << " [" << errno << "]" << endl; 180 return true; 181 } 182 183 while (1) 184 { 185 char buf[1024]; 186 187 const size_t sz = fread(buf, 1, 1024, pipe); 188 out.write(buf, sz); 189 190 if (feof(pipe) || ferror(pipe)) 191 break; 192 } 193 194 out << endl; 195 196 if (ferror(pipe)) 197 out << kRed << "ERROR - Reading from pipe '" << cmd << "': " << strerror(errno) << " [" << errno << "]" << endl; 198 199 pclose(pipe); 200 201 return true; 202 } 203 163 204 164 205 bool ReadlineColor::Process(ostream &out, const string &str) 165 206 { 166 207 // ----------- Readline ----------- 208 209 if (str.substr(0, 2)==".!") 210 return ExecuteShellCommand(out, str.substr(2)); 167 211 168 212 if (str=="lh" || str=="history") -
trunk/FACT++/src/ReadlineColor.h
r10998 r11326 6 6 namespace ReadlineColor 7 7 { 8 bool ExecuteShellCommand(std::ostream &out, const std::string &cmd); 9 8 10 bool PrintBootMsg(std::ostream &out, const std::string &name, bool interactive=true); 9 11 bool PrintAttributes(std::ostream &out);
Note:
See TracChangeset
for help on using the changeset viewer.