\hypertarget{irexec_8cpp-example}{}\section{irexec.\+cpp}

\begin{DoxyCodeInclude}
\textcolor{comment}{/****************************************************************************}
\textcolor{comment}{** irexec.c ****************************************************************}
\textcolor{comment}{****************************************************************************}
\textcolor{comment}{*}
\textcolor{comment}{* irexec  - execute programs according to the pressed remote control buttons}
\textcolor{comment}{*}
\textcolor{comment}{* Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>}
\textcolor{comment}{* Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>}
\textcolor{comment}{*}
\textcolor{comment}{*/}

\textcolor{preprocessor}{#ifndef \_GNU\_SOURCE}
\textcolor{preprocessor}{#define \_GNU\_SOURCE}
\textcolor{preprocessor}{#endif}

\textcolor{preprocessor}{#ifdef HAVE\_CONFIG\_H}
\textcolor{preprocessor}{# include <config.h>}
\textcolor{preprocessor}{#endif}

\textcolor{preprocessor}{#include <errno.h>}
\textcolor{preprocessor}{#include <unistd.h>}
\textcolor{preprocessor}{#include <stdarg.h>}
\textcolor{preprocessor}{#include <stdio.h>}
\textcolor{preprocessor}{#include <stdlib.h>}
\textcolor{preprocessor}{#include <string.h>}
\textcolor{preprocessor}{#include <getopt.h>}
\textcolor{preprocessor}{#include <sys/types.h>}
\textcolor{preprocessor}{#include <sys/wait.h>}

\textcolor{preprocessor}{#include "\hyperlink{lirc__client_8h}{lirc\_client.h}"}
\textcolor{preprocessor}{#include "\hyperlink{lirc__log_8h}{lirc\_log.h}"}


\textcolor{keyword}{static} \textcolor{keyword}{const} \textcolor{keywordtype}{char}* \textcolor{keyword}{const} USAGE =
        \textcolor{stringliteral}{"Usage: irexec [options] [lircrc config\_file]\(\backslash\)n"}
        \textcolor{stringliteral}{"\(\backslash\)t-d --daemon\(\backslash\)t\(\backslash\)tRun in background\(\backslash\)n"}
        \textcolor{stringliteral}{"\(\backslash\)t-D --loglevel=level\(\backslash\)t'error', 'info', 'notice',... or 0..10\(\backslash\)n"}
        \textcolor{stringliteral}{"\(\backslash\)t-n --name=progname\(\backslash\)tUse this program name for lircrc matching\(\backslash\)n"}
        \textcolor{stringliteral}{"\(\backslash\)t-h --help\(\backslash\)t\(\backslash\)tDisplay usage summary\(\backslash\)n"}
        \textcolor{stringliteral}{"\(\backslash\)t-v --version\(\backslash\)t\(\backslash\)tDisplay version\(\backslash\)n"};

\textcolor{keyword}{static} \textcolor{keyword}{const} \textcolor{keyword}{struct }option options[] = \{
        \{ \textcolor{stringliteral}{"help"},     no\_argument,       NULL, \textcolor{charliteral}{'h'} \},
        \{ \textcolor{stringliteral}{"version"},  no\_argument,       NULL, \textcolor{charliteral}{'v'} \},
        \{ \textcolor{stringliteral}{"daemon"},   no\_argument,       NULL, \textcolor{charliteral}{'d'} \},
        \{ \textcolor{stringliteral}{"name"},     required\_argument, NULL, \textcolor{charliteral}{'n'} \},
        \{ \textcolor{stringliteral}{"loglevel"}, required\_argument, NULL, \textcolor{charliteral}{'D'} \},
        \{ 0,          0,                 0,    0   \}
\};

\textcolor{keyword}{static} \textcolor{keywordtype}{int} opt\_daemonize        = 0;
\textcolor{keyword}{static} \hyperlink{lirc__log_8h_a24e101a095456e946efea0b971fc2ee3}{loglevel\_t} opt\_loglevel        = LIRC\_NOLOG;
\textcolor{keyword}{static} \textcolor{keyword}{const} \textcolor{keywordtype}{char}* opt\_progname = \textcolor{stringliteral}{"irexec"};

\textcolor{keyword}{static} \textcolor{keywordtype}{char} path[256] = \{0\};


\textcolor{keyword}{static} \textcolor{keywordtype}{void} run\_command(\textcolor{keyword}{const} \textcolor{keywordtype}{char}* cmd)
\{
        \textcolor{keywordtype}{char}* \textcolor{keyword}{const} vp[] = \{
                strdupa(SH\_PATH), strdupa(\textcolor{stringliteral}{"-c"}), strdupa(cmd), NULL
        \};
        pid\_t pid1;
        pid\_t pid2;

        pid1 = fork();
        \textcolor{keywordflow}{if} (pid1 < 0) \{
                \hyperlink{lirc__log_8c_aecf5f8345788bbd42f490f228fee30e0}{logperror}(LIRC\_ERROR, \textcolor{stringliteral}{"Cannot fork"});
                perror(\textcolor{stringliteral}{"Cannot fork()"});
                exit(EXIT\_FAILURE);
        \}
        \textcolor{keywordflow}{if} (pid1 == 0) \{
                pid2 = fork();
                \textcolor{keywordflow}{if} (pid2 < 0) \{
                        \hyperlink{lirc__log_8c_aecf5f8345788bbd42f490f228fee30e0}{logperror}(LIRC\_ERROR, \textcolor{stringliteral}{"Cannot do secondary fork()"});
                        exit(EXIT\_FAILURE);
                \}
                \textcolor{keywordflow}{if} (pid2 == 0) \{
                        logprintf(LIRC\_DEBUG, \textcolor{stringliteral}{"Execing command \(\backslash\)"%s\(\backslash\)""}, cmd);
                        execvp(SH\_PATH, vp);
                        \textcolor{comment}{/* not reached */}
                        \hyperlink{lirc__log_8c_aecf5f8345788bbd42f490f228fee30e0}{logperror}(LIRC\_ERROR, \textcolor{stringliteral}{"execvp failed"});
                        fputs(\textcolor{stringliteral}{"execvp failed\(\backslash\)n"}, stderr);
                \} \textcolor{keywordflow}{else} \{
                        waitpid(pid2, NULL, WNOHANG);
                        exit(0);
                \}
        \} \textcolor{keywordflow}{else} \{
                waitpid(pid1, NULL, 0);
        \}
\}


\textcolor{keyword}{static} \textcolor{keywordtype}{void} process\_input(\textcolor{keyword}{struct} \hyperlink{structlirc__config}{lirc\_config}* config)
\{
        \textcolor{keywordtype}{char}* code;
        \textcolor{keywordtype}{char}* c;
        \textcolor{keywordtype}{int} r;

        \textcolor{keywordflow}{while} (\hyperlink{group__lirc__client_gaf352dc9bb17d32c0f867ce5ca8a2f2d3}{lirc\_nextcode}(&code) == 0) \{
                \textcolor{keywordflow}{if} (code == NULL)
                        \textcolor{keywordflow}{continue};
                r = \hyperlink{group__lirc__client_ga8cb15780179dec56adc42559ee964a68}{lirc\_code2char}(config, code, &c);
                \textcolor{keywordflow}{while} (r == 0 && c != NULL) \{
                        run\_command(c);
                        r = \hyperlink{group__lirc__client_ga8cb15780179dec56adc42559ee964a68}{lirc\_code2char}(config, code, &c);
                \}
                free(code);
                \textcolor{keywordflow}{if} (r == -1)
                        \textcolor{keywordflow}{break};
        \}
\}


\textcolor{keywordtype}{int} irexec(\textcolor{keyword}{const} \textcolor{keywordtype}{char}* configfile)
\{
        \textcolor{keyword}{struct }\hyperlink{structlirc__config}{lirc\_config}* config;

        \textcolor{keywordflow}{if} (opt\_daemonize) \{
                \textcolor{keywordflow}{if} (daemon(0, 0) == -1) \{
                        perror(\textcolor{stringliteral}{"Can't daemonize"});
                        \textcolor{keywordflow}{return} EXIT\_FAILURE;
                \}
        \}
        \textcolor{keywordflow}{if} (\hyperlink{group__lirc__client_gaf92c865f0895259aa40e2aed51c71001}{lirc\_init}(opt\_progname, opt\_daemonize ? 0 : 1) == -1)
                \textcolor{keywordflow}{return} EXIT\_FAILURE;

        \textcolor{keywordflow}{if} (\hyperlink{group__lirc__client_ga62e50c43a39bd8858d42bc11adf40972}{lirc\_readconfig}(configfile, &config, NULL) != 0) \{
                fputs(\textcolor{stringliteral}{"Cannot parse config file\(\backslash\)n"}, stderr);
                \textcolor{keywordflow}{return} EXIT\_FAILURE;
        \}
        \hyperlink{lirc__log_8c_a5af3dc8e52fabe122ac7ec52c6852885}{lirc\_log\_get\_clientlog}(\textcolor{stringliteral}{"irexec"}, path, \textcolor{keyword}{sizeof}(path));
        unlink(path);
        \hyperlink{lirc__log_8c_a2eaf417e2b2dd63861d17b2c836fe1b8}{lirc\_log\_set\_file}(path);
        \hyperlink{lirc__log_8c_a3154a9cb342ea5f79edfe7ced8504ccd}{lirc\_log\_open}(\textcolor{stringliteral}{"irexec"}, 1, opt\_loglevel);

        process\_input(config);
        \hyperlink{group__lirc__client_ga50999022b09857ba43aa2f3f29c72b9e}{lirc\_deinit}();

        \hyperlink{group__lirc__client_ga771f0592a8d378bc2ea314fdf4efa233}{lirc\_freeconfig}(config);
        \textcolor{keywordflow}{return} EXIT\_SUCCESS;
\}


\textcolor{keywordtype}{int} main(\textcolor{keywordtype}{int} argc, \textcolor{keywordtype}{char}* argv[])
\{
        \textcolor{keywordtype}{int} c;

        \textcolor{keywordflow}{while} ((c = getopt\_long(argc, argv, \textcolor{stringliteral}{"D:hvdn:"}, options, NULL)) != -1) \{
                \textcolor{keywordflow}{switch} (c) \{
                \textcolor{keywordflow}{case} \textcolor{charliteral}{'h'}:
                        puts(USAGE);
                        \textcolor{keywordflow}{return} EXIT\_SUCCESS;
                \textcolor{keywordflow}{case} \textcolor{charliteral}{'v'}:
                        puts(\textcolor{stringliteral}{"irexec "} VERSION);
                        \textcolor{keywordflow}{return} EXIT\_SUCCESS;
                \textcolor{keywordflow}{case} \textcolor{charliteral}{'d'}:
                        opt\_daemonize = 1;
                        \textcolor{keywordflow}{break};
                \textcolor{keywordflow}{case} \textcolor{charliteral}{'n'}:
                        opt\_progname = optarg;
                        \textcolor{keywordflow}{break};
                \textcolor{keywordflow}{case} \textcolor{charliteral}{'D'}:
                        opt\_loglevel = \hyperlink{lirc__log_8c_a2a2913bd83563a14c1202e9aafd5900c}{string2loglevel}(optarg);
                        \textcolor{keywordflow}{break};
                \textcolor{keywordflow}{default}:
                        fputs(USAGE, stderr);
                        \textcolor{keywordflow}{return} EXIT\_FAILURE;
                \}
        \}
        \textcolor{keywordflow}{if} (optind < argc - 1) \{
                fputs(\textcolor{stringliteral}{"Too many arguments\(\backslash\)n"}, stderr);
                \textcolor{keywordflow}{return} EXIT\_FAILURE;
        \}
        \textcolor{keywordflow}{if} (opt\_loglevel == LIRC\_BADLEVEL) \{
                fprintf(stderr, \textcolor{stringliteral}{"Bad debug level: %s\(\backslash\)n"}, optarg);
                \textcolor{keywordflow}{return} EXIT\_FAILURE;
        \}
        \textcolor{keywordflow}{return} irexec(optind != argc ? argv[optind] : NULL);
\}
\end{DoxyCodeInclude}
 