[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

RUN COMMAND library for C++



Her på listen havde vi engang en thread om hvordan
man kalder et shell kommando og samler op på både
stdout og stderr samtidig uden at have nogle kæmpe
buffere. Resultatet af diskussionerne var en C version
der i sagens natur er lidt grim, fordi der skal udføres
en del oprydning (hvilket C ikke understøtter godt).

Jeg har nu lavet en C++ version, som kan hentes på
http://stadler.homepage.dk/computer_dk.html

Dokumentationen er stadig lidt rough. Men eller
ville jeg være glad for, hvis nogen kikker på det
og giver lidt feedbak.
Og hvis nogen synes, at librariet er nyttigt, så kan pakkerne
måske endda tilmældes hos debian.

Hans-Christian Stadler

PS 1: md5 summerne
26f6126969429a8d08623419406af664  libruncommand_0.1-1_i386.deb
990fcc9dd15a7c7ec6912ca7efacb8e9  libruncommand-dev_0.1-1_i386.deb
2a245ca78e56a69962f52b13a40865c2  libruncommand-0.1.tar.gz

PS 2: Eksempel på brug
#include <iostream>
#include <sstream>
#include <csignal>
#include <pthread.h>
#include <run_command.hxx>

using namespace std;

void sig_handler(int sig) {
        if (sig == SIGPIPE)
                cout << "Received SIGPIPE" << endl;
        else
                cout << "Received signal " << sig << endl;
}

void* second(void *arg) {
        cout << "Run command thread running " << (const char *)arg << " with stdin=cin" << endl;
        run::command((const char *)arg, &cin, &cout, &cerr);
        return (void *)0;
}

int main(int argc, char *argv[])
{
        if (argc < 2) {
                if (argc > 0)
                        cout << "Usage: " << argv[0] << " command [stdin]" << endl;
                return -1;
        }

        signal(SIGPIPE, &sig_handler);

        pthread_t tid;
        if (pthread_create(&tid, (pthread_attr_t *)0, &second, argv[1]) != 0)
                cerr << "Thread creation failed" << endl, exit(-1);

        int status;
        ostringstream out, err;
        istringstream in(argc==3 ? argv[2] : "");

        cout << argv[0] << ": Run command " << argv[1] << " with stdin=" << (argc==3 ? argv[2] : "null") << endl;

        try {
                status = run::command(argv[1], argc==3 ? &in : (istream *)0, &out, &err);
        } catch (run::exception &ex) {
                cerr << ex.file() << " (" << ex.line() << "): exception: msg=" << ex.msg() << ", errno=" << ex.err() << endl;
                return -2;
        }

        if (WIFEXITED(status))
                cout << "exited with status=" << WEXITSTATUS(status) << endl;
        else if (WIFSIGNALED(status))
                cout << "exit due to signal " << WTERMSIG(status) << endl;
        cout << "stdout=" << endl << out.str() << endl;
        cout << "stderr=" << endl << err.str() << endl;

        if (pthread_join(tid, (void **)0))
                cerr << "Thread join failed" << endl, exit(-1);

        return 0;
}

PS 3: Eksempel på output af eksempel (run_command er ovenstående,
run_command_slave 1 kopierer stdin til stdout og stderr)

sslug@sslug:~/Projects/C++/run-command$ echo hello | ./run_command './run_command_slave 1' juhuu
Run command thread running ./run_command_slave 1 with stdin=cin
./run_command: Run command ./run_command_slave 1 with stdin=juhuu
hheellllooexited with status=0
stdout=
juhuu
stderr=
juhuu



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:44 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *