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