[m-rev.] for post-commit review: move get_environment_var_map/3 to the io module

Julien Fischer jfischer at opturion.com
Tue Aug 24 02:42:07 AEST 2021


Move get_environment_var_map/3 to the io module.

compiler/options_file.m:
      Move the code for reading the complete environment from here ...

library/io.m:
      to here.

NEWS:
      Announce the addition.

Julien.


diff --git a/NEWS b/NEWS
index f7c9106..14a6eed 100644
--- a/NEWS
+++ b/NEWS
@@ -155,6 +155,7 @@ Changes to the Mercury standard library

  * The following predicates have been added to this module:

+    - pred `get_environment_var_map/3`
      - pred `read_named_file_as_string/4`
      - pred `read_named_file_as_lines/4`
      - pred `write_line_cc/4`
diff --git a/compiler/options_file.m b/compiler/options_file.m
index e252a47..eda8c5d 100644
--- a/compiler/options_file.m
+++ b/compiler/options_file.m
@@ -113,22 +113,6 @@

  %---------------------------------------------------------------------------%

-    % Values of this type map the names of environment variables
-    % to their values.
-    %
-:- type env_var_map == map(string, string).
-
-    % Return a map containing all the environment variables in the current
-    % environment, together with their values.
-    %
-    % XXX Unused right now, but should enable us to avoid passing around
-    % I/O states just to check whether an environment variable specifies
-    % some options for a module.
-    %
-:- pred get_environment_var_map(env_var_map::out, io::di, io::uo) is det.
-
-%---------------------------------------------------------------------------%
-
      % dump_options_file(ErrorStream, FileName, Vars, !IO):
      %
      % Write out the given database given by Vars to a file named FileName,
@@ -1646,101 +1630,6 @@ lookup_variable_value(Variables, VarName, Value, !UndefVarNames, !IO) :-

  %---------------------------------------------------------------------------%

