Subversion Repositories tropofold.echam

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 michaesp 1
! F2KCLI : Fortran 200x Command Line Interface
2
! copyright Interactive Software Services Ltd. 2001
3
! For conditions of use see manual.txt
4
!
5
! Platform    : Unix/Linux
6
! Compiler    : Any Fortran 9x compiler supporting IARGC/GETARG
7
!               which counts the first true command line argument
8
!               after the program name as argument number one.
9
!               (Excludes compilers which require a special USE
10
!               statement to make IARGC/GETARG available).
11
! To compile  : f90 -c f2kcli.f90
12
!               (exact compiler name will vary)
13
! Implementer : Lawson B. Wakefield, I.S.S. Ltd.
14
! Date        : February 2001
15
!
16
     MODULE MO_F2KCLI
17
#if defined (NAG)
18
        USE f90_unix
19
!#else
20
!        EXTERNAL  :: GETARG, IARG, IARGC
21
#endif
22
!        INTRINSIC :: LEN_TRIM, PRESENT, LEN
23
!        PRIVATE   :: LEN_TRIM, PRESENT, LEN
24
!        INTEGER   :: IARGC
25
!
26
      CONTAINS
27
!
28
      SUBROUTINE GET_COMMAND(COMMAND,LENGTH,STATUS)
29
!
30
! Description. Returns the entire command by which the program was
31
!   invoked.
32
!
33
! Class. Subroutine.
34
!
35
! Arguments.
36
! COMMAND (optional) shall be scalar and of type default character.
37
!   It is an INTENT(OUT) argument. It is assigned the entire command
38
!   by which the program was invoked. If the command cannot be
39
!   determined, COMMAND is assigned all blanks.
40
! LENGTH (optional) shall be scalar and of type default integer. It is
41
!   an INTENT(OUT) argument. It is assigned the significant length
42
!   of the command by which the program was invoked. The significant
43
!   length may include trailing blanks if the processor allows commands
44
!   with significant trailing blanks. This length does not consider any
45
!   possible truncation or padding in assigning the command to the
46
!   COMMAND argument; in fact the COMMAND argument need not even be
47
!   present. If the command length cannot be determined, a length of
48
!   0 is assigned.
49
! STATUS (optional) shall be scalar and of type default integer. It is
50
!   an INTENT(OUT) argument. It is assigned the value 0 if the
51
!   command retrieval is sucessful. It is assigned a processor-dependent
52
!   non-zero value if the command retrieval fails.
53
!
54
      CHARACTER(LEN=*), INTENT(OUT), OPTIONAL :: COMMAND
55
      INTEGER         , INTENT(OUT), OPTIONAL :: LENGTH
56
      INTEGER         , INTENT(OUT), OPTIONAL :: STATUS
57
!
58
      INTEGER                   :: IARG,NARG,IPOS
59
      INTEGER            , SAVE :: LENARG
60
      CHARACTER(LEN=2000), SAVE :: ARGSTR
61
      LOGICAL            , SAVE :: GETCMD = .TRUE.
62
!
63
! Under Unix we must reconstruct the command line from its constituent
64
! parts. This will not be the original command line. Rather it will be
65
! the expanded command line as generated by the shell.
66
!
67
      IF (GETCMD) THEN
68
          NARG = IARGC()
69
          IF (NARG > 0) THEN
70
              IPOS = 1
71
              DO IARG = 1,NARG
72
                CALL GETARG(IARG,ARGSTR(IPOS:))
73
                LENARG = LEN_TRIM(ARGSTR)
74
                IPOS   = LENARG + 2
75
                IF (IPOS > LEN(ARGSTR)) EXIT
76
              END DO
77
          ELSE
78
              ARGSTR = ' '
79
              LENARG = 0
80
          ENDIF
81
          GETCMD = .FALSE.
82
      ENDIF
83
      IF (PRESENT(COMMAND)) COMMAND = ARGSTR
84
      IF (PRESENT(LENGTH))  LENGTH  = LENARG
85
      IF (PRESENT(STATUS))  STATUS  = 0
86
      RETURN
87
      END SUBROUTINE GET_COMMAND
88
!
89
      INTEGER FUNCTION COMMAND_ARGUMENT_COUNT()
90
!
91
! Description. Returns the number of command arguments.
92
!
93
! Class. Inquiry function
94
!
95
! Arguments. None.
96
!
97
! Result Characteristics. Scalar default integer.
98
!
99
! Result Value. The result value is equal to the number of command
100
!   arguments available. If there are no command arguments available
101
!   or if the processor does not support command arguments, then
102
!   the result value is 0. If the processor has a concept of a command
103
!   name, the command name does not count as one of the command
104
!   arguments.
105
!
106
      COMMAND_ARGUMENT_COUNT = IARGC()
107
      RETURN
