Imported Upstream version 2.8.4
[platform/upstream/man-db.git] / src / accessdb.c
1 /*
2  * accessdb.c: show every key/content pair in the database.
3  *
4  * Copyright (C) 1994, 1995 Graeme W. Wilford. (Wilf.)
5  * Copyright (C) 2002 Colin Watson.
6  *
7  * This file is part of man-db.
8  *
9  * man-db is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * man-db is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with man-db; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  * Tue Apr 26 12:56:44 BST 1994  Wilf. (G.Wilford@ee.surrey.ac.uk) 
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif /* HAVE_CONFIG_H */
29
30 #include <stdio.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include "argp.h"
36 #include "progname.h"
37 #include "xvasprintf.h"
38
39 #include "gettext.h"
40 #define _(String) gettext (String)
41 #define N_(String) gettext_noop (String)
42
43 #include "manconfig.h"
44
45 #include "error.h"
46 #include "sandbox.h"
47
48 #include "mydbm.h"
49
50 const char *cat_root;
51 man_sandbox *sandbox;  /* unused, but needed by libman */
52
53 /* for db_storage.c */
54 char *database;
55
56 const char *argp_program_version = "accessdb " PACKAGE_VERSION;
57 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
58 error_t argp_err_exit_status = FAIL;
59
60 static const char args_doc[] = N_("[MAN DATABASE]");
61 static const char doc[] = "\v" N_("The man database defaults to %s%s.");
62
63 static struct argp_option options[] = {
64         { "debug",      'd',    0,      0,      N_("emit debugging messages") },
65         { 0, 'h', 0, OPTION_HIDDEN, 0 }, /* compatibility for --help */
66         { 0 }
67 };
68
69 static error_t parse_opt (int key, char *arg, struct argp_state *state)
70 {
71         switch (key) {
72                 case 'd':
73                         debug_level = 1;
74                         return 0;
75                 case 'h':
76                         argp_state_help (state, state->out_stream,
77                                          ARGP_HELP_STD_HELP &
78                                          ~ARGP_HELP_PRE_DOC);
79                         break;
80                 case ARGP_KEY_ARG:
81                         if (database)
82                                 argp_usage (state);
83                         database = arg;
84                         return 0;
85                 case ARGP_KEY_NO_ARGS:
86                         database = mkdbname (cat_root);
87                         return 0;
88         }
89         return ARGP_ERR_UNKNOWN;
90 }
91
92 static char *help_filter (int key, const char *text,
93                           void *input ATTRIBUTE_UNUSED)
94 {
95         switch (key) {
96                 case ARGP_KEY_HELP_PRE_DOC:
97                         /* We have no pre-options help text, but the input
98                          * text may contain header junk due to gettext ("").
99                          */
100                         return NULL;
101                 case ARGP_KEY_HELP_POST_DOC:
102                         return xasprintf (text, cat_root, MAN_DB);
103                 default:
104                         return (char *) text;
105         }
106 }
107
108 static struct argp argp = { options, parse_opt, args_doc, doc, 0,
109                             help_filter };
110
111 int main (int argc, char *argv[])
112 {
113         MYDBM_FILE dbf;
114         datum key;
115         int ret = OK;
116
117         set_program_name (argv[0]);
118
119         init_debug ();
120         init_locale ();
121
122         if (is_directory (FHS_CAT_ROOT) == 1)
123                 cat_root = FHS_CAT_ROOT;
124         else if (is_directory (CAT_ROOT) == 1)
125                 cat_root = CAT_ROOT;
126
127         if (argp_parse (&argp, argc, argv, 0, 0, 0))
128                 exit (FAIL);
129
130         dbf = MYDBM_RDOPEN (database);
131         if (dbf && dbver_rd (dbf)) {
132                 MYDBM_CLOSE (dbf);
133                 dbf = NULL;
134         }
135         if (!dbf)
136                 error (FATAL, errno, _("can't open %s for reading"), database);
137
138         key = MYDBM_FIRSTKEY (dbf);
139
140         while (MYDBM_DPTR (key) != NULL) {
141                 datum content, nextkey;
142                 char *t, *nicekey;
143
144                 content = MYDBM_FETCH (dbf, key);
145                 if (!MYDBM_DPTR (content)) {
146                         debug ("key %s has no content!\n", MYDBM_DPTR (key));
147                         ret = FATAL;
148                         goto next;
149                 }
150                 nicekey = xstrdup (MYDBM_DPTR (key));
151                 while ( (t = strchr (nicekey, '\t')) )
152                         *t = '~';
153                 while ( (t = strchr (MYDBM_DPTR (content), '\t')) )
154                         *t = ' ';
155                 printf ("%s -> \"%s\"\n", nicekey, MYDBM_DPTR (content));
156                 free (nicekey); 
157                 MYDBM_FREE_DPTR (content);
158 next:
159                 nextkey = MYDBM_NEXTKEY (dbf, key);
160                 MYDBM_FREE_DPTR (key);
161                 key = nextkey;
162         }
163
164         MYDBM_CLOSE (dbf);
165         exit (ret);
166 }