summaryrefslogtreecommitdiff
path: root/solenv/bin/langwrap
blob: 97a50f1762ee219ae84542d7dd9886c949909e81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/perl -w
#
# langwrap - language wrapper for building resources
#
# $Id: langwrap,v 1.2 2008-08-18 13:10:41 vg Exp $

use Getopt::Std;

###### globals ######

$is_debug = 0;
$nfield   = 0;
@LoL 	  = ();
@command  = ();

###### main ######

# Version
$idStr = ' $Revision: 1.2 $ ';
$idStr =~ /Revision:\s+(\S+)\s+\$/
    ? ($langwrapRev = $1) : ($langwrapRev = "-");

print "langwrap -- Version: $langwrapRev\n";

# Options
&check_options();

# parse command file
&parse_commandfile($opt_c);

# create list with command lines
&create_commands();

# finally execute commands
foreach $cmd (@command) {
    if ($is_debug) {
	print $cmd . "\n";
    } else {
	system($cmd);
	$res = $? >> 8;
	if ($res) {
	    print "langwrap: command execution failed with exitcode $res.\n";
	    exit($res);
	}
    }
}

exit(0);
 
###### routines ######

### parse_commandfile()
sub parse_commandfile {
    my($file) = shift;
    my(@field);
    
    open(COMMAND, "<$file") or die "can´t open $file";

    while (<COMMAND>) {
	$line = $_;
	chomp($line);
	if ( ($line =~ //) || ($line =~ /^\r/) || ($line =~ /^#/) ) {
	    next;
	}
						   
	@field = split " ", $line;
	push @LoL, [@field];
	if (!$nfield) {
	    $nfield = $#field + 1;
	} else {
	    if ( $nfield != ($#field + 1) ) {
		print "langwrap: error in <cmdfile>: every row must ";
		print "have the same # of columns.\n";
		exit(3);
	    }
	}
    }
    
    close(COMMAND);
}

### create_command()
sub create_commands() {
    my($cmd, $cmdline, $arg_string, $ntempl);

    $cmd = shift @ARGV;
    $arg_string = join(" ", @ARGV);
    # just count the number of templates
    $ntempl = ($arg_string =~ s/@\d+@/$&/eg);
    if ( $ntempl >= $nfield ) {
	print "lnagwrap: # of templates > # of fields in <cmdfile>.\n";
	exit(4);
    }

    # create command lines
    for $i (0..$#LoL) {
	$cmdline = $arg_string;
	$cmdline =~ s/@(\d+)@/$LoL[$i][$1]/eg;
	push @command, $cmd . " " . $cmdline;
    }
}
    
### check_options()
sub check_options {

    if ( !getopts('c:') ) {
	&usage();
    }

    if ( !$opt_c ) {
	&usage();
    }

    if ( ! -r $opt_c ) {
	print "langwrap: $opt_c is not a readable file.\n";
	exit(2);
    }

    if ( $#ARGV < 1 ) {
	print "langwrap: empty <template_string>.\n";
	&usage();
    }
}

### usage()
sub usage {
    print "Usage: langwrap -c cmdfile tool <template_string>\n";
    print "<template_string> is of form: ...\@1\@ .... \@2\@...\n";
    print "with \@<n>\@ template #n\n";
    exit(1);
}