1280
  7   ?ˆl–Ãa¾”*i?u8 Y¸î41hß�þDô¡YÇ 'št’ºÈüÿê„x?é¿ç (ñ    ?#!/usr/bin/perl
# Call a PostgreSQL client program with the version, cluster and default
# database specified in ~/.postgresqlrc or
# /etc/postgresql-common/user_clusters.
#
# (C) 2005-2009 Martin Pitt <mpitt@debian.org>
# (C) 2013-2022 Christoph Berg <myon@debian.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.

use warnings;
use strict;
use POSIX;
use PgCommon;

my ($version, $cluster);
my $explicit_host = exists $ENV{PGHOST};
my $explicit_port = $ENV{PGPORT};
my $explicit_service = exists $ENV{PGSERVICE};

# Evaluate PGCLUSTER (unless PGHOST is set as well)
if (exists $ENV{'PGCLUSTER'} and not $explicit_host) {
    ($version, $cluster) = split ('/', $ENV{'PGCLUSTER'}, 2);
    error "Invalid version $version specified in PGCLUSTER" unless version_exists $version;
    error 'No cluster specified with $PGCLUSTER' unless $cluster;
}

# Check for --cluster argument and filter it out
for (my $i = 0; $i <= $#ARGV; ++$i) {
    last if $ARGV[$i] eq '--';

    if ($ARGV[$i] eq '--cluster') {
        error '--cluster option needs an argument (<version>/<cluster>)' if ($i >= $#ARGV);

        ($version, $cluster) = split ('/', $ARGV[$i+1], 2);
        error "Invalid version $version specified with --cluster" unless version_exists $version;
        error 'No cluster specified with --cluster' unless $cluster;

        splice @ARGV, $i, 2;
        last;
    } elsif ($ARGV[$i] =~ /^--cluster=(\d+\.?\d)\/(.+)/) {
        ($version, $cluster) = ($1, $2);
        error "Invalid version $version specified with --cluster" unless version_exists $version;
        error 'No cluster specified with --cluster' unless $cluster;

        splice @ARGV, $i, 1;
        last;
    }
    # --host or -h on command line, drop info from PGCLUSTER
    if ($ARGV[$i] =~ /^--host\b/ || $ARGV[$i] =~ /^-\w*h\w*$/) {
        ($version, $cluster) = (undef, undef);
        $explicit_host = 1;
        delete $ENV{PGCLUSTER};
    }
    # --port or -p on command line
    if ($ARGV[$i] =~ /^--port\b(?:=(\d+))?/ || $ARGV[$i] =~ /^-\w*p(\d+)?$/) {
        if (defined $1) {
            $explicit_port = $1;
        } elsif ($i < $#ARGV) {
            $explicit_port = $ARGV[$i+1];
        }
    }
    # "service=" in connection string
    if ($ARGV[$i] =~ /\bservice=/) {
        $explicit_service = 1;
    }
}

# if only a port is specified, look for local cluster on specified port
if ($explicit_port and not $version and not $cluster and not $explicit_host and not $explicit_service) {
    LOOP: foreach my $v (reverse get_versions()) {
        foreach my $c (get_version_clusters $v) {
            my $p = get_cluster_port $v, $c;
            if ($p eq $explicit_port) {
                $version = $v;
                # set PGCLUSTER variable for information
                $ENV{PGCLUSTER} = "$version/$c";
                last LOOP;
            }
        }
    }
}

# if we don't have a cluster, and no specific host or port was given, consult postgresqlrc
# or fall back to default port cluster (on 5432), or undef otherwise
my ($db);
($version, $cluster, $db) = user_cluster_map() unless ($cluster or $explicit_host or $explicit_port);

my ($host, $port);

if ($cluster) {
    # check if we have a network cluster (N.N/the.host.name:port)
    if ($cluster =~ /^(\S+):(\d*)$/) {
	$host = $1;
	$port = $2 || $PgCommon::defaultport;
    } elsif (not cluster_exists($version, $cluster)) {
        # a specific cluster was requested, error out because it doesn't exist
        error "Cluster $version $cluster does not exist";
    } else {
        $host = get_cluster_soc