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
152 push( @converted_lines, "# Commented out by fvwm-convert-2.6: $line" );
155 # This should have happened in the fvwm-2.4 convert script, but handle it
157 sub convert_windowshadesteps
161 $line = "Style * WindowShadeSteps $1" :
162 $line = "Style * " . $line;
164 push( @converted_lines, $line );
167 sub convert_edge_resistance
171 # This gets converted into two parts. One is the EdgeResistance
172 # command, the other is a style line.
174 # We could only ever have had two numbers as arguments to
176 my( $edge_res_arg, $move_res_arg ) =
177 ( $line =~ /edgeresistance\s*(\d+)\s*(\d+)/i );
179 push( @converted_lines,
181 EdgeResistance $edge_res_arg
182 Style * EdgeMoveResistance $move_res_arg| );
185 sub convert_snapattraction
189 push( @converted_lines, "Style * " . $line );
192 sub convert_key_mouse_bindings
195 my @components = split( /(\s+)/, $line, 5 );
197 # Also, conditional commands should now be separated with commas and not
198 # whitespace, so try and fix these up where we can. It's not the
199 # intention we'll catch them all, but at least try and do so based on
200 # where they're likely to be used.
201 __convert_conditionals(\@components);
203 push( @converted_lines, join '', @components );
206 sub handle_continuation
208 no strict "refs"; # Yes, yes...
211 if( !defined $last_func_ref || $last_func_ref eq '' )
213 my @func_parts = split( /(\+\s*\"?(?:i|c|d|h|m)\"?\s*)/i, $line, 2 );
215 __convert_conditionals(\@func_parts);
217 push( @converted_lines, join '', @func_parts );
221 eval { &{$last_func_ref}($line) };
228 my @read_parts = split( /\s+/, $line );
230 push( @converted_lines, $line );
232 # Crudely try and work out if the file is readable, and if it is add it
233 # to the list of further files to convert.
235 # This won't handle having to interpolate out any env vars set via
236 # SetEnv, or worse yet, outside of FVWM's environment. The user will
237 # just have to run this script on that file manually.
238 my $fname = $read_parts[1];
239 return unless defined $fname and $fname ne '';
243 push( @additional_files, [$fname] );
257 # Then we assume FVWM_USERDIR ("$HOME/.fvwm/"), and if that file can't
258 # be found there, try CWD, and if that fails we just give up.
260 # Canonicalise the starting point by removing "$." -- we can guess what
261 # it ought to be replaced with.
262 $fname =~ s/^\$\.\/?//;
264 if( -e "$ENV{FVWM_USERDIR}/$fname" )
266 push( @additional_files,
267 ["$ENV{FVWM_USERDIR}/$fname"] );
271 if( -e "$ENV{HOME}/.fvwm/$fname" )
273 push( @additional_files,
274 ["$ENV{HOME}/.fvwm/$fname"] );
278 my $cwd_path = getcwd();
280 if( -e "$cwd_path/$fname" )
282 push( @additional_files, [$fname] );
286 warn "Unable to follow: $line\n";
289 sub check_func_definition
293 if( $line !~ /^addtofunc\s+(?:start|init|restart)function.*/i )
298 # Then we have a standard function line in the form:
302 # Ensure we run it all through __convert_conditionals()
303 my @func_parts = split( /(\s+)/, $line, 4 );
304 __convert_conditionals( \@func_parts );
306 push( @converted_lines, join '', @func_parts );
313 $last_func_ref = "convert_initfunc";
315 if( $line =~ /addtofunc\s+initfunction\s+\"??[icmhd]{1}\"??\s+.*/i ||
316 $line =~ /addtofunc\s+initfunction\s*/i )
318 $line =~ s/addtofunc\s+initfunction\s*//i;
323 return if !defined $line || $line eq '';
325 # What we need to do now is convert this from:
331 # + I Test (Init) Foo
333 my @func_cmd = split( /\s+/, $line, 3 );
334 unshift( @func_cmd, '' ) unless @func_cmd > 2;
336 # Remove any quotes around the action type --- they're not needed
338 $func_cmd[1] =~ s/\"//g;
339 $func_cmd[1] .= q| Test (Init) |;
341 # Run the command through the conditional function to ensure we
342 # handle those correctly.
343 __convert_conditionals( \@func_cmd );
345 push( @{ $converted_funcs{initfunction} }, join ' ', @func_cmd );
348 sub convert_restartfunc
351 $last_func_ref = "convert_restartfunc";
353 # We treat this exactly like startfunction.
354 if( $line =~ /addtofunc\s+restartfunction\s+\"??[icmhd]{1}\"??\s+.*/i )
356 # Split this string. We can throw away the "AddToFunc" part as this
357 # is irrelevant. But we want the following result:
358 # ( 'I', 'Some Command' )
359 $line =~ s/addtofunc\s+restartfunction\s*//i;
362 $line =~ s/addtofunc\s+restartfunction\s*//i;
364 return if $line eq '';
366 # Remove the continuation prefix as we can add this in when writing out
367 # the function definitions later.
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 );
380 sub convert_startfunc
384 # Now, it's possible that we have something like this:
386 # AddToFunc StartFunction I Some Command
388 # Extract the command part, add it to the hash for our functions, and
389 # flag the fact we're dealing with StartFunction at this point for any
390 # continuation lines (+ I Foo) since we can't determine the context of
391 # them without such a thing.
393 if( $line =~ /addtofunc\s+startfunction\s+\"??[icmhd]{1}\"??\s+.*/i )
395 # Split this string. We can throw away the "AddToFunc" part as this
396 # is irrelevant. But we want the following result:
397 # ( 'I', 'Some Command' )
398 $line =~ s/addtofunc\s+startfunction\s*//i;
400 $line =~ s/addtofunc\s+startfunction\s*//i;
402 # Remove the continuation prefix as we can add this in when writing out
403 # the function definitions later.
406 return if !defined $line || $line eq '';
408 my @func_cmd = split( /\s+/, $line, 2 );
409 $func_cmd[1] =~ s/\"//g;
411 # Run the command through the conditional function to ensure we
412 # handle those correctly.
413 __convert_conditionals( \@func_cmd );
415 push( @{ $converted_funcs{startfunction} }, join ' ', @func_cmd );
416 $last_func_ref = "convert_startfunc";
421 my( $dest_file ) = @_;
422 open( my $f, '>', $dest_file ) or
423 die "Couldn't open $dest_file: $!\n";
425 # If we had any continuation lines, preserve them as best we can.
426 @converted_lines = map {
427 join "\\\n", split /\\/, $_
430 print $f join( "\n", @converted_lines );
432 # Write out the functions.
433 if( defined $converted_funcs{initfunction} or
434 defined $converted_funcs{startfunction} )
436 print $f qq|\n\nDestroyFunc StartFunction\nAddToFunc StartFunction\n|;
438 # Put the Init stuff before anything else.
439 for( @{ $converted_funcs{initfunction} },
440 @{ $converted_funcs{startfunction } } )
453 if( $line =~ /^style/i )
455 convert_styles($line);
456 } elsif( $line =~ /^\s*\*fvwmtheme:??/i ) {
457 convert_fvwmtheme($line);
458 } elsif( $line =~ /^\s*modulepath\s*/i ) {
459 handle_modulepath( $line );
460 } elsif( $line =~ /^\s*windowshadesteps.*/i ) {
461 convert_windowshadesteps($line);
462 } elsif( $line =~ /^\s*module(?:synchronous)?.*?fvwmtheme$/i ) {
463 convert_fvwmtheme($line);
464 } elsif( $line =~ /^\s*edgeresistance\s*\d+\s*\d+/i ) {
465 convert_edge_resistance($line);
466 } elsif( $line =~ /^\s*key|mouse/i ) {
467 convert_key_mouse_bindings($line);
468 } elsif( $line =~ /^\s*snap(?:attraction|grid)/i ) {
469 convert_snapattraction( $line );
470 } elsif( $line =~ /^\s*addtofunc\s+initfunction/i ) {
471 convert_initfunc( $line );
472 } elsif( $line =~ /^\s*addtofunc\s+startfunction.*/i ) {
473 convert_startfunc( $line );
474 } elsif( $line =~ /^\s*addtofunc\s+restartfunction/i ) {
475 convert_restartfunc( $line );
476 } elsif( $line =~ /^\s*addtofunc\s+\w+.*/i ) {
477 check_func_definition( $line );
478 } elsif( $line =~ /^\s*\+\s*\"??[ichmd]{1}\s*\"??\s+.*/i ) {
479 handle_continuation( $line );
480 } elsif( $line =~ /^\s*read\s*[\/\w]+/i ) {
481 handle_read_file( $line );
483 # Could be a comment, or a continuation, or simply something we
484 # don't need to convert. As far as continuation lines are
485 # concerned, these are kept in order just by pushing them onto the
486 # array --- but converting continuation lines is tricky since we'd
487 # need to determine the context of the continuation. I can't be
489 push( @converted_lines, $_ );
495 print "fvwm-convert-2.6 [-f] [-h] source-file destination-file\n";
501 "follow-read|f" => \$follow_read,
504 # But we still require @ARGV to be populated with our filenames.
505 usage() unless( @ARGV > 0 and @ARGV <=2 );
508 process_files( \@files );
510 if( @additional_files && !$follow_read )
512 print "The following files were detected, but not processed:\n\n",
513 join("\n", @$_ ) for @additional_files,;
517 # Only do this is we've been asked.
518 if( @additional_files && $follow_read )
521 process_files( \@additional_files );