4 * Copyright (c) 1990, 1991, John W. Eaton.
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
12 * Department of Chemical Engineering
13 * The University of Texas at Austin
18 static const char rcsid[] =
26 #include <sys/param.h>
27 #include <sys/utsname.h>
56 extern char *malloc ();
57 extern char *getenv ();
61 extern int strncmp ();
65 extern int fprintf ();
66 extern FILE *fopen ();
68 extern char *sprintf ();
71 extern char **glob_filename ();
72 extern int is_newer ();
73 extern int is_directory ();
74 extern int is_file ();
75 extern int do_system_command ();
79 static char *machine_arch;
82 static char *manpathlist[MAXDIRS];
83 static char *shortsec;
85 static char *colon_sep_section_list;
86 static char **section_list;
87 static char *roff_directive;
91 static int print_where;
92 static char *ultimate_source ();
95 static char *locale, *locale_opts, *locale_nroff, *locale_codeset;
96 static char locale_terr[3], locale_lang[3];
97 static char *man_locale;
98 static int use_man_locale;
99 static int use_original;
104 static struct ltable ltable[] = {
105 {"KOI8-R", "koi8-r"},
106 {"ISO8859-1", "latin1"},
107 {"ISO8859-15", "latin1"},
112 static int troff = 0;
118 static char args[] = "M:P:S:adfhkm:op:tw?";
120 static char args[] = "M:P:S:adfhkm:p:tw?";
124 static char args[] = "M:P:S:adfhkm:op:w?";
126 static char args[] = "M:P:S:adfhkm:p:w?";
143 extern char *mkprogname ();
145 char **get_section_list ();
151 prognam = mkprogname (argv[0]);
156 (void) setlocale(LC_ALL, "");
158 man_getopt (argc, argv);
161 gripe_no_name ((char *)NULL);
163 section_list = get_section_list ();
165 if (optind == argc - 1)
167 tmp = is_section (argv[optind], manp);
179 while (optind < argc)
181 nextarg = argv[optind++];
184 * See if this argument is a valid section name. If not,
185 * is_section returns NULL.
187 tmp = is_section (nextarg, manp);
194 fprintf (stderr, "\nsection: %s\n", shortsec);
200 do_apropos (nextarg);
201 status = (status ? 0 : 1); /* reverts status, see below */
205 status = (status ? 0 : 1); /* reverts status, see below */
207 else if (strchr (nextarg, '/') != NULL && is_file (nextarg) == 1)
209 format_and_display (NULL, ultimate_source(nextarg, dirname(nextarg)),
214 status = man (nextarg);
217 gripe_not_found (nextarg, longsec);
220 return (status==0); /* status==1 --> exit(0),
221 status==0 --> exit(1) */
227 static char usage_string[1024] = "%s, version %s\n\n";
232 "usage: %s [-adfhkotw] [section] [-M path] [-P pager] [-S list]\n\
233 [-m machine] [-p string] name ...\n\n";
236 "usage: %s [-adfhktw] [section] [-M path] [-P pager] [-S list]\n\
237 [-m machine] [-p string] name ...\n\n";
242 "usage: %s [-adfhkow] [section] [-M path] [-P pager] [-S list]\n\
243 [-m machine] [-p string] name ...\n\n";
246 "usage: %s [-adfhkw] [section] [-M path] [-P pager] [-S list]\n\
247 [-m machine] [-p string] name ...\n\n";
251 static char s2[] = " a : find all matching entries\n\
252 d : print gobs of debugging information\n\
253 f : same as whatis(1)\n\
254 h : print this help message\n\
255 k : same as apropos(1)\n";
258 static char s3[] = " o : use original, non-localized manpages\n";
262 static char s4[] = " t : use troff to format pages for printing\n";
265 static char s5[] = " w : print location of man page(s) that would be displayed\n\n\
266 M path : set search path for manual pages to `path'\n\
267 P pager : use program `pager' to display pages\n\
268 S list : colon separated section list\n\
269 m machine : search for alternate architecture man pages\n";
271 static char s6[] = " p string : string tells which preprocessors to run\n\
272 e - [n]eqn(1) p - pic(1) t - tbl(1)\n\
273 g - grap(1) r - refer(1) v - vgrind(1)\n";
275 strcat (usage_string, s1);
276 strcat (usage_string, s2);
278 strcat (usage_string, s3);
282 strcat (usage_string, s4);
285 strcat (usage_string, s5);
287 strcat (usage_string, s6);
289 fprintf (stderr, usage_string, prognam, version, prognam);
294 add_dir_to_mpath_list (mp, p)
300 status = is_directory (p);
302 if (status < 0 && debug)
304 fprintf (stderr, "Warning: couldn't stat file %s!\n", p);
306 else if (status == 0 && debug)
308 fprintf (stderr, "Warning: %s isn't a directory!\n", p);
310 else if (status == 1)
313 fprintf (stderr, "adding %s to manpathlist\n", p);
321 * Get options from the command line and user environment.
324 man_getopt (argc, argv)
326 register char **argv;
332 extern void downcase ();
333 extern char *manpath ();
335 while ((c = getopt (argc, argv, args)) != EOF)
340 manp = strdup (optarg);
343 pager = strdup (optarg);
344 if (setenv("PAGER", pager, 1) != 0)
345 (void)fprintf(stderr, "setenv PAGER=%s\n", pager);
348 colon_sep_section_list = strdup (optarg);
358 gripe_incompatible ("-f and -t");
360 gripe_incompatible ("-f and -k");
362 gripe_incompatible ("-f and -w");
367 gripe_incompatible ("-k and -t");
369 gripe_incompatible ("-k and -f");
371 gripe_incompatible ("-k and -w");
375 machine_arch = optarg;
376 if ((machine = strchr(optarg, ':')) != NULL)
387 roff_directive = strdup (optarg);
392 gripe_incompatible ("-t and -k");
394 gripe_incompatible ("-t and -f");
396 gripe_incompatible ("-t and -w");
402 gripe_incompatible ("-w and -k");
404 gripe_incompatible ("-w and -f");
406 gripe_incompatible ("-w and -t");
418 /* "" intentionally used to catch error */
419 if ((locale = setlocale(LC_CTYPE, "")) != NULL)
420 locale_codeset = nl_langinfo(CODESET);
421 if (!use_original && locale != NULL && *locale_codeset != '\0' &&
422 strcmp(locale_codeset, "US-ASCII") != 0
424 char *tmp, *short_locale;
425 struct ltable *pltable;
430 if ((short_locale = strdup(locale)) == NULL) {
431 perror ("ctype locale strdup");
434 if ((tmp = strchr(short_locale, '.')) != NULL)
437 if (strlen(short_locale) == 2)
438 strcpy(locale_lang, short_locale);
439 else if ((tmp = strchr(short_locale, '_')) == NULL ||
440 tmp != short_locale + 2 ||
444 perror ("ctype locale format");
447 strncpy(locale_terr, short_locale + 3, 2);
448 locale_terr[2] = '\0';
449 strncpy(locale_lang, short_locale, 2);
450 locale_lang[2] = '\0';
455 if (locale != NULL) {
456 for (pltable = ltable; pltable->lcode != NULL; pltable++) {
457 if (strcmp(pltable->lcode, locale_codeset) == 0) {
458 locale_nroff = pltable->nroff;
462 asprintf(&man_locale, "%s.%s", locale_lang, locale_codeset);
465 if (locale == NULL) {
467 perror ("ctype locale");
470 if (*locale_codeset == '\0') {
472 perror ("ctype codeset");
476 #endif /* __FreeBSD__ */
478 if (pager == NULL || *pager == '\0')
479 if ((pager = getenv ("PAGER")) == NULL || *pager == '\0')
480 pager = strdup (PAGER);
483 fprintf (stderr, "\nusing %s as pager\n", pager);
485 if (machine_arch == NULL && (machine_arch = getenv ("MACHINE_ARCH")) == NULL)
486 machine_arch = MACHINE_ARCH;
488 if (machine == NULL && (machine = getenv ("MACHINE")) == NULL)
490 static struct utsname utsname;
492 if (uname(&utsname) == -1)
497 machine = utsname.machine;
501 fprintf (stderr, "\nusing %s:%s architecture\n", machine_arch, machine);
505 if ((manp = manpath (0)) == NULL)
510 "\nsearch path for pages determined by manpath is\n%s\n\n",
515 * Expand the manpath into a list for easier handling.
518 for (p = manp; ; p = end+1)
520 if (mp == manpathlist + MAXDIRS - 1) {
521 fprintf (stderr, "Warning: too many directories in manpath, truncated!\n");
524 if ((end = strchr (p, ':')) != NULL)
527 mp = add_dir_to_mpath_list (mp, p);
537 * Check to see if the argument is a valid section number. If the
538 * first character of name is a numeral, or the name matches one of
539 * the sections listed in section_list, we'll assume that it's a section.
540 * The list of sections in config.h simply allows us to specify oddly
541 * named directories like .../man3f. Yuk.
544 is_section (name, path)
549 char *temp, *end, *loc;
553 for (vs = section_list; *vs != NULL; vs++)
554 if ((strcmp (*vs, name) == 0)
555 || (isdigit ((unsigned char)name[0]) && strlen(name) == 1))
556 return (longsec = strdup(name));
559 if (isdigit ((unsigned char)name[0]))
561 while (*plist != NULL)
563 asprintf (&temp, "%s/man%c/*", *plist, name[0]);
567 vs = glob_filename (temp);
568 if (vs == (char **) -1)
573 for ( ; *vs != NULL; vs++)
575 end = strrchr (*vs, '/');
576 if ((loc = strstr (end, name)) != NULL && loc - end > 2
578 && (*(loc+strlen(name)) == '\0' || *(loc+strlen(name)) == '.'))
587 asprintf (&temp, "%c", name[0]);
588 longsec = strdup (name);
597 * Handle the apropos option. Cheat by using another program.
604 register char *command;
606 len = strlen (APROPOS) + strlen (name) + 4;
608 if ((command = (char *) malloc(len)) == NULL)
609 gripe_alloc (len, "command");
611 sprintf (command, "%s \"%s\"", APROPOS, name);
613 (void) do_system_command (command);
619 * Handle the whatis option. Cheat by using another program.
626 register char *command;
628 len = strlen (WHATIS) + strlen (name) + 4;
630 if ((command = (char *) malloc(len)) == NULL)
631 gripe_alloc (len, "command");
633 sprintf (command, "%s \"%s\"", WHATIS, name);
635 (void) do_system_command (command);
641 * Change a name of the form ...man/man1/name.1 to ...man/cat1/name.1
642 * or a name of the form ...man/cat1/name.1 to ...man/man1/name.1
645 convert_name (name, to_cat)
649 register char *to_name;
651 register char *t2 = NULL;
656 int olen = strlen(name);
657 int cextlen = strlen(COMPRESS_EXT);
658 int len = olen + cextlen;
660 to_name = malloc (len+1);
662 gripe_alloc (len+1, "to_name");
663 strcpy (to_name, name);
665 /* Avoid tacking it on twice */
666 if (olen >= 1 && strcmp(name + olen, COMPRESS_EXT) != 0)
667 strcat (to_name, COMPRESS_EXT);
670 to_name = strdup (name);
672 to_name = strdup (name);
675 t1 = strrchr (to_name, '/');
679 t2 = strrchr (to_name, '/');
682 /* Skip architecture part (if present). */
683 if (t2 != NULL && (t1 - t2 < 5 || *(t2 + 1) != 'm' || *(t2 + 3) != 'n'))
687 t2 = strrchr (to_name, '/');
693 gripe_converting_name (name, to_cat);
707 fprintf (stderr, "to_name in convert_name () is: %s\n", to_name);
713 * Try to find the man page corresponding to the given name. The
714 * reason we do this with globbing is because some systems have man
715 * page directories named man3 which contain files with names like
716 * XtPopup.3Xt. Rather than requiring that this program know about
717 * all those possible names, we simply try to match things like
718 * .../man[sect]/name[sect]*. This is *much* easier.
720 * Note that globbing is only done when the section is unspecified.
723 glob_for_file (path, section, longsec, name, cat)
730 char pathname[FILENAME_MAX];
737 snprintf (pathname, sizeof(pathname), "%s/cat%s/%s.%s*", path, section,
740 snprintf (pathname, sizeof(pathname), "%s/man%s/%s.%s*", path, section,
744 fprintf (stderr, "globbing %s\n", pathname);
746 gf = glob_filename (pathname);
748 if ((gf == (char **) -1 || *gf == NULL) && isdigit ((unsigned char)*section)
749 && strlen (longsec) == 1)
752 snprintf (pathname, sizeof(pathname), "%s/cat%s/%s.%c*", path, section, name, *section);
754 snprintf (pathname, sizeof(pathname), "%s/man%s/%s.%c*", path, section, name, *section);
756 gf = glob_filename (pathname);
758 if ((gf == (char **) -1 || *gf == NULL) && isdigit ((unsigned char)*section)
759 && strlen (longsec) == 1)
762 snprintf (pathname, sizeof(pathname), "%s/cat%s/%s.0*", path, section, name);
764 snprintf (pathname, sizeof(pathname), "%s/man%s/%s.0*", path, section, name);
766 fprintf (stderr, "globbing %s\n", pathname);
767 gf = glob_filename (pathname);
773 * Return an un-globbed name in the same form as if we were doing
777 make_name (path, section, longsec, name, cat)
785 static char *names[3];
786 char buf[FILENAME_MAX];
789 snprintf (buf, sizeof(buf), "%s/cat%s/%s.%s", path, section, name, longsec);
791 snprintf (buf, sizeof(buf), "%s/man%s/%s.%s", path, section, name, longsec);
793 if (access (buf, R_OK) == 0)
794 names[i++] = strdup (buf);
797 * If we're given a section that looks like `3f', we may want to try
798 * file names like .../man3/foo.3f as well. This seems a bit
799 * kludgey to me, but what the hey...
801 if (section[1] != '\0')
804 snprintf (buf, sizeof(buf), "%s/cat%c/%s.%s", path, section[0], name, section);
806 snprintf (buf, sizeof(buf), "%s/man%c/%s.%s", path, section[0], name, section);
808 if (access (buf, R_OK) == 0)
809 names[i++] = strdup (buf);
821 char *end = file + (strlen (file) - 1);
823 while (end > file && end[-1] != '.')
836 if (*end == 'Z' || !strcmp(end, "gz") || !strcmp(end, "bz2"))
843 * Simply display the preformatted page.
846 display_cat_file (file)
850 char command[FILENAME_MAX];
854 if (access (file, R_OK) == 0)
856 char *expander = get_expander (file);
858 if (expander != NULL)
859 snprintf (command, sizeof(command), "%s %s | %s", expander, file, pager);
861 snprintf (command, sizeof(command), "%s %s", pager, file);
863 found = do_system_command (command);
869 * Try to find the ultimate source file. If the first line of the
870 * current file is not of the form
874 * the input file name is returned.
877 ultimate_source (name, path)
881 static char buf[BUFSIZ];
882 static char ult[FILENAME_MAX];
888 strncpy (ult, name, sizeof(ult)-1);
889 ult[sizeof(ult)-1] = '\0';
890 strncpy (buf, name, sizeof(buf)-1);
891 ult[sizeof(buf)-1] = '\0';
896 if ((fp = gzopen (ult, "r")) == NULL)
898 /* check for the compressed version too */
899 strlcat(ult, ".gz", FILENAME_MAX);
900 if ((fp = gzopen (ult, "r")) == NULL)
901 return ult; /* we munged it, but it doesn't exist anyway */
904 if ((fp = fopen (ult, "r")) == NULL)
909 end = gzgets (fp, buf, BUFSIZ);
912 end = fgets (buf, BUFSIZ, fp);
916 if (!end || strlen (buf) < 5)
920 if (*beg++ == '.' && *beg++ == 's' && *beg++ == 'o')
922 while ((*beg == ' ' || *beg == '\t') && *beg != '\0')
926 while (*end != ' ' && *end != '\t' && *end != '\n' && *end != '\0')
931 snprintf(ult, sizeof(ult), "%s/%s", path, beg);
932 snprintf(buf, sizeof(buf), "%s", ult);
938 fprintf (stderr, "found ultimate source file %s\n", ult);
944 add_directive (first, d, file, buf, bufsize)
951 if (strcmp (d, "") != 0)
956 snprintf(buf, bufsize, "%s %s", d, file);
960 strncat (buf, " | ", bufsize-strlen(buf)-1);
961 strncat (buf, d, bufsize-strlen(buf)-1);
967 parse_roff_directive (cp, file, buf, bufsize)
976 int preproc_found = 0;
979 if ((exp = get_expander(file)) != NULL)
980 add_directive (&first, exp, file, buf, bufsize);
982 while ((c = *cp++) != '\0')
989 fprintf (stderr, "found eqn(1) directive\n");
993 add_directive (&first, EQN, file, buf, bufsize);
996 char lbuf[FILENAME_MAX];
998 snprintf(lbuf, sizeof(lbuf), "%s -T%s", NEQN,
999 locale_opts == NULL ? "ascii" : locale_opts);
1000 add_directive (&first, lbuf, file, buf, bufsize);
1002 add_directive (&first, NEQN, file, buf, bufsize);
1011 fprintf (stderr, "found grap(1) directive\n");
1014 add_directive (&first, GRAP, file, buf, bufsize);
1021 fprintf (stderr, "found pic(1) directive\n");
1024 add_directive (&first, PIC, file, buf, bufsize);
1031 fprintf (stderr, "found tbl(1) directive\n");
1035 add_directive (&first, TBL, file, buf, bufsize);
1041 fprintf (stderr, "found vgrind(1) directive\n");
1043 add_directive (&first, VGRIND, file, buf, bufsize);
1049 fprintf (stderr, "found refer(1) directive\n");
1051 add_directive (&first, REFER, file, buf, bufsize);
1070 add_directive (&first, TROFF, file, buf, bufsize);
1075 char lbuf[FILENAME_MAX];
1077 snprintf(lbuf, sizeof(lbuf), "%s -T%s%s%s", NROFF,
1078 locale_opts == NULL ? "ascii" : locale_opts,
1079 use_man_locale ? " -dlocale=" : "",
1080 use_man_locale ? man_locale : "");
1081 add_directive (&first, lbuf, file, buf, bufsize);
1083 add_directive (&first, NROFF " -Tascii", file, buf, bufsize);
1086 if (use_col && !troff)
1087 add_directive (&first, COL, file, buf, bufsize);
1096 make_roff_command (file)
1105 static char buf [BUFSIZ];
1109 if (roff_directive != NULL)
1112 fprintf (stderr, "parsing directive from command line\n");
1114 status = parse_roff_directive (roff_directive, file, buf, sizeof(buf));
1120 gripe_roff_command_from_command_line (file);
1124 if ((fp = gzopen (file, "r")) != NULL)
1126 if ((fp = fopen (file, "r")) != NULL)
1131 gzgets (fp, line, BUFSIZ);
1134 fgets (line, BUFSIZ, fp);
1137 if (*cp++ == '\'' && *cp++ == '\\' && *cp++ == '"' && *cp++ == ' ')
1140 fprintf (stderr, "parsing directive from file\n");
1142 status = parse_roff_directive (cp, file, buf, sizeof(buf));
1148 gripe_roff_command_from_file (file);
1154 * Is there really any point in continuing to look for
1155 * preprocessor options if we can't even read the man page source?
1157 gripe_reading_man_file (file);
1161 if ((cp = getenv ("MANROFFSEQ")) != NULL)
1164 fprintf (stderr, "parsing directive from environment\n");
1166 status = parse_roff_directive (cp, file, buf, sizeof(buf));
1172 gripe_roff_command_from_env ();
1176 fprintf (stderr, "using default preprocessor sequence\n");
1178 status = parse_roff_directive ("t", file, buf, sizeof(buf));
1181 else /* can't happen */
1185 sig_t ohup, oint, oquit, oterm;
1186 static char temp[FILENAME_MAX];
1197 ohup = signal(SIGHUP, cleantmp);
1198 oint = signal(SIGINT, cleantmp);
1199 oquit = signal(SIGQUIT, cleantmp);
1200 oterm = signal(SIGTERM, cleantmp);
1206 signal(SIGHUP, ohup);
1207 signal(SIGINT, oint);
1208 signal(SIGQUIT, oquit);
1209 signal(SIGTERM, oterm);
1213 * Try to format the man page and create a new formatted file. Return
1214 * 1 for success and 0 for failure.
1217 make_cat_file (path, man_file, cat_file, manid)
1218 register char *path;
1219 register char *man_file;
1220 register char *cat_file;
1224 #if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
1230 char command[FILENAME_MAX];
1232 roff_command = make_roff_command (man_file);
1233 if (roff_command == NULL)
1236 snprintf(temp, sizeof(temp), "%s.tmpXXXXXX", cat_file);
1237 if ((f = mkstemp(temp)) >= 0 &&
1238 #if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
1239 (fp = gzdopen(f, "w")) != NULL)
1241 (fp = fdopen(f, "w")) != NULL)
1246 if (fchmod (f, CATMODE) < 0) {
1253 fprintf (stderr, "mode of %s is now %o\n", temp, CATMODE);
1255 snprintf (command, sizeof(command), "(cd %s ; %s)", path,
1258 fprintf (stderr, "Formatting page, please wait...");
1262 fprintf (stderr, "\ntrying command: %s\n", command);
1269 if ((pp = popen(command, "r")) == NULL) {
1271 fprintf(stderr, "Failed.\n");
1280 #if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
1293 while ((s = getc(pp)) != EOF) {
1294 #if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
1302 if (!f || ((s = pclose(pp)) == -1)) {
1304 fprintf(stderr, "Failed.\n");
1310 #if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
1319 fprintf(stderr, "Failed.\n");
1320 gripe_system_command(s);
1323 #if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
1334 else if (rename(temp, cat_file) == -1) {
1337 "\nHmm! Can't seem to rename %s to %s, check permissions on man dir!\n",
1343 #if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
1352 #if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
1360 fprintf(stderr, "Failed.\n");
1367 fprintf(stderr, "No output, debug mode.\n");
1371 fprintf(stderr, "Done.\n");
1384 fprintf (stderr, "Couldn't open %s for writing.\n", temp);
1397 * Try to format the man page source and save it, then display it. If
1398 * that's not possible, try to format the man page source and display
1401 * Note that we've already been handed the name of the ultimate source
1402 * file at this point.
1405 format_and_display (path, man_file, cat_file)
1406 register char *path;
1407 register char *man_file;
1408 register char *cat_file;
1413 char command[FILENAME_MAX];
1417 if (access (man_file, R_OK) != 0)
1420 if (troff || path == NULL)
1422 roff_command = make_roff_command (man_file);
1423 if (roff_command == NULL)
1426 snprintf (command, sizeof(command), "(cd %s ; %s)", path, roff_command);
1428 snprintf (command, sizeof(command), "%s | %s", roff_command, pager);
1430 found = do_system_command (command);
1434 status = is_newer (man_file, cat_file);
1436 fprintf (stderr, "status from is_newer() = %d\n", status);
1438 if (status == 1 || status == -2)
1441 * Cat file is out of date. Try to format and save it.
1445 printf ("%s\n", man_file);
1453 found = make_cat_file (path, man_file, cat_file, 1);
1458 /* Try again as real user - see note below.
1460 effective group (user) ID == real group (user) ID
1461 except for the call above, I believe the problems
1462 of reading private man pages is avoided. */
1463 found = make_cat_file (path, man_file, cat_file, 0);
1466 found = make_cat_file (path, man_file, cat_file, 0);
1471 * Creating the cat file worked. Now just display it.
1473 (void) display_cat_file (cat_file);
1478 * Couldn't create cat file. Just format it and
1479 * display it through the pager.
1481 roff_command = make_roff_command (man_file);
1482 if (roff_command == NULL)
1485 snprintf (command, sizeof(command), "(cd %s ; %s | %s)", path,
1486 roff_command, pager);
1488 found = do_system_command (command);
1492 else if (access (cat_file, R_OK) == 0)
1495 * Formatting not necessary. Cat file is newer than source
1496 * file, or source file is not present but cat file is.
1500 printf ("%s (source: %s)\n", cat_file, man_file);
1505 found = display_cat_file (cat_file);
1513 * See if the preformatted man page or the source exists in the given
1517 try_section (path, section, longsec, name, glob)
1524 register int found = 0;
1525 register int to_cat;
1527 register char **names;
1529 static int arch_search;
1530 char buf[FILENAME_MAX];
1534 snprintf(buf, sizeof(buf), "%s/man%s/%s", path, section, machine);
1535 if (is_directory (buf) == 1)
1537 snprintf(buf, sizeof(buf), "%s/%s", machine, name);
1539 found = try_section (path, section, longsec, buf, glob);
1541 if (found && !findall) /* only do this architecture... */
1544 if (strcmp(machine_arch, machine) != 0)
1546 snprintf(buf, sizeof(buf), "%s/man%s/%s", path, section, machine_arch);
1547 if (is_directory (buf) == 1)
1549 snprintf(buf, sizeof(buf), "%s/%s", machine_arch, name);
1551 found = try_section (path, section, longsec, buf, glob);
1553 if (found && !findall) /* only do this architecture... */
1562 fprintf (stderr, "trying section %s with globbing\n", section);
1564 fprintf (stderr, "trying section %s without globbing\n", section);
1567 #ifndef NROFF_MISSING
1569 * Look for man page source files.
1573 names = glob_for_file (path, section, longsec, name, cat);
1575 names = make_name (path, section, longsec, name, cat);
1577 if (names == (char **) -1 || *names == NULL)
1579 * No files match. See if there's a preformatted page around that
1582 #endif /* NROFF_MISSING */
1588 names = glob_for_file (path, section, longsec, name, cat);
1590 names = make_name (path, section, longsec, name, cat);
1592 if (names != (char **) -1 && *names != NULL)
1594 for (np = names; *np != NULL; np++)
1598 printf ("%s\n", *np);
1603 found += display_cat_file (*np);
1609 #ifndef NROFF_MISSING
1612 for (np = names; *np != NULL; np++)
1614 register char *cat_file = NULL;
1615 register char *man_file;
1617 man_file = ultimate_source (*np, path);
1623 cat_file = convert_name (man_file, to_cat);
1626 fprintf (stderr, "will try to write %s if needed\n", cat_file);
1629 found += format_and_display (path, man_file, cat_file);
1632 #endif /* NROFF_MISSING */
1637 * Search for manual pages.
1639 * If preformatted manual pages are supported, look for the formatted
1640 * file first, then the man page source file. If they both exist and
1641 * the man page source file is newer, or only the source file exists,
1642 * try to reformat it and write the results in the cat directory. If
1643 * it is not possible to write the cat file, simply format and display
1646 * If preformatted pages are not supported, or the troff option is
1647 * being used, only look for the man page source file.
1660 char buf[FILENAME_MAX];
1666 if (shortsec != NULL)
1668 for (mp = manpathlist; *mp != NULL; mp++)
1671 fprintf (stderr, "\nsearching in %s\n", *mp);
1677 if (locale != NULL) {
1678 locale_opts = locale_nroff;
1680 if (*locale_lang != '\0' && *locale_terr != '\0') {
1681 snprintf(buf, sizeof(buf), "%s/%s_%s.%s", *mp,
1682 locale_lang, locale_terr, locale_codeset);
1683 if (is_directory (buf) == 1)
1684 l_found = try_section (buf, shortsec, longsec, name, glob);
1687 if (*locale_lang != '\0') {
1688 snprintf(buf, sizeof(buf), "%s/%s.%s", *mp,
1689 locale_lang, locale_codeset);
1690 if (is_directory (buf) == 1)
1691 l_found = try_section (buf, shortsec, longsec, name, glob);
1694 if (!l_found && strcmp(locale_lang, "en") != 0) {
1695 snprintf(buf, sizeof(buf), "%s/en.%s", *mp,
1697 if (is_directory (buf) == 1)
1698 l_found = try_section (buf, shortsec, longsec, name, glob);
1706 found += try_section (*mp, shortsec, longsec, name, glob);
1712 if (found && !findall) /* i.e. only do this section... */
1718 for (sp = section_list; *sp != NULL; sp++)
1720 for (mp = manpathlist; *mp != NULL; mp++)
1723 fprintf (stderr, "\nsearching in %s\n", *mp);
1729 if (locale != NULL) {
1730 locale_opts = locale_nroff;
1732 if (*locale_lang != '\0' && *locale_terr != '\0') {
1733 snprintf(buf, sizeof(buf), "%s/%s_%s.%s", *mp,
1734 locale_lang, locale_terr, locale_codeset);
1735 if (is_directory (buf) == 1)
1736 l_found = try_section (buf, *sp, longsec, name, glob);
1739 if (*locale_lang != '\0') {
1740 snprintf(buf, sizeof(buf), "%s/%s.%s", *mp,
1741 locale_lang, locale_codeset);
1742 if (is_directory (buf) == 1)
1743 l_found = try_section (buf, *sp, longsec, name, glob);
1746 if (!l_found && strcmp(locale_lang, "en") != 0) {
1747 snprintf(buf, sizeof(buf), "%s/en.%s", *mp,
1749 if (is_directory (buf) == 1)
1750 l_found = try_section (buf, *sp, longsec, name, glob);
1758 found += try_section (*mp, *sp, longsec, name, glob);
1764 if (found && !findall) /* i.e. only do this section... */
1778 #define TMP_SECTION_LIST_SIZE 100
1779 static char *tmp_section_list[TMP_SECTION_LIST_SIZE];
1781 if (colon_sep_section_list == NULL)
1783 if ((p = getenv ("MANSECT")) == NULL)
1785 return std_sections;
1789 colon_sep_section_list = strdup (p);
1794 for (p = colon_sep_section_list; i < TMP_SECTION_LIST_SIZE ; p = end+1)
1796 if ((end = strchr (p, ':')) != NULL)
1799 tmp_section_list[i++] = strdup (p);
1805 tmp_section_list [i] = NULL;
1806 return tmp_section_list;