-:- pragma foreign_import_module("C", io).   % for char **environ
-
-get_environment_var_map(EnvVarMap, !IO) :-
-    get_environment_var_assoc_list([], EnvVarAL, !IO),
-    map.from_assoc_list(EnvVarAL, EnvVarMap).
-
-:- type env_var_assoc_list == assoc_list(string, string).
-
-:- pred get_environment_var_assoc_list(
-    env_var_assoc_list::in, env_var_assoc_list::out,
-    io::di, io::uo) is det.
-
-:- pragma foreign_proc("C",
-    get_environment_var_assoc_list(EnvVarAL0::in, EnvVarAL::out,
-        _IO0::di, _IO::uo),
-    [may_call_mercury, promise_pure, tabled_for_io],
-"
-    MR_Word cur_env = EnvVarAL0;
-    MR_Word next_env;
-    int     i;
-    char    **environ_vars;
-
-    // See the comments about the enivorn global in library/io.m
-    // for an explanation of this.
-    #if defined(MR_MAC_OSX)
-        environ_vars = (*_NSGetEnviron());
-    #else
-        environ_vars = environ;
-    #endif
-
-    for (i = 0; environ_vars[i] != NULL; i++) {
-        MC_record_env_var_equal_value(environ_vars[i], cur_env, &next_env);
-        cur_env = next_env;
-    }
-
-    EnvVarAL = cur_env;
-").
-
-:- pragma foreign_proc("Java",
-    get_environment_var_assoc_list(EnvVarAL0::in, EnvVarAL::out,
-        _IO0::di, _IO::uo),
-    [may_call_mercury, promise_pure],
-"
-    EnvVarAL = EnvVarAL0;
-    java.util.Map<String, String> env = java.lang.System.getenv();
-    for (java.util.Map.Entry<String, String> entry : env.entrySet()) {
-        String name = entry.getKey();
-        String value = entry.getValue();
-        EnvVarAL = MC_record_env_var_and_value(name, value, EnvVarAL);
-    }
-").
-
-:- pragma foreign_proc("C#",
-    get_environment_var_assoc_list(EnvVarAL0::in, EnvVarAL::out,
-        _IO0::di, _IO::uo),
-    [may_call_mercury, promise_pure],
-"
-    EnvVarAL = EnvVarAL0;
-    System.Collections.IDictionary env =
-        System.Environment.GetEnvironmentVariables();
-    foreach (System.Collections.DictionaryEntry entry in env) {
-        string name = (string) entry.Key;
-        string value = (string) entry.Value;
-        EnvVarAL = MC_record_env_var_and_value(name, value, EnvVarAL);
-    }
-").
-
-:- pred record_env_var_equal_value(string::in,
-    env_var_assoc_list::in, env_var_assoc_list::out) is det.
-:- pragma foreign_export("C", record_env_var_equal_value(in, in, out),
-    "MC_record_env_var_equal_value").
-
-record_env_var_equal_value(EnvVarNameEqValue, !EnvVarAL) :-
-    ( if
-        sub_string_search(EnvVarNameEqValue, "=", IndexOfEq),
-        string.split(EnvVarNameEqValue, IndexOfEq, EnvVarName, EqEnvVarValue),
-        string.first_char(EqEnvVarValue, _Eq, EnvVarValue)
-    then
-        !:EnvVarAL = [EnvVarName - EnvVarValue | !.EnvVarAL]
-    else
-        unexpected($pred, "No = in environment entry")
-    ).
-
-:- pred record_env_var_and_value(string::in, string::in,
-    env_var_assoc_list::in, env_var_assoc_list::out) is det.
-:- pragma foreign_export("C#", record_env_var_and_value(in, in, in, out),
-    "MC_record_env_var_and_value").
-:- pragma foreign_export("Java", record_env_var_and_value(in, in, in, out),
-    "MC_record_env_var_and_value").
-
-record_env_var_and_value(EnvVarName, EnvVarValue, !EnvVarAL) :-
-    !:EnvVarAL = [EnvVarName - EnvVarValue | !.EnvVarAL].
-
-%---------------------------------------------------------------------------%
-
  dump_options_file(DebugStream, FileName, Variables, !IO) :-
      io.open_output(FileName, OpenResult, !IO),
      (
diff --git a/library/io.m b/library/io.m
index e3998ed..2c2bc0f 100644
--- a/library/io.m
+++ b/library/io.m
@@ -1823,6 +1823,17 @@
      %
  :- pred have_set_environment_var is semidet.

+    % Values of this type map the names of environment variables
+    % to their values.
+    %
+:- type environment_var_map == map(string, string).
+
+    % Return a map containing all the environment variables in the current
+    % environment, together with their values.
+    %
+:- pred get_environment_var_map(environment_var_map::out,
+    io::di, io::uo) is det.
+
  %---------------------------------------------------------------------------%
  %
  % System access predicates.
@@ -2203,6 +2214,7 @@

  :- implementation.

+:- import_module assoc_list.
  :- import_module dir.
  :- import_module exception.
  :- import_module int.
@@ -2210,6 +2222,7 @@
  :- import_module int16.
  :- import_module int32.
  :- import_module int64.
+:- import_module pair.
  :- import_module parser.
  :- import_module require.
  :- import_module stream.string_writer.
@@ -11682,6 +11695,99 @@ set_environment_var(Var, Value, IO0, IO) :-

  %---------------------%

+get_environment_var_map(EnvVarMap, !IO) :-
+    get_environment_var_assoc_list([], EnvVarAL, !IO),
+    map.from_assoc_list(EnvVarAL, EnvVarMap).
+
+:- type env_var_assoc_list == assoc_list(string, string).
+
+:- pred get_environment_var_assoc_list(
+    env_var_assoc_list::in, env_var_assoc_list::out,
+    io::di, io::uo) is det.
+
+:- pragma foreign_proc("C",
+    get_environment_var_assoc_list(EnvVarAL0::in, EnvVarAL::out,
+        _IO0::di, _IO::uo),
+    [may_call_mercury, promise_pure, tabled_for_io],
+"
+    MR_Word cur_env = EnvVarAL0;
+    MR_Word next_env;
+    int     i;
+    char    **environ_vars;
+
+    // See the comments about the environ global below for an
+    // explanation of this.
+    #if defined(MR_MAC_OSX)
+        environ_vars = (*_NSGetEnviron());
+    #else
+        environ_vars = environ;
+    #endif
+
+    for (i = 0; environ_vars[i] != NULL; i++) {
+        ML_record_env_var_equal_value(environ_vars[i], cur_env, &next_env);
+        cur_env = next_env;
+    }
+
+    EnvVarAL = cur_env;
+").
+
+:- pragma foreign_proc("Java",
+    get_environment_var_assoc_list(EnvVarAL0::in, EnvVarAL::out,
+        _IO0::di, _IO::uo),
+    [may_call_mercury, promise_pure],
+"
+    EnvVarAL = EnvVarAL0;
+    java.util.Map<String, String> env = java.lang.System.getenv();
+    for (java.util.Map.Entry<String, String> entry : env.entrySet()) {
+        String name = entry.getKey();
+        String value = entry.getValue();
+        EnvVarAL = ML_record_env_var_and_value(name, value, EnvVarAL);
+    }
+").
+
+:- pragma foreign_proc("C#",
+    get_environment_var_assoc_list(EnvVarAL0::in, EnvVarAL::out,
+        _IO0::di, _IO::uo),
+    [may_call_mercury, promise_pure],
+"
+    EnvVarAL = EnvVarAL0;
+    System.Collections.IDictionary env =
+        System.Environment.GetEnvironmentVariables();
+    foreach (System.Collections.DictionaryEntry entry in env) {
+        string name = (string) entry.Key;
+        string value = (string) entry.Value;
+        EnvVarAL = ML_record_env_var_and_value(name, value, EnvVarAL);
+    }
+").
+
+:- pred record_env_var_equal_value(string::in,
+    env_var_assoc_list::in, env_var_assoc_list::out) is det.
+:- pragma foreign_export("C", record_env_var_equal_value(in, in, out),
+    "ML_record_env_var_equal_value").
+
+record_env_var_equal_value(EnvVarNameEqValue, !EnvVarAL) :-
+    ( if
+        sub_string_search(EnvVarNameEqValue, "=", IndexOfEq),
+        string.split(EnvVarNameEqValue, IndexOfEq, EnvVarName, EqEnvVarValue),
+        string.first_char(EqEnvVarValue, _Eq, EnvVarValue)
+    then
+        !:EnvVarAL = [EnvVarName - EnvVarValue | !.EnvVarAL]
+    else
+        unexpected($pred, "No = in environment entry")
+    ).
+
+:- pred record_env_var_and_value(string::in, string::in,
+    env_var_assoc_list::in, env_var_assoc_list::out) is det.
+:- pragma foreign_export("C#", record_env_var_and_value(in, in, in, out),
+    "ML_record_env_var_and_value").
+:- pragma foreign_export("Java", record_env_var_and_value(in, in, in, out),
+    "ML_record_env_var_and_value").
+
+record_env_var_and_value(EnvVarName, EnvVarValue, !EnvVarAL) :-
+    !:EnvVarAL = [EnvVarName - EnvVarValue | !.EnvVarAL].
+
+%---------------------%
+
  :- pragma foreign_decl("C", "
  #include <stdlib.h> // for getenv() and setenv()
  ").


More information about the reviews mailing list