# Try to find a valid TERM entry.
#
-for term in linux dtterm xterm vt100; do
- if has_term $term; then
- export TERM=$term
- break
+# Note that running answerback will prevent keyboard input, which is annoying
+# if you try to start typing while the shell is starting up.
+term=
+eval $(answerback 2>/dev/null)
+case "$ANSWERBACK" in
+ PuTTY) term=xterm-256color;;
+ urxvt) term=rxvt-unicode;;
+esac
+
+if [ ! -z "$term" ]; then
+ if ! has_term $term; then
+ export TERMINFO=~/.terminfo
fi
-done
+ export TERM=$term
+elif ! has_term $TERM; then
+ for term in rxvt-unicode xterm-256color xterm-88color linux dtterm xterm-color xterm vt100; do
+ if has_term $term; then
+ export TERM=$term
+ break
+ fi
+ done
+fi
-unset term
+unset ANSWERBACK term
--- /dev/null
+/* answerback.c version 0.1
+ * Written by John Newbigin <jn@it.swin.edu.au>
+ * GPL Version 2
+ * Compile with 'gcc -Wall -o answerback answerback.c'
+ * */
+#include <stdio.h>
+#include <unistd.h>
+#include <termios.h>
+#include <signal.h>
+#include <string.h>
+int main(int argc, char **argv)
+{
+ char *name = "ANSWERBACK";
+ char code = 5;
+ char buffer[16];
+ int r;
+ sigset_t sig, sigsave;
+ struct termios term, termsave;
+ FILE *fp;
+ if(argc == 2)
+ {
+ name = argv[1];
+ }
+ if((fp = fopen(ctermid(NULL), "r+")) == NULL)
+ {
+ perror("open");
+ return 0;
+ }
+ setbuf(fp, NULL);
+ sigemptyset(&sig);
+ sigaddset(&sig, SIGINT);
+ sigaddset(&sig, SIGTSTP);
+ sigprocmask(SIG_BLOCK, &sig, &sigsave);
+ tcgetattr(fileno(fp), &termsave);
+ term = termsave;
+ term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON);
+ term.c_cc[VMIN] = 0;
+ term.c_cc[VTIME] = 10;
+ tcsetattr(fileno(fp), TCSAFLUSH, &term);
+ write(fileno(fp), &code, 1);
+ memset(buffer, 0, sizeof(buffer));
+ r = read(fileno(fp), buffer, sizeof(buffer) - 1);
+ tcsetattr(fileno(fp), TCSAFLUSH, &termsave);
+ sigprocmask(SIG_SETMASK, &sigsave, NULL);
+ fclose(fp);
+ if(r > 0)
+ {
+ printf("%s='%s'\n", name, buffer);
+ }
+ return 0;
+}