Changeset 11326 for trunk/FACT++


Ignore:
Timestamp:
07/10/11 17:55:23 (13 years ago)
Author:
tbretz
Message:
Added .! command to be able to execute shell commands.
Location:
trunk/FACT++/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/Readline.cc

    r11131 r11326  
    861861// --------------------------------------------------------------------------
    862862//
     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//
     871bool 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//
    863904//! Print the available commands. This is intended for being overwritten
    864905//! by deriving classes.
     
    903944    fprintf(rl_outstream, "   c,commands   Dump available commands\n");
    904945    fprintf(rl_outstream, "   k,keylist    Dump key bindings\n");
     946    fprintf(rl_outstream, "   .! command   Execute a shell command\n");
    905947    fprintf(rl_outstream, "   .w n         Sleep n milliseconds\n");
    906948    fprintf(rl_outstream, "   .x filename  Execute a script of commands\n");
     
    9611003    }
    9621004
     1005    if (str.substr(0, 2)==".!")
     1006    {
     1007         ExecuteShellCommand(str.substr(2));
     1008         return true;
     1009    }
     1010
    9631011    // ----------- Readline static -------------
    9641012
  • trunk/FACT++/src/Readline.h

    r11247 r11326  
    9090    virtual void Run(const char *prompt=0);
    9191    static  void Stop();
     92    virtual bool ExecuteShellCommand(const std::string &cmd);
    9293    int          Execute(const std::string &fname);
    9394    bool         IsStopped() const;
  • trunk/FACT++/src/ReadlineColor.cc

    r11044 r11326  
    153153    out << kBold << "   k,keylist    " << kReset << "Dump key bindings" << endl;
    154154    out << kBold << "   a,attrs      " << kReset << "Dump available stream attributes" << endl;
     155    out << kBold << "   .! command   " << kReset << "Execute a shell command";
    155156    out << kBold << "   .w n         " << kReset << "Sleep n milliseconds";
    156157    out << kBold << "   .x filename  " << kReset << "Execute a script of commands" << endl;
     
    161162}
    162163
     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//
     174bool 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
    163204
    164205bool ReadlineColor::Process(ostream &out, const string &str)
    165206{
    166207    // ----------- Readline -----------
     208
     209    if (str.substr(0, 2)==".!")
     210         return ExecuteShellCommand(out, str.substr(2));
    167211
    168212    if (str=="lh" || str=="history")
  • trunk/FACT++/src/ReadlineColor.h

    r10998 r11326  
    66namespace ReadlineColor
    77{
     8    bool ExecuteShellCommand(std::ostream &out, const std::string &cmd);
     9
    810    bool PrintBootMsg(std::ostream &out, const std::string &name, bool interactive=true);
    911    bool PrintAttributes(std::ostream &out);
Note: See TracChangeset for help on using the changeset viewer.