Convert over to use GetOpt::Long and introduce -f and -h.
[fvwm.git] / bin / fvwm-convert-2.6.in
blobf4ff86a6d3d4da037b304bcc5d7e4207391ca350
1 #!@PERL@
2 # -*-perl-*-
4 # Convert .fvwm2rc from 2.4.x format to 2.6.x format.
6 # Original author:  Thomas Adam <thomas.adam22@gmail.com> Dec. 2009
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 use strict;
23 use Cwd;
24 use Getopt::Long;
26 # Global array for all our converted lines.
27 my @converted_lines = ();
29 # Global softref for addtofunc continuations.
30 my $last_func_ref;
31 my %converted_funcs = ();
33 # Global for additional files...
34 my @additional_files = ();
36 # GetOpts
37 my $follow_read = '';
38 my $process_read = 0;
40 # Convert conditional command syntax correctly.
41 sub __convert_conditionals
43     my( $cond ) = @_;
44     my( $line ) = $cond->[-1];
46     $line = "$1 ". join( ', ', split( /\s+/, $2 ) ) . " $3" if $line =~
47     /(all|current|direction|next|none|prev|pick|thiswindow|windowid)\s*(\(.*?\))(.*)/i;
49     $cond->[-1] = $line;
52 # Process the files specified and output them to a destination file.
53 sub process_files
55     my( $files ) = @_;
57     no strict "refs";
58     foreach my $f ( @$files )
59     {
60         my( $s, $d ) = @$f;
62         warn "Following:  Read $s...\n" if $process_read;
64         if( (!defined $d or $d eq '') and
65             ! -e "$s.converted" ) 
66         {
67             $d = "$s.converted";
68         } elsif( -e $d ) {
69             die "Destination file:  $d exists\n";
70         }
72         open( my $in_file, '<', $s ) or die
73             "Unable to open source file:  $!\n";
75         while( <$in_file> )
76         {  
77             chomp;
78             dispatch_line($_);
79         }
80         
81         write_out_file($d);
82         @converted_lines = ();
83         %converted_funcs = ();
84     }
87 # Convert style syntax over where applicable.
88 sub convert_styles
90     my( $line ) = @_;
91     my @converted;
93     # At the very least, we can cheat and just negate everything.  Whilst it
94     # isn't deprecated yet, it will be -- so it won't hurt to do it here.
96     # Split the line out first of all, between the "Style foo" part, and the
97     # actual styles being applied.
98     my( @style_parts ) = ($line =~ /^(style\s+\"??[\w+*?]\"??)(.*)$/i);
100     # Convert the second part over.
101     foreach( split( /\s*,\s*/, $style_parts[1] ) )
102     {
103         # FIXME -- this should be flagged up as bugs in FVWM.
104         s/(?:No)(.*)/\!$1/ unless $_ =~ /nopposition/i;
105         push @converted, $_;
106     }
108     push @converted_lines, $style_parts[0] . join(', ',
109         @converted);
112 # Buckshot approach at turning fvwmthemes into colorsets.  Can't really do
113 # much more than this, but at least gives the user something to go on.
114 sub convert_fvwmtheme
116     my( $line ) = @_;
118     $line =~ s/^\*fvwmtheme\s*:?//i;
119     $line = undef if $line =~ /modulesynchronous.*?fvwmtheme/i;
121     push @converted_lines, $line;
124 # Comment out the modulepath line -- grr.
125 sub handle_modulepath
127     my( $line ) = @_;
128     $line = "# " . $line;
130     push( @converted_lines, $line );
133 # This should have happened in the fvwm-2.4 convert script, but handle it
134 # here anyway.
135 sub convert_windowshadesteps
137     my( $line ) = @_;
138     $line =~ /(\d+)p?/ ? 
139         $line = "Style * WindowShadeSteps $1" : 
140         $line = "Style * " . $line;
142     push( @converted_lines, $line );
145 sub convert_edge_resistance
147     my( $line ) = @_;
149     # This gets converted into two parts.  One is the EdgeResistance
150     # command, the other is a style line.
151     #
152     # We could only ever have had two numbers as arguments to
153     # EdgeResistance.
154     my( $edge_res_arg, $move_res_arg ) = 
155         ( $line =~ /edgeresistance\s*(\d+)\s*(\d+)/i );
157     push( @converted_lines,
158         qq|
159         EdgeResistance $edge_res_arg
160         Style * EdgeMoveResistance $move_res_arg| );
163 sub convert_snapattraction
165     my( $line ) = @_;
167     push( @converted_lines, "Style * " . $line );
170 sub convert_key_mouse_bindings
172     my( $line ) = @_;
173     my @components = split( /\s+/, $line, 5 );
175     # Take the last component.  We no longer care for "[*]" as conditional
176     # command parameters.  But we shouldn't really put another conditional
177     # in its place, so we'll just remove it.
178     $components[-1] =~ s/\[\*\]//;
180     # Also, conditional commands should now be separated with commas and not
181     # whitespace, so try and fix these up where we can.  It's not the
182     # intention we'll catch them all, but at least try and do so based on
183     # where they're likely to be used.
184     __convert_conditionals(\@components);
186     push( @converted_lines, join ' ', @components );
189 sub handle_continuation
191     no strict "refs"; # Yes, yes...
192     my( $line ) = @_;
194     return if !defined $last_func_ref || $last_func_ref eq '';
196     eval { &{$last_func_ref}($line) };
198     warn "$@\n" if $@;
201 sub handle_read_file
203     my( $line ) = @_;
204     my @read_parts = split( /\s+/, $line );
205     # Crudely try and work out if the file is readable, and if it is add it
206     # to the list of further files to convert.
207     #
208     # This won't handle having to interpolate out any env vars set via
209     # SetEnv, or worse yet, outside of FVWM's environment.  The user will
210     # just have to run this script on that file manually.
211     my $fname = $read_parts[1];
212     return unless defined $fname and $fname ne '';
214     if( -e $fname )
215     {
216         push( @additional_files, [$fname] );
217         
218         # We're done.
219         return;
220     }
222     # If we have this:
223     #
224     # Read foo
225     #
226     # Or this:
227     #
228     # Read $./foo
229     #
230     # Then we assume FVWM_USERDIR ("$HOME/.fvwm/"), and if that file can't 
231     # be found there, try CWD, and if that fails we just give up.
232     
233     # Canonicalise the starting point by removing "$." -- we can guess what
234     # it ought to be replaced with.
235     $fname =~ s/^\$\.\/?//;
237     if( -e "$ENV{FVWM_USERDIR}/$fname" )
238     {
239         push( @additional_files,
240             ["$ENV{FVWM_USERDIR}/$fname"] );
241         return;
242     }
244     if( -e "$ENV{HOME}/.fvwm/$fname" )
245     {
246         push( @additional_files,
247             ["$ENV{HOME}/.fvwm/$fname"] );
248         return;
249     }
251     my $cwd_path = getcwd();
253     if( -e "$cwd_path/$fname" )
254     {
255         push( @additional_files, [$fname] );
256         return;
257     }
259     warn "Unable to follow:  $line\n";
262 sub check_func_definition
264     my( $line ) = @_;
266     if( $line !~ /^addtofunc\s+(?:start|init|restart)function.*/i )
267     {
268         $last_func_ref = '';
269     }
272 sub convert_initfunc
274     my( $line ) = @_;
275     $last_func_ref = "convert_initfunc";
277     if( $line =~ /addtofunc\s+initfunction\s+\"??[icmhd]{1}\"??\s+.*/i ||
278         $line =~ /addtofunc\s+initfunction\s*/i )
279     {
280         $line =~ s/addtofunc\s+initfunction\s*//i;
281     }
283     $line =~ s/^\s*\+//;
285     return if !defined $line || $line eq '';
287     # What we need to do now is convert this from:
288     #
289     # + I Foo
290     #
291     # to:
292     #
293     # + I Test (Init) Foo
295     my @func_cmd = split( /\s+/, $line, 3 );
296     unshift( @func_cmd, '' ) unless @func_cmd > 2;
298     # Remove any quotes around the action type --- they're not needed
299     # anymore.
300     $func_cmd[1] =~ s/\"//g;
301     $func_cmd[1] .= q| Test (Init) |;
303     # Run the command through the conditional function to ensure we
304     # handle those correctly.
305     __convert_conditionals( \@func_cmd );
307     push( @{ $converted_funcs{initfunction} }, join ' ', @func_cmd );
310 sub convert_restartfunc
312     my( $line ) = @_;
313     $last_func_ref = "convert_restartfunc";
314     
315     # We treat this exactly like startfunction.
316     if( $line =~ /addtofunc\s+restartfunction\s+\"??[icmhd]{1}\"??\s+.*/i )
317     {
318         # Split this string.  We can throw away the "AddToFunc" part as this
319         # is irrelevant.  But we want the following result:
320         # ( 'I', 'Some Command' )
321         $line =~ s/addtofunc\s+restartfunction\s*//i;
322     }
324     $line =~ s/addtofunc\s+restartfunction\s*//i;
325     
326     return if $line eq '';
328     # Remove the continuation prefix as we can add this in when writing out
329     # the function definitions later. 
330     $line =~ s/^\s*\+//;
331     
332     my @func_cmd = split( /\s+/, $line, 2 );
333     $func_cmd[1] =~ s/\"//g;
335     # Run the command through the conditional function to ensure we
336     # handle those correctly.
337     __convert_conditionals( \@func_cmd );
339     push( @{ $converted_funcs{startfunction} }, join ' ', @func_cmd );
342 sub convert_startfunc
344     my( $line ) = @_;
346     # Now, it's possible that we have something like this:
347     #
348     # AddToFunc StartFunction I Some Command
349     #
350     # Extract the command part, add it to the hash for our functions, and
351     # flag the fact we're dealing with StartFunction at this point for any
352     # continuation lines (+ I Foo) since we can't determine the context of
353     # them without such a thing.
355     if( $line =~ /addtofunc\s+startfunction\s+\"??[icmhd]{1}\"??\s+.*/i )
356     {
357         # Split this string.  We can throw away the "AddToFunc" part as this
358         # is irrelevant.  But we want the following result:
359         # ( 'I', 'Some Command' )
360         $line =~ s/addtofunc\s+startfunction\s*//i;
361     }
362     $line =~ s/addtofunc\s+startfunction\s*//i;
363     
364     # Remove the continuation prefix as we can add this in when writing out
365     # the function definitions later. 
366     $line =~ s/^\s*\+//;
368     return if !defined $line || $line eq '';
369     
370     my @func_cmd = split( /\s+/, $line, 2 );
371     $func_cmd[1] =~ s/\"//g;
373     # Run the command through the conditional function to ensure we
374     # handle those correctly.
375     __convert_conditionals( \@func_cmd );
377     push( @{ $converted_funcs{startfunction} }, join ' ', @func_cmd );
378     $last_func_ref = "convert_startfunc";
381 sub write_out_file
383     my( $dest_file ) = @_;
384     open( my $f, '>', $dest_file ) or
385         die "Couldn't open $dest_file: $!\n";
387     # Write out most of the file.
388     print $f join( "\n", @converted_lines );
390     # Write out the functions.
391     print $f qq|\n\nDestroyFunc StartFunction\nAddToFunc StartFunction\n|;
393     # Put the Init stuff before anything else.
394     for( @{ $converted_funcs{initfunction} }, 
395          @{ $converted_funcs{startfunction } } )
396     {
397         print $f "+ $_\n";
398     }
400     close( $f );
403 sub dispatch_line
405     my( $line ) = @_;
407     if( $line =~ /^style/i )
408     {
409         convert_styles($line);
410     } elsif( $line =~ /^\s*\*fvwmtheme:??/i ) {
411         convert_fvwmtheme($line);
412     } elsif( $line =~ /^\s*modulepath\s*/i ) {
413         handle_modulepath( $line );
414     } elsif( $line =~ /^\s*windowshadesteps.*/i ) {
415         convert_windowshadesteps($line);
416     } elsif( $line =~ /^\s*module(?:synchronous)?.*?fvwmtheme$/i ) {
417         convert_fvwmtheme($line);
418     } elsif( $line =~ /^\s*edgeresistance\s*\d+\s*\d+/i ) {
419         convert_edge_resistance($line);
420     } elsif( $line =~ /^\s*key|mouse/i ) {
421         convert_key_mouse_bindings($line);
422     } elsif( $line =~ /^\s*snap(?:attraction|grid)/i ) {
423         convert_snapattraction( $line );
424     } elsif( $line =~ /^\s*addtofunc\s+initfunction/i ) {
425         convert_initfunc( $line );
426     } elsif( $line =~ /^\s*addtofunc\s+startfunction.*/i ) {
427         convert_startfunc( $line );
428     } elsif( $line =~ /^\s*addtofunc\s+restartfunction/i ) {
429         convert_restartfunc( $line );
430     } elsif( $line =~ /^\s*addtofunc\s+\w+.*/i ) {
431         check_func_definition( $line );
432     } elsif( $line =~ /^\s*\+\s*\"??[ichmd]{1}\s*\"??\s+.*/i ) {
433         handle_continuation( $line );
434     } elsif( $line =~ /^\s*read\s*[\/\w]+/i ) {
435         handle_read_file( $line );
436     } else {
437         # Could be a comment, or a continuation, or simply something we
438         # don't need to convert.  As far as continuation lines are
439         # concerned, these are kept in order just by pushing them onto the
440         # array --- but converting continuation lines is tricky since we'd
441         # need to determine the context of the continuation.  I can't be
442         # bothered.
443         push( @converted_lines, $_ );
444     }
447 sub usage
449     print "fvwm-convert-2.6 [-f] [-h] source-file destination-file\n";
450     exit;
453 GetOptions(
454     "help|h" => \&usage,
455     "follow-read|f" => \$follow_read,
456 ) || usage();
458 # But we still require @ARGV to be populated with our filenames.
459 usage() unless( @ARGV > 0 and @ARGV <=2 );
461 my @files = [@ARGV];
462 process_files( \@files );
464 if( @additional_files && !$follow_read )
466     print "The following files were detected, but not processed:\n\n",
467     join("\n", @$_ ) for @additional_files,;
468     print "\n";
471 # Only do this is we've been asked.
472 if( @additional_files && $follow_read )
474     $process_read = 1;
475     process_files( \@additional_files );