aboutsummaryrefslogtreecommitdiff
path: root/gnu/usr.bin/man/man/man.c
blob: b51d87a1818113912d0a911497fbc34b6a3a12ba (plain) (blame)
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
/*
 * man.c
 *
 * Copyright (c) 1990, 1991, John W. Eaton.
 *
 * You may distribute under the terms of the GNU General Public
 * License as specified in the file COPYING that comes with the man
 * distribution.
 *
 * John W. Eaton
 * jwe@che.utexas.edu
 * Department of Chemical Engineering
 * The University of Texas at Austin
 * Austin, Texas  78712
 */

#ifndef lint
static const char rcsid[] =
  "$FreeBSD: src/gnu/usr.bin/man/man/man.c,v 1.65.2.2.4.1 2010/12/21 17:10:29 kensmith Exp $";
#endif /* not lint */

#define MAN_MAIN

#include <sys/file.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/utsname.h>
#include <ctype.h>
#include <errno.h>
#ifdef __FreeBSD__
#include <locale.h>
#include <langinfo.h>
#include <libgen.h>
#endif
#include <stdio.h>
#include <string.h>
#include <signal.h>
#if HAVE_LIBZ > 0
#include <zlib.h>
#endif
#include "config.h"
#include "gripes.h"
#include "version.h"

#ifdef POSIX
#include <unistd.h>
#else
#ifndef R_OK
#define R_OK 4
#endif
#endif

#ifdef STDC_HEADERS
#include <stdlib.h>
#else
extern char *malloc ();
extern char *getenv ();
extern void free ();
extern int system ();
extern int strcmp ();
extern int strncmp ();
extern int exit ();
extern int fflush ();
extern int printf ();
extern int fprintf ();
extern FILE *fopen ();
extern int fclose ();
extern char *sprintf ();
#endif

extern char **glob_filename ();
extern int is_newer ();
extern int is_directory ();
extern int is_file ();
extern int do_system_command ();

char *prognam;
static char *pager;
static char *machine_arch;
static char *machine;
static char *manp;
static char *manpathlist[MAXDIRS];
static char *shortsec;
static char *longsec;
static char *colon_sep_section_list;
static char **section_list;
static char *roff_directive;
static int apropos;
static int whatis;
static int findall;
static int print_where;
static char *ultimate_source ();

#ifdef __FreeBSD__
static char *locale, *locale_opts, *locale_nroff, *locale_codeset;
static char locale_terr[3], locale_lang[3];
static char *man_locale;
static int use_man_locale;
static int use_original;
struct ltable {
	char *lcode;
	char *nroff;
};
static struct ltable ltable[] = {
	{"KOI8-R", "koi8-r"},
	{"ISO8859-1", "latin1"},
	{"ISO8859-15", "latin1"},
	{"UTF-8", "utf8"},
	{NULL}
};
#endif

static int troff = 0;

int debug;

#ifdef HAS_TROFF
#ifdef __FreeBSD__
static char args[] = "M:P:S:adfhkm:op:tw?";
#else
static char args[] = "M:P:S:adfhkm:p:tw?";
#endif
#else
#ifdef __FreeBSD__
static char args[] = "M:P:S:adfhkm:op:w?";
#else
static char args[] = "M:P:S:adfhkm:p:w?";
#endif
#endif

#ifdef SETUID
uid_t ruid;
uid_t euid;
#endif

int
main (argc, argv)
     int argc;
     char **argv;
{
  int status = 0;
  char *nextarg;
  char *tmp;
  extern char *mkprogname ();
  char *is_section ();
  char **get_section_list ();
  void man_getopt ();
  void do_apropos ();
  void do_whatis ();
  int man ();

  prognam = mkprogname (argv[0]);
  longsec = NULL;

  unsetenv("IFS");
#ifdef __FreeBSD__
  (void) setlocale(LC_ALL, "");
#endif
  man_getopt (argc, argv);

  if (optind == argc)
    gripe_no_name ((char *)NULL);

  section_list = get_section_list ();

  if (optind == argc - 1)
    {
      tmp = is_section (argv[optind], manp);

      if (tmp != NULL)
	gripe_no_name (tmp);
    }

#ifdef SETUID
  ruid = getuid();
  euid = geteuid();
  seteuid(ruid);
#endif

  while (optind < argc)
    {
      nextarg = argv[optind++];

      /*
       * See if this argument is a valid section name.  If not,
       * is_section returns NULL.
       */
      tmp = is_section (nextarg, manp);

      if (tmp != NULL)
	{
	  shortsec = tmp;

	  if (debug)
	    fprintf (stderr, "\nsection: %s\n", shortsec);

	  continue;
	}

      if (apropos) {
	do_apropos (nextarg);
	status = (status ? 0 : 1); /* reverts status, see below */
      }
      else if (whatis) {
	do_whatis (nextarg);
	status = (status ? 0 : 1); /* reverts status, see below */
      }
      else if (strchr (nextarg, '/') != NULL && is_file (nextarg) == 1)
	{
	  format_and_display (NULL, ultimate_source(nextarg, dirname(nextarg)),
			      NULL);
	}
      else
	{
	  status = man (nextarg);

	  if (status == 0)
	    gripe_not_found (nextarg, longsec);
	}
    }
  return (status==0);         /* status==1 --> exit(0),
                                 status==0 --> exit(1) */
}

