From bdeb4bab506946f8b13a3a0cbd0f01324bafc4bf Mon Sep 17 00:00:00 2001 From: Anton Blanchard Date: Thu, 18 Feb 2021 11:26:02 +1100 Subject: [PATCH] Restore terminal if exit is called inside a TCL script at startup If exit is called in a TCL script that is executed at startup, the libc exit() function is called directly and we don't get a chance to reset the terminal. We return to the shell with echo off, and have to run "reset". A simple example: echo exit > test.tcl magic -noconsole -dnull test.tcl There are a few ways we could solve this. We could register an exit handler using atexit(). Here I use Tcl_SetExitProc() to register a callback with the TCL interpreter. --- utils/main.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/utils/main.c b/utils/main.c index 55988732..06e06c7d 100644 --- a/utils/main.c +++ b/utils/main.c @@ -770,6 +770,12 @@ mainInitAfterArgs() return 0; } +void tcl_exit_hook(ClientData clientData) +{ + TxResetTerminal(); + exit(0); +} + /* * ---------------------------------------------------------------------------- * mainInitFinal: @@ -794,6 +800,9 @@ mainInitFinal() char *rname; int result; + /* Reset terminal if exit is called inside a TCL script */ + Tcl_SetExitProc(tcl_exit_hook); + #ifdef MAGIC_WRAPPER /* Read in system pre-startup file, if it exists. */ @@ -1187,6 +1196,8 @@ mainInitFinal() UndoFlush(); TxClearPoint(); + Tcl_SetExitProc(NULL); + return 0; }