main (sample/psql.rb)

    # File sample/psql.rb, line 1015
    def main
      dbname = nil
      host = "localhost"
      port = 5432
      qfilename = nil

      singleQuery = nil
      settings = PsqlSettings.new(nil, nil, nil, nil, nil, FALSE, FALSE,
                                  FALSE, FALSE, FALSE, FALSE, FALSE)
      settings.opt = PrintOpt.new(FALSE, FALSE, FALSE, FALSE, FALSE,
                                  FALSE, nil, nil, nil, nil)

      listDatabases = FALSE
      successResult = TRUE
      singleSlashCmd = FALSE

      settings.opt.align = TRUE
      settings.opt.header = TRUE
      settings.queryFout = STDOUT
      settings.opt.fieldSep = DEFAULT_FIELD_SEP.dup
      settings.opt.pager = TRUE
      settings.quiet = FALSE
      settings.notty = FALSE
      settings.useReadline = TRUE

      parsed = parseArgs(0, nil, "AelHnsqStx", "a:", "c:", "d:", "f:", "F:",
                         "h:", "o:", "p:", "T:")

      if $OPT_A
        settings.opt.align = FALSE
      end

      if $OPT_a
        #fe_setauthsvc(optarg, errbuf);
        printf("not implemented, sorry.\n")
        exit(1)
      end

      if $OPT_c
        singleQuery = $OPT_c
        if singleQuery[0, 1] == "\\"
          singleSlashCmd = TRUE
        end
      end

      if $OPT_d
        dbname = $OPT_d
      end

      if $OPT_e
        settings.echoQuery = TRUE
      end

      if $OPT_f
        qfilename = $OPT_f
      end

      if $OPT_F
        settings.opt.fieldSep = $OPT_F
      end

      if $OPT_l
        listDatabases = TRUE
      end

      if $OPT_h
        host = $OPT_h
      end

      if $OPT_H
        settings.opt.html3 = TRUE
      end

      if $OPT_n
        settings.useReadline = FALSE
      end

      if $OPT_o
        setFout(settings, $OPT_o)
      end

      if $OPT_p
        port = $OPT_p.to_i
      end

      if $OPT_q
        settings.quiet = TRUE
      end

      if $OPT_s
        settings.singleStep = TRUE
      end

      if $OPT_S
        settings.singleLineMode = TRUE
      end

      if $OPT_t
        settings.opt.header = FALSE
      end

      if $OPT_T
        settings.opt.tableOpt = $OPT_T
      end

      if $OPT_x
        settings.opt.expanded = TRUE
      end

      if ARGV.length == 1
        dbname = ARGV[0]
      end

      if listDatabases
        dbname = "template1"
      end

      settings.db = PGconn.connect(host, port, "", "", dbname);
      dbname = settings.db.db

      if settings.db.status() == PGconn::CONNECTION_BAD
        printf(STDERR, "Connection to database '%s' failed.\n", dbname)
        printf(STDERR, "%s", settings.db.error)
        exit(1)
      end
      if listDatabases
        exit(listAllDbs(settings))
      end
      if (!settings.quiet && !singleQuery && !qfilename)
        printf("Welcome to the POSTGRESQL interactive sql monitor:\n")
        printf("  Please read the file COPYRIGHT for copyright terms of POSTGRESQL\n\n")
        printf("   type \\? for help on slash commands\n")
        printf("   type \\q to quit\n")
        printf("   type \\g or terminate with semicolon to execute query\n")
        printf(" You are currently connected to the database: %s\n\n", dbname)
      end
      if (qfilename || singleSlashCmd)
        if singleSlashCmd
          line = singleQuery
        else
          line = sprintf("\\i %s", qfilename)
        end
        HandleSlashCmds(settings, line, "")
      else
        if settings.useReadline
          begin
            require "readline"
            $readline_ok = TRUE
          rescue
            $readline_ok = FALSE
          end
        else
          $readline_ok = FALSE
        end
        if singleQuery
          success = SendQuery(settings, singleQuery, false, false, 0)
          successResult = success
        else
          successResult = MainLoop(settings, STDIN)
        end
      end
      settings.db.finish()
      return !successResult
    end