Coverage Report

Created: 2025-07-18 06:18

/src/gdbm/tools/gdbmtool.h
Line
Count
Source (jump to first uncovered line)
1
/* This file is part of GDBM, the GNU data base manager.
2
   Copyright (C) 1990-2025 Free Software Foundation, Inc.
3
4
   GDBM is free software; you can redistribute it and/or modify
5
   it under the terms of the GNU General Public License as published by
6
   the Free Software Foundation; either version 3, or (at your option)
7
   any later version.
8
9
   GDBM is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
   GNU General Public License for more details.
13
14
   You should have received a copy of the GNU General Public License
15
   along with GDBM. If not, see <http://www.gnu.org/licenses/>.    */
16
17
#include "autoconf.h"
18
#include "gdbmdefs.h"
19
#include "gdbm.h"
20
#include "gdbmapp.h"
21
#include <stdlib.h>
22
#include <stdio.h>
23
#include <stdarg.h>
24
#include <string.h>
25
#include <ctype.h>
26
#include <limits.h>
27
28
#ifndef GDBM_ARG_UNUSED
29
# define GDBM_ARG_UNUSED __attribute__ ((__unused__))
30
#endif
31
32
#ifndef GDBM_PRINTFLIKE
33
# define GDBM_PRINTFLIKE(fmt,narg) \
34
  __attribute__ ((__format__ (__printf__, fmt, narg)))
35
#endif
36
37
/* Position in input file */
38
struct point
39
{
40
  char *file;             /* file name */
41
  unsigned line;          /* line number */
42
  unsigned col;           /* column number */
43
};
44
45
/* Location in input file */
46
struct locus
47
{
48
  struct point beg, end;
49
};
50
51
typedef struct locus gdbm_yyltype_t;
52
53
21.1k
#define YYLTYPE gdbm_yyltype_t
54
55
#define YYLLOC_DEFAULT(Current, Rhs, N)           \
56
536k
  do                    \
57
536k
    {                   \
58
536k
      if (N)                 \
59
536k
  {                 \
60
423k
    (Current).beg = YYRHSLOC(Rhs, 1).beg;         \
61
423k
    (Current).end = YYRHSLOC(Rhs, N).end;         \
62
423k
  }                  \
63
536k
      else                  \
64
536k
  {                 \
65
113k
    (Current).beg = YYRHSLOC(Rhs, 0).end;         \
66
113k
    (Current).end = (Current).beg;          \
67
113k
  }                  \
68
536k
    }                   \
69
536k
  while (0)
70
71
1.23k
#define YY_LOCATION_PRINT(File, Loc) locus_print (File, &(Loc))
72
73
void locus_print (FILE *fp, struct locus const *loc);
74
void vlerror (struct locus const *loc, const char *fmt, va_list ap);
75
void lerror (struct locus const *loc, const char *fmt, ...)
76
     GDBM_PRINTFLIKE (2, 3);
77
78
void terror (const char *fmt, ...)
79
     GDBM_PRINTFLIKE (1, 2);
80
void dberror (char const *fmt, ...)
81
     GDBM_PRINTFLIKE (1, 2);
82
83
char *make_prompt (void);
84
85
#define GDBMTOOLRC ".gdbmtoolrc"
86
#define GDBMTOOL_DEFFILE "junk.gdbm"
87