void
usage ()
{
  static char usage_string[1024] = "%s, version %s\n\n";

#ifdef HAS_TROFF
#ifdef __FreeBSD__
  static char s1[] =
    "usage: %s [-adfhkotw] [section] [-M path] [-P pager] [-S list]\n\
           [-m machine] [-p string] name ...\n\n";
#else
  static char s1[] =
    "usage: %s [-adfhktw] [section] [-M path] [-P pager] [-S list]\n\
           [-m machine] [-p string] name ...\n\n";
#endif
#else
#ifdef __FreeBSD__
  static char s1[] =
    "usage: %s [-adfhkow] [section] [-M path] [-P pager] [-S list]\n\
           [-m machine] [-p string] name ...\n\n";
#else
  static char s1[] =
    "usage: %s [-adfhkw] [section] [-M path] [-P pager] [-S list]\n\
           [-m machine] [-p string] name ...\n\n";
#endif
#endif

static char s2[] = "  a : find all matching entries\n\
  d : print gobs of debugging information\n\
  f : same as whatis(1)\n\
  h : print this help message\n\
  k : same as apropos(1)\n";

#ifdef __FreeBSD__
  static char s3[] = "  o : use original, non-localized manpages\n";
#endif

#ifdef HAS_TROFF
  static char s4[] = "  t : use troff to format pages for printing\n";
#endif

  static char s5[] = "  w : print location of man page(s) that would be displayed\n\n\
  M path    : set search path for manual pages to `path'\n\
  P pager   : use program `pager' to display pages\n\
  S list    : colon separated section list\n\
  m machine : search for alternate architecture man pages\n";

  static char s6[] = "  p string : string tells which preprocessors to run\n\
               e - [n]eqn(1)   p - pic(1)    t - tbl(1)\n\
               g - grap(1)     r - refer(1)  v - vgrind(1)\n";

  strcat (usage_string, s1);
  strcat (usage_string, s2);
#ifdef __FreeBSD__
  strcat (usage_string, s3);
#endif

#ifdef HAS_TROFF
  strcat (usage_string, s4);
#endif

  strcat (usage_string, s5);

  strcat (usage_string, s6);

  fprintf (stderr, usage_string, prognam, version, prognam);
  exit(1);
}

char **
add_dir_to_mpath_list (mp, p)
     char **mp;
     char *p;
{
  int status;

  status = is_directory (p);

  if (status < 0 && debug)
    {
      fprintf (stderr, "Warning: couldn't stat file %s!\n", p);
    }
  else if (status == 0 && debug)
    {
      fprintf (stderr, "Warning: %s isn't a directory!\n", p);
    }
  else if (status == 1)
    {
      if (debug)
	fprintf (stderr, "adding %s to manpathlist\n", p);

      *mp++ = strdup (p);
    }
  return mp;
}

/*
 * Get options from the command line and user environment.
 */
void
man_getopt (argc, argv)
     register int argc;
     register char **argv;
{
  register int c;
  register char *p;
  register char *end;
  register char **mp;
  extern void downcase ();
  extern char *manpath ();

  while ((c = getopt (argc, argv, args)) != EOF)
    {
      switch (c)
	{
	case 'M':
	  manp = strdup (optarg);
	  break;
	case 'P':
	  pager = strdup (optarg);
	  if (setenv("PAGER", pager, 1) != 0)
		  (void)fprintf(stderr, "setenv PAGER=%s\n", pager);
	  break;
	case 'S':
	  colon_sep_section_list = strdup (optarg);
	  break;
	case 'a':
	  findall++;
	  break;
	case 'd':
	  debug++;
	  break;
	case 'f':
	  if (troff)
	    gripe_incompatible ("-f and -t");
	  if (apropos)
	    gripe_incompatible ("-f and -k");
	  if (print_where)
	    gripe_incompatible ("-f and -w");
	  whatis++;
	  break;
	case 'k':
	  if (troff)
	    gripe_incompatible ("-k and -t");
	  if (whatis)
	    gripe_incompatible ("-k and -f");
	  if (print_where)
	    gripe_incompatible ("-k and -w");
	  apropos++;
	  break;
	case 'm':
	  machine_arch = optarg;
	  if ((machine = strchr(optarg, ':')) != NULL)
	    *machine++ = '\0';
	  else
	    machine = optarg;
	  break;
#ifdef __FreeBSD__
	case 'o':
	  use_original++;
	  break;
#endif
	case 'p':
	  roff_directive = strdup (optarg);
	  break;
#ifdef HAS_TROFF
	case 't':
	  if (apropos)
	    gripe_incompatible ("-t and -k");
	  if (whatis)
	    gripe_incompatible ("-t and -f");
	  if (print_where)
	    gripe_incompatible ("-t and -w");
	  troff++;
	  break;
#endif
	case 'w':
	  if (apropos)
	    gripe_incompatible ("-w and -k");
	  if (whatis)
	    gripe_incompatible ("-w and -f");
	  if (troff)
	    gripe_incompatible ("-w and -t");
	  print_where++;
	  break;
	case 'h':
	case '?':
	default:
	  usage();
	  break;
	}
    }

#ifdef __FreeBSD__
  /* "" intentionally used to catch error */
  if ((locale = setlocale(LC_CTYPE, "")) != NULL)
	locale_codeset = nl_langinfo(CODESET);
  if (!use_original && locale != NULL && *locale_codeset != '\0' &&
      strcmp(locale_codeset, "US-ASCII") != 0
     ) {
	char *tmp, *short_locale;
	struct ltable *pltable;

	*locale_lang = '\0';
	*locale_terr = '\0';

	if ((short_locale = strdup(locale)) == NULL) {
		perror ("ctype locale strdup");
		exit (1);
	}
	if ((tmp = strchr(short_locale, '.')) != NULL)
		*tmp = '\0';

	if (strlen(short_locale) == 2)
		strcpy(locale_lang, short_locale);
	else if ((tmp = strchr(short_locale, '_')) == NULL ||
		 tmp != short_locale + 2 ||
		 strlen(tmp + 1) != 2
		) {
		errno = EINVAL;
		perror ("ctype locale format");
		locale = NULL;
	} else {
		strncpy(locale_terr, short_locale + 3, 2);
		locale_terr[2] = '\0';
		strncpy(locale_lang, short_locale, 2);
		locale_lang[2] = '\0';
	}

	free(short_locale);

	if (locale != NULL) {
		for (pltable = ltable; pltable->lcode != NULL; pltable++) {
			if (strcmp(pltable->lcode, locale_codeset) == 0) {
				locale_nroff = pltable->nroff;
				break;
			}
		}
		asprintf(&man_locale, "%s.%s", locale_lang, locale_codeset);
	}
  } else {
	if (locale == NULL) {
		errno = EINVAL;
		perror ("ctype locale");
	} else {
		locale = NULL;
		if (*locale_codeset == '\0') {
			errno = EINVAL;
			perror ("ctype codeset");
		}
	}
  }
#endif /* __FreeBSD__ */

  if (pager == NULL || *pager == '\0')
    if ((pager = getenv ("PAGER")) == NULL || *pager == '\0')
      pager = strdup (PAGER);

  if (debug)
    fprintf (stderr, "\nusing %s as pager\n", pager);

  if (machine_arch == NULL && (machine_arch = getenv ("MACHINE_ARCH")) == NULL)
    machine_arch = MACHINE_ARCH;

  if (machine == NULL && (machine = getenv ("MACHINE")) == NULL)
    {
      static struct utsname utsname;

      if (uname(&utsname) == -1)
	{
	  perror ("uname");
	  exit (1);
	}
      machine = utsname.machine;
    }

  if (debug)
    fprintf (stderr, "\nusing %s:%s architecture\n", machine_arch, machine);

  if (manp == NULL)
    {
      if ((manp = manpath (0)) == NULL)
	gripe_manpath ();

      if (debug)
	fprintf (stderr,
		 "\nsearch path for pages determined by manpath is\n%s\n\n",
		 manp);
    }

  /*
   * Expand the manpath into a list for easier handling.
   */
  mp = manpathlist;
  for (p = manp; ; p = end+1)
    {
      if (mp == manpathlist + MAXDIRS - 1) {
	fprintf (stderr, "Warning: too many directories in manpath, truncated!\n");
	break;
      }
      if ((end = strchr (p, ':')) != NULL)
	*end = '\0';

      mp = add_dir_to_mpath_list (mp, p);
      if (end == NULL)
	break;

      *end = ':';
    }
  *mp = NULL;
}

