C  THIS VERSION: 14/02/1994 AT 02:52:34 PM.
      PROGRAM MAINL
C
C  THIS IS THE DRIVER PROGRAM FOR RUNNING LANCELOT FOR IBM, VAX AND
C  UNIX SYSTEMS. IT OPENS AND CLOSES ALL THE FILES AND CALLS THE
C  ROUTINE WRKSP WHICH PARTITIONS THE WORKSPACE AND WHICH IN TURN
C  CALLS THE ROUTINE LANCE WHICH DOES ALL THE WORK.
C
      INTEGER          INPUT , IOUT  , IINRUN, IOUTSL, IOUT1L
      INTEGER          IINPB , IOUTPB, IALIVE
      LOGICAL          DEBUG , DEBUGF, DEBUGG, ALIVE
CUNIX CHARACTER * 64   PRBDAT, PRBSOL, ONELIN, PRBSAV, ALIVED
CWFC  CHARACTER * 64   PRBDAT, PRBSOL, ONELIN, PRBSAV, ALIVED
CVMS  CHARACTER * 64   PRBDAT, PRBSOL, ONELIN, PRBSAV, ALIVED
CS    EXTERNAL         SWRKSP
CD    EXTERNAL         DWRKSP
CIBM  EXTERNAL         XUFLOW, ERRSET
CDBUG external common_handler
C
C  SET PARAMETER VALUES.
C  SET DEBUG  TO .TRUE. TO PRINT ALL INPUT DATA.
C  SET DEBUGF TO .TRUE. TO CHECK ALL ELEMENT DERIVATIVES.
C  SET DEBUGG TO .TRUE. TO CHECK ALL GROUP DERIVATIVES.
C  SET IOUT TO THE REQUIRED STANDARD OUTPUT DEVICE.
C
      PARAMETER        ( DEBUGF = .FALSE., DEBUGG = .FALSE. )
      PARAMETER        ( DEBUG  = .FALSE. )
C     PARAMETER        ( DEBUG  = .TRUE. )
      PARAMETER        ( IOUT   = 6 )
