environ.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Header for environment manipulation library.
  2. Copyright (C) 1989-2022 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef COMMON_ENVIRON_H
  14. #define COMMON_ENVIRON_H
  15. #include <vector>
  16. #include <set>
  17. /* Class that represents the environment variables as seen by the
  18. inferior. */
  19. class gdb_environ
  20. {
  21. public:
  22. /* Regular constructor and destructor. */
  23. gdb_environ ()
  24. {
  25. /* Make sure that the vector contains at least a NULL element.
  26. If/when we add more variables to it, NULL will always be the
  27. last element. */
  28. m_environ_vector.push_back (NULL);
  29. }
  30. ~gdb_environ ()
  31. {
  32. clear ();
  33. }
  34. /* Move constructor. */
  35. gdb_environ (gdb_environ &&e)
  36. : m_environ_vector (std::move (e.m_environ_vector)),
  37. m_user_set_env (std::move (e.m_user_set_env)),
  38. m_user_unset_env (std::move (e.m_user_unset_env))
  39. {
  40. /* Make sure that the moved-from vector is left at a valid
  41. state (only one NULL element). */
  42. e.m_environ_vector.clear ();
  43. e.m_environ_vector.push_back (NULL);
  44. e.m_user_set_env.clear ();
  45. e.m_user_unset_env.clear ();
  46. }
  47. /* Move assignment. */
  48. gdb_environ &operator= (gdb_environ &&e);
  49. /* Create a gdb_environ object using the host's environment
  50. variables. */
  51. static gdb_environ from_host_environ ();
  52. /* Clear the environment variables stored in the object. */
  53. void clear ();
  54. /* Return the value in the environment for the variable VAR. The
  55. returned pointer is only valid as long as the gdb_environ object
  56. is not modified. */
  57. const char *get (const char *var) const;
  58. /* Store VAR=VALUE in the environment. */
  59. void set (const char *var, const char *value);
  60. /* Unset VAR in environment. */
  61. void unset (const char *var);
  62. /* Return the environment vector represented as a 'char **'. */
  63. char **envp () const;
  64. /* Return the user-set environment vector. */
  65. const std::set<std::string> &user_set_env () const;
  66. /* Return the user-unset environment vector. */
  67. const std::set<std::string> &user_unset_env () const;
  68. private:
  69. /* Unset VAR in environment. If UPDATE_UNSET_LIST is true, then
  70. also update M_USER_UNSET_ENV to reflect the unsetting of the
  71. environment variable. */
  72. void unset (const char *var, bool update_unset_list);
  73. /* A vector containing the environment variables. */
  74. std::vector<char *> m_environ_vector;
  75. /* The environment variables explicitly set by the user. */
  76. std::set<std::string> m_user_set_env;
  77. /* The environment variables explicitly unset by the user. */
  78. std::set<std::string> m_user_unset_env;
  79. };
  80. #endif /* COMMON_ENVIRON_H */