EnvExpander.h
Go to the documentation of this file.
1/**
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @author Fabian Reister ( fabian dot reister at kit dot edu )
17 * @date 2025
18 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19 * GNU General Public License
20 */
21
22#pragma once
23
24
25#include <cstdint>
26#include <cstdlib>
27#include <string>
28#include <tuple>
29#include <vector>
30
31#include <boost/process/environment.hpp>
32
34{
35
36
38 {
39 public:
40
41 /**
42 * Construct an EnvExpander initialized with the current process
43 * environment. The instance keeps a working copy of the environment
44 * (stored in `env_`) which `apply()` will update when assignments are
45 * applied. This allows expansions to optionally see earlier changes.
46 */
48
49 /**
50 * Parse and apply the comma-separated assignment list to the internal
51 * environment `env_` and to the process environment via `setenv`.
52 *
53 * The input format is a comma-separated list of assignments. Each
54 * assignment may be a replace (NAME=val), append (NAME+=val) or
55 * prepend (NAME=+val). Whitespace around entries is ignored. Values
56 * may include environment variable references (e.g. $HOME or ${HOME}),
57 * which will be expanded before applying the assignment.
58 *
59 * Current implementation notes: `apply` uses `expand` to expand the
60 * RHS values and then applies the parsed operations. The working copy
61 * `env_` is updated so later calls can observe previous changes.
62 */
63 void apply(const std::string& input, bool expandExisting = true);
64
65 /**
66 * Return the expanded form of the comma-separated assignment list
67 * without modifying the process environment. This mirrors the
68 * expansions performed by `apply`.
69 *
70 * Special behavior: append and prepend operations are returned as
71 * explicit replacement assignments combining the existing environment
72 * value and the expanded RHS. For example, if the current value of
73 * PATH is "/usr/bin" and the input contains "PATH+=/opt/bin", this
74 * method returns "PATH=/usr/bin:/opt/bin" (or "PATH=/opt/bin" if
75 * PATH was not previously set).
76 */
77 static std::string expand(const std::string& input);
78
79 enum class Operation : std::uint8_t
80 {
84 };
85
86 boost::process::environment env_;
87
88 // ---------------------------
89 // Parsing helpers
90 // ---------------------------
91
92 static std::vector<std::string> splitAssignments(const std::string& input);
93
94 static std::tuple<std::string, Operation, std::string>
95 splitOperation(const std::string& assignment);
96
97 // ---------------------------
98 // Variable expansion with warnings
99 // ---------------------------
100
101 static std::string expandVariables(const std::string& value);
102
103 // ---------------------------
104 // Apply update to environment
105 // ---------------------------
106
107 void applyOperation(const std::string& name, Operation op, const std::string& val);
108 };
109
110
111} // namespace armarx::core::system
static std::vector< std::string > splitAssignments(const std::string &input)
static std::string expand(const std::string &input)
Return the expanded form of the comma-separated assignment list without modifying the process environ...
EnvExpander()
Construct an EnvExpander initialized with the current process environment.
static std::tuple< std::string, Operation, std::string > splitOperation(const std::string &assignment)
boost::process::environment env_
Definition EnvExpander.h:86
void applyOperation(const std::string &name, Operation op, const std::string &val)
static std::string expandVariables(const std::string &value)
void apply(const std::string &input, bool expandExisting=true)
Parse and apply the comma-separated assignment list to the internal environment env_ and to the proce...
This file is part of ArmarX.