[comp.sys.hp] argv, argc into Fortran or C subroutine?

bglenden@colobus.cv.nrao.edu (Brian Glendenning) (04/30/91)

I need to get command line arguments into a Fortran program that has
some C linked in, but no C main().

On other systems I have either
1) Used a Fortran library call or
2) Accessed an extern char** symbol from a C routine

Since the 720 short-term loaner has almost no documentation associated
with it, I don't know how to do either. Could some one let me know?
Thanks.

Brian
--
       Brian Glendenning - National Radio Astronomy Observatory
bglenden@nrao.edu          bglenden@nrao.bitnet          (804) 296-0286

john@hpucph.hp.com (John Damm Srensen) (05/06/91)

Unfortunately there is no suported interface to initialize the
Fortran environment from a C program.

But still there is away which on a 700/800 is like this:

main(argc,argv,envp)
int argc;
char **argv,**envp;
{
 FTN_SET_AR(argc,argv,envp);
 FTN_F_INIT();
 printf("Arg 1 : %s\n",argv[1]);
 /* From here on you may call Fortran routines which
    use Fortran I/O or Fortran intrinsics like getarg igetarg
*/
}

john@hpucph.hp.com Speaking for myself only.

jbc@hpcupt3.cup.hp.com (Jeff Caldwell) (05/07/91)

>I need to get command line arguments into a Fortran program that has
>some C linked in, but no C main().
>
>On other systems I have either
>1) Used a Fortran library call or
>2) Accessed an extern char** symbol from a C routine
>
>Since the 720 short-term loaner has almost no documentation associated
>with it, I don't know how to do either. Could some one let me know?
>Thanks.
>
>Brian

Brian, 
To my knowledge, on the HP 700 series machine, their are a number of ways 
to get ahold of the command line arguments from Fortran.  Below, I will 
describe two ways that I am familiar with: the PROGRAM statement and use 
of intrinsics (igetarg and iargc).

The PROGRAM statement can be used to access information from the command
line as follows:

	
        PROGRAM parm(arg1, arg2, arg3)
        CHARACTER*15 arg1, arg2, arg3
        INTEGER atoi    ! system conversion routine

        arg1(15:15) = char(0) ! Put nulls at the end of the strings for "atoi".
        arg2(15:15) = char(0)
        arg3(15:15) = char(0)
        iarg1 = atoi(arg1)    ! ATOI can be used if the arguments are numeric.
        iarg2 = atoi(arg2)    ! ATOI will convert ascii strings to integers.
        iarg3 = atoi(arg3)
        PRINT *, "INITIAL VALUES:", arg1, arg2, arg3
        PRINT *, "NUMERIC EQUIVALENT:", iarg1, iarg2, iarg3
        END

% f77 program.f
% a.out  10 20 30
 INITIAL VALUES:10            20            30
 NUMERIC EQUIVALENT: 10 20 30

%  a.out testing command args
 INITIAL VALUES:testing       command       args
 NUMERIC EQUIVALENT: 0 0 0

% a.out mixed 30.3 args
 INITIAL VALUES:mixed         30.3          args
 NUMERIC EQUIVALENT: 0 30 0


Simply using the program statement is fairly easy.  However, a more general 
way to access command-line arguments from series 700 Fortran is to use the
igetarg() and iargc() functions.  The igetarg() function requires 3 
arguments:
    igetarg(n, str, slen)

    where:
	n     is an integer specifying which command-line argument is
              requested.
	str   is a character variable that will contain the requested command-
	      line argument, padded with blanks on the end. 
	slen  is an integer specifying the maximum length of str, so that the 
	      routine knows how far to pad the string with blanks.

    note: the number of significant characters in "str" is returned as the
	  value of the function igetarg. -1 is returned if the argument
          doesn't exist.

    note: The name of the program being run is the zeroth argument.


    iargc() return the number of parameters actually present on the command-
	  line.

   Example:

        program args
        character*30 s
        integer numargs, length

        numargs = iargc()
        do 10, i = 0, numargs
           length = igetarg(i, s, 30)
           print *, "ARG ", i, " : ", s
   10   continue
        end

% f77 program.f
% a.out testing 1 2 3 of the program
 ARG  0 : a.out
 ARG  1 : testing
 ARG  2 : 1
 ARG  3 : 2
 ARG  4 : 3
 ARG  5 : of
 ARG  6 : the
 ARG  7 : program

			Good Luck,

				Jeff Caldwell

Disclaimer:  All information presented here is not an official statement of
H.P.  It is provided only for informative purposes and does not constitute
a guarantee from HP.  Only my opinion is presented here, not HP's ...

kgo@hpuerca.HP.COM (Kelly Oden) (05/07/91)

bglenden@colobus.cv.nrao.edu (Brian Glendenning) writes:

>I need to get command line arguments into a Fortran program that has
>some C linked in, but no C main().
>
>Since the 720 short-term loaner has almost no documentation associated
>with it, I don't know how to do either. Could some one let me know?

Try the following fortran intrinsics calls:

Function iargc() is integer type and returns the number of parameters 
on the command line.

igetarg(n, str, slen) ---called as function or subroutine
	where:
		n - integer specifying which command line arg to get
	      str - character variable that will contain the arg requested
	     slen - integer specifying max length of str(for padding).

  igetarg() returns the number of significant characters in str, otherwise -1.
  (when n=0, igetarg returns the program name)

This is a little sketchy but I hope it helps. 

bglenden@colobus.cv.nrao.edu (Brian Glendenning) (05/08/91)

In article <1340019@hpuerca.HP.COM> kgo@hpuerca.HP.COM (Kelly Oden) writes:
   bglenden@colobus.cv.nrao.edu (Brian Glendenning) writes:
   >I need to get command line arguments into a Fortran program that has
   >some C linked in, but no C main().
   >
   >Since the 720 short-term loaner has almost no documentation associated
   >with it, I don't know how to do either. Could some one let me know?

   Try the following fortran intrinsics calls:

   Function iargc() is integer type and returns the number of parameters 
   on the command line.

   igetarg(n, str, slen) ---called as function or subroutine

This indeed worked, once I realized that iargc() needed to be called
before igetarg (some setup, presumably).

Brian
--
       Brian Glendenning - National Radio Astronomy Observatory
bglenden@nrao.edu          bglenden@nrao.bitnet          (804) 296-0286

rd@hpdtczb.HP.COM (Dick Dowell) (05/08/91)

The "iargc" and "igetarg" solutions posted are good ones.  I have found
that some machines (notably the series 800, but that was a few years
ago) don't recognize igetarg.  By using the "guess method" I came upon
a call to "getarg" which is 
     call getarg(num,arg)
where "num" is the argument number, arg is character variable to get argument.
This works on Sun, and Apollo.  On the 800, I believe argument 1 is the
program name, on the others it is the first argument.  I use iargc on all
the machines to get the number of arguments (again, test this stuff before
using it).

Dick "compatibility?" Dowell (rd@dtc.hp.com)