#!/usr/sbin/perl -i

#
# prep - Ada source code preprocessor
#
# Usage:
#
#    prep {+|-flag...} [filename...]
#
# Summary:
#
#    prep moves preprocessor comments of the form --(flag) to the left
#    or right of a line of Ada source.  If one or more files are specified
#    on the command line, they will be processed in-place.  If no files
#    are specified, the command acts as a filter between standard input and
#    standard output.
#

# Example:
#
#    Consider the following lines of Ada source:
#	
#	with Interfaces.C.Strings;--(gnat)
#	with c_strings;--(vads);
#	package universal_strings is
#	
#	   procedure Foo;
#	
#	   pragma Interface (C, Foo);--(vads)
#	   pragma Interface_Name (Foo, "_foo");--(vads)
#	
#	   pragma Import (C, Foo, "foo", "_foo);--(gnat)
#	
#	   --...
#
#  If processed with the command "prep -vads +gnat" the result will be:
#	
#	with Interfaces.C.Strings;--(gnat)
#	--(vads)with c_strings;;
#	package universal_strings is
#	
#	   procedure Foo;
#	
#	--(vads)   pragma Interface (C, Foo);
#	--(vads)   pragma Interface_Name (Foo, "_foo");
#	
#	   pragma Import (C, Foo, "foo", "_foo);--(gnat)
#	
#	   --...

#
# Author:
#
#    Tom Quiggle,
#    Silicon Graphics
#
# Version: 1.0
#
# Modification Log:
#
#    06Oct95    initial revision
#
# Report bugs/enhancements/suggestions to:
#
#    quiggle@sgi.com
#

while (@ARGV && ($_ = $ARGV[0]) =~ /^[+-](\w*)/) {

   @rights = (@rights, $1) if /^\+/;
   @lefts  = (@lefts,  $1) if /^\-/;
   shift;

}

while (<>) {

   chop;

   foreach $left (@lefts) {
     if ( /$left/ ) {
       $_ =~ s/--\($left\)//;
       $_ = "--($left)" . $_;
     }
   }

   foreach $right (@rights) {
     if ( /$right/ ) {
       $_ =~ s/--\($right\)//;
       $_ = $_ . "--($right)";
     }
   }

   print "$_\n";

}
