1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/* This file is part of GDBM, the GNU data base manager.
Copyright (C) 1990-2024 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 <errno.h>
#include <ctype.h>
#include <signal.h>
#include <pwd.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <termios.h>
#include <stdarg.h>
#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif
static void
source_rcfile (void)
{
instream_t istr = NULL;
if (access (GDBMTOOLRC, R_OK) == 0)
{
istr = instream_file_create (GDBMTOOLRC);
}
else
{
char *fname;
char *p = getenv ("HOME");
if (!p)
{
struct passwd *pw = getpwuid (getuid ());
if (!pw)
{
terror (_("cannot find home directory"));
return;
}
p = pw->pw_dir;
}
fname = mkfilename (p, GDBMTOOLRC, NULL);
if (access (fname, R_OK) == 0)
{
istr = instream_file_create (GDBMTOOLRC);
}
free (fname);
}
if (istr)
{
if (input_context_push (istr))
exit (EXIT_FATAL);
yyparse ();
}
}
#if GDBM_DEBUG_ENABLE
void
debug_printer (char const *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
}
#endif
char *parseopt_program_doc = N_("examine and/or modify a GDBM database");
char *parseopt_program_args = N_("DBFILE [COMMAND [ARG ...]]");
enum {
OPT_LEX_TRACE = 256,
OPT_GRAM_TRACE
};
struct gdbm_option optab[] = {
{ 'b', "block-size", N_("SIZE"), N_("set block size") },
{ 'c', "cache-size", N_("SIZE"), N_("set cache size") },
{ 'f', "file", N_("FILE"), N_("read commands from FILE") },
{ 'g', NULL, "FILE", NULL, PARSEOPT_HIDDEN },
{ 'l', "no-lock", NULL, N_("disable file locking") },
{ 'm', "no-mmap", NULL, N_("do not use mmap") },
{ 'n', "newdb", NULL, N_("create database") },
{ 'N', "norc", NULL, N_("do not read .gdbmtoolrc file") },
{ '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") },
{ 'd', "db-descriptor",
/* TRANSLATORS: File Descriptor. */
N_("FD"),
N_("open database at the given file descriptor") },
{ 'x', "extended", NULL, N_("extended format (numsync)") },
{ 0, "numsync", NULL, NULL, PARSEOPT_ALIAS },
{ 't', "trace", NULL, N_("enable trace mode") },
{ 'T', "timing", NULL, N_("print timing after each command") },
#if GDBMTOOL_DEBUG
{ OPT_LEX_TRACE, "lex-trace", NULL, N_("enable lexical analyzer traces") },
{ OPT_GRAM_TRACE, "gram-trace", NULL, N_("enable grammar traces") },
#endif
{ 0 }
};
#ifdef WITH_READLINE
# define instream_default_create instream_readline_create
#else
# define instream_default_create instream_stdin_create
#endif
struct gdbmtool_closure
{
int argc;
char **argv;
};
static int
gdbmtool_init (void *data, instream_t *pinstr)
{
struct gdbmtool_closure *clos = data;
int argc = clos->argc;
char **argv = clos->argv;
int opt;
int bv;
int norc = 0;
char *source = NULL;
instream_t input = NULL;
for (opt = parseopt_first (argc, argv, optab);
opt != EOF;
opt = parseopt_next ())
switch (opt)
{
case 'd':
if (variable_set ("fd", VART_STRING, optarg) != VAR_OK)
{
terror (_("invalid file descriptor: %s"), optarg);
exit (EXIT_USAGE);
}
break;
case 'f':
source = optarg;
break;
case 'l':
bv = 0;
variable_set ("lock", VART_BOOL, &bv);
break;
case 'm':
bv = 0;
variable_set ("mmap", VART_BOOL, &bv);
break;
case 's':
bv = 1;
variable_set ("sync", VART_BOOL, &bv);
break;
case 'r':
variable_set ("open", VART_STRING, "readonly");
break;
case 'n':
variable_set ("open", VART_STRING, "newdb");
break;
case 'N':
norc = 1;
break;
case 'c':
variable_set ("cachesize", VART_STRING, optarg);
break;
case 'b':
variable_set ("blocksize", VART_STRING, optarg);
break;
case 'g':
variable_set ("filename", VART_STRING, optarg);
break;
case 't':
bv = 1;
variable_set ("trace", VART_BOOL, &bv);
break;
case 'T':
bv = 1;
variable_set ("timing", VART_BOOL, &bv);
break;
case 'q':
bv = 1;
variable_set ("quiet", VART_BOOL, &bv);
break;
case 'x':
variable_set ("format", VART_STRING, "numsync");
break;
case OPT_LEX_TRACE:
lex_trace (1);
break;
case OPT_GRAM_TRACE:
gram_trace (1);
break;
default:
if (optopt == 0)
terror (_("unknown option %s; try `%s -h' for more info"),
argv[optind-1], progname);
else
terror (_("unknown option %c; try `%s -h' for more info"),
optopt, progname);
exit (EXIT_USAGE);
}
argc -= optind;
argv += optind;
if (source && strcmp (source, "-"))
{
input = instream_file_create (source);
if (!input)
exit (1);
}
if (argc >= 1)
{
variable_set ("filename", VART_STRING, argv[0]);
argc--;
argv++;
if (argc)
{
if (input)
{
terror (_("--file and command cannot be used together"));
exit (EXIT_USAGE);
}
input = instream_argv_create (argc, argv);
if (!input)
exit (1);
}
}
if (!norc)
source_rcfile ();
if (!input)
input = instream_default_create ();
*pinstr = input;
return 0;
}
int
main (int argc, char *argv[])
{
struct gdbmtool_closure closure;
set_progname (argv[0]);
#if GDBM_DEBUG_ENABLE
gdbm_debug_printer = debug_printer;
#endif
#ifdef HAVE_SETLOCALE
setlocale (LC_ALL, "");
#endif
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
closure.argc = argc;
closure.argv = argv;
return gdbmshell_run (gdbmtool_init, &closure);
}
|