Author: allison
Date: Tue Dec 9 10:23:14 2008
New Revision: 33718
Modified:
trunk/src/pmc/filehandle.pmc
Log:
[I/O] Adding an interactive readline method.
Modified: trunk/src/pmc/filehandle.pmc
==============================================================================
--- trunk/src/pmc/filehandle.pmc (original)
+++ trunk/src/pmc/filehandle.pmc Tue Dec 9 10:23:14 2008
@@ -21,6 +21,17 @@
#include "parrot/parrot.h"
#include "../src/io/io_private.h"
+#ifdef PARROT_HAS_READLINE
+#ifdef __cplusplus
+extern "C" {
+#endif
+ char *readline(const char *);
+ void add_history(const char*);
+#ifdef __cplusplus
+}
+#endif
+#endif
+
pmclass FileHandle need_ext {
ATTR INTVAL flags; /* Filehandle flags */
@@ -267,6 +278,36 @@
/*
+=item C<METHOD readline_interactive(STRING *prompt)>
+
+Read a line from the filehandle and return it in a string.
+
+=cut
+
+*/
+
+ METHOD readline_interactive(STRING *prompt :optional, INTVAL got_prompt
:opt_flag) {
+ STRING *string_result;
+#ifdef PARROT_HAS_READLINE
+ char * const r = readline(got_prompt ? prompt->strstart : NULL);
+
+ if (r) {
+ if (*r)
+ add_history(r);
+
+ string_result = string_from_cstring(INTERP, r, 0);
+ mem_sys_free(r);
+ RETURN(STRING *string_result);
+ }
+#endif
+
+ string_result = CONST_STRING(INTERP, "");
+
+ RETURN(STRING *string_result);
+ }
+
+/*
+
=item METHOD readall(STRING *name);
Read the entire contents of a file named I<name> into a Parrot string. On a