lilypond-1.5.5
[lilypond.git] / bin / convert-mudela
blob000f5059cb7a7c84b42f64f562b2349a1f88ac71
1 #!/usr/bin/perl -w
3 =head1 TODO
5 detect \lyrics and \melodic, and do substitution accordingly.
6 count <> and {} ?
8 Ugh . Perl sux. Anybody for Python?
10 =cut
15 # version of "supporting" engine, not mudela conversions.
21 $convert_mudela_version = "0.1.1";
23 use Getopt::Long;
26 sub version_compare
28 local ($a,$b)=@_;
29 return &cmpver;
33 sub cmpver
35 my(@a)= split /\./,$a;
36 my(@b)= split /\./,$b;
38 for $i (0,1,2) {
39 return $a[$i] <=> $b[$i] if ($a[$i] != $b[$i]);
41 return $a cmp $b;
44 sub version_string_conv
46 my ($from_version, $to_version) = @_;
47 s/\version \"$from_version\"/\version \"$to_version\"/g;
50 ################################################################
52 sub no_conv
56 sub convert_0_0_52_to_0_0_53
59 s/include \"/$1\\include \"/g;
63 sub convert_0_0_54_to_0_0_55
65 s/%{/% {/g;
69 sub convert_0_0_53_to_0_0_54
71 print STDERR "Not smart enough to convert \\transpose\n" if (/\\transpose/) ;
74 # we-re not at 58 yet, but this is at least one of the rules
75 sub convert_0_0_55_to_0_0_56
77 s/\"\|\|\"/\"|.\"/g;
80 sub convert_0_0_56_to_0_0_57
82 s/\(([ \]\[|\t-\.>]|\\[<!>a-z]+)*\)/\~ $1/g;
85 sub convert_0_0_57_to_0_0_58
87 s/\[ *([^\[\]]*)\] *([1-9]*) *\/ *([1-9]*)/[$2\/$3 $1]1\/1/g;
90 sub convert_0_0_58_to_0_0_59
92 die "Not smart enough to convert 0.0.58 to 0.0.59\n";
95 sub convert_0_0_59_to_0_0_60
97 s/(\\unitspace [0-9.mcptin\\ ]+|\\geometric [0-9.]+|\\width [0-9.mcp\\tin]+)/$1;/g;
98 s/(\\output \"[^\"]+\")/$1;/;
99 s/(\\tempo [0-9: ]+)/$1;/;
102 sub convert_0_0_60_to_0_0_61
104 s/(\\unitspace|\\geometric|\\width)/$1=/g;
108 ###############################################################
110 sub last_conversion
112 my @v = &versions;
113 return pop @v;
115 sub identify
118 print STDERR "This is convert-mudela " . $convert_mudela_version .
119 " (up to mudela version ", last_conversion, ")\n";
123 sub usage
125 print STDERR "Usage: convert-mudela [options] [mudela-file]...\n"
126 . "Convert old mudela source from mudela-file or stdin\n\n"
127 . "Options:\n"
128 . " -e, --edit perform in-place conversion\n"
129 . " -f, --from=PATHLEVEL use source version 0.0.PATCHLEVEL\n"
130 . " -h, --help print this help\n"
131 . " -o, --output=FILE name output file\n"
132 . " -s, --show-rules print all known conversion rules\n"
133 . " -t, --to=VERSION convert to version VERSION\n"
137 my %minor_conversions = ("0.0.50" => \&no_conv,
138 "0.0.52" => \&convert_0_0_50_to_0_0_52,
139 "0.0.53" => \&convert_0_0_52_to_0_0_53,
140 "0.0.54" => \&convert_0_0_53_to_0_0_54,
141 "0.0.55" => \&convert_0_0_54_to_0_0_55,
142 "0.0.56" => \&convert_0_0_55_to_0_0_56,
143 "0.0.57" => \&convert_0_0_56_to_0_0_57,
144 "0.0.58" => \&convert_0_0_57_to_0_0_58,
145 "0.0.59" => \&convert_0_0_58_to_0_0_59,
146 "0.0.60" => \&convert_0_0_59_to_0_0_60,
147 "0.0.61" => \&convert_0_0_60_to_0_0_61
151 sub versions
153 return (sort keys %minor_conversions);
157 sub show_rules
159 print "Rules: ", join(", ", sort keys %minor_conversions), "\n";
163 sub do_conversion
165 my ($from,$to) = @_;
167 my @applicable_conversion;
168 my @mudela_levels;
170 # die "This is too old to convert " if $from < 50;
171 my @v = versions;
172 foreach $ver (@v) {
173 if (version_compare($ver, $from) > 0 && version_compare($ver,$to) <= 0 ){
174 push @applicable_conversion, $minor_conversions{$ver};
175 push @mudela_levels, $ver;
179 print STDERR "Applying following rules: ", join(", ", @mudela_levels) , "\n";
181 while (<INLY>) {
182 foreach $subroutine (@applicable_conversion) {
184 &$subroutine;
187 version_string_conv $from, $to;
188 print OUTLY;
192 sub get_auto_from
194 my ($fn)=@_;
195 my ($ver);
196 open INLY, $fn || die "Can't open";
198 while (<INLY>) {
199 s/^.*\\version \"([^\"]*)\".*$//;
200 if (defined ($1)) {
201 print STDERR "Guessing version: ", $1, ".. ";
202 $ver = $1;
203 last;
206 if (!defined($ver)){
207 print STDERR "can't determine mudela version in $fn.\n";
208 my $u;
209 return $u;
211 close INLY;
212 return $ver;
215 sub set_files
217 $infile = "-";
218 $outfile = "-";
219 $outfile = $opt_output if (defined($opt_output));
221 if ($ARGV [0]) {
222 $infile = $ARGV[0];
224 if (( ! -f $infile) && (! $infile =~ /\\.ly$/s ) ){
225 $infile .= ".ly";
229 if ($opt_edit && $infile ne "-") {
230 $opt_edit = 1;
231 $outfile = "$infile.NEW";
232 $infile = "$infile";
234 print STDERR "Input ", (($infile eq "-") ?"STDIN" : $infile), " .. ";
238 sub do_one_arg
240 set_files;
242 local ($from_version, $to_version);
243 $from_version = $opt_from;
244 $to_version = $opt_to;
246 ($from_version = get_auto_from $infile) unless defined($opt_from);
247 return if (!defined($from_version));
249 ($to_version = last_conversion) unless (defined($opt_to));
251 die "can't open \`$infile\'" unless open INLY,$infile ;
252 die "can't open \`$outfile\'" unless open OUTLY, ">$outfile";
254 do_conversion $from_version, $to_version;
255 close INLY;
256 close OUTLY;
258 if ($opt_edit) {
259 rename $infile, "$infile~";
260 rename $outfile, "$infile";
264 ## "main"
266 identify;
269 GetOptions ("help", "output=s", "from=i", "to=i", "minor=i", "edit", "show-rules");
271 if ($opt_help) {
272 usage();
273 $opt_help = 0; # to extinguish typo check.
274 exit 0;
277 if ($opt_show_rules) {
278 show_rules ;
279 $opt_show_rules = 0; # to extinguish typo check.
280 exit 0;
283 local ( $infile,$outfile);
284 my $processed_one=0;
286 while (defined($ARGV[0])) {
287 do_one_arg;
288 shift @ARGV;
289 $processed_one = 1;
291 do_one_arg unless ($processed_one);