- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gexecutableprocessor.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
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 // gexecutableprocessor.cpp
19 //
20
21 #include "gdef.h"
22 #include "gsmtp.h"
23 #include "gprocess.h"
24 #include "gnewprocess.h"
25 #include "gexecutableprocessor.h"
26 #include "gstr.h"
27 #include "groot.h"
28 #include "glog.h"
29
30 GSmtp::ExecutableProcessor::ExecutableProcessor( const G::Executable & exe ) :
31 m_exe(exe) ,
32 m_ok(true) ,
33 m_cancelled(false) ,
34 m_repoll(false)
35 {
36 }
37
38 GSmtp::ExecutableProcessor::~ExecutableProcessor()
39 {
40 }
41
42 bool GSmtp::ExecutableProcessor::cancelled() const
43 {
44 return m_cancelled ;
45 }
46
47 bool GSmtp::ExecutableProcessor::repoll() const
48 {
49 return m_repoll ;
50 }
51
52 std::string GSmtp::ExecutableProcessor::text() const
53 {
54 if( m_ok ) return std::string() ;
55 const std::string default_ = "pre-processing failed" ;
56 return m_text.empty() ? default_ : m_text ;
57 }
58
59 bool GSmtp::ExecutableProcessor::process( const std::string & path )
60 {
61 int exit_code = preprocessCore( G::Path(path) ) ;
62
63 // zero, special or failure
64 bool is_zero = exit_code == 0 ;
65 bool is_special = exit_code >= 100 && exit_code <= 107 ;
66 bool is_failure = !is_zero && !is_special ;
67 if( is_failure )
68 {
69 G_WARNING( "GSmtp::ExecutableProcessor::preprocess: pre-processing failed: exit code " << exit_code ) ;
70 }
71
72 // set special-repoll and special-cancelled flags
73 m_repoll = is_special && ((exit_code-100)&2) != 0 ;
74 m_cancelled = is_special && ((exit_code-100)&1) == 0 ;
75
76 // treat special as ok, except for special-cancelled
77 m_ok = is_zero || ( is_special && !m_cancelled ) ;
78 return m_ok ;
79 }
80
81 int GSmtp::ExecutableProcessor::preprocessCore( const G::Path & path )
82 {
83 G_LOG( "GSmtp::ExecutableProcessor::preprocess: running \"" << m_exe.displayString() << " " << path << "\"" ) ;
84
85 // add the path of the content file as a trailing command-line parameter
86 G::Strings args( m_exe.args() ) ;
87 args.push_back( path.str() ) ;
88
89 // run the program
90 std::string raw_output ;
91 int exit_code = G::NewProcess::spawn( G::Root::nobody() , m_exe.exe() , args ,
92 &raw_output , 127 , execErrorHandler ) ;
93
94 // search the output for diagnostics
95 m_text = parseOutput( raw_output ) ;
96 G_LOG( "GSmtp::ExecutableProcessor::preprocess: exit status " << exit_code << " (\"" << m_text << "\")" ) ;
97
98 return exit_code ;
99 }
100
101 std::string GSmtp::ExecutableProcessor::execErrorHandler( int error )
102 {
103 // (this runs in the fork()ed child process)
104 std::ostringstream ss ;
105 ss << "<<exec error " << error << ": " << G::Str::lower(G::Process::strerror(error)) << ">>" ;
106 return ss.str() ;
107 }
108
109 std::string GSmtp::ExecutableProcessor::parseOutput( std::string s ) const
110 {
111 G_DEBUG( "GSmtp::ExecutableProcessor::parseOutput: in: \"" << G::Str::printable(s) << "\"" ) ;
112
113 const std::string start_1("<<") ;
114 const std::string end_1(">>") ;
115 const std::string start_2("[[") ;
116 const std::string end_2("]]") ;
117
118 std::string result ;
119 while( G::Str::replaceAll( s , "\r\n" , "\n" ) ) ;
120 G::Str::replaceAll( s , "\r" , "\n" ) ;
121 G::Strings lines ;
122 G::Str::splitIntoFields( s , lines , "\n" ) ;
123 for( G::Strings::iterator p = lines.begin() ; p != lines.end() ; ++p )
124 {
125 std::string line = *p ;
126 size_t pos_start = line.find(start_1) ;
127 size_t pos_end = line.find(end_1) ;
128 if( pos_start != 0U )
129 {
130 pos_start = line.find(start_2) ;
131 pos_end = line.find(end_2) ;
132 }
133 if( pos_start == 0U && pos_end != std::string::npos )
134 {
135 result = G::Str::printable(line.substr(2U,pos_end-2U)) ;
136 break ;
137 }
138 }
139 G_DEBUG( "GSmtp::ExecutableProcessor::parseOutput: out: \"" << G::Str::printable(result) << "\"" ) ;
140 return result ;
141 }
142
143 G::Signal1<bool> & GSmtp::ExecutableProcessor::doneSignal()
144 {
145 return m_done_signal ;
146 }
147
148 void GSmtp::ExecutableProcessor::abort()
149 {
150 // no-op -- not asynchronous
151 }
152
153 void GSmtp::ExecutableProcessor::start( const std::string & message_file )
154 {
155 // not asynchronous -- process() synchronously and emit the done signal
156 bool ok = process( message_file ) ;
157 m_done_signal.emit( ok ) ;
158 }
159
GSmtp::ExecutableProcessor::ExecutableProcessor
ExecutableProcessor(const G::Executable &)
Constructor.
Definition: gexecutableprocessor.cpp:30
G::NewProcess::spawn
static int spawn(Identity nobody, const Path &exe, const Strings &args, std::string *pipe_result_p=NULL, int error_return=127, std::string(*error_decode_fn)(int)=0)
Runs a command in an unprivileged child process.
Definition: gnewprocess_unix_exec_enabled.cpp:232
G::Path::str
std::string str() const
Returns the path string.
Definition: gpath.cpp:135
G::Str::printable
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
Definition: gstr.cpp:507
GSmtp::ExecutableProcessor::start
virtual void start(const std::string &path)
Final override from from GSmtp::Processor.
Definition: gexecutableprocessor.cpp:153
glog.h
G::Root::nobody
static Identity nobody()
Returns the 'nobody' identity.
Definition: groot.cpp:82
G::Strings
std::list< std::string > Strings
A std::list of std::strings.
Definition: gstrings.h:39
G::Str::splitIntoFields
static void splitIntoFields(const std::string &in, Strings &out, const std::string &seperators, char escape= '\0', bool discard_bogus_escapes=true)
Splits the string into fields.
Definition: gstr.cpp:765
G::Executable
A structure representing an external program, holding a path and a set of arguments.
Definition: gexecutable.h:43
G::Signal1< bool >
groot.h
GSmtp::ExecutableProcessor::text
virtual std::string text() const
Final override from from GSmtp::Processor.
Definition: gexecutableprocessor.cpp:52
G::Str::lower
static std::string lower(const std::string &s)
Returns a copy of 's' in which all uppercase characters have been replaced by lowercase characters...
Definition: gstr.cpp:408
gexecutableprocessor.h
gstr.h
G::Str::replaceAll
static unsigned int replaceAll(std::string &s, const std::string &from, const std::string &to)
Does a global replace on string 's', replacing all occurences of sub-string 'from' with 'to'...
Definition: gstr.cpp:78
GSmtp::ExecutableProcessor::abort
virtual void abort()
Final override from from GSmtp::Processor.
Definition: gexecutableprocessor.cpp:148
G_LOG
#define G_LOG(expr)
Definition: glog.h:98
gprocess.h
GSmtp::ExecutableProcessor::~ExecutableProcessor
virtual ~ExecutableProcessor()
Destructor.
Definition: gexecutableprocessor.cpp:38
G_DEBUG
#define G_DEBUG(expr)
Definition: glog.h:95
gnewprocess.h
GSmtp::ExecutableProcessor::cancelled
virtual bool cancelled() const
Final override from from GSmtp::Processor.
Definition: gexecutableprocessor.cpp:42
gsmtp.h
GSmtp::ExecutableProcessor::doneSignal
virtual G::Signal1< bool > & doneSignal()
Final override from from GSmtp::Processor.
Definition: gexecutableprocessor.cpp:143
GSmtp::ExecutableProcessor::repoll
virtual bool repoll() const
Final override from from GSmtp::Processor.
Definition: gexecutableprocessor.cpp:47
G::Path
A Path object represents a file system path.
Definition: gpath.h:44
G::Process::strerror
static std::string strerror(int errno_)
Translates an 'errno' value into a meaningful diagnostic string.
Definition: gprocess_unix.cpp:119
G_WARNING
#define G_WARNING(expr)
Definition: glog.h:107