File: gmt5syntax.in

package info (click to toggle)
gmt 6.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 107,344 kB
  • sloc: ansic: 272,264; sh: 8,955; fortran: 49; makefile: 44; perl: 32; csh: 9
file content (54 lines) | stat: -rwxr-xr-x 1,627 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl
eval 'exec perl -x -wS $0 ${1+"$@"}'
  if 0;
#
#  Copyright (c) 1991-2022 by the GMT Team (https://www.generic-mapping-tools.org/team.html)
#  See LICENSE.TXT file for copying and redistribution conditions.
#
# gmt5syntax convert old GMT script to use new 'gmt <module>' syntax
# usage: gmt5syntax old_script > new_script

use strict;
use warnings;

# name of the main gmt executable
my $progname = 'gmt@GMT_INSTALL_NAME_SUFFIX@';

# words to prepend with $progname
my @modulenames = `$progname --show-classic`;
chomp (@modulenames);

# add more compatibility modules
my @compat_modules = split (";", "@GMT_COMPAT_MODULES@");
push @modulenames, @compat_modules;

# Regexp::Assemble creates a single efficient regexp from multiple regexp
my $have_assemble = eval "use Regexp::Assemble; 1" ? 1 : 0;

my $re;
if ($have_assemble) {
  # build smart regexp from @modulenames
  my $ra = Regexp::Assemble->new(anchor_word_begin => 1, anchor_word_end => 1);
  $ra->add(@modulenames);
  #say $ra->as_string; # print assembled regexp
  $re = $ra->re;
}
else {
  # concatenate modulenames to '\b(backtracker|blockmean|blockmedian|...)\b'
  $re= '\b(' . (join '|', @modulenames) . ')\b';
}

# convert lines to new syntax
while (<>) {
  s{(^[^#<>]+)}{ # skip anything after comment or I/O redirection
    my $str = $1;
    $str =~ s/($re)/$progname $1/g unless /$progname ($re)/; # prepend $progname
    $str
  }eg;
  s{(^[# ]+[^<>]+)}{ # convert gmt commands directly following a comment
    my $str = $1;
    $str =~ s/^([# ]+)($re)/$1$progname $2/g unless /$progname ($re)/; # prepend $progname
    $str
  }eg;
  print;
}