NAME
Terminal::Identify - Perl extension for identifying the terminal emulator
SYNOPSIS
Use the package like this
use Terminal::Identify; # Imports all methods
use Terminal::Identify qw(whichterminalami); # Imports method whichterminalami()
# Identify the terminal emulator in use.
whichterminalami(["PROC"|"PATH"|"FTN"]); # Standard methods invocation
Terminal::Identify::whichterminalami(["PROC"|"PATH"|"FTN"]); # Alternate methods invocation
or like this
use Terminal::Identify; # Exports all methods
use Terminal::Identify qw(whichterminalami); # Exports method whichterminalami()
# Set the global output format flag of the package.
$OutputFormat = ["PROC"|"PATH"|"FTN"];
$Terminal::Identify::OutputFormat = ["PROC"|"PATH"|"FTN"];
# Identify the terminal emulator in use.
whichterminalami(); # Standard methods invocation
Terminal::Identify::whichterminalami(); # Alternate methods invocation
OUTPUT CONTROL
The former introduced string arguments PROC
, PATH
and FTN
in square brackets in the method call are optional.
["PROC"|"PATH"|"FTN"]
Valid method arguments are separated by a logical or. Without such a given subroutine argument, the process name of the terminal is printed to the screen. These arguments are controlling the format of the output of the identified terminal.
In addition to the method arguments introduced previously, there is a global package variable which can be used to control the format of the output of the identified terminal.
$OutputFormat
If the global package variable is set, the method arguments are ignored if existing in the method call.
OUTPUT FORMAT
The output format of the identified terminal can be influenced by the subroutine arguments "PROC"
, "PATH"
and "FTN"
.
PROC => Full process of the terminal emulator which is used.
PATH => Path to the location of the terminal emulator which is used.
FTN => Friendly terminal name of the terminal emulator which is used.
DESCRIPTION
The main objective of this package is to provide a method which is capable of identifying the terminal emulator a logged-in user is actual using.
In addition to the terminal emulator, the system console and a remote console are also recognised.
The logged-in user is related to a valid login shell directly. The login shell of the logged-in user as well as the logged-in user is determined. Next the terminal path to the pseudo terminal slave (pts) is identified.
Based on the previously informations the related process of the logged-in user, the login shell and the terminal path is determined. The evaluation of the PID of the process of the current running Perl script results in the PPID. The command related to this PPID is the name of the terminal emulator in use.
The package works together with different terminal emulators. When terminal emulators are spawned from an initial invoked terminal emulator, each terminal emulator is correctly recognised.
If the logged-in user changes during the session, this is recognised. Also using the sudo command does not affect the recognition of the terminal emulator.
The terminal emulator in use by the logged-in user can be identified by the main command whichterminalami()
or the other defined aliases.
EXAMPLES
Example 1
# Load the Perl module.
use Terminal::Identify;
# Declare the terminal variable.
my $terminal;
# Method call without an argument.
$terminal = whichterminalami();
print $terminal . "\n";
# Method call with argument "PROC".
$terminal = whichterminalami("PROC");
print $terminal . "\n";
# Method call with argument "PATH".
$terminal = whichterminalami("PATH");
print $terminal . "\n";
# Method call with argument "FTN".
$terminal = whichterminalami("FTN");
print $terminal . "\n";
Example 2
# Load the Perl module.
use Terminal::Identify;
# Declare the terminal variable.
my $terminal;
# Reset the global output format flag.
$OutputFormat = "";
# Method call without an argument.
$terminal = whichterminalami();
print $terminal . "\n";
# Set the global output format flag.
$OutputFormat = "PROC";
# Method call without an argument.
$terminal = whichterminalami();
print $terminal . "\n";
# Set the global output format flag.
$OutputFormat = "PATH";
# Method call without an argument.
$terminal = whichterminalami();
print $terminal . "\n";
# Set the global output format flag.
$OutputFormat = "FTN";
# Method call without an argument.
$terminal = whichterminalami();
print $terminal . "\n";
SYSTEM COMPATIBILITY
The module will work on Linux and on Unix or Unix-like operating systems in general until something else was shown.
FUNCTIONALITY REQUIREMENT
The following Linux commands must be available for package functionality:
ps
users
which
grep
The subsequent system files must be exist for package functionality:
/etc/shells
/etc/passwd
METHOD ALIASES
Aliases for whichterminalami
, which can be used are:
whichtermami <= whichterminalami
which_terminal <= whichterminalami
identify_terminal <= whichterminalami
TERMINALS TESTED
Terminal emulators tested so far with the package:
Alacritty
Aterm
Cool Retro Term
Deepin Terminal
Eterm
Gnome Terminal
Guake Terminal
Hyper
kitty
Konsole
LilyTerm
LXTerminal
MATE-Terminal
mlterm
mlterm-tiny
pterm
QTerminal
ROXTerm
Sakura
SCREEN
Tabby
Terminator
Terminology
Termit
Tilda
Tilix
tmux
UXTerm
Xfce4-Terminal-Emulator
xiterm+thai
Xterm
Yakuake
LIMITATIONS
The limitations of the package are given by the Linux commands and the Linux system files which are used by the package. The Linux command ps
, the Linux command users
, the Linux command which
and the Linux command grep
must be available. The Linux system files /etc/shells
and /etc/passwd
must be exist.
When the Linux command su is used, then the detection results in su as terminal emulator, which is wrong. This has to be checked and changed.
OPEN ISSUES
Subroutines must be implemented to check whether the necessary system programmes and the necessary system files are available.
KNOWN BUGS
The terminal emulator WezTerm can not be identified. Reason is a missing pseudo terminal slave while analysing the coupled terminal window.
ERROR CODES
The package returns no error codes at the moment.
NOTES
Problems were found with the use of the Inline C module. The problem is caused by user and root rights. Until this issue is resolved, the POSIX module is used instead of the C code.
PROGRAM-TECHNICAL BACKGROUND
The Linux command ps can be used e.g. in BASH to find out, which terminal emulator is in use by the current user. The command line to do this is quite easy:
ps -o 'cmd=' -p $(ps -o 'ppid=' -p $$)
If this is done from within a script it fails. There are a few hurdles to overcome in order to carry out this procedure from a script.
A distinction must be made between user and superuser. Using su or sudo makes a difference for running a script.
ABSTRACT
The module identifies the terminal emulator which the logged-in user is using currently. For this purpose, the login shells and the logged-in users are determined. The Perl script from which we identify the terminal emulator itself runs in a pseudo terminal slave (pts) with its own identification number. This pseudo terminal slave (pts) is identified, too. Based on all the former informations, the terminal emulator in use can be determined. If the Perl script runs from within the system console, the output returns the system console.
SEE ALSO
ps(1) - Linux manual page
users(1) - Linux manual page
which(1) - Linux manual page
grep(1) - Linux manual page
shells(5) - Linux manual page
passwd(5) - Linux manual page
AUTHOR
Dr. Peter Netz, <ztenretep@cpan.org>
COPYRIGHT AND LICENSE
Copyright (C) 2022 by Dr. Peter Netz
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.30.0 or, at your option, any later version of Perl 5 you may have available.