home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!pipex!unipalm!uknet!mcsun!sunic!hagbard!mefos!pam
- From: pam@mefos.se (PA Monwall)
- Newsgroups: comp.lang.fortran
- Subject: Re: Unix ARGC,ARGV in Fortran 77
- Message-ID: <1848@mefos.se>
- Date: 18 Aug 92 14:23:49 GMT
- References: <14542@borg.cs.unc.edu>
- Organization: MEFOS, Sweden
- Lines: 284
-
- marshall@marshall.cs.unc.edu (Jonathan A. Marshall) writes:
- : I need to write a Fortran program that will accept command-line arguments.
- : I'm running Ultrix f77 on a DECstation 5000. In C, command-line arguments
- : can be passed with argc and argv.
- :
-
- I got this from the net some months ago......
-
- In article <JEFF.92Mar8195119@sparc23.cs.tamu.edu> Jeff@cs.tamu.edu writes:
- >Any of you guys know if it is possible (any way) to get the command line
- >arguments into a FORTRAN program. I'd like to know about ways this
- >could be done on Sun's and Cray's but (unfortunately) I'm teaching a
- >class on VAXes and using VAX FORTRAN, so that's what I was really
- >shooting for. Although VAX FORTRAN seems to have a lot added to it, I
- >couldn't find any information about recovery of the program invoking
- >args, nor do I know anyone who knows how...
- >
-
- The quick and dirty way:
-
- CHARACTER*255 LINE
-
-
- CALL LIB$GET_FOREIGN( LINE,,LENGTH )
-
-
- type *, line(:length)
-
- end
-
-
- $ for bla.for
- $ link bla
- $ blabla :== $disk:[dir]bla
- $ blabla this is one argument to that program bla
-
- This gives you the whole line as typed in variable LINE, well, after DCL did
- its tricks and you get the double quotes as well. In other words, you have to
- do the parsing yourself. Look up $HELP RTL LIB LIB$GET_FOREIGN
-
- The better method: Use a CLD file and define real DCL commands. More
- complicated but gives nicer interface:
-
- First define your commands in file MONGO.CLD:
-
- module mongo_cld ! this name is used in cli$dcl_parse, see below
- define verb mongo
- parameter p1,value(type=$infile)
- ! ^^^^^^^^ only says I want a file name
- ! you may omit the whole ,value(...)
- ! or set VALUE(REQUIRED) if you always
- ! require a parameter
- ! similar for more parameters
- qualifier terminal, value(required)
-
-
- This defines a command with one optional parameter and an optional switch
- /TERMINAL which must have a value like /term=77.
-
-
- Then write a program that makes use of it:
-
- character*255 cmd_line, infile
- integer cli$dcl_parse, cli$present, cli$get_value
-
- call lib$get_foreign( cmd_line,, len )
-
- status = cli$dcl_parse( 'mongo '//cmd_line(:len), mongo_CLD )
- ^^^^^
- * this is what you defined after VERB and
- * is actually not really used here but must
- * be present
- * ^^^^^^^^^
- * This is what is
- * after MODULE
- if( .not.status ) call exit( status )
-
- if( cli$present('terminal') ) then
- *
- * Check if switch /TERMINAL is given. There is also a cli$absent
- *
- status = cli$get_value( 'terminal', infile )
- if( .not.status ) call exit( status )
-
- * do something with infile which holds the value assigned to
- * terminal like
- * /terminal=77 as a charcater string
-
- end if
- if( cli$present('p1') ) then
- *
- * First parameter given?
- *
- status = cli$get_value( 'p1', infile )
- if( .not.status ) call exit( status )
- *
- * do something with first parameter
- *
- end if
-
- end
-
- $ FOR BLA
- $ SET COMMAND/OBJECT MONGO.CLD
- $ LINK BLA, MONGO
- $ BLABLA :== $DISK:[DIR]BLA
-
- $ BLABLA/TERM=66 hugo.txt
-
-
- Note you can't start the program with $ RUN in either case. You must define
- a symbol and don't forget that $. For a full description of the CLD business
- you want to look into the manual should you have access to the grey wall. You
- can do much more.
-
- ------------------
-
- In article <JEFF.92Mar8195119@sparc23.cs.tamu.edu>,
- jeff@sparc23.cs.tamu.edu (Jeff Goldberg) writes:
-
- > Any of you guys know if it is possible (any way) to get the command line
- > arguments into a FORTRAN program. I'd like to know about ways this
-
- The following routine comes from the book "Fortran Tools for VAX/VMS and
- MS-DOS" by Russell K. Jones and Tracy Crabtree, John Wiley and Sons, New
- York (1988), ISBN 0-471-61976-0. This book is superb in my inexperienced
- opinion, and has increased my Fortran productivity enormously by teaching
- me how to write solid and reusable subroutines.
-
- ------------
- ! getargs - get command line arguments and argument count, VMS specific
-
- subroutine getargs(argc,argv)
- integer argc !argument count
- character*(*) argv(*) !array of arguments
- include 'global.def'
- include '($clidef)'
- character*(MAXSTR) line !local string for parsing
- integer*2 length !total length of command line
- integer i,k
-
- call cli$get_value('$LINE',line,length) ! VMS
- i = 1
- argc = 0
-
- do while (line(i:i) .ne. BLANK) !skip the command invocation
- i = i + 1
- end do
- i = i + 1 !skip the space after command
-
- do while (i .le. length)
-
- k = i
-
- if (line(i:i) .eq. DQUOTE) then
- argc = argc + 1
- i = i + 1 !skip the quote mark
- k = i
- do while (i .ne. length .and. line(i:i) .ne. DQUOTE)
- i = i + 1
- end do
- argv(argc) = line(k:(i-1))//NULL !skip trailing quote
- i = i + 1 !move past quote
- else if (line(i:i) .eq. BLANK) then
- i = i + 1
- else
- argc = argc + 1
- do while (i .le. length .and. line(i:i) .ne. BLANK)
- i = i + 1
- end do
- argv(argc) = line(k:i-1)//NULL
- end if
-
- end do
-
- return
- end
-
- ------------------
-
- ! global.def - global symbolic constants for tools
-
-
- ! maximum character string lengths and string termination
- character*1 NULL
- parameter (NULL = char(0))
- integer MAXLINE
- parameter (MAXLINE = 132)
- integer MAXSTR
- parameter (MAXSTR = 255)
-
- ! file i/o parameters
- integer MAXOPEN
- parameter (MAXOPEN = 30)
- character*1 NEWLINE
- parameter (NEWLINE = char(10))
- character*1 NL ! NewLine
- parameter (NL = char(10))
- character*1 EOF
- parameter (EOF = char(26))
- character*2 EOL
- parameter (EOL = NEWLINE//NULL)
- ! standard units
- integer STDIN
- parameter (STDIN = 5)
- integer STDOUT
- parameter (STDOUT = 6)
- integer STDERR
- parameter (STDERR = 7)
- ! i/o unit access codes
- integer IOERROR
- parameter (IOERROR = -1)
- integer IOREAD
- parameter (IOREAD = -2)
- integer IOWRITE
- parameter (IOWRITE = -3)
- integer IOAPPEND
- parameter (IOAPPEND = -4)
- integer IOFORTRAN
- parameter (IOFORTRAN = -5)
-
- ! command line arguments
- integer MAXARGS
- parameter (MAXARGS = 10)
- character*1 QUALIFIER
- parameter (QUALIFIER = '-')
-
- ! preprocessor buffer size
- integer PPLINESIZE
- parameter (PPLINESIZE = 2048)
- ! other standard definitions
- character*1 COMMENT
- parameter (COMMENT = '!')
- character*1 ESCAPE
- parameter (ESCAPE = '\')
- character*1 WILDCARD
- parameter (WILDCARD = '*')
- character*1 SKIPCARD
- parameter (SKIPCARD = '~')
- integer ENDLIST
- parameter (ENDLIST = -2147483647) ! -(2**31)
- integer YES
- parameter (YES = 1)
- integer NO
- parameter (NO = 0)
- integer BAD
- parameter (BAD = -1)
-
- ! ascii characters
- character*1 BELL
- parameter (BELL = char(7))
- character*1 BACKSPACE
- parameter (BACKSPACE = char(8))
- character*1 TAB
- parameter (TAB = char(9))
- character*1 LINEFEED
- parameter (LINEFEED = char(10))
- character*1 FORMFEED
- parameter (FORMFEED = char(12))
- character*1 CR
- parameter (CR = char(13))
- character*1 ESC
- parameter (ESC = char(27))
- character*1 BLANK
- parameter (BLANK = char(32))
- character*1 APOSTROPHE
- parameter (APOSTROPHE = char(39)) !' '
- character*1 DQUOTE
- parameter (DQUOTE = char(34)) !" "
- character*1 QUOTE
- parameter (QUOTE = APOSTROPHE)
-
- --------
-
- Maybe it can be of some help.
-
- --PA
-
- ----------------------------!------------------------------------------------
- /////// /////// // // ! PA Monwall E-mail: pam@mefos.se
- // // // // /////// ! Foundation for Metallurgical Research (MEFOS)
- /////// /////// // / // ! Box 812, S-951 28 Lulea, Sweden
- // // // // // ! Tel: +46 920 55640 Home : +46 920 221581
- // // // // // ! Fax: +46 920 55832 Telex: 80482 MEFOS S
-