diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-07-12 12:41:15 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-07-12 14:34:16 +0300 |
commit | 89f200b55b309aa67c1fbfc11d4de211725348f4 (patch) | |
tree | 39e2e43f29766cbebbdf61f160b5d062ea63c996 /src | |
parent | e8cad816f36b1cad11bb67c96f0ce878cf30844e (diff) | |
download | gdbm-89f200b55b309aa67c1fbfc11d4de211725348f4.tar.gz gdbm-89f200b55b309aa67c1fbfc11d4de211725348f4.tar.bz2 |
Line-editing support in gdbmtool
* configure.ac: Check if GNU Readline is available.
* src/Makefile.am: Add new files.
* src/input-rl.c: New file.
* src/input-std.c: New file.
* src/gdbmtool.c (handler_param): Move declaration to
the header file.
(quit_handler): Call input_done.
(command_tab): Add the "history" command.
(command_generator): New function.
(slist_new_s, slist_new_l)
(slist_insert): New functions.
(main): Call input_init and input_done.
* src/gdbmtool.h: New protos.
* src/gram.y: Use slist_insert to construct string lists.
* src/lex.l (read_input): Remove. Use input_read instead.
(print_prompt_at_bol): New function.
(print_prompt): Remove.
(make_prompt): New function.
* NEWS: Document changes.
* README: Document readline support.
* doc/gdbm.texi: Document line editing in gdbmtool.
* doc/gdbmtool.1: Likewise.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 12 | ||||
-rw-r--r-- | src/gdbmtool.c | 102 | ||||
-rw-r--r-- | src/gdbmtool.h | 31 | ||||
-rw-r--r-- | src/gram.y | 7 | ||||
-rw-r--r-- | src/input-rl.c | 235 | ||||
-rw-r--r-- | src/input-std.c | 43 | ||||
-rw-r--r-- | src/lex.l | 134 |
7 files changed, 483 insertions, 81 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 @@ -323,20 +323,6 @@ get_screen_lines () #endif 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 @@ -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 */ @@ -902,7 +890,6 @@ struct command struct argdef args[NARGS]; char *doc; }; - struct command command_tab[] = { #define S(s) #s, sizeof (#s) - 1 @@ -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) { @@ -1193,6 +1235,21 @@ slist_free (struct slist *lp) lp = next; } } + +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]); @@ -1545,15 +1602,18 @@ main (int argc, char *argv[]) textdomain (PACKAGE); sort_commands (); - + /* 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); @@ -104,6 +106,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); @@ -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 */ +} + @@ -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) +void +print_prompt_at_bol (void) { - if (interactive) + if (YY_AT_BOL ()) { - 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,58 +505,92 @@ 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)) { case VAR_OK: 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) + { + slist_insert (&tail, slist_new_l (prompt, s - prompt)); + if (!head) + head = tail; + } + if (expand_char (s[1], &tail) == 0) { - putchar ('%'); - break; + if (!head) + head = tail; + prompt = s + 2; } - expand_char (*s); + else + prompt = s; + s += 2; } else - putchar (*s); + ++s; + } + + if (s > prompt) + { + slist_insert (&tail, slist_new_l (prompt, s - prompt)); + if (!head) + head = tail; } - fflush (stdout); + 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; } |