# File sample/psql.rb, line 904
def MainLoop(settings, source)

  success        = TRUE
  interactive    = TRUE
  insideQuote    = FALSE
  querySent      = FALSE
  done           = FALSE

  query          = nil
  queryWaiting   = nil
  slashCmdStatus = -1
  interactive    = (source == STDIN && !settings.notty)

  if settings.quiet
    settings.prompt = nil
  else
    settings.prompt =  settings.db.db + PROMPT
  end

  while !done
    if slashCmdStatus == 3
      line = query
      query = nil
    else
      if interactive && !settings.quiet
        if insideQuote
          settings.prompt[settings.prompt.length-3,1] = "\'"
        elsif (queryWaiting != nil && !querySent)
          settings.prompt[settings.prompt.length-3,1] = "-"
        else
          settings.prompt[settings.prompt.length-3,1] = "="
        end
      end
      line = gets(settings.prompt, source)
    end

    if line == nil
      printf("EOF\n")
      done = TRUE
    else

      ### debbegging information ###
      if !interactive && !settings.singleStep && !settings.quiet
        printf(STDERR, "%s\n", line)
      end

      ### ommit comment ###
      begin_comment = line.index("--")
      if begin_comment
        line = line[0, begin_comment]
      end

      ### erase unnecessary characters ###
      line.gsub!(/[ \t\f\n\r]+\z/, "")
      if line.length == 0
        next
      end
      ### begin slash command handling ###
      if line[0, 1] == "\\"
        query = line
        slashCmdStatus, query = HandleSlashCmds(settings, line, nil)
        if slashCmdStatus == 0 && query != nil
          success = SendQuery(settings, query, FALSE, FALSE, 0) && success
          querySent = TRUE
        elsif slashCmdStatus == 1
          query = nil
        elsif slashCmdStatus == 2
          break
        end
        line = nil
        next
      end

      ### begin query command handling ###
      slashCmdStatus = -1
      if settings.singleLineMode
        success = SendQuery(settings, line, FALSE, FALSE, 0) && success
        querySent = TRUE
      else

        if queryWaiting 
          queryWaiting += " " + line 
        else
          queryWaiting =  line
        end
      
        for i in 0..line.length-1
          if line[i, 1] == "\'"
            insideQuote = !insideQuote
          end
        end

        if !insideQuote
          if line[line.length-1, 1] == ";"
            query = queryWaiting
            queryWaiting = nil

            success = SendQuery(settings, query, FALSE, FALSE, 0) && success
            querySent = TRUE
          else
            querySent = FALSE
          end
        else
          querySent = FALSE
        end
      end
    end
  end # while
  return success
end