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
26 # Global array for all our converted lines.
27 my @converted_lines = ();
29 # Global softref for addtofunc continuations.
31 my %converted_funcs = ();
33 # Global for additional files...
34 my @additional_files = ();
40 # Convert conditional command syntax correctly.
41 sub __convert_conditionals
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;
52 # Process the files specified and output them to a destination file.
58 foreach my $f ( @$files )
62 warn "Following: Read $s...\n" if $process_read;
64 if( (!defined $d or $d eq '') and
69 die "Destination file: $d exists\n";
72 open( my $in_file, '<', $s ) or die
73 "Unable to open source file: $!\n";
82 @converted_lines = ();
83 %converted_funcs = ();
87 # Convert style syntax over where applicable.
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] ) )
103 # FIXME -- this should be flagged up as bugs in FVWM.
104 s/(?:No)(.*)/\!$1/ unless $_ =~ /nopposition/i;
108 push @converted_lines, $style_parts[0] . join(', ',
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
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
128 $line = "# " . $line;
130 push( @converted_lines, $line );
133 # This should have happened in the fvwm-2.4 convert script, but handle it
135 sub convert_windowshadesteps
139 $line = "Style * WindowShadeSteps $1" :
140 $line = "Style * " . $line;
142 push( @converted_lines, $line );
145 sub convert_edge_resistance
149 # This gets converted into two parts. One is the EdgeResistance
150 # command, the other is a style line.
152 # We could only ever have had two numbers as arguments to
154 my( $edge_res_arg, $move_res_arg ) =
155 ( $line =~ /edgeresistance\s*(\d+)\s*(\d+)/i );
157 push( @converted_lines,
159 EdgeResistance $edge_res_arg
160 Style * EdgeMoveResistance $move_res_arg| );
163 sub convert_snapattraction
167 push( @converted_lines, "Style * " . $line );
170 sub convert_key_mouse_bindings
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...
194 return if !defined $last_func_ref || $last_func_ref eq '';
196 eval { &{$last_func_ref}($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.
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 '';
216 push( @additional_files, [$fname] );
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.
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" )
239 push( @additional_files,
240 ["$ENV{FVWM_USERDIR}/$fname"] );
244 if( -e "$ENV{HOME}/.fvwm/$fname" )
246 push( @additional_files,
247 ["$ENV{HOME}/.fvwm/$fname"] );
251 my $cwd_path = getcwd();
253 if( -e "$cwd_path/$fname" )
255 push( @additional_files, [$fname] );
259 warn "Unable to follow: $line\n";
262 sub check_func_definition
266 if( $line !~ /^addtofunc\s+(?:start|init|restart)function.*/i )
275 $last_func_ref = "convert_initfunc";
277 if( $line =~ /addtofunc\s+initfunction\s+\"??[icmhd]{1}\"??\s+.*/i ||
278 $line =~ /addtofunc\s+initfunction\s*/i )
280 $line =~ s/addtofunc\s+initfunction\s*//i;
285 return if !defined $line || $line eq '';
287 # What we need to do now is convert this from:
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
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
313 $last_func_ref = "convert_restartfunc";
315 # We treat this exactly like startfunction.
316 if( $line =~ /addtofunc\s+restartfunction\s+\"??[icmhd]{1}\"??\s+.*/i )
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;
324 $line =~ s/addtofunc\s+restartfunction\s*//i;
326 return if $line eq '';
328 # Remove the continuation prefix as we can add this in when writing out
329 # the function definitions later.
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
346 # Now, it's possible that we have something like this:
348 # AddToFunc StartFunction I Some Command
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 )
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;
362 $line =~ s/addtofunc\s+startfunction\s*//i;
364 # Remove the continuation prefix as we can add this in when writing out
365 # the function definitions later.
368 return if !defined $line || $line eq '';
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";
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 } } )
407 if( $line =~ /^style/i )
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 );
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
443 push( @converted_lines, $_ );
449 print "fvwm-convert-2.6 [-f] [-h] source-file destination-file\n";
455 "follow-read|f" => \$follow_read,
458 # But we still require @ARGV to be populated with our filenames.
459 usage() unless( @ARGV > 0 and @ARGV <=2 );
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,;
471 # Only do this is we've been asked.
472 if( @additional_files && $follow_read )
475 process_files( \@additional_files );