Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

fuzion/sys/env_vars.fz


# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation.  If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
#  Tokiwa Software GmbH, Germany
#
#  Source code of Fuzion standard library feature fuzion.sys.env_var
#
#  Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------

# fuzion.sys.env_vars -- low-level feature to access environment variables
#
module env_vars is


  # intrinsic to check if env var with given name exists
  #
  # NOTE: parameter s must be 0-terminated char *
  #
  private has0(s fuzion.sys.Pointer) bool => intrinsic_constructor


  # intrinsic to get env var with given name
  #
  # NOTE: parameter s must be 0-terminated char *
  #
  private get0(s fuzion.sys.Pointer) String
  => intrinsic

  # intrinsic to set env var
  private set0(s fuzion.sys.Pointer, t fuzion.sys.Pointer) bool => intrinsic_constructor

  # intrinsic to unset env var
  private unset0(s fuzion.sys.Pointer) bool => intrinsic_constructor


  # check if env var with given name exists
  #
  module has(s String) bool =>
    has0 (c_string s)


  # intrinsic to get env var with given name
  #
  module get(s String) option String =>
    a := c_string s
    if has0 a then
      get0 a
    else
      nil

  # set the value of the env var with given name to the given new value
  # will overwrite the current value, if it is set
  # NYI: #627: rename to set once set keyword is removed
  module set1(name String, val String) outcome unit =>
    k := c_string name
    v := c_string val

    if set0 k v
      unit
    else
      error "failed to set env var"

  # unset the env var with the given name
  # note this returns success (unit) if the given env var did not exist
  module unset(name String) outcome unit =>
    k := c_string name

    if unset0 k
      unit
    else
      error "failed to unset env var"