SHOW: Implement SHOW ENVIRONMENT.
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 7 Mar 2023 05:16:37 +0000 (21:16 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 7 Mar 2023 05:17:38 +0000 (21:17 -0800)
Requested by knassen@chartermi.net.

NEWS
src/language/commands/set.c
tests/language/commands/show.at

diff --git a/NEWS b/NEWS
index 79c0f000bf738fbaccdb01ba435bc655e9b990b1..9abe5bc18bd7b0fb28ec6a1e12da4527d2c86039 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -19,6 +19,8 @@ Changes after 1.6.2:
 
  * DISPLAY MACROS is now implemented.
 
+ * SHOW ENVIRONMENT is now implemented.
+
  * Removed the MODIFY VARS command, which is not in SPSS.
 
  * Building from a Git repository, which previously required GIMP, now
index 905b489f79afe8cc314e48cbe277c4abc3ec9aa6..7998850127eff11c6a42bb1f74dbf6c2b30baeb2 100644 (file)
@@ -45,6 +45,7 @@
 #include "libpspp/i18n.h"
 #include "libpspp/integer-format.h"
 #include "libpspp/message.h"
+#include "libpspp/string-array.h"
 #include "math/random.h"
 #include "output/driver.h"
 #include "output/journal.h"
@@ -1369,6 +1370,33 @@ show_all_cc (const struct dataset *ds, struct pivot_table **ptp)
     }
 }
 
+static void
+show_environment (void)
+{
+  struct pivot_table *pt = pivot_table_create (N_("Environment Variables"));
+  pivot_dimension_create (pt, PIVOT_AXIS_ROW, N_("Variable"));
+
+  struct string_array sa = STRING_ARRAY_INITIALIZER;
+  for (char **env = environ; *env; env++)
+    string_array_append (&sa, *env);
+  string_array_sort (&sa);
+
+  for (size_t i = 0; i < sa.n; i++)
+    {
+      struct substring value = ss_cstr (sa.strings[i]);
+      struct substring variable;
+      ss_get_until (&value, '=', &variable);
+
+      char *variable_s = ss_xstrdup (variable);
+      char *value_s = ss_xstrdup (value);
+      add_row (pt, variable_s, value_s);
+      free (variable_s);
+      free (value_s);
+    }
+  string_array_destroy (&sa);
+  pivot_table_submit (pt);
+}
+
 int
 cmd_show (struct lexer *lexer, struct dataset *ds)
 {
@@ -1392,6 +1420,8 @@ cmd_show (struct lexer *lexer, struct dataset *ds)
         show_copying (ds);
       else if (lex_match_id (lexer, "SYSTEM"))
         show_system (ds);
+      else if (lex_match_id (lexer, "ENVIRONMENT"))
+        show_environment ();
       else if (lex_match_id (lexer, "TITLE"))
         {
           struct setting s = { .name = "TITLE", .show = show_TITLE };
index 02c8ee1f94f3759173fdd86f9cdb9f9e4e45b19b..afaf86174fd48495588db5f21761c61711420bc2 100644 (file)
@@ -17,7 +17,6 @@ dnl
 AT_BANNER([SHOW])
 
 AT_SETUP([SHOW N])
-
 AT_DATA([show.sps], [dnl
 DATA LIST LIST NOTABLE /x.
 BEGIN DATA.
@@ -28,25 +27,26 @@ END DATA.
 
 SHOW N.
 ])
-
 AT_CHECK([pspp -O format=csv show.sps], [0], [dnl
 Table: Settings
 N,3
 ])
-
 AT_CLEANUP
 
 
 AT_SETUP([SHOW N empty])
-
 AT_DATA([shown-empty.sps], [dnl
 SHOW N.
 ])
-
 AT_CHECK([pspp -O format=csv shown-empty.sps], [0], [dnl
 Table: Settings
 N,Unknown
 ])
-
 AT_CLEANUP
 
+AT_SETUP([SHOW ENVIRONMENT])
+AT_DATA([show.sps], [dnl
+SHOW ENVIRONMENT.
+])
+AT_CHECK([pspp -O format=csv show.sps], [0], [ignore])
+AT_CLEANUP
\ No newline at end of file