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