/*
 * Check to see if the argument is a valid section number.  If the
 * first character of name is a numeral, or the name matches one of
 * the sections listed in section_list, we'll assume that it's a section.
 * The list of sections in config.h simply allows us to specify oddly
 * named directories like .../man3f.  Yuk.
 */
char *
is_section (name, path)
     char *name;
     char *path;
{
  register char **vs;
  char *temp, *end, *loc;
  char **plist;
  int x;

  for (vs = section_list; *vs != NULL; vs++)
    if ((strcmp (*vs, name) == 0)
	|| (isdigit ((unsigned char)name[0]) && strlen(name) == 1))
      return (longsec = strdup(name));

  plist = manpathlist;
  if (isdigit ((unsigned char)name[0]))
    {
      while (*plist != NULL)
	{
	  asprintf (&temp, "%s/man%c/*", *plist, name[0]);
	  plist++;

	  x = 0;
	  vs = glob_filename (temp);
	  if (vs == (char **) -1)
	    {
	      free (temp);
	      return NULL;
	    }
	  for ( ; *vs != NULL; vs++)
	    {
	      end = strrchr (*vs, '/');
	      if ((loc = strstr (end, name)) != NULL && loc - end > 2
		  && *(loc-1) == '.'
		  && (*(loc+strlen(name)) == '\0' || *(loc+strlen(name)) == '.'))
		{
		  x = 1;
		  break;
		}
	    }
	  free (temp);
	  if (x == 1)
	    {
	      asprintf (&temp, "%c", name[0]);
	      longsec = strdup (name);
	      return (temp);
	    }
	}
    }
  return NULL;
}

/*
 * Handle the apropos option.  Cheat by using another program.
 */
void
do_apropos (name)
     register char *name;
{
  register int len;
  register char *command;

  len = strlen (APROPOS) + strlen (name) + 4;

  if ((command = (char *) malloc(len)) == NULL)
    gripe_alloc (len, "command");

  sprintf (command, "%s \"%s\"", APROPOS, name);

  (void) do_system_command (command);

  free (command);
}

/*
 * Handle the whatis option.  Cheat by using another program.
 */
void
do_whatis (name)
     register char *name;
{
  register int len;
  register char *command;

  len = strlen (WHATIS) + strlen (name) + 4;

  if ((command = (char *) malloc(len)) == NULL)
    gripe_alloc (len, "command");

  sprintf (command, "%s \"%s\"", WHATIS, name);

  (void) do_system_command (command);

  free (command);
}

/*
 * Change a name of the form ...man/man1/name.1 to ...man/cat1/name.1
 * or a name of the form ...man/cat1/name.1 to ...man/man1/name.1
 */
