This is an autogenerated patch header for a single-debian-patch file. The
delta against upstream is either kept as a single patch, or maintained
in some VCS, and exported as a single patch instead of more manageable
atomic patches.

--- kbtin-2.1.orig/tintin.h
+++ kbtin-2.1/tintin.h
@@ -81,11 +81,7 @@
                                              char, it will be sent 'as is'
                                              to the MUD */
 #define MAX_SESNAME_LENGTH 512 /* don't accept session names longer than this */
-#if defined(__FreeBSD__) || defined(__OpenBSD__)
 #define MAX_RECURSION 64
-#else
-#define MAX_RECURSION 128
-#endif
 #ifndef DEFAULT_FILE_DIR
 #define DEFAULT_FILE_DIR "." /* Path to Tintin files, or HOME */
 #endif
@@ -126,7 +122,7 @@
 #define DEFAULT_LOG_MESS true
 #define DEFAULT_TICK_MESS true
 #define DEFAULT_PRETICK 10
-#define DEFAULT_CHARSET "ISO-8859-1"      /* the MUD-side charset */
+#define DEFAULT_CHARSET "UTF-8"           /* the MUD-side charset */
 #define DEFAULT_LOGCHARSET LOGCS_LOCAL
 #define DEFAULT_PARTIAL_LINE_MARKER 0
 #define BAD_CHAR '?'                      /* marker for chars illegal for a charset */
@@ -226,6 +222,7 @@ enum
 #include <signal.h>
 #include <errno.h>
 #include <sys/time.h>
+#include <strings.h>
 #include <time.h>
 #ifdef HAVE_ZLIB
 # include <zlib.h>
