/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
  ql_env.qpp

  Qore Programming Language

  Copyright (C) 2003 - 2023 David Nichols

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  DEALINGS IN THE SOFTWARE.

  Note that the Qore library is released under a choice of three open-source
  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
  information.
*/

#include <qore/Qore.h>
#include "qore/intern/ql_env.h"

#include <cstdio>
#include <cstdlib>

/** @defgroup env_functions Environment Functions
    Environment functions
*/
///@{
//! Retrieves the value of an environment variable or @ref nothing if the variable is not set
/**
    @param var the name of the environment variable

    @return the value of an environment variable or @ref nothing if the variable is not set

    @par Example:
    @code{.py}
*string v = getenv("PATH");
    @endcode

    @since %Qore 0.8.4 tagged with PO_NO_EXTERNAL_INFO
 */
*string getenv(string var) [flags=CONSTANT;dom=EXTERNAL_INFO] {
   return SysEnv.getAsStringNode(var->getBuffer());
}

//! This function variant does nothing at all; it is only included for backwards-compatibility with qore prior to version 0.8.0 for functions that would ignore type errors in arguments
/**
 */
nothing getenv() [flags=NOOP;dom=EXTERNAL_INFO] {
}

//! Sets an environment variable to a value
/**
    @param env the name of the environment variable
    @param val the new value of the environment variable

    @return -1 for error or 0 for success; if an error occurs, errno() and/or strerror() can be used to get the error code or message

    @par Example:
    @code{.py}
if (setenv("PATH", "/bin:/usr/bin"))
    printf("error setting PATH: %s\n", strerror());
    @endcode

    @see unsetenv() to unset or clear an environment variable

    @since %Qore 0.8.4 tagged with PO_NO_PROCESS
 */
int setenv(string env, softstring val) [dom=PROCESS] {
   return SysEnv.set(env->getBuffer(), val->getBuffer());
}

//! This function variant does nothing at all; it is only included for backwards-compatibility with qore prior to version 0.8.0 for functions that would ignore type errors in arguments
/**
 */
nothing setenv() [flags=NOOP;dom=PROCESS] {
}

//! Unsets an environment variable
/**
    @param env the name of the environment variable to unset

    @return -1 for error or 0 for success; if an error occurs, errno() and/or strerror() can be used to get the error code or message

    @par Example:
    @code{.py}
if (unsetenv("PATH"))
    printf("error unsetting PATH: %s\n", strerror());
    @endcode

    @see setenv() to set an environment variable to a value

    @since %Qore 0.8.4 tagged with PO_NO_PROCESS
 */
int unsetenv(string env) [dom=PROCESS] {
   return SysEnv.unset(env->getBuffer());
}

//! This function variant does nothing at all; it is only included for backwards-compatibility with qore prior to version 0.8.0 for functions that would ignore type errors in arguments
/**
 */
nothing unsetenv() [flags=NOOP;dom=EXTERNAL_INFO] {
}
///@}
