aboutsummaryrefslogtreecommitdiff
path: root/src/input-rl.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2018-05-23 19:18:14 +0300
committerSergey Poznyakoff <gray@gnu.org>2018-05-23 19:18:14 +0300
commit7edbff3372dfd387e4f6da45f43ba8ada6dfbe43 (patch)
treed3049ab8c07773c82104e720f92be555ae276179 /src/input-rl.c
parent008b71a4d58ad33cf5a41e2aa55b9393e8420531 (diff)
downloadgdbm-7edbff3372dfd387e4f6da45f43ba8ada6dfbe43.tar.gz
gdbm-7edbff3372dfd387e4f6da45f43ba8ada6dfbe43.tar.bz2
gdbmtool: accept commands from command line as well as from file
Commands can be supplied to gdbmtool in three ways: 1. From a file, using the --file (-f) option: gdbmtool -f comfile 2. From the command line, if first argument is the database name: gdbmtool test.db count \; fetch mykey \; avail Note the use of semicolon to delimit commands. 3. From the interactive shell, if neither of the above is used. * src/Makefile.am: Add new sources. * src/gdbmtool.c: Use new stream functions for input. * src/gdbmtool.h (setsource): Remove proto. (instream_t): New data type. (instream_name, instream_read) (instream_close, instream_interactive) (instream_eq): New functions. (instream_stdin_create) (instream_argv_create) (instream_file_create) (interactive, input_context_push): New protos. * src/gram.y: Accept ; in place of newline. * src/input-argv.c: New file. * src/input-file.c: New file. * src/input-rl.c: Rewrite to provide instream_t API. * src/input-std.c: Likewise. * src/lex.l: Rewrite. * tests/.gitignore: Update. * tests/Makefile.am: Add new tests. Incorporate DejaGNU tests. * tests/config/default.exp: New file. * tests/gdbmtool/base.exp: New file. * tests/gdbmtool00.at: New file. * tests/gdbmtool01.at: New file. * tests/gdbmtool02.at: New file. * tests/testsuite.at: Include new tests.
Diffstat (limited to 'src/input-rl.c')
-rw-r--r--src/input-rl.c153
1 files changed, 90 insertions, 63 deletions
diff --git a/src/input-rl.c b/src/input-rl.c
index d09feff..695d351 100644
--- a/src/input-rl.c
+++ b/src/input-rl.c
@@ -62,64 +62,6 @@ retrieve_history (char *str)
return 0;
}
-ssize_t
-input_read (FILE *fp, char *buf, size_t size)
-{
- static char *input_line;
- static size_t input_length;
- static size_t input_off;
-#define input_ptr() (input_line + input_off)
-#define input_size() (input_length - input_off)
-
- if (interactive)
- {
- size_t len = input_size ();
- if (!len)
- {
- if (input_line)
- {
- newline:
- free (input_line);
- input_line = NULL;
- buf[0] = '\n';
- return 1;
- }
- else
- {
- char *prompt;
- again:
- prompt = make_prompt ();
- input_line = readline (prompt);
- free (prompt);
- if (!input_line)
- return 0;
- input_length = strlen (input_line);
- input_off = 0;
- if (input_length)
- {
- if (retrieve_history (input_line))
- {
- free (input_line);
- goto again;
- }
- }
- else
- goto newline;
- len = input_size ();
- add_history (input_line);
- }
- }
-
- if (len > size)
- len = size;
- memcpy (buf, input_ptr (), len);
- input_off += len;
-
- return len;
- }
- return fread (buf, 1, size, fp);
-}
-
struct history_param
{
int from;
@@ -180,7 +122,7 @@ input_history_handler (struct handler_param *param)
#define HISTFILE_SUFFIX "_history"
static char *
-get_history_file_name ()
+get_history_file_name (void)
{
static char *filename = NULL;
@@ -222,14 +164,99 @@ input_init (void)
rl_readline_name = (char *) progname;
rl_attempted_completion_function = shell_completion;
rl_pre_input_hook = pre_input;
- if (interactive)
- read_history (get_history_file_name ());
+ read_history (get_history_file_name ());
}
void
input_done (void)
{
- if (interactive)
- write_history (get_history_file_name ());
+ write_history (get_history_file_name ());
+}
+
+static void
+instream_stdin_close (instream_t istr)
+{
+ free (istr);
+}
+
+static ssize_t
+stdin_read_readline (instream_t istr, char *buf, size_t size)
+{
+ static char *input_line;
+ static size_t input_length;
+ static size_t input_off;
+#define input_ptr() (input_line + input_off)
+#define input_size() (input_length - input_off)
+ size_t len = input_size ();
+ if (!len)
+ {
+ if (input_line)
+ {
+ newline:
+ free (input_line);
+ input_line = NULL;
+ buf[0] = '\n';
+ return 1;
+ }
+ else
+ {
+ char *prompt;
+ again:
+ prompt = make_prompt ();
+ input_line = readline (prompt);
+ free (prompt);
+ if (!input_line)
+ return 0;
+ input_length = strlen (input_line);
+ input_off = 0;
+ if (input_length)
+ {
+ if (retrieve_history (input_line))
+ {
+ free (input_line);
+ goto again;
+ }
+ }
+ else
+ goto newline;
+ len = input_size ();
+ add_history (input_line);
+ }
+ }
+
+ if (len > size)
+ len = size;
+ memcpy (buf, input_ptr (), len);
+ input_off += len;
+
+ return len;
+}
+
+static ssize_t
+instream_stdin_read (instream_t istr, char *buf, size_t size)
+{
+ if (istr->in_inter)
+ return stdin_read_readline (istr, buf, size);
+ return fread (buf, 1, size, stdin);
+}
+
+static int
+instream_stdin_eq (instream_t a, instream_t b)
+{
+ return 0;
}
+instream_t
+instream_stdin_create (void)
+{
+ struct instream *istr;
+
+ istr = emalloc (sizeof *istr);
+ istr->in_name = "stdin";
+ istr->in_inter = isatty (fileno (stdin));
+ istr->in_read = instream_stdin_read;
+ istr->in_close = instream_stdin_close;
+ istr->in_eq = instream_stdin_eq;
+
+ return istr;
+}

Return to:

Send suggestions and report system problems to the System administrator.