@@ -436,6 +433,7 @@ static inline char toalower(char x) { re
 #define N(x) ((x)*DENOM)
 
 #define assert(p) do if (!(p)){fprintf(stderr, "ASSERT FAILED in %s:%u : "#p "\n", __FILE__, __LINE__);abort();}while(0)
+#define ZERO(x) bzero(&(x), sizeof(x))
 
 #ifdef HAVE_HS
 // Should be in globals.h but for the typedef...
--- kbtin-2.1.orig/.cirrus.yml
+++ kbtin-2.1/.cirrus.yml
@@ -14,7 +14,7 @@ freebsd_13_task:
 
 macos_x_task:
   macos_instance:
-    image: big-sur-xcode
+    image: ghcr.io/cirruslabs/macos-ventura-base:latest
   install_script:
     - brew update
     - brew install cmake gnutls zlib
--- /dev/null
+++ kbtin-2.1/.github/workflows/codeql.yml
@@ -0,0 +1,68 @@
+name: "CodeQL"
+
+on:
+  push:
+    branches: [ "master" ]
+  pull_request:
+    # The branches below must be a subset of the branches above
+    branches: [ "master" ]
+  schedule:
+    - cron: '29 11 * * 4'
+
+jobs:
+  analyze:
+    name: Analyze
+    runs-on: ubuntu-latest
+    permissions:
+      actions: read
+      contents: read
+      security-events: write
+
+    strategy:
+      fail-fast: false
+      matrix:
+        language: [ 'cpp' ]
+        # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
+        # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
+
+    steps:
+    - name: Checkout repository
+      uses: actions/checkout@v3
+
+    - name: Install B-Deps
+      run: |
+        sudo apt-get update
+        sudo apt-get -y install cmake libgnutls28-dev libz-dev libhyperscan-dev
+
+    # Initializes the CodeQL tools for scanning.
+    - name: Initialize CodeQL
+      uses: github/codeql-action/init@v2
+      with:
+        languages: ${{ matrix.language }}
+        # If you wish to specify custom queries, you can do so here or in a config file.
+        # By default, queries listed here will override any specified in a config file.
+        # Prefix the list here with "+" to use these queries and those in the config file.
+
+        # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
+        # queries: security-extended,security-and-quality
+
+
+    # Autobuild attempts to build any compiled languages  (C/C++, C#, Go, or Java).
+    # If this step fails, then you should remove it and run the build manually (see below)
+    - name: Autobuild
+      uses: github/codeql-action/autobuild@v2
+
+    # ℹ️ Command-line programs to run using the OS shell.
+    # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
+
+    #   If the Autobuild fails above, remove it and uncomment the following three lines.
+    #   modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
+
+    # - run: |
+    #   echo "Run, Build Application using script"
+    #   ./location_of_script_within_repo/buildscript.sh
+
+    - name: Perform CodeQL Analysis
+      uses: github/codeql-action/analyze@v2
+      with:
+        category: "/language:${{matrix.language}}"
--- /dev/null
+++ kbtin-2.1/.github/workflows/coverity.yml
@@ -0,0 +1,26 @@
+name: Coverity scan
+
+on:
+  push:
+    branches: [coverity]
+
+jobs:
+  coverity:
+    runs-on: ubuntu-latest
+    steps:
+    - name: Update apt
+      run: sudo apt-get update -qq
+    - name: Install dependencies
+      run: sudo apt-get install -y
+               cmake
+               libgnutls28-dev
+               libz-dev
+               libhyperscan-dev
+    - uses: actions/checkout@v3
+    - name: cmake
+      run: cmake -B ${{github.workspace}}/build
+    - uses: vapier/coverity-scan-action@v1
+      with:
+        email: ${{ secrets.COVERITY_SCAN_EMAIL }}
+        token: ${{ secrets.COVERITY_SCAN_TOKEN }}
+        command: make -C ${{github.workspace}}/build
--- kbtin-2.1.orig/KBtin_help
+++ kbtin-2.1/KBtin_help
@@ -863,7 +863,7 @@ None of the above symbols will heed ".gz
 #logtype <type>
 Sets the logging mode to <type>.  Valid modes are:
 * raw     Everything as-is.
-* lf      ANSI with cr/lfs converted to lfs [default].
+* lf      ANSI with cr/lf converted to lf [default].
 * ttyrec  ANSI with timing information, in a format compatible with ttyrec(1).
 
 You can change the logtype at any moment -- however, changing it in the middle
--- kbtin-2.1.orig/action.c
+++ kbtin-2.1/action.c
@@ -75,11 +75,11 @@ static void parse_action(const char *arg
     if (!*left)
     {
         tintin_printf(ses, "#Defined %ss:", what);
-        show_tlist(l, 0, 0);
+        show_tlist(l, 0, 0, false);
     }
     else if (*left && !*right)
     {
-        if (!show_tlist(l, left, 0) && ses->mesvar[MSG_ACTION])
+        if (!show_tlist(l, left, 0, false) && ses->mesvar[MSG_ACTION])
             tintin_printf(ses, "#That %s (%s) is not defined.", what, left);
     }
     else
--- kbtin-2.1.orig/antisub.c
+++ kbtin-2.1/antisub.c
@@ -27,7 +27,7 @@ void antisubstitute_command(const char *
 
     if (!*left)
     {
-        tintin_puts("#THESE ANTISUBSTITUTES HAS BEEN DEFINED:", ses);
+        tintin_puts("#THESE ANTISUBSTITUTES HAVE BEEN DEFINED:", ses);
         show_slist(ass);
     }
     else
--- kbtin-2.1.orig/colors.c
+++ kbtin-2.1/colors.c
@@ -268,6 +268,7 @@ void do_in_MUD_colors(char *txt, bool qu
         case 127:
             // ␡
             *out++=0xe2; *out++=0x90; *out++=0xa1;
+            break;
         case 27:
             if (*(txt+1)=='[')
             {
--- kbtin-2.1.orig/eval.c
+++ kbtin-2.1/eval.c
@@ -88,7 +88,7 @@ static bool do_inline(const char *line,
     memcpy(savestacks, stacks, sizeof(savestacks));
 
     ptr=command;
-    while (*line&&(*line!=' '))
+    while (*line && !isaspace(*line))
         *ptr++=*line++;
     *ptr=0;
     line=space_out(line);
@@ -192,7 +192,7 @@ static bool conv_to_nums(char *arg, stru
     ptr = arg;
     while (*ptr)
     {
-        if (*ptr == ' ') ;
+        if (isaspace(*ptr)) ;
         else if (*ptr == tintin_char)
             /* inline commands */
         {
@@ -406,7 +406,7 @@ static bool conv_to_nums(char *arg, stru
             tintin_eprintf(ses, "#Error. Invalid expression in #if or #math in {%s}.", arg);
             return false;
         }
-        if (*ptr != ' ')
+        if (!isaspace(*ptr))
         {
             stacks[i].pos = i + 1;
             i++;
--- kbtin-2.1.orig/events.c
+++ kbtin-2.1/events.c
@@ -47,7 +47,7 @@ static void list_events(const char *arg,
         tintin_printf(ses, "#Defined events:");
         while (ev)
         {
-            tintin_printf(ses, "(%d.%03d)\t {%s}", (int)((ev->time-ct)/NANO),
+            tintin_printf(ses, "(%lld.%03d)\t {%s}", (ev->time-ct)/NANO,
                 msec(ev->time-ct), ev->event);
             ev = ev->next;
         }
@@ -59,7 +59,7 @@ static void list_events(const char *arg,
         {
             if (match(left, ev->event))
             {
-                tintin_printf(ses, "(%d.%03d)\t {%s}", (ev->time-ct)/NANO,
+                tintin_printf(ses, "(%lld.%03d)\t {%s}", (ev->time-ct)/NANO,
                     msec(ev->time-ct), ev->event);
                 flag = true;
             }
@@ -162,7 +162,7 @@ void undelay_command(const char *arg, st
         {
             flag=true;
             if (ses==activesession && ses->mesvar[MSG_EVENT])
-                tintin_printf(ses, "#Ok. Event {%s} at %ld.%03d won't be executed.",
+                tintin_printf(ses, "#Ok. Event {%s} at %lld.%03d won't be executed.",
                     (*ev)->event, ((*ev)->time-ct)/NANO, msec((*ev)->time-ct));
             remove_event(ev);
         }
--- kbtin-2.1.orig/files.c
+++ kbtin-2.1/files.c
@@ -379,6 +379,8 @@ static FILE* open_logfile(struct session
         zip="gzip -9";
     else if (len>=5 && !strcmp(fname+len-4, ".bz2"))
         zip="bzip2";
+    else if (len>=5 && !strcmp(fname+len-4, ".bz3"))
+        zip="bzip3";
     else if (len>=4 && !strcmp(fname+len-3, ".xz"))
         zip="xz";
     else if (len>=5 && !strcmp(fname+len-4, ".zst"))
@@ -551,7 +553,7 @@ struct session* do_read(FILE *myfile, co
     int nl;
     mbstate_t cs;
 
-    memset(&cs, 0, sizeof(cs));
+    ZERO(cs);
 
     want_tt_char = !in_read && !tintin_char_set;
     if (!ses->verbose)
@@ -613,9 +615,10 @@ struct session* do_read(FILE *myfile, co
                 continue;
             }
         }
+        else
+            ignore_lines=false;
         ses = parse_input(buffer, true, ses);
         recursion=0;
-        ignore_lines=false;
         strcpy(buffer, line);
     }
     if (*buffer)
@@ -1025,7 +1028,7 @@ void textin_command(const char *arg, str
     char buffer[BUFFER_SIZE], filename[BUFFER_SIZE], *cptr, lfname[BUFFER_SIZE];
     mbstate_t cs;
 
-    memset(&cs, 0, sizeof(cs));
+    ZERO(cs);
 
     get_arg_in_braces(arg, buffer, 1);
     substitute_vars(buffer, buffer, ses);
--- kbtin-2.1.orig/help.c
+++ kbtin-2.1/help.c
@@ -47,12 +47,12 @@ void help_command(const char *arg, struc
 #endif
     if (!myfile)
     {
-        sprintf(filestring, "%s_help", tintin_exec);
+        snprintf(filestring, sizeof filestring, "%s_help", tintin_exec);
         myfile = check_file(filestring);
     }
     if (!myfile)
     {
-        sprintf(filestring, "%s/KBtin_help", getenv("HOME"));
+        snprintf(filestring, sizeof filestring, "%s/KBtin_help", getenv("HOME"));
         myfile = check_file(filestring);
     }
     if (!myfile)
--- kbtin-2.1.orig/highlight.c
+++ kbtin-2.1/highlight.c
@@ -112,7 +112,7 @@ void highlight_command(const char *arg,
     if (!*left)
     {
         tintin_printf(ses, "#THESE HIGHLIGHTS HAVE BEEN DEFINED:");
-        show_tlist(ses->highs, 0, 0);
+        show_tlist(ses->highs, 0, 0, true);
         return;
     }
 
--- kbtin-2.1.orig/kbtree.h
+++ kbtin-2.1/kbtree.h
@@ -35,10 +35,10 @@
 #define KB_MAX_DEPTH 64
 
 typedef struct {
-	intptr_t is_internal:1, n:31;
+	uintptr_t is_internal:1, n:31;
 } kbnode_t;
 
-#define __KB_WORD sizeof(intptr_t)
+#define __KB_WORD sizeof(uintptr_t)
 
 typedef struct {
 	kbnode_t *x;
--- kbtin-2.1.orig/lists.c
+++ kbtin-2.1/lists.c
@@ -178,7 +178,10 @@ bool isatom(const char *arg)
         /* one element list = '{elem}' */
         return false;
 
-    return !strchr(arg, ' ');
+    for (; *arg; arg++)
+        if (isaspace(*arg))
+            return false;
+    return true;
         /* argument contains spaces i.e. = 'elem1 elem2' */
         /* this is incompatibile with supposed " behaviour */
 }
@@ -668,7 +671,7 @@ void collate_command(const char *arg, st
             j=strtol(arg, &err, 10), arg=err;
         else
             j=1;
-        if (!*arg || *arg==' ')
+        if (!*arg || isaspace(*arg))
             continue;
         arg = get_arg_in_braces(arg, cur, 0);
         if (!j)
@@ -716,7 +719,7 @@ void expand_command(const char *arg, str
             j=strtol(arg, &err, 10), arg=err;
         else
             j=1;
-        if (!*arg || *arg==' ')
+        if (!*arg || isaspace(*arg))
             continue;
         arg = get_arg_in_braces(arg, cur, 0);
         if (j>BUFFER_SIZE/2)
--- kbtin-2.1.orig/main.c
+++ kbtin-2.1/main.c
@@ -34,10 +34,6 @@
 #include <sys/stat.h>
 #include <sys/resource.h>
 
-#ifndef BADSIG
-#define BADSIG (void (*)(int))-1
-#endif
-
 typedef void (*sighandler_t)(int);
 
 extern void end_command(const char *arg, struct session *ses);
@@ -114,13 +110,13 @@ static void setup_signals(void)
     sigemptyset(&act.sa_mask);
     act.sa_flags=SA_RESTART;
 
-    if (signal(SIGTERM, (sighandler_t)myquitsig) == BADSIG)
+    if (signal(SIGTERM, (sighandler_t)myquitsig) == SIG_ERR)
         syserr("signal SIGTERM");
-    if (signal(SIGQUIT, (sighandler_t)myquitsig) == BADSIG)
+    if (signal(SIGQUIT, (sighandler_t)myquitsig) == SIG_ERR)
         syserr("signal SIGQUIT");
-    if (signal(SIGINT, (sighandler_t)myquitsig) == BADSIG)
+    if (signal(SIGINT, (sighandler_t)myquitsig) == SIG_ERR)
         syserr("signal SIGINT");
-    if (signal(SIGHUP, (sighandler_t)sighup) == BADSIG)
+    if (signal(SIGHUP, (sighandler_t)sighup) == SIG_ERR)
         syserr("signal SIGHUP");
     act.sa_handler=(sighandler_t)tstphandler;
     if (sigaction(SIGTSTP, &act, 0))
@@ -138,16 +134,16 @@ static void setup_signals(void)
 
     if (ui_own_output)
     {
-        if (signal(SIGSEGV, (sighandler_t)sigsegv) == BADSIG)
+        if (signal(SIGSEGV, (sighandler_t)sigsegv) == SIG_ERR)
             syserr("signal SIGSEGV");
-        if (signal(SIGFPE, (sighandler_t)sigfpe) == BADSIG)
+        if (signal(SIGFPE, (sighandler_t)sigfpe) == SIG_ERR)
             syserr("signal SIGFPE");
     }
 
     act.sa_handler=(sighandler_t)sigchild;
     if (sigaction(SIGCHLD, &act, 0))
         syserr("sigaction SIGCHLD");
-    if (signal(SIGPIPE, SIG_IGN) == BADSIG)
+    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
         syserr("signal SIGPIPE");
 }
 
@@ -422,7 +418,7 @@ static void tintin(void)
     int inbuf=0;
     mbstate_t instate;
 
-    memset(&instate, 0, sizeof(instate));
+    ZERO(instate);
 
     for (;;)
     {
--- kbtin-2.1.orig/misc.c
+++ kbtin-2.1/misc.c
@@ -731,7 +731,7 @@ void system_command(const char *arg, str
             tintin_puts1("#ERROR EXECUTING SHELL COMMAND.", ses);
             return;
         }
-        memset(&cs, 0, sizeof(cs));
+        ZERO(cs);
 
         save_lastintitle=ses->lastintitle;
         while (fgets(buf, BUFFER_SIZE, output))
@@ -1057,7 +1057,7 @@ void info_command(const char *arg, struc
     if (ses!=nullsession)
     {
         timens_t ct=current_time();
-        tintin_printf(ses, "Idle time: %d.%d, server idle: %d.%d",
+        tintin_printf(ses, "Idle time: %lld.%lld, server idle: %lld.%lld",
             (ct-ses->idle_since)/NANO, (ct-ses->idle_since)%NANO/(NANO/10),
             (ct-ses->server_idle_since)/NANO, (ct-ses->server_idle_since)%NANO/(NANO/10));
     }
@@ -1364,7 +1364,7 @@ void chr_command(const char *arg, struct
             {
                 while (isadigit(*lp))
                     v=v*10 + *lp++-'0';
-                if (*lp && *lp!=' ' && *lp!='\t')
+                if (*lp && !isaspace(*lp))
                 {
                     tintin_eprintf(ses, "#chr: not a valid number in {%s}", left);
                     return;
--- kbtin-2.1.orig/net.c
+++ kbtin-2.1/net.c
@@ -67,7 +67,7 @@ int connect_mud(const char *host, const
     struct addrinfo *ai, hints;
     int sock;
 
-    memset(&hints, 0, sizeof(hints));
+    ZERO(hints);
     hints.ai_family=AF_UNSPEC;
     hints.ai_socktype=SOCK_STREAM;
     hints.ai_protocol=IPPROTO_TCP;
@@ -121,6 +121,7 @@ int connect_mud(const char *host, const
                 if (abort_connect)
                 {
                     tintin_eprintf(ses, "#CONNECTION TIMED OUT");
+                    close(sock);
                     continue;
                 }
                 else
@@ -128,6 +129,7 @@ int connect_mud(const char *host, const
             default:
                 alarm(0);
                 tintin_eprintf(ses, "#%s", strerror(errno));
+                close(sock);
                 continue;
             }
         }
--- kbtin-2.1.orig/parse.c
+++ kbtin-2.1/parse.c
@@ -106,7 +106,7 @@ struct session* parse_input(const char *
         input = get_command(input, command);
         substitute_vars(command, command, ses);
         nspaces=0;
-        while (*input==' ')
+        while (isaspace(*input))
         {
             input++;
             nspaces++;
@@ -201,13 +201,11 @@ static inline bool is_speedwalk_dirs(con
 static void do_speedwalk(const char *cp, struct session *ses)
 {
     char sc[2];
-    const char *loc;
-    int loopcnt, i;
 
     strcpy(sc, "x");
     while (*cp)
     {
-        loc = cp;
+        const char *loc = cp;
         bool multflag = false;
         while (isadigit(*cp))
         {
@@ -216,9 +214,9 @@ static void do_speedwalk(const char *cp,
         }
         if (multflag && *cp)
         {
-            sscanf(loc, "%d%c", &loopcnt, sc);
-            i = 0;
-            while (i++ < loopcnt)
+            int loopcnt = atoi(loc);
+            sc[0] = *cp;
+            while (loopcnt-- > 0)
                 write_com_arg_mud(sc, "", 0, ses);
         }
         else if (*cp)
@@ -226,10 +224,6 @@ static void do_speedwalk(const char *cp,
             sc[0] = *cp;
             write_com_arg_mud(sc, "", 0, ses);
         }
-        /* Added the if to make sure we didn't move the pointer outside the
-           bounds of the original pointer.  Corrects the bug with speedwalking
-           where if you typed "u7" tintin would go apeshit. (JE)
-         */
         if (*cp)
             cp++;
     }
@@ -524,7 +518,7 @@ static inline const char* get_arg_stop_s
             s++;
             inside = !inside;
         }
-        else if (!inside && *s == ' ')
+        else if (!inside && isaspace(*s))
             break;
         else
             *arg++ = *s++;
@@ -569,7 +563,7 @@ static const char* get_command(const cha
             else
                 break;
         }
-        else if (!inside && (*s==' ' || *s==9))
+        else if (!inside && isaspace(*s))
             break;
         else
             *arg++ = *s++;
--- kbtin-2.1.orig/pty.c
+++ kbtin-2.1/pty.c
@@ -53,7 +53,7 @@ static char TtyProto[] = "/dev/ttyXY";
 # endif
 
 
-int openpty(int *amaster, int *aslave, char *dummy, struct termios *termp, struct winsize *wp)
+static int openpty(int *amaster, int *aslave, char *dummy, struct termios *termp, struct winsize *wp)
 {
     int master, slave;
 
@@ -162,10 +162,8 @@ ok:
         ioctl(master, TIOCSWINSZ, wp);
     /* let's ignore errors on these ioctls silently */
 
-    if (amaster)
-        *amaster=master;
-    if (aslave)
-        *aslave=slave;
+    *amaster=master;
+    *aslave=slave;
     return 0;
 }
 
--- kbtin-2.1.orig/run.c
+++ kbtin-2.1/run.c
@@ -23,8 +23,8 @@ static void print_stty(int fd)
     char buf[BUFFER_SIZE], *bptr;
 #define battr(c, a, b) bptr+=sprintf(bptr, " %s%s~7~", (ta.c_##c&a)?"~9~":"~2~-~4~", b);
 
-    memset(&ta, 0, sizeof(ta));
-    memset(&ws, 0, sizeof(ws));
+    ZERO(ta);
+    ZERO(ws);
     tintin_printf(0, "~7~pty attributes (fd=%d):", fd);
     if (tcgetattr(fd, &ta))
         tintin_printf(0, " attrs: unknown");
--- kbtin-2.1.orig/session.c
+++ kbtin-2.1/session.c
@@ -102,7 +102,7 @@ static int list_sessions(const char *arg
 
     if (!*left)
     {
-        tintin_puts("#THESE SESSIONS HAS BEEN DEFINED:", ses);
+        tintin_puts("#THESE SESSIONS HAVE BEEN DEFINED:", ses);
         for (sesptr = sessionlist; sesptr; sesptr = sesptr->next)
             if (sesptr!=nullsession)
                 show_session(sesptr);
@@ -312,7 +312,7 @@ void kill_all(struct session *ses, bool
     ses->antisubs = init_slist();
     ses->binds = init_hash();
     ses->path_begin = ses->path_length = 0;
-    bzero(ses->path, sizeof(ses->path));
+    ZERO(ses->path);
     ses->pathdirs = init_hash();
 #ifdef HAVE_HS
     ses->highs_dirty = true;
@@ -384,7 +384,7 @@ void init_nullses(void)
         nullsession->hooks[i]=0;
     nullsession->path_begin = 0;
     nullsession->path_length = 0;
-    bzero(nullsession->path, sizeof(nullsession->path));
+    ZERO(nullsession->path);
     nullsession->last_line[0] = 0;
     nullsession->linenum = 0;
     nullsession->events = NULL;
@@ -483,7 +483,7 @@ static struct session *new_session(const
     newsession->next = sessionlist;
     newsession->path_begin = 0;
     newsession->path_length = 0;
-    bzero(newsession->path, sizeof(newsession->path));
+    ZERO(newsession->path);
     newsession->more_coming = false;
     newsession->events = NULL;
     newsession->verbose = ses->verbose;
@@ -641,9 +641,12 @@ void seslist(char *result)
                 *result++=' ';
             else
                 flag=true;
-            result+=snprintf(result, BUFFER_SIZE-5+r0-result,
+            int slen = snprintf(result, BUFFER_SIZE-5+r0-result,
                 isatom(sesptr->name) ? "%s" : "{%s}", sesptr->name);
-            if (result-r0>BUFFER_SIZE-10)
+            if (slen <= 0)
+                return;
+            result += slen;
+            if (result-r0 > BUFFER_SIZE-10)
                 return; /* pathological session names */
         }
 }
--- kbtin-2.1.orig/ssl.c
+++ kbtin-2.1/ssl.c
@@ -116,7 +116,6 @@ static void save_cert(gnutls_x509_crt_t
         return;
     }
     snprintf(fname, BUFFER_SIZE, "%s/%s/%s", home, CONFIG_DIR, CERT_DIR);
-    mkdir(fname, 0755);
     if (mkdir(fname, 0755) && errno!=EEXIST)
     {
         tintin_eprintf(oldses, "#Cannot create certs dir (%s): %s", fname, strerror(errno));
--- kbtin-2.1.orig/string.c
+++ kbtin-2.1/string.c
@@ -598,7 +598,7 @@ bad:
         if (tt==err)
             goto bad;
         tt=err;
-        while (*tt==' ')
+        while (isaspace(*tt))
             tt++;
         switch (toalower(*tt))
         {
@@ -625,7 +625,7 @@ bad:
         }
         if (*tt==',')
             tt++;
-        while (*tt==' ')
+        while (isaspace(*tt))
             tt++;
         if (!strncmp(tt, "and", 3))
             tt+=3;
--- kbtin-2.1.orig/substitute.c
+++ kbtin-2.1/substitute.c
@@ -26,6 +26,8 @@ static void list_subs(const char *left,
     kbtree_t(trip) *sub = ses->subs;
 
     TRIP_ITER(sub, mysubs)
+        if (!match(left, mysubs->left))
+            continue;
         if (gag)
         {
             if (!strcmp(mysubs->right, EMPTY_LINE))
@@ -45,13 +47,13 @@ static void list_subs(const char *left,
         }
     ENDITER
 
-    if (!flag && ses->mesvar[MSG_SUBSTITUTE])
-    {
-        if (strcmp(left, "*"))
-            tintin_printf(ses, "#THAT %s IS NOT DEFINED.", gag? "GAG":"SUBSTITUTE");
-        else
-            tintin_printf(ses, "#NO %sS HAVE BEEN DEFINED.", gag? "GAG":"SUBSTITUTE");
-    }
+    if (flag)
+        return;
+
+    if (strcmp(left, "*"))
+        tintin_printf(ses, "#THAT %s IS NOT DEFINED.", gag? "GAG":"SUBSTITUTE");
+    else
+        tintin_printf(ses, "#NO %sS HAVE BEEN DEFINED.", gag? "GAG":"SUBSTITUTE");
 }
 
 static void parse_sub(const char *left_, const char *right,  bool gag, struct session *ses)
--- kbtin-2.1.orig/tests/data/#write, #writeses.out	
+++ kbtin-2.1/tests/data/#write, #writeses.out	
@@ -1,6 +1,5 @@
 #COMMANDS-FILE WRITTEN.
 #DELTA COMMANDS FILE WRITTEN.
-#charset {UTF-8}
 #alias {bar} {a}
 #alias {foo} {a}
 #promptaction {bar} {a} {5}
@@ -14,7 +13,6 @@
 #route {foo} {bar} {e} 10
 #route {bar} {baz} {e} 10
 -----
-#charset {UTF-8}
 #alias {bar} {a}
 #promptaction {bar} {a} {5}
 #antisub {bar}
--- /dev/null
+++ kbtin-2.1/tests/data/list one.in	
@@ -0,0 +1,6 @@
+#act a foo
+#act a
+#act b
+#sub a foo
+#sub a
+#sub b
--- /dev/null
+++ kbtin-2.1/tests/data/list one.out	
@@ -0,0 +1,4 @@
+{a}={foo} @ {5}
+#THESE SUBSTITUTES HAVE BEEN DEFINED:
+{a}={foo}
+#THAT SUBSTITUTE IS NOT DEFINED.
--- kbtin-2.1.orig/tests/data/remove-nonexist-add-exist.out
+++ kbtin-2.1/tests/data/remove-nonexist-add-exist.out
@@ -1,4 +1,4 @@
-#THESE ANTISUBSTITUTES HAS BEEN DEFINED:
+#THESE ANTISUBSTITUTES HAVE BEEN DEFINED:
 {x}
 #THESE SUBSTITUTES HAVE BEEN DEFINED:
 {x}={2}
@@ -9,7 +9,8 @@
 #Defined promptactions:
 {x}={2} @ {5}
 **********
-#THESE ANTISUBSTITUTES HAS BEEN DEFINED:
+#THESE ANTISUBSTITUTES HAVE BEEN DEFINED:
+#NO SUBSTITUTES HAVE BEEN DEFINED.
 #THESE HIGHLIGHTS HAVE BEEN DEFINED:
 #Defined actions:
 #Defined promptactions:
--- /dev/null
+++ kbtin-2.1/tests/data/tabbed_args.in
@@ -0,0 +1,2 @@
+#var foo	{a b}
+#show {[$foo]}
--- /dev/null
+++ kbtin-2.1/tests/data/tabbed_args.out
@@ -0,0 +1 @@
+[a b]
--- kbtin-2.1.orig/tests/data/unlist.out
+++ kbtin-2.1/tests/data/unlist.out
@@ -6,7 +6,7 @@
 {foo}={x} @ {5}
 #THESE SUBSTITUTES HAVE BEEN DEFINED:
 {foo}={x}
-#THESE ANTISUBSTITUTES HAS BEEN DEFINED:
+#THESE ANTISUBSTITUTES HAVE BEEN DEFINED:
 {foo}
 #THESE HIGHLIGHTS HAVE BEEN DEFINED:
 {foo}={red}
--- kbtin-2.1.orig/tlist.c
+++ kbtin-2.1/tlist.c
@@ -19,8 +19,6 @@ not_numeric:
         a++;
         b++;
     }
-    if (!a && !b)
-        return 0;
     if (!isadigit(*a) || !isadigit(*b))
         return (*a<*b)? -1 : (*a>*b)? 1 : 0;
     while (*a=='0')
@@ -102,9 +100,9 @@ void show_trip(const ptrip t)
         tintin_printf(0, "~7~{%s~7~}={%s~7~}", t->left, t->right);
 }
 
-bool show_tlist(kbtree_t(trip) *l, const char *pat, const char *msg)
+bool show_tlist(kbtree_t(trip) *l, const char *pat, const char *msg, bool no_pr)
 {
-    if (pat && is_literal(pat))
+    if (no_pr && pat && is_literal(pat))
     {
         struct trip srch = {(char*)pat, 0, 0};
         const ptrip *t = kb_get(trip, l, &srch);
--- kbtin-2.1.orig/unicode.c
+++ kbtin-2.1/unicode.c
@@ -203,7 +203,7 @@ void utf8_to_local(char *d, const char *
 {
     mbstate_t cs;
 
-    memset(&cs, 0, sizeof(cs));
+    ZERO(cs);
     utf8_to_mb(&d, s, &cs);
     *d=0;
 }
@@ -216,7 +216,7 @@ void local_to_utf8(char *d, const char *
 
     if (!cs)
     {
-        memset(&cs0, 0, sizeof(cs0));
+        ZERO(cs0);
         cs=&cs0;
     }
     len=strlen(s);
@@ -262,7 +262,7 @@ void init_locale(void)
 
 bool new_conv(struct charset_conv *conv, const char *name, int dir)
 {
-    memset(conv, 0, sizeof(struct charset_conv));
+    ZERO(*conv);
     conv->name=name;
     conv->dir=dir;
     if (!strcasecmp(name, "UTF-8") || !strcasecmp(name, "UTF8"))
@@ -285,7 +285,7 @@ bool new_conv(struct charset_conv *conv,
 
 void nullify_conv(struct charset_conv *conv)
 {
-    memset(conv, 0, sizeof(struct charset_conv));
+    ZERO(*conv);
 }
 
 void cleanup_conv(struct charset_conv *conv)
--- kbtin-2.1.orig/user_pipe.c
+++ kbtin-2.1/user_pipe.c
@@ -15,7 +15,7 @@ static void userpipe_init(void)
     tty=isatty(1);
     color=lastcolor=7;
     i_pos=done_input;
-    memset(&outstate, 0, sizeof(outstate));
+    ZERO(outstate);
 }
 
 static void userpipe_textout(const char *txt)
--- kbtin-2.1.orig/user_tty.c
+++ kbtin-2.1/user_tty.c
@@ -714,11 +714,12 @@ static enum
 } state=TS_NORMAL;
 static int bits = 0;
 #define MAXNVAL 10
-static int val[MAXNVAL], nval;
+static int val[MAXNVAL+1], nval;
 static bool usertty_process_kbd(struct session *ses, WC ch)
 {
     char txt[16];
 
+    assert(ses);
 #ifdef KEYBOARD_DEBUG
     if (ch==27)
         tintin_printf(ses, "~5~[~13~ESC~5~]~7~");
@@ -805,8 +806,6 @@ static bool usertty_process_kbd(struct s
                 if (ret(false))
                     redraw_in();
                 touch_bottom();
-                if (!ses)
-                    break;
                 if (hist_num==HISTORY_SIZE-1)
                     break;
                 if (!history[hist_num+1])
@@ -822,8 +821,6 @@ static bool usertty_process_kbd(struct s
                 if (ret(false))
                     redraw_in();
                 touch_bottom();
-                if (!ses)
-                    break;
                 if (hist_num==-1)
                     break;
                 do --hist_num;
@@ -1041,8 +1038,6 @@ static bool usertty_process_kbd(struct s
             touch_bottom();
             if (ret(false))
                 redraw_in();
-            if (!ses)
-                break;
             if (hist_num==HISTORY_SIZE-1 || !history[hist_num+1])
                 break;
             do hist_num++;
@@ -1055,8 +1050,6 @@ static bool usertty_process_kbd(struct s
             touch_bottom();
             if (ret(false))
                 redraw_in();
-            if (!ses)
-                break;
             if (hist_num==-1)
                 break;
             hist_num=-1;
@@ -1614,7 +1607,7 @@ static void usertty_init(void)
 {
     char* term;
 
-    memset(&outstate, 0, sizeof(outstate));
+    ZERO(outstate);
 #ifdef XTERM_TITLE
     xterm=getenv("DISPLAY")&&(getenv("WINDOWID")||getenv("KONSOLE_DCOP_SESSION"));
 #endif
@@ -1663,11 +1656,9 @@ static void usertty_init(void)
 
     sprintf(done_input, "~12~KB~3~tin ~7~%s by ~11~kilobyte@angband.pl~9~\n", VERSION);
     usertty_textout(done_input);
-    {
-        for (int i=0;i<COLS;++i)
-            done_input[i]='-';
-        sprintf(done_input+COLS, "~7~\n");
-    }
+    for (int i=0;i<COLS;++i)
+        done_input[i]='-';
+    sprintf(done_input+COLS, "~7~\n");
     usertty_textout(done_input);
 }
 
