1 /* Header for environment manipulation library. 2 Copyright (C) 1989-2021 Free Software Foundation, Inc. 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 #ifndef COMMON_ENVIRON_H 18 #define COMMON_ENVIRON_H 19 20 #include <vector> 21 #include <set> 22 23 /* Class that represents the environment variables as seen by the 24 inferior. */ 25 26 class gdb_environ 27 { 28 public: 29 /* Regular constructor and destructor. */ gdb_environ()30 gdb_environ () 31 { 32 /* Make sure that the vector contains at least a NULL element. 33 If/when we add more variables to it, NULL will always be the 34 last element. */ 35 m_environ_vector.push_back (NULL); 36 } 37 ~gdb_environ()38 ~gdb_environ () 39 { 40 clear (); 41 } 42 43 /* Move constructor. */ gdb_environ(gdb_environ && e)44 gdb_environ (gdb_environ &&e) 45 : m_environ_vector (std::move (e.m_environ_vector)), 46 m_user_set_env (std::move (e.m_user_set_env)), 47 m_user_unset_env (std::move (e.m_user_unset_env)) 48 { 49 /* Make sure that the moved-from vector is left at a valid 50 state (only one NULL element). */ 51 e.m_environ_vector.clear (); 52 e.m_environ_vector.push_back (NULL); 53 e.m_user_set_env.clear (); 54 e.m_user_unset_env.clear (); 55 } 56 57 /* Move assignment. */ 58 gdb_environ &operator= (gdb_environ &&e); 59 60 /* Create a gdb_environ object using the host's environment 61 variables. */ 62 static gdb_environ from_host_environ (); 63 64 /* Clear the environment variables stored in the object. */ 65 void clear (); 66 67 /* Return the value in the environment for the variable VAR. The 68 returned pointer is only valid as long as the gdb_environ object 69 is not modified. */ 70 const char *get (const char *var) const; 71 72 /* Store VAR=VALUE in the environment. */ 73 void set (const char *var, const char *value); 74 75 /* Unset VAR in environment. */ 76 void unset (const char *var); 77 78 /* Return the environment vector represented as a 'char **'. */ 79 char **envp () const; 80 81 /* Return the user-set environment vector. */ 82 const std::set<std::string> &user_set_env () const; 83 84 /* Return the user-unset environment vector. */ 85 const std::set<std::string> &user_unset_env () const; 86 87 private: 88 /* Unset VAR in environment. If UPDATE_UNSET_LIST is true, then 89 also update M_USER_UNSET_ENV to reflect the unsetting of the 90 environment variable. */ 91 void unset (const char *var, bool update_unset_list); 92 93 /* A vector containing the environment variables. */ 94 std::vector<char *> m_environ_vector; 95 96 /* The environment variables explicitly set by the user. */ 97 std::set<std::string> m_user_set_env; 98 99 /* The environment variables explicitly unset by the user. */ 100 std::set<std::string> m_user_unset_env; 101 }; 102 103 #endif /* COMMON_ENVIRON_H */ 104