home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / fortran / 3090 < prev    next >
Encoding:
Internet Message Format  |  1992-08-19  |  9.1 KB

  1. Path: sparky!uunet!pipex!unipalm!uknet!mcsun!sunic!hagbard!mefos!pam
  2. From: pam@mefos.se (PA Monwall)
  3. Newsgroups: comp.lang.fortran
  4. Subject: Re: Unix ARGC,ARGV in Fortran 77
  5. Message-ID: <1848@mefos.se>
  6. Date: 18 Aug 92 14:23:49 GMT
  7. References: <14542@borg.cs.unc.edu>
  8. Organization: MEFOS, Sweden
  9. Lines: 284
  10.  
  11. marshall@marshall.cs.unc.edu (Jonathan A. Marshall) writes:
  12. : I need to write a Fortran program that will accept command-line arguments.
  13. : I'm running Ultrix f77 on a DECstation 5000.  In C, command-line arguments
  14. : can be passed with argc and argv.
  15.  
  16. I got this from the net some months ago......
  17.  
  18. In article <JEFF.92Mar8195119@sparc23.cs.tamu.edu> Jeff@cs.tamu.edu writes:
  19. >Any of you guys know if it is possible (any way) to get the command line
  20. >arguments into a FORTRAN program.  I'd like to know about ways this
  21. >could be done on Sun's and Cray's but (unfortunately) I'm teaching a
  22. >class on VAXes and using VAX FORTRAN, so that's what I was really
  23. >shooting for.  Although VAX FORTRAN seems to have a lot added to it, I
  24. >couldn't find any information about recovery of the program invoking
  25. >args, nor do I know anyone who knows how...
  26. >
  27.  
  28. The quick and dirty way:
  29.  
  30.     CHARACTER*255 LINE
  31.  
  32.  
  33.     CALL LIB$GET_FOREIGN( LINE,,LENGTH )
  34.  
  35.  
  36.     type *, line(:length)
  37.  
  38.     end
  39.  
  40.  
  41. $ for bla.for
  42. $ link bla
  43. $ blabla :== $disk:[dir]bla
  44. $ blabla this is one argument to that program bla
  45.  
  46. This gives you the whole line as typed in variable LINE, well, after DCL did
  47. its tricks and you get the double quotes as well.  In other words, you have to
  48. do the parsing yourself.  Look up $HELP RTL LIB LIB$GET_FOREIGN
  49.  
  50. The better method:  Use a CLD file and define real DCL commands.  More
  51. complicated but gives nicer interface:
  52.  
  53. First define your commands in file MONGO.CLD:
  54.  
  55. module mongo_cld    ! this name is used in cli$dcl_parse, see below
  56. define verb mongo
  57.     parameter p1,value(type=$infile)
  58. !                               ^^^^^^^^ only says I want a file name
  59. !                                        you may omit the whole ,value(...)
  60. !                                        or set VALUE(REQUIRED) if you always
  61. !                                        require a parameter
  62. !       similar for more parameters
  63.     qualifier terminal, value(required)
  64.  
  65.  
  66. This defines a command with one optional parameter and an optional switch
  67. /TERMINAL which must have a value like /term=77.
  68.  
  69.  
  70. Then write a program that makes use of it:
  71.  
  72.     character*255 cmd_line, infile
  73.     integer cli$dcl_parse, cli$present, cli$get_value
  74.  
  75.     call lib$get_foreign( cmd_line,, len )
  76.     
  77.     status = cli$dcl_parse( 'mongo '//cmd_line(:len), mongo_CLD )
  78.                                  ^^^^^
  79. *                                this is what you defined after VERB and
  80. *                                is actually not really used here but must
  81. *                                be present               
  82. *                                                         ^^^^^^^^^
  83. *                                                         This is what is 
  84. *                                                         after MODULE
  85.     if( .not.status ) call exit( status )
  86.  
  87.     if( cli$present('terminal') ) then
  88. *
  89. *          Check if switch /TERMINAL is given.  There is also a cli$absent
  90. *
  91.        status = cli$get_value( 'terminal', infile )
  92.        if( .not.status ) call exit( status )
  93.  
  94. *       do something with infile which holds the value assigned to
  95. *       terminal like
  96. *       /terminal=77 as a charcater string
  97.  
  98.     end if
  99.     if( cli$present('p1') ) then
  100. *
  101. *          First parameter given?
  102. *
  103.        status = cli$get_value( 'p1', infile )
  104.        if( .not.status ) call exit( status )
  105. *
  106. *          do something with first parameter
  107. *
  108.     end if
  109.  
  110.     end
  111.  
  112. $ FOR BLA
  113. $ SET COMMAND/OBJECT MONGO.CLD
  114. $ LINK BLA, MONGO
  115. $ BLABLA :== $DISK:[DIR]BLA
  116.  
  117. $ BLABLA/TERM=66 hugo.txt
  118.  
  119.  
  120. Note you can't start the program with $ RUN in either case.  You must define
  121. a symbol and don't forget that $.  For a full description of the CLD business
  122. you want to look into the manual should you have access to the grey wall.  You
  123. can do much more.
  124.  
  125. ------------------
  126.  
  127. In article <JEFF.92Mar8195119@sparc23.cs.tamu.edu>,
  128. jeff@sparc23.cs.tamu.edu (Jeff Goldberg) writes:
  129.  
  130. > Any of you guys know if it is possible (any way) to get the command line
  131. > arguments into a FORTRAN program.  I'd like to know about ways this
  132.  
  133. The following routine comes from the book "Fortran Tools for VAX/VMS and
  134. MS-DOS" by Russell K. Jones and Tracy Crabtree, John Wiley and Sons, New
  135. York (1988), ISBN 0-471-61976-0. This book is superb in my inexperienced
  136. opinion, and has increased my Fortran productivity enormously by teaching
  137. me how to write solid and reusable subroutines. 
  138.  
  139. ------------
  140. ! getargs  -  get command line arguments and argument count, VMS specific
  141.  
  142.       subroutine getargs(argc,argv)
  143.       integer            argc            !argument count 
  144.       character*(*)      argv(*)         !array of arguments
  145.       include            'global.def'
  146.       include            '($clidef)'
  147.       character*(MAXSTR) line            !local string for parsing
  148.       integer*2          length          !total length of command line
  149.       integer            i,k      
  150.       
  151.       call cli$get_value('$LINE',line,length)    ! VMS
  152.       i = 1
  153.       argc = 0
  154.       
  155.       do while (line(i:i) .ne. BLANK)  !skip the command invocation
  156.          i = i + 1
  157.       end do
  158.       i = i + 1                     !skip the space after command
  159.  
  160.       do while (i .le. length)
  161.  
  162.         k = i
  163.  
  164.         if (line(i:i) .eq. DQUOTE) then 
  165.           argc = argc + 1
  166.           i = i + 1               !skip the quote mark
  167.           k = i
  168.           do while (i .ne. length .and. line(i:i) .ne. DQUOTE)
  169.             i = i + 1
  170.           end do
  171.           argv(argc) = line(k:(i-1))//NULL   !skip trailing quote
  172.           i = i + 1                           !move past quote
  173.         else if (line(i:i) .eq. BLANK) then
  174.           i = i + 1
  175.         else
  176.           argc = argc + 1
  177.           do while (i .le. length .and. line(i:i) .ne. BLANK)
  178.              i = i + 1
  179.           end do
  180.           argv(argc) = line(k:i-1)//NULL
  181.         end if
  182.  
  183.       end do
  184.  
  185.       return
  186.       end
  187.  
  188. ------------------
  189.  
  190. ! global.def  -  global symbolic constants for tools
  191.  
  192.  
  193. ! maximum character string lengths and string termination
  194.       character*1   NULL
  195.       parameter     (NULL = char(0))
  196.       integer       MAXLINE
  197.       parameter     (MAXLINE = 132)
  198.       integer       MAXSTR
  199.       parameter     (MAXSTR = 255)
  200.  
  201. !  file i/o parameters
  202.       integer       MAXOPEN
  203.       parameter     (MAXOPEN = 30)
  204.       character*1   NEWLINE
  205.       parameter     (NEWLINE = char(10))
  206.       character*1   NL    ! NewLine
  207.       parameter     (NL = char(10))
  208.       character*1   EOF
  209.       parameter     (EOF = char(26))
  210.       character*2   EOL
  211.       parameter     (EOL = NEWLINE//NULL)
  212. !  standard units
  213.       integer       STDIN
  214.       parameter     (STDIN = 5)
  215.       integer       STDOUT
  216.       parameter     (STDOUT = 6)
  217.       integer       STDERR
  218.       parameter     (STDERR = 7)
  219. !  i/o unit access codes
  220.       integer       IOERROR
  221.       parameter     (IOERROR = -1)
  222.       integer       IOREAD
  223.       parameter     (IOREAD = -2)
  224.       integer       IOWRITE
  225.       parameter     (IOWRITE = -3)
  226.       integer       IOAPPEND
  227.       parameter     (IOAPPEND = -4)
  228.       integer       IOFORTRAN
  229.       parameter     (IOFORTRAN = -5)
  230.  
  231. !  command line arguments
  232.       integer       MAXARGS
  233.       parameter     (MAXARGS = 10)
  234.       character*1   QUALIFIER
  235.       parameter     (QUALIFIER = '-')
  236.  
  237. !  preprocessor buffer size
  238.       integer       PPLINESIZE
  239.       parameter     (PPLINESIZE = 2048)
  240. !  other standard definitions
  241.       character*1   COMMENT
  242.       parameter     (COMMENT = '!')
  243.       character*1   ESCAPE
  244.       parameter     (ESCAPE = '\')
  245.       character*1   WILDCARD
  246.       parameter     (WILDCARD = '*')
  247.       character*1   SKIPCARD
  248.       parameter     (SKIPCARD = '~')
  249.       integer       ENDLIST
  250.       parameter     (ENDLIST = -2147483647)   ! -(2**31)
  251.     integer        YES
  252.     parameter    (YES = 1)
  253.     integer        NO
  254.     parameter    (NO = 0)
  255.     integer        BAD
  256.     parameter    (BAD = -1)
  257.  
  258. !  ascii characters
  259.       character*1   BELL
  260.       parameter     (BELL = char(7))
  261.       character*1   BACKSPACE
  262.       parameter     (BACKSPACE = char(8))
  263.       character*1   TAB
  264.       parameter     (TAB = char(9))
  265.       character*1   LINEFEED
  266.       parameter     (LINEFEED = char(10))
  267.       character*1   FORMFEED
  268.       parameter     (FORMFEED = char(12))
  269.       character*1   CR
  270.       parameter     (CR = char(13))
  271.       character*1   ESC
  272.       parameter     (ESC = char(27))
  273.       character*1   BLANK
  274.       parameter     (BLANK = char(32))
  275.       character*1   APOSTROPHE
  276.       parameter     (APOSTROPHE = char(39))  !' '
  277.       character*1   DQUOTE
  278.       parameter     (DQUOTE = char(34))      !" "
  279.       character*1   QUOTE
  280.       parameter     (QUOTE = APOSTROPHE)
  281.  
  282. --------
  283.  
  284. Maybe it can be of some help.
  285.  
  286. --PA
  287.  
  288. ----------------------------!------------------------------------------------
  289.     /////// /////// //   // ! PA Monwall               E-mail: pam@mefos.se
  290.    //   // //   // ///////  ! Foundation for Metallurgical Research (MEFOS)
  291.   /////// /////// // / //   ! Box 812, S-951 28 Lulea, Sweden
  292.  //      //   // //   //    ! Tel: +46 920 55640  Home : +46 920 221581
  293. //      //   // //   //     ! Fax: +46 920 55832  Telex: 80482 MEFOS S 
  294.