]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/ttyutils: add get_terminal_default_type()
authorKarel Zak <kzak@redhat.com>
Mon, 29 May 2023 13:10:24 +0000 (15:10 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 29 May 2023 13:10:24 +0000 (15:10 +0200)
Based on code from agetty.

Signed-off-by: Karel Zak <kzak@redhat.com>
include/ttyutils.h
lib/ttyutils.c

index e28a2b03bf66b859fd8e28ed2743f3df300a03b5..39182b66bb77628142615b447c352b78af7d1b63 100644 (file)
@@ -88,6 +88,7 @@ struct chardata {
 extern int get_terminal_dimension(int *cols, int *lines);
 extern int get_terminal_width(int default_width);
 extern int get_terminal_type(const char **type);
+extern char *get_terminal_default_type(const char *ttyname, int is_serial);
 extern int get_terminal_stdfd(void);
 extern int get_terminal_name(const char **path, const char **name,
                             const char **number);
index 70645650391b1864d5b24444a7330bfcf7703b08..7c8860e63b97fe5a3d7a495d5aadee641a1adeb0 100644 (file)
@@ -7,9 +7,40 @@
 #include <ctype.h>
 #include <unistd.h>
 
+#ifdef HAVE_GETTTYNAM
+# include <ttyent.h>
+#endif
+
 #include "c.h"
 #include "ttyutils.h"
 
+#ifdef __linux__
+#  ifndef DEFAULT_VCTERM
+#    define DEFAULT_VCTERM "linux"
+#  endif
+#  if defined (__s390__) || defined (__s390x__)
+#    define DEFAULT_TTYS0  "dumb"
+#    define DEFAULT_TTY32  "ibm327x"
+#    define DEFAULT_TTYS1  "vt220"
+#  endif
+#  ifndef DEFAULT_STERM
+#    define DEFAULT_STERM  "vt102"
+#  endif
+#elif defined(__GNU__)
+#  ifndef DEFAULT_VCTERM
+#    define DEFAULT_VCTERM "hurd"
+#  endif
+#  ifndef DEFAULT_STERM
+#    define DEFAULT_STERM  "vt102"
+#  endif
+#else
+#  ifndef DEFAULT_VCTERM
+#    define DEFAULT_VCTERM "vt100"
+#  endif
+#  ifndef DEFAULT_STERM
+#    define DEFAULT_STERM  "vt100"
+#  endif
+#endif
 
 static int get_env_int(const char *name)
 {
@@ -130,6 +161,37 @@ int get_terminal_type(const char **type)
        return 0;
 }
 
+char *get_terminal_default_type(const char *ttyname, int is_serial)
+{
+#ifdef HAVE_GETTTYNAM
+       if (ttyname) {
+               struct ttyent *ent = getttynam(ttyname);
+
+               if (ent && ent->ty_type)
+                       return strdup(ent->ty_type);
+       }
+#endif
+
+#if defined (__s390__) || defined (__s390x__)
+       if (ttyname) {
+               /*
+                * Special terminal on first serial line on a S/390(x) which
+                * is due legacy reasons a block terminal of type 3270 or
+                * higher.  Whereas the second serial line on a S/390(x) is
+                * a real character terminal which is compatible with VT220.
+                */
+               if (strcmp(ttyname, "ttyS0") == 0)              /* linux/drivers/s390/char/con3215.c */
+                       return strdup(DEFAULT_TTYS0);
+               else if (strncmp(ttyname, "3270/tty", 8) == 0)  /* linux/drivers/s390/char/con3270.c */
+                       return strdup(DEFAULT_TTY32);
+               else if (strcmp(ttyname, "ttyS1") == 0)         /* linux/drivers/s390/char/sclp_vt220.c */
+                       return strdup(DEFAULT_TTYS1);
+       }
+#endif
+
+       return strdup(is_serial ? DEFAULT_STERM : DEFAULT_VCTERM);
+}
+
 #ifdef TEST_PROGRAM_TTYUTILS
 # include <stdlib.h>
 int main(void)
@@ -138,13 +200,21 @@ int main(void)
        int c, l;
 
        if (get_terminal_name(&path, &name, &num) == 0) {
-               fprintf(stderr, "tty path:   %s\n", path);
-               fprintf(stderr, "tty name:   %s\n", name);
-               fprintf(stderr, "tty number: %s\n", num);
+               char *term;
+
+               fprintf(stderr, "tty path:      %s\n", path);
+               fprintf(stderr, "tty name:      %s\n", name);
+               fprintf(stderr, "tty number:    %s\n", num);
+
+               fprintf(stderr, "tty term:      %s\n", getenv("TERM"));
+
+               term = get_terminal_default_type(name, 0);
+               fprintf(stderr, "tty dflt term: %s\n", term);
+               free(term);
        }
        get_terminal_dimension(&c, &l);
-       fprintf(stderr,         "tty cols:   %d\n", c);
-       fprintf(stderr,         "tty lines:  %d\n", l);
+       fprintf(stderr,         "tty cols:      %d\n", c);
+       fprintf(stderr,         "tty lines:     %d\n", l);
 
 
        return EXIT_SUCCESS;