108
      END FUNCTION COMMAND_ARGUMENT_COUNT
109
!
110
      SUBROUTINE GET_COMMAND_ARGUMENT(NUMBER,VALUE,LENGTH,STATUS)
111
!
112
! Description. Returns a command argument.
113
!
114
! Class. Subroutine.
115
!
116
! Arguments.
117
! NUMBER shall be scalar and of type default integer. It is an
118
!   INTENT(IN) argument. It specifies the number of the command
119
!   argument that the other arguments give information about. Useful
120
!   values of NUMBER are those between 0 and the argument count
121
!   returned by the COMMAND_ARGUMENT_COUNT intrinsic.
122
!   Other values are allowed, but will result in error status return
123
!   (see below).  Command argument 0 is defined to be the command
124
!   name by which the program was invoked if the processor has such
125
!   a concept. It is allowed to call the GET_COMMAND_ARGUMENT
126
!   procedure for command argument number 0, even if the processor
127
!   does not define command names or other command arguments.
128
!   The remaining command arguments are numbered consecutively from
129
!   1 to the argument count in an order determined by the processor.
130
! VALUE (optional) shall be scalar and of type default character.
131
!   It is an INTENT(OUT) argument. It is assigned the value of the
132
!   command argument specified by NUMBER. If the command argument value
133
!   cannot be determined, VALUE is assigned all blanks.
134
! LENGTH (optional) shall be scalar and of type default integer.
135
!   It is an INTENT(OUT) argument. It is assigned the significant length
136
!   of the command argument specified by NUMBER. The significant
137
!   length may include trailing blanks if the processor allows command
138
!   arguments with significant trailing blanks. This length does not
139
!   consider any possible truncation or padding in assigning the
140
!   command argument value to the VALUE argument; in fact the
141
!   VALUE argument need not even be present. If the command
142
!   argument length cannot be determined, a length of 0 is assigned.
143
! STATUS (optional) shall be scalar and of type default integer.
144
!   It is an INTENT(OUT) argument. It is assigned the value 0 if
145
!   the argument retrieval is sucessful. It is assigned a
146
!   processor-dependent non-zero value if the argument retrieval fails.
147
!
148
! NOTE
149
!   One possible reason for failure is that NUMBER is negative or
150
!   greater than COMMAND_ARGUMENT_COUNT().
151
!
152
      INTEGER         , INTENT(IN)            :: NUMBER
153
      CHARACTER(LEN=*), INTENT(OUT), OPTIONAL :: VALUE
154
      INTEGER         , INTENT(OUT), OPTIONAL :: LENGTH
155
      INTEGER         , INTENT(OUT), OPTIONAL :: STATUS
156
!
157
!  A temporary variable for the rare case case where LENGTH is
158
!  specified but VALUE is not. An arbitrary maximum argument length
159
!  of 1000 characters should cover virtually all situations.
160
!
161
      CHARACTER(LEN=1000) :: TMPVAL
162
!
163
! Possible error codes:
164
! 1 = Argument number is less than minimum
165
! 2 = Argument number exceeds maximum
166
!
167
      IF (NUMBER < 0) THEN
168
          IF (PRESENT(VALUE )) VALUE  = ' '
169
          IF (PRESENT(LENGTH)) LENGTH = 0
170
          IF (PRESENT(STATUS)) STATUS = 1
171
          RETURN
172
      ELSE IF (NUMBER > IARGC()) THEN
173
          IF (PRESENT(VALUE )) VALUE  = ' '
174
          IF (PRESENT(LENGTH)) LENGTH = 0
175
          IF (PRESENT(STATUS)) STATUS = 2
176
          RETURN
177
      END IF
178
!
179
! Get the argument if VALUE is present
180
!
181
      IF (PRESENT(VALUE)) CALL GETARG(NUMBER,VALUE)
182
!
183
! The LENGTH option is fairly pointless under Unix.
184
! Trailing spaces can only be specified using quotes.
185
! Since the command line has already been processed by the
186
! shell before the application sees it, we have no way of
187
! knowing the true length of any quoted arguments. LEN_TRIM
188
! is used to ensure at least some sort of meaningful result.
189
!
190
      IF (PRESENT(LENGTH)) THEN
191
          IF (PRESENT(VALUE)) THEN
192
              LENGTH = LEN_TRIM(VALUE)
193
          ELSE
194
              CALL GETARG(NUMBER,TMPVAL)
195
              LENGTH = LEN_TRIM(TMPVAL)
196
          END IF
197
      END IF
198
!
199
! Since GETARG does not return a result code, assume success
200
!
201
      IF (PRESENT(STATUS)) STATUS = 0
202
      RETURN
203
      END SUBROUTINE GET_COMMAND_ARGUMENT
204
!
205
    END MODULE MO_F2KCLI