~unity-team/unity-scopes-api/vivid+overlay

« back to all changes in this revision

Viewing changes to src/scopes/internal/Utils.cpp

  • Committer: CI Train Bot
  • Author(s): Pawel Stolowski
  • Date: 2015-09-21 13:32:10 UTC
  • mfrom: (261.1.44 staging)
  • Revision ID: ci-train-bot@canonical.com-20150921133210-53i4a31z2kkik91u
Merged devel.
Approved by: Marcus Tomlinson

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
#include <unity/scopes/internal/Utils.h>
20
20
#include <unity/scopes/ScopeExceptions.h>
21
21
#include <unity/UnityExceptions.h>
 
22
#include <unity/util/ResourcePtr.h>
22
23
 
23
24
#include <boost/filesystem.hpp>
24
25
 
25
26
#include <iomanip>
26
27
#include <locale>
27
28
#include <mutex>
 
29
#include <wordexp.h>
28
30
 
29
31
#include <sys/stat.h>
30
32
 
190
192
    }
191
193
}
192
194
 
 
195
vector<string> split_exec_args(string const& id, string const& custom_exec)
 
196
{
 
197
    if (custom_exec.empty())
 
198
    {
 
199
        throw unity::InvalidArgumentException("Invalid empty executable for scope: '" + id + "'");
 
200
    }
 
201
 
 
202
    wordexp_t exp;
 
203
    vector<string> result;
 
204
 
 
205
    // Split command into program and args
 
206
    if (wordexp(custom_exec.c_str(), &exp, WRDE_NOCMD) == 0)
 
207
    {
 
208
        util::ResourcePtr<wordexp_t*, decltype(&wordfree)> free_guard(&exp, wordfree);
 
209
        for (size_t i = 0; i < exp.we_wordc; ++i )
 
210
        {
 
211
            auto argument = string(exp.we_wordv[i]);
 
212
            if(argument.find_first_of(' ') != std::string::npos)
 
213
            {
 
214
                // This argument contains spaces, enclose it in quotation marks
 
215
                result.emplace_back("\"" + argument + "\"");
 
216
            }
 
217
            else
 
218
            {
 
219
                result.emplace_back(argument);
 
220
            }
 
221
        }
 
222
    }
 
223
    else
 
224
    {
 
225
        throw unity::InvalidArgumentException("Invalid executable for scope: '" + id + "'");
 
226
    }
 
227
 
 
228
    return result;
 
229
}
 
230
 
 
231
string convert_exec_rel_to_abs(string const& id, boost::filesystem::path const& scope_dir, string const& custom_exec)
 
232
{
 
233
    string result;
 
234
 
 
235
    // Loop through each argument of the scope runner command and ensure all path args are absolute
 
236
    auto custom_exec_args = split_exec_args(id, custom_exec);
 
237
    for (auto custom_exec_arg : custom_exec_args)
 
238
    {
 
239
        boost::filesystem::path argument(custom_exec_arg);
 
240
        if (argument.is_relative())
 
241
        {
 
242
            // First look inside the arch-specific directory
 
243
            if (boost::filesystem::exists(scope_dir / DEB_HOST_MULTIARCH / argument))
 
244
            {
 
245
                // Append the argument as a relative path
 
246
                result += (scope_dir / DEB_HOST_MULTIARCH / argument).native() + " ";
 
247
            }
 
248
            // Next try in the non arch-aware directory
 
249
            else if (boost::filesystem::exists(scope_dir / argument))
 
250
            {
 
251
                // Append the argument as a relative path
 
252
                result += (scope_dir / argument).native() + " ";
 
253
            }
 
254
            // If this is the first argument (program name) it must exist, throw here
 
255
            else if (result.empty())
 
256
            {
 
257
                throw unity::InvalidArgumentException(
 
258
                        "Nonexistent scope runner executable: '" + custom_exec_arg
 
259
                                + "' for scope: '" + id + "'");
 
260
            }
 
261
            // Otherwise just append the argument as is
 
262
            else
 
263
            {
 
264
                result += custom_exec_arg + " ";
 
265
            }
 
266
        }
 
267
        else
 
268
        {
 
269
            result += custom_exec_arg + " ";
 
270
        }
 
271
    }
 
272
    result.resize(result.size() - 1);
 
273
    return result;
 
274
}
 
275
 
193
276
} // namespace internal
194
277
 
195
278
} // namespace scopes