88
typedef struct instream *instream_t;
89
90
struct instream
91
{
92
  char *in_name;           /* Input stream name */
93
  int in_inter;            /* True if this is an interactive stream */
94
  ssize_t (*in_read) (instream_t, char*, size_t);
95
         /* Read from stream */
96
  void (*in_close) (instream_t);
97
         /* Close the stream */
98
  int (*in_eq) (instream_t, instream_t);
99
         /* Return true if both streams refer to the
100
            same input */
101
  int (*in_history_size) (instream_t);
102
         /* Return size of the history buffer (entries) */
103
  const char *(*in_history_get) (instream_t, int);
104
         /* Get Nth line from the history buffer */
105
};
106
107
static inline char const *
108
instream_name (instream_t in)
109
7.21k
{
110
7.21k
  return in->in_name;
111
7.21k
}
Unexecuted instantiation: gdbm_fuzzer.c:instream_name
Unexecuted instantiation: gram.c:instream_name
lex.c:instream_name
Line
Count
Source
109
7.21k
{
110
7.21k
  return in->in_name;
111
7.21k
}
Unexecuted instantiation: gdbmshell.c:instream_name
Unexecuted instantiation: var.c:instream_name
Unexecuted instantiation: util.c:instream_name
Unexecuted instantiation: pagerfile.c:instream_name
Unexecuted instantiation: datconv.c:instream_name
Unexecuted instantiation: input-file.c:instream_name
Unexecuted instantiation: input-null.c:instream_name
112
113
static inline ssize_t
114
instream_read (instream_t in, char *buf, size_t size)
115
7.21k
{
116
7.21k
  return in->in_read (in, buf, size);
117
7.21k
}
Unexecuted instantiation: gdbm_fuzzer.c:instream_read
Unexecuted instantiation: gram.c:instream_read
lex.c:instream_read
Line
Count
Source
115
7.21k
{
116
7.21k
  return in->in_read (in, buf, size);
117
7.21k
}
Unexecuted instantiation: gdbmshell.c:instream_read
Unexecuted instantiation: var.c:instream_read
Unexecuted instantiation: util.c:instream_read
Unexecuted instantiation: pagerfile.c:instream_read
Unexecuted instantiation: datconv.c:instream_read
Unexecuted instantiation: input-file.c:instream_read
Unexecuted instantiation: input-null.c:instream_read
118
119
static inline void
120
instream_close (instream_t in)
121
7.21k
{
122
7.21k
  in->in_close (in);
123
7.21k
}
Unexecuted instantiation: gdbm_fuzzer.c:instream_close
Unexecuted instantiation: gram.c:instream_close
lex.c:instream_close
Line
Count
Source
121
7.21k
{
122
7.21k
  in->in_close (in);
123
7.21k
}
Unexecuted instantiation: gdbmshell.c:instream_close
Unexecuted instantiation: var.c:instream_close
Unexecuted instantiation: util.c:instream_close
Unexecuted instantiation: pagerfile.c:instream_close
Unexecuted instantiation: datconv.c:instream_close
Unexecuted instantiation: input-file.c:instream_close
Unexecuted instantiation: input-null.c:instream_close
124
125
static inline int
126
instream_interactive (instream_t in)
127
76.6k
{
128
76.6k
  return in->in_inter;
129
76.6k
}
Unexecuted instantiation: gdbm_fuzzer.c:instream_interactive
Unexecuted instantiation: gram.c:instream_interactive
lex.c:instream_interactive
Line
Count
Source
127
72.4k
{
128
72.4k
  return in->in_inter;
129
72.4k
}
gdbmshell.c:instream_interactive
Line
Count
Source
127
4.22k
{
128
4.22k
  return in->in_inter;
129
4.22k
}
Unexecuted instantiation: var.c:instream_interactive
Unexecuted instantiation: util.c:instream_interactive
Unexecuted instantiation: pagerfile.c:instream_interactive
Unexecuted instantiation: datconv.c:instream_interactive
Unexecuted instantiation: input-file.c:instream_interactive
Unexecuted instantiation: input-null.c:instream_interactive
130
131
static inline int
132
instream_eq (instream_t a, instream_t b)
133
0
{
134
0
  return a->in_eq (a, b);
135
0
}
Unexecuted instantiation: gdbm_fuzzer.c:instream_eq
Unexecuted instantiation: gram.c:instream_eq
Unexecuted instantiation: lex.c:instream_eq
Unexecuted instantiation: gdbmshell.c:instream_eq
Unexecuted instantiation: var.c:instream_eq
Unexecuted instantiation: util.c:instream_eq
Unexecuted instantiation: pagerfile.c:instream_eq
Unexecuted instantiation: datconv.c:instream_eq
Unexecuted instantiation: input-file.c:instream_eq
Unexecuted instantiation: input-null.c:instream_eq
136
137
static inline int
138
instream_history_size (instream_t in)
139
0
{
140
0
  return in->in_history_size ? in->in_history_size (in) : -1;
141
0
}
Unexecuted instantiation: gdbm_fuzzer.c:instream_history_size
Unexecuted instantiation: gram.c:instream_history_size
Unexecuted instantiation: lex.c:instream_history_size
Unexecuted instantiation: gdbmshell.c:instream_history_size
Unexecuted instantiation: var.c:instream_history_size
Unexecuted instantiation: util.c:instream_history_size
Unexecuted instantiation: pagerfile.c:instream_history_size
Unexecuted instantiation: datconv.c:instream_history_size
Unexecuted instantiation: input-file.c:instream_history_size
Unexecuted instantiation: input-null.c:instream_history_size
142
143
static inline const char *
144
instream_history_get (instream_t in, int n)
145
0
{
146
0
  return in->in_history_get ? in->in_history_get (in, n) : NULL;
147
0
}
Unexecuted instantiation: gdbm_fuzzer.c:instream_history_get
Unexecuted instantiation: gram.c:instream_history_get
Unexecuted instantiation: lex.c:instream_history_get
Unexecuted instantiation: gdbmshell.c:instream_history_get
Unexecuted instantiation: var.c:instream_history_get
Unexecuted instantiation: util.c:instream_history_get
Unexecuted instantiation: pagerfile.c:instream_history_get
Unexecuted instantiation: datconv.c:instream_history_get
Unexecuted instantiation: input-file.c:instream_history_get
Unexecuted instantiation: input-null.c:instream_history_get
148
149
instream_t instream_stdin_create (void);
150
instream_t instream_argv_create (int argc, char **argv);
151
instream_t instream_file_create (char const *name);
152
instream_t instream_null_create (void);
153
#ifdef WITH_READLINE
154
instream_t instream_readline_create (void);
155
#endif
156
157
int interactive (void);
158
int input_context_push (instream_t);
159
int input_context_pop (void);
160
void input_context_drain (void);
161
int input_history_size (void);
162
const char *input_history_get (int n);
163
const char *input_stream_name (void);
164
165

