commit e821ed80afb2e4c922c000dbe59043064096516b
Author:     Jan Klemkow <[email protected]>
AuthorDate: Sun Oct 9 23:33:33 2022 +0200
Commit:     Jan Klemkow <[email protected]>
CommitDate: Sun Oct 9 23:33:33 2022 +0200

    refactor printing of terminal title
    
     - fix segmentation fault reported by NRK <[email protected]>
     - remove redundant code
     - move code to util.c.  So, filter programs can also use
       this feature.
     - while here, reorder util.h in the same order as util.c

diff --git a/lchat.c b/lchat.c
index d5d8faf..9725124 100644
--- a/lchat.c
+++ b/lchat.c
@@ -37,6 +37,7 @@
 
 static struct termios origin_term;
 static struct winsize winsize;
+static char *TERM;
 
 static void
 sigwinch(int sig)
@@ -48,13 +49,8 @@ sigwinch(int sig)
 static void
 exit_handler(void)
 {
-       char *title = getenv("TERM");
-
        /* reset terminal's window name */
-       if (strncmp(title, "screen", 6) == 0)
-               printf("\033k%s\033\\", title);
-       else
-               printf("\033]0;%s\a", title);
+       set_title(TERM, TERM);
 
        if (tcsetattr(STDIN_FILENO, TCSANOW, &origin_term) == -1)
                die("tcsetattr:");
@@ -171,6 +167,9 @@ main(int argc, char *argv[])
        char *prompt = read_file_line(".prompt");
        char *title = read_file_line(".title");
 
+       if ((TERM = getenv("TERM")) == NULL)
+               TERM = "";
+
        if (sl == NULL)
                die("Failed to initialize slackline");
 
@@ -252,10 +251,7 @@ main(int argc, char *argv[])
                if ((title = basename(path)) == NULL)
                        die("basename:");
        }
-       if (strncmp(getenv("TERM"), "screen", 6) == 0)
-               printf("\033k%s\033\\", title);
-       else
-               printf("\033]0;%s\a", title);
+       set_title(TERM, title);
 
        /* prepare terminal reset on exit */
        if (tcgetattr(fd, &origin_term) == -1)
diff --git a/util.c b/util.c
index fd36421..0516577 100644
--- a/util.c
+++ b/util.c
@@ -62,3 +62,12 @@ bell_match(const char *str, const char *regex_file)
 
        return false;
 }
+
+void
+set_title(const char *term, char *title)
+{
+       if (strncmp(term, "screen", 6) == 0)
+               printf("\033k%s\033\\", title);
+       else
+               printf("\033]0;%s\a", title);
+}
diff --git a/util.h b/util.h
index 9241384..3cac358 100644
--- a/util.h
+++ b/util.h
@@ -1,7 +1,8 @@
 #ifndef _UTIL_H_
 #define _UTIL_H_
 
-bool bell_match(const char *str, const char *regex_file);
 void die(const char *fmt, ...);
+bool bell_match(const char *str, const char *regex_file);
+void set_title(const char *term, const char *title);
 
 #endif

Reply via email to