From 05ac5124c8fc1f8ff09526bb6aa8c58dd94c5105 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fredrik=20H=C3=BCbinette=20=28Hubbe=29?= <hubbe@hubbe.net>
Date: Mon, 8 Jul 1996 23:58:27 +0200
Subject: [PATCH] readline module

Rev: src/modules/readlinemod/Makefile.src:1.1
Rev: src/modules/readlinemod/configure.in:1.1
Rev: src/modules/readlinemod/readline_machine.h.in:1.1
Rev: src/modules/readlinemod/readlinemod.c:1.1
---
 src/modules/readlinemod/Makefile.src          | 19 ++++
 src/modules/readlinemod/configure.in          | 15 ++++
 src/modules/readlinemod/readline_machine.h.in | 16 ++++
 src/modules/readlinemod/readlinemod.c         | 90 +++++++++++++++++++
 4 files changed, 140 insertions(+)
 create mode 100644 src/modules/readlinemod/Makefile.src
 create mode 100644 src/modules/readlinemod/configure.in
 create mode 100644 src/modules/readlinemod/readline_machine.h.in
 create mode 100644 src/modules/readlinemod/readlinemod.c

diff --git a/src/modules/readlinemod/Makefile.src b/src/modules/readlinemod/Makefile.src
new file mode 100644
index 0000000000..46940d3472
--- /dev/null
+++ b/src/modules/readlinemod/Makefile.src
@@ -0,0 +1,19 @@
+SRCDIR=@srcdir@
+VPATH=@srcdir@:@srcdir@/../..:../..
+PREFLAGS=-I. -I$(SRCDIR) -I$(SRCDIR)/../.. -I../..
+CFLAGS=$(PREFLAGS) $(OTHERFLAGS)
+
+FILES=readlinemod.o
+LIB=readlinemod.a
+
+$(LIB): $(FILES)
+	-rm -f $(LIB)
+	ar cq $(LIB) $(FILES)
+	-@RANLIB@ $(LIB)
+	echo >linker_options @LIBS@
+
+clean:
+	-rm -f *.o *.a
+
+depend:
+	gcc -MM $(PREFLAGS) $(SRCDIR)/*.c | $(FIXDEP) $(SRCDIR)
diff --git a/src/modules/readlinemod/configure.in b/src/modules/readlinemod/configure.in
new file mode 100644
index 0000000000..64b90a5b5b
--- /dev/null
+++ b/src/modules/readlinemod/configure.in
@@ -0,0 +1,15 @@
+AC_INIT(readlinemod.c)
+AC_CONFIG_HEADER(readline_machine.h)
+
+AC_PROG_CC
+AC_PROG_RANLIB
+AC_SUBST(RANLIB)
+
+AC_CHECK_HEADERS(readline.h)
+AC_CHECK_HEADERS(history.h)
+AC_CHECK_LIB(termcap, tputs)
+AC_CHECK_LIB(readline, readline)
+
+AC_OUTPUT(Makefile,echo FOO >stamp-h )
+
+
diff --git a/src/modules/readlinemod/readline_machine.h.in b/src/modules/readlinemod/readline_machine.h.in
new file mode 100644
index 0000000000..df03aee30d
--- /dev/null
+++ b/src/modules/readlinemod/readline_machine.h.in
@@ -0,0 +1,16 @@
+#ifndef GDBM_MACHINE_H
+#define GDBM_MACHINE_H
+
+/* Define this if you have <readline.h> */
+#undef HAVE_READLINE_H
+
+/* Define this if you have <history.h> */
+#undef HAVE_HISTORY_H
+
+/* Define this if you have -lreadline */
+#undef HAVE_LIBREADLINE
+
+/* Define this if you have -ltermcap */
+#undef HAVE_LIBTERMCAP
+
+#endif
diff --git a/src/modules/readlinemod/readlinemod.c b/src/modules/readlinemod/readlinemod.c
new file mode 100644
index 0000000000..a84802d775
--- /dev/null
+++ b/src/modules/readlinemod/readlinemod.c
@@ -0,0 +1,90 @@
+/*\
+||| This file a part of uLPC, and is copyright by Fredrik Hubinette
+||| uLPC is distributed as GPL (General Public License)
+||| See the files COPYING and DISCLAIMER for more information.
+\*/
+#include "global.h"
+#include "readline_machine.h"
+#include "types.h"
+#include "interpret.h"
+#include "svalue.h"
+#include "stralloc.h"
+#include "array.h"
+#include "object.h"
+#include "macros.h"
+
+#if !defined(HAVE_READLINE_H) || !defined(HAVE_HISTORY_H)
+#undef HAVE_LIBREADLINE
+#endif
+
+#ifdef HAVE_LIBREADLINE
+
+#include <readline.h>
+#include <history.h>
+
+static void f_readline(INT32 args)
+{
+  char *r;
+  if(args < 1)
+    error("Too few arguments to readline().\n");
+
+  if(sp[-args].type != T_STRING)
+    error("Bad argument 1 to readline()\n");
+
+  r=readline(sp[-args].u.string->str);
+  pop_n_elems(args);
+  if(r)
+  {
+    if(*r) add_history(r);
+    push_string(make_shared_string(r));
+    free(r);
+  } else {
+    push_int(0);
+  }
+}
+
+#else
+
+#include <stdio.h>
+
+#define BLOCK 16384
+
+static void f_readline(INT32 args)
+{
+  char line[BLOCK];
+  char *r;
+  if(args < 1)
+    error("Too few arguments to readline().\n");
+
+  if(sp[-args].type != T_STRING)
+    error("Bad argument 1 to readline()\n");
+
+  puts(sp[-args].u.string->str);
+
+  pop_n_elems(args);
+  if(fgets(line,BLOCK,stdin))
+  {
+    INT32 len;
+    if(len=strlen(line))
+    {
+      if(line[len-1]=='\n')
+      {
+	push_string(make_shared_binary_string(line,len-1));
+	return;
+      }
+    }
+  }
+  push_int(0);
+}
+
+#endif
+
+void init_readlinemod_efuns(void)
+{
+  rl_bind_key('\t', rl_insert);
+  add_efun("readline",f_readline,"function(string:string)",OPT_SIDE_EFFECT);
+}
+void exit_readlinemod(void) {}
+void init_readlinemod_programs(void) { }
+
+
-- 
GitLab