startIServ :: DynFlags -> IO IServ
startIServ dflags = do
  let prog = pgm_i dflags
      opts = getOpts dflags opt_i
      (javaProg, defaultJavaOpts) = pgm_java dflags
      javaOpts = getOpts dflags opt_java
      realProg = javaProg
      realOpts = defaultJavaOpts ++ javaOpts ++ ["-jar", prog] ++ opts
  debugTraceMsg dflags 3 $ text "Starting " <>
    text (realProg ++ " " ++ intercalate " " realOpts)
  let createProc = lookupHook createIservProcessHook
                              (\cp -> do { (mstdin,mstdout,mstderr,ph) <- createProcess cp
                                         ; return (mstdin, mstdout, mstderr, ph) })
                              dflags
  (ph, rh, wh, errh) <- runWithPipes dflags createProc realProg realOpts
  hSetEncoding rh latin1
  hSetEncoding wh latin1
  lo_ref <- newIORef Nothing
  cache_ref <- newIORef emptyUFM
  return $ IServ
    { iservPipe = Pipe { pipeRead = rh
                       , pipeWrite = wh
                       , pipeLeftovers = lo_ref }
    , iservProcess = ph
    , iservErrors = errh
    , iservLookupSymbolCache = cache_ref
    , iservPendingFrees = []
    }
startIServ :: DynFlags -> IO IServ
startIServ dflags = do
  let prog = pgm_i dflags
      opts = getOpts dflags opt_i
      (javaProg, defaultJavaOpts) = pgm_java dflags
      javaOpts = getOpts dflags opt_java
      realProg = javaProg
      realOpts = defaultJavaOpts ++ javaOpts ++ ["-jar", prog] ++ opts
  debugTraceMsg dflags 3 $ text "Starting " <>
    text (realProg ++ " " ++ intercalate " " realOpts)
  let createProc = lookupHook createIservProcessHook
                              (\cp -> do { (mstdin,mstdout,mstderr,ph) <- createProcess cp
                                         ; return (mstdin, mstdout, mstderr, ph) })
                              dflags
  (ph, rh, wh, errh) <- runWithPipes dflags createProc realProg realOpts
  hSetEncoding rh latin1
  hSetEncoding wh latin1
  lo_ref <- newIORef Nothing
  cache_ref <- newIORef emptyUFM
  return $ IServ
    { iservPipe = Pipe { pipeRead = rh
                       , pipeWrite = wh
                       , pipeLeftovers = lo_ref }
    , iservProcess = ph
    , iservErrors = errh
    , iservLookupSymbolCache = cache_ref
    , iservPendingFrees = []
    }
startIServ dflags = do
  let prog = pgm_i dflags
      opts = getOpts dflags opt_i
      (javaProg, defaultJavaOpts) = pgm_java dflags
      javaOpts = getOpts dflags opt_java
      realProg = javaProg
      realOpts = defaultJavaOpts ++ javaOpts ++ ["-jar", prog] ++ opts
  debugTraceMsg dflags 3 $ text "Starting " <>
    text (realProg ++ " " ++ intercalate " " realOpts)
  let createProc = lookupHook createIservProcessHook
                              (\cp -> do { (mstdin,mstdout,mstderr,ph) <- createProcess cp
                                         ; return (mstdin, mstdout, mstderr, ph) })
                              dflags
  (ph, rh, wh, errh) <- runWithPipes dflags createProc realProg realOpts
  hSetEncoding rh latin1
  hSetEncoding wh latin1
  lo_ref <- newIORef Nothing
  cache_ref <- newIORef emptyUFM
  return $ IServ
    { iservPipe = Pipe { pipeRead = rh
                       , pipeWrite = wh
                       , pipeLeftovers = lo_ref }
    , iservProcess = ph
    , iservErrors = errh
    , iservLookupSymbolCache = cache_ref
    , iservPendingFrees = []
    }