HandleSlashCmds (sample/psql.rb)

    # File sample/psql.rb, line 589
    def HandleSlashCmds(settings, line, query)
      status = 1
      cmd = unescape(line[1, line.length])
      args = cmd.split

      case args[0]
      when 'a' # toggles to align fields on output
        settings.opt.align =
          toggle(settings, settings.opt.align, "field alignment")

      when 'C' # define new caption
        if !args[1]
          settings.opt.caption = ""
        else
          settings.opt.caption = args[1]
        end

      when 'c', 'connect' # connect new database
        do_connect(settings, args[1])

      when 'copy' # copy from file
        do_copy(settings, args[1], args[2], args[3])

      when 'd' # \d describe tables or columns in a table
        if !args[1]
          tableList(settings, FALSE, 'b', FALSE)
        elsif args[1] == "*"
          tableList(settings, FALSE, 'b', FALSE)
          tableList(settings, TRUE, 'b', FALSE)
        else
          tableDesc(settings, args[1])
        end

      when 'da'
        descbuf =  "SELECT a.aggname AS aggname, t.typname AS type, "
        descbuf += "obj_description (a.oid) as description "
        descbuf += "FROM pg_aggregate a, pg_type t "
        descbuf += "WHERE a.aggbasetype = t.oid "
        if (args[1])
          descbuf += "AND a.aggname ~ '^"
          descbuf += args[1]
          descbuf += "' "
        end
        descbuf += "UNION SELECT a.aggname AS aggname, "
        descbuf += "'all types' as type, obj_description (a.oid) "
        descbuf += "as description FROM pg_aggregate a "
        descbuf += "WHERE a.aggbasetype = 0"
        if (args[1])
          descbuf += "AND a.aggname ~ '^"
          descbuf += args[1]
          descbuf += "' "
        end
        descbuf += "ORDER BY aggname, type;"
        res = SendQuery(settings, descbuf, FALSE, FALSE, 0)

      when 'di'
        tableList(settings, FALSE, 'i', FALSE)

      when 'ds'
        tableList(settings, FALSE, 'S', FALSE)

      when 'dS'
        tableList(settings, FALSE, 'b', TRUE)

      when 'dt'
        tableList(settings, FALSE, 't', FALSE)

      when 'e' # edit
        status, query = do_edit(args[1], query)

      when 'E'
        if args[1]
          begin
            lastfile = args[1]
            File.file?(lastfile) && (mt = File.mtime(lastfile))
            editFile(lastfile)
            File.file?(lastfile) && (mt2 = File.mtime(lastfile))
            fd = File.new(lastfile, "r")
            if mt != mt2
              MainLoop(settings, fd)
              fd.close()
            else
              if !settings.quiet
                printf(STDERR, "warning: %s not modified. query not executed\n", lastfile)
              end
              fd.close()
            end
          rescue
            #
          end
        else
          printf(STDERR, "\\r must be followed by a file name initially\n");
        end
      when 'f'
        if args[1]
          settings.opt.fieldSep = args[1]
          if !settings.quiet
            printf(STDERR, "field separater changed to '%s'\n", settings.opt.fieldSep)
          end
        end

      when 'g' # \g means send query
        if !args[1]
          settings.gfname = nil
        else
          settings.gfname = args[1]
        end
        status = 0

      when 'h' # help
        if args[2]
          args[1] += " " + args[2]
        end
        do_help(args[1])

      when 'i' # \i is include file
        if args[1]
          begin
            fd = File.open(args[1], "r")
            MainLoop(settings, fd)
            fd.close()
          rescue Errno::ENOENT
            printf(STDERR, "file named %s could not be opened\n", args[1])
          end
        else
          printf(STDERR, "\\i must be followed by a file name\n")
        end
      when 'l' # \l is list database
        listAllDbs(settings)

      when 'H'
        settings.opt.html3 =
           toggle(settings, settings.opt.html3, "HTML3.0 tabular output")

        if settings.opt.html3
          settings.opt.standard = FALSE
        end

      when 'o'
        setFout(settings, args[1])

      when 'p'
        if query
          File.print(query)
          File.print("\n")
        end

      when 'q' # \q is quit
        status = 2

      when 'r' # reset(clear) the buffer
        query = nil
        if !settings.quiet
          printf(STDERR, "buffer reset(cleared)\n")
        end

      when 's' # \s is save history to a file
        begin
          if (args[1])
            fd = File.open(args[1], "w")
          else
            fd = STDOUT
          end
          Readline::HISTORY.each do |his|
            fd.write (his + "\n")
          end
          if !fd.tty?
            begin
              fd.close
            end
          end
        rescue
          printf(STDERR, "cannot write history \n");
        end

      when 'm' # monitor like type-setting
        settings.opt.standard =
          toggle(settings, settings.opt.standard, "standard SQL separaters and padding")
        if settings.opt.standard
          settings.opt.html3 = FALSE
          settings.opt.expanded = FALSE
          settings.opt.align = TRUE
          settings.opt.header = TRUE
          if settings.opt.fieldSep
            settings.opt.fieldSep = ""
          end
          settings.opt.fieldSep = "|"
          if !settings.quiet
            printf(STDERR, "field separater changed to '%s'\n", settings.opt.fieldSep)
          end
        else
          if settings.opt.fieldSep
            settings.opt.fieldSep = ""
          end
          settings.opt.fieldSep = DEFAULT_FIELD_SEP
          if !settings.quiet
            printf(STDERR, "field separater changed to '%s'\n", settings.opt.fieldSep)
          end
        end

      when 't' # toggle headers
        settings.opt.header =
          toggle(settings, settings.opt.header, "output headings and row count")

      when 'T' # define html <table ...> option
        if !args[1]
          settings.opt.tableOpt = nil
        else
          settings.opt.tableOpt = args[1]
        end

      when 'x'
        settings.opt.expanded =
          toggle(settings, settings.opt.expanded, "expanded table representation")

      when '!'
        do_shell(args[1])

      when '?' # \? is help
        slashUsage(settings)
      end

      return status, query
    end