Introducing cppsh: A bash-Like Shell with C++ Syntax

cppsh

It’s been a long time in the mak­ing, but I am proud to announce the first beta release of cppsh, the bash-like shell specif­i­cal­ly designed for those engi­neers who find them­selves most com­fort­able at the reins of a C++ com­pil­er. The best fea­tures from both bash and the C++ lan­guage come togeth­er in cppsh to make you a more pro­duc­tive shell user. Some of the most impor­tant fea­tures of cppsh include:

File Iterators

File iter­a­tors allow you to tra­verse the files in your work­ing direc­to­ry using the con­ve­nient C++ STL iter­a­tor syntax:

cppsh>
for ( cppsh::file::const_iterator file_iterator =
        cppsh::list_cwd().begin();
      file_iterator != cppsh::list_cwd().end();
      ++file_iterator )
{
  typedef cppsh::command_line_entry<std::string> entry;
  std::vector<entry> command_line;
  command_line.push_back( entry( "mv" ) );
  command_line.push_back( entry( "-f" ) );
  command_line.push_back( entry( *file_iterator ) );
  command_line.push_back( entry( *file_iterator + ".bak" ) );
  cppsh::execute_command( command_line, std::cout );
}

Command Pipelines

Following the UNIX tra­di­tion, cppsh makes it easy to feed the out­put of one com­mand into anoth­er command:

cppsh>
typedef cppsh::command_line_entry<std::string> entry;
typedef std::vector<entry> command;

command cat_command;
cat_command.push_back( entry( "cat" ) );
cat_command.push_back( entry( "datafile" ) );

command sort_command;
sort_command.push_back( entry( "sort" ) );
sort_command.push_back( entry( "--unique" ) );

command wc_command;
wc_command.push_back( entry( "wc" ) );
wc_command.push_back( entry( "--lines" ) );

cppsh::command_pipeline<command> pipeline;
pipeline.push_back( cat_command );
pipeline.push_back( sort_command );
pipeline.push_back( wc_command );

pipeline.execute( std::cout );

Configurability

Like many UNIX pro­grams, cppsh can be con­fig­ured by edit­ing the .cpp­shrc file in your home direc­to­ry. Unlike most UNIX pro­grams, how­ev­er, the .cpp­shrc file is a full-fledged C++ head­er file. The .cpp­shrc file is respon­si­ble for defin­ing the cppsh_shell type. This is done by cre­at­ing a user-spe­cif­ic traits class and pass­ing it as a para­me­ter basic_cppsh_shell template:

#ifndef DOT_CPPSHRC
#define DOT_CPPSHRC

#include <cppsh/basic_cppsh_shell.hpp>

namespace cppsh {

struct user_cppsh_traits
{
    typedef vi_editing_mode editing_mode_t;

    static const int command_history = 1000;

    static std::string prompt()
    {
        return "cppsh>";
    }
};

typedef basic_cppsh_shell<user_cppsh_traits> cppsh_shell;

} // namespace cppsh

#endif // DOT_CPPSHRC

Extensibility

If you’re a demand­ing user, you might find that the .cpp­shrc file does not offer the pow­er you need to cus­tomize cppsh to fit your needs. You’re still in luck! All of the fea­tures described above (and more) are packed into only 412,011 lines of C++ code, so you can eas­i­ly hack cppsh to fit your own needs. Internally, cppsh makes exten­sive use of tem­plate metapro­gram­ming, so the code is terse and easy to understand.

What are you waiting for?

Get start­ed with cppsh today — vis­it the project page for down­loads and doc­u­men­ta­tion. You’ll be hap­py you did.

The cppsh team

Comments are disabled for this post