char *
convert_name (name, to_cat)
     register char *name;
     register int to_cat;
{
  register char *to_name;
  register char *t1;
  register char *t2 = NULL;

#ifdef DO_COMPRESS
  if (to_cat)
    {
      int olen = strlen(name);
      int cextlen = strlen(COMPRESS_EXT);
      int len = olen + cextlen;

      to_name = malloc (len+1);
      if (to_name == NULL)
	gripe_alloc (len+1, "to_name");
      strcpy (to_name, name);
      olen -= cextlen;
      /* Avoid tacking it on twice */
      if (olen >= 1 && strcmp(name + olen, COMPRESS_EXT) != 0)
      	strcat (to_name, COMPRESS_EXT);
    }
  else
    to_name = strdup (name);
#else
  to_name = strdup (name);
#endif

  t1 = strrchr (to_name, '/');
  if (t1 != NULL)
    {
      *t1 = '\0';
      t2 = strrchr (to_name, '/');
      *t1 = '/';

      /* Skip architecture part (if present). */
      if (t2 != NULL && (t1 - t2 < 5 || *(t2 + 1) != 'm' || *(t2 + 3) != 'n'))
	{
	  t1 = t2;
	  *t1 = '\0';
	  t2 = strrchr (to_name, '/');
	  *t1 = '/';
	}
    }

  if (t2 == NULL)
    gripe_converting_name (name, to_cat);

  if (to_cat)
    {
      *(++t2) = 'c';
      *(t2+2) = 't';
    }
  else
    {
      *(++t2) = 'm';
      *(t2+2) = 'n';
    }

  if (debug)
    fprintf (stderr, "to_name in convert_name () is: %s\n", to_name);

  return to_name;
}

/*
 * Try to find the man page corresponding to the given name.  The
 * reason we do this with globbing is because some systems have man
 * page directories named man3 which contain files with names like
 * XtPopup.3Xt.  Rather than requiring that this program know about
 * all those possible names, we simply try to match things like
 * .../man[sect]/name[sect]*.  This is *much* easier.
 *
 * Note that globbing is only done when the section is unspecified.
 */
char **
glob_for_file (path, section, longsec, name, cat)
     char *path;
     char *section;
     char *longsec;
     char *name;
     int cat;
{
  char pathname[FILENAME_MAX];
  char **gf;

  if (longsec == NULL)
    longsec = section;

  if (cat)
    snprintf (pathname, sizeof(pathname), "%s/cat%s/%s.%s*", path, section,
	name, longsec);
  else
    snprintf (pathname, sizeof(pathname), "%s/man%s/%s.%s*", path, section,
	name, longsec);

  if (debug)
    fprintf (stderr, "globbing %s\n", pathname);

  gf = glob_filename (pathname);

  if ((gf == (char **) -1 || *gf == NULL) && isdigit ((unsigned char)*section)
      && strlen (longsec) == 1)
    {
      if (cat)
	snprintf (pathname, sizeof(pathname), "%s/cat%s/%s.%c*", path, section, name, *section);
      else
	snprintf (pathname, sizeof(pathname), "%s/man%s/%s.%c*", path, section, name, *section);

      gf = glob_filename (pathname);
    }
  if ((gf == (char **) -1 || *gf == NULL) && isdigit ((unsigned char)*section)
      && strlen (longsec) == 1)
    {
      if (cat)
	snprintf (pathname, sizeof(pathname), "%s/cat%s/%s.0*", path, section, name);
      else
	snprintf (pathname, sizeof(pathname), "%s/man%s/%s.0*", path, section, name);
      if (debug)
	fprintf (stderr, "globbing %s\n", pathname);
      gf = glob_filename (pathname);
    }
  return gf;
}

/*
 * Return an un-globbed name in the same form as if we were doing
 * globbing.
 */
char **
make_name (path, section, longsec, name, cat)
     char *path;
     char *section;
     char *longsec;
     char *name;
     int cat;
{
  register int i = 0;
  static char *names[3];
  char buf[FILENAME_MAX];

  if (cat)
    snprintf (buf, sizeof(buf), "%s/cat%s/%s.%s", path, section, name, longsec);
  else
    snprintf (buf, sizeof(buf), "%s/man%s/%s.%s", path, section, name, longsec);

  if (access (buf, R_OK) == 0)
    names[i++] = strdup (buf);

  /*
   * If we're given a section that looks like `3f', we may want to try
   * file names like .../man3/foo.3f as well.  This seems a bit
   * kludgey to me, but what the hey...
   */
  if (section[1] != '\0')
    {
      if (cat)
	snprintf (buf, sizeof(buf), "%s/cat%c/%s.%s", path, section[0], name, section);
      else
	snprintf (buf, sizeof(buf), "%s/man%c/%s.%s", path, section[0], name, section);

      if (access (buf, R_OK) == 0)
	names[i++] = strdup (buf);
    }

  names[i] = NULL;

  return &names[0];
}

char *
get_expander (file)
     char *file;
{
  char *end = file + (strlen (file) - 1);

  while (end > file && end[-1] != '.')
    --end;
  if (end == file)
    return NULL;
#ifdef FCAT
  if (*end == 'F')
    return FCAT;
#endif	/* FCAT */
#ifdef YCAT
  if (*end == 'Y')
    return YCAT;
#endif	/* YCAT */
#ifdef ZCAT
  if (*end == 'Z' || !strcmp(end, "gz") || !strcmp(end, "bz2"))
    return ZCAT;
#endif	/* ZCAT */
  return NULL;
}

/*
 * Simply display the preformatted page.
 */
int
display_cat_file (file)
     register char *file;
{
  register int found;
  char command[FILENAME_MAX];

  found = 0;

  if (access (file, R_OK) == 0)
    {
      char *expander = get_expander (file);

      if (expander != NULL)
	snprintf (command, sizeof(command), "%s %s | %s", expander, file, pager);
      else
	snprintf (command, sizeof(command), "%s %s", pager, file);

      found = do_system_command (command);
    }
  return found;
}

/*
 * Try to find the ultimate source file.  If the first line of the
 * current file is not of the form
 *
 *      .so man3/printf.3s
 *
 * the input file name is returned.
 */
