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
27 # Global array for all our converted lines.
28 my @converted_lines = ();
30 # Global softref for addtofunc continuations.
32 my %converted_funcs = ();
34 # Global for additional files...
35 my @additional_files = ();
41 # Convert conditional command syntax correctly.
42 sub __convert_conditionals
45 my( $line ) = $cond->[-1];
47 # Take the last component. We no longer care for "[*]" as conditional
48 # command parameters. But we shouldn't really put another conditional
49 # in its place, so we'll just remove it.
52 # And convert over Next [$1] syntax.
53 $line =~ s/\[(.*?)\]/\($1\)/;
55 $line = "$1 ". join( ', ', split( /\s+/, $2 ) ) . " $3" if $line =~
56 /(all|current|direction|next|none|prev|pick|thiswindow|windowid)\s*(\(.*?\))(.*)/i;
61 # Process the files specified and output them to a destination file.
67 foreach my $f ( @$files )
70 my $cwd_path = getcwd();
72 warn "Following: Read $s...\n" if $process_read;
74 if( !defined $d or $d eq '' )
76 my $fbasename = basename( $s );
77 $d = "$cwd_path/$fbasename.converted";
81 die "Destination file: $d exists\n";
84 open( my $in_file, '<', $s ) or die
85 "Unable to open source file: $!\n";
91 # We have to handle continuation lines here, such as:
93 # Style foo !Bar, !Baz, \
104 @converted_lines = ();
105 %converted_funcs = ();
109 # Convert style syntax over where applicable.
115 # At the very least, we can cheat and just negate everything. Whilst it
116 # isn't deprecated yet, it will be -- so it won't hurt to do it here.
118 # Split the line out first of all, between the "Style foo" part, and the
119 # actual styles being applied.
120 my( @style_parts ) = ($line =~ /^(style\s+\"??[\w+*?]\"??)(.*)$/i);
122 # Convert the second part over.
123 foreach( split( /\s*,\s*/, $style_parts[1] ) )
125 # There is no !PPosition style, but there is !UsePPosition
126 s/(?:No)(.*)/\!$1/ unless $_ =~ /nopposition/i;
127 s/nopposition/!UsePPosition/i;
131 push @converted_lines, $style_parts[0] . join(', ',
135 # Buckshot approach at turning fvwmthemes into colorsets. Can't really do
136 # much more than this, but at least gives the user something to go on.
137 sub convert_fvwmtheme
141 $line =~ s/^\*fvwmtheme\s*:?//i;
142 $line = undef if $line =~ /modulesynchronous.*?fvwmtheme/i;
144 push @converted_lines, $line;
147 # Comment out the modulepath line -- grr.
148 sub handle_modulepath
151 $line = "# " . $line;
153 push( @converted_lines, $line );
156 # This should have happened in the fvwm-2.4 convert script, but handle it
158 sub convert_windowshadesteps
162 $line = "Style * WindowShadeSteps $1" :
163 $line = "Style * " . $line;
165 push( @converted_lines, $line );
168 sub convert_edge_resistance
172 # This gets converted into two parts. One is the EdgeResistance
173 # command, the other is a style line.
175 # We could only ever have had two numbers as arguments to
177 my( $edge_res_arg, $move_res_arg ) =
178 ( $line =~ /edgeresistance\s*(\d+)\s*(\d+)/i );
180 push( @converted_lines,
182 EdgeResistance $edge_res_arg
183 Style * EdgeMoveResistance $move_res_arg| );
186 sub convert_snapattraction
190 push( @converted_lines, "Style * " . $line );
193 sub convert_key_mouse_bindings
196 my @components = split( /(\s+)/, $line, 5 );
198 # Also, conditional commands should now be separated with commas and not
199 # whitespace, so try and fix these up where we can. It's not the
200 # intention we'll catch them all, but at least try and do so based on
201 # where they're likely to be used.
202 __convert_conditionals(\@components);
204 push( @converted_lines, join '', @components );
207 sub handle_continuation
209 no strict "refs"; # Yes, yes...
212 if( !defined $last_func_ref || $last_func_ref eq '' )
214 my @func_parts = split( /(\+\s*\"?(?:i|c|d|h|m)\"?\s*)/i, $line, 2 );
216 __convert_conditionals(\@func_parts);
218 push( @converted_lines, join '', @func_parts );
222 eval { &{$last_func_ref}($line) };
229 my @read_parts = split( /\s+/, $line );
231 push( @converted_lines, $line );
233 # Crudely try and work out if the file is readable, and if it is add it
234 # to the list of further files to convert.
236 # This won't handle having to interpolate out any env vars set via
237 # SetEnv, or worse yet, outside of FVWM's environment. The user will
238 # just have to run this script on that file manually.
239 my $fname = $read_parts[1];
240 return unless defined $fname and $fname ne '';
244 push( @additional_files, [$fname] );
258 # Then we assume FVWM_USERDIR ("$HOME/.fvwm/"), and if that file can't
259 # be found there, try CWD, and if that fails we just give up.
261 # Canonicalise the starting point by removing "$." -- we can guess what
262 # it ought to be replaced with.
263 $fname =~ s/^\$\.\/?//;
265 if( -e "$ENV{FVWM_USERDIR}/$fname" )
267 push( @additional_files,
268 ["$ENV{FVWM_USERDIR}/$fname"] );
272 if( -e "$ENV{HOME}/.fvwm/$fname" )
274 push( @additional_files,
275 ["$ENV{HOME}/.fvwm/$fname"] );
279 my $cwd_path = getcwd();
281 if( -e "$cwd_path/$fname" )
283 push( @additional_files, [$fname] );
287 warn "Unable to follow: $line\n";
290 sub check_func_definition
294 if( $line !~ /^addtofunc\s+(?:start|init|restart)function.*/i )
299 # Then we have a standard function line in the form:
303 # Ensure we run it all through __convert_conditionals()
304 my @func_parts = split( /(\s+)/, $line, 4 );
305 __convert_conditionals( \@func_parts );
307 push( @converted_lines, join '', @func_parts );
314 $last_func_ref = "convert_initfunc";
316 if( $line =~ /addtofunc\s+initfunction\s+\"??[icmhd]{1}\"??\s+.*/i ||
317 $line =~ /addtofunc\s+initfunction\s*/i )
319 $line =~ s/addtofunc\s+initfunction\s*//i;
324 return if !defined $line || $line eq '';
326 # What we need to do now is convert this from:
332 # + I Test (Init) Foo
334 my @func_cmd = split( /\s+/, $line, 3 );
335 unshift( @func_cmd, '' ) unless @func_cmd > 2;
337 # Remove any quotes around the action type --- they're not needed
339 $func_cmd[1] =~ s/\"//g;
340 $func_cmd[1] .= q| Test (Init) |;
342 # Run the command through the conditional function to ensure we
343 # handle those correctly.
344 __convert_conditionals( \@func_cmd );
346 push( @{ $converted_funcs{initfunction} }, join ' ', @func_cmd );
349 sub convert_restartfunc
352 $last_func_ref = "convert_restartfunc";
354 # We treat this exactly like startfunction.
355 if( $line =~ /addtofunc\s+restartfunction\s+\"??[icmhd]{1}\"??\s+.*/i )
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+restartfunction\s*//i;
363 $line =~ s/addtofunc\s+restartfunction\s*//i;
365 return if $line eq '';
367 # Remove the continuation prefix as we can add this in when writing out
368 # the function definitions later.
371 my @func_cmd = split( /\s+/, $line, 2 );
372 $func_cmd[1] =~ s/\"//g;
374 # Run the command through the conditional function to ensure we
375 # handle those correctly.
376 __convert_conditionals( \@func_cmd );
378 push( @{ $converted_funcs{startfunction} }, join ' ', @func_cmd );
381 sub convert_startfunc
385 # Now, it's possible that we have something like this:
387 # AddToFunc StartFunction I Some Command
389 # Extract the command part, add it to the hash for our functions, and
390 # flag the fact we're dealing with StartFunction at this point for any
391 # continuation lines (+ I Foo) since we can't determine the context of
392 # them without such a thing.
394 if( $line =~ /addtofunc\s+startfunction\s+\"??[icmhd]{1}\"??\s+.*/i )
396 # Split this string. We can throw away the "AddToFunc" part as this
397 # is irrelevant. But we want the following result:
398 # ( 'I', 'Some Command' )
399 $line =~ s/addtofunc\s+startfunction\s*//i;
401 $line =~ s/addtofunc\s+startfunction\s*//i;
403 # Remove the continuation prefix as we can add this in when writing out
404 # the function definitions later.
407 return if !defined $line || $line eq '';
409 my @func_cmd = split( /\s+/, $line, 2 );
410 $func_cmd[1] =~ s/\"//g;
412 # Run the command through the conditional function to ensure we
413 # handle those correctly.
414 __convert_conditionals( \@func_cmd );
416 push( @{ $converted_funcs{startfunction} }, join ' ', @func_cmd );
417 $last_func_ref = "convert_startfunc";
422 my( $dest_file ) = @_;
423 open( my $f, '>', $dest_file ) or
424 die "Couldn't open $dest_file: $!\n";
426 # If we had any continuation lines, preserve them as best we can.
427 @converted_lines = map {
428 join "\\\n", split /\\/, $_
431 print $f join( "\n", @converted_lines );
433 # Write out the functions.
434 if( defined $converted_funcs{initfunction} or
435 defined $converted_funcs{startfunction} )
437 print $f qq|\n\nDestroyFunc StartFunction\nAddToFunc StartFunction\n|;
439 # Put the Init stuff before anything else.
440 for( @{ $converted_funcs{initfunction} },
441 @{ $converted_funcs{startfunction } } )
454 if( $line =~ /^style/i )
456 convert_styles($line);
457 } elsif( $line =~ /^\s*\*fvwmtheme:??/i ) {
458 convert_fvwmtheme($line);
459 } elsif( $line =~ /^\s*modulepath\s*/i ) {
460 handle_modulepath( $line );
461 } elsif( $line =~ /^\s*windowshadesteps.*/i ) {
462 convert_windowshadesteps($line);
463 } elsif( $line =~ /^\s*module(?:synchronous)?.*?fvwmtheme$/i ) {
464 convert_fvwmtheme($line);
465 } elsif( $line =~ /^\s*edgeresistance\s*\d+\s*\d+/i ) {
466 convert_edge_resistance($line);
467 } elsif( $line =~ /^\s*key|mouse/i ) {
468 convert_key_mouse_bindings($line);
469 } elsif( $line =~ /^\s*snap(?:attraction|grid)/i ) {
470 convert_snapattraction( $line );
471 } elsif( $line =~ /^\s*addtofunc\s+initfunction/i ) {
472 convert_initfunc( $line );
473 } elsif( $line =~ /^\s*addtofunc\s+startfunction.*/i ) {
474 convert_startfunc( $line );
475 } elsif( $line =~ /^\s*addtofunc\s+restartfunction/i ) {
476 convert_restartfunc( $line );
477 } elsif( $line =~ /^\s*addtofunc\s+\w+.*/i ) {
478 check_func_definition( $line );
479 } elsif( $line =~ /^\s*\+\s*\"??[ichmd]{1}\s*\"??\s+.*/i ) {
480 handle_continuation( $line );
481 } elsif( $line =~ /^\s*read\s*[\/\w]+/i ) {
482 handle_read_file( $line );
484 # Could be a comment, or a continuation, or simply something we
485 # don't need to convert. As far as continuation lines are
486 # concerned, these are kept in order just by pushing them onto the
487 # array --- but converting continuation lines is tricky since we'd
488 # need to determine the context of the continuation. I can't be
490 push( @converted_lines, $_ );
496 print "fvwm-convert-2.6 [-f] [-h] source-file destination-file\n";
502 "follow-read|f" => \$follow_read,
505 # But we still require @ARGV to be populated with our filenames.
506 usage() unless( @ARGV > 0 and @ARGV <=2 );
509 process_files( \@files );
511 if( @additional_files && !$follow_read )
513 print "The following files were detected, but not processed:\n\n",
514 join("\n", @$_ ) for @additional_files,;
518 # Only do this is we've been asked.
519 if( @additional_files && $follow_read )
522 process_files( \@additional_files );