[comp.os.os9] Wanted: Acces of the commandline

3017_411@uwovax.uwo.ca (Karl-Michael Schindler) (03/10/89)

Hi there,

	I need some hints to acces the Commandline.  The system is OS/9 68K 
and I have only a Fortran Compiler.  I couldn't find any hints.  Am I getting 
blind?  Thanks in advance  -  Michael.

Karl - Michael Schindler <3017_411@uwovax.uwo.ca>

piner@pur-phy (Richard Piner) (03/16/89)

In article <1832@uwovax.uwo.ca> 3017_411@uwovax.uwo.ca (Karl-Michael Schindler) writes:
>Hi there,
>
>	I need some hints to acces the Commandline.  The system is OS/9 68K 
>and I have only a Fortran Compiler.  I couldn't find any hints.  Am I getting 
>blind?  Thanks in advance  -  Michael.
>
>Karl - Michael Schindler <3017_411@uwovax.uwo.ca>

If you have FORTRAN, you also have the assembler. So here is some
assembly code to get at command line arguments from FORTRAN.
Note "svarg" must be the FIRST executable statement in your program.
-------------------------------------------------------------------
	nam	args
*****************************************************************
*	routines to handle argument passing from OS9
*	when a program starts up
*
*	interesting facts
*
*	after fstart finishes (at the begining of _main)
*	d1 has the address of pointers to important strings
*
*			d2 = number of argument on command line (not always)
*			d1 > address of argument 1 (the program name)	
*			a2 > address of argument 2 (the first parameter)
*				 address of second arg
*				 etc.
*				 00000000	list terminated by zero long
*	environ(a6) > address of first env table entry
*				  address of second env table entry
*				  etc.
*				  00000000   list terminated by zero long
*
*	author  Richard Piner
*
*****************************************************************
	use	/h0/defs/defsfile

*Edition	equ	1
*Typ_Lang	set	0
*Attr_Rev	set	(ReEnt<<8)+ReEntBit

	psect	ARGS_a,0,0,0,0,0
	vsect
argadd	dc.l	0
nargs	dc.l	0
helpst	dc.b	"-?"
	ends
*************************************************************
*	call svarg -or- if(svarg()) .......
*	call as the first executable statement in the program
*	if svarg is called as a logical function, it returns .true.
*	if the first argument of the command line is "-?".
*************************************************************
	ttl	SVARG_
SVARG_:	move.l	d1,argadd(a6)	save it
*
*	add code to count the arguments
*
	move.l	d1,a0		copy the address of the pointer list to a0
	moveq.l	#0,d2		set argument count to 0
svlp	addq.l	#1,d2	increment the counter
	cmpi.l	#0,(a0)+	check for a zero pointer
	bne	svlp			if not zero, loop
	subq.l	#1,d2		don't count the program name as an arguemnt
*
	move.b	d2,nargs(a6)	save arg count
	cmpi.b	#2,d2			check number of arguments
	bmi	nohlp				not enough argument, no help
	move.l	d1,a0		pointer to array of pointers to arguments
	move.l	(a0)+,a2	address of first argument (name of program)
	move.l	(a0),a2		address of second argument
	lea.l	helpst(a6),a1	get the address of the string "-?"
	cmpm.w	(a1)+,(a2)+	test word character
	bne		nohlp		no match, no help
	move.l	#1,d0		return true
	rts
nohlp
	move.l	#0,d0		return false
	rts					ALL done

*********************************************************
*	n=iargc()
*	n=long integer
*	number of argument from command line
*********************************************************
	ttl	IARGC_
IARGC_:	clr.l	d0	clear register
	move.b	nargs(a6),d0	recall nargs

*	move.l	environ(a6),a0		get address of second argument

	rts

*********************************************************
*	ier=getarg(n,arg)
*	n=number of command line argument wanted
*	arg=destination string (be sure you have enough room)
*	ier=0 if ok, =-1 if an error
*********************************************************
	ttl	GETARG_
GETARG_:	movem.l	a0-a2/d1-d2,-(sp)	save registers

	move.l	(a0),d2		get number of argument requested
	clr.l	d0
	move.b	nargs(a6),d0	recall number args
	cmp.l	d0,d2		compare two number
	ble.s	getarg	ok, get args
	move.l	#-1,d0		set error flag
	bra.s				getret
getarg
	move.l	argadd(a6),a0	get pointer to args
	move.l	d2,d0			move arg count to d0
	move.l	d1,a1			copy address of destination
getlop
	move.l	(a0)+,a2		get address of string
	sub.l	#1,d0			decrement count
	bne.s	getlop			loop till zero
getlop2
	move.b	(a2),(a1)		copy a byte at a time
	tst.b	(a2)+			zero byte yet?
	beq.s	getret			yes, get out
	add.l	#1,a1			increment pointers
	bra.s	getlop2			loop for next byte
getret
	movem.l	(sp)+,a0-a2/d1-d2	restore register
	rts
	ends