-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
dinit-env.cc
133 lines (114 loc) · 4.08 KB
/
dinit-env.cc
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <fstream>
#include "dinit-log.h"
#include "dinit-env.h"
environment main_env;
// Log a parse error when reading the environment file.
static void log_bad_env(int linenum)
{
log(loglevel_t::ERROR, "Invalid environment variable setting in environment file (line ", linenum, ")");
}
static void log_bad_env_cmd(int linenum)
{
log(loglevel_t::ERROR, "Unknown command in environment file (line ", linenum, ")");
}
// Read and set environment variables from a file. May throw std::bad_alloc, std::system_error.
void read_env_file(const char *env_file_path, bool log_warnings, environment &env)
{
std::ifstream env_file(env_file_path);
if (! env_file) return;
env_file.exceptions(std::ios::badbit);
auto &clocale = std::locale::classic();
std::string line;
int linenum = 0;
while (std::getline(env_file, line)) {
linenum++;
auto lpos = line.begin();
auto lend = line.end();
while (lpos != lend && std::isspace(*lpos, clocale)) {
++lpos;
}
if (lpos == lend) continue; // empty line
if (*lpos == '#') {
continue;
}
if (*lpos == '=') {
if (log_warnings) {
log_bad_env(linenum);
}
continue;
}
// "!COMMAND" form.
if (*lpos == '!') {
++lpos; // lpos = first char of command
auto epos = lpos;
do {
++epos;
} while(epos != lend && !std::isspace(*epos, clocale));
const char *lpos_p = line.data() + (lpos - line.begin());
string_view cmd {lpos_p, (size_t)(epos - lpos)};
std::vector<string_view> cmd_args;
while (epos != lend) {
// skip whitespace
while (std::isspace(*epos, clocale)) {
++epos;
if (epos == lend) goto process_cmd; // no more args
}
// all non-ws is argument until next ws
const char *arg_begin = line.c_str() + (epos - line.begin());
auto arg_begin_i = epos;
while (epos != lend && !std::isspace(*epos)) {
++epos;
}
cmd_args.push_back(string_view {arg_begin, (size_t)(epos - arg_begin_i)});
}
process_cmd:
if (cmd == "clear") {
env.clear_no_inherit();
}
else if (cmd == "unset") {
for (string_view arg : cmd_args) {
env.undefine_var(std::string(arg.data(), arg.length()));
}
}
else if (cmd == "import") {
for (string_view arg : cmd_args) {
env.import_parent_var(std::string(arg.data(), arg.length()));
}
}
else if (log_warnings) {
log_bad_env_cmd(linenum);
}
continue;
}
// ENV=VALUE form.
auto name_begin = lpos++;
// skip until '=' or whitespace:
while (lpos != lend && *lpos != '=' && !std::isspace(*lpos, clocale)) ++lpos;
auto name_end = lpos;
// skip whitespace:
while (lpos != lend && std::isspace(*lpos, clocale)) ++lpos;
if (lpos == lend || *lpos != '=') {
if (log_warnings) {
log_bad_env(linenum);
}
continue;
}
++lpos;
auto val_begin = lpos;
auto val_end = lend;
if (val_begin != (name_end + 1) || name_begin != line.begin()) {
// there are spaces that we need to eliminate
std::string name_and_val;
name_and_val.reserve((name_end - name_begin) + 1 + (val_end - val_begin));
name_and_val = line.substr(name_begin - line.begin(), name_end - name_begin);
name_and_val.append(1, '=');
name_and_val.append(val_begin, val_end);
env.set_var(std::move(name_and_val));
}
else {
line.shrink_to_fit();
env.set_var(std::move(line));
}
}
}