aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am12
-rw-r--r--src/gdbmtool.c100
-rw-r--r--src/gdbmtool.h31
-rw-r--r--src/gram.y7
-rw-r--r--src/input-rl.c235
-rw-r--r--src/input-std.c43
-rw-r--r--src/lex.l130
7 files changed, 480 insertions, 78 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 6a05634..b55deb6 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -79,7 +79,11 @@ libgdbmapp_a_SOURCES =\
# Programs
bin_PROGRAMS = gdbmtool gdbm_load gdbm_dump
-gdbmtool_LDADD = ./libgdbmapp.a ./libgdbm.la
+gdbmtool_LDADD = \
+ ./libgdbmapp.a\
+ ./libgdbm.la\
+ @READLINE_LIBS@
+
gdbmtool_SOURCES = \
datconv.c\
gram.y\
@@ -89,6 +93,12 @@ gdbmtool_SOURCES = \
var.c\
util.c
+if GDBM_COND_READLINE
+ gdbmtool_SOURCES += input-rl.c
+else
+ gdbmtool_SOURCES += input-std.c
+endif
+
AM_YFLAGS = -dtv
#AM_LFLAGS = -d
diff --git a/src/gdbmtool.c b/src/gdbmtool.c
index 65b6740..2d57cbc 100644
--- a/src/gdbmtool.c
+++ b/src/gdbmtool.c
@@ -324,20 +324,6 @@ get_screen_lines ()
return -1;
}
-
-#define ARG_UNUSED __attribute__ ((__unused__))
-
-#define NARGS 5
-
-struct handler_param
-{
- int argc;
- struct gdbmarg **argv;
- FILE *fp;
- void *data;
-};
-
-
/* Open database */
void
open_handler (struct handler_param *param)
@@ -751,7 +737,7 @@ quit_handler (struct handler_param *param ARG_UNUSED)
{
if (gdbm_file != NULL)
gdbm_close (gdbm_file);
-
+ input_done ();
exit (EXIT_OK);
}
@@ -891,6 +877,8 @@ struct argdef
int ds;
};
+#define NARGS 5
+
struct command
{
char *name; /* Command name */
@@ -903,7 +891,6 @@ struct command
char *doc;
};
-
struct command command_tab[] = {
#define S(s) #s, sizeof (#s) - 1
{ S(count), T_CMD,
@@ -1007,6 +994,13 @@ struct command command_tab[] = {
NULL, open_handler, NULL,
{ { "FILE", GDBM_ARG_STRING }, { NULL } },
N_("open new database") },
+#ifdef WITH_READLINE
+ { S(history), T_CMD,
+ input_history_begin, input_history_handler, NULL,
+ { { N_("[FROM]"), GDBM_ARG_STRING },
+ { N_("[COUNT]"), GDBM_ARG_STRING },
+ { NULL } }, N_("show input history") },
+#endif
#undef S
{ 0 }
};
@@ -1026,6 +1020,39 @@ sort_commands ()
sizeof (command_tab[0]), cmdcmp);
}
+/* Generator function for command completion. STATE lets us know whether
+ to start from scratch; without any state (i.e. STATE == 0), then we
+ start at the top of the list. */
+char *
+command_generator (const char *text, int state)
+{
+ const char *name;
+ static int len;
+ static struct command *cmd;
+
+ /* If this is a new word to complete, initialize now. This includes
+ saving the length of TEXT for efficiency, and initializing the index
+ variable to 0. */
+ if (!state)
+ {
+ cmd = command_tab;
+ len = strlen (text);
+ }
+
+ if (!cmd->name)
+ return NULL;
+
+ /* Return the next name which partially matches from the command list. */
+ while ((name = cmd->name))
+ {
+ cmd++;
+ if (strncmp (name, text, len) == 0)
+ return strdup (name);
+ }
+
+ /* If no names matched, then return NULL. */
+ return NULL;
+}
/* ? - help handler */
#define CMDCOLS 30
@@ -1174,7 +1201,7 @@ gdbmarg_kvpair (struct kvpair *kvp, struct locus *loc)
}
struct slist *
-slist_new (char *s)
+slist_new_s (char *s)
{
struct slist *lp = emalloc (sizeof (*lp));
lp->next = NULL;
@@ -1182,6 +1209,21 @@ slist_new (char *s)
return lp;
}
+struct slist *
+slist_new (char const *s)
+{
+ return slist_new_s (estrdup (s));
+}
+
+struct slist *
+slist_new_l (char const *s, size_t l)
+{
+ char *copy = emalloc (l + 1);
+ memcpy (copy, s, l);
+ copy[l] = 0;
+ return slist_new_s (copy);
+}
+
void
slist_free (struct slist *lp)
{
@@ -1194,6 +1236,21 @@ slist_free (struct slist *lp)
}
}
+void
+slist_insert (struct slist **where, struct slist *what)
+{
+ if (*where)
+ {
+ while (what->next)
+ what = what->next;
+ what->next = (*where)->next;
+ (*where)->next = what;
+ }
+ else
+ what->next = NULL;
+ *where = what;
+}
+
struct kvpair *
kvpair_string (struct locus *loc, char *val)
{
@@ -1216,7 +1273,6 @@ kvpair_list (struct locus *loc, struct slist *s)
return p;
}
-
static void
kvlist_free (struct kvpair *kvp)
{
@@ -1534,6 +1590,7 @@ main (int argc, char *argv[])
int opt;
int bv;
int norc = 0;
+ int res;
char *source = "-";
set_progname (argv[0]);
@@ -1548,12 +1605,15 @@ main (int argc, char *argv[])
/* Initialize variables. */
intr = isatty (0);
+ interactive = intr; /* Used early by input_init */
dsdef[DS_KEY] = dsegm_new_field (datadef_lookup ("string"), NULL, 1);
dsdef[DS_CONTENT] = dsegm_new_field (datadef_lookup ("string"), NULL, 1);
variable_set ("open", VART_STRING, "wrcreat");
variable_set ("pager", VART_STRING, getenv ("PAGER"));
+ input_init ();
+
for (opt = parseopt_first (argc, argv, optab);
opt != EOF;
opt = parseopt_next ())
@@ -1640,5 +1700,7 @@ main (int argc, char *argv[])
if (setsource (source, intr))
exit (EXIT_FATAL);
- return yyparse ();
+ res = yyparse ();
+ input_done ();
+ return res;
}
diff --git a/src/gdbmtool.h b/src/gdbmtool.h
index 84f1409..d0e9aa0 100644
--- a/src/gdbmtool.h
+++ b/src/gdbmtool.h
@@ -24,6 +24,8 @@
#include <stdarg.h>
#include <ctype.h>
+#define ARG_UNUSED __attribute__ ((__unused__))
+
/* Position in input file */
struct point
{
@@ -94,7 +96,7 @@ void lerror (struct locus *loc, const char *fmt, ...);
void terror (const char *fmt, ...);
-void print_prompt (void);
+char *make_prompt (void);
int setsource (const char *filename, int intr);
@@ -105,6 +107,17 @@ extern int open_mode;
#define GDBMTOOLRC ".gdbmtoolrc"
#define GDBMTOOL_DEFFILE "junk.gdbm"
+ssize_t input_read (FILE *fp, char *buf, size_t size);
+void input_init (void);
+void input_done (void);
+
+struct handler_param;
+void input_history_handler (struct handler_param *param);
+int input_history_begin (struct handler_param *param, size_t *exp_count);
+
+void print_prompt_at_bol (void);
+char *command_generator (const char *text, int state);
+
struct slist
{
@@ -112,8 +125,11 @@ struct slist
char *str;
};
-struct slist *slist_new (char *s);
+struct slist *slist_new (char const *s);
+struct slist *slist_new_s (char *s);
+struct slist *slist_new_l (char const *s, size_t l);
void slist_free (struct slist *);
+void slist_insert (struct slist **where, struct slist *what);
#define KV_STRING 0
#define KV_LIST 1
@@ -161,6 +177,14 @@ struct gdbmarglist
struct gdbmarg *head, *tail;
};
+struct handler_param
+{
+ int argc;
+ struct gdbmarg **argv;
+ FILE *fp;
+ void *data;
+};
+
void gdbmarglist_init (struct gdbmarglist *, struct gdbmarg *);
void gdbmarglist_add (struct gdbmarglist *, struct gdbmarg *);
void gdbmarglist_free (struct gdbmarglist *lst);
@@ -262,3 +286,6 @@ char *mkfilename (const char *dir, const char *file, const char *suf);
char *tildexpand (char *s);
int vgetyn (const char *prompt, va_list ap);
int getyn (const char *prompt, ...);
+
+int getnum (int *pnum, char *arg, char **endp);
+int get_screen_lines (void);
diff --git a/src/gram.y b/src/gram.y
index 9a6e3c2..2f6d7c0 100644
--- a/src/gram.y
+++ b/src/gram.y
@@ -168,13 +168,12 @@ value : string
slist : string
{
- $$.head = $$.tail = slist_new ($1);
+ $$.head = $$.tail = slist_new_s ($1);
}
| slist ',' string
{
- struct slist *s = slist_new ($3);
- $1.tail->next = s;
- $1.tail = s;
+ struct slist *s = slist_new_s ($3);
+ slist_insert (&$1.tail, s);
$$ = $1;
}
;
diff --git a/src/input-rl.c b/src/input-rl.c
new file mode 100644
index 0000000..f435ed4
--- /dev/null
+++ b/src/input-rl.c
@@ -0,0 +1,235 @@
+/* This file is part of GDBM, the GNU data base manager.
+ Copyright (C) 2016 Free Software Foundation, Inc.
+
+ GDBM is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ GDBM is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "gdbmtool.h"
+#include <readline/readline.h>
+#include <readline/history.h>
+
+static char *pre_input_line;
+
+static int
+pre_input (void)
+{
+ if (pre_input_line)
+ {
+ rl_insert_text (pre_input_line);
+ free (pre_input_line);
+ pre_input_line = NULL;
+ rl_redisplay ();
+ }
+ return 0;
+}
+
+static int
+retrieve_history (char *str)
+{
+ char *out;
+ int rc;
+
+ rc = history_expand (str, &out);
+ switch (rc)
+ {
+ case -1:
+ yyerror (out);
+ free (out);
+ return 1;
+
+ case 0:
+ break;
+
+ case 1:
+ pre_input_line = out;
+ return 1;
+
+ case 2:
+ printf ("%s\n", out);
+ free (out);
+ return 1;
+ }
+ 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;
+ int count;
+};
+
+int
+input_history_begin (struct handler_param *param, size_t *exp_count)
+{
+ struct history_param *p;
+ int from = 0, count = history_length;
+
+ switch (param->argc)
+ {
+ case 1:
+ if (getnum (&count, param->argv[0]->v.string, NULL))
+ return 1;
+ if (count > history_length)
+ count = history_length;
+ else
+ from = history_length - count;
+ break;
+
+ case 2:
+ if (getnum (&from, param->argv[0]->v.string, NULL))
+ return 1;
+ if (from)
+ --from;
+ if (getnum (&count, param->argv[1]->v.string, NULL))
+ return 1;
+
+ if (count > history_length)
+ count = history_length;
+ }
+ p = emalloc (sizeof *p);
+ p->from = from;
+ p->count = count;
+ param->data = p;
+ if (exp_count)
+ *exp_count = count;
+ return 0;
+}
+
+void
+input_history_handler (struct handler_param *param)
+{
+ struct history_param *p = param->data;
+ int i;
+ HIST_ENTRY **hlist;
+ FILE *fp = param->fp;
+
+ hlist = history_list ();
+ for (i = 0; i < p->count; i++)
+ fprintf (fp, "%4d) %s\n", p->from + i + 1, hlist[p->from + i]->line);
+}
+
+#define HISTFILE_PREFIX "~/."
+#define HISTFILE_SUFFIX "_history"
+
+static char *
+get_history_file_name ()
+{
+ static char *filename = NULL;
+
+ if (!filename)
+ {
+ char *hname;
+
+ hname = emalloc (sizeof HISTFILE_PREFIX + strlen (rl_readline_name) +
+ sizeof HISTFILE_SUFFIX - 1);
+ strcpy (hname, HISTFILE_PREFIX);
+ strcat (hname, rl_readline_name);
+ strcat (hname, HISTFILE_SUFFIX);
+ filename = tildexpand (hname);
+ free (hname);
+ }
+ return filename;
+}
+
+static char **
+shell_completion (const char *text, int start, int end)
+{
+ char **matches;
+
+ matches = (char **) NULL;
+
+ /* If this word is at the start of the line, then it is a command
+ to complete. Otherwise it is the name of a file in the current
+ directory. */
+ if (start == 0)
+ matches = rl_completion_matches (text, command_generator);
+
+ return (matches);
+}
+
+void
+input_init (void)
+{
+ /* Allow conditional parsing of the ~/.inputrc file. */
+ 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 ());
+}
+
+void
+input_done (void)
+{
+ if (interactive)
+ write_history (get_history_file_name ());
+}
+
diff --git a/src/input-std.c b/src/input-std.c
new file mode 100644
index 0000000..4508368
--- /dev/null
+++ b/src/input-std.c
@@ -0,0 +1,43 @@
+/* This file is part of GDBM, the GNU data base manager.
+ Copyright (C) 2016 Free Software Foundation, Inc.
+
+ GDBM is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ GDBM is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "gdbmtool.h"
+
+ssize_t
+input_read (FILE *fp, char *buf, size_t size)
+{
+ if (interactive)
+ {
+ print_prompt_at_bol ();
+ if (fgets (buf, size, fp) == NULL)
+ return 0;
+ return strlen (buf);
+ }
+ return fread (buf, 1, size, fp);
+}
+
+void
+input_init (void)
+{
+ /* nothing */
+}
+
+void
+input_done (void)
+{
+ /* nothing */
+}
+
diff --git a/src/lex.l b/src/lex.l
index 9c2c5f8..236474d 100644
--- a/src/lex.l
+++ b/src/lex.l
@@ -46,7 +46,7 @@ advance_line ()
#define YY_INPUT(buf,result,max_size) \
do \
{ \
- result = read_input (buf, max_size); \
+ result = input_read (yyin, buf, max_size); \
} \
while (0);
@@ -56,8 +56,6 @@ void string_addc (int c);
char *string_end (void);
int unescape (int c);
-static ssize_t read_input (char *buf, size_t size);
-
struct context /* Input context */
{
struct context *parent; /* Pointer to the parent context */
@@ -315,18 +313,16 @@ end_def (void)
BEGIN (INITIAL);
}
-static ssize_t
-read_input (char *buf, size_t size)
-{
- if (interactive)
+void
+print_prompt_at_bol (void)
{
if (YY_AT_BOL ())
- print_prompt ();
- if (fgets (buf, size, yyin) == NULL)
- return 0;
- return strlen (buf);
+ {
+ char *s = make_prompt ();
+ fputs (s, stdout);
+ fflush (stdout);
+ free (s);
}
- return fread (buf, 1, size, yyin);
}
@@ -453,44 +449,40 @@ lerror (struct locus *loc, const char *fmt, ...)
}
-struct prompt_exp;
-
-void
-pe_file_name (struct prompt_exp *p)
+static struct slist *
+pe_file_name (void)
{
- if (file_name)
- fwrite (file_name, strlen (file_name), 1, stdout);
+ return file_name ? slist_new (file_name) : NULL;
}
-void
-pe_program_name (struct prompt_exp *p)
+static struct slist *
+pe_program_name (void)
{
- fwrite (progname, strlen (progname), 1, stdout);
+ return slist_new (progname);
}
-void
-pe_package_name (struct prompt_exp *p)
+static struct slist *
+pe_package_name (void)
{
- fwrite (PACKAGE_NAME, sizeof (PACKAGE_NAME) - 1, 1, stdout);
+ return slist_new (PACKAGE_NAME);
}
-void
-pe_program_version (struct prompt_exp *p)
+static struct slist *
+pe_program_version (void)
{
- fwrite (PACKAGE_VERSION, sizeof (PACKAGE_VERSION) - 1, 1, stdout);
+ return slist_new (PACKAGE_VERSION);
}
-void
-pe_space (struct prompt_exp *p)
+static struct slist *
+pe_space (void)
{
- fwrite (" ", 1, 1, stdout);
+ return slist_new (" ");
}
struct prompt_exp
{
int ch;
- void (*fun) (struct prompt_exp *);
- char *cache;
+ struct slist *(*fun) (void);
};
struct prompt_exp prompt_exp[] = {
@@ -502,8 +494,8 @@ struct prompt_exp prompt_exp[] = {
{ 0 }
};
-static void
-expand_char (int c)
+static int
+expand_char (int c, struct slist **tailp)
{
struct prompt_exp *p;
@@ -513,30 +505,32 @@ expand_char (int c)
{
if (c == p->ch)
{
- if (p->cache)
- free (p->cache);
- p->fun (p);
- return;
+ struct slist *s = p->fun ();
+ if (s)
+ slist_insert (tailp, s);
+ return 0;
}
}
}
- putchar ('%');
- putchar (c);
+ return 1;
}
char const *
-psname ()
+psname (void)
{
if (YYSTATE == DEF || YYSTATE == MLSTR)
return "ps2";
return "ps1";
}
-void
-print_prompt ()
+char *
+make_prompt (void)
{
const char *s;
const char *prompt;
+ struct slist *head = NULL, *tail = NULL, *p;
+ char *ret, *end;
+ size_t len;
switch (variable_get (psname (), VART_STRING, (void *) &prompt))
{
@@ -544,27 +538,59 @@ print_prompt ()
break;
case VAR_ERR_NOTSET:
- return;
+ return NULL;
default:
abort ();
}
- for (s = prompt; *s; s++)
+ for (s = prompt; *s; )
{
- if (*s == '%')
+ if (*s == '%' && s[1])
{
- if (!*++s)
+ if (s > prompt)
{
- putchar ('%');
- break;
+ slist_insert (&tail, slist_new_l (prompt, s - prompt));
+ if (!head)
+ head = tail;
+ }
+ if (expand_char (s[1], &tail) == 0)
+ {
+ if (!head)
+ head = tail;
+ prompt = s + 2;
}
- expand_char (*s);
+ else
+ prompt = s;
+ s += 2;
}
else
- putchar (*s);
+ ++s;
}
- fflush (stdout);
+ if (s > prompt)
+ {
+ slist_insert (&tail, slist_new_l (prompt, s - prompt));
+ if (!head)
+ head = tail;
+ }
+
+ len = 0;
+ for (p = head; p; p = p->next)
+ len += strlen (p->str);
+
+ ret = emalloc (len + 1);
+ end = ret;
+ for (p = head; p; p = p->next)
+ {
+ s = p->str;
+ while (*s)
+ *end++ = *s++;
+ }
+ *end = 0;
+
+ slist_free (head);
+
+ return ret;
}

Return to:

Send suggestions and report system problems to the System administrator.