Environment.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Error.h>
  8. #include <AK/Function.h>
  9. #include <AK/IterationDecision.h>
  10. #include <AK/Optional.h>
  11. #include <AK/StringView.h>
  12. #include <AK/Vector.h>
  13. namespace Core::Environment {
  14. char** raw_environ();
  15. struct Entry {
  16. StringView full_entry;
  17. StringView name;
  18. StringView value;
  19. static Entry from_chars(char const* input);
  20. static Entry from_string(StringView input);
  21. };
  22. class EntryIterator {
  23. public:
  24. constexpr bool operator==(EntryIterator other) const { return m_index == other.m_index; }
  25. constexpr bool operator!=(EntryIterator other) const { return m_index != other.m_index; }
  26. constexpr EntryIterator operator++()
  27. {
  28. ++m_index;
  29. return *this;
  30. }
  31. constexpr EntryIterator operator++(int)
  32. {
  33. auto result = *this;
  34. ++m_index;
  35. return result;
  36. }
  37. Entry operator*() { return Entry::from_chars(raw_environ()[m_index]); }
  38. static EntryIterator begin();
  39. static EntryIterator end();
  40. private:
  41. explicit constexpr EntryIterator(size_t index)
  42. : m_index(index)
  43. {
  44. }
  45. size_t m_index { 0 };
  46. };
  47. EntryIterator entries();
  48. size_t size();
  49. bool has(StringView name);
  50. enum class SecureOnly {
  51. No,
  52. Yes,
  53. };
  54. Optional<StringView> get(StringView name, SecureOnly = SecureOnly::No);
  55. enum class Overwrite {
  56. No,
  57. Yes,
  58. };
  59. ErrorOr<void> set(StringView name, StringView value, Overwrite);
  60. ErrorOr<void> unset(StringView name);
  61. ErrorOr<void> put(StringView env);
  62. ErrorOr<void> clear();
  63. }