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