1#!/usr/bin/perl -w 2# 3# langwrap - language wrapper for building resources 4# 5# $Id: langwrap,v 1.2 2008-08-18 13:10:41 vg Exp $ 6 7use Getopt::Std; 8 9###### globals ###### 10 11$is_debug = 0; 12$nfield = 0; 13@LoL = (); 14@command = (); 15 16###### main ###### 17 18# Version 19$idStr = ' $Revision: 1.2 $ '; 20$idStr =~ /Revision:\s+(\S+)\s+\$/ 21 ? ($langwrapRev = $1) : ($langwrapRev = "-"); 22 23print "langwrap -- Version: $langwrapRev\n"; 24 25# Options 26&check_options(); 27 28# parse command file 29&parse_commandfile($opt_c); 30 31# create list with command lines 32&create_commands(); 33 34# finally execute commands 35foreach $cmd (@command) { 36 if ($is_debug) { 37 print $cmd . "\n"; 38 } else { 39 system($cmd); 40 $res = $? >> 8; 41 if ($res) { 42 print "langwrap: command execution failed with exitcode $res.\n"; 43 exit($res); 44 } 45 } 46} 47 48exit(0); 49 50###### routines ###### 51 52### parse_commandfile() 53sub parse_commandfile { 54 my($file) = shift; 55 my(@field); 56 57 open(COMMAND, "<$file") or die "can�t open $file"; 58 59 while (<COMMAND>) { 60 $line = $_; 61 chomp($line); 62 if ( ($line =~ //) || ($line =~ /^\r/) || ($line =~ /^#/) ) { 63 next; 64 } 65 66 @field = split " ", $line; 67 push @LoL, [@field]; 68 if (!$nfield) { 69 $nfield = $#field + 1; 70 } else { 71 if ( $nfield != ($#field + 1) ) { 72 print "langwrap: error in <cmdfile>: every row must "; 73 print "have the same # of columns.\n"; 74 exit(3); 75 } 76 } 77 } 78 79 close(COMMAND); 80} 81 82### create_command() 83sub create_commands() { 84 my($cmd, $cmdline, $arg_string, $ntempl); 85 86 $cmd = shift @ARGV; 87 $arg_string = join(" ", @ARGV); 88 # just count the number of templates 89 $ntempl = ($arg_string =~ s/@\d+@/$&/eg); 90 if ( $ntempl >= $nfield ) { 91 print "lnagwrap: # of templates > # of fields in <cmdfile>.\n"; 92 exit(4); 93 } 94 95 # create command lines 96 for $i (0..$#LoL) { 97 $cmdline = $arg_string; 98 $cmdline =~ s/@(\d+)@/$LoL[$i][$1]/eg; 99 push @command, $cmd . " " . $cmdline; 100 } 101} 102 103### check_options() 104sub check_options { 105 106 if ( !getopts('c:') ) { 107 &usage(); 108 } 109 110 if ( !$opt_c ) { 111 &usage(); 112 } 113 114 if ( ! -r $opt_c ) { 115 print "langwrap: $opt_c is not a readable file.\n"; 116 exit(2); 117 } 118 119 if ( $#ARGV < 1 ) { 120 print "langwrap: empty <template_string>.\n"; 121 &usage(); 122 } 123} 124 125### usage() 126sub usage { 127 print "Usage: langwrap -c cmdfile tool <template_string>\n"; 128 print "<template_string> is of form: ...\@1\@ .... \@2\@...\n"; 129 print "with \@<n>\@ template #n\n"; 130 exit(1); 131} 132