How to portably start in interactive mode for keyboard traps #917
dnewhall
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I ran into this the other day and it took me forever to figure out how to do it, so I'm putting this here for anyone who needs this.
Let's say you decide to use KornShell to write a game or a text editor. You have a script, but you need to run it in interactive mode for the KEYBD traps. However, you cannot pass arguments or redirect input to it or Ksh thinks it's supposed to run a script and falls out of interactive mode.
The only way I've found to get this to work in a platform-independent manner is the following:
This runs the script using
/bin/sh, checks to see if an internal variable is defined, and if not then runs the correctkshin interactive mode with the current script passed as its environment/.kshrcfile.All other attempts I tried using things like
/usr/bin/env -Sor abusingexecdidn't work, or weren't portable.The arguments need to be passed as special variables (
_ARG_1, etc.), because if arguments are passed to an interactive shell, it thinks it's a script and exits when done. We don't want that; we want it to stay running for the keyboard input. Therefore, we define the arguments as_ARG_1, etc. so the code we shoehorn in through the ".kshrc file" can access them.Only the environment variables specified with the call to
/usr/bin/env -iare passed to the script.TERMis needed if you are doing any ANSI escape code shenanigans (which you probably are for an application requiring the keyboard like this). We zero outPS1so it's not displayed and the same withHISTFILEso you can't accidentally scroll through and run commands from the history.The only potential snafu I can identify is if your platform's
/bin/shparses the entire file before running and rejects the KornShell-specific syntax.And the KEYBD traps for the curious:
And, remember, you NEED to call
exitto quit the script if you setignoreeof, so only something like the providedquitshould be used.Beta Was this translation helpful? Give feedback.
All reactions