+ fprintf(stdout, "bti - version %s\n", VERSION);
+}
+
+static char *get_string(const char *name)
+{
+ char *temp;
+ char *string;
+
+ string = zalloc(1000);
+ if (!string)
+ exit(1);
+ if (name != NULL)
+ fprintf(stdout, "%s", name);
+ if (!fgets(string, 999, stdin))
+ return NULL;
+ temp = strchr(string, '\n');
+ if (temp)
+ *temp = '\0';
+ return string;
+}
+
+/*
+ * Try to get a handle to a readline function from a variety of different
+ * libraries. If nothing is present on the system, then fall back to an
+ * internal one.
+ *
+ * Logic originally based off of code in the e2fsutils package in the
+ * lib/ss/get_readline.c file, which is licensed under the MIT license.
+ *
+ * This keeps us from having to relicense the bti codebase if readline
+ * ever changes its license, as there is no link-time dependancy.
+ * It is a run-time thing only, and we handle any readline-like library
+ * in the same manner, making bti not be a derivative work of any
+ * other program.
+ */
+static void session_readline_init(struct session *session)
+{
+ /* Libraries we will try to use for readline/editline functionality */
+ const char *libpath = "libreadline.so.6:libreadline.so.5:"
+ "libreadline.so.4:libreadline.so:libedit.so.2:"
+ "libedit.so:libeditline.so.0:libeditline.so";
+ void *handle = NULL;
+ char *tmp, *cp, *next;
+ int (*bind_key)(int, void *);
+ void (*insert)(void);
+
+ /* default to internal function if we can't or won't find anything */
+ session->readline = get_string;
+ if (!isatty(0))
+ return;
+ session->interactive = 1;
+
+ tmp = malloc(strlen(libpath)+1);
+ if (!tmp)
+ return;
+ strcpy(tmp, libpath);
+ for (cp = tmp; cp; cp = next) {
+ next = strchr(cp, ':');
+ if (next)
+ *next++ = 0;
+ if (*cp == 0)
+ continue;
+ handle = dlopen(cp, RTLD_NOW);
+ if (handle) {
+ dbg("Using %s for readline library\n", cp);
+ break;
+ }
+ }
+ free(tmp);
+ if (!handle) {
+ dbg("No readline library found.\n");
+ return;
+ }
+
+ session->readline_handle = handle;
+ session->readline = (char *(*)(const char *))dlsym(handle, "readline");
+ if (session->readline == NULL) {
+ /* something odd happened, default back to internal stuff */
+ session->readline_handle = NULL;
+ session->readline = get_string;
+ return;
+ }
+
+ /*
+ * If we found a library, turn off filename expansion
+ * as that makes no sense from within bti.
+ */
+ bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
+ insert = (void (*)(void))dlsym(handle, "rl_insert");
+ if (bind_key && insert)
+ bind_key('\t', insert);
+}
+
+static void session_readline_cleanup(struct session *session)
+{
+ if (session->readline_handle)
+ dlclose(session->readline_handle);