Index: trunk/gsdl/bin/script/expand_macros.pl
===================================================================
--- trunk/gsdl/bin/script/expand_macros.pl	(revision 11104)
+++ trunk/gsdl/bin/script/expand_macros.pl	(revision 11121)
@@ -3,7 +3,7 @@
 ##################################################################################
 #                                                                                #
-# expand_macros.pl -- recursively expand greenstone macros / 1080805 - 1050905   #
+# expand_macros.pl -- recursively expand greenstone macros / 1080805 - 1230106   #
 #                                                                                #
-# Copyright (C) 2005 Jens Wille <j_wille at gmx.net>                             #
+# Copyright (C) 2005,2006 Jens Wille <j_wille at gmx.net>                        #
 #                                                                                #
 # This program is free software; you can redistribute it and/or                  #
@@ -60,6 +60,9 @@
 # - handle macro options better (v, c, l, ...?)
 #
-#
-# KNOWN BUGS:
+# - implement some kind of "persistent" macro db (so that it doesn't need to be
+#   built each and every time)
+#
+#
+# KNOWN ISSUES:
 #
 # - for a sufficiently large file (> 12288 bytes == 12 k) (paged-)read will quit
@@ -74,5 +77,5 @@
 
 use File::Basename        qw(basename dirname);
-use File::Spec::Functions qw(catdir catfile tmpdir);
+use File::Spec::Functions qw(catdir catfile curdir);
 
 use IO::Handle            qw(autoflush);
@@ -85,5 +88,5 @@
 
 my $NAME    = basename $0;
-my $VERSION = '0.21';
+my $VERSION = '0.22';
 
 
@@ -116,5 +119,5 @@
 my $DEFINITION_START    = '\{';
 
-# end of macro definition: closing curly bracket ('{'), not escaped,
+# end of macro definition: closing curly bracket ('}'), not escaped,
 # i.e. not preceded by a backslash ('\')
 # (need to double-escape backslash here!)
@@ -137,5 +140,5 @@
   'paged'       => 0,
   'pager'       => $ENV{'PAGER'} || 'less',
-  'histfile'    => catfile($ENV{'GSDLTMPDIR'} || tmpdir, 'expand_macros.hist'),
+  'histfile'    => catfile($ENV{'HOME'} || curdir, '.expand_macros.history'),
   'histsize'    => 100,
   'macro_dirs'  => [catdir($ENV{'GSDLHOME'}, 'macros')],
@@ -195,4 +198,19 @@
 
     (you can restrict your macro name query to a certain package by prepending the macro name with '<package-name>:')
+
+    EXAMPLES:
+
+      get definition of the 'pagescriptextra' macro
+      > $NAME _pagescriptextra_
+
+      get definition of the 'pagescriptextra' macro, package 'query' only
+      > $NAME _query:pagescriptextra_
+
+      get definition of the 'pagescriptextra' macro, package 'query' only -- and recursively get definitions of
+      macros used within that definition (up to level 2)
+      > $NAME -d 2 _query:pagescriptextra_
+
+      get all the macros that use the 'pagescriptextra' macro (names only)
+      > $NAME -r -s _pagescriptextra_
 
 
@@ -217,5 +235,5 @@
 HERE_HELP
 
-my $INTERACTIVE_HELP = <<HERE_HELP;
+my $HELP_INTERACTIVE = <<HERE_HELP;
 $NAME: expand greenstone macros in ***interactive browse mode*** (v$VERSION)
 
@@ -224,5 +242,5 @@
 
     - commands are equal to command line options, except that they start with a dot '.'
-      (NOTE: not all command line options are available as command, see list below)
+      (NOTE: not all command line options are available as commands, see list below)
 
     - commands that take an optional argument ([...]) will print their current value if
@@ -283,10 +301,22 @@
 
 
-### some sanity checks
+### some sanity checks (dunno which one to check first ;-)
 
 # need one of our "actions": batch, query or interactive
 # ("batch" requiring at least one macro name or regexp specified)
-die "$USAGE\n"
-  unless @{$ARG{'args'}} || $ARG{'interactive'};
+unless (@{$ARG{'args'}} || $ARG{'interactive'}) {
+  warn "$USAGE";
+
+  warn <<HERE_WARN unless $ENV{'GSDLHOME'};
+
+GSDLHOME not set!
+
+for this script to run your greenstone environment needs to be set up. please
+change into the directory where greenstone has been installed and run/source
+the appropriate setup script.
+HERE_WARN
+
+  die "\n";
+}
 
 # need GSDLHOME for default macro directory
@@ -295,4 +325,9 @@
   unless $ENV{'GSDLHOME'};
 
+## need one of our "actions": batch, query or interactive
+# ("batch" requiring at least one macro name or regexp specified)
+#die "$USAGE\n"
+#  unless @{$ARG{'args'}} || $ARG{'interactive'};
+
 
 ### action!
@@ -327,7 +362,6 @@
 
         # now recurse those macros
-        foreach my $m (@refs) {
-          get_macro($m);
-        }
+        get_macro($_)
+          foreach @refs;
       }
     }
