[X2Go-Commits] [x2goserver] 09/30: x2goserver/lib/x2goupdateoptionsstring: add function to transform the intermediate options string representation back into a string and some code that uses this new function.

git-admin at x2go.org git-admin at x2go.org
Thu Dec 13 11:22:47 CET 2018


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch master
in repository x2goserver.

commit 538bb7b66eb60e2e528be97364e44656548b9d8f
Author: Mihai Moldovan <ionic at ionic.de>
Date:   Fri Dec 7 10:56:20 2018 +0100

    x2goserver/lib/x2goupdateoptionsstring: add function to transform the intermediate options string representation back into a string and some code that uses this new function.
---
 debian/changelog                       |   3 +
 x2goserver/lib/x2goupdateoptionsstring | 117 ++++++++++++++++++++++++++++++++-
 2 files changed, 119 insertions(+), 1 deletion(-)

diff --git a/debian/changelog b/debian/changelog
index c1567f5..ffd4e5d 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -24,6 +24,9 @@ x2goserver (4.1.0.4-0x2go1) UNRELEASED; urgency=medium
     - x2goserver/lib: add new (stub) file x2goupdateoptionsstring to deal with
       options string manipulations. Currently only parsing into an
       intermediate state is supported, the script dumps that state for now.
+    - x2goserver/lib/x2goupdateoptionsstring: add function to transform the
+      intermediate options string representation back into a string and some
+      code that uses this new function.
   * debian/control:
     + Build-depend upon lsb-release for distro version detection.
   * debian/x2goserver.manpages:
diff --git a/x2goserver/lib/x2goupdateoptionsstring b/x2goserver/lib/x2goupdateoptionsstring
index f710339..d0f2ee2 100755
--- a/x2goserver/lib/x2goupdateoptionsstring
+++ b/x2goserver/lib/x2goupdateoptionsstring
@@ -174,6 +174,117 @@ sub parse_options {
   return $ret;
 }
 
+# Takes an intermediate options string representation array reference(!) and
+# returns a string.
+# This is essentially the opposite of parse_options.
+# Parsing an options string and passing the result through this function again
+# SHOULD (if initial options string has been a valid one to begin with) yield
+# the initial options string again.
+# On error, returns undef.
+sub intermediate_to_string {
+  my $ret = undef;
+  my $error_detected = 0;
+
+  my $options = shift;
+
+  if ('ARRAY' ne ref ($options)) {
+    print STDERR "Invalid options reference type passed (" . ref ($options) . "), returning undef.\n";
+    $error_detected = 1;
+  }
+
+  if (!($error_detected)) {
+    if (1 == scalar (@$options)) {
+      foreach my $entry (@$options) {
+        if (!defined ($entry)) {
+          print STDERR "Invalid options array passed, returning undef.\n";
+          $error_detected = 1;
+        }
+      }
+    }
+  }
+
+  if (!($error_detected)) {
+    # Last entry should contain the display port part only.
+    # We can detect it through counting.
+    my $elements_left = @$options;
+
+    # Handle entries iteratively, merging then into one string.
+    foreach my $entry (@$options) {
+      --$elements_left;
+
+      if (!defined ($entry)) {
+        print STDERR "Invalid options entry encountered, returning undef.\n";
+        $error_detected = 1;
+        last;
+      }
+
+      if ('HASH' ne ref ($entry)) {
+        print STDERR "Entry in array has invalid type (" . ref ($entry) ."), returning undef.\n";
+        $error_detected = 1;
+        last;
+      }
+
+      if (1 < scalar (keys (%$entry))) {
+        print STDERR "More than one entry encountered in hash of current element, returning undef.\n";
+        $error_detected = 1;
+        last;
+      }
+
+      # Must be either empty or have one element, so... go for it.
+      if (0 == scalar (keys (%$entry))) {
+        if (0 != $elements_left) {
+          if (defined ($ret)) {
+            $ret .= ',';
+          }
+          else {
+            # Mark first entry as empty. Don't remove this, or else.
+            $ret = '';
+          }
+        }
+        else {
+          # Special handling for last element, which is always supposed to
+          # contain the display port.
+          print STDERR "No entry found in display port hash, returning undef.\n";
+          $error_detected = 1;
+          last;
+        }
+      }
+      else {
+        # This foreach loop may look weird because, at that point, we know that
+        # the hash contains one key exactly, but it's still an elegant way to
+        # fetch the key and pseudo-iterate over it.
+        foreach my $key (keys (%$entry)) {
+          my $tmp = $key;
+
+          if (0 != $elements_left) {
+            if (defined ($entry->{$key})) {
+              $tmp .= '=' . $entry->{$key};
+            }
+          }
+
+          if (defined ($ret)) {
+            if (0 != $elements_left) {
+              $ret = join (',', ($ret, $tmp));
+            }
+            else {
+              $ret .= $tmp;
+            }
+          }
+          else {
+            $ret = $tmp;
+          }
+        }
+      }
+    }
+  }
+
+  if ($error_detected) {
+    $ret = undef;
+  }
+
+  return $ret;
+}
+
 Getopt::Long::Configure("gnu_getopt", "no_auto_abbrev");
 
 my $help = 0;
@@ -184,7 +295,11 @@ pod2usage(-verbose => 2, -exitval => 0) if $man;
 
 my $options = shift;
 
-print STDERR Dumper (parse_options ($options)) . "\n";
+my $intermediate = parse_options ($options);
+
+print STDERR Dumper ($intermediate) . "\n";
+
+print STDERR Dumper (intermediate_to_string ($intermediate)) . "\n";
 
 #my $value = shift;
 #my $allow_negative = shift;

--
Alioth's /home/x2go-admin/maintenancescripts/git/hooks/post-receive-email on /srv/git/code.x2go.org/x2goserver.git


More information about the x2go-commits mailing list