summaryrefslogtreecommitdiff
path: root/lib/sbin/f_file_processor.pl
blob: 437fbcd7a3a59d2c0d20cbaa3774cf0cf35add55 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/perl -sw
#/*@@
#  @file      f_file_processor.pl
#  @date      Jan 22 1995
#  @author    Paul Walker
#  @desc
#  Postprocessor for Fortran files.
#
#  Reads STDIN, writes to STDOUT.
#
#  removes all comments
#  replaces && with newline and tab to col 7
#  Breaks lines greater than 72 or 132 cols
#     (depending on fixed or free format)
#  Does this using multi-line matching!
#
#  If run with -free_format, chooses free-format line splitting.
#
#  @enddesc
#  @version $Header$
#@@*/

# Possible command line options:
#    -free-format
#    -line_directives=[yes|no]
#    -source_file_name=[filename]
# Reads input from stdin.
# The result will be printed to stdout.

# Do we want line directives?
$line_directives = $line_directives eq 'yes';

# Pick the correct set of comments to remove.
if ($free_format)
{
  $standard_comments = "^\\s*!(?!\\\$(omp|hpf))";
}
else
{
  $standard_comments = "^[c!*](?!\\\$(omp|hpf))";
}

# Maximum line length for free form Fortran
$max_line_length = 132;
# Indentation for continued free form Fortran
$indentation = 2;
$indent = " " x $indentation;

