diff options
Diffstat (limited to 'src/testgdbm.c')
-rw-r--r-- | src/testgdbm.c | 107 |
1 files changed, 89 insertions, 18 deletions
diff --git a/src/testgdbm.c b/src/testgdbm.c index 2ac3826..155c69e 100644 --- a/src/testgdbm.c +++ b/src/testgdbm.c @@ -1,9 +1,6 @@ -/* testgdbm.c - Driver program to test the database routines and to - help debug gdbm. Uses inside information to show "system" information */ - /* This file is part of GDBM, the GNU data base manager. Copyright (C) 1990, 1991, 1993, 2007, 2011, 2013 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 @@ -36,21 +33,23 @@ #ifdef HAVE_LOCALE_H # include <locale.h> #endif const char *progname; /* Program name */ -char *prompt = "testgdbm> "; +#define DEFAULT_PROMPT "gdbmtool> " +char *prompt; char *file_name = NULL; /* Database file name */ GDBM_FILE gdbm_file = NULL; /* Database to operate upon */ int interactive; /* Are we running in interactive mode? */ datum key_data; /* Current key */ datum return_data; /* Current data */ int key_z = 1; /* Keys are nul-terminated strings */ int data_z = 1; /* Data are nul-terminated strings */ +int quiet_option = 0; /* Omit usual welcome banner at startup */ #define SIZE_T_MAX ((size_t)-1) unsigned input_line; @@ -797,13 +796,83 @@ key_z_handler (struct handler_param *param) void data_z_handler (struct handler_param *param) { data_z = !data_z; fprintf (param->fp, _("Zero terminated data: %s\n"), boolstr (data_z)); } + +struct prompt_exp; + +void +pe_file_name (struct prompt_exp *p) +{ + fwrite (file_name, strlen (file_name), 1, stdout); +} + +struct prompt_exp +{ + int ch; + void (*fun) (struct prompt_exp *); + char *cache; +}; + +struct prompt_exp prompt_exp[] = { + { 'f', pe_file_name }, + { 0 } +}; +static void +expand_char (int c) +{ + struct prompt_exp *p; + + if (c && c != '%') + { + for (p = prompt_exp; p->ch; p++) + { + if (c == p->ch) + { + if (p->cache) + free (p->cache); + return p->fun (p); + } + } + } + putchar ('%'); + putchar (c); +} + +void +outprompt () +{ + char *s; + + for (s = prompt; *s; s++) + { + if (*s == '%') + { + if (!*++s) + { + putchar ('%'); + break; + } + expand_char (*s); + } + else + putchar (*s); + } + + fflush (stdout); +} + +void +prompt_handler (struct handler_param *param) +{ + free (prompt); + prompt = estrdup (param->argv[0]); +} void help_handler (struct handler_param *param); int help_begin (struct handler_param *param, size_t *exp_count); struct command { @@ -888,12 +957,15 @@ struct command command_tab[] = { { S(data-zero), 'Z', NULL, data_z_handler, NULL, { NULL, NULL }, N_("toggle data nul-termination") }, { S(help), '?', help_begin, help_handler, NULL, { NULL, NULL, }, N_("print this help list") }, + { S(prompt), 0, + NULL, prompt_handler, NULL, + { N_("text") }, N_("set command prompt") }, { S(quit), 'q', NULL, quit_handler, NULL, { NULL, NULL, }, N_("quit the program") }, #undef S { 0 } }; @@ -1018,29 +1090,25 @@ getword (char *s, char **endp) SKIPWS (s); } *endp = s; return p; } -/* The test program allows one to call all the routines plus the hash function. - The commands are single letter commands. The user is prompted for missing - pieces of information. See the help command (?) for a list of all - commands. */ - -char *parseopt_program_doc = "Test and modify a GDBM database"; -char *parseopt_program_args = N_("FILE"); +char *parseopt_program_doc = N_("examine and/or modify a GDBM database"); +char *parseopt_program_args = N_("DBFILE"); struct gdbm_option optab[] = { { 'b', "block-size", N_("SIZE"), N_("set block size") }, { 'c', "cache-size", N_("SIZE"), N_("set cache size") }, { 'g', NULL, "FILE", NULL, PARSEOPT_HIDDEN }, { 'l', "no-lock", NULL, N_("disable file locking") }, - { 'm', "no-mmap", NULL, N_("disable file mmap") }, + { 'm', "no-mmap", NULL, N_("do not use mmap") }, { 'n', "newdb", NULL, N_("create database") }, { 'r', "read-only", NULL, N_("open database in read-only mode") }, { 's', "synchronize", NULL, N_("synchronize to disk after each write") }, + { 'q', "quiet", NULL, N_("don't print initial banner") }, { 0 } }; #define ARGINC 16 int @@ -1113,12 +1181,16 @@ main (int argc, char *argv[]) break; case 'g': file_name = optarg; break; + case 'q': + quiet_option = 1; + break; + default: terror (EXIT_USAGE, _("unknown option; try `%s -h' for more info\n"), progname); } argc -= optind; @@ -1156,15 +1228,17 @@ main (int argc, char *argv[]) terror (EXIT_FATAL, _("gdbm_setopt failed: %s"), gdbm_strerror (gdbm_errno)); signal (SIGPIPE, SIG_IGN); /* Welcome message. */ + if (interactive && !quiet_option) + printf (_("\nWelcome to the gdbm tool. Type ? for help.\n\n")); if (interactive) - printf (_("\nWelcome to the gdbm test program. Type ? for help.\n\n")); - + prompt = estrdup (DEFAULT_PROMPT); + memset (¶m, 0, sizeof (param)); argmax = 0; while (1) { int i; @@ -1178,16 +1252,13 @@ main (int argc, char *argv[]) free (param.argv[i]); param.argc = 0; input_line++; if (interactive) - { - printf ("%s", prompt); - fflush (stdout); - } + outprompt (); if (fgets (cmdbuf, sizeof cmdbuf, stdin) == NULL) { putchar ('\n'); break; } |