Giter VIP home page Giter VIP logo

noshell's Introduction

CI

NoShell library

Overview

The NoShell C++11 library allows to easily starts sub-process with a syntax similar to the shell while not using the shell. It like using the "system()", "popen()", or "spawn()" calls, but easier to use and features similar to the shell.

For example, the following code will run the "grep" command and setup the stream is to read from its standard out.

noshell::istream is;
noshell::Exit e = "grep"_C("-c", "/proc/cpuinfo") | is;

int ncpu;
is >> ncpu;

Now ncpu contains the number of CPUs on your Linux machine. NoShell allows to create pipelines of commands as well, and do redirections (with '<', '>', '>>'), like in the shell. For example, the following shell code:

grep < /proc/cpuinfo | wc -l > ncpus

would be done with NoShell like this:

noshell::Exit e = "grep"_C() < "/proc/cpuinfo" | "wc"_C("-l") > "ncpus";

Note that in the last example, the shell /bin/sh was not used. The redirection of the standard output of grep to the standard input of wc and the standard output of wc to the file "ncpus" is done by the NoShell library itself.

For more documentation and examples, see the doc directory.

Why such a library?

Using a shell to interpret the command passed to "system()", "popen()", or equivalent, is full of problems. The shell has a very complex syntax and the parsing of a command from a string brings security issues and problems with escaping (e.g. spaces in paths, special characters, etc.).

Starting sub-process with fork()/exec() or spawn(), and making the redirections with pipe()/dup() is not so easy and takes a lot of boilerplate code. It is easy to get it wrong and leak resources (file descriptors, memory). NoShell makes all of this simple and convenient, and does not leak resources.

NoShell brings some of the functionality of a shell (running multiple process in a pipeline with input/output redirections) without its problems: the strings are not interpreted and passed directly as arguments to the exec call.

Installation

For installation, use autoconf/automake:

autoreconf -i
./configure
make
sudo make install

At this point, this library has only been tested on Linux, with g++ version 4.7 or newer and clang++ version 3.2.

CMake

Alternatively, you can use CMake to build the library:

mkdir build
cmake -S . -B build
cmake --build build
sudo cmake --install build

Run the tests with:

cd build && ctest

Using the library

Add the following to your make file:

CXXFLAGS = $(shell pkg-config --cflags noshell)
LDFLAGS = $(shell pkg-config --libs noshell)

CMake

Add the following to your CMakeLists.txt:

find_package(noshell REQUIRED)

# link shared library
target_link_libraries(mytarget noshell::noshell)

# link static library
target_link_libraries(mytarget noshell::noshell-static)

noshell's People

Contributors

gmarcais avatar guoh27 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.