aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--doc/gdbm.texi1
-rw-r--r--src/Makefile.am4
-rw-r--r--src/gdbmtool.c99
-rw-r--r--src/gdbmtool.h57
-rw-r--r--src/gram.y28
-rw-r--r--src/input-argv.c106
-rw-r--r--src/input-file.c88
-rw-r--r--src/input-rl.c153
-rw-r--r--src/input-std.c45
-rw-r--r--src/lex.l315
-rw-r--r--tests/.gitignore3
-rw-r--r--tests/Makefile.am37
-rw-r--r--tests/config/default.exp221
-rw-r--r--tests/gdbmtool/base.exp9
-rw-r--r--tests/gdbmtool00.at37
-rw-r--r--tests/gdbmtool01.at37
-rw-r--r--tests/gdbmtool02.at31
-rw-r--r--tests/testsuite.at5
18 files changed, 995 insertions, 281 deletions
diff --git a/doc/gdbm.texi b/doc/gdbm.texi
index 2aac1c3..0876f8d 100644
--- a/doc/gdbm.texi
+++ b/doc/gdbm.texi
@@ -11,6 +11,7 @@
11* GDBM: (gdbm). The GNU database manager. 11* GDBM: (gdbm). The GNU database manager.
12* gdbm_dump: (gdbm) gdbm_dump. Dump the GDBM database into a flat file. 12* gdbm_dump: (gdbm) gdbm_dump. Dump the GDBM database into a flat file.
13* gdbm_load: (gdbm) gdbm_load. Load the database from a flat file. 13* gdbm_load: (gdbm) gdbm_load. Load the database from a flat file.
14* gdbmtool: (gdbm) gdbmtool. Examine and modify a GDBM database.
14@end direntry 15@end direntry
15@end ifinfo 16@end ifinfo
16 17
diff --git a/src/Makefile.am b/src/Makefile.am
index aff6982..766dba8 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -96,6 +96,8 @@ gdbmtool_LDADD = \
96gdbmtool_SOURCES = \ 96gdbmtool_SOURCES = \
97 datconv.c\ 97 datconv.c\
98 gram.y\ 98 gram.y\
99 input-argv.c\
100 input-file.c\
99 lex.l\ 101 lex.l\
100 gdbmtool.h\ 102 gdbmtool.h\
101 gdbmtool.c\ 103 gdbmtool.c\
@@ -109,7 +111,7 @@ else
109endif 111endif
110 112
111AM_YFLAGS = -dtv 113AM_YFLAGS = -dtv
112#AM_LFLAGS = -d 114AM_LFLAGS = -d
113 115
114gdbm_load_LDADD = ./libgdbmapp.a ./libgdbm.la 116gdbm_load_LDADD = ./libgdbmapp.a ./libgdbm.la
115gdbm_dump_LDADD = ./libgdbmapp.a ./libgdbm.la 117gdbm_dump_LDADD = ./libgdbmapp.a ./libgdbm.la
diff --git a/src/gdbmtool.c b/src/gdbmtool.c
index b322260..c522ad0 100644
--- a/src/gdbmtool.c
+++ b/src/gdbmtool.c
@@ -78,7 +78,7 @@ opendb (char *dbname)
78 78
79 if (open_mode == GDBM_NEWDB) 79 if (open_mode == GDBM_NEWDB)
80 { 80 {
81 if (interactive && variable_is_true ("confirm") && 81 if (interactive () && variable_is_true ("confirm") &&
82 access (dbname, F_OK) == 0) 82 access (dbname, F_OK) == 0)
83 { 83 {
84 if (!getyn (_("database %s already exists; overwrite"), dbname)) 84 if (!getyn (_("database %s already exists; overwrite"), dbname))
@@ -1106,9 +1106,10 @@ void
1106source_handler (struct handler_param *param) 1106source_handler (struct handler_param *param)
1107{ 1107{
1108 char *fname = tildexpand (PARAM_STRING (param, 0)); 1108 char *fname = tildexpand (PARAM_STRING (param, 0));
1109 if (setsource (fname, 0) == 0) 1109 instream_t istr = instream_file_create (fname);
1110 yyparse ();
1111 free (fname); 1110 free (fname);
1111 if (input_context_push (istr) == 0)
1112 yyparse ();
1112} 1113}
1113 1114
1114 1115
@@ -1456,7 +1457,7 @@ command_lookup (const char *str, struct locus *loc, struct command **pcmd)
1456 break; 1457 break;
1457 1458
1458 case fcom_found: 1459 case fcom_found:
1459 if (!interactive) 1460 if (!interactive ())
1460 { 1461 {
1461 state = fcom_abort; 1462 state = fcom_abort;
1462 found = NULL; 1463 found = NULL;
@@ -1479,9 +1480,8 @@ command_lookup (const char *str, struct locus *loc, struct command **pcmd)
1479 } 1480 }
1480 1481
1481 if (state == fcom_init) 1482 if (state == fcom_init)
1482 lerror (loc, 1483 lerror (loc, interactive () ? _("Invalid command. Try ? for help.") :
1483 interactive ? _("Invalid command. Try ? for help.") : 1484 _("Unknown command"));
1484 _("Unknown command"));
1485 if (!found) 1485 if (!found)
1486 return T_BOGUS; 1486 return T_BOGUS;
1487 1487
@@ -1490,7 +1490,12 @@ command_lookup (const char *str, struct locus *loc, struct command **pcmd)
1490} 1490}
1491 1491
1492char *parseopt_program_doc = N_("examine and/or modify a GDBM database"); 1492char *parseopt_program_doc = N_("examine and/or modify a GDBM database");
1493char *parseopt_program_args = N_("DBFILE"); 1493char *parseopt_program_args = N_("DBFILE [COMMAND [ARG ...]]");
1494
1495enum {
1496 OPT_LEX_TRACE = 256,
1497 OPT_GRAM_TRACE
1498};
1494 1499
1495struct gdbm_option optab[] = { 1500struct gdbm_option optab[] = {
1496 { 'b', "block-size", N_("SIZE"), N_("set block size") }, 1501 { 'b', "block-size", N_("SIZE"), N_("set block size") },
@@ -1504,6 +1509,8 @@ struct gdbm_option optab[] = {
1504 { 'r', "read-only", NULL, N_("open database in read-only mode") }, 1509 { 'r', "read-only", NULL, N_("open database in read-only mode") },
1505 { 's', "synchronize", NULL, N_("synchronize to disk after each write") }, 1510 { 's', "synchronize", NULL, N_("synchronize to disk after each write") },
1506 { 'q', "quiet", NULL, N_("don't print initial banner") }, 1511 { 'q', "quiet", NULL, N_("don't print initial banner") },
1512 { OPT_LEX_TRACE, "lex-trace", NULL, N_("enable lexical analyzer traces") },
1513 { OPT_GRAM_TRACE, "gram-trace", NULL, N_("enable grammar traces") },
1507 { 0 } 1514 { 0 }
1508}; 1515};
1509 1516
@@ -1786,7 +1793,7 @@ static struct gdbmarglist last_args;
1786void 1793void
1787run_last_command (void) 1794run_last_command (void)
1788{ 1795{
1789 if (interactive) 1796 if (interactive ())
1790 { 1797 {
1791 if (last_cmd) 1798 if (last_cmd)
1792 { 1799 {
@@ -1846,7 +1853,7 @@ run_command (struct command *cmd, struct gdbmarglist *arglist)
1846 /* Optional argument */ 1853 /* Optional argument */
1847 break; 1854 break;
1848 1855
1849 if (!interactive) 1856 if (!interactive ())
1850 { 1857 {
1851 terror (_("%s: not enough arguments"), cmd->name); 1858 terror (_("%s: not enough arguments"), cmd->name);
1852 return 1; 1859 return 1;
@@ -1896,7 +1903,7 @@ run_command (struct command *cmd, struct gdbmarglist *arglist)
1896 pagfp = NULL; 1903 pagfp = NULL;
1897 1904
1898 expected_lines = 0; 1905 expected_lines = 0;
1899 expected_lines_ptr = (interactive && pager) ? &expected_lines : NULL; 1906 expected_lines_ptr = (interactive () && pager) ? &expected_lines : NULL;
1900 if (!(cmd->begin && cmd->begin (&param, expected_lines_ptr))) 1907 if (!(cmd->begin && cmd->begin (&param, expected_lines_ptr)))
1901 { 1908 {
1902 if (pager && expected_lines > get_screen_lines ()) 1909 if (pager && expected_lines > get_screen_lines ())
@@ -1938,12 +1945,13 @@ run_command (struct command *cmd, struct gdbmarglist *arglist)
1938} 1945}
1939 1946
1940static void 1947static void
1941source_rcfile () 1948source_rcfile (void)
1942{ 1949{
1950 instream_t istr = NULL;
1951
1943 if (access (GDBMTOOLRC, R_OK) == 0) 1952 if (access (GDBMTOOLRC, R_OK) == 0)
1944 { 1953 {
1945 if (setsource (GDBMTOOLRC, 0) == 0) 1954 istr = instream_file_create (GDBMTOOLRC);
1946 yyparse ();
1947 } 1955 }
1948 else 1956 else
1949 { 1957 {
@@ -1962,11 +1970,17 @@ source_rcfile ()
1962 fname = mkfilename (p, GDBMTOOLRC, NULL); 1970 fname = mkfilename (p, GDBMTOOLRC, NULL);
1963 if (access (fname, R_OK) == 0) 1971 if (access (fname, R_OK) == 0)
1964 { 1972 {
1965 if (setsource (fname, 0) == 0) 1973 istr = instream_file_create (GDBMTOOLRC);
1966 yyparse ();
1967 } 1974 }
1968 free (fname); 1975 free (fname);
1969 } 1976 }
1977
1978 if (istr)
1979 {
1980 if (input_context_push (istr))
1981 exit (EXIT_FATAL);
1982 yyparse ();
1983 }
1970} 1984}
1971 1985
1972#if GDBM_DEBUG_ENABLE 1986#if GDBM_DEBUG_ENABLE
@@ -1984,12 +1998,12 @@ debug_printer (char const *fmt, ...)
1984int 1998int
1985main (int argc, char *argv[]) 1999main (int argc, char *argv[])
1986{ 2000{
1987 int intr;
1988 int opt; 2001 int opt;
1989 int bv; 2002 int bv;
1990 int norc = 0; 2003 int norc = 0;
1991 int res; 2004 int res;
1992 char *source = "-"; 2005 char *source = NULL;
2006 instream_t input = NULL;
1993 2007
1994 set_progname (argv[0]); 2008 set_progname (argv[0]);
1995#if GDBM_DEBUG_ENABLE