catman.c revision 1.22 1 1.22 itojun /* $NetBSD: catman.c,v 1.22 2003/07/13 12:27:14 itojun Exp $ */
2 1.10 dante
3 1.1 jtc /*
4 1.10 dante * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 jtc * All rights reserved.
6 1.1 jtc *
7 1.10 dante * Author: Baldassare Dante Profeta <dante (at) mclink.it>
8 1.10 dante *
9 1.1 jtc * Redistribution and use in source and binary forms, with or without
10 1.1 jtc * modification, are permitted provided that the following conditions
11 1.1 jtc * are met:
12 1.1 jtc * 1. Redistributions of source code must retain the above copyright
13 1.1 jtc * notice, this list of conditions and the following disclaimer.
14 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jtc * notice, this list of conditions and the following disclaimer in the
16 1.1 jtc * documentation and/or other materials provided with the distribution.
17 1.1 jtc * 3. All advertising materials mentioning features or use of this software
18 1.1 jtc * must display the following acknowledgement:
19 1.10 dante * This product includes software developed by the NetBSD
20 1.10 dante * Foundation, Inc. and its contributors.
21 1.10 dante * 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.10 dante * contributors may be used to endorse or promote products derived
23 1.10 dante * from this software without specific prior written permission.
24 1.1 jtc *
25 1.10 dante * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.10 dante * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.10 dante * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.10 dante * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.10 dante * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.10 dante * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.10 dante * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.10 dante * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.10 dante * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.10 dante * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.10 dante * POSSIBILITY OF SUCH DAMAGE.
36 1.1 jtc */
37 1.2 mycroft
38 1.10 dante #include <sys/types.h>
39 1.10 dante #include <sys/queue.h>
40 1.7 lukem #include <sys/cdefs.h>
41 1.10 dante #include <sys/param.h>
42 1.4 chopps #include <sys/stat.h>
43 1.4 chopps #include <sys/wait.h>
44 1.14 tsutsui #include <sys/utsname.h>
45 1.10 dante #include <ctype.h>
46 1.6 cgd #include <dirent.h>
47 1.6 cgd #include <err.h>
48 1.6 cgd #include <errno.h>
49 1.10 dante #include <fnmatch.h>
50 1.6 cgd #include <limits.h>
51 1.10 dante #include <libgen.h>
52 1.1 jtc #include <stdio.h>
53 1.1 jtc #include <stdlib.h>
54 1.1 jtc #include <string.h>
55 1.1 jtc #include <unistd.h>
56 1.10 dante #include <glob.h>
57 1.1 jtc
58 1.18 mycroft #include "manconf.h"
59 1.6 cgd #include "pathnames.h"
60 1.4 chopps
61 1.10 dante int f_nowhatis = 0;
62 1.10 dante int f_noaction = 0;
63 1.10 dante int f_noformat = 0;
64 1.10 dante int f_ignerr = 0;
65 1.10 dante int f_noprint = 0;
66 1.10 dante int dowhatis = 0;
67 1.10 dante
68 1.15 jdolecek TAG *defp; /* pointer to _default list */
69 1.15 jdolecek
70 1.15 jdolecek int main __P((int, char * const *));
71 1.15 jdolecek static void setdefentries __P((char *, char *, const char *));
72 1.10 dante static void uniquepath __P((void));
73 1.10 dante static void catman __P((void));
74 1.10 dante static void scanmandir __P((const char *, const char *));
75 1.22 itojun static int splitentry __P((char *, char *, size_t, char *, size_t));
76 1.10 dante static void setcatsuffix __P((char *, const char *, const char *));
77 1.10 dante static void makecat __P((const char *, const char *, const char *,
78 1.10 dante const char *));
79 1.10 dante static void makewhatis __P((void));
80 1.10 dante static void dosystem __P((const char *));
81 1.10 dante static void usage __P((void));
82 1.10 dante
83 1.1 jtc
84 1.1 jtc int
85 1.1 jtc main(argc, argv)
86 1.4 chopps int argc;
87 1.15 jdolecek char * const *argv;
88 1.1 jtc {
89 1.10 dante char *m_path = NULL;
90 1.10 dante char *m_add = NULL;
91 1.4 chopps int c;
92 1.1 jtc
93 1.10 dante while ((c = getopt(argc, argv, "km:M:npsw")) != -1) {
94 1.1 jtc switch (c) {
95 1.4 chopps case 'k':
96 1.4 chopps f_ignerr = 1;
97 1.4 chopps break;
98 1.1 jtc case 'n':
99 1.4 chopps f_nowhatis = 1;
100 1.1 jtc break;
101 1.1 jtc case 'p':
102 1.4 chopps f_noaction = 1;
103 1.4 chopps break;
104 1.4 chopps case 's':
105 1.4 chopps f_noprint = 1;
106 1.1 jtc break;
107 1.1 jtc case 'w':
108 1.4 chopps f_noformat = 1;
109 1.1 jtc break;
110 1.10 dante case 'm':
111 1.10 dante m_add = optarg;
112 1.10 dante break;
113 1.1 jtc case 'M':
114 1.10 dante m_path = optarg;
115 1.1 jtc break;
116 1.1 jtc default:
117 1.1 jtc usage();
118 1.1 jtc }
119 1.1 jtc }
120 1.1 jtc
121 1.1 jtc argc -= optind;
122 1.1 jtc argv += optind;
123 1.1 jtc
124 1.4 chopps if (f_noprint && f_noaction)
125 1.4 chopps f_noprint = 0;
126 1.4 chopps
127 1.6 cgd if (argc > 1)
128 1.6 cgd usage();
129 1.10 dante
130 1.10 dante config(_PATH_MANCONF);
131 1.15 jdolecek setdefentries(m_path, m_add, (argc == 0) ? NULL : argv[argc-1]);
132 1.10 dante uniquepath();
133 1.1 jtc
134 1.5 chopps if (f_noformat == 0 || f_nowhatis == 0)
135 1.10 dante catman();
136 1.4 chopps if (f_nowhatis == 0 && dowhatis)
137 1.10 dante makewhatis();
138 1.1 jtc
139 1.10 dante return(0);
140 1.1 jtc }
141 1.1 jtc
142 1.10 dante static void
143 1.10 dante setdefentries(m_path, m_add, sections)
144 1.10 dante char *m_path;
145 1.10 dante char *m_add;
146 1.15 jdolecek const char *sections;
147 1.10 dante {
148 1.15 jdolecek TAG *defnewp, *sectnewp, *subp;
149 1.15 jdolecek ENTRY *e_defp, *e_subp;
150 1.15 jdolecek const char *p;
151 1.15 jdolecek char *slashp, *machine;
152 1.10 dante char buf[MAXPATHLEN * 2];
153 1.10 dante int i;
154 1.10 dante
155 1.10 dante /* Get the machine type. */
156 1.14 tsutsui if ((machine = getenv("MACHINE")) == NULL) {
157 1.14 tsutsui struct utsname utsname;
158 1.14 tsutsui
159 1.14 tsutsui if (uname(&utsname) == -1) {
160 1.14 tsutsui perror("uname");
161 1.14 tsutsui exit(1);
162 1.14 tsutsui }
163 1.14 tsutsui machine = utsname.machine;
164 1.14 tsutsui }
165 1.10 dante
166 1.10 dante /* If there's no _default list, create an empty one. */
167 1.17 lukem defp = getlist("_default", 1);
168 1.10 dante
169 1.17 lukem subp = getlist("_subdir", 1);
170 1.15 jdolecek
171 1.10 dante /*
172 1.10 dante * 0: If one or more sections was specified, rewrite _subdir list.
173 1.10 dante */
174 1.10 dante if (sections != NULL) {
175 1.17 lukem sectnewp = getlist("_section_new", 1);
176 1.21 itojun for (p = sections; *p;) {
177 1.15 jdolecek i = snprintf(buf, sizeof(buf), "man%c", *p++);
178 1.21 itojun for (; *p && !isdigit(*p) && i < sizeof(buf) - 1; i++)
179 1.15 jdolecek buf[i] = *p++;
180 1.15 jdolecek buf[i] = '\0';
181 1.15 jdolecek addentry(sectnewp, buf, 0);
182 1.10 dante }
183 1.15 jdolecek subp = sectnewp;
184 1.10 dante }
185 1.10 dante
186 1.10 dante /*
187 1.10 dante * 1: If the user specified a MANPATH variable, or set the -M
188 1.10 dante * option, we replace the _default list with the user's list,
189 1.10 dante * appending the entries in the _subdir list and the machine.
190 1.10 dante */
191 1.10 dante if (m_path == NULL)
192 1.10 dante m_path = getenv("MANPATH");
193 1.10 dante if (m_path != NULL) {
194 1.17 lukem while ((e_defp = TAILQ_FIRST(&defp->list)) != NULL) {
195 1.10 dante free(e_defp->s);
196 1.10 dante TAILQ_REMOVE(&defp->list, e_defp, q);
197 1.10 dante }
198 1.10 dante for (p = strtok(m_path, ":");
199 1.10 dante p != NULL; p = strtok(NULL, ":")) {
200 1.10 dante slashp = p[strlen(p) - 1] == '/' ? "" : "/";
201 1.17 lukem TAILQ_FOREACH(e_subp, &subp->list, q) {
202 1.21 itojun if (!strncmp(e_subp->s, "cat", 3))
203 1.10 dante continue;
204 1.10 dante (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
205 1.10 dante p, slashp, e_subp->s, machine);
206 1.15 jdolecek addentry(defp, buf, 0);
207 1.10 dante }
208 1.10 dante }
209 1.10 dante }
210 1.10 dante
211 1.10 dante /*
212 1.10 dante * 2: If the user did not specify MANPATH, -M or a section, rewrite
213 1.10 dante * the _default list to include the _subdir list and the machine.
214 1.10 dante */
215 1.10 dante if (m_path == NULL) {
216 1.17 lukem defp = getlist("_default", 1);
217 1.17 lukem defnewp = getlist("_default_new1", 1);
218 1.17 lukem
219 1.17 lukem TAILQ_FOREACH(e_defp, &defp->list, q) {
220 1.10 dante slashp =
221 1.10 dante e_defp->s[strlen(e_defp->s) - 1] == '/' ? "" : "/";
222 1.17 lukem TAILQ_FOREACH(e_subp, &subp->list, q) {
223 1.21 itojun if (!strncmp(e_subp->s, "cat", 3))
224 1.10 dante continue;
225 1.10 dante (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
226 1.15 jdolecek e_defp->s, slashp, e_subp->s, machine);
227 1.15 jdolecek addentry(defnewp, buf, 0);
228 1.10 dante }
229 1.10 dante }
230 1.15 jdolecek defp = defnewp;
231 1.10 dante }
232 1.10 dante
233 1.10 dante /*
234 1.10 dante * 3: If the user set the -m option, insert the user's list before
235 1.10 dante * whatever list we have, again appending the _subdir list and
236 1.10 dante * the machine.
237 1.10 dante */
238 1.10 dante if (m_add != NULL)
239 1.10 dante for (p = strtok(m_add, ":"); p != NULL; p = strtok(NULL, ":")) {
240 1.10 dante slashp = p[strlen(p) - 1] == '/' ? "" : "/";
241 1.17 lukem TAILQ_FOREACH(e_subp, &subp->list, q) {
242 1.21 itojun if (!strncmp(e_subp->s, "cat", 3))
243 1.10 dante continue;
244 1.10 dante (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
245 1.10 dante p, slashp, e_subp->s, machine);
246 1.15 jdolecek addentry(defp, buf, 1);
247 1.10 dante }
248 1.10 dante }
249 1.10 dante }
250 1.1 jtc
251 1.10 dante /*
252 1.10 dante * Remove entries (directory) which are symbolic links to other entries.
253 1.10 dante * Some examples are showed below:
254 1.10 dante * 1) if /usr/X11 -> /usr/X11R6 then remove all /usr/X11/man entries.
255 1.10 dante * 2) if /usr/local/man -> /usr/share/man then remove all /usr/local/man
256 1.10 dante * entries
257 1.10 dante */
258 1.10 dante static void
259 1.10 dante uniquepath(void)
260 1.1 jtc {
261 1.15 jdolecek TAG *defnewp;
262 1.10 dante ENTRY *e_defp;
263 1.10 dante glob_t manpaths;
264 1.10 dante struct stat st1;
265 1.10 dante struct stat st2;
266 1.10 dante struct stat st3;
267 1.21 itojun int i, j, len, lnk, gflags;
268 1.10 dante char path[PATH_MAX], *p;
269 1.10 dante
270 1.17 lukem gflags = 0;
271 1.17 lukem TAILQ_FOREACH(e_defp, &defp->list, q) {
272 1.17 lukem glob(e_defp->s, GLOB_BRACE | GLOB_NOSORT | gflags, NULL,
273 1.17 lukem &manpaths);
274 1.17 lukem gflags = GLOB_APPEND;
275 1.10 dante }
276 1.10 dante
277 1.17 lukem defnewp = getlist("_default_new2", 1);
278 1.10 dante
279 1.21 itojun for (i = 0; i < manpaths.gl_pathc; i++) {
280 1.10 dante lnk = 0;
281 1.10 dante lstat(manpaths.gl_pathv[i], &st1);
282 1.21 itojun for (j = 0; j < manpaths.gl_pathc; j++) {
283 1.15 jdolecek if (i != j) {
284 1.10 dante lstat(manpaths.gl_pathv[j], &st2);
285 1.15 jdolecek if (st1.st_ino == st2.st_ino) {
286 1.22 itojun strlcpy(path, manpaths.gl_pathv[i],
287 1.22 itojun sizeof(path));
288 1.21 itojun for (p = path; *(p+1) != '\0';) {
289 1.10 dante p = dirname(p);
290 1.10 dante lstat(p, &st3);
291 1.15 jdolecek if (S_ISLNK(st3.st_mode)) {
292 1.10 dante lnk = 1;
293 1.10 dante break;
294 1.10 dante }
295 1.10 dante }
296 1.10 dante } else {
297 1.10 dante len = readlink(manpaths.gl_pathv[i],
298 1.21 itojun path, sizeof(path) - 1);
299 1.15 jdolecek if (len == -1)
300 1.10 dante continue;
301 1.20 itojun path[len] = '\0';
302 1.15 jdolecek if (!strcmp(path, manpaths.gl_pathv[j]))
303 1.10 dante lnk = 1;
304 1.10 dante }
305 1.15 jdolecek if (lnk)
306 1.10 dante break;
307 1.10 dante }
308 1.10 dante }
309 1.15 jdolecek
310 1.15 jdolecek if (!lnk)
311 1.15 jdolecek addentry(defnewp, manpaths.gl_pathv[i], 0);
312 1.10 dante }
313 1.10 dante
314 1.10 dante globfree(&manpaths);
315 1.10 dante
316 1.15 jdolecek defp = defnewp;
317 1.10 dante }
318 1.10 dante
319 1.10 dante static void
320 1.10 dante catman(void)
321 1.10 dante {
322 1.10 dante ENTRY *e_path;
323 1.15 jdolecek const char *mandir;
324 1.10 dante char catdir[PATH_MAX], *cp;
325 1.10 dante
326 1.17 lukem TAILQ_FOREACH(e_path, &defp->list, q) {
327 1.10 dante mandir = e_path->s;
328 1.22 itojun strlcpy(catdir, mandir, sizeof(catdir));
329 1.21 itojun if (!(cp = strstr(catdir, "man/man")))
330 1.10 dante continue;
331 1.21 itojun cp += 4; *cp++ = 'c'; *cp++ = 'a'; *cp = 't';
332 1.10 dante scanmandir(catdir, mandir);
333 1.10 dante }
334 1.10 dante }
335 1.10 dante
336 1.10 dante static void
337 1.10 dante scanmandir(catdir, mandir)
338 1.10 dante const char *catdir;
339 1.10 dante const char *mandir;
340 1.10 dante {
341 1.10 dante TAG *buildp, *crunchp;
342 1.10 dante ENTRY *e_build, *e_crunch;
343 1.4 chopps char manpage[PATH_MAX];
344 1.4 chopps char catpage[PATH_MAX];
345 1.10 dante char linkname[PATH_MAX];
346 1.10 dante char buffer[PATH_MAX], *bp;
347 1.10 dante char tmp[PATH_MAX];
348 1.10 dante char buildsuff[256], buildcmd[256];
349 1.10 dante char crunchsuff[256], crunchcmd[256];
350 1.10 dante char match[256];
351 1.4 chopps struct stat manstat;
352 1.4 chopps struct stat catstat;
353 1.10 dante struct stat lnkstat;
354 1.4 chopps struct dirent *dp;
355 1.4 chopps DIR *dirp;
356 1.10 dante int len, error;
357 1.10 dante
358 1.10 dante if ((dirp = opendir(mandir)) == 0) {
359 1.10 dante warn("can't open %s", mandir);
360 1.10 dante return;
361 1.10 dante }
362 1.4 chopps
363 1.10 dante if (stat(catdir, &catstat) < 0) {
364 1.10 dante if (errno != ENOENT) {
365 1.10 dante warn("can't stat %s", catdir);
366 1.10 dante closedir(dirp);
367 1.10 dante return;
368 1.10 dante }
369 1.10 dante if (f_noprint == 0)
370 1.10 dante printf("mkdir %s\n", catdir);
371 1.21 itojun if (f_noaction == 0 && mkdir(catdir, 0755) < 0) {
372 1.10 dante warn("can't create %s", catdir);
373 1.10 dante closedir(dirp);
374 1.10 dante return;
375 1.4 chopps }
376 1.10 dante }
377 1.10 dante
378 1.10 dante while ((dp = readdir(dirp)) != NULL) {
379 1.10 dante if (strcmp(dp->d_name, ".") == 0 ||
380 1.10 dante strcmp(dp->d_name, "..") == 0)
381 1.10 dante continue;
382 1.10 dante
383 1.10 dante snprintf(manpage, sizeof(manpage), "%s/%s", mandir, dp->d_name);
384 1.10 dante snprintf(catpage, sizeof(catpage), "%s/%s", catdir, dp->d_name);
385 1.4 chopps
386 1.13 mycroft e_build = NULL;
387 1.17 lukem buildp = getlist("_build", 1);
388 1.17 lukem TAILQ_FOREACH(e_build, &buildp->list, q) {
389 1.22 itojun splitentry(e_build->s, buildsuff, sizeof(buildsuff),
390 1.22 itojun buildcmd, sizeof(buildcmd));
391 1.17 lukem snprintf(match, sizeof(match), "*%s",
392 1.17 lukem buildsuff);
393 1.21 itojun if (!fnmatch(match, manpage, 0))
394 1.17 lukem break;
395 1.10 dante }
396 1.4 chopps
397 1.21 itojun if (e_build == NULL)
398 1.4 chopps continue;
399 1.13 mycroft
400 1.13 mycroft e_crunch = NULL;
401 1.17 lukem crunchp = getlist("_crunch", 1);
402 1.17 lukem TAILQ_FOREACH(e_crunch, &crunchp->list, q) {
403 1.22 itojun splitentry(e_crunch->s, crunchsuff, sizeof(crunchsuff),
404 1.22 itojun crunchcmd, sizeof(crunchcmd));
405 1.17 lukem snprintf(match, sizeof(match), "*%s", crunchsuff);
406 1.21 itojun if (!fnmatch(match, manpage, 0))
407 1.17 lukem break;
408 1.4 chopps }
409 1.4 chopps
410 1.10 dante if (lstat(manpage, &manstat) <0) {
411 1.10 dante warn("can't stat %s", manpage);
412 1.10 dante continue;
413 1.10 dante } else {
414 1.21 itojun if (S_ISLNK(manstat.st_mode)) {
415 1.22 itojun strlcpy(buffer, catpage, sizeof(buffer));
416 1.22 itojun strlcpy(linkname, basename(buffer),
417 1.22 itojun sizeof(linkname));
418 1.20 itojun len = readlink(manpage, buffer,
419 1.20 itojun sizeof(buffer) - 1);
420 1.20 itojun buffer[PATH_MAX - 1] = '\0';
421 1.20 itojun if (len == -1) {
422 1.10 dante warn("can't stat read symbolic link %s",
423 1.22 itojun manpage);
424 1.10 dante continue;
425 1.10 dante }
426 1.10 dante buffer[len] = '\0';
427 1.10 dante bp = basename(buffer);
428 1.22 itojun strlcpy(tmp, manpage, sizeof(tmp));
429 1.10 dante snprintf(manpage, sizeof(manpage), "%s/%s",
430 1.22 itojun dirname(tmp), bp);
431 1.22 itojun strlcpy(tmp, catpage, sizeof(tmp));
432 1.10 dante snprintf(catpage, sizeof(catpage), "%s/%s",
433 1.22 itojun dirname(tmp), buffer);
434 1.1 jtc }
435 1.10 dante else
436 1.21 itojun *linkname = '\0';
437 1.10 dante }
438 1.10 dante
439 1.21 itojun if (!e_crunch) {
440 1.10 dante *crunchsuff = *crunchcmd = '\0';
441 1.10 dante }
442 1.10 dante setcatsuffix(catpage, buildsuff, crunchsuff);
443 1.21 itojun if (*linkname != '\0')
444 1.10 dante setcatsuffix(linkname, buildsuff, crunchsuff);
445 1.1 jtc
446 1.10 dante if (stat(manpage, &manstat) < 0) {
447 1.10 dante warn("can't stat %s", manpage);
448 1.10 dante continue;
449 1.1 jtc }
450 1.1 jtc
451 1.10 dante if (!S_ISREG(manstat.st_mode)) {
452 1.21 itojun warnx("not a regular file %s", manpage);
453 1.10 dante continue;
454 1.10 dante }
455 1.4 chopps
456 1.10 dante if ((error = stat(catpage, &catstat)) &&
457 1.10 dante errno != ENOENT) {
458 1.10 dante warn("can't stat %s", catpage);
459 1.10 dante continue;
460 1.10 dante }
461 1.4 chopps
462 1.10 dante if ((error && errno == ENOENT) ||
463 1.11 mycroft manstat.st_mtime > catstat.st_mtime) {
464 1.10 dante if (f_noformat) {
465 1.10 dante dowhatis = 1;
466 1.10 dante } else {
467 1.10 dante /*
468 1.10 dante * reformat out of date manpage
469 1.10 dante */
470 1.10 dante makecat(manpage, catpage, buildcmd, crunchcmd);
471 1.10 dante dowhatis = 1;
472 1.4 chopps }
473 1.10 dante }
474 1.4 chopps
475 1.21 itojun if (*linkname != '\0') {
476 1.22 itojun strlcpy(tmp, catpage, sizeof(tmp));
477 1.10 dante snprintf(tmp, sizeof(tmp), "%s/%s", dirname(tmp),
478 1.10 dante linkname);
479 1.10 dante if ((error = lstat(tmp, &lnkstat)) &&
480 1.5 chopps errno != ENOENT) {
481 1.10 dante warn("can't stat %s", tmp);
482 1.4 chopps continue;
483 1.4 chopps }
484 1.4 chopps
485 1.10 dante if (error && errno == ENOENT) {
486 1.10 dante if (f_noformat) {
487 1.5 chopps dowhatis = 1;
488 1.10 dante } else {
489 1.5 chopps /*
490 1.10 dante * create symbolic link
491 1.5 chopps */
492 1.5 chopps if (f_noprint == 0)
493 1.10 dante printf("ln -s %s %s\n", catpage,
494 1.21 itojun linkname);
495 1.10 dante if (f_noaction == 0) {
496 1.22 itojun strlcpy(tmp, catpage,
497 1.22 itojun sizeof(tmp));
498 1.21 itojun if (chdir(dirname(tmp)) == -1) {
499 1.10 dante warn("can't chdir");
500 1.10 dante continue;
501 1.10 dante }
502 1.10 dante
503 1.15 jdolecek if (symlink(catpage, linkname)
504 1.10 dante == -1) {
505 1.10 dante warn("can't create"
506 1.10 dante " symbolic"
507 1.10 dante " link %s",
508 1.10 dante linkname);
509 1.10 dante continue;
510 1.10 dante }
511 1.10 dante }
512 1.5 chopps dowhatis = 1;
513 1.5 chopps }
514 1.4 chopps }
515 1.4 chopps }
516 1.1 jtc }
517 1.10 dante closedir(dirp);
518 1.1 jtc }
519 1.1 jtc
520 1.10 dante static int
521 1.22 itojun splitentry(s, first, firstlen, second, secondlen)
522 1.10 dante char *s;
523 1.10 dante char *first;
524 1.22 itojun size_t firstlen;
525 1.10 dante char *second;
526 1.22 itojun size_t secondlen;
527 1.1 jtc {
528 1.10 dante char *c;
529 1.10 dante
530 1.21 itojun for (c = s; *c != '\0' && !isspace(*c); ++c)
531 1.21 itojun ;
532 1.21 itojun if (*c == '\0')
533 1.10 dante return(0);
534 1.22 itojun if (c - s + 1 > firstlen)
535 1.22 itojun return(0);
536 1.10 dante strncpy(first, s, c-s);
537 1.10 dante first[c-s] = '\0';
538 1.21 itojun for (; *c != '\0' && isspace(*c); ++c)
539 1.21 itojun ;
540 1.22 itojun if (strlcpy(second, c, secondlen) >= secondlen)
541 1.22 itojun return(0);
542 1.22 itojun return(1);
543 1.10 dante }
544 1.10 dante
545 1.10 dante static void
546 1.10 dante setcatsuffix(catpage, suffix, crunchsuff)
547 1.10 dante char *catpage;
548 1.10 dante const char *suffix;
549 1.10 dante const char *crunchsuff;
550 1.10 dante {
551 1.10 dante TAG *tp;
552 1.10 dante char *p;
553 1.10 dante
554 1.21 itojun for (p = catpage + strlen(catpage); p != catpage; p--)
555 1.21 itojun if (!fnmatch(suffix, p, 0)) {
556 1.17 lukem tp = getlist("_suffix", 1);
557 1.17 lukem if (! TAILQ_EMPTY(&tp->list)) {
558 1.17 lukem sprintf(p, "%s%s",
559 1.17 lukem TAILQ_FIRST(&tp->list)->s, crunchsuff);
560 1.10 dante } else {
561 1.10 dante sprintf(p, ".0%s", crunchsuff);
562 1.10 dante }
563 1.10 dante break;
564 1.10 dante }
565 1.10 dante }
566 1.10 dante
567 1.10 dante static void
568 1.10 dante makecat(manpage, catpage, buildcmd, crunchcmd)
569 1.10 dante const char *manpage;
570 1.10 dante const char *catpage;
571 1.10 dante const char *buildcmd;
572 1.10 dante const char *crunchcmd;
573 1.10 dante {
574 1.10 dante char crunchbuf[1024];
575 1.10 dante char sysbuf[2048];
576 1.10 dante
577 1.10 dante snprintf(sysbuf, sizeof(sysbuf), buildcmd, manpage);
578 1.10 dante
579 1.21 itojun if (*crunchcmd != '\0') {
580 1.10 dante snprintf(crunchbuf, sizeof(crunchbuf), crunchcmd, catpage);
581 1.10 dante snprintf(sysbuf, sizeof(sysbuf), "%s | %s", sysbuf, crunchbuf);
582 1.10 dante } else {
583 1.10 dante snprintf(sysbuf, sizeof(sysbuf), "%s > %s", sysbuf, catpage);
584 1.10 dante }
585 1.1 jtc
586 1.4 chopps if (f_noprint == 0)
587 1.1 jtc printf("%s\n", sysbuf);
588 1.4 chopps if (f_noaction == 0)
589 1.4 chopps dosystem(sysbuf);
590 1.1 jtc }
591 1.1 jtc
592 1.10 dante static void
593 1.10 dante makewhatis(void)
594 1.10 dante {
595 1.10 dante TAG *whatdbp;
596 1.10 dante ENTRY *e_whatdb;
597 1.10 dante char sysbuf[1024];
598 1.10 dante
599 1.17 lukem whatdbp = getlist("_whatdb", 1);
600 1.17 lukem TAILQ_FOREACH(e_whatdb, &whatdbp->list, q) {
601 1.10 dante snprintf(sysbuf, sizeof(sysbuf), "%s %s",
602 1.17 lukem _PATH_WHATIS, dirname(e_whatdb->s));
603 1.10 dante if (f_noprint == 0)
604 1.10 dante printf("%s\n", sysbuf);
605 1.10 dante if (f_noaction == 0)
606 1.10 dante dosystem(sysbuf);
607 1.10 dante }
608 1.10 dante }
609 1.10 dante
610 1.10 dante static void
611 1.4 chopps dosystem(cmd)
612 1.4 chopps const char *cmd;
613 1.4 chopps {
614 1.4 chopps int status;
615 1.4 chopps
616 1.4 chopps if ((status = system(cmd)) == 0)
617 1.4 chopps return;
618 1.4 chopps
619 1.4 chopps if (status == -1)
620 1.4 chopps err(1, "cannot execute action");
621 1.4 chopps if (WIFSIGNALED(status))
622 1.4 chopps errx(1, "child was signaled to quit. aborting");
623 1.4 chopps if (WIFSTOPPED(status))
624 1.4 chopps errx(1, "child was stopped. aborting");
625 1.4 chopps if (f_ignerr == 0)
626 1.7 lukem errx(1, "*** Exited %d", status);
627 1.7 lukem warnx("*** Exited %d (continuing)", status);
628 1.4 chopps }
629 1.1 jtc
630 1.10 dante static void
631 1.10 dante usage(void)
632 1.1 jtc {
633 1.6 cgd (void)fprintf(stderr,
634 1.10 dante "usage: catman [-knpsw] [-m manpath] [sections]\n");
635 1.10 dante (void)fprintf(stderr,
636 1.10 dante " catman [-knpsw] [-M manpath] [sections]\n");
637 1.1 jtc exit(1);
638 1.1 jtc }
639