char *
ultimate_source (name, path)
     char *name;
     char *path;
{
  static  char buf[BUFSIZ];
  static  char ult[FILENAME_MAX];

  FILE *fp;
  char *beg;
  char *end;

  strncpy (ult, name, sizeof(ult)-1);
  ult[sizeof(ult)-1] = '\0';
  strncpy (buf, name, sizeof(buf)-1);
  ult[sizeof(buf)-1] = '\0';

 next:

#if HAVE_LIBZ > 0
  if ((fp = gzopen (ult, "r")) == NULL)
  {
    /* check for the compressed version too */
    strlcat(ult, ".gz", FILENAME_MAX);
    if ((fp = gzopen (ult, "r")) == NULL)
      return ult; /* we munged it, but it doesn't exist anyway */
  }
#else
  if ((fp = fopen (ult, "r")) == NULL)
    return ult;
#endif

#if HAVE_LIBZ > 0
  end = gzgets (fp, buf, BUFSIZ);
  gzclose(fp);
#else
  end = fgets (buf, BUFSIZ, fp);
  fclose(fp);
#endif

  if (!end || strlen (buf) < 5)
    return ult;

  beg = buf;
  if (*beg++ == '.' && *beg++ == 's' && *beg++ == 'o')
    {
      while ((*beg == ' ' || *beg == '\t') && *beg != '\0')
	beg++;

      end = beg;
      while (*end != ' ' && *end != '\t' && *end != '\n' && *end != '\0')
	end++;

      *end = '\0';

      snprintf(ult, sizeof(ult), "%s/%s", path, beg);
      snprintf(buf, sizeof(buf), "%s", ult);

      goto next;
    }

  if (debug)
    fprintf (stderr, "found ultimate source file %s\n", ult);

  return ult;
}

void
add_directive (first, d, file, buf, bufsize)
     int *first;
     char *d;
     char *file;
     char *buf;
     int bufsize;
{
  if (strcmp (d, "") != 0)
    {
      if (*first)
	{
	  *first = 0;
	  snprintf(buf, bufsize, "%s %s", d, file);
	}
      else
	{
	  strncat (buf, " | ", bufsize-strlen(buf)-1);
	  strncat (buf, d, bufsize-strlen(buf)-1);
	}
    }
}

int
parse_roff_directive (cp, file, buf, bufsize)
  char *cp;
  char *file;
  char *buf;
  int bufsize;
{
  char c;
  char *exp;
  int first = 1;
  int preproc_found = 0;
  int use_col = 0;

  if ((exp = get_expander(file)) != NULL)
	add_directive (&first, exp, file, buf, bufsize);

  while ((c = *cp++) != '\0')
    {
      switch (c)
	{
	case 'e':

	  if (debug)
	    fprintf (stderr, "found eqn(1) directive\n");

	  preproc_found++;
	  if (troff)
	    add_directive (&first, EQN, file, buf, bufsize);
	  else {
#ifdef __FreeBSD__
	    char lbuf[FILENAME_MAX];

	    snprintf(lbuf, sizeof(lbuf), "%s -T%s", NEQN,
		     locale_opts == NULL ? "ascii" : locale_opts);
	    add_directive (&first, lbuf, file, buf, bufsize);
#else
	    add_directive (&first, NEQN, file, buf, bufsize);
#endif
	  }

	  break;

	case 'g':

	  if (debug)
	    fprintf (stderr, "found grap(1) directive\n");

	  preproc_found++;
	  add_directive (&first, GRAP, file, buf, bufsize);

	  break;

	case 'p':

	  if (debug)
	    fprintf (stderr, "found pic(1) directive\n");

	  preproc_found++;
	  add_directive (&first, PIC, file, buf, bufsize);

	  break;

	case 't':

	  if (debug)
	    fprintf (stderr, "found tbl(1) directive\n");

	  preproc_found++;
	  use_col++;
	  add_directive (&first, TBL, file, buf, bufsize);
	  break;

	case 'v':

	  if (debug)
	    fprintf (stderr, "found vgrind(1) directive\n");

	  add_directive (&first, VGRIND, file, buf, bufsize);
	  break;

	case 'r':

	  if (debug)
	    fprintf (stderr, "found refer(1) directive\n");

	  add_directive (&first, REFER, file, buf, bufsize);
	  break;

	case ' ':
	case '\t':
	case '\n':

	  goto done;

	default:

	  return -1;
	}
    }

 done:

#ifdef HAS_TROFF
  if (troff)
    add_directive (&first, TROFF, file, buf, bufsize);
  else
#endif
    {
#ifdef __FreeBSD__
      char lbuf[FILENAME_MAX];

      snprintf(lbuf, sizeof(lbuf), "%s -T%s%s%s", NROFF,
	       locale_opts == NULL ? "ascii" : locale_opts,
	       use_man_locale ? " -dlocale=" : "",
	       use_man_locale ? man_locale : "");
	    add_directive (&first, lbuf, file, buf, bufsize);
#else
      add_directive (&first, NROFF " -Tascii", file, buf, bufsize);
#endif
    }
  if (use_col && !troff)
      add_directive (&first, COL, file, buf, bufsize);

  if (preproc_found)
    return 0;
  else
    return 1;
}

