catman.c revision 1.15 1 1.15 jdolecek /* $NetBSD: catman.c,v 1.15 2000/05/29 21:05:34 jdolecek 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.10 dante #include "config.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.10 dante static int splitentry __P((char *, char *, char *));
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
117 1.1 jtc case '?':
118 1.1 jtc default:
119 1.1 jtc usage();
120 1.1 jtc }
121 1.1 jtc }
122 1.1 jtc
123 1.1 jtc argc -= optind;
124 1.1 jtc argv += optind;
125 1.1 jtc
126 1.4 chopps if (f_noprint && f_noaction)
127 1.4 chopps f_noprint = 0;
128 1.4 chopps
129 1.6 cgd if (argc > 1)
130 1.6 cgd usage();
131 1.10 dante
132 1.10 dante config(_PATH_MANCONF);
133 1.15 jdolecek setdefentries(m_path, m_add, (argc == 0) ? NULL : argv[argc-1]);
134 1.10 dante uniquepath();
135 1.1 jtc
136 1.5 chopps if (f_noformat == 0 || f_nowhatis == 0)
137 1.10 dante catman();
138 1.4 chopps if (f_nowhatis == 0 && dowhatis)
139 1.10 dante makewhatis();
140 1.1 jtc
141 1.10 dante return(0);
142 1.1 jtc }
143 1.1 jtc
144 1.10 dante static void
145 1.10 dante setdefentries(m_path, m_add, sections)
146 1.10 dante char *m_path;
147 1.10 dante char *m_add;
148 1.15 jdolecek const char *sections;
149 1.10 dante {
150 1.15 jdolecek TAG *defnewp, *sectnewp, *subp;
151 1.15 jdolecek ENTRY *e_defp, *e_subp;
152 1.15 jdolecek const char *p;
153 1.15 jdolecek char *slashp, *machine;
154 1.10 dante char buf[MAXPATHLEN * 2];
155 1.10 dante int i;
156 1.10 dante
157 1.10 dante /* Get the machine type. */
158 1.14 tsutsui if ((machine = getenv("MACHINE")) == NULL) {
159 1.14 tsutsui struct utsname utsname;
160 1.14 tsutsui
161 1.14 tsutsui if (uname(&utsname) == -1) {
162 1.14 tsutsui perror("uname");
163 1.14 tsutsui exit(1);
164 1.14 tsutsui }
165 1.14 tsutsui machine = utsname.machine;
166 1.14 tsutsui }
167 1.10 dante
168 1.10 dante /* If there's no _default list, create an empty one. */
169 1.10 dante if ((defp = getlist("_default")) == NULL)
170 1.10 dante defp = addlist("_default");
171 1.10 dante
172 1.15 jdolecek subp = getlist("_subdir");
173 1.15 jdolecek
174 1.10 dante /*
175 1.10 dante * 0: If one or more sections was specified, rewrite _subdir list.
176 1.10 dante */
177 1.10 dante if (sections != NULL) {
178 1.10 dante sectnewp = addlist("_section_new");
179 1.10 dante for(p=sections; *p;) {
180 1.15 jdolecek i = snprintf(buf, sizeof(buf), "man%c", *p++);
181 1.15 jdolecek for(; *p && !isdigit(*p) && i<sizeof(buf)-1; i++)
182 1.15 jdolecek buf[i] = *p++;
183 1.15 jdolecek buf[i] = '\0';
184 1.15 jdolecek addentry(sectnewp, buf, 0);
185 1.10 dante }
186 1.15 jdolecek subp = sectnewp;
187 1.10 dante }
188 1.10 dante
189 1.10 dante /*
190 1.10 dante * 1: If the user specified a MANPATH variable, or set the -M
191 1.10 dante * option, we replace the _default list with the user's list,
192 1.10 dante * appending the entries in the _subdir list and the machine.
193 1.10 dante */
194 1.10 dante if (m_path == NULL)
195 1.10 dante m_path = getenv("MANPATH");
196 1.10 dante if (m_path != NULL) {
197 1.10 dante while ((e_defp = defp->list.tqh_first) != NULL) {
198 1.10 dante free(e_defp->s);
199 1.10 dante TAILQ_REMOVE(&defp->list, e_defp, q);
200 1.10 dante }
201 1.10 dante for (p = strtok(m_path, ":");
202 1.10 dante p != NULL; p = strtok(NULL, ":")) {
203 1.10 dante slashp = p[strlen(p) - 1] == '/' ? "" : "/";
204 1.15 jdolecek e_subp = subp == NULL ? NULL : subp->list.tqh_first;
205 1.10 dante for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
206 1.10 dante if(!strncmp(e_subp->s, "cat", 3))
207 1.10 dante continue;
208 1.10 dante (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
209 1.10 dante p, slashp, e_subp->s, machine);
210 1.15 jdolecek addentry(defp, buf, 0);
211 1.10 dante }
212 1.10 dante }
213 1.10 dante }
214 1.10 dante
215 1.10 dante /*
216 1.10 dante * 2: If the user did not specify MANPATH, -M or a section, rewrite
217 1.10 dante * the _default list to include the _subdir list and the machine.
218 1.10 dante */
219 1.10 dante if (m_path == NULL) {
220 1.10 dante defp = getlist("_default");
221 1.15 jdolecek defnewp = addlist("_default_new1");
222 1.10 dante e_defp =
223 1.10 dante defp->list.tqh_first == NULL ? NULL : defp->list.tqh_first;
224 1.10 dante for (; e_defp; e_defp = e_defp->q.tqe_next) {
225 1.10 dante slashp =
226 1.10 dante e_defp->s[strlen(e_defp->s) - 1] == '/' ? "" : "/";
227 1.15 jdolecek e_subp = subp == NULL ? NULL : subp->list.tqh_first;
228 1.10 dante for (; e_subp; e_subp = e_subp->q.tqe_next) {
229 1.10 dante if(!strncmp(e_subp->s, "cat", 3))
230 1.10 dante continue;
231 1.10 dante (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
232 1.15 jdolecek e_defp->s, slashp, e_subp->s, machine);
233 1.15 jdolecek addentry(defnewp, buf, 0);
234 1.10 dante }
235 1.10 dante }
236 1.15 jdolecek defp = defnewp;
237 1.10 dante }
238 1.10 dante
239 1.10 dante /*
240 1.10 dante * 3: If the user set the -m option, insert the user's list before
241 1.10 dante * whatever list we have, again appending the _subdir list and
242 1.10 dante * the machine.
243 1.10 dante */
244 1.10 dante if (m_add != NULL)
245 1.10 dante for (p = strtok(m_add, ":"); p != NULL; p = strtok(NULL, ":")) {
246 1.10 dante slashp = p[strlen(p) - 1] == '/' ? "" : "/";
247 1.15 jdolecek e_subp = subp == NULL ? NULL : subp->list.tqh_first;
248 1.10 dante for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
249 1.10 dante if(!strncmp(e_subp->s, "cat", 3))
250 1.10 dante continue;
251 1.10 dante (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
252 1.10 dante p, slashp, e_subp->s, machine);
253 1.15 jdolecek addentry(defp, buf, 1);
254 1.10 dante }
255 1.10 dante }
256 1.10 dante }
257 1.1 jtc
258 1.10 dante /*
259 1.10 dante * Remove entries (directory) which are symbolic links to other entries.
260 1.10 dante * Some examples are showed below:
261 1.10 dante * 1) if /usr/X11 -> /usr/X11R6 then remove all /usr/X11/man entries.
262 1.10 dante * 2) if /usr/local/man -> /usr/share/man then remove all /usr/local/man
263 1.10 dante * entries
264 1.10 dante */
265 1.10 dante static void
266 1.10 dante uniquepath(void)
267 1.1 jtc {
268 1.15 jdolecek TAG *defnewp;
269 1.10 dante ENTRY *e_defp;
270 1.10 dante glob_t manpaths;
271 1.10 dante struct stat st1;
272 1.10 dante struct stat st2;
273 1.10 dante struct stat st3;
274 1.10 dante int i,j,len,lnk;
275 1.10 dante char path[PATH_MAX], *p;
276 1.10 dante
277 1.10 dante
278 1.10 dante e_defp = defp->list.tqh_first;
279 1.10 dante glob(e_defp->s, GLOB_BRACE | GLOB_NOSORT, NULL, &manpaths);
280 1.10 dante for(e_defp = e_defp->q.tqe_next; e_defp; e_defp = e_defp->q.tqe_next) {
281 1.10 dante glob(e_defp->s, GLOB_BRACE | GLOB_NOSORT | GLOB_APPEND, NULL,
282 1.10 dante &manpaths);
283 1.10 dante }
284 1.10 dante
285 1.15 jdolecek defnewp = addlist("_default_new2");
286 1.10 dante
287 1.10 dante for(i=0; i<manpaths.gl_pathc; i++) {
288 1.10 dante lnk = 0;
289 1.10 dante lstat(manpaths.gl_pathv[i], &st1);
290 1.10 dante for(j=0; j<manpaths.gl_pathc; j++) {
291 1.15 jdolecek if (i != j) {
292 1.10 dante lstat(manpaths.gl_pathv[j], &st2);
293 1.15 jdolecek if (st1.st_ino == st2.st_ino) {
294 1.10 dante strcpy(path, manpaths.gl_pathv[i]);
295 1.10 dante for(p = path; *(p+1) != '\0';) {
296 1.10 dante p = dirname(p);
297 1.10 dante lstat(p, &st3);
298 1.15 jdolecek if (S_ISLNK(st3.st_mode)) {
299 1.10 dante lnk = 1;
300 1.10 dante break;
301 1.10 dante }
302 1.10 dante }
303 1.10 dante } else {
304 1.10 dante len = readlink(manpaths.gl_pathv[i],
305 1.10 dante path, PATH_MAX);
306 1.15 jdolecek if (len == -1)
307 1.10 dante continue;
308 1.15 jdolecek if (!strcmp(path, manpaths.gl_pathv[j]))
309 1.10 dante lnk = 1;
310 1.10 dante }
311 1.15 jdolecek if (lnk)
312 1.10 dante break;
313 1.10 dante }
314 1.10 dante }
315 1.15 jdolecek
316 1.15 jdolecek if (!lnk)
317 1.15 jdolecek addentry(defnewp, manpaths.gl_pathv[i], 0);
318 1.10 dante }
319 1.10 dante
320 1.10 dante globfree(&manpaths);
321 1.10 dante
322 1.15 jdolecek defp = defnewp;
323 1.10 dante }
324 1.10 dante
325 1.10 dante static void
326 1.10 dante catman(void)
327 1.10 dante {
328 1.10 dante TAG *pathp;
329 1.10 dante ENTRY *e_path;
330 1.15 jdolecek const char *mandir;
331 1.10 dante char catdir[PATH_MAX], *cp;
332 1.10 dante
333 1.10 dante
334 1.10 dante pathp = getlist("_default");
335 1.10 dante for(e_path = pathp->list.tqh_first; e_path;
336 1.10 dante e_path = e_path->q.tqe_next) {
337 1.10 dante mandir = e_path->s;
338 1.15 jdolecek strcpy(catdir, mandir);
339 1.10 dante if(!(cp = strstr(catdir, "man/man")))
340 1.10 dante continue;
341 1.10 dante cp+=4; *cp++ = 'c'; *cp++ = 'a'; *cp = 't';
342 1.10 dante scanmandir(catdir, mandir);
343 1.10 dante }
344 1.10 dante }
345 1.10 dante
346 1.10 dante static void
347 1.10 dante scanmandir(catdir, mandir)
348 1.10 dante const char *catdir;
349 1.10 dante const char *mandir;
350 1.10 dante {
351 1.10 dante TAG *buildp, *crunchp;
352 1.10 dante ENTRY *e_build, *e_crunch;
353 1.4 chopps char manpage[PATH_MAX];
354 1.4 chopps char catpage[PATH_MAX];
355 1.10 dante char linkname[PATH_MAX];
356 1.10 dante char buffer[PATH_MAX], *bp;
357 1.10 dante char tmp[PATH_MAX];
358 1.10 dante char buildsuff[256], buildcmd[256];
359 1.10 dante char crunchsuff[256], crunchcmd[256];
360 1.10 dante char match[256];
361 1.4 chopps struct stat manstat;
362 1.4 chopps struct stat catstat;
363 1.10 dante struct stat lnkstat;
364 1.4 chopps struct dirent *dp;
365 1.4 chopps DIR *dirp;
366 1.10 dante int len, error;
367 1.10 dante
368 1.10 dante if ((dirp = opendir(mandir)) == 0) {
369 1.10 dante warn("can't open %s", mandir);
370 1.10 dante return;
371 1.10 dante }
372 1.4 chopps
373 1.10 dante if (stat(catdir, &catstat) < 0) {
374 1.10 dante if (errno != ENOENT) {
375 1.10 dante warn("can't stat %s", catdir);
376 1.10 dante closedir(dirp);
377 1.10 dante return;
378 1.10 dante }
379 1.10 dante if (f_noprint == 0)
380 1.10 dante printf("mkdir %s\n", catdir);
381 1.10 dante if (f_noaction == 0 && mkdir(catdir,0755) < 0) {
382 1.10 dante warn("can't create %s", catdir);
383 1.10 dante closedir(dirp);
384 1.10 dante return;
385 1.4 chopps }
386 1.10 dante }
387 1.10 dante
388 1.10 dante while ((dp = readdir(dirp)) != NULL) {
389 1.10 dante if (strcmp(dp->d_name, ".") == 0 ||
390 1.10 dante strcmp(dp->d_name, "..") == 0)
391 1.10 dante continue;
392 1.10 dante
393 1.10 dante snprintf(manpage, sizeof(manpage), "%s/%s", mandir, dp->d_name);
394 1.10 dante snprintf(catpage, sizeof(catpage), "%s/%s", catdir, dp->d_name);
395 1.4 chopps
396 1.13 mycroft e_build = NULL;
397 1.10 dante buildp = getlist("_build");
398 1.13 mycroft if(buildp) {
399 1.13 mycroft for(e_build = buildp->list.tqh_first; e_build;
400 1.13 mycroft e_build = e_build->q.tqe_next) {
401 1.13 mycroft splitentry(e_build->s, buildsuff, buildcmd);
402 1.13 mycroft snprintf(match, sizeof(match), "*%s",
403 1.13 mycroft buildsuff);
404 1.13 mycroft if(!fnmatch(match, manpage, 0))
405 1.13 mycroft break;
406 1.13 mycroft }
407 1.10 dante }
408 1.4 chopps
409 1.13 mycroft if(e_build == NULL)
410 1.4 chopps continue;
411 1.13 mycroft
412 1.13 mycroft e_crunch = NULL;
413 1.13 mycroft crunchp = getlist("_crunch");
414 1.15 jdolecek if (crunchp) {
415 1.13 mycroft for(e_crunch = crunchp->list.tqh_first; e_crunch;
416 1.13 mycroft e_crunch=e_crunch->q.tqe_next) {
417 1.13 mycroft splitentry(e_crunch->s, crunchsuff, crunchcmd);
418 1.13 mycroft snprintf(match, sizeof(match), "*%s",
419 1.13 mycroft crunchsuff);
420 1.13 mycroft if(!fnmatch(match, manpage, 0))
421 1.13 mycroft break;
422 1.10 dante }
423 1.4 chopps }
424 1.4 chopps
425 1.10 dante if (lstat(manpage, &manstat) <0) {
426 1.10 dante warn("can't stat %s", manpage);
427 1.10 dante continue;
428 1.10 dante } else {
429 1.10 dante if(S_ISLNK(manstat.st_mode)) {
430 1.10 dante strcpy(buffer, catpage);
431 1.10 dante strcpy(linkname, basename(buffer));
432 1.10 dante len = readlink(manpage, buffer, PATH_MAX);
433 1.10 dante if(len == -1) {
434 1.10 dante warn("can't stat read symbolic link %s",
435 1.10 dante manpage);
436 1.10 dante continue;
437 1.10 dante }
438 1.10 dante buffer[len] = '\0';
439 1.10 dante bp = basename(buffer);
440 1.10 dante strcpy(tmp, manpage);
441 1.10 dante snprintf(manpage, sizeof(manpage), "%s/%s",
442 1.10 dante dirname(tmp), bp);
443 1.10 dante strcpy(tmp, catpage);
444 1.10 dante snprintf(catpage, sizeof(catpage), "%s/%s",
445 1.10 dante dirname(tmp), buffer);
446 1.1 jtc }
447 1.10 dante else
448 1.10 dante *linkname='\0';
449 1.10 dante }
450 1.10 dante
451 1.10 dante if(!e_crunch) {
452 1.10 dante *crunchsuff = *crunchcmd = '\0';
453 1.10 dante }
454 1.10 dante setcatsuffix(catpage, buildsuff, crunchsuff);
455 1.10 dante if(*linkname != '\0')
456 1.10 dante setcatsuffix(linkname, buildsuff, crunchsuff);
457 1.1 jtc
458 1.10 dante if (stat(manpage, &manstat) < 0) {
459 1.10 dante warn("can't stat %s", manpage);
460 1.10 dante continue;
461 1.1 jtc }
462 1.1 jtc
463 1.10 dante if (!S_ISREG(manstat.st_mode)) {
464 1.10 dante warnx("not a regular file %s",manpage);
465 1.10 dante continue;
466 1.10 dante }
467 1.4 chopps
468 1.10 dante if ((error = stat(catpage, &catstat)) &&
469 1.10 dante errno != ENOENT) {
470 1.10 dante warn("can't stat %s", catpage);
471 1.10 dante continue;
472 1.10 dante }
473 1.4 chopps
474 1.10 dante if ((error && errno == ENOENT) ||
475 1.11 mycroft manstat.st_mtime > catstat.st_mtime) {
476 1.10 dante if (f_noformat) {
477 1.10 dante dowhatis = 1;
478 1.10 dante } else {
479 1.10 dante /*
480 1.10 dante * reformat out of date manpage
481 1.10 dante */
482 1.10 dante makecat(manpage, catpage, buildcmd, crunchcmd);
483 1.10 dante dowhatis = 1;
484 1.4 chopps }
485 1.10 dante }
486 1.4 chopps
487 1.10 dante if(*linkname != '\0') {
488 1.10 dante strcpy(tmp, catpage);
489 1.10 dante snprintf(tmp, sizeof(tmp), "%s/%s", dirname(tmp),
490 1.10 dante linkname);
491 1.10 dante if ((error = lstat(tmp, &lnkstat)) &&
492 1.5 chopps errno != ENOENT) {
493 1.10 dante warn("can't stat %s", tmp);
494 1.4 chopps continue;
495 1.4 chopps }
496 1.4 chopps
497 1.10 dante if (error && errno == ENOENT) {
498 1.10 dante if (f_noformat) {
499 1.5 chopps dowhatis = 1;
500 1.10 dante } else {
501 1.5 chopps /*
502 1.10 dante * create symbolic link
503 1.5 chopps */
504 1.5 chopps if (f_noprint == 0)
505 1.10 dante printf("ln -s %s %s\n", catpage,
506 1.10 dante linkname);
507 1.10 dante if (f_noaction == 0) {
508 1.10 dante strcpy(tmp, catpage);
509 1.10 dante if(chdir(dirname(tmp)) == -1) {
510 1.10 dante warn("can't chdir");
511 1.10 dante continue;
512 1.10 dante }
513 1.10 dante
514 1.15 jdolecek if (symlink(catpage, linkname)
515 1.10 dante == -1) {
516 1.10 dante warn("can't create"
517 1.10 dante " symbolic"
518 1.10 dante " link %s",
519 1.10 dante linkname);
520 1.10 dante continue;
521 1.10 dante }
522 1.10 dante }
523 1.5 chopps dowhatis = 1;
524 1.5 chopps }
525 1.4 chopps }
526 1.4 chopps }
527 1.1 jtc }
528 1.10 dante closedir(dirp);
529 1.1 jtc }
530 1.1 jtc
531 1.10 dante static int
532 1.10 dante splitentry(s, first, second)
533 1.10 dante char *s;
534 1.10 dante char *first;
535 1.10 dante char *second;
536 1.1 jtc {
537 1.10 dante char *c;
538 1.10 dante
539 1.10 dante for(c = s; *c != '\0' && !isspace(*c); ++c);
540 1.10 dante if(*c == '\0')
541 1.10 dante return(0);
542 1.10 dante strncpy(first, s, c-s);
543 1.10 dante first[c-s] = '\0';
544 1.10 dante for(; *c != '\0' && isspace(*c); ++c);
545 1.10 dante strcpy(second, c);
546 1.10 dante return(1);
547 1.10 dante }
548 1.10 dante
549 1.10 dante static void
550 1.10 dante setcatsuffix(catpage, suffix, crunchsuff)
551 1.10 dante char *catpage;
552 1.10 dante const char *suffix;
553 1.10 dante const char *crunchsuff;
554 1.10 dante {
555 1.10 dante TAG *tp;
556 1.10 dante ENTRY *ep;
557 1.10 dante char *p;
558 1.10 dante
559 1.10 dante for(p = catpage + strlen(catpage); p!=catpage; p--)
560 1.10 dante if(!fnmatch(suffix, p, 0)) {
561 1.10 dante if((tp = getlist("_suffix"))) {
562 1.10 dante ep = tp->list.tqh_first;
563 1.10 dante sprintf(p, "%s%s", ep->s, crunchsuff);
564 1.10 dante } else {
565 1.10 dante sprintf(p, ".0%s", crunchsuff);
566 1.10 dante }
567 1.10 dante break;
568 1.10 dante }
569 1.10 dante }
570 1.10 dante
571 1.10 dante static void
572 1.10 dante makecat(manpage, catpage, buildcmd, crunchcmd)
573 1.10 dante const char *manpage;
574 1.10 dante const char *catpage;
575 1.10 dante const char *buildcmd;
576 1.10 dante const char *crunchcmd;
577 1.10 dante {
578 1.10 dante char crunchbuf[1024];
579 1.10 dante char sysbuf[2048];
580 1.10 dante
581 1.10 dante snprintf(sysbuf, sizeof(sysbuf), buildcmd, manpage);
582 1.10 dante
583 1.10 dante if(*crunchcmd != '\0') {
584 1.10 dante snprintf(crunchbuf, sizeof(crunchbuf), crunchcmd, catpage);
585 1.10 dante snprintf(sysbuf, sizeof(sysbuf), "%s | %s", sysbuf, crunchbuf);
586 1.10 dante } else {
587 1.10 dante snprintf(sysbuf, sizeof(sysbuf), "%s > %s", sysbuf, catpage);
588 1.10 dante }
589 1.1 jtc
590 1.4 chopps if (f_noprint == 0)
591 1.1 jtc printf("%s\n", sysbuf);
592 1.4 chopps if (f_noaction == 0)
593 1.4 chopps dosystem(sysbuf);
594 1.1 jtc }
595 1.1 jtc
596 1.10 dante static void
597 1.10 dante makewhatis(void)
598 1.10 dante {
599 1.10 dante TAG *whatdbp;
600 1.10 dante ENTRY *e_whatdb;
601 1.10 dante char sysbuf[1024];
602 1.10 dante
603 1.10 dante whatdbp = getlist("_whatdb");
604 1.10 dante for(e_whatdb = whatdbp->list.tqh_first; e_whatdb;
605 1.10 dante e_whatdb = e_whatdb->q.tqe_next) {
606 1.10 dante snprintf(sysbuf, sizeof(sysbuf), "%s %s",
607 1.10 dante _PATH_WHATIS, dirname(e_whatdb->s));
608 1.10 dante if (f_noprint == 0)
609 1.10 dante printf("%s\n", sysbuf);
610 1.10 dante if (f_noaction == 0)
611 1.10 dante dosystem(sysbuf);
612 1.10 dante }
613 1.10 dante }
614 1.10 dante
615 1.10 dante static void
616 1.4 chopps dosystem(cmd)
617 1.4 chopps const char *cmd;
618 1.4 chopps {
619 1.4 chopps int status;
620 1.4 chopps
621 1.4 chopps if ((status = system(cmd)) == 0)
622 1.4 chopps return;
623 1.4 chopps
624 1.4 chopps if (status == -1)
625 1.4 chopps err(1, "cannot execute action");
626 1.4 chopps if (WIFSIGNALED(status))
627 1.4 chopps errx(1, "child was signaled to quit. aborting");
628 1.4 chopps if (WIFSTOPPED(status))
629 1.4 chopps errx(1, "child was stopped. aborting");
630 1.4 chopps if (f_ignerr == 0)
631 1.7 lukem errx(1, "*** Exited %d", status);
632 1.7 lukem warnx("*** Exited %d (continuing)", status);
633 1.4 chopps }
634 1.1 jtc
635 1.10 dante static void
636 1.10 dante usage(void)
637 1.1 jtc {
638 1.6 cgd (void)fprintf(stderr,
639 1.10 dante "usage: catman [-knpsw] [-m manpath] [sections]\n");
640 1.10 dante (void)fprintf(stderr,
641 1.10 dante " catman [-knpsw] [-M manpath] [sections]\n");
642 1.1 jtc exit(1);
643 1.1 jtc }
644