        SUBROUTINE ZX_GET_PARAMETER(n,string,length)                 
C
C----------------------------------------------------------------------
C
C.IDENTIFICATION: Subroutine Z_GET_PARAMETER
C.LIBRARY:        GENERAL (this is NOT a VOS routine !!!)
C.AUTHOR:         L.Chiappetti - IFCTR Milano
C.VERSIONS:       0.0 - 21 May 92 - Test version
C                 1.0 - 30 Oct 92 - unchanged                 
C.PURPOSE:        Retrieve one runstring argument                        
C.METHOD:         does its own parsing
C.SYNTAX:         CALL ZX_GET_PARAMETER(n,string,length)              
C.PARAMETERS:     INTEGER   n      : n-th parameter is wished (in)
C                 CHARACTER string : the returned string
C                 INTEGER   length : the length of the string 
C.RESTRICTIONS:   
C.NOTES:          THIS VERSION SHOULD WORK IDENTICALLY IN VMS AND UNIX
C.FILES:          none
C.REFERENCES      Z_GET_COMMAND in the VOS library 
C
C----------------------------------------------------------------------
C
C      INCLUDE   'implicit_none.inc'
       CHARACTER STRING*(*)
       INCLUDE   'runstring.inc'
       CHARACTER RUNSTRING*(MAXRUN)
       INTEGER   n,length
       INTEGER   TRUE_LENGTH,NTIMES,L,NCOMMA,I,KSTART,KEND,NCHAR,N1
       LOGICAL   QUOTED
       INCLUDE   'voscommon.inc'
C      allowed separators are blank, tab and comma
       CHARACTER BLANK,TAB,COMMA,QUOTE,PLACEHOLDER
       PARAMETER (BLANK=' ',TAB=CHAR(8),COMMA=',',QUOTE='''')
C      for truly stupid fortrans, just in case
       SAVE NTIMES,RUNSTRING,PLACEHOLDER
C
C     First time only, store the runstring here
C
       DATA NTIMES/0/
       LENGTH=0
       STRING=' '
       IF(NTIMES.EQ.0)THEN
         NTIMES=1
         CALL Z_GET_COMMAND(RUNSTRING)
C        write(*,*)runstring
         IF(VOSCOMMON_ERROR.NE.0)RETURN
         CALL GET_GLOBAL_DEFAULT('PLACEHOLDER','.',PLACEHOLDER)
C        write(*,*)' passed ggd ',placeholder
C        write(*,*)runstring
       ENDIF
C
       VOSCOMMON_ERROR=0
       VOSCOMMON_SYSTEMERROR=0
       IF(N.LE.0)    RETURN
C
C-----looks for commas or other separator-------------------------------
C
      L=TRUE_LENGTH(RUNSTRING)
      NCOMMA=0
      QUOTED=.FALSE.
      DO 111 I=1,L  
C     replace unconditionally tabs with whitespace
C     should this be done in Z_GET_COMMAND ?
      IF(RUNSTRING(I:I).EQ.TAB)RUNSTRING(I:I)=BLANK
C     toggle quoted flag if quote character encountered
      IF(RUNSTRING(I:I).EQ.QUOTE)QUOTED=.NOT.QUOTED    
      IF(QUOTED)GOTO 111
C     look if a separator is found
      IF(RUNSTRING(I:I).EQ.BLANK.OR.
     .   RUNSTRING(I:I).EQ.COMMA)THEN
C        a sequence of a whitespace followed by a comma or a comma
C        followed by a whitespace must be ignored  ...
C        but a sequence of comma-whitespace-comma has to be taken
C        as identifiying a "missing" parameter
         IF( (RUNSTRING(I:I+1)  .EQ.BLANK//COMMA        
     .   .OR. RUNSTRING(I:I+1)  .EQ.COMMA//BLANK)       
     .   .AND.RUNSTRING(I-1:I+1).NE.COMMA//BLANK//COMMA)GOTO 111
         NCOMMA=NCOMMA+1
         IF(NCOMMA.EQ.N)THEN
            KSTART=I+1
            GOTO 112
         ENDIF
      ENDIF
 111  CONTINUE
C     LENGTH=0 already
      RETURN
C     Parameter was not found
C
C.....Looks for end of parameter........................................
C
 112  CONTINUE
C     QUOTED=.FALSE.  already by definition
      DO 113 I=KSTART,L       
C     toggle quoted flag if quote character encountered
      IF(RUNSTRING(I:I).EQ.QUOTE)QUOTED=.NOT.QUOTED    
      IF(QUOTED)GOTO 113
      IF(RUNSTRING(I:I).EQ.BLANK.OR.
     .   RUNSTRING(I:I).EQ.COMMA)THEN
         KEND=I-1
         GOTO 114
      ENDIF
 113  CONTINUE
C     if no last separator found take up to last non blank
      KEND=L
C
C-----Verify if string found--------------------------------------------
C
 114  CONTINUE
      IF(KEND.LT.KSTART)THEN
C       LENGTH=0  already
        RETURN
C       Parameter not found (was null, like in case of ,,)
      ELSE
        NCHAR=KEND-KSTART+1
        LENGTH=NCHAR
        N1 = LEN(STRING)
        IF(NCHAR.GT.N1)THEN
C         only N1 chars transmitted (maximum)  but no error is generated
          NCHAR=N1
          KEND=NCHAR+KSTART-1
        ENDIF
C       LENGTH=NCHAR  should this be moved here ?
        STRING=RUNSTRING(KSTART:KEND)
      ENDIF
C
C     special cases to be handled as null arguments
C   
C     a quoted string entirely enclosing all blanks
      IF(STRING(1:1).EQ.QUOTE.AND.STRING(LENGTH:LENGTH).EQ.QUOTE)THEN
        IF(TRUE_LENGTH(STRING(2:LENGTH-1)).EQ.0)THEN
          LENGTH=0
          STRING=' '
        ENDIF
      ENDIF
C
C     a null argument explicitly specified as such using a placeholder
      IF(STRING.EQ.PLACEHOLDER)THEN
        LENGTH=0
        STRING=' '
      ENDIF 
      RETURN
      END
