! !$Author$ !$Date$ !$Revision$ !$HeadURL$ ! ! F2KCLI : Fortran 200x Command Line Interface ! copyright Interactive Software Services Ltd. 2001 ! For conditions of use see manual.txt ! ! Platform : Unix/Linux ! Compiler : Any Fortran 77 compiler supporting the following: ! (a) Versions of IARGC/GETARG which count the ! first true command line argument after the ! program name as argument number one. ! (b) Long routine names containing underscores ! To compile : f77 -c f2kcli.f ! (exact compiler name will vary) ! Implementer : Lawson B. Wakefield, I.S.S. Ltd. ! Date : February 2001 ! INTEGER FUNCTION COMMAND_ARGUMENT_COUNT() ! ! Description. Returns the number of command arguments. ! ! Class. Inquiry function ! ! Arguments. None. ! ! Result Characteristics. Scalar default integer. ! ! Result Value. The result value is equal to the number of command ! arguments available. If there are no command arguments available ! or if the processor does not support command arguments, then ! the result value is 0. If the processor has a concept of a command ! name, the command name does not count as one of the command ! arguments. ! INTEGER iargc COMMAND_ARGUMENT_COUNT = iargc() RETURN END ! SUBROUTINE GET_COMMAND(COMMAND,LENGTH,STATUS) ! ! Description. Returns the entire command by which the program was ! invoked. ! ! Class. Subroutine. ! ! Arguments. ! COMMAND (optional) shall be scalar and of type default character. ! It is an INTENT(OUT) argument. It is assigned the entire command ! by which the program was invoked. If the command cannot be ! determined, COMMAND is assigned all blanks. ! LENGTH (optional) shall be scalar and of type default integer. It is ! an INTENT(OUT) argument. It is assigned the significant length ! of the command by which the program was invoked. The significant ! length may include trailing blanks if the processor allows commands ! with significant trailing blanks. This length does not consider any ! possible truncation or padding in assigning the command to the ! COMMAND argument; in fact the COMMAND argument need not even be ! present. If the command length cannot be determined, a length of ! 0 is assigned. ! STATUS (optional) shall be scalar and of type default integer. It is ! an INTENT(OUT) argument. It is assigned the value 0 if the ! command retrieval is sucessful. It is assigned a processor-dependent ! non-zero value if the command retrieval fails. ! ! NOTE ! (1) The Fortran 77 implementation of this routine does not support ! optional arguments, so all arguments must be specified by the ! caller. ! CHARACTER*(*) COMMAND INTEGER LENGTH INTEGER STATUS ! INTEGER IARG,NARG,IPOS,LENARG CHARACTER*2000 ARGSTR LOGICAL GETCMD ! SAVE ARGSTR SAVE LENARG ! DATA GETCMD/.TRUE./ INTEGER iargc ! ! Under Unix we must reconstruct the command line from its constituent ! parts. This will not be the original command line. Rather it will be ! the expanded command line as generated by the shell. ! IF (GETCMD) THEN NARG = iargc() IF (NARG.GT.0) THEN IPOS = 1 DO 100 IARG = 1,NARG CALL getarg(IARG,ARGSTR(IPOS:)) LENARG = LEN_TRIM(ARGSTR) IPOS = LENARG + 2 IF (IPOS.GT.LEN(ARGSTR)) GOTO 200 100 CONTINUE ELSE ARGSTR = ' ' LENARG = 0 ENDIF 200 GETCMD = .FALSE. ENDIF COMMAND = ARGSTR LENGTH = LENARG STATUS = 0 RETURN END ! SUBROUTINE GET_COMMAND_ARGUMENT(NUMBER,VALUE,LENGTH,STATUS) ! ! Description. Returns a command argument. ! ! Class. Subroutine. ! ! Arguments. ! NUMBER shall be scalar and of type default integer. It is an ! INTENT(IN) argument. It specifies the number of the command ! argument that the other arguments give information about. Useful ! values of NUMBER are those between 0 and the argument count ! returned by the COMMAND_ARGUMENT_COUNT intrinsic. ! Other values are allowed, but will result in error status return ! (see below). Command argument 0 is defined to be the command ! name by which the program was invoked if the processor has such ! a concept. It is allowed to call the GET_COMMAND_ARGUMENT ! procedure for command argument number 0, even if the processor ! does not define command names or other command arguments. ! The remaining command arguments are numbered consecutively from ! 1 to the argument count in an order determined by the processor. ! VALUE (optional) shall be scalar and of type default character. ! It is an INTENT(OUT) argument. It is assigned the value of the ! command argument specified by NUMBER. If the command argument value ! cannot be determined, VALUE is assigned all blanks. ! LENGTH (optional) shall be scalar and of type default integer. ! It is an INTENT(OUT) argument. It is assigned the significant length ! of the command argument specified by NUMBER. The significant ! length may include trailing blanks if the processor allows command ! arguments with significant trailing blanks. This length does not ! consider any possible truncation or padding in assigning the ! command argument value to the VALUE argument; in fact the ! VALUE argument need not even be present. If the command ! argument length cannot be determined, a length of 0 is assigned. ! STATUS (optional) shall be scalar and of type default integer. ! It is an INTENT(OUT) argument. It is assigned the value 0 if ! the argument retrieval is sucessful. It is assigned a ! processor-dependent non-zero value if the argument retrieval fails. ! ! NOTE ! (1) One possible reason for failure is that NUMBER is negative or ! greater than COMMAND_ARGUMENT_COUNT(). ! (2) The Fortran 77 implementation of this routine does not support ! optional arguments, so all arguments must be specified by the ! caller. ! INTEGER NUMBER CHARACTER*(*) VALUE INTEGER LENGTH INTEGER STATUS ! INTEGER IPOS !Compiler says not used INTEGER iargc INTEGER LEN_TRIMF2K ! ! Possible error codes: ! 1 = Argument number is less than minimum ! 2 = Argument number exceeds maximum ! IF (NUMBER.LT.0) THEN VALUE = ' ' LENGTH = 0 STATUS = 1 RETURN ELSE IF (NUMBER.GT.iargc()) THEN VALUE = ' ' LENGTH = 0 STATUS = 2 RETURN ENDIF ! ! Get the argument ! CALL getarg(NUMBER,VALUE) ! ! The LENGTH option is fairly pointless under Unix. ! Trailing spaces can only be specified using quotes. ! Since the command line has already been processed by the ! shell before the application sees it, we have no way of ! knowing the true length of any quoted arguments. ! Just find last non-blank character in string. ! LENGTH = LEN_TRIMF2K(VALUE) ! ! Since getarg does not return a result code, assume success ! STATUS = 0 RETURN END ! INTEGER FUNCTION LEN_TRIMF2K(STRING) ! ! Return actual length of supplied string, ! excluding trailing blanks or zero if blank. ! ! STRING = String to search ! CHARACTER*(*) STRING INTEGER IPOS ! DO 100 IPOS = LEN(STRING),1,-1 IF (STRING(IPOS:IPOS).NE.' ') THEN LEN_TRIMF2K = IPOS RETURN ENDIF 100 CONTINUE ! ! String is blank ! LEN_TRIMF2K = 0 RETURN END