<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env perl
# $XTermId: query-xres.pl,v 1.6 2019/10/06 23:56:18 tom Exp $
# -----------------------------------------------------------------------------
# this file is part of xterm
#
# Copyright 2019 by Thomas E. Dickey
#
#                         All Rights Reserved
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Except as contained in this notice, the name(s) of the above copyright
# holders shall not be used in advertising or otherwise to promote the
# sale, use or other dealings in this Software without prior written
# authorization.
# -----------------------------------------------------------------------------
# Report features enabled/disabled via resource-settings

# TODO: handle 8-bit controls

use strict;
use warnings;

use Getopt::Std;
use IO::Handle;

our ( $opt_a, $opt_d, $opt_e, $opt_m, $opt_q );

our @query_params;
our @query_result;

$Getopt::Std::STANDARD_HELP_VERSION = 1;
&amp;getopts('acdemq') || die(
    "Usage: $0 [options]\n
Options:\n
  -a      (same as -d -e -m)
  -d      query disabled/disallowed features
  -e      query enabled/allowed features
  -m      query modified keys
  -q      quicker results by merging queries
"
);

if (
    $#ARGV &lt; 0
    and not( defined($opt_d)
        or defined($opt_e)
        or defined($opt_m) )
  )
{
    $opt_a = 1;
}

sub get_reply($) {
    open TTY, "+&lt;/dev/tty" or die("Cannot open /dev/tty\n");
    autoflush TTY 1;
    my $old = `stty -g`;
    system "stty raw -echo min 0 time 5";

    print TTY @_;
    my $reply = &lt;TTY&gt;;
    close TTY;
    system "stty $old";
    if ( defined $reply ) {
        die("^C received\n") if ( "$reply" eq "\003" );
    }
    return $reply;
}

sub hexified($) {
    my $value  = $_[0];
    my $result = "";
    my $n;

    for ( $n = 0 ; $n &lt; length($value) ; ++$n ) {
        $result .= sprintf( "%02X", ord substr( $value, $n, 1 ) );
    }
    return $result;
}

sub begin_query() {
    @query_params = ();
}

sub add_param($) {
    $query_params[ $#query_params + 1 ] = &amp;hexified( $_[0] );
}

sub finish_query() {
    my $reply = &amp;get_reply( "\x1bP+Q" . join( ';', @query_params ) . "\x1b\\" );

    return unless defined $reply;
    while ( $reply =~ /\x1bP1\+R[[:xdigit:]]+[=;][[:xdigit:]]*.*\x1b\\/ ) {
        my $n;
        my $parse;

        $reply =~ s/^\x1bP1\+R//;
        $parse = $reply;
        $reply =~ s/\x1b\\.*$//;
        $parse = substr( $parse, length($reply) );
        $parse =~ s/^\x1b\\//;

        my $result = "";
        my $count  = 0;
        my $state  = 0;
        my $error  = "?";
        for ( $n = 0 ; $n &lt; length($reply) ; ) {
            my $c = substr( $reply, $n, 1 );

            if ( $c eq ';' ) {
                $n += 1;
                printf "%d%s\t%s\n", $count, $error, $result
                  if ( $result ne "" );
                $result = "";
                $state  = 0;
                $error  = "?";
                $count++;
            }
            elsif ( $c eq '=' ) {
                $error = ""
                  if (  $count &lt;= $#query_params
                    and &amp;hexified($result) eq $query_params[$count] );
                $n += 1;
                $result .= $c;
                $state = 1;
            }
            elsif ( $c =~ /[[:punct:]]/ ) {
                $n += 1;
                $result .= $c;
            }
            else {
                my $k = hex substr( $reply, $n, 2 );
                if ( $k == 0x1b ) {
                    $result .= "\\E";
                }
                elsif ( $k == 0x7f ) {
                    $result .= "^?";
                }
                elsif ( $k == 32 ) {
                    $result .= "\\s";
                }
                elsif ( $k &lt; 32 ) {
                    $result .= sprintf( "^%c", $k + 64 );
                }
                elsif ( $k &gt; 128 ) {
                    $result .= sprintf( "\\%03o", $k );
                }
                else {
                    $result .= chr($k);
                }
                $n += 2;
            }
        }
        printf "%d%s\t%s\n", $count, $error, $result if ( $result ne "" );
        $reply = $parse;
    }
}

sub do_query($) {
    my $name = shift;

    &amp;begin_query unless ($opt_q);
    &amp;add_param($name);
    &amp;finish_query unless ($opt_q);
}

&amp;begin_query if ($opt_q);

while ( $#ARGV &gt;= 0 ) {
    &amp;do_query( shift @ARGV );
}

if ( defined($opt_a) || defined($opt_d) ) {
    &amp;do_query("disallowedColorOps");
    &amp;do_query("disallowedFontOps");
    &amp;do_query("disallowedMouseOps");
    &amp;do_query("disallowedPasteControls");
    &amp;do_query("disallowedTcapOps");
    &amp;do_query("disallowedWindowOps");
}

if ( defined($opt_a) ) {
    &amp;do_query("allowSendEvents");
    &amp;do_query("allowPasteControls");
    &amp;do_query("allowC1Printable");
    &amp;do_query("saveLines");
}

if ( defined($opt_a) || defined($opt_e) ) {
    &amp;do_query("allowColorOps");
    &amp;do_query("allowFontOps");
    &amp;do_query("allowMouseOps");
    &amp;do_query("allowPasteControls");
    &amp;do_query("allowTcapOps");
    &amp;do_query("allowTitleOps");
    &amp;do_query("allowWindowOps");
}

if ( defined($opt_a) || defined($opt_m) ) {
    &amp;do_query("formatOtherKeys");
    &amp;do_query("modifyCursorKeys");
    &amp;do_query("modifyFunctionKeys");
    &amp;do_query("modifyKeyboard");
    &amp;do_query("modifyOtherKeys");
}

&amp;finish_query if ($opt_q);

1;
</pre></body></html>