Initial import from FreeBSD RELENG_4:
[games.git] / gnu / usr.bin / man / manpath / manpath.c
1 /*
2  * manpath.c
3  *
4  * Copyright (c) 1990, 1991, John W. Eaton.
5  *
6  * You may distribute under the terms of the GNU General Public
7  * License as specified in the file COPYING that comes with the man
8  * distribution.
9  *
10  * John W. Eaton
11  * jwe@che.utexas.edu
12  * Department of Chemical Engineering
13  * The University of Texas at Austin
14  * Austin, Texas  78712
15  *
16  * $FreeBSD: src/gnu/usr.bin/man/manpath/manpath.c,v 1.11.2.2 2003/02/15 05:33:06 kris Exp $
17  */
18
19 #define MANPATH_MAIN
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include "config.h"
26 #include "manpath.h"
27 #include "gripes.h"
28
29 #ifdef STDC_HEADERS
30 #include <stdlib.h>
31 #else
32 extern int fprintf ();
33 extern int strcmp ();
34 extern int strncmp ();
35 extern char *memcpy ();
36 extern char *getenv();
37 extern char *malloc();
38 extern void free ();
39 extern int exit ();
40 #endif
41
42 extern char *strdup ();
43 extern int is_directory ();
44
45 #ifndef MAIN
46 extern int debug;
47 #endif
48
49 #ifdef MAIN
50
51 #ifndef STDC_HEADERS
52 extern char *strcpy ();
53 extern int fflush ();
54 #endif
55
56 char *prognam;
57 int debug;
58 int locale;
59 char *man_locales;
60
61 /*
62  * Examine user's PATH and print a reasonable MANPATH.
63  */
64 int
65 main(argc, argv)
66      int argc;
67      char **argv;
68 {
69   int c;
70   int quiet;
71   char *mp;
72   extern int getopt ();
73   extern char *mkprogname ();
74   void usage ();
75   char *manpath ();
76
77   quiet = 1;
78
79   prognam = mkprogname (argv[0]);
80
81   while ((c = getopt (argc, argv, "dhLq?")) != EOF)
82     {
83       switch (c)
84         {
85         case 'd':
86           debug++;
87           break;
88         case 'L':
89           locale++;
90           break;
91         case 'q':
92           quiet = 0;
93           break;
94         case '?':
95         case 'h':
96         default:
97           usage();
98           break;
99         }
100     }
101
102   mp = manpath (quiet);
103
104   fprintf (stdout, "%s\n", mp);
105   fflush (stdout);
106
107   return 0;
108 }
109
110 void
111 usage ()
112 {
113   fprintf (stderr, "usage: %s [-dLq]\n", prognam);
114   exit (1);
115 }
116 #endif /* MAIN */
117
118 /*
119  * If the environment variable MANPATH is set, return it.
120  * If the environment variable PATH is set and has a nonzero length,
121  * try to determine the corresponding manpath, otherwise, return the
122  * default manpath.
123  *
124  * The manpath.config file is used to map system wide /bin directories
125  * to top level man page directories.
126  *
127  * For directories which are in the user's path but not in the
128  * manpath.config file, see if there is a subdirectory `man' or `MAN'.
129  * If so, add that directory to the path.  Example:  user has
130  * $HOME/bin in his path and the directory $HOME/bin/man exists -- the
131  * directory $HOME/bin/man will be added to the manpath.
132  *
133  * Also search for a `man' directory next to the directory on the path.
134  * Example: $HOME/bin will look for $HOME/man
135  */
136 char *
137 manpath (perrs)
138      register int perrs;
139 {
140   register int len;
141   register char *manpathlist;
142   register char *path;
143   int  get_dirlist ();
144   char *def_path ();
145   char *get_manpath ();
146
147   if (get_dirlist ())
148       gripe_reading_mp_config (config_file);
149
150 #ifdef MAIN
151   if (locale)
152     {
153       if ((manpathlist = getenv ("MANLOCALES")) != NULL)
154         /*
155          * This must be it.
156          */
157         {
158           if (perrs)
159             fprintf (stderr, "(Warning: MANLOCALES environment variable set)\n");
160           return strdup (manpathlist);
161         }
162       return (man_locales ? man_locales : "");
163     }
164 #endif /* MAIN */
165
166   if ((manpathlist = getenv ("MANPATH")) != NULL)
167     /*
168      * This must be it.
169      */
170     {
171       if (perrs)
172         fprintf (stderr, "(Warning: MANPATH environment variable set)\n");
173       return strdup (manpathlist);
174     }
175   else if ((path = getenv ("PATH")) == NULL)
176     /*
177      * Things aren't going to work well, but hey...
178      */
179     {
180       if (perrs)
181         fprintf (stderr, "Warning: path not set\n");
182       return def_path (perrs);
183     }
184   else
185     {
186       if ((len = strlen (path)) == 0)
187         /*
188          * Things aren't going to work well here either...
189          */
190         {
191           if (perrs)
192             fprintf (stderr, "Warning: path set but has zero length\n");
193           return def_path (perrs);
194         }
195       return get_manpath (perrs, path);
196     }
197 }
198
199 /*
200  * Get the list of bin directories and the corresponding man
201  * directories from the manpath.config file.
202  *
203  * This is ugly.
204  */
205 int
206 get_dirlist ()
207 {
208   int i;
209   char *bp;
210   char *p;
211   char buf[BUFSIZ];
212   DIRLIST *dlp = list;
213   FILE *config;
214
215   if ((config = fopen (config_file, "r")) == NULL)
216     gripe_getting_mp_config (config_file);
217
218   while ((bp = fgets (buf, BUFSIZ, config)) != NULL)
219     {
220       while (*bp && (*bp == ' ' || *bp == '\t'))
221         bp++;
222
223       if (*bp == '#' || *bp == '\n')
224         continue;
225
226       if (!strncmp ("MANDATORY_MANPATH", bp, 17) ||
227           !strncmp ("OPTIONAL_MANPATH", bp, 16))
228         {
229           if ((p = strchr (bp, ' ')) == NULL &&
230               (p = strchr (bp, '\t')) == NULL) {
231             fclose(config);
232             return -1;
233           }
234
235           dlp->type = *bp == 'M'? MANPATH_MANDATORY: MANPATH_OPTIONAL;
236
237           bp = p;
238
239           while (*bp && *bp != '\n' && (*bp == ' ' || *bp == '\t'))
240             bp++;
241
242           i = 0;
243           while (*bp && *bp != '\n' && *bp != ' ' && *bp != '\t')
244             dlp->mandir[i++] = *bp++;
245           dlp->mandir[i] = '\0';
246
247           if (debug)
248             fprintf (stderr, "found %s man directory %s\n",
249                      dlp->type == MANPATH_MANDATORY? "mandatory": "optional",
250                      dlp->mandir);
251         }
252       else if (!strncmp ("MANPATH_MAP", bp, 11))
253         {
254           if ((p = strchr (bp, ' ')) == NULL &&
255               (p = strchr (bp, '\t')) == NULL) {
256             fclose(config);
257             return -1;
258           }
259
260           bp = p;
261
262           dlp->type = MANPATH_MAP;
263
264           while (*bp && *bp != '\n' && (*bp == ' ' || *bp == '\t'))
265             bp++;
266
267           i = 0;
268           while (*bp && *bp != '\n' && *bp != ' ' && *bp != '\t')
269             dlp->bin[i++] = *bp++;
270           dlp->bin[i] = '\0';
271
272           while (*bp && *bp != '\n' && (*bp == ' ' || *bp == '\t'))
273             bp++;
274
275           i = 0;
276           while (*bp && *bp != '\n' && *bp != ' ' && *bp != '\t')
277             dlp->mandir[i++] = *bp++;
278           dlp->mandir[i] = '\0';
279
280           if (debug)
281             fprintf (stderr, "found manpath map %s --> %s\n",
282                      dlp->bin, dlp->mandir);
283         }
284       else if (!strncmp ("MANLOCALES", bp, 10))
285         {
286           if ((p = strchr (bp, ' ')) == NULL &&
287               (p = strchr (bp, '\t')) == NULL) {
288             fclose(config);
289             return -1;
290           }
291
292           bp = p;
293
294           while (*bp && *bp != '\n' && (*bp == ' ' || *bp == '\t'))
295             bp++;
296
297           for (p = bp; *p && *p != '\n'; p++)
298                 ;
299           do {
300                 *p-- = '\0';
301           } while (p >= bp && (*p == ' ' || *p == '\t'));
302
303 #ifdef MAIN
304           if (man_locales != NULL)
305                 free (man_locales);
306
307           if ((man_locales = strdup (bp)) == NULL) {
308                 fclose(config);
309                 return -1;
310           }
311 #endif  /* MAIN */
312
313           if (debug)
314             fprintf (stderr, "found man locales: %s\n", bp);
315         }
316       else
317         {
318           gripe_reading_mp_config (config_file);
319         }
320       dlp++;
321     }
322
323   fclose(config);
324   dlp->bin[0] = '\0';
325   dlp->mandir[0] = '\0';
326   dlp->type = MANPATH_NONE;
327
328   return 0;
329 }
330
331 /*
332  * Construct the default manpath.  This picks up mandatory
333  * and optional (if they exist) manpaths only.
334  */
335 char *
336 def_path (perrs)
337      int perrs;
338 {
339   register int len;
340   register char *manpathlist, *p;
341   register DIRLIST *dlp;
342
343   len = 0;
344   dlp = list;
345   while (dlp->type != MANPATH_NONE) {
346     if (dlp->type == MANPATH_MANDATORY || dlp->type == MANPATH_OPTIONAL)
347       len += strlen (dlp->mandir) + 1;
348     dlp++;
349   }
350
351   manpathlist = (char *) malloc (len);
352   if (manpathlist == NULL)
353     gripe_alloc (len, "manpathlist");
354
355   *manpathlist = '\0';
356
357   dlp = list;
358   p = manpathlist;
359   while (dlp->type != MANPATH_NONE) {
360     if (dlp->type == MANPATH_MANDATORY || dlp->type == MANPATH_OPTIONAL) {
361       int status;
362       char *path = dlp->mandir;
363
364       status = is_directory(path);
365
366       if (status < 0 && perrs && dlp->type == MANPATH_MANDATORY)
367         {
368           fprintf (stderr, "Warning: couldn't stat file %s!\n", path);
369         }
370       else if (status == 0 && perrs)
371         {
372           fprintf (stderr, "Warning: %s isn't a directory!\n", path);
373         }
374       else if (status == 1)
375         {
376           len = strlen (path);
377           memcpy (p, path, len);
378           p += len;
379           *p++ = ':';
380         }
381     }
382     dlp++;
383   }
384
385   p[-1] = '\0';
386
387   return manpathlist;
388 }
389
390 /*
391  * For each directory in the user's path, see if it is one of the
392  * directories listed in the manpath.config file.  If so, and it is
393  * not already in the manpath, add it.  If the directory is not listed
394  * in the manpath.config file, see if there is a subdirectory `man' or
395  * `MAN'.  If so, and it is not already in the manpath, add it.
396  * Example:  user has $HOME/bin in his path and the directory
397  * $HOME/bin/man exists -- the directory $HOME/bin/man will be added
398  * to the manpath.
399  */
400 char *
401 get_manpath (perrs, path)
402      register int perrs;
403      register char *path;
404 {
405   register int len;
406   register char *tmppath;
407   register char *t;
408   register char *p;
409   register char **lp;
410   register char *end;
411   register char *manpathlist;
412   register DIRLIST *dlp;
413   void add_dir_to_list ();
414   char *has_subdirs ();
415
416   tmppath = strdup (path);
417
418   for (p = tmppath; ; p = end+1)
419     {
420       if ((end = strchr(p, ':')) != NULL)
421         *end = '\0';
422
423       if (debug)
424         fprintf (stderr, "\npath directory %s ", p);
425
426       if (*p != '/')
427         {
428           if (debug)
429             fprintf (stderr, "is not an absolute pathname\n");
430
431           goto found;   /* skip. */
432         }
433
434       /*
435        * The directory we're working on is in the config file.
436        * If we haven't added it to the list yet, do.
437        */
438       for (dlp = list; dlp->mandir[0] != '\0'; dlp++)
439         if (dlp->bin[0] != '\0' && !strcmp (p, dlp->bin))
440           {
441             if (debug)
442               fprintf (stderr, "is in the config file\n");
443
444             add_dir_to_list (tmplist, dlp->mandir, perrs);
445             goto found;
446           }
447
448       /*
449        * The directory we're working on isn't in the config file.  See
450        * if it has man or MAN subdirectories.  If so, and it hasn't
451        * been added to the list, do.
452        */
453       if (debug)
454         fprintf (stderr, "is not in the config file\n");
455
456       t = has_subdirs (p);
457       if (t != NULL)
458         {
459           if (debug)
460             fprintf (stderr, "but it does have a man or MAN subdirectory\n");
461
462           add_dir_to_list (tmplist, t, perrs);
463           free (t);
464         }
465       else
466         {
467           if (debug)
468             fprintf (stderr, "and doesn't have man or MAN subdirectories\n");
469         }
470
471     found:
472
473       if (!end)
474         break;
475     }
476
477   if (debug)
478     fprintf (stderr, "\nadding mandatory man directories\n\n");
479
480   dlp = list;
481   while (dlp->type != MANPATH_NONE) {
482     if (dlp->type == MANPATH_MANDATORY || dlp->type == MANPATH_OPTIONAL)
483       add_dir_to_list (tmplist, dlp->mandir,
484         dlp->type == MANPATH_MANDATORY? perrs: 0);
485     dlp++;
486   }
487
488   len = 0;
489   lp = tmplist;
490   while (*lp != NULL)
491     {
492       len += strlen (*lp) + 1;
493       lp++;
494     }
495
496   if (!len)
497     return strdup("");
498
499   manpathlist = (char *) malloc (len);
500   if (manpathlist == NULL)
501     gripe_alloc (len, "manpathlist");
502
503   *manpathlist = '\0';
504
505   lp = tmplist;
506   p = manpathlist;
507   while (*lp != NULL)
508     {
509       len = strlen (*lp);
510       memcpy (p, *lp, len);
511       p += len;
512       *p++ = ':';
513       lp++;
514     }
515
516   p[-1] = '\0';
517
518   return manpathlist;
519 }
520
521 /*
522  * Add a directory to the manpath list if it isn't already there.
523  */
524 void
525 add_dir_to_list (lp, dir, perrs)
526      char **lp;
527      char *dir;
528      int perrs;
529 {
530   extern char *strdup ();
531   int status;
532
533   while (*lp != NULL)
534     {
535       if (!strcmp (*lp, dir))
536         {
537           if (debug)
538             fprintf (stderr, "%s is already in the manpath\n", dir);
539           return;
540         }
541       lp++;
542     }
543   /*
544    * Not found -- add it.
545    */
546   status = is_directory(dir);
547
548   if (status < 0 && perrs)
549     {
550       fprintf (stderr, "Warning: couldn't stat file %s!\n", dir);
551     }
552   else if (status == 0 && perrs)
553     {
554       fprintf (stderr, "Warning: %s isn't a directory!\n", dir);
555     }
556   else if (status == 1)
557     {
558       if (debug)
559         fprintf (stderr, "adding %s to manpath\n", dir);
560
561       *lp = strdup (dir);
562     }
563 }
564
565 /*
566  * Check to see if the current directory has man or MAN
567  * subdirectories.
568  */
569 char *
570 has_subdirs (p)
571      register char *p;
572 {
573   int len;
574   register char *t;
575
576   len = strlen (p);
577
578   t = (char *) malloc ((unsigned) len + 5);
579   if (t == NULL)
580     gripe_alloc (len+5, "p\n");
581
582   memcpy (t, p, len);
583   strcpy (t + len, "/man");
584
585   if (is_directory (t) == 1)
586     return t;
587
588   strcpy (t + len, "/MAN");
589
590   if (is_directory (t) == 1)
591     return t;
592
593   /* If the path ends in `bin' then replace with `man' and see if that works. */
594   if (len > 3 && strncmp(t+len-4, "/bin", 4) == 0) {
595     strcpy(t+len-4, "/man");
596
597     if (is_directory(t) == 1) 
598        return t;
599   }
600
601   return NULL;
602 }