char *
make_roff_command (file)
     char *file;
{
#if HAVE_LIBZ > 0
  gzFile fp;
#else
  FILE *fp;
#endif
  char line [BUFSIZ];
  static char buf [BUFSIZ];
  int status;
  char *cp;

  if (roff_directive != NULL)
    {
      if (debug)
	fprintf (stderr, "parsing directive from command line\n");

      status = parse_roff_directive (roff_directive, file, buf, sizeof(buf));

      if (status == 0)
	return buf;

      if (status == -1)
	gripe_roff_command_from_command_line (file);
    }

#if HAVE_LIBZ > 0
  if ((fp = gzopen (file, "r")) != NULL)
#else
  if ((fp = fopen (file, "r")) != NULL)
#endif
    {
      cp = line;
#if HAVE_LIBZ > 0
      gzgets (fp, line, BUFSIZ);
      gzclose(fp);
#else
      fgets (line, BUFSIZ, fp);
      fclose(fp);
#endif
      if (*cp++ == '\'' && *cp++ == '\\' && *cp++ == '"' && *cp++ == ' ')
	{
	  if (debug)
	    fprintf (stderr, "parsing directive from file\n");

	  status = parse_roff_directive (cp, file, buf, sizeof(buf));

	  if (status == 0)
	    return buf;

	  if (status == -1)
	    gripe_roff_command_from_file (file);
	}
    }
  else
    {
      /*
       * Is there really any point in continuing to look for
       * preprocessor options if we can't even read the man page source?
       */
      gripe_reading_man_file (file);
      return NULL;
    }

  if ((cp = getenv ("MANROFFSEQ")) != NULL)
    {
      if (debug)
	fprintf (stderr, "parsing directive from environment\n");

      status = parse_roff_directive (cp, file, buf, sizeof(buf));

      if (status == 0)
	return buf;

      if (status == -1)
	gripe_roff_command_from_env ();
    }

  if (debug)
    fprintf (stderr, "using default preprocessor sequence\n");

  status = parse_roff_directive ("t", file, buf, sizeof(buf));
  if (status >= 0)
    return buf;
  else		/* can't happen */
    return NULL;
}

sig_t ohup, oint, oquit, oterm;
static char temp[FILENAME_MAX];

void cleantmp()
{
	unlink(temp);
	exit(1);
}

void
set_sigs()
{
  ohup = signal(SIGHUP, cleantmp);
  oint = signal(SIGINT, cleantmp);
  oquit = signal(SIGQUIT, cleantmp);
  oterm = signal(SIGTERM, cleantmp);
}

void
restore_sigs()
{
  signal(SIGHUP, ohup);
  signal(SIGINT, oint);
  signal(SIGQUIT, oquit);
  signal(SIGTERM, oterm);
}

/*
 * Try to format the man page and create a new formatted file.  Return
 * 1 for success and 0 for failure.
 */
int
make_cat_file (path, man_file, cat_file, manid)
     register char *path;
     register char *man_file;
     register char *cat_file;
{
  int s, f;
  FILE *pp;
#if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
  gzFile fp;
#else
  FILE *fp;
#endif
  char *roff_command;
  char command[FILENAME_MAX];

  roff_command = make_roff_command (man_file);
  if (roff_command == NULL)
      return 0;

  snprintf(temp, sizeof(temp), "%s.tmpXXXXXX", cat_file);
  if ((f = mkstemp(temp)) >= 0 && 
#if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
      (fp = gzdopen(f, "w")) != NULL)
#else
      (fp = fdopen(f, "w")) != NULL)
#endif
    {
      set_sigs();

      if (fchmod (f, CATMODE) < 0) {
	perror("fchmod");
	unlink(temp);
	restore_sigs();
	fclose(fp);
	return 0;
      } else if (debug)
	fprintf (stderr, "mode of %s is now %o\n", temp, CATMODE);

      snprintf (command, sizeof(command), "(cd %s ; %s)", path,
		roff_command);

      fprintf (stderr, "Formatting page, please wait...");
      fflush(stderr);

      if (debug)
	fprintf (stderr, "\ntrying command: %s\n", command);
      else {

#ifdef SETUID
	if (manid)
	  seteuid(ruid);
#endif
	if ((pp = popen(command, "r")) == NULL) {
	  s = errno;
	  fprintf(stderr, "Failed.\n");
	  errno = s;
	  perror("popen");
#ifdef SETUID
	  if (manid)
	    seteuid(euid);
#endif
	  unlink(temp);
	  restore_sigs();
#if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
	  gzclose(fp);
#else
	  fclose(fp);
#endif
	  return 0;
	}
#ifdef SETUID
	if (manid)
	  seteuid(euid);
#endif

	f = 0;
	while ((s = getc(pp)) != EOF) {
#if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
	  gzputc(fp, s);
#else
	  putc(s, fp);
#endif
	  f++;
	}

	if (!f || ((s = pclose(pp)) == -1)) {
	  s = errno;
	  fprintf(stderr, "Failed.\n");
	  errno = s;
	  if (f)
	    perror("pclose");
	  unlink(temp);
	  restore_sigs();
#if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
	  gzclose(fp);
#else
	  fclose(fp);
#endif
	  return 0;
	}

	if (s != 0) {
	  fprintf(stderr, "Failed.\n");
	  gripe_system_command(s);
	  unlink(temp);
	  restore_sigs();
#if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
	  gzclose(fp);
#else
	  fclose(fp);
#endif
	  return 0;
	}
      }

      if (debug)
	unlink(temp);
      else if (rename(temp, cat_file) == -1) {
	s = errno;
	fprintf(stderr,
		 "\nHmm!  Can't seem to rename %s to %s, check permissions on man dir!\n",
		 temp, cat_file);
	errno = s;
	perror("rename");
	unlink(temp);
	restore_sigs();
#if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
	gzclose(fp);
#else
	fclose(fp);
#endif
	return 0;
      }
      restore_sigs();

#if defined(HAVE_LIBZ) && defined(DO_COMPRESS)
      if (gzclose(fp)) {
#else
      if (fclose(fp)) {
#endif
	s = errno;
	if (!debug)
	  unlink(cat_file);
	fprintf(stderr, "Failed.\n");
	errno = s;
	perror("fclose");
	return 0;
      }

      if (debug) {
	fprintf(stderr, "No output, debug mode.\n");
	return 0;
      }

      fprintf(stderr, "Done.\n");

      return 1;
    }
  else
    {
      if (f >= 0) {
	s = errno;
	unlink(temp);
	errno = s;
      }
      if (debug) {
	s = errno;
	fprintf (stderr, "Couldn't open %s for writing.\n", temp);
	errno = s;
      }
      if (f >= 0) {
	perror("fdopen");
	close(f);
      }

      return 0;
    }
}