166
void print_prompt_at_bol (void);
167
char *command_generator (const char *text, int state);
168
169

170
struct slist
171
{
172
  struct slist *next;
173
  char *str;
174
};
175
176
struct slist *slist_new (char const *s);
177
struct slist *slist_new_s (char *s);
178
struct slist *slist_new_l (char const *s, size_t l);
179
void slist_free (struct slist *);
180
void slist_insert (struct slist **where, struct slist *what);
181

182
59.8k
#define KV_STRING 0
183
0
#define KV_LIST   1
184
185
struct kvpair
186
{
187
  struct kvpair *next;
188
  int type;
189
  struct locus loc;
190
  char *key;
191
  union
192
  {
193
    char *s;
194
    struct slist *l;
195
  } val;
196
};
197
198
struct kvpair *kvpair_string (struct locus *loc, char *val);
199
struct kvpair *kvpair_list (struct locus *loc, struct slist *s);
200
struct kvpair *kvlist_find (struct kvpair *kv, char const *tag);
201
void kvlist_free (struct kvpair *kvp);
202
203

204
35.8k
#define GDBM_ARG_STRING 0
205
29.9k
#define GDBM_ARG_DATUM  1
206
0
#define GDBM_ARG_KVPAIR 2
207
#define GDBM_ARG_MAX    3
208
209
/* Argument to a command handler */
210
struct gdbmarg
211
{
212
  struct gdbmarg *next;
213
  int type;
214
  int ref;
215
  struct locus loc;
216
  union
217
  {
218
    char *string;
219
    datum dat;
220
    struct kvpair *kvpair;
221
  } v;
222
};
223
224
/* List of arguments */
225
struct gdbmarglist
226
{
227
  struct gdbmarg *head, *tail;
228
};
229
230
struct command_param
231
{
232
  size_t argc;
233
  size_t argmax;
234
  struct gdbmarg **argv;
235
  struct gdbmarg *vararg;
236
};
237
238
64.0k
#define HANDLER_PARAM_INITIALIZER { 0, 0, NULL, NULL }
239
240
2.99k
#define PARAM_LOCPTR(p,n) (&(p)->argv[n]->loc)
241
2.99k
#define PARAM_STRING(p,n) ((p)->argv[n]->v.string)
242
14.9k
#define PARAM_DATUM(p,n)  ((p)->argv[n]->v.dat)
243
#define PARAM_KVPAIR(p,n) ((p)->argv[n]->v.kvpair)
244
245
void gdbmarglist_init (struct gdbmarglist *, struct gdbmarg *);
246
void gdbmarglist_add (struct gdbmarglist *, struct gdbmarg *);
247
void gdbmarglist_free (struct gdbmarglist *lst);
248
249
struct gdbmarg *gdbmarg_string (char *, struct locus *);
250
struct gdbmarg *gdbmarg_datum (datum *, struct locus *);
251
struct gdbmarg *gdbmarg_kvpair (struct kvpair *kvl, struct locus *);
252
253
int gdbmarg_free (struct gdbmarg *arg);
254
void gdbmarg_destroy (struct gdbmarg **parg);
255

256
enum
257
  {
258
    mode_initial,
259
    mode_transparent,
260
    mode_pager
261
  };
262
263
struct pagerfile
264
{
265
  FILE *stream;
266
  char *pager;
267
  int mode;
268
  ssize_t maxlines;
269
  size_t nlines;
270
  char *bufbase;
271
  size_t bufsize;
272
  size_t bufmax;
273
};
274
275
typedef struct pagerfile PAGERFILE;
276
277
PAGERFILE *pager_create (char const *pager);
278
PAGERFILE *pager_open (FILE *stream, size_t maxlines, char const *pager);
279
void pager_close (struct pagerfile *pfp);
280
ssize_t pager_write (struct pagerfile *pfp, const char *buffer, size_t size);
281
ssize_t pager_writez (struct pagerfile *pfp, const char *str);
282
ssize_t pager_writeln (struct pagerfile *pfp, const char *str);
283
int pager_putc (struct pagerfile *pfp, int c);
284
int pager_vprintf (struct pagerfile *pfp, const char *fmt, va_list ap);
285
int pager_printf (struct pagerfile *pfp, const char *fmt, ...);
286
int pager_flush (struct pagerfile *pfp);
287
static inline int pager_fileno (struct pagerfile *pfp)
288
0
{
289
0
  return fileno (pfp->stream);
290
0
}
Unexecuted instantiation: gdbm_fuzzer.c:pager_fileno
Unexecuted instantiation: gram.c:pager_fileno
Unexecuted instantiation: lex.c:pager_fileno
Unexecuted instantiation: gdbmshell.c:pager_fileno
Unexecuted instantiation: var.c:pager_fileno
Unexecuted instantiation: util.c:pager_fileno
Unexecuted instantiation: pagerfile.c:pager_fileno
Unexecuted instantiation: datconv.c:pager_fileno
Unexecuted instantiation: input-file.c:pager_fileno
Unexecuted instantiation: input-null.c:pager_fileno
291
int pager_error (struct pagerfile *pfp);
292

293
struct command_environ
294
{
295
  PAGERFILE *pager;
296
  void *data;
297
};
298
299
64.0k
#define COMMAND_ENVIRON_INITIALIZER { NULL, NULL }
300

301
struct command;
302
int command_lookup (const char *str, struct locus *loc, struct command **pcmd);
303
304
int run_command (struct command *cmd, struct gdbmarglist *arglist, char *pipe);
305
int run_last_command (void);
306

307
struct xdatum;
308
void xd_expand (struct xdatum *xd, size_t size);
309
void xd_store (struct xdatum *xd, void *val, size_t size);
310
311

312
struct datadef
313
{
314
  char *name;
315
  int size;
316
  int (*format) (PAGERFILE *, void *ptr, int size);
317
  int (*scan) (struct xdatum *xd, char *str);
318
};
319
320
struct datadef *datadef_lookup (const char *name);
321
322
struct field
323
{
324
  struct datadef *type;
325
  int dim;
326
  char *name;
327
};
328
329
38.2k
#define FDEF_FLD 0
330
0
#define FDEF_OFF 1
331
0
#define FDEF_PAD 2
332
333
struct dsegm
334
{
335
  struct dsegm *next;
336
  int type;
337
  union
338
  {
339
    int n;
340
    struct field field;
341
  } v;
342
};
343
344
struct dsegm *dsegm_new (int type);
345
struct dsegm *dsegm_new_field (struct datadef *type, char *id, int dim);
346
void dsegm_list_free (struct dsegm *dp);
347
struct dsegm *dsegm_list_find (struct dsegm *dp, char const *name);
348
349
10.3k
#define DS_KEY     0
350
10.5k
#define DS_CONTENT 1
351
12.6k
#define DS_MAX     2
352
353
extern struct dsegm *dsdef[];
354

355
447k
#define VART_STRING 0
356
184k
#define VART_BOOL   1
357
150k
#define VART_INT    2
358
359
enum
360
  {
361
    VAR_OK,           /* operation succeeded */
362
    VAR_ERR_NOTSET,   /* Only for variable_get: variable is not set */
363
    VAR_ERR_NOTDEF,   /* no such variable */
364
    VAR_ERR_BADTYPE,  /* variable cannot be coerced to the requested type
365
       (software error) */
366
    VAR_ERR_BADVALUE, /* Only for variable_set: the value is not valid for
367
       this variable. */
368
    VAR_ERR_GDBM,     /* GDBM error */
369
  };
370
371
372
int variable_set (const char *name, int type, void *val);
373
int variable_get (const char *name, int type, void **val);
374
int variable_unset(const char *name);
375
int variable_is_set (const char *name);
376
int variable_is_true (const char *name);
377
void variable_print_all (FILE *fp);
378
static inline int
379
variable_has_errno (char *varname, int e)
380
51.2k
{
381
51.2k
  return variable_get (varname, VART_INT, (void**)&e) == VAR_OK && e == 1;
382
51.2k
}
Unexecuted instantiation: gdbm_fuzzer.c:variable_has_errno
gram.c:variable_has_errno
Line
Count
Source
380
27.2k
{
381
27.2k
  return variable_get (varname, VART_INT, (void**)&e) == VAR_OK && e == 1;
382
27.2k
}
Unexecuted instantiation: lex.c:variable_has_errno
gdbmshell.c:variable_has_errno
Line
Count
Source
380
24.0k
{
381
24.0k
  return variable_get (varname, VART_INT, (void**)&e) == VAR_OK && e == 1;
382
24.0k
}
Unexecuted instantiation: var.c:variable_has_errno
Unexecuted instantiation: util.c:variable_has_errno
Unexecuted instantiation: pagerfile.c:variable_has_errno
Unexecuted instantiation: datconv.c:variable_has_errno
Unexecuted instantiation: input-file.c:variable_has_errno
Unexecuted instantiation: input-null.c:variable_has_errno
383
static inline int
384
gdbm_error_is_masked (int e)
385
32.8k
{
386
32.8k
  return variable_has_errno ("errormask", e);
387
32.8k
}
Unexecuted instantiation: gdbm_fuzzer.c:gdbm_error_is_masked
gram.c:gdbm_error_is_masked
Line
Count
Source
385
27.2k
{
386
27.2k
  return variable_has_errno ("errormask", e);
387
27.2k
}
Unexecuted instantiation: lex.c:gdbm_error_is_masked
gdbmshell.c:gdbm_error_is_masked
Line
Count
Source
385
5.55k
{
386
5.55k
  return variable_has_errno ("errormask", e);
387
5.55k
}
Unexecuted instantiation: var.c:gdbm_error_is_masked
Unexecuted instantiation: util.c:gdbm_error_is_masked
Unexecuted instantiation: pagerfile.c:gdbm_error_is_masked
Unexecuted instantiation: datconv.c:gdbm_error_is_masked
Unexecuted instantiation: input-file.c:gdbm_error_is_masked
Unexecuted instantiation: input-null.c:gdbm_error_is_masked
388

389
int unescape (int c);
390
int escape (int c);
391
void begin_def (void);
392
void end_def (void);
393
394
int yylex (void);
395
int yylex_destroy (void);
396
void yyerror (char const *s);
397
#define YYERROR_IS_DECLARED 1
398
int yyparse (void);
399
400
void lex_trace (int n);
401
void gram_trace (int n);
402
403
void datum_format (PAGERFILE *fp, datum const *dat, struct dsegm *ds);
404
void datum_format_file (FILE *fp, datum const *dat, struct dsegm *ds);
405
int datum_scan (datum *dat, struct dsegm *ds, struct kvpair *kv);
406
void dsprint (PAGERFILE *fp, int what, struct dsegm *ds);
407
408
char *mkfilename (const char *dir, const char *file, const char *suf);
409
char *tildexpand (char *s);
410
int vgetyn (const char *prompt, va_list ap);
411
int getyn (const char *prompt, ...) GDBM_PRINTFLIKE (1, 2);
412
413
int getnum (int *pnum, char *arg, char **endp);
414
415
int gdbmshell (instream_t input);
416
int gdbmshell_run (int (*init) (void *, instream_t *), void *data);
417
418
int gdbmshell_setopt (char *name, int opt, int val);
419
420
void variables_init (void);
421
void variables_free (void);