1 /* cclive
2  * Copyright (C) 2010-2013  Toni Gundogdu <legatvs@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <ccinternal>
19 
20 #include <stdexcept>
21 #include <sstream>
22 #include <cstdio>
23 #include <cerrno>
24 
25 #include <sys/wait.h>
26 
27 #include <boost/program_options/variables_map.hpp>
28 #include <boost/foreach.hpp>
29 #include <pcrecpp.h>
30 
31 #ifndef foreach
32 #define foreach BOOST_FOREACH
33 #endif
34 
35 #include <ccquvi>
36 #include <ccoptions>
37 #include <ccfile>
38 #include <ccre>
39 #include <ccutil>
40 #include <cclog>
41 
42 namespace cc
43 {
44 
45 typedef std::vector<std::string> vst;
46 
invoke_exec(const vst & args)47 static int invoke_exec(const vst& args)
48 {
49   const size_t sz = args.size();
50   const char **argv = new const char* [sz+2];
51   if (!argv)
52     throw std::runtime_error("memory allocation error");
53 
54   argv[0] = args[0].c_str();
55 
56   for (size_t i=1; i<sz; ++i)
57     argv[i] = args[i].c_str();
58 
59   argv[sz] = NULL;
60 
61   fflush(stdout);
62   fflush(stderr);
63 
64   pid_t child_pid = fork();
65   if (child_pid == -1)
66     {
67       delete [] argv;
68       throw std::runtime_error(cc::perror("fork"));
69     }
70 
71   if (child_pid == 0)
72     {
73       execvp(argv[0], (char **)argv);
74       exit(1);
75     }
76 
77   delete [] argv;
78 
79   int wait_status = 0;
80   while (waitpid(child_pid, &wait_status, 0) == (pid_t)-1)
81     {
82       if (errno != EINTR)
83         {
84           cc::log << "error waiting for " << args[0] << std::endl;
85           break;
86         }
87     }
88 
89   if (WIFSIGNALED(wait_status))
90     {
91       cc::log << args[0]
92               << " terminated by signal "
93               << WTERMSIG(wait_status)
94               << std::endl;
95     }
96 
97   if (WEXITSTATUS(wait_status) == 0)
98     return 0; // OK.
99 
100   return 1;
101 }
102 
tokenize(const std::string & r,const std::string & s,vst & dst)103 static void tokenize(const std::string& r,
104                      const std::string& s,
105                      vst& dst)
106 {
107   pcrecpp::StringPiece sp(s);
108   pcrecpp::RE rx(r);
109 
110   std::string t;
111   while (rx.FindAndConsume(&sp, &t))
112     {
113       pcrecpp::RE("[\"']").GlobalReplace("", &t);
114       dst.push_back(t);
115     }
116 }
117 
118 namespace po = boost::program_options;
119 
exec(const file & file)120 void exec(const file& file)
121 {
122   const vst m = cc::opts.map()["exec"].as<vst>();
123   foreach (std::string e, m)
124   {
125     pcrecpp::RE("%f").GlobalReplace(file.path(), &e);
126     pcrecpp::RE("%n").GlobalReplace(file.name(), &e);
127     pcrecpp::RE("%t").GlobalReplace(file.title(), &e);
128 
129     vst args;
130     tokenize("([\"'](.*?)[\"']|\\S+)", e, args);
131     invoke_exec(args);
132   }
133 }
134 
135 } // namespace cc
136 
137 // vim: set ts=2 sw=2 tw=72 expandtab:
138