/*
 * Try to format the man page source and save it, then display it.  If
 * that's not possible, try to format the man page source and display
 * it directly.
 *
 * Note that we've already been handed the name of the ultimate source
 * file at this point.
 */
int
format_and_display (path, man_file, cat_file)
     register char *path;
     register char *man_file;
     register char *cat_file;
{
  int status;
  register int found;
  char *roff_command;
  char command[FILENAME_MAX];

  found = 0;

  if (access (man_file, R_OK) != 0)
    return 0;

  if (troff || path == NULL)
    {
      roff_command = make_roff_command (man_file);
      if (roff_command == NULL)
	return 0;
      if (troff)
	snprintf (command, sizeof(command), "(cd %s ; %s)", path, roff_command);
      else
	snprintf (command, sizeof(command), "%s | %s", roff_command, pager);

      found = do_system_command (command);
    }
  else
    {
      status = is_newer (man_file, cat_file);
      if (debug)
	fprintf (stderr, "status from is_newer() = %d\n", status);

      if (status == 1 || status == -2)
	{
	  /*
	   * Cat file is out of date.  Try to format and save it.
	   */
	  if (print_where)
	    {
	      printf ("%s\n", man_file);
	      found++;
	    }
	  else
	    {

#ifdef SETUID
	      seteuid(euid);
	      found = make_cat_file (path, man_file, cat_file, 1);
	      seteuid(ruid);

	      if (!found)
	        {
		  /* Try again as real user - see note below.
		     By running with
		       effective group (user) ID == real group (user) ID
		     except for the call above, I believe the problems
		     of reading private man pages is avoided.  */
		  found = make_cat_file (path, man_file, cat_file, 0);
	        }
#else
	      found = make_cat_file (path, man_file, cat_file, 0);
#endif
	      if (found)
		{
		  /*
		   * Creating the cat file worked.  Now just display it.
		   */
		  (void) display_cat_file (cat_file);
		}
	      else
		{
		  /*
		   * Couldn't create cat file.  Just format it and
		   * display it through the pager.
		   */
		  roff_command = make_roff_command (man_file);
		  if (roff_command == NULL)
		    return 0;
		  else
		    snprintf (command, sizeof(command), "(cd %s ; %s | %s)", path,
			     roff_command, pager);

		  found = do_system_command (command);
		}
	    }
	}
      else if (access (cat_file, R_OK) == 0)
	{
	  /*
	   * Formatting not necessary.  Cat file is newer than source
	   * file, or source file is not present but cat file is.
	   */
	  if (print_where)
	    {
	      printf ("%s (source: %s)\n", cat_file, man_file);
	      found++;
	    }
	  else
	    {
	      found = display_cat_file (cat_file);
	    }
	}
    }
  return found;
}

/*
 * See if the preformatted man page or the source exists in the given
 * section.
 */
int
try_section (path, section, longsec, name, glob)
     char *path;
     char *section;
     char *longsec;
     char *name;
     int glob;
{
  register int found = 0;
  register int to_cat;
  register int cat;
  register char **names;
  register char **np;
  static int arch_search;
  char buf[FILENAME_MAX];

  if (!arch_search)
    {
      snprintf(buf, sizeof(buf), "%s/man%s/%s", path, section, machine);
      if (is_directory (buf) == 1)
	{
	  snprintf(buf, sizeof(buf), "%s/%s", machine, name);
	  arch_search++;
	  found = try_section (path, section, longsec, buf, glob);
	  arch_search--;
	  if (found && !findall)   /* only do this architecture... */
	    return found;
	}
      if (strcmp(machine_arch, machine) != 0)
	{
	  snprintf(buf, sizeof(buf), "%s/man%s/%s", path, section, machine_arch);
	  if (is_directory (buf) == 1)
	    {
	      snprintf(buf, sizeof(buf), "%s/%s", machine_arch, name);
	      arch_search++;
	      found = try_section (path, section, longsec, buf, glob);
	      arch_search--;
	      if (found && !findall)   /* only do this architecture... */
		return found;
	    }
	}
    }

  if (debug)
    {
      if (glob)
	fprintf (stderr, "trying section %s with globbing\n", section);
      else
	fprintf (stderr, "trying section %s without globbing\n", section);
    }

#ifndef NROFF_MISSING
  /*
   * Look for man page source files.
   */
  cat = 0;
  if (glob)
    names = glob_for_file (path, section, longsec, name, cat);
  else
    names = make_name (path, section, longsec, name, cat);

  if (names == (char **) -1 || *names == NULL)
    /*
     * No files match.  See if there's a preformatted page around that
     * we can display.
     */
#endif /* NROFF_MISSING */
    {
      if (!troff)
	{
	  cat = 1;
	  if (glob)
	    names = glob_for_file (path, section, longsec, name, cat);
	  else
	    names = make_name (path, section, longsec, name, cat);

	  if (names != (char **) -1 && *names != NULL)
	    {
	      for (np = names; *np != NULL; np++)
		{
		  if (print_where)
		    {
		      printf ("%s\n", *np);
		      found++;
		    }
		  else
		    {
		      found += display_cat_file (*np);
		    }
		}
	    }
	}
    }
#ifndef NROFF_MISSING
  else
    {
      for (np = names; *np != NULL; np++)
	{
	  register char *cat_file = NULL;
	  register char *man_file;

	  man_file = ultimate_source (*np, path);

	  if (!troff)
	    {
	      to_cat = 1;

	      cat_file = convert_name (man_file, to_cat);

	      if (debug)
		fprintf (stderr, "will try to write %s if needed\n", cat_file);
	    }

	  found += format_and_display (path, man_file, cat_file);
	}
    }
#endif /* NROFF_MISSING */
  return found;
}

