-
Notifications
You must be signed in to change notification settings - Fork 2
/
scripts.cpp
49 lines (40 loc) · 1.04 KB
/
scripts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (C) 2019 by Yuri Victorovich. All rights reserved.
#include "scripts.h"
#include "util.h"
#include <rang.hpp>
#include <string>
#include <map>
#include <iostream>
#include <ostream>
namespace Scripts {
//
// helpers
//
static std::string escape(const std::string &script) {
// escape in order to be single-quoted, and run by /bin/sh via system(3)
// XXX not sure if this is a correct escaping procedure, because it isn't clear how system(3) interprets the string
std::ostringstream ss;
for (auto chr : script)
switch (chr) {
case '\'':
ss << "'\\''";
break;
case '$':
case '#':
case '"':
ss << "\\";
default:
ss << chr;
}
return ss.str();
}
//
// interface
//
void section(const char *sec, const std::map<std::string, std::map<std::string, std::string>> &scripts, FnRunner fnRunner) {
auto it = scripts.find(sec);
if (it != scripts.end())
for (auto &script : it->second)
fnRunner(STR("/bin/sh -c '" << escape(Util::pathSubstituteVarsInString(script.second)) << "'"));
}
}