xref: /AOO42X/main/solenv/bin/langwrap (revision df6a13dbd7c4cc4d919e81cf98635de82158a3ba)
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
25use Getopt::Std;
26
27###### globals ######
28
29$is_debug = 0;
30$nfield   = 0;
31@LoL      = ();
32@command  = ();
33
34###### main ######
35
36# Version
37$idStr = ' $Revision: 1.2 $ ';
38$idStr =~ /Revision:\s+(\S+)\s+\$/
39    ? ($langwrapRev = $1) : ($langwrapRev = "-");
40
41print "langwrap -- Version: $langwrapRev\n";
42
43# Options
44&check_options();
45
46# parse command file
47&parse_commandfile($opt_c);
48
49# create list with command lines
50&create_commands();
51
52# finally execute commands
53foreach $cmd (@command) {
54    if ($is_debug) {
55    print $cmd . "\n";
56    } else {
57    system($cmd);
58    $res = $? >> 8;
59    if ($res) {
60        print "langwrap: command execution failed with exitcode $res.\n";
61        exit($res);
62    }
63    }
64}
65
66exit(0);
67
68###### routines ######
69
70### parse_commandfile()
71sub parse_commandfile {
72    my($file) = shift;
73    my(@field);
74
75    open(COMMAND, "<$file") or die "can�t open $file";
76
77    while (<COMMAND>) {
78    $line = $_;
79    chomp($line);
80    if ( ($line =~ //) || ($line =~ /^\r/) || ($line =~ /^#/) ) {
81        next;
82    }
83
84    @field = split " ", $line;
85    push @LoL, [@field];
86    if (!$nfield) {
87        $nfield = $#field + 1;
88    } else {
89        if ( $nfield != ($#field + 1) ) {
90        print "langwrap: error in <cmdfile>: every row must ";
91        print "have the same # of columns.\n";
92        exit(3);
93        }
94    }
95    }
96
97    close(COMMAND);
98}
99
100### create_command()
101sub create_commands() {
102    my($cmd, $cmdline, $arg_string, $ntempl);
103
104    $cmd = shift @ARGV;
105    $arg_string = join(" ", @ARGV);
106    # just count the number of templates
107    $ntempl = ($arg_string =~ s/@\d+@/$&/eg);
108    if ( $ntempl >= $nfield ) {
109    print "lnagwrap: # of templates > # of fields in <cmdfile>.\n";
110    exit(4);
111    }
112
113    # create command lines
114    for $i (0..$#LoL) {
115    $cmdline = $arg_string;
116    $cmdline =~ s/@(\d+)@/$LoL[$i][$1]/eg;
117    push @command, $cmd . " " . $cmdline;
118    }
119}
120
121### check_options()
122sub check_options {
123
124    if ( !getopts('c:') ) {
125    &usage();
126    }
127
128    if ( !$opt_c ) {
129    &usage();
130    }
131
132    if ( ! -r $opt_c ) {
133    print "langwrap: $opt_c is not a readable file.\n";
134    exit(2);
135    }
136
137    if ( $#ARGV < 1 ) {
138    print "langwrap: empty <template_string>.\n";
139    &usage();
140    }
141}
142
143### usage()
144sub usage {
145    print "Usage: langwrap -c cmdfile tool <template_string>\n";
146    print "<template_string> is of form: ...\@1\@ .... \@2\@...\n";
147    print "with \@<n>\@ template #n\n";
148    exit(1);
149}
150