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

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

used in the method call are optional. The 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 three 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

$OutputFormat

which can be used to control the format of the output of the identified terminal. If the global package variable is set, the method arguments are ignored if existing in the method call.

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.

According to the current state of development, it can also be recognised whether it is the System Console or a remotely used System Console.

The logged-in user is related to a valid login shell. Knowing this, the logged-in user as well as the login shell of the logged-in user are determined. Next the terminal path to the pseudo terminal slave (pts) is identified. Based on this 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 running Perl script results in the PPID. The command related to this PPID is the name of the terminal emulator in use. Not only a terminal emulator can be identified by this package, it is also able to detect if a system console is being used by a logged-in user. The package also works if different terminal emulators are used and when they were spawned from on initial started terminal emulator.

Changing a user and using su and sudo are currently no longer a problem for the detection of a terminal emulator.

The terminal emulator in use by the logged-in user can be identified by the main command whichterminalami() and the other defined aliases.

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.

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

Following Linux commands should be available for functionality:

  • ps

  • users

  • which

  • grep

  • shells

  • 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

  • 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.

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.