summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2022-02-20 21:52:23 +0200
committerSergey Poznyakoff <gray@gnu.org>2022-02-20 21:52:23 +0200
commit6655b123df5ca64327a8f630b7e7d972e253ad73 (patch)
tree45e2ffd6cf1a6b552552cd6ad25cc4f11cb9df77
parentd159e3d81953551bddd929f6a7b9e083198dd933 (diff)
downloadgdbm-6655b123df5ca64327a8f630b7e7d972e253ad73.tar.gz
Use getline in gdbm shell
* configure.ac: Check if getline is available. * tools/gdbmshell.c (argsprep): Use getline. [!HAVE_GETLINE] (getline): Simple replacement.
-rw-r--r--configure.ac2
-rw-r--r--tools/gdbmshell.c43
2 files changed, 39 insertions, 6 deletions
diff --git a/configure.ac b/configure.ac
index 4841b02..3547d9c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -121,7 +121,7 @@ AM_GNU_GETTEXT_VERSION
AC_CHECK_HEADERS([sys/file.h string.h locale.h getopt.h])
-AC_CHECK_FUNCS([ftruncate flock lockf fsync setlocale getopt_long])
+AC_CHECK_FUNCS([ftruncate flock lockf fsync setlocale getopt_long getline])
if test x$mapped_io = xyes
then
diff --git a/tools/gdbmshell.c b/tools/gdbmshell.c
index 22c4938..1d7a29f 100644
--- a/tools/gdbmshell.c
+++ b/tools/gdbmshell.c
@@ -2929,14 +2929,43 @@ timing_stop (struct timing *t)
t->sys = timeval_sub (r.ru_stime, t->sys);
}
+#ifndef HAVE_GETLINE
+ssize_t
+getline (char **pbuf, size_t *psize, FILE *fp)
+{
+ char *buf = *pbuf;
+ size_t size = *psize;
+ ssize_t off = 0;
+
+ do
+ {
+ if (!buf || size == 0 || off == size - 1)
+ {
+ buf = e2nrealloc (buf, &size, 1);
+ }
+ if (!fgets (buf + off, size - off, fp))
+ {
+ if (off == 0)
+ off = -1;
+ break;
+ }
+ off += strlen (buf + off);
+ }
+ while (buf[off - 1] != '\n');
+
+ *pbuf = buf;
+ *psize = size;
+ return off;
+}
+#endif
+
static int
argsprep (struct command *cmd, struct gdbmarglist *arglist,
struct command_param *param)
{
int i;
struct gdbmarg *arg = arglist ? arglist->head : NULL;
- char argbuf[128];
-
+
for (i = 0; cmd->args[i].name && arg; i++, arg = arg->next)
{
if (param_push_arg (param, arg, &cmd->args[i]))
@@ -2946,6 +2975,9 @@ argsprep (struct command *cmd, struct gdbmarglist *arglist,
for (; cmd->args[i].name; i++)
{
char *argname = cmd->args[i].name;
+ char *argbuf = NULL;
+ size_t argsize =0;
+ ssize_t n;
struct gdbmarg *t;
if (*argname == '[')
@@ -2959,15 +2991,16 @@ argsprep (struct command *cmd, struct gdbmarglist *arglist,
}
printf ("%s? ", argname);
fflush (stdout);
- if (fgets (argbuf, sizeof argbuf, stdin) == NULL)
+ errno = 0;
+ if ((n = getline (&argbuf, &argsize, stdin)) < 0)
{
- terror ("%s", _("unexpected eof"));
+ terror ("%s", errno ? strerror (errno) : _("unexpected eof"));
return 1;
}
trimnl (argbuf);
- t = gdbmarg_string (estrdup (argbuf), &yylloc);
+ t = gdbmarg_string (argbuf, &yylloc);
if (param_push_arg (param, t, &cmd->args[i]))
{
gdbmarg_free (t);