# Loop over all lines.
$line = 1;
$file = "";
$autoline = 1;
$autofile = "";
while (<>)
{
  # Get rid of final \n
  chomp;

  # Handle directives
  if (/^\#/)
  {
    if ($line_directives)
    {
      # Handle line directives
      if (/^\#\s*(\d+)\s*"([^"]*)"/)
      {
        $line = $1;
        $file = $2;
        if ($file eq '<stdin>')
        {
          $file = $source_file_name;
        }
      } else {
        ++$line;
      }
      next;
    }
    else
    {
      # Ignore directives
      next;
    }
  }

  # Get rid of any tabs
  s/\t/        /g;

  # Chop Fortran comments to 132 columns (they stay in code)
  # removing any quotes
  # (standard c C, or even ! comments)
  if (/$standard_comments/i)
  {
    # Remove quotes
    s/['"]//g;
    if (/(.{$max_line_length,$max_line_length})/)
    {
      &printline ($1);
    }
    else
    {
      &printline ($_);
    }
  }
  else
  {
    # Get rid of ! comments : a bit tricky as ! may appear inside strings

    # the following code by Fokke Dijkstra also checks for comments
    # on a line with a string
    # Search for possible comment
    if (/!(?!\$(omp|hpf))/i)
    {
      # find all ! " and ' and check for strings or comments
      $string = 0;
      while (m/(["'!])/g)
      {
        # keep track of position for substr include last character
        $position = (pos) - 1;

        # check if we are currently in a string and possibly end it,
        # or check for a new string or a comment
        if ($string)
        {
          if ($1 eq "\'" && $string == 1)
          {
            $string = 0;
          }
          if ($1 eq "\"" && $string == 2)
          {
            $string = 0;
          }
        }
        elsif ($1 eq "\'")
        {
          $string = 1;
        }
        elsif ($1 eq "\"")
        {
          $string = 2;
        }
        elsif ($1 eq "!")
        {
          $_ = substr ($_, 0, $position);
          last;
        }
      }
    }

    # Get rid of trailing blanks
    s/\s*$//;

    # Put in the line breaks (&&)
    if($free_format)
    {
      s/\s*\&\&\s*/\n$indent/g;
    }
    else
    {
      s/\s*\&\&\s*/\n      /g;
    }

    foreach my $LINE (split('\n',$_))
    {
      &splitline($LINE);
    }
  }

  ++$line;
}

#/*@@
#  @routine    splitline
#  @date       Wed Nov 24 12:14:55 1999
#  @author     Tom Goodale
#  @desc
#  Chooses the correct routine to split lines.
#  @enddesc
#@@*/
sub splitline
{
  my ($LINE) = @_;

  if($free_format)
  {
    &free_format_splitline($LINE);
  }
  else
  {
    &fixed_format_splitline($LINE);
  }

}

#/*@@
#  @routine    fixed_format_splitline
#  @date       1995
#  @author     Paul Walker
#  @desc
#  Splits lines for F77 or fixed-format F90
#  @enddesc
#@@*/
sub fixed_format_splitline
{
  my ($LINE) = @_;

  # Note the new treatement of comments with \S
  if ($LINE =~ /^([^\S].{71,71})/m)
  {
    &printline ($1);
    $LINE =~ s/.{72,72}//m;
    while ($LINE =~ /^(.{66,66})/m)
    {
      &printline ("     &$1");
      $LINE =~ s/.{66,66}//m;
    }
    &printline ("     &$LINE");
  }
  else
  {
    &printline ($LINE);
  }

}

#/*@@
#  @routine    free_format_splitline
#  @date       Thu Sep 30 12:05:36 1999
#  @author     Erik Schnetter
#  @desc
#  Splits lines for free-format Fortran 90.
#  @enddesc
#@@*/
sub free_format_splitline
{
  my ($LINE) = @_;
  my $OUT;
  my $maxlen1 = $max_line_length - 1;
  my $maxlen1i = $max_line_length - $indentation - 1;
  my $maxlen2i = $max_line_length - $indentation - 2;
  my $sentinel = "";

  if ($LINE =~ /^(.{$maxlen1,$maxlen1})../m)
  {
    $OUT = $1;
    if ($OUT =~ /^\s*(!\$(omp|hpf))/mi)
    {
      $sentinel = $1;
      $maxlen1i = $maxlen1i - length($sentinel);
      $maxlen2i = $maxlen2i - length($sentinel);
    }
    # Check if the line already has a continuation mark.
    $OUT = "$OUT&" if (! ($OUT =~ /\&\s*$/m));
    &printline ($OUT);
    $LINE =~ s/.{$maxlen1,$maxlen1}//m;

    while ($LINE =~ /^(.{$maxlen1i,$maxlen1i})/m)
    {
      $LINE =~ /^(.{$maxlen2i,$maxlen2i})/m;
      $OUT = $1;
      $OUT = "$indent$sentinel&$OUT" if (! ($OUT =~ /^\s*\&/m));
      $OUT = "$OUT&" if (! ($OUT =~ /\&\s*$/m));
      &printline ($OUT);
      $LINE =~ s/.{$maxlen2i,$maxlen2i}//m;
    }

    if ($LINE =~ /^\&\s*$/m)
    {
      &printline ("$indent$sentinel& $LINE");
    }
    elsif ($LINE =~ /^\s*\&\s*$/m)
    {
      &printline ("$indent$sentinel&$LINE");
    }
    else
    {
      $OUT = $LINE;
      $OUT = "$indent$sentinel&$OUT" if (! ($LINE =~ /^\s*\&/m));
      &printline ($OUT);
    }
  }
  else
  {
    &printline ($LINE);
  }
}



# Print a line and append a newline
# Emit line number and file name directives if necessary
sub printline
{
  my ($LINE) = @_;
  
  if ($LINE eq '') {
    # don't print empty lines
  } else {
    if ($line_directives) {
      if ($file ne $autofile) {
        print "# $line \"$file\"\n";
        $autoline = $line;
        $autofile = $file;
      } elsif ($line ne $autoline) {
        if ($line>$autoline && $line<=$autoline+3) {
          while ($autoline!=$line) {
            print "\n";
            ++$autoline;
          }
        } else {
          # print "# $line \"$file\"\n";
          print "# $line\n";
          $autoline = $line;
          $autofile = $file;
        }
      }
    }
    print "$LINE\n";
    ++$autoline;
  }
}