fpt and WinFPT Reference Manual - Command-line Commands

| SimCon Home | Ref Manual Home |

Formatting Fortran Code - Pretty Printing

fpt reads Fortran code in:

  1. Fixed 72-column format
  2. Fixed format with code lines up to 140 characters long
  3. Tab format with 72 column lines
  4. Tab format with lines up to 140 characters long
  5. Free format

fpt always reformats the code. The layout (Free-format, tab-format or fixed-format), the case of keywords and symbols and the spacing are under the control of the user. The commands which control the format, (including some reformatting and restructuring commands used in code migration) are listed below.

We recommend that users set up their own preferred format as follows:

  1. Choose a small code with which to experiment.
  2. Run fpt interactively on this code - e.g. $ fpt my_code.f %i
  3. Use the WINDOW, TYPE, UP and DOWN commands to examine the code.
  4. Test the formatting commands shown below and display the code again until you are happy with the format.
  5. Save the formatting rules with the SAVE FORMAT command, e.g. FPT> save format my_format.fsp
  6. Re-run fpt on the example code, using your new formatting file, e.g. FPT> quit Final exit from interact [yes/no]: y $ fpt my_code.f my_format.fsp %i
  7. Examine the code again to check that everything is as expected
  8. Optionally, copy the file my_format.fsp to the main fpt installation directory and add a reference to it in config.fsp in place of the formatting file already there. (Please see the configuration file for a description of this file).

An example of setting up a format is shown after the command descriptions below.

 
Code Formatting Commands

Layout

The commands are: FIXED FORMAT OUTPUT, FREE FORMAT OUTPUT and TAB FORMAT OUTPUT.

In fixed format, a code line begins with a label field of 5 characters, which are always spaces if there is no statement label. Column 6 contains the line continuation character if this is present. The continuation character may be any printing character except "0". Line comments are delimited by the characters "C", "c", "*" or "!".

In free format there is no reserved label field. Line continuation characters are written at the end of the line to be continued and are always "&" characters. A continuation character is written at the start of a continuation line if a long token such as a character string is split by the continuation. Line comments are deliited by the character "!". This is the recommended code layout because it avoids the risk of a number of potential coding errors. Free format is supported by all Fortran 90 systems and by gnu g77.

In tab format code the label field is delimited by a tab character. The continuation character must be numeric and may not be "0" and is written after the tab. Line comments are delimited by the characters "C", "c", "*" or "!". This format is non-standard but is supported by many extended Fortran 77 and Fortran 90 systems.

Line Length

OUTPUT CODE LINE LENGTH sets the length of the output code line. Tab characters are counted as the number of columns required to reach the next tab stop at 8 character intervals. Comments may extend beyond this length to a maximum of 140 characters.

Indentation

The command INDENT sets the number of spaces to indent code for each level of nesting.

MAXIMUM INDENTATION and MAXIMUM NUMBER OF SPACES TO INDENT set the maximum levels of indentation, either as the number of levels or the number of spaces.

INDENT COMMENTS specifies whether line comments are to be indented at the same level as the code.

INDENT CONTINUATION LINES specifies an additional number of spaces to indent continuation lines.

INDENT LABELS specifies a leading indentation for statement labels in fixed or tab layout.

INDENT SUBPROGRAM CODE specifies a leading indentation for the code within a sub-program.

Header Comments

[RE]FORMAT HEADER and INDENT HEADERS specify whether header comments are to be indented with the code or transcribed unchanged. Header comments are comments which precede or follow the sub-program statement and are written before the first declaration or executable statement within a sub-program.

Upper and Lower Case

LOWER CASE and UPPER CASE specify the upper or lower case for Fortran keywords, intrinsic functions, symbols and operators.

SYMBOLS AS FIRST WRITTEN specifies that user-defined symbols (such as variable names) are written with the upper and lower case organisation found when they are first encountered.

Spacing between tokens

NO SPACES removes all unnecessary spacing. Spaces are inserted only between two consecutive alphanumeric tokens and between alphanumeric tokens and dot-delimited tokens such as .AND..