CIBM  PARAMETER ( INPUT = 55, IINRUN = 56, IOUTSL = 57, IOUT1L = 58 )
CIBM  PARAMETER ( IINPB = 59, IOUTPB = 59,              IALIVE = 60 )
CUNIX PARAMETER ( INPUT = 55, IINRUN = 56, IOUTSL = 57, IOUT1L = 58 )
CUNIX PARAMETER ( IINPB = 59, IOUTPB =     IINPB      , IALIVE = 60 )
CWFC  PARAMETER ( INPUT = 55, IINRUN = 56, IOUTSL = 57, IOUT1L = 58 )
CWFC  PARAMETER ( IINPB = 59, IOUTPB =     IINPB      , IALIVE = 60 )
CVMS  PARAMETER ( INPUT = 55, IINRUN = 56, IOUTSL = 57, IOUT1L = 58 )
CVMS  PARAMETER ( IINPB = 59, IOUTPB =     IINPB      , IALIVE = 60 )
CDBUG integer i, ieee_handler
CDBUG i = ieee_handler( 'set', 'common', common_handler )
CDBUG if ( i .ne. 0 ) write(6,*)' could not establish fp signal handler'   
C
C  ** OPEN LANCELOT SPECIFICATION FILE FOR IBM SYSTEMS.
C
CIBM  OPEN( IINRUN, FORM = 'FORMATTED' )
CIBM  OPEN( INPUT , FORM = 'FORMATTED' )
CIBM  OPEN( IOUTSL, FORM = 'FORMATTED' )
CIBM  OPEN( IOUT1L, FORM = 'FORMATTED' )
CIBM  OPEN( IINPB , FORM = 'FORMATTED' )
CIBM  OPEN( IOUTPB, FORM = 'FORMATTED' )
CIBM  CALL ERRSET( 212, 256, - 1, 1 )
CIBM  CALL XUFLOW( 0 )
C
C  ** OPEN LANCELOT SPECIFICATION FILE FOR UNIX SYSTEMS.
C
CUNIX OPEN( IINRUN, FILE = 'SPEC.SPC', FORM = 'FORMATTED',
CUNIX*      STATUS = 'UNKNOWN'  )
C
C  BUILD DATA INPUT FILE NAME FOR UNIX SYSTEMS.
C
CUNIX PRBDAT = 'OUTSDIF.d'
CUNIX PRBSOL = 'SOLUTION.d'
CUNIX ONELIN = 'SUMMARY.d'
CUNIX PRBSAV = 'SAVEDATA.d'
CUNIX ALIVED = 'ALIVE.d'
C
C  ** OPEN LANCELOT SPECIFICATION FILE FOR WATCOM + MSDOS
C
CWFC  OPEN( IINRUN, FILE = 'SPEC.SPC', FORM = 'FORMATTED',
CWFC *      STATUS = 'UNKNOWN'  )
C
C  BUILD DATA INPUT FILE NAME FOR WATCOM + MSDOS
C
CWFC  PRBDAT = 'OUTSDIF.DAT'
CWFC  PRBSOL = 'SOLUTION.DAT'
CWFC  ONELIN = 'SUMMARY.DAT'
CWFC  PRBSAV = 'SAVEDATA.DAT'
CWFC  ALIVED = 'ALIVE.DAT'
C
C  ** OPEN LANCELOT SPECIFICATION FILE FOR VAX SYSTEMS.
C
CVMS  OPEN( IINRUN, FILE = 'SPEC.SPC',  STATUS = 'UNKNOWN',
CVMS *      CARRIAGECONTROL = 'LIST' )
C
C  BUILD DATA INPUT FILE NAME FOR VAX SYSTEMS.
C
CVMS  PRBDAT = 'OUTSDIF.DAT'
CVMS  PRBSOL = 'SOLUTION.DAT'
CVMS  ONELIN = 'SUMMARY.DAT'
CVMS  PRBSAV = 'SAVEDATA.DAT'
CVMS  ALIVED = 'ALIVE.DAT'
C
C OPEN THE RELEVANT FILE FOR UNIX SYSTEMS.
C
CUNIX OPEN( INPUT , FILE = PRBDAT, FORM = 'FORMATTED',
CUNIX*      STATUS = 'OLD'  )
CUNIX REWIND INPUT
CUNIX OPEN( IOUTSL, FILE = PRBSOL, FORM = 'FORMATTED',
CUNIX*      STATUS = 'UNKNOWN'  )
CUNIX REWIND IOUTSL
CUNIX OPEN( IOUT1L, FILE = ONELIN, FORM = 'FORMATTED',
CUNIX*      STATUS = 'UNKNOWN'  )
CUNIX OPEN( IINPB , FILE = PRBSAV, FORM = 'FORMATTED',
CUNIX*      STATUS = 'UNKNOWN'  )
CUNIX REWIND IINPB
C
C OPEN THE RELEVANT FILE FOR WATCOM + MSDOS
C
CWFC  OPEN( INPUT , FILE = PRBDAT, FORM = 'FORMATTED',
CWFC *      STATUS = 'OLD'  )
CWFC  REWIND INPUT
CWFC  OPEN( IOUTSL, FILE = PRBSOL, FORM = 'FORMATTED',
CWFC *      STATUS = 'UNKNOWN'  )
CWFC  REWIND IOUTSL
CWFC  OPEN( IOUT1L, FILE = ONELIN, FORM = 'FORMATTED',
CWFC *      STATUS = 'UNKNOWN'  )
CWFC  OPEN( IINPB , FILE = PRBSAV, FORM = 'FORMATTED',
CWFC *      STATUS = 'UNKNOWN'  )
CWFC  REWIND IINPB
C
C OPEN THE RELEVANT FILE FOR VAX SYSTEMS.
C
CVMS  OPEN ( INPUT , FILE = PRBDAT,  STATUS = 'UNKNOWN',
CVMS *       CARRIAGECONTROL = 'LIST' )
CVMS  OPEN ( IOUTSL, FILE = PRBSOL,  STATUS = 'UNKNOWN',
CVMS *       CARRIAGECONTROL = 'LIST' )
CVMS  OPEN ( IOUT1L, FILE = ONELIN,  STATUS = 'UNKNOWN',
CVMS *       CARRIAGECONTROL = 'LIST' )
CVMS  OPEN ( IINPB , FILE = PRBSAV,  STATUS = 'UNKNOWN',
CVMS *       CARRIAGECONTROL = 'LIST' )
C
C  CREATE A FILE WHICH THE USER MAY SUBSEQUENTLY REMOVE TO CAUSE
C  IMMEDIATE TERMINATION OF A RUN.
C
CIBM  INQUIRE( UNIT = IALIVE, EXIST = ALIVE )
CUNIX INQUIRE( FILE = ALIVED, EXIST = ALIVE )
CWFC  INQUIRE( FILE = ALIVED, EXIST = ALIVE )
CVMS  INQUIRE( FILE = 'ALIVE.DAT', EXIST = ALIVE )
      IF ( .NOT. ALIVE ) THEN
