	SUBROUTINE STRKEY(A)
C
C       This function stores keywords and parameters in a local buffer when
C  a .OPTION is called.  This function is called by MIIPSOPT, GETCOM,
C  SAVE, and USRINP.
C
C		Version 2, Donald H. Gudehus, January 4, 1998
C		Changed STRKEY from FUNCTION to SUBROUTINE and commented
C		out STRKEY=1.
C		Version 3, Donald H. Gudehus, October 15, 2001
C		Dimensioned C as C*132
C		Version 4, D. H. G., September 20, 2002
C		Increased size of C to 2048 to cover variables of
C		large dimensionA
C		Version 5, D. H. G., February 5, 2008
C		Further increased the size of B and C to 4096
C
	CHARACTER *(*) A,KEY
	PARAMETER (NBUF=4096) !  Size of B
	CHARACTER B*4096,C*4096
	COMMON /BKEY/ C
C
C  Copy parameters to buffer B
	N=LEN(A)
C  N is the number of characters entered as an option plus optional 
C  parameters in MIIPSOPT.FOR counting from and including last character
C  of the option, irrespective of the number of characters in the option.
C  A normal OPTION or a .OPTION call gives N=1.
	IF (N.LE.2) THEN !  Empty parameter list
	  B='()'
	ELSE
	  B=A
	END IF
C	STRKEY=1
	RETURN
C
C
	ENTRY ADDKEY(A)
C
C       This entry adds parameters for a single option, contained in A, 
C  and which came from the SAVE file, to any possible optional new
C  parameters (if a .OPTION was called with new keyword values) already
C  in the list in buffer B.  This entry is called by RESTR after each
C  record is read from the SAVE file and which in turn is called by
C  MIIPSOPT or similar programs.
C
C	NB=INDEX(B,')') !  Present length
C  Find the last ')' in the string
	NST=1
	NB=0
	NB2=1
	DO WHILE (NB2.NE.0)
	  NB=INDEX(B(NST:),')')+NB
	  NST=NB
	  NB2=INDEX(B(NST+1:),')')
	  NST=NB2+NB+1
	  NB=NB+NB2
	END DO
C  Check to see if last input was a key word without a value.  This can
C  happen when a default is taken
	IF (B((NB-1):(NB-1)).EQ.'=') THEN  
	  B=B(:NB-1)//A//')' !  IF SO DO NOT PUT A SPACE BETWEEN THE INPUTS
	ELSE
	  B=B(:NB-1)//' '//A//')' !  Adds new string
	END IF
	NB=INDEX(B,')')
	IF (NB.GT.NBUF.OR.NB.EQ.0) THEN
	  CALL ERROR(-3,'PARAMETER BUFFER HAS OVERFLOWED')
	END IF
C	PRINT *, B,' B IN ADDKEY'
	RETURN
C
C
	ENTRY GETKEY(KEY,NL)
C
C       This entry finds the keyword in the local buffer B and returns the
C  associated parameter list as a character string in C in the COMMON block
C  BKEY.  NL is -1 on return if KEY is not present otherwise it is the length
C  of the string.  This entry is called by USRINP.
C
	NL=-1
	NA=INDEX(B,KEY) !  Position of Keyword;  0 if not present, a 3 if
			!  present
	IF (NA.EQ.0) RETURN !  Not present
	N1=INDEX(B(NA:),'=')+NA !  Position of start of parameters
C  Now find end of parameter list
	NB=INDEX(B(N1:),'=') !  Position of next '=' relative to start of
			     !  parameters
	IF (KEY.EQ.'Expression='.AND.B(N1+NB-2:N1+NB-2).EQ.'>') THEN
		!  In the option AR the parameters have an '=', but this can
		!  be identified by 'Expression' and '>'
	  NB=INDEX(B(NB+N1:),'=')+NB
	END IF
	IF (NB.EQ.0) THEN !  This was the last keyword
C	  N2=INDEX(B,')')-1 !  OK if only one ')'
C  Find the last ')' in the string
	  NST=1
	  N2=0
	  N22=1
	  DO WHILE (N22.NE.0)
	    N2=INDEX(B(NST:),')')+N2
	    NST=N2
	    N22=INDEX(B(NST+1:),')')
	    NST=N22+N2+1
	    N2=N2+N22
	  END DO
	  N2=N2-1	  
	ELSE  ! Searches for the preceeding separator
	  NB=NB+N1-1
	  N3=N1
   10	  N2=N3-1
	  N3=INDEX(B(N3+1:),' ')+N3 !  Next space
	  IF (N3.LT.NB) GO TO 10 !  Not far enough yet
	END IF
	NL=N2-N1+1
	C=B(N1:N2)
C	PRINT *, C,' C IN GETKEY'
	IF (NL.EQ.0) RETURN !  Dont clear if keyword only
	B=B(:NA-2)//B(N2+1:) !  Squeeze out these parameters
C	print *, B,' B IN GETKEY'
	RETURN
	END