/*
 * Search for manual pages.
 *
 * If preformatted manual pages are supported, look for the formatted
 * file first, then the man page source file.  If they both exist and
 * the man page source file is newer, or only the source file exists,
 * try to reformat it and write the results in the cat directory.  If
 * it is not possible to write the cat file, simply format and display
 * the man file.
 *
 * If preformatted pages are not supported, or the troff option is
 * being used, only look for the man page source file.
 *
 */
int
man (name)
     char *name;
{
  register int found;
  register int glob;
  register char **mp;
  register char **sp;
#ifdef __FreeBSD__
  int l_found;
  char buf[FILENAME_MAX];
#endif

  found = 0;

  fflush (stdout);
  if (shortsec != NULL)
    {
      for (mp = manpathlist; *mp != NULL; mp++)
	{
	  if (debug)
	    fprintf (stderr, "\nsearching in %s\n", *mp);

	  glob = 1;

#ifdef __FreeBSD__
	  l_found = 0;
	  if (locale != NULL) {
	    locale_opts = locale_nroff;
	    use_man_locale = 1;
	    if (*locale_lang != '\0' && *locale_terr != '\0') {
	      snprintf(buf, sizeof(buf), "%s/%s_%s.%s", *mp,
		       locale_lang, locale_terr, locale_codeset);
	      if (is_directory (buf) == 1)
		l_found = try_section (buf, shortsec, longsec, name, glob);
	    }
	    if (!l_found) {
	      if (*locale_lang != '\0') {
		snprintf(buf, sizeof(buf), "%s/%s.%s", *mp,
			 locale_lang, locale_codeset);
		if (is_directory (buf) == 1)
		  l_found = try_section (buf, shortsec, longsec, name, glob);
	      }
	      use_man_locale = 0;
	      if (!l_found && strcmp(locale_lang, "en") != 0) {
		snprintf(buf, sizeof(buf), "%s/en.%s", *mp,
			 locale_codeset);
		if (is_directory (buf) == 1)
		  l_found = try_section (buf, shortsec, longsec, name, glob);
	      }
	    }
	    locale_opts = NULL;
	    use_man_locale = 0;
	  }
	  if (!l_found) {
#endif
	  found += try_section (*mp, shortsec, longsec, name, glob);
#ifdef __FreeBSD__
	  } else
	    found += l_found;
#endif

	  if (found && !findall)   /* i.e. only do this section... */
	    return found;
	}
    }
  else
    {
      for (sp = section_list; *sp != NULL; sp++)
	{
	  for (mp = manpathlist; *mp != NULL; mp++)
	    {
	      if (debug)
		fprintf (stderr, "\nsearching in %s\n", *mp);

	      glob = 1;

#ifdef __FreeBSD__
	      l_found = 0;
	      if (locale != NULL) {
		locale_opts = locale_nroff;
		use_man_locale = 1;
		if (*locale_lang != '\0' && *locale_terr != '\0') {
		  snprintf(buf, sizeof(buf), "%s/%s_%s.%s", *mp,
			   locale_lang, locale_terr, locale_codeset);
		  if (is_directory (buf) == 1)
		    l_found = try_section (buf, *sp, longsec, name, glob);
		}
		if (!l_found) {
		  if (*locale_lang != '\0') {
		    snprintf(buf, sizeof(buf), "%s/%s.%s", *mp,
			     locale_lang, locale_codeset);
		    if (is_directory (buf) == 1)
		      l_found = try_section (buf, *sp, longsec, name, glob);
		  }
		  use_man_locale = 0;
		  if (!l_found && strcmp(locale_lang, "en") != 0) {
		    snprintf(buf, sizeof(buf), "%s/en.%s", *mp,
			     locale_codeset);
		    if (is_directory (buf) == 1)
		      l_found = try_section (buf, *sp, longsec, name, glob);
		  }
		}
		locale_opts = NULL;
		use_man_locale = 0;
	      }
	      if (!l_found) {
#endif
	      found += try_section (*mp, *sp, longsec, name, glob);
#ifdef __FreeBSD__
	      } else
		found += l_found;
#endif

	      if (found && !findall)   /* i.e. only do this section... */
		return found;
	    }
	}
    }
  return found;
}

char **
get_section_list ()
{
  int i;
  char *p;
  char *end;
#define TMP_SECTION_LIST_SIZE 100
  static char *tmp_section_list[TMP_SECTION_LIST_SIZE];

  if (colon_sep_section_list == NULL)
    {
      if ((p = getenv ("MANSECT")) == NULL)
	{
	  return std_sections;
	}
      else
	{
	  colon_sep_section_list = strdup (p);
	}
    }

  i = 0;
  for (p = colon_sep_section_list; i < TMP_SECTION_LIST_SIZE ; p = end+1) 
    {
      if ((end = strchr (p, ':')) != NULL)
	*end = '\0';

      tmp_section_list[i++] = strdup (p);

      if (end == NULL)
	break;
    }

  tmp_section_list [i] = NULL;
  return tmp_section_list;
}