Imported Upstream version 1.11
[platform/upstream/gdbm.git] / src / gdbmtool.h
1 /* This file is part of GDBM, the GNU data base manager.
2    Copyright (C) 1990, 1991, 1993, 2007, 2011, 2013 Free Software Foundation,
3    Inc.
4
5    GDBM is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3, or (at your option)
8    any later version.
9
10    GDBM is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with GDBM. If not, see <http://www.gnu.org/licenses/>.    */
17
18 #include "autoconf.h"
19 #include "gdbmdefs.h"
20 #include "gdbm.h"
21 #include "gdbmapp.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <ctype.h>
26
27 /* Position in input file */
28 struct point
29 {
30   char *file;             /* file name */
31   unsigned line;          /* line number */  
32   unsigned col;           /* column number */ 
33 };
34
35 /* Location in input file */
36 struct locus
37 {
38   struct point beg, end;
39 };
40
41 typedef struct locus gdbm_yyltype_t;
42  
43 #define YYLTYPE gdbm_yyltype_t 
44
45 #define YYLLOC_DEFAULT(Current, Rhs, N)                       \
46   do                                                          \
47     {                                                         \
48       if (N)                                                  \
49         {                                                     \
50           (Current).beg = YYRHSLOC(Rhs, 1).beg;               \
51           (Current).end = YYRHSLOC(Rhs, N).end;               \
52         }                                                     \
53       else                                                    \
54         {                                                     \
55           (Current).beg = YYRHSLOC(Rhs, 0).end;               \
56           (Current).end = (Current).beg;                      \
57         }                                                     \
58     }                                                         \
59   while (0)
60
61 #define YY_LOCATION_PRINT(File, Loc)                      \
62   do                                                      \
63     {                                                     \
64       if ((Loc).beg.col == 0)                             \
65         fprintf (File, "%s:%u",                           \
66                  (Loc).beg.file,                          \
67                  (Loc).beg.line);                         \
68       else if (strcmp ((Loc).beg.file, (Loc).end.file))   \
69         fprintf (File, "%s:%u.%u-%s:%u.%u",               \
70                  (Loc).beg.file,                          \
71                  (Loc).beg.line, (Loc).beg.col,           \
72                  (Loc).end.file,                          \
73                  (Loc).end.line, (Loc).end.col);          \
74       else if ((Loc).beg.line != (Loc).end.line)          \
75         fprintf (File, "%s:%u.%u-%u.%u",                  \
76                  (Loc).beg.file,                          \
77                  (Loc).beg.line, (Loc).beg.col,           \
78                  (Loc).end.line, (Loc).end.col);          \
79       else if ((Loc).beg.col != (Loc).end.col)            \
80         fprintf (File, "%s:%u.%u-%u",                     \
81                  (Loc).beg.file,                          \
82                  (Loc).beg.line, (Loc).beg.col,           \
83                  (Loc).end.col);                          \
84       else                                                \
85         fprintf (File, "%s:%u.%u",                        \
86                  (Loc).beg.file,                          \
87                  (Loc).beg.line,                          \
88                  (Loc).beg.col);                          \
89     }                                                     \
90   while (0)
91
92 void vlerror (struct locus *loc, const char *fmt, va_list ap);
93 void lerror (struct locus *loc, const char *fmt, ...);
94
95 void terror (const char *fmt, ...);
96
97 void print_prompt (void);
98
99 int setsource (const char *filename, int intr);
100
101 extern char *file_name;
102 extern int interactive;
103 extern int open_mode;
104
105 #define GDBMTOOLRC ".gdbmtoolrc"
106 #define GDBMTOOL_DEFFILE "junk.gdbm"
107
108 \f
109 struct slist
110 {
111   struct slist *next;
112   char *str;
113 };
114
115 struct slist *slist_new (char *s);
116 void slist_free (struct slist *);
117 \f
118 #define KV_STRING 0
119 #define KV_LIST   1
120
121 struct kvpair
122 {
123   struct kvpair *next;
124   int type;
125   struct locus loc;
126   char *key;
127   union
128   {
129     char *s;
130     struct slist *l;
131   } val;
132 };
133
134 struct kvpair *kvpair_string (struct locus *loc, char *val);
135 struct kvpair *kvpair_list (struct locus *loc, struct slist *s);
136
137 \f
138 #define ARG_STRING 0
139 #define ARG_DATUM  1
140 #define ARG_KVPAIR 2
141 #define ARG_MAX    3
142
143 /* Argument to a command handler */
144 struct gdbmarg
145 {
146   struct gdbmarg *next;
147   int type;
148   int ref;
149   struct locus loc;
150   union
151   {
152     char *string;
153     datum dat;
154     struct kvpair *kvpair;
155   } v;
156 };
157
158 /* List of arguments */
159 struct gdbmarglist
160 {
161   struct gdbmarg *head, *tail;
162 };
163
164 void gdbmarglist_init (struct gdbmarglist *, struct gdbmarg *);
165 void gdbmarglist_add (struct gdbmarglist *, struct gdbmarg *);
166 void gdbmarglist_free (struct gdbmarglist *lst);
167
168 struct gdbmarg *gdbmarg_string (char *, struct locus *);
169 struct gdbmarg *gdbmarg_datum (datum *, struct locus *);
170 struct gdbmarg *gdbmarg_kvpair (struct kvpair *kvl, struct locus *);
171
172 int gdbmarg_free (struct gdbmarg *arg);
173 void gdbmarg_destroy (struct gdbmarg **parg);
174 \f
175 struct command;
176 int command_lookup (const char *str, struct locus *loc, struct command **pcmd);
177
178 int run_command (struct command *cmd, struct gdbmarglist *arglist);
179 \f
180 struct xdatum;
181 void xd_expand (struct xdatum *xd, size_t size);
182 void xd_store (struct xdatum *xd, void *val, size_t size);
183
184 \f
185 struct datadef
186 {
187   char *name;
188   int size;
189   int (*format) (FILE *, void *ptr, int size);
190   int (*scan) (struct xdatum *xd, char *str);
191 };
192
193 struct datadef *datadef_lookup (const char *name);
194
195 struct field
196 {
197   struct datadef *type;
198   int dim;
199   char *name;
200 };
201
202 #define FDEF_FLD 0
203 #define FDEF_OFF 1
204 #define FDEF_PAD 2
205
206 struct dsegm
207 {
208   struct dsegm *next;
209   int type;
210   union
211   {
212     int n;
213     struct field field;
214   } v;
215 };
216
217 struct dsegm *dsegm_new (int type);
218 struct dsegm *dsegm_new_field (struct datadef *type, char *id, int dim);
219 void dsegm_free_list (struct dsegm *dp);
220
221 #define DS_KEY     0
222 #define DS_CONTENT 1
223 #define DS_MAX     2
224
225 extern struct dsegm *dsdef[];
226 \f
227 #define VART_STRING 0
228 #define VART_BOOL   1
229 #define VART_INT    2
230
231 #define VAR_OK           0       /* operation succeeded */
232 #define VAR_ERR_NOTSET   1       /* Only for variable_get:
233                                     variable is not set */ 
234 #define VAR_ERR_NOTDEF   2       /* no such variable */ 
235 #define VAR_ERR_BADTYPE  3       /* variable cannot be coerced to the
236                                     requested type (software error) */
237 #define VAR_ERR_BADVALUE 4       /* Only for variable_set: the value is
238                                     not valid for this variable. */
239
240 int variable_set (const char *name, int type, void *val);
241 int variable_get (const char *name, int type, void **val);
242 int variable_is_set (const char *name);
243 int variable_is_true (const char *name);
244 void variable_print_all (FILE *fp);
245
246 \f
247 int unescape (int c);
248 int escape (int c);
249 void begin_def (void);
250 void end_def (void);
251
252 int yylex (void);
253 int yyerror (char *s);
254 int yyparse (void);
255
256 void datum_format (FILE *fp, datum const *dat, struct dsegm *ds);
257 int datum_scan (datum *dat, struct dsegm *ds, struct kvpair *kv);
258 void dsprint (FILE *fp, int what, struct dsegm *ds);
259
260 char *mkfilename (const char *dir, const char *file, const char *suf);
261 char *tildexpand (char *s);
262 int vgetyn (const char *prompt, va_list ap);
263 int getyn (const char *prompt, ...);