@@ -343,7 +377,6 @@
 
       # now print those macros
-      foreach my $m (@macros) {
-        get_macro($m);
-      }
+      get_macro($_)
+        foreach @macros;
     }
 
@@ -360,6 +393,5 @@
   $SIG{'PIPE'} = 'IGNORE';
 
-  # get the pager executable
-  # no need to test if not in "paged" mode
+  # get the pager executable (no need to test if not in "paged" mode)
   get_pager()
     if $ARG{'paged'};
@@ -459,6 +491,6 @@
       next unless length $line;      # skip empty lines
       next if     $line =~ /^\s*$/;  # skip "empty" lines
-      next if     $line =~ /^\s*#/;  # skip comments (i hope this doesn't
-                                     # affect cases we actually wanted to keep)
+      next if     $line =~ /^\s*#/;  # skip comments (hope this doesn't affect
+                                     # cases we actually wanted to keep)
 
       if    ($line =~ /^$PACKAGE_DECLARATION\s*($PACKAGE_NAME)/) {
@@ -476,7 +508,4 @@
         # don't include unnecessary version, unless we're interactive (where version may change during session)
         next if $ARG{'version'} ne '-1' && $version ne $ARG{'version'} && ! $ARG{'interactive'};
-
-        # is the macro definition already finished?
-        $contd = ($content =~ s/\s*$DEFINITION_END.*//) ? 0 : 1;
 
         if (exists $macro_db{$name}->{$curpkg}->{$version}) {
@@ -496,13 +525,16 @@
         $macro_db{$name}->{$curpkg}->{$version}->{'line'}    = $.;
         $macro_db{$name}->{$curpkg}->{$version}->{'content'} = [$content] if length $content;
+
+        # is the macro definition already finished?
+        $contd = ($content =~ s/\s*$DEFINITION_END.*//) ? 0 : 1;
       }
       elsif ($contd) {
         # continuation of macro definition
 
+        # store additional content
+        push(@{$macro_db{$name}->{$curpkg}->{$version}->{'content'}} => $line);
+
         # is the macro definition already finished?
         $contd = ($line =~ s/\s*$DEFINITION_END.*//) ? 0 : 1;
-
-        # store additional content
-        push(@{$macro_db{$name}->{$curpkg}->{$version}->{'content'}} => $line);
       }
       else {
@@ -675,5 +707,5 @@
                            # we want to skip some macros that have no content anyway (defined
                            # from within the server) - unless we're doing a "reverse" search
-    next if $seen{$m}++ || ($m =~ /^(cgiarg.*|if|httpimg|gwcgi|(decoded)?compressedoptions)$/i
+    next if $seen{$m}++ || ($m =~ /^(cgiarg.*|histvalue\d+|if|httpimg|gwcgi|(decoded)?compressedoptions)$/i
                             && ! $ARG{'reverse'});
 
@@ -702,8 +734,9 @@
 # usage:
 #   @macros = get_r_macros($macro)
-#   @macros = get_r_macros($query)
+#   @macros = get_r_macros($query, $is_query)
 #
 #   macro:    macro name
 #   query:    query string (regular expression)
+#   is_query: boolean value to indicate whether arg is a query or a macro
 #
 #   => macros: list of macros
@@ -947,9 +980,9 @@
 #
 # usage:
-#   $line = prompt([$prompt])
+#   $reply = prompt([$prompt])
 #
 #   prompt:  optional prompt (default: '> ')
 #
-#   => line: user input
+#   => reply: user input
 #
 sub prompt {
@@ -958,19 +991,19 @@
 
   # read user input
-  my $line = $term->readline($prompt);
-
-  if (defined $line) {
+  my $reply = $term->readline($prompt);
+
+  if (defined $reply) {
     # add input to history, unless it's just a number
-    $term->addhistory($line)
-      if $line =~ /[[:alpha:]]/;
-
-    if ($line =~ s/^\s*["']*\s*\././) {
+    $term->addhistory($reply)
+      if $reply =~ /[[:alpha:]]/;
+
+    if ($reply =~ s/^\s*["']*\s*\././) {
       # execute command
-      my $return = parse_command($line);
+      my $return = parse_command($reply);
 
       return $return if defined $return;
     }
     else {
-      return $line;
+      return $reply;
     }
   }
@@ -998,10 +1031,10 @@
   if ($ARG{'paged'}) {
     # pass output to pager
-    open(LESS, "| $ARG{'pager'}")
+    open(PAGER, "| $ARG{'pager'}")
       or die "can't open pipe to '$ARG{'pager'}': $!";
 
-    print LESS "$output";
-
-    close LESS;
+    print PAGER "$output";
+
+    close PAGER;
   }
   else {
@@ -1024,5 +1057,5 @@
 #
 sub wait_for_user {
-  print "[press <enter> to continue]";
+  print "[press key to continue]";
   print "\n" if <STDIN>;
 }
@@ -1053,5 +1086,5 @@
 
     if    ($command =~ /^(h|\?|help)$/) {
-      print "$INTERACTIVE_HELP\n";
+      print "$HELP_INTERACTIVE\n";
 
       # wait for user reaction to continue