CIBM     OPEN ( IALIVE, FORM = 'FORMATTED' )
CUNIX    OPEN ( IALIVE, FILE = ALIVED, FORM = 'FORMATTED',
CUNIX*           STATUS = 'UNKNOWN'  )
CUNIX    REWIND IALIVE
CWFC     OPEN ( IALIVE, FILE = ALIVED, FORM = 'FORMATTED',
CWFC *           STATUS = 'UNKNOWN'  )
CWFC     REWIND IALIVE
CVMS     OPEN ( IALIVE, FILE = ALIVED, STATUS = 'UNKNOWN',
CVMS *          CARRIAGECONTROL = 'LIST' )
         WRITE( IALIVE, 2000 )
         CLOSE( IALIVE )
      END IF
C
C  PARTITION THE WORKSPACE PRIOR TO PERFORMING THE OPTIMIZATION.
C
CS    CALL SWRKSP( INPUT , IOUT  , IINRUN, IOUTSL, IOUT1L, IINPB ,
CD    CALL DWRKSP( INPUT , IOUT  , IINRUN, IOUTSL, IOUT1L, IINPB ,
     *             IOUTPB, IALIVE, DEBUG , DEBUGF, DEBUGG )
C
C  CLOSE THE OPENED FILES.
C
      CLOSE( INPUT  )
      CLOSE( IINRUN )
      CLOSE( IOUTSL )
      CLOSE( IOUT1L )
      CLOSE( IINPB  )
C
C  CLOSE AND DELETE THE REMAINING FILES.
C
CIBM  INQUIRE( UNIT = IALIVE, EXIST = ALIVE )
CUNIX INQUIRE( FILE = ALIVED, EXIST = ALIVE )
CWFC  INQUIRE( FILE = ALIVED, EXIST = ALIVE )
CVMS  INQUIRE( FILE = 'ALIVE.DAT', EXIST = ALIVE )
      IF ( ALIVE ) THEN
CIBM     OPEN ( IALIVE, FORM = 'FORMATTED' )
CUNIX    OPEN ( IALIVE, FILE = ALIVED, FORM = 'FORMATTED',
CUNIX*           STATUS = 'UNKNOWN'  )
CUNIX    REWIND IALIVE
CWFC     OPEN ( IALIVE, FILE = ALIVED, FORM = 'FORMATTED',
CWFC *           STATUS = 'UNKNOWN'  )
CWFC     REWIND IALIVE
CVMS     OPEN ( IALIVE, FILE = ALIVED, STATUS = 'UNKNOWN',
CVMS *          CARRIAGECONTROL = 'LIST' )
         CLOSE( IALIVE, STATUS = 'DELETE' )
      END IF
      STOP
 1000 FORMAT( A64 )
 1010 FORMAT( I6 )
 2000 FORMAT( ' LANCELOT rampages onwards ' )
      END
CDBUG integer function 
CDBUG* common_handler( sig, code, sigcontext, addr )
CDBUG integer sig, code, sigcontext(5), addr
CDBUG write( 6, ' ("ieee exception", Z3, 
CDBUG* " occured at address", z8) ' ) loc(code), loc(addr)
CCDBUG call abort( )
CDBUG return
CDBUG end
