1*91e872dfSPedro Giffuni#!/usr/bin/env perl -w 2b31e36b3SAndrew Rist# ************************************************************* 3b31e36b3SAndrew Rist# 4b31e36b3SAndrew Rist# Licensed to the Apache Software Foundation (ASF) under one 5b31e36b3SAndrew Rist# or more contributor license agreements. See the NOTICE file 6b31e36b3SAndrew Rist# distributed with this work for additional information 7b31e36b3SAndrew Rist# regarding copyright ownership. The ASF licenses this file 8b31e36b3SAndrew Rist# to you under the Apache License, Version 2.0 (the 9b31e36b3SAndrew Rist# "License"); you may not use this file except in compliance 10b31e36b3SAndrew Rist# with the License. You may obtain a copy of the License at 11b31e36b3SAndrew Rist# 12b31e36b3SAndrew Rist# http://www.apache.org/licenses/LICENSE-2.0 13b31e36b3SAndrew Rist# 14b31e36b3SAndrew Rist# Unless required by applicable law or agreed to in writing, 15b31e36b3SAndrew Rist# software distributed under the License is distributed on an 16b31e36b3SAndrew Rist# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17b31e36b3SAndrew Rist# KIND, either express or implied. See the License for the 18b31e36b3SAndrew Rist# specific language governing permissions and limitations 19b31e36b3SAndrew Rist# under the License. 20b31e36b3SAndrew Rist# 21b31e36b3SAndrew Rist# ************************************************************* 22cdf0e10cSrcweir# 23cdf0e10cSrcweir# langwrap - language wrapper for building resources 24cdf0e10cSrcweir 25cdf0e10cSrcweiruse Getopt::Std; 26cdf0e10cSrcweir 27cdf0e10cSrcweir###### globals ###### 28cdf0e10cSrcweir 29cdf0e10cSrcweir$is_debug = 0; 30cdf0e10cSrcweir$nfield = 0; 31cdf0e10cSrcweir@LoL = (); 32cdf0e10cSrcweir@command = (); 33cdf0e10cSrcweir 34cdf0e10cSrcweir###### main ###### 35cdf0e10cSrcweir 36cdf0e10cSrcweir# Version 37cdf0e10cSrcweir$idStr = ' $Revision: 1.2 $ '; 38cdf0e10cSrcweir$idStr =~ /Revision:\s+(\S+)\s+\$/ 39cdf0e10cSrcweir ? ($langwrapRev = $1) : ($langwrapRev = "-"); 40cdf0e10cSrcweir 41cdf0e10cSrcweirprint "langwrap -- Version: $langwrapRev\n"; 42cdf0e10cSrcweir 43cdf0e10cSrcweir# Options 44cdf0e10cSrcweir&check_options(); 45cdf0e10cSrcweir 46cdf0e10cSrcweir# parse command file 47cdf0e10cSrcweir&parse_commandfile($opt_c); 48cdf0e10cSrcweir 49cdf0e10cSrcweir# create list with command lines 50cdf0e10cSrcweir&create_commands(); 51cdf0e10cSrcweir 52cdf0e10cSrcweir# finally execute commands 53cdf0e10cSrcweirforeach $cmd (@command) { 54cdf0e10cSrcweir if ($is_debug) { 55cdf0e10cSrcweir print $cmd . "\n"; 56cdf0e10cSrcweir } else { 57cdf0e10cSrcweir system($cmd); 58cdf0e10cSrcweir $res = $? >> 8; 59cdf0e10cSrcweir if ($res) { 60cdf0e10cSrcweir print "langwrap: command execution failed with exitcode $res.\n"; 61cdf0e10cSrcweir exit($res); 62cdf0e10cSrcweir } 63cdf0e10cSrcweir } 64cdf0e10cSrcweir} 65cdf0e10cSrcweir 66cdf0e10cSrcweirexit(0); 67cdf0e10cSrcweir 68cdf0e10cSrcweir###### routines ###### 69cdf0e10cSrcweir 70cdf0e10cSrcweir### parse_commandfile() 71cdf0e10cSrcweirsub parse_commandfile { 72cdf0e10cSrcweir my($file) = shift; 73cdf0e10cSrcweir my(@field); 74cdf0e10cSrcweir 75cdf0e10cSrcweir open(COMMAND, "<$file") or die "can�t open $file"; 76cdf0e10cSrcweir 77cdf0e10cSrcweir while (<COMMAND>) { 78cdf0e10cSrcweir $line = $_; 79cdf0e10cSrcweir chomp($line); 80cdf0e10cSrcweir if ( ($line =~ //) || ($line =~ /^\r/) || ($line =~ /^#/) ) { 81cdf0e10cSrcweir next; 82cdf0e10cSrcweir } 83cdf0e10cSrcweir 84cdf0e10cSrcweir @field = split " ", $line; 85cdf0e10cSrcweir push @LoL, [@field]; 86cdf0e10cSrcweir if (!$nfield) { 87cdf0e10cSrcweir $nfield = $#field + 1; 88cdf0e10cSrcweir } else { 89cdf0e10cSrcweir if ( $nfield != ($#field + 1) ) { 90cdf0e10cSrcweir print "langwrap: error in <cmdfile>: every row must "; 91cdf0e10cSrcweir print "have the same # of columns.\n"; 92cdf0e10cSrcweir exit(3); 93cdf0e10cSrcweir } 94cdf0e10cSrcweir } 95cdf0e10cSrcweir } 96cdf0e10cSrcweir 97cdf0e10cSrcweir close(COMMAND); 98cdf0e10cSrcweir} 99cdf0e10cSrcweir 100cdf0e10cSrcweir### create_command() 101cdf0e10cSrcweirsub create_commands() { 102cdf0e10cSrcweir my($cmd, $cmdline, $arg_string, $ntempl); 103cdf0e10cSrcweir 104cdf0e10cSrcweir $cmd = shift @ARGV; 105cdf0e10cSrcweir $arg_string = join(" ", @ARGV); 106cdf0e10cSrcweir # just count the number of templates 107cdf0e10cSrcweir $ntempl = ($arg_string =~ s/@\d+@/$&/eg); 108cdf0e10cSrcweir if ( $ntempl >= $nfield ) { 109cdf0e10cSrcweir print "lnagwrap: # of templates > # of fields in <cmdfile>.\n"; 110cdf0e10cSrcweir exit(4); 111cdf0e10cSrcweir } 112cdf0e10cSrcweir 113cdf0e10cSrcweir # create command lines 114cdf0e10cSrcweir for $i (0..$#LoL) { 115cdf0e10cSrcweir $cmdline = $arg_string; 116cdf0e10cSrcweir $cmdline =~ s/@(\d+)@/$LoL[$i][$1]/eg; 117cdf0e10cSrcweir push @command, $cmd . " " . $cmdline; 118cdf0e10cSrcweir } 119cdf0e10cSrcweir} 120cdf0e10cSrcweir 121cdf0e10cSrcweir### check_options() 122cdf0e10cSrcweirsub check_options { 123cdf0e10cSrcweir 124cdf0e10cSrcweir if ( !getopts('c:') ) { 125cdf0e10cSrcweir &usage(); 126cdf0e10cSrcweir } 127cdf0e10cSrcweir 128cdf0e10cSrcweir if ( !$opt_c ) { 129cdf0e10cSrcweir &usage(); 130cdf0e10cSrcweir } 131cdf0e10cSrcweir 132cdf0e10cSrcweir if ( ! -r $opt_c ) { 133cdf0e10cSrcweir print "langwrap: $opt_c is not a readable file.\n"; 134cdf0e10cSrcweir exit(2); 135cdf0e10cSrcweir } 136cdf0e10cSrcweir 137cdf0e10cSrcweir if ( $#ARGV < 1 ) { 138cdf0e10cSrcweir print "langwrap: empty <template_string>.\n"; 139cdf0e10cSrcweir &usage(); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir} 142cdf0e10cSrcweir 143cdf0e10cSrcweir### usage() 144cdf0e10cSrcweirsub usage { 145cdf0e10cSrcweir print "Usage: langwrap -c cmdfile tool <template_string>\n"; 146cdf0e10cSrcweir print "<template_string> is of form: ...\@1\@ .... \@2\@...\n"; 147cdf0e10cSrcweir print "with \@<n>\@ template #n\n"; 148cdf0e10cSrcweir exit(1); 149cdf0e10cSrcweir} 150