diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-05-13 19:34:35 +0000 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-05-13 19:34:35 +0000 |
commit | 1cc001721ba1923f7e145c74b6b2fa32b36e65da (patch) | |
tree | 276d55d1d4e4a25825d7cf3b3352cdbcaad8698e | |
parent | aa0eaa5bc642167db8cfb5778cf9a0091401a4f3 (diff) | |
download | gdbm-1cc001721ba1923f7e145c74b6b2fa32b36e65da.tar.gz gdbm-1cc001721ba1923f7e145c74b6b2fa32b36e65da.tar.bz2 |
Add "set" and "define" to the main command table.
* src/gdbmtool.c: Include gram.h
(command) <tok>: New member.
(command_tab): List "define" and "set".
(find_command): Rewrite as command_lookup, with different
return type and signature.
(run_command): Take struct command * as its first argument.
* src/gdbmtool.h (command_lookup): New proto.
(run_command): Change signature.
(datadef_locate): Rename to datadef_lookup. All uses changed.
* src/gram.y: Update.
* src/lex.l: Change handling of IDENT rules.
-rw-r--r-- | ChangeLog | 18 | ||||
-rw-r--r-- | src/datconv.c | 2 | ||||
-rw-r--r-- | src/gdbmtool.c | 80 | ||||
-rw-r--r-- | src/gdbmtool.h | 7 | ||||
-rw-r--r-- | src/gram.y | 29 | ||||
-rw-r--r-- | src/lex.l | 17 |
6 files changed, 97 insertions, 56 deletions
@@ -1,7 +1,23 @@ 2013-05-13 Sergey Poznyakoff <gray@gnu.org.ua> + Add "set" and "define" to the main command table. + + * src/gdbmtool.c: Include gram.h + (command) <tok>: New member. + (command_tab): List "define" and "set". + (find_command): Rewrite as command_lookup, with different + return type and signature. + (run_command): Take struct command * as its first argument. + * src/gdbmtool.h (command_lookup): New proto. + (run_command): Change signature. + (datadef_locate): Rename to datadef_lookup. All uses changed. + * src/gram.y: Update. + * src/lex.l: Change handling of IDENT rules. + +2013-05-13 Sergey Poznyakoff <gray@gnu.org.ua> + Implement shell variables in gdbmtool. - + * src/var.c: New file. * src/Makefile.am: Add var.c * src/datconv.c (datum_format): Remove the "delim" diff --git a/src/datconv.c b/src/datconv.c index 10a1406..19049aa 100644 --- a/src/datconv.c +++ b/src/datconv.c @@ -166,7 +166,7 @@ static struct datadef datatab[] = { }; struct datadef * -datadef_locate (const char *name) +datadef_lookup (const char *name) { struct datadef *p; diff --git a/src/gdbmtool.c b/src/gdbmtool.c index 48dd986..039f149 100644 --- a/src/gdbmtool.c +++ b/src/gdbmtool.c @@ -17,6 +17,7 @@ #include "gdbmtool.h" #include "gdbm.h" +#include "gram.h" #include <errno.h> #include <ctype.h> @@ -775,6 +776,7 @@ struct command { char *name; /* Command name */ size_t len; /* Name length */ + int tok; int (*begin) (struct handler_param *param, size_t *); void (*handler) (struct handler_param *param); void (*end) (void *data); @@ -785,94 +787,102 @@ struct command struct command command_tab[] = { #define S(s) #s, sizeof (#s) - 1 - { S(count), + { S(count), T_CMD, NULL, count_handler, NULL, { { NULL } }, N_("count (number of entries)") }, - { S(delete), + { S(delete), T_CMD, NULL, delete_handler, NULL, { { N_("key"), ARG_DATUM, DS_KEY }, { NULL } }, N_("delete") }, - { S(export), + { S(export), T_CMD, NULL, export_handler, NULL, { { N_("file"), ARG_STRING }, { "[truncate]", ARG_STRING }, { "[binary|ascii]", ARG_STRING }, { NULL } }, N_("export") }, - { S(fetch), + { S(fetch), T_CMD, NULL, fetch_handler, NULL, { { N_("key"), ARG_DATUM, DS_KEY }, { NULL } }, N_("fetch") }, - { S(import), + { S(import), T_CMD, NULL, import_handler, NULL, { { N_("file"), ARG_STRING }, { "[replace]", ARG_STRING }, { "[nometa]" , ARG_STRING }, { NULL } }, N_("import") }, - { S(list), + { S(list), T_CMD, list_begin, list_handler, NULL, { { NULL } }, N_("list") }, - { S(next), + { S(next), T_CMD, NULL, nextkey_handler, NULL, { { N_("[key]"), ARG_STRING }, { NULL } }, N_("nextkey") }, - { S(store), + { S(store), T_CMD, NULL, store_handler, NULL, { { N_("key"), ARG_DATUM, DS_KEY }, { N_("data"), ARG_DATUM, DS_CONTENT }, { NULL } }, N_("store") }, - { S(first), + { S(first), T_CMD, NULL, firstkey_handler, NULL, { { NULL } }, N_("firstkey") }, #if 0 FIXME - { S(read), + { S(read), T_CMD, NULL, read_handler, NULL, { { N_("file"), ARG_STRING }, { "[replace]", ARG_STRING }, { NULL } }, N_("read entries from file and store") }, #endif - { S(reorganize), + { S(reorganize), T_CMD, NULL, reorganize_handler, NULL, { { NULL } }, N_("reorganize") }, - { S(avail), + { S(avail), T_CMD, avail_begin, avail_handler, NULL, { { NULL } }, N_("print avail list") }, - { S(bucket), + { S(bucket), T_CMD, print_bucket_begin, print_current_bucket_handler, NULL, { { N_("bucket-number"), ARG_STRING }, { NULL } }, N_("print a bucket") }, - { S(current), + { S(current), T_CMD, print_current_bucket_begin, print_current_bucket_handler, NULL, { { NULL } }, N_("print current bucket") }, - { S(dir), + { S(dir), T_CMD, print_dir_begin, print_dir_handler, NULL, { { NULL } }, N_("print hash directory") }, - { S(header), + { S(header), T_CMD, print_header_begin , print_header_handler, NULL, { { NULL } }, N_("print file header") }, - { S(hash), + { S(hash), T_CMD, NULL, hash_handler, NULL, { { N_("key"), ARG_DATUM, DS_KEY }, { NULL } }, N_("hash value of key") }, - { S(cache), + { S(cache), T_CMD, print_cache_begin, print_cache_handler, NULL, { { NULL } }, N_("print the bucket cache") }, - { S(status), + { S(status), T_CMD, NULL, status_handler, NULL, { { NULL } }, N_("print current program status") }, - { S(version), + { S(version), T_CMD, NULL, print_version_handler, NULL, { { NULL } }, N_("print version of gdbm") }, - { S(help), + { S(help), T_CMD, help_begin, help_handler, NULL, { { NULL } }, N_("print this help list") }, - { S(quit), + { S(quit), T_CMD, NULL, quit_handler, NULL, { { NULL } }, N_("quit the program") }, + { S(set), T_SET, + NULL, NULL, NULL, + { { "[var=value...]" }, NULL }, N_("set or list variables") }, + { S(define), T_DEF, + NULL, NULL, NULL, + { { "key|content", ARG_STRING }, + { "{ field-list }", ARG_STRING }, + { NULL } }, N_("define structure") }, #undef S { 0 } }; @@ -927,8 +937,8 @@ help_handler (struct handler_param *param) } } -struct command * -find_command (const char *str) +int +command_lookup (const char *str, struct locus *loc, struct command **pcmd) { enum { fcom_init, fcom_found, fcom_ambig, fcom_abort } state = fcom_init; struct command *cmd, *found = NULL; @@ -964,9 +974,14 @@ find_command (const char *str) } if (state == fcom_init) - syntax_error (interactive ? _("Invalid command. Try ? for help.") : - _("Unknown command")); - return found; + parse_error (loc, + interactive ? _("Invalid command. Try ? for help.") : + _("Unknown command")); + if (!found) + return T_BOGUS; + + *pcmd = found; + return found->tok; } char *parseopt_program_doc = N_("examine and/or modify a GDBM database"); @@ -1223,20 +1238,15 @@ coerce (struct gdbmarg *arg, struct argdef *def) } int -run_command (const char *verb, struct gdbmarglist *arglist) +run_command (struct command *cmd, struct gdbmarglist *arglist) { int i; - struct command *cmd; struct gdbmarg *arg; char *pager = getenv ("PAGER"); char argbuf[128]; size_t expected_lines, *expected_lines_ptr; FILE *pagfp = NULL; - cmd = find_command (verb); - if (!cmd) - return 1; - arg = arglist ? arglist->head : NULL; for (i = 0; cmd->args[i].name && arg; i++, arg = arg->next) @@ -1433,8 +1443,8 @@ main (int argc, char *argv[]) /* Initialize variables. */ interactive = isatty (0); - dsdef[DS_KEY] = dsegm_new_field (datadef_locate ("string"), NULL, 1); - dsdef[DS_CONTENT] = dsegm_new_field (datadef_locate ("string"), NULL, 1); + dsdef[DS_KEY] = dsegm_new_field (datadef_lookup ("string"), NULL, 1); + dsdef[DS_CONTENT] = dsegm_new_field (datadef_lookup ("string"), NULL, 1); if (reader) { diff --git a/src/gdbmtool.h b/src/gdbmtool.h index 98c6758..a4df02a 100644 --- a/src/gdbmtool.h +++ b/src/gdbmtool.h @@ -164,8 +164,11 @@ struct gdbmarg *gdbmarg_kvpair (struct kvpair *kvl); int gdbmarg_free (struct gdbmarg *arg); void gdbmarg_destroy (struct gdbmarg **parg); + +struct command; +int command_lookup (const char *str, struct locus *loc, struct command **pcmd); -int run_command (const char *verb, struct gdbmarglist *arglist); +int run_command (struct command *cmd, struct gdbmarglist *arglist); struct xdatum; void xd_expand (struct xdatum *xd, size_t size); @@ -179,7 +182,7 @@ struct datadef int (*scan) (struct xdatum *xd, char *str); }; -struct datadef *datadef_locate (const char *name); +struct datadef *datadef_lookup (const char *name); struct field { @@ -27,10 +27,11 @@ struct dsegm *dsdef[DS_MAX]; %locations %token <type> T_TYPE -%token T_OFF T_PAD T_DEF T_SET +%token T_OFF T_PAD T_DEF T_SET T_BOGUS +%token <cmd> T_CMD %token <num> T_NUM %token <string> T_IDENT T_WORD -%type <string> string verb +%type <string> string %type <arg> arg %type <arglist> arglist arg1list %type <dsegm> def @@ -51,6 +52,7 @@ struct dsegm *dsdef[DS_MAX]; struct datadef *type; struct dsegm *dsegm; struct { struct dsegm *head, *tail; } dsegmlist; + struct command *cmd; } %% @@ -64,7 +66,7 @@ stmtlist : stmt ; stmt : /* empty */ '\n' - | verb arglist '\n' + | T_CMD arglist '\n' { if (run_command ($1, &$2) && !interactive) exit (EXIT_USAGE); @@ -72,6 +74,16 @@ stmt : /* empty */ '\n' } | set '\n' | defn '\n' + | T_BOGUS '\n' + { + if (interactive) + { + yyclearin; + yyerrok; + } + else + YYERROR; + } | error { end_def(); } '\n' { if (interactive) @@ -84,9 +96,6 @@ stmt : /* empty */ '\n' } ; -verb : T_IDENT - ; - arglist : /* empty */ { gdbmarglist_init (&$$, NULL); @@ -166,14 +175,6 @@ slist : string string : T_IDENT | T_WORD - | T_DEF - { - $$ = estrdup ("def"); - } - | T_SET - { - $$ = estrdup ("set"); - } ; defn : T_DEF defid { begin_def (); } '{' deflist optcomma '}' @@ -111,8 +111,6 @@ O [0-7] #.*\n advance_line (); #.* /* end-of-file comment */; -def { return T_DEF; } -set { return T_SET; } <DEF>off { return T_OFF; } <DEF>pad { return T_PAD; } <DEF>0[xX]{X}{X}* { yylval.num = strtoul (yytext, NULL, 16); @@ -122,8 +120,21 @@ set { return T_SET; } <DEF>0|{P} { yylval.num = strtoul (yytext, NULL, 10); return T_NUM; }; +^[ \t]*{IDENT} { if (YYSTATE == DEF) + { + if (yylval.type = datadef_lookup (yytext)) + return T_TYPE; + } + else + { + return command_lookup (yytext, &yylloc, &yylval.cmd); + } + + yylval.string = estrdup (yytext); + return T_IDENT; + } {IDENT} { if (YYSTATE == DEF && - (yylval.type = datadef_locate (yytext))) + (yylval.type = datadef_lookup (yytext))) return T_TYPE; else { |