From 86433c71d23ce2ee33ec45258d5e79cf736219ad Mon Sep 17 00:00:00 2001
From: scottmk <scottmk@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Date: Mon, 13 Sep 2010 12:35:59 +0000
Subject: [PATCH] Add a Perl script for extracting permission names from the
 output of the dump_idl utility (and hence from an IDL file).

AM   Open-ILS/examples/list_perms.pl


git-svn-id: svn://svn.open-ils.org/ILS/trunk@17625 dcc99617-32d9-48b4-a31d-7c20da2025e4
---
 Open-ILS/examples/list_perms.pl | 59 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100755 Open-ILS/examples/list_perms.pl

diff --git a/Open-ILS/examples/list_perms.pl b/Open-ILS/examples/list_perms.pl
new file mode 100755
index 0000000000..40e801fc40
--- /dev/null
+++ b/Open-ILS/examples/list_perms.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl -w
+
+# Extract permission names from the output of the dump_idl utility.
+
+# The logic necessarily makes assumptions about the format of
+# dump_idl's output.  If that format changes, the logic may no longer work.
+
+# In the output: each permission name appears on a separate line, 
+# flush left.  Normally the output should be piped into sort -u in
+# order to eliminate duplicates.
+
+use strict;
+
+my $in_perms = 0;
+my $perm_indent;
+
+# Return the number of leading white space characters.
+# We do not distinguish between tabs and spaces.
+sub indent_level {
+	my $str = shift;
+	return 0 unless (defined( $str ));
+	$str =~ s/\S.*//;       # Remove everything after the leading white space
+	return length $str;     # Take the length of what's left
+}
+
+while(<>) {
+	if( $in_perms ) {
+		
+		# Check the indentation to see if we're still
+		# inside the list of permissions.
+
+		if ( indent_level( $_ ) > $perm_indent ) {
+
+			# This line contains a permission name.
+			# Strip off the leading white space and write it.
+
+			s/^\s*//;
+			print;
+		} else {
+
+			# We're no longer inside the list of permissions.
+
+			$in_perms = 0;
+		}
+	} elsif (/\s+permission [(]string array[)]$/) {
+
+		# We are entering a list of permissions, each of which is
+		# indented further than this line.  When we see a line that
+		# is *not* further indented, that will end the list.
+
+		# The indentation is defined as the number of leading white
+		# space characters, be they tabs or spaces.  If the format of
+		# the dump_idl output is changed to involve some bizarre and
+		# perverse mixture of tabs and spaces, this logic may not work.
+
+		$in_perms = 1;
+		$perm_indent = indent_level( $_ );
+	}
+}
-- 
2.11.0