SPACE BEFORE and SPACE AFTER specifies whether a space character is to precede or follow a specific keyword or operator, variable names, intrinsic function names or numbers.

SPACED OPERATORS sets the spacing rules for all operators and delimiters except comma characters and underscore characters written in kind tags (Spaces between numbers and kind tags are illegal).

WRITE CONTINUATION CHARACTER IN COLUMN ... specifies the column in which the continuation character is to be written in free format. Spaces are inserted before the continuation character.

Continuation Line Handling

KEEP CONTINUATION LINES and IGNORE CONTINUATION LINES specifies whether the pattern of continuation lines in the original source is to be retained.

MAXIMUM NUMBER OF CONTINUATION LINES specifies the maximum number of continuation lines. Statements with too many continuations are compressed by suspending the continuation line and spacing rules, or are split into multiple statements.

CONTINUATION CHARACTER sets the continuation character for fixed format and tab format code.

COLUMN FORMAT CONDITIONALS re-formats IF and DO WHILE statements with the component tests aligned in columns.

COLUMN FORMAT DECLARATIONS reformats declaration statements with one object declared on each line.

Tab Characters

(Note: These commands apply after the leading tab character if code is written in tab format)

KEEP EMBEDDED TABS causes all tab characters embedded in the code line to be retained.

CHANGE EMBEDDED TABS TO SPACES causes tabs embedded in the code line to be replaced by spaces, keeping the original tab-stop positions of the tokens which follow the tabs.

REMOVE EMBEDDED TABS removes all embedded tabs and the tokens then follow the token spacing rules set in the SPACE BEFORE and SPACE AFTER commands.

Writing Numbers

REAL NUMBERS AS WRITTEN and COMPLETE REAL NUMBERS specify whether leading and trailing "0" characters are to be added to real numbers which start or end with decimal point characters.

HEX and Octal Numbers

CHANGE HEX NUMBERS and CHANGE OCTAL NUMBERS specify formats for writing HEX and octal numbers.

Code Re-structuring

CHANGE MULTIPLE ASSIGNMENTS changes multiple assignment statements to separate statements.

CHANGE DO CONTINUE TO DO ENDDO changes the trailing CONTINUE statement to ENDDO and removes the statement label (Unless it is referenced by a LEAVE statement.

CHANGE IF TO IF THEN changes IF statements with executable sub-statements to IF-THEN constructs.

SEPARATE DO AND GOTO LABELS changes the code where a GOTO construct ends on the closing label of a DO loop. An additional statement label is inserted and the constructs are separated.

Fortran FORMAT specifications

FORMAT FORMAT SPECIFICATIONS specifies whether the tokens within FORMAT specifications are to be formatted according to the spacing rules or transcribed unchanged.

Comments

REMOVE COMMENTS AFTER END removes line comments which lie between the last END statement in a file and the end of the file. These have been observed to cause problems on some systems.

TRAILING COMMENTS optionally converts trailing comments to line comments. Some old compilers do not support trailing comments.

Symbol Names and Label Numbers

Note: The states of these commands are preserved by the SAVE SETTINGS command, not by the SAVE FORMAT command.

RENUMBER LABELS systematically renumbers all statement labels.

RENAME renames user-defined symbols such as variable names and sub-program names.

Migration

CHANGE TRAILING COMMENT DELIMITER changes the C language trailing comment delimiter "/*" to "!". The C-style delimiter is accepted by some systems.

Obfuscation

HIDE NAMES and OBFUSCATE remove comments and changes names to meaningless strings.

Displaying Code to Test Your Format

WINDOW sets the range of code lines to be displayed.

TYPE types the reformatted code.

UP and DOWN moves up and down through the file or sub-program.

Displaying and Saving the Format

SHOW FORMAT shows the current code formatting rules.

SAVE FORMAT saves the current formatting rules to a file.

SAVE SETTINGS saves all of the current settings, including the format rules, to file.

 
Setting up a Format - An Example

We choose a sample code. The subroutine GOLLUM. The original code is shown below.

SUBROUTINE GOLLUM COMMON /C/L,A,B,I,NA,IV,I7,I8,N,DISTP,AJUST,MIN,ISTAT,JTK,KOENIG, * IGNORE,IO,IGOL(80) COMMON /L/IENTR(4),IENTC(4),IKLNR(8),IKLNC(8),IGLER(25),IGLEC(25), * IBASR(2),IBASC(2),LI2(4),LI2R(5),LI2C(5),IGO(4),MINES COMMON /U/LAUNCH,NUMOUT,NUME(2) IF(NUMOUT.EQ.0)RETURN WRITE(L,302) 302 FORMAT(' EAGLES-'/) I7=0 DO 301 I=1,LAUNCH IF(IGLER(I).EQ.0)GO TO 301 I7=I7+1 WRITE(L,303)I,IGLEC(I),IGLER(I) 303 FORMAT('+',I2,' - (',I2,',',I2,') '$) IF(I7.NE.5)GO TO 301 I7=0 WRITE(L,304) 304 FORMAT(/' '$) 301 CONTINUE WRITE(L,304) RETURN END

Note that GOLLUM is written in fixed format, there is no indentation and everything is in upper case.

We open the code under fpt (Under Linux)

$ fpt gollum.f %i ------------------------------------------------------------------------------ WARNING Number 2517 Severity 1 (Worst 0) Count 1 ANSI FORTRAN 77 intrinsic(s) used as local identifier(s) See Listing File or FORTRAN output for occurrences. ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ NOTE Number 2447 Severity 0 (Worst 1) Count 2 No main program encountered ------------------------------------------------------------------------------ FPT>

Not a good start - there are intrinsic function names used for variables!

We type the code to see what the current default formatting will do:

FPT> t 1:10 1 SUBROUTINE GOLLUM 2 COMMON /C/ 2 1 L 2 1 , A 2 1 , B 2 1 , I 2 1 , NA 2 1 , IV 2 1 , I7 2 1 , I8 2 1 , N 2 1 , DISTP 2 1 , AJUST 2 1 , MIN !-----------^---------------------------------------------------------- fpt - 2517 ANSI FORTRAN 77 intrinsic used as a local identifier !---------------------------------------------------------------------- 2 1 , ISTAT 2 1 , JTK 2 1 , KOENIG 3 1 , IGNORE 3 1 , IO Page>

The current default formatting formats declarations in columns, with one object declared on each line. Not helpful with this code so we will turn this feature off and try again:

FPT> FPT> no column format FPT> type 1:10 1 SUBROUTINE GOLLUM 2 COMMON /C/L,A,B,I,NA,IV,I7,I8,N,DISTP,AJUST,MIN,ISTAT,JTK,KOENIG, !----------------------------------------------------^----------------- fpt - 2517 ANSI FORTRAN 77 intrinsic used as a local identifier !---------------------------------------------------------------------- 3 1 IGNORE,IO,IGOL(80) 4 COMMON /L/IENTR(4),IENTC(4),IKLNR(8),IKLNC(8),IGLER(25),IGLEC(25), 5 1 IBASR(2),IBASC(2),LI2(4),LI2R(5),LI2C(5),IGO(4),MINES 6 COMMON /U/LAUNCH,NUMOUT,NUME(2) 7 IF (NUMOUT .EQ. 0) RETURN 8 WRITE (L,302) 9 302 FORMAT (' EAGLES-'/) 10 >> I7=0 FPT>

Better! Now we get rid of the variable name MIN. This is a maintenance trap. We rename it and suppress the error message embedded in the code:

FPT> FPT> rename min gollum_min FPT> suppress error 2517 *** NOTE *** The following errors have been suppressed 2517 ANSI FORTRAN 77 intrinsic(s) used as local identifier(s) FPT> t 1:10 1 SUBROUTINE GOLLUM 2 COMMON /C/L,A,B,I,NA,IV,I7,I8,N,DISTP,AJUST,GOLLUM_MIN,ISTAT,JTK, 3 1 KOENIG,IGNORE,IO,IGOL(80) 4 COMMON /L/IENTR(4),IENTC(4),IKLNR(8),IKLNC(8),IGLER(25),IGLEC(25), 5 1 IBASR(2),IBASC(2),LI2(4),LI2R(5),LI2C(5),IGO(4),MINES 6 COMMON /U/LAUNCH,NUMOUT,NUME(2) 7 IF (NUMOUT .EQ. 0) RETURN 8 WRITE (L,302) 9 302 FORMAT (' EAGLES-'/) 10 >> I7=0 FPT>

Note that MIN has been renamed. We are still using fixed format, and everything is still upper case. We can change that:

FPT> free format FPT> lower case symbols FPT> t 1:10 1 SUBROUTINE gollum 2 COMMON /c/l,a,b,i,na,iv,i7,i8,n,distp,ajust,gollum_min,istat, & 3 jtk,koenig,ignore,io,igol(80) 4 COMMON /l/ientr(4),ientc(4),iklnr(8),iklnc(8),igler(25),iglec( & 5 25),ibasr(2),ibasc(2),li2(4),li2r(5),li2c(5),igo(4),mines 6 COMMON /u/launch,numout,nume(2) 7 IF (numout .EQ. 0) RETURN 8 WRITE (l,302) 9 302 FORMAT (' EAGLES-'/) 10 >> i7=0 FPT>

Better still, but perhaps a little cramped. Try spacing out the operators:

FPT> spaced operators FPT> t 1:10 1 SUBROUTINE gollum 2 COMMON / c / l , a , b , i , na , iv , i7 , i8 , n , distp , & 3 ajust , gollum_min , istat , jtk , koenig , ignore , io , & 3 igol ( 80 ) 4 COMMON / l / ientr ( 4 ) , ientc ( 4 ) , iklnr ( 8 ) , iklnc ( & 5 8 ) , igler ( 25 ) , iglec ( 25 ) , ibasr ( 2 ) , ibasc ( 2 ) & 5 ,li2 ( 4 ) , li2r ( 5 ) , li2c ( 5 ) , igo ( 4 ) , mines 6 COMMON / u / launch , numout , nume ( 2 ) 7 IF ( numout .EQ. 0 ) RETURN 8 WRITE ( l , 302 ) 9 302 FORMAT ( ' EAGLES-'/) 10 >> i7 = 0 FPT>

There is too much space around the commas. Also, in free format we can make the lines longer. Type the whole file this time:

FPT> output code line length: 90 FPT> write continuation char in col 89 FPT> no space before "," FPT> t 1:25 1 SUBROUTINE gollum 2 COMMON / c / l, a, b, i, na, iv, i7, i8, n, distp, ajust, gollum_min, istat, jtk & 3 ,koenig, ignore, io, igol ( 80 ) 4 COMMON / l / ientr ( 4 ), ientc ( 4 ), iklnr ( 8 ), iklnc ( 8 ), igler ( 25 ), & 5 iglec ( 25 ), ibasr ( 2 ), ibasc ( 2 ), li2 ( 4 ), li2r ( 5 ), li2c ( 5 ), igo & 5 (4 ), mines 6 COMMON / u / launch, numout, nume ( 2 ) 7 IF ( numout .EQ. 0 ) RETURN 8 WRITE ( l, 302 ) 9 302 FORMAT ( ' EAGLES-'/) 10 i7 = 0 11 DO 301 i = 1, launch 12 IF ( igler ( i ) .EQ. 0 ) GOTO 301 13 i7 = i7 + 1 14 WRITE ( l, 303 ) i, iglec ( i ), igler ( i ) 15 303 FORMAT ( '+', I2, ' - (', I2, ',', I2, ') '$) 16 IF ( i7 .NE. 5 ) GOTO 301 17 i7 = 0 18 WRITE ( l, 304 ) 19 304 FORMAT ( /' '$) 20 301 CONTINUE 21 WRITE ( l, 304 ) Page> 22 RETURN 23 END ****** <EOF> ****** FPT>

When it stops at a Page> prompt we just type a carriage control.

We don't like the IF statement at line 7 and the labels are out of order.

FPT> change if to if then FPT> renumber labels FPT> t 1:25 1 SUBROUTINE gollum 2 COMMON / c / l, a, b, i, na, iv, i7, i8, n, distp, ajust, gollum_min, istat, jtk & 3 ,koenig, ignore, io, igol ( 80 ) 4 COMMON / l / ientr ( 4 ), ientc ( 4 ), iklnr ( 8 ), iklnc ( 8 ), igler ( 25 ), & 5 iglec ( 25 ), ibasr ( 2 ), ibasc ( 2 ), li2 ( 4 ), li2r ( 5 ), li2c ( 5 ), igo & 5 (4 ), mines 6 COMMON / u / launch, numout, nume ( 2 ) 7 IF ( numout .EQ. 0 ) THEN 8 RETURN 8 ENDIF 8 WRITE ( l, 1000 ) 9 1000 FORMAT ( ' EAGLES-'/) 10 i7 = 0 11 DO 1030 i = 1, launch 12 IF ( igler ( i ) .EQ. 0 ) THEN 13 GOTO 1030 13 ENDIF 13 i7 = i7 + 1 14 WRITE ( l, 1010 ) i, iglec ( i ), igler ( i ) 15 1010 FORMAT ( '+', I2, ' - (', I2, ',', I2, ') '$) 16 IF ( i7 .NE. 5 ) THEN 17 GOTO 1030 Page> 17 ENDIF 17 i7 = 0 18 WRITE ( l, 1020 ) 19 1020 FORMAT ( /' '$) 20 1030 CONTINUE 21 WRITE ( l, 1020 ) 22 RETURN 23 END ****** <EOF> ****** FPT>

Good enough for now! We renamed a variable, so simply saving the format isn't enough. We save the settings:

FPT> save settings gollum_settings.fsp FPT> exit Final exit from interact [yes/no]: y Output the FORTRAN Code [yes/no]: y $

Here is the file which we created:

!H!**************************************************************************** !H! File: gollum_settings.fsp !H! Output by fpt 3.8-b I-Linux On 03-APR-14 At 16:47:30 !H!**************************************************************************** % CONFIGURATION SETTINGS ! ====================== ! Control of fpt % COMMAND CONTINUATION CHARACTER: "-" ! Diagnostics % SUPPRESS ERROR 2517 ! ANSI FORTRAN 77 intrinsic(s) used as local identifier(s) ! Input Code Characteristics % DO NOT TRUNCATE NAMES % KIND MODULE NAME: MODULE_KINDS % DO NOT CHANGE LOGICAL SIZE % DO NOT CHANGE INTEGER SIZE % DO NOT CHANGE REAL SIZE % DO NOT CHANGE COMPLEX SIZE % DO NOT CHANGE BYTE TO INTEGER % DO NOT CHANGE DOUBLE PRECISION TO REAL % DO NOT CHANGE DOUBLE COMPLEX TO COMPLEX ! Intrinsic Functions % ACCEPT ALL INTRINSICS ! Filters and Control of Reports % WINDOW 4:4 % LINE NUMBERING ON ! Systematic Changes % CHANGE IF TO IF - THEN % RENUMBER LABELS % RENAME SYMBOL: "MIN" "GOLLUM_MIN" ! FORTRAN Output Files % PRIMARY OUTPUT FILE NAME EXTENSION: "FPT" % INCLUDE OUTPUT FILE NAME EXTENSION: "FPI" ! Format ! ------ ! Free / Fixed / Tab format %FREE FORMAT OUTPUT %OUTPUT CODE LINE LENGTH: 90 ! Indentation %DO NOT INDENT HEADER COMMENTS %INDENT COMMENTS %INDENT SUB-PROGRAM CODE BY: 8 COLUMNS %INDENT EACH LEVEL OF NESTING BY: 3 COLUMNS %MAXIMUM INDENTATION NESTING LEVEL: 8 %INDENT LABELS BY: 0 COLUMNS %INDENT CONTINUATION LINES BY: 1 COLUMNS ! Continuation lines %WRITE CONTINUATION CHARACTER IN COLUMN: 89 %CONTINUATION CHARACTER :"1" %KEEP CONTINUATION LINES ! Upper or lower case %UPPER CASE KEYWORDS %LOWER CASE SYMBOLS %UPPER CASE INTRINSICS %UPPER CASE OPERATORS %LOWER CASE FILE NAMES ! Spacing before symbols, intrinsics, numbers etc. %NO SPACE BEFORE SYMBOLS %NO SPACE BEFORE INTRINSICS %NO SPACE BEFORE NUMBERS %NO SPACE BEFORE OCTALS %NO SPACE BEFORE LABELS %NO SPACE BEFORE STRINGS %NO SPACE BEFORE FORMAT TOKENS %NO SPACE BEFORE TRAILING COMMENTS ! Spacing after symbols, intrinsics, numbers etc. %NO SPACE AFTER SYMBOLS %NO SPACE AFTER INTRINSICS %NO SPACE AFTER NUMBERS %NO SPACE AFTER OCTALS %NO SPACE AFTER LABELS %NO SPACE AFTER STRINGS %NO SPACE AFTER FORMAT TOKENS %SPACE AFTER CONTINUATIONS ! Spacing before keywords %NO SPACE BEFORE KEYWORDS %SPACE BEFORE "FUNCTION" %SPACE BEFORE "CALL" %SPACE BEFORE "GO" %SPACE BEFORE "GOTO" %SPACE BEFORE "PAUSE" %SPACE BEFORE "RETURN" %SPACE BEFORE "STOP" %SPACE BEFORE "ASSIGN" ! Spacing before auxiliary keywords %SPACE BEFORE "THEN" ! Spacing after keywords %NO SPACE AFTER KEYWORDS %SPACE AFTER "PARAMETER" %SPACE AFTER "COMMON" %SPACE AFTER "STRUCTURE" %SPACE AFTER "RECORD" %SPACE AFTER "EQUIVALENCE" %SPACE AFTER "DATA" %SPACE AFTER "NAMELIST" %SPACE AFTER "WHILE" %SPACE AFTER "ELSEIF" %SPACE AFTER "END" %SPACE AFTER "GOTO" %SPACE AFTER "IF" %SPACE AFTER "PAUSE" %SPACE AFTER "RETURN" %SPACE AFTER "STOP" %SPACE AFTER "ACCEPT" %SPACE AFTER "ENCODE" %SPACE AFTER "DECODE" %SPACE AFTER "PRINT" %SPACE AFTER "READ" %SPACE AFTER "TYPE" %SPACE AFTER "WRITE" %SPACE AFTER "BACKSPACE" %SPACE AFTER "CLOSE" %SPACE AFTER "DELETE" %SPACE AFTER "ENDFILE" %SPACE AFTER "INQUIRE" %SPACE AFTER "OPEN" %SPACE AFTER "REWIND" %SPACE AFTER "UNLOCK" %SPACE AFTER "OPTIONS" %SPACE AFTER "FORMAT" ! Spacing after auxiliary keywords %SPACE AFTER "TYPE" %SPACE AFTER "END" %SPACE AFTER "OPEN" ! Spacing before operators %SPACE BEFORE OPERATORS %NO SPACE BEFORE ".NOT." %NO SPACE BEFORE "," %NO SPACE BEFORE "_" ! Spacing after operators %SPACE AFTER OPERATORS %NO SPACE AFTER "_" ! Real numbers %COMPLETE REAL NUMBERS ! Hex and Octal numbers %DO NOT CHANGE HEX NUMBERS %DO NOT CHANGE OCTAL NUMBERS ! Column format %NO COLUMN FORMAT DECLARATIONS %NO COLUMN FORMAT CONDITIONALS ! Comments %TRAILING COMMENTS %DO NOT CHANGE COMMENT DELIMITER ! Tabs within code and before comments %KEEP EMBEDDED TABS ! Special cases %DO NOT WRITE STOP STATEMENTS ON ONE LINE % END OF CONFIGURATION SETTINGS ! End of gollum_settings.fsp

Copyright ©1995 to 2024 Software Validation Ltd. All rights reserved.