diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-05-14 16:08:34 +0000 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-05-14 16:08:34 +0000 |
commit | 8cbaa44e2f1b80dd8954a0e06e0bc8a52494237f (patch) | |
tree | acb81d52eed9a1e4f72a02b251b4bfb76691ef25 /src/util.c | |
parent | 045ec749566683e9a4471a81901b7a3f37066376 (diff) | |
download | gdbm-8cbaa44e2f1b80dd8954a0e06e0bc8a52494237f.tar.gz gdbm-8cbaa44e2f1b80dd8954a0e06e0bc8a52494237f.tar.bz2 |
Change variable support.
* src/util.c: New file.
* src/Makefile.am: Add new files.
* src/gdbmtool.c (open_mode): New variable.
(terror): Remove.
(mkfilename, tildexpand): Move to util.c
(opendb): Use open_mode.
(import_handler): Likewise.
(optab): New option -N (--norc).
* src/gdbmtool.h (vparse_error): Rename to vlerror.
(parse_error): Rename to lerror.
(syntax_error): Rename tp terror.
All uses updated.
(VAR_ERR_FAILURE): Remove.
(VAR_ERR_BADVALUE): New error code.
(variable_mode_name): Remove.
(mkfilename, tildexpand)
(vgetyn, getyn): New protos.
* src/lex.l (initialized): New static.
(setsource): Set initialized.
(vlerror): Print locus only if the lexer has been
initialized.
* src/var.c: Rewrite.
Diffstat (limited to 'src/util.c')
-rw-r--r-- | src/util.c | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..d46325b --- /dev/null +++ b/src/util.c @@ -0,0 +1,127 @@ +/* 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 + 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 <pwd.h> + +char * +mkfilename (const char *dir, const char *file, const char *suf) +{ + char *tmp; + size_t dirlen = strlen (dir); + size_t suflen = suf ? strlen (suf) : 0; + size_t fillen = strlen (file); + size_t len; + + while (dirlen > 0 && dir[dirlen-1] == '/') + dirlen--; + + len = dirlen + (dir[0] ? 1 : 0) + fillen + suflen; + tmp = emalloc (len + 1); + memcpy (tmp, dir, dirlen); + if (dir[0]) + tmp[dirlen++] = '/'; + memcpy (tmp + dirlen, file, fillen); + if (suf) + memcpy (tmp + dirlen + fillen, suf, suflen); + tmp[len] = 0; + return tmp; +} + +char * +tildexpand (char *s) +{ + if (s[0] == '~') + { + char *p = s + 1; + size_t len = strcspn (p, "/"); + struct passwd *pw; + + if (len == 0) + pw = getpwuid (getuid ()); + else + { + char *user = emalloc (len + 1); + + memcpy (user, p, len); + user[len] = 0; + pw = getpwnam (user); + free (user); + } + if (pw) + return mkfilename (pw->pw_dir, p + len + 1, NULL); + } + return estrdup (s); +} + +int +vgetyn (const char *prompt, va_list ap) +{ + int state = 0; + int c, resp; + + do + { + switch (state) + { + case 1: + if (c == ' ' || c == '\t') + continue; + resp = c; + state = 2; + /* fall through */ + case 2: + if (c == '\n') + { + switch (resp) + { + case 'y': + case 'Y': + return 1; + case 'n': + case 'N': + return 0; + default: + fprintf (stdout, "%s\n", _("Please, reply 'y' or 'n'")); + } + state = 0; + } else + break; + + case 0: + vfprintf (stdout, prompt, ap); + fprintf (stdout, " [y/n]?"); + fflush (stdout); + state = 1; + break; + } + } while ((c = getchar ()) != EOF); + exit (EXIT_USAGE); +} + +int +getyn (const char *prompt, ...) +{ + va_list ap; + int rc; + + va_start (ap, prompt); + rc = vgetyn (prompt, ap); + va_end (ap); + return rc; +} + |