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