NAME

Terminal::Identify - Perl extension for identifying the terminal emulator

SYNOPSIS

Methods call using a subroutine argument:

use Terminal::Identify;                      # Imports all methods
use Terminal::Identify qw(whichterminalami); # Imports method whichterminalami()

# Identify the terminal emulator.
whichterminalami(["PROC"|"PATH"|"FTN"]);                     # Standard methods invocation
Terminal::Identify::whichterminalami(["PROC"|"PATH"|"FTN"]); # Alternate methods invocation

Methods call using the global variable:

use Terminal::Identify;                      # Imports all methods
use Terminal::Identify qw(whichterminalami); # Imports method whichterminalami()

# Set the global output format flag.
$OutputFormat = ["PROC"|"PATH"|"FTN"];
$Terminal::Identify::OutputFormat = ["PROC"|"PATH"|"FTN"];

# Identify the terminal emulator.
whichterminalami();                     # Standard methods invocation
Terminal::Identify::whichterminalami(); # Alternate methods invocation

OPTIONS

Output control

The string arguments PROC, PATH and FTN in square brackets in the method call are optional.

["PROC"|"PATH"|"FTN"]

The valid method arguments are separated by a logical or. The string arguments are controlling the format of the output of the identified terminal. Without a subroutine argument, the process name of the terminal emulator is printed to the terminal window.

In addition to the usage of the valid method arguments, there is a global variable which can be used to control the format of the output of the identified terminal.

$OutputFormat

If the global variable is set, the method arguments are ignored if existing in the method call. Unset the process name of the terminal emulator is printed to the terminal window.

Output format

As described above the output format of the identified terminal can be influenced by the options "PROC", "PATH" and "FTN".

The output format is as follows:

    PROC => Full process command line of the terminal emulator
    
    PATH => Path to the location of the executable of the terminal emulator
    
    FTN =>  Friendly terminal name of the terminal emulator

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

  • Black Box

  • 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

TESTING ENVIRONMENT

The package has been installed on various personal computers and laptops. This was done in the desktop environment using different terminal emulators, logged into the system console locally and logged in remotely. The Linux OS is based on Debian.

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

Terminal emulators which are installed using Flatpak are not all the time correct identified.

KNOWN BUGS

The system file /etc/shells is not existing on solaris. If /etc/shells not exists, a backup solution must be integrated. This is a to-do for one of the next versions.

ERROR CODES

1 = Windows is recognised as OS.
2 = Linux command 'which' does not exist.
3 = Linux command 'grep' does not exist.
4 = Linux command 'users' does not exist.
5 = Linux command 'ps' does not exist.

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 $$)

The previous presented one-liner makes use of the fact, that a login shell e.g. Bash or Zsh is used by the terminal emulator.

This can be verified by following command line:

ps -o 'cmd=' -p $$

As consequence of this statement the PPID is the PID of the terminal emulator. The command related to the PPID is the name or process which invoked the terminal emulator.

If this procedure is done from within a script it fails in recognition of the terminal emulator. There are a few hurdles to overcome in order to carry out this procedure from within a script.

A distinction must be made between user and superuser, which invokes a script. Calling a script using sudo makes a difference for the recognition of the terminal emulator.

When the user logs in into the desktop, the user is connected to a login shell. Nevertheless, the user, which executes a script, can be different to the logged-in user.

Whenever a terminal is opened, this terminal is connected to a pseudo terminal slave. These pseudo-terminal slaves are numbered consecutively.

It should also be noted that a script can be executed from the command line as follows, maybe also with command line arguments:

sudo perl -w testscript.pl

The goal is to find the process that belongs to the executed script. The PPID is identified via the PID. This PPID is then the PID of the terminal emulator. Accordingly, the command line in the process output is the process that started the terminal emulator.

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. It is also recognised, when the script runs in a remote 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.