readUntilMatchParenAux :: Int -> ProcessHandle -> Handle -> String -> IO String
readUntilMatchParenAux n cp h str =
  let os = length $ filter (== '(') str
      cs = length $ filter (== ')') str
  in if n < 1 then do
    appendFile vseErrFile $ "readUntilMatchParen failed after\n" ++ str ++ "\n"
    return ""
  else if os == cs && os > 0 then return str
  else do
  mc <- myGetChar cp h
  case mc of
    Wait -> do
      appendFile vseErrFile "waiting for character ...\n"
      readUntilMatchParenAux (n - 1) cp h str
    Stop -> return str
    JustChar c -> readUntilMatchParen cp h $ c : str
readUntilMatchParenAux :: Int -> ProcessHandle -> Handle -> String -> IO String
readUntilMatchParenAux n cp h str =
  let os = length $ filter (== '(') str
      cs = length $ filter (== ')') str
  in if n < 1 then do
    appendFile vseErrFile $ "readUntilMatchParen failed after\n" ++ str ++ "\n"
    return ""
  else if os == cs && os > 0 then return str
  else do
  mc <- myGetChar cp h
  case mc of
    Wait -> do
      appendFile vseErrFile "waiting for character ...\n"
      readUntilMatchParenAux (n - 1) cp h str
    Stop -> return str
    JustChar c -> readUntilMatchParen cp h $ c : str
readUntilMatchParenAux n cp h str =
  let os = length $ filter (== '(') str
      cs = length $ filter (== ')') str
  in if n < 1 then do
    appendFile vseErrFile $ "readUntilMatchParen failed after\n" ++ str ++ "\n"
    return ""
  else if os == cs && os > 0 then return str
  else do
  mc <- myGetChar cp h
  case mc of
    Wait -> do
      appendFile vseErrFile "waiting for character ...\n"
      readUntilMatchParenAux (n - 1) cp h str
    Stop -> return str
    JustChar c -> readUntilMatchParen cp h $ c : str