1a5ae21e4Smrg#!/usr/bin/env perl
204b94745Smrg# $XTermId: decsed.pl,v 1.6 2015/02/22 01:37:20 tom Exp $
3a5ae21e4Smrg# -----------------------------------------------------------------------------
4a5ae21e4Smrg# this file is part of xterm
5a5ae21e4Smrg#
6a5ae21e4Smrg# Copyright 2015 by Thomas E. Dickey
7a5ae21e4Smrg#
8a5ae21e4Smrg#                         All Rights Reserved
9a5ae21e4Smrg#
10a5ae21e4Smrg# Permission is hereby granted, free of charge, to any person obtaining a
11a5ae21e4Smrg# copy of this software and associated documentation files (the
12a5ae21e4Smrg# "Software"), to deal in the Software without restriction, including
13a5ae21e4Smrg# without limitation the rights to use, copy, modify, merge, publish,
14a5ae21e4Smrg# distribute, sublicense, and/or sell copies of the Software, and to
15a5ae21e4Smrg# permit persons to whom the Software is furnished to do so, subject to
16a5ae21e4Smrg# the following conditions:
17a5ae21e4Smrg#
18a5ae21e4Smrg# The above copyright notice and this permission notice shall be included
19a5ae21e4Smrg# in all copies or substantial portions of the Software.
20a5ae21e4Smrg#
21a5ae21e4Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22a5ae21e4Smrg# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23a5ae21e4Smrg# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24a5ae21e4Smrg# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
25a5ae21e4Smrg# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26a5ae21e4Smrg# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27a5ae21e4Smrg# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a5ae21e4Smrg#
29a5ae21e4Smrg# Except as contained in this notice, the name(s) of the above copyright
30a5ae21e4Smrg# holders shall not be used in advertising or otherwise to promote the
31a5ae21e4Smrg# sale, use or other dealings in this Software without prior written
32a5ae21e4Smrg# authorization.
33a5ae21e4Smrg# -----------------------------------------------------------------------------
34a5ae21e4Smrg# Exercise DECSED (erase display) with or without DECSCA (protect against
35a5ae21e4Smrg# DECSED).
36a5ae21e4Smrguse strict;
37a5ae21e4Smrg
38a5ae21e4Smrguse Getopt::Std;
39a5ae21e4Smrg
40a5ae21e4Smrgour ( $opt_c,  $opt_n,       $opt_p,     $opt_w );
41a5ae21e4Smrgour ( $lineno, $test_string, $term_wide, $term_high );
42a5ae21e4Smrg
43a5ae21e4Smrgsub move($$) {
44a5ae21e4Smrg    my $y = shift;
45a5ae21e4Smrg    my $x = shift;
46a5ae21e4Smrg    printf "\x1b[%d;%dH", $y, $x;
47a5ae21e4Smrg}
48a5ae21e4Smrg
49a5ae21e4Smrgsub protect($) {
50a5ae21e4Smrg    my $code = shift;
51a5ae21e4Smrg    printf "\x1b[%d\"q", $code;
52a5ae21e4Smrg}
53a5ae21e4Smrg
54a5ae21e4Smrgsub set_color($) {
55a5ae21e4Smrg    my $code = shift;
56a5ae21e4Smrg    if ( $code == 1 ) {
57a5ae21e4Smrg        printf "\x1b[0;36;44m";    # cyan-on-blue
58a5ae21e4Smrg    }
59a5ae21e4Smrg    else {
60a5ae21e4Smrg        printf "\x1b[0;39;49m";
61a5ae21e4Smrg    }
62a5ae21e4Smrg}
63a5ae21e4Smrg
64a5ae21e4Smrg# returns a string of two-column characters given an ASCII alpha/numeric string
65a5ae21e4Smrgsub double_cells($) {
66a5ae21e4Smrg    my $value = $_[0];
67a5ae21e4Smrg    $value =~ s/ /  /g;
68a5ae21e4Smrg    pack(
69a5ae21e4Smrg        "U*",
70a5ae21e4Smrg        map {
71a5ae21e4Smrg            ( $_ <= 32 || $_ > 127 )    # if non-ASCII character...
72a5ae21e4Smrg              ? 32                      # ...just show a blank
73a5ae21e4Smrg              : ( 0xff00 + ( $_ - 32 ) )    # map to "Fullwidth Form"
74a5ae21e4Smrg          } unpack( "C*", $value )
75a5ae21e4Smrg    );                                      # unpack unsigned-char characters
76a5ae21e4Smrg}
77a5ae21e4Smrg
78a5ae21e4Smrg# write the text for the given line-number
79a5ae21e4Smrgsub fill_line($$) {
80a5ae21e4Smrg    my $number = shift;
81a5ae21e4Smrg    my $offset = shift;
82a5ae21e4Smrg    my $length = $opt_w ? ( $term_wide / 2 ) : $term_wide;
83a5ae21e4Smrg    my $actual;
84a5ae21e4Smrg    my $margin = 0;
85a5ae21e4Smrg    $actual = $length;
86a5ae21e4Smrg    my $string = $test_string;
87a5ae21e4Smrg    while ( ( $opt_w ? ( 2 * length($string) ) : length($string) ) <
88a5ae21e4Smrg        ( $offset + $length ) )
89a5ae21e4Smrg    {
90a5ae21e4Smrg        $string = $string . $test_string;
91a5ae21e4Smrg    }
92a5ae21e4Smrg    $string = substr( $string, $offset, $length );
93a5ae21e4Smrg    $string = double_cells($string) if ($opt_w);
94a5ae21e4Smrg    printf "%s", $string;
95a5ae21e4Smrg
96a5ae21e4Smrg    printf "\n";
97a5ae21e4Smrg    return ++$offset;
98a5ae21e4Smrg}
99a5ae21e4Smrg
100a5ae21e4Smrgsub main::HELP_MESSAGE() {
101a5ae21e4Smrg    printf STDERR <<EOF
102a5ae21e4SmrgUsage: $0 [options] DECSED [y [x]]
103a5ae21e4Smrg
104a5ae21e4SmrgThe test
105a5ae21e4Smrg    fills the screen (-n for normal, -w for wide characters, default DECALN)
106a5ae21e4Smrg    then positions to the given y,x (default is middle of screen),
107a5ae21e4Smrg    writes a '*' at the cursor position
108a5ae21e4Smrg    moves back to given y,x again
109a5ae21e4Smrg    erases with the DECSED value (0=below, 1=above, 2=all=default),
110a5ae21e4Smrg    moves the cursor up/down one line to avoid overwriting by prompt
111a5ae21e4Smrg
112a5ae21e4SmrgOptions:
113a5ae21e4Smrg
114a5ae21e4Smrg-c   use color
115a5ae21e4Smrg-n   write normal-characters rather than using DECALN
116a5ae21e4Smrg-p   protect screen against erasure using DECSCA (DECALN is unprotected)
117a5ae21e4Smrg-w   write wide-characters rather than using DECALN
118a5ae21e4SmrgEOF
119a5ae21e4Smrg      ;
120a5ae21e4Smrg    exit;
121a5ae21e4Smrg}
122a5ae21e4Smrg
123a5ae21e4Smrg&getopts('cnpw') || &main::HELP_MESSAGE;
124a5ae21e4Smrg
125a5ae21e4Smrg$term_wide = `tput cols`;
126a5ae21e4Smrg$term_high = `tput lines`;
127a5ae21e4Smrg
128a5ae21e4Smrg$test_string =
129a5ae21e4Smrg  "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz";
130a5ae21e4Smrg
131a5ae21e4Smrgmy $parm_DECSED = ( $#ARGV >= 0 ) ? $ARGV[0] : 2;
132a5ae21e4Smrgmy $parm_ycoord = ( $#ARGV >= 1 ) ? $ARGV[1] : $term_high / 2;
133a5ae21e4Smrgmy $parm_xcoord = ( $#ARGV >= 2 ) ? $ARGV[2] : $term_wide / 2;
134a5ae21e4Smrg
135a5ae21e4Smrgbinmode( STDOUT, ":utf8" );
136a5ae21e4Smrg&protect(1)   if ($opt_p);
137a5ae21e4Smrg&set_color(1) if ($opt_c);
138a5ae21e4Smrgif ( $opt_n or $opt_w ) {
139a5ae21e4Smrg    my $offset = 0;
140a5ae21e4Smrg    for ( $lineno = 0 ; $lineno < $term_high - 1 ; ++$lineno ) {
141a5ae21e4Smrg        $offset = &fill_line( $lineno, $offset );
142a5ae21e4Smrg    }
143a5ae21e4Smrg}
144a5ae21e4Smrgelse {
145a5ae21e4Smrg    printf "\x1b#8";    # DECALN
146a5ae21e4Smrg}
147a5ae21e4Smrg&move( $parm_ycoord, $parm_xcoord );
148a5ae21e4Smrgprintf '*';
149a5ae21e4Smrg&move( $parm_ycoord, $parm_xcoord );
150a5ae21e4Smrgprintf "\x1b[?%dJ", $parm_DECSED;
151a5ae21e4Smrgif ( $parm_DECSED == 0 ) {
152a5ae21e4Smrg    &move( $parm_ycoord + 1, $parm_xcoord );
153a5ae21e4Smrg}
154a5ae21e4Smrgelsif ( $parm_DECSED == 1 ) {
155a5ae21e4Smrg    &move( $parm_ycoord - 1, $parm_xcoord );
156a5ae21e4Smrg}
157a5ae21e4Smrg&set_color(0) if ($opt_c);
158a5ae21e4Smrg&protect(0)   if ($opt_p);
159a5ae21e4Smrg
160a5ae21e4Smrgexit;
161