1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
/*
* catman.c: create and/or update cat files
*
* Copyright (C) 1994, 1995 Graeme W. Wilford. (Wilf.)
* Copyright (C) 2001, 2002, 2003, 2006, 2007, 2008, 2009, 2010, 2011
* Colin Watson.
*
* This file is part of man-db.
*
* man-db 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 2 of the License, or
* (at your option) any later version.
*
* man-db 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 man-db; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Thu Dec 8 00:03:12 GMT 1994 Wilf. (G.Wilford@ee.surrey.ac.uk)
*/
/* MAX_ARGS must be >= 7, 5 for options, 1 for page and 1 for NULL */
#define MAX_ARGS 1024
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#ifndef NAME_MAX
# if defined(_POSIX_VERSION) && defined(_POSIX_NAME_MAX)
# define NAME_MAX _POSIX_NAME_MAX
# else /* !_POSIX_VERSION */
# ifdef MAXNAMLEN
# define NAME_MAX MAXNAMLEN
# else /* !MAXNAMLEN */
# define NAME_MAX 255 /* default to max */
# endif /* MAXNAMLEN */
# endif /* _POSIX_VERSION */
#endif /* !NAME_MAX */
#ifndef ARG_MAX
# if defined(_POSIX_VERSION) && defined(_POSIX_ARG_MAX)
# define ARG_MAX _POSIX_ARG_MAX
# else /* !_POSIX_VERSION */
# define ARG_MAX 4096 /* default to min */
# endif /* _POSIX_VERSION */
#endif /* !ARG_MAX */
#include "argp.h"
#include "progname.h"
#include "gettext.h"
#include <locale.h>
#define _(String) gettext (String)
#define N_(String) gettext_noop (String)
#include "manconfig.h"
#include "cleanup.h"
#include "error.h"
#include "pipeline.h"
#include "sandbox.h"
#include "mydbm.h"
#include "db_storage.h"
#include "manp.h"
/* globals */
int quiet = 1;
MYDBM_FILE dbf_close_post_fork;
char *manp;
extern char *user_config_file;
char *database;
man_sandbox *sandbox; /* unused, but needed by libman */
static const char **sections;
const char *argp_program_version = "catman " PACKAGE_VERSION;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
error_t argp_err_exit_status = FAIL;
static const char args_doc[] = N_("[SECTION...]");
static struct argp_option options[] = {
{ "debug", 'd', 0, 0, N_("emit debugging messages") },
{ "manpath", 'M', N_("PATH"), 0, N_("set search path for manual pages to PATH") },
{ "config-file", 'C', N_("FILE"), 0, N_("use this user configuration file") },
{ 0, 'h', 0, OPTION_HIDDEN, 0 }, /* compatibility for --help */
{ 0 }
};
static error_t parse_opt (int key, char *arg, struct argp_state *state)
{
char *mansect;
switch (key) {
case 'd':
debug_level = 1;
return 0;
case 'M':
manp = arg;
return 0;
case 'C':
user_config_file = arg;
return 0;
case 'h':
argp_state_help (state, state->out_stream,
ARGP_HELP_STD_HELP);
break;
case ARGP_KEY_ARGS:
sections = xmalloc ((state->argc - state->next + 1) *
sizeof *sections);
memcpy (sections, state->argv + state->next,
(state->argc - state->next) *
sizeof *sections);
sections[state->argc - state->next] = NULL;
return 0;
case ARGP_KEY_NO_ARGS:
mansect = getenv ("MANSECT");
if (mansect && *mansect) {
/* MANSECT contains sections */
const char *sec;
int i = 0;
mansect = xstrdup (mansect);
sections = NULL;
for (sec = strtok (mansect, ":"); sec;
sec = strtok (NULL, ":")) {
sections = xnrealloc
(sections, i + 2,
sizeof *sections);
sections[i++] = sec;
}
if (sections)
sections[i] = NULL;
free (mansect);
} else {
/* use default sections */
static const char *std_sections[] =
STD_SECTIONS;
sections = std_sections;
}
return 0;
}
return ARGP_ERR_UNKNOWN;
}
static struct argp argp = { options, parse_opt, args_doc };
static char *locale;
static char *manpathlist[MAXDIRS];
static void post_fork (void)
{
pop_all_cleanups ();
if (dbf_close_post_fork)
MYDBM_CLOSE (dbf_close_post_fork);
}
/* Execute man with the appropriate catman args. Always frees cmd. */
static void catman (pipecmd *cmd)
{
pipeline *p;
int status;
if (debug_level) {
/* just show the command, but don't execute it */
fputs ("man command = ", stderr);
pipecmd_dump (cmd, stderr);
putc ('\n', stderr);
pipecmd_free (cmd);
return;
}
p = pipeline_new_commands (cmd, (void *) 0);
status = pipeline_run (p);
if (status)
error (CHILD_FAIL, 0,
_("man command failed with exit status %d"), status);
}
/* Add key to this command, stripping off tab-and-following if necessary.
* Return length of argument.
*/
static size_t add_arg (pipecmd *cmd, datum key)
{
char *tab;
size_t len;
tab = strrchr (MYDBM_DPTR (key), '\t');
if (tab == MYDBM_DPTR (key))
tab = NULL;
if (tab)
*tab = '\0';
pipecmd_arg (cmd, MYDBM_DPTR (key));
len = strlen (MYDBM_DPTR (key));
debug ("key: '%s' (%zu), len: %zd\n",
MYDBM_DPTR (key), (size_t) MYDBM_DSIZE (key), len);
if (tab)
*tab = '\t';
return len;
}
/* find all pages that are in the supplied manpath and section and that are
ultimate source files. */
static int parse_for_sec (const char *manpath, const char *section)
{
MYDBM_FILE dbf;
pipecmd *basecmd, *cmd;
datum key;
size_t arg_size, initial_bit;
int message = 1, first_arg;
dbf = MYDBM_RDOPEN (database);
if (!dbf) {
error (0, errno, _("cannot read database %s"), database);
return 1;
}
if (dbver_rd (dbf)) {
MYDBM_CLOSE (dbf);
return 1;
}
dbf_close_post_fork = dbf;
basecmd = pipecmd_new (MAN);
pipecmd_clearenv (basecmd);
/* As we supply a NULL environment to save precious execve() space,
we must also supply a locale if necessary */
if (locale) {
pipecmd_args (basecmd, "-L", locale, (void *) 0);
initial_bit = sizeof "-L" + strlen (locale) + 1;
} else
initial_bit = 0;
pipecmd_args (basecmd, "-caM", manpath, (void *) 0); /* manpath */
pipecmd_args (basecmd, "-S", section, (void *) 0); /* section */
initial_bit += sizeof MAN + sizeof "-caM" +
strlen (manpath) + strlen (section) + 2;
cmd = pipecmd_dup (basecmd);
first_arg = pipecmd_get_nargs (cmd);
arg_size = initial_bit;
key = MYDBM_FIRSTKEY (dbf);
while (MYDBM_DPTR (key) != NULL) {
datum nextkey;
/* ignore db identifier keys */
if (*MYDBM_DPTR (key) != '$') {
datum content;
content = MYDBM_FETCH (dbf, key);
if (!MYDBM_DPTR (content))
error (FATAL, 0,
_( "NULL content for key: %s"),
MYDBM_DPTR (key));
/* ignore overflow entries */
if (*MYDBM_DPTR (content) != '\t') {
struct mandata entry;
split_content (MYDBM_DPTR (content), &entry);
/* Accept if the entry is an ultimate manual
page and the section matches the one we're
currently dealing with */
if (entry.id == ULT_MAN &&
strcmp (entry.sec, section) == 0) {
if (message) {
printf (_("\nUpdating cat files for section %s of man hierarchy %s\n"),
section, manpath);
message = 0;
}
arg_size += add_arg (cmd, key) + 1;
debug ("arg space free: %zd bytes\n",
ARG_MAX - arg_size);
/* Check to see if we have enough room
to add another max sized filename
and that we haven't run out of array
space too */
if (arg_size >= ARG_MAX - NAME_MAX ||
pipecmd_get_nargs (cmd) ==
MAX_ARGS) {
catman (cmd);
cmd = pipecmd_dup (basecmd);
arg_size = initial_bit;
}
}
/* == MYDBM_DPTR (content), freed below */
entry.addr = NULL;
free_mandata_elements (&entry);
}
/* we don't need the content ever again */
assert (MYDBM_DPTR (content)); /* just to be sure */
MYDBM_FREE_DPTR (content);
}
nextkey = MYDBM_NEXTKEY (dbf, key);
MYDBM_FREE_DPTR (key);
key = nextkey;
}
dbf_close_post_fork = NULL;
MYDBM_CLOSE (dbf);
if (pipecmd_get_nargs (cmd) > first_arg)
catman (cmd);
else
pipecmd_free (cmd);
pipecmd_free (basecmd);
return 0;
}
static int check_access (const char *directory)
{
if (!CAN_ACCESS (directory, W_OK)) {
error (0, errno, _("cannot write within %s"), directory);
return 1;
}
return 0;
}
int main (int argc, char *argv[])
{
char *sys_manp;
char **mp;
const char **sp;
set_program_name (argv[0]);
init_debug ();
pipeline_install_post_fork (post_fork);
init_locale ();
locale = setlocale (LC_MESSAGES, NULL);
if (locale)
locale = xstrdup (locale);
else
locale = xstrdup ("C");
if (argp_parse (&argp, argc, argv, 0, 0, 0))
exit (FAIL);
for (sp = sections; *sp; sp++)
debug ("sections: %s\n", *sp);
/* Deal with the MANPATH */
/* This is required for get_catpath(), regardless */
sys_manp = get_manpath (NULL);
/* pick up the system manpath or use the supplied one */
if (!manp) {
manp = get_mandb_manpath ();
if (!manp)
manp = sys_manp;
}
debug ("manpath=%s\n", manp);
/* get the manpath as an array of pointers */
create_pathlist (manp, manpathlist);
for (mp = manpathlist; *mp; mp++) {
char *catpath;
size_t len;
catpath = get_catpath (*mp, SYSTEM_CAT | USER_CAT);
if (catpath) {
if (is_directory (catpath) != 1) {
free (catpath);
continue;
}
database = mkdbname (catpath);
} else {
if (is_directory (*mp) != 1)
continue;
database = mkdbname (*mp);
catpath = xstrdup (*mp);
}
len = strlen (catpath);
for (sp = sections; *sp; sp++) {
*(catpath + len) = '\0';
catpath = appendstr (catpath, "/cat", *sp, (void *) 0);
if (is_directory (catpath) != 1)
continue;
if (check_access (catpath))
continue;
if (parse_for_sec (*mp, *sp)) {
error (0, 0, _("unable to update %s"), *mp);
break;
}
}
free (catpath);
}
free_pathlist (manpathlist);
free (locale);
exit (OK);
}
|