summaryrefslogtreecommitdiff
path: root/sim/src/tests/scripts/mmpsubstitute
blob: 65661efe2d522b401db7bd0d7a42600ff358c67f (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
#!/usr/bin/perl

# usage:
#
# mmpsubstitute value < template.mmp > result.mmp

# Reads a template file from stdin, and adds value wherever $$ appears
# in the template, writing the result to stdout.  If value is 10, for
# example, and a fragment of the template read: (123 $$45 678) the
# resulting output would be: (123 55 678) where 10 was added to the
# number 45 which followed the $$ substitution marker.  If $$ is not
# followed immediately by a string of digits, value just replaces the
# $$.

$value = $ARGV[0];

while (<STDIN>) {
    while (/\$\$/) {
	$pre = $`;
	printf STDOUT "%s", $pre;
	$post = $'; #' <-- to make emacs happy
	if ($post =~ /^-?\d+/) {
	    $num = $&;
	    $post = $'; #' <-- to make emacs happy
	    printf STDOUT "%d", $num + $value;
	} else {
	    printf STDOUT "%d", $value;
	}
	$_ = $post;
    }
    printf STDOUT "%s", $_;
}