BSD 2 development
[unix-history] / src / man.c
CommitLineData
52358b92
BJ
1/* Copyright (c) 1979 Regents of the University of California */
2#include <retrofit.h>
3#include <stdio.h>
4#include <sgtty.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7/*
8 * man - intelligent man command
9 *
10 * Author: Bill Joy UCB August 25, 1977
11 *
12 * Man is an intelligent man command which obviates the need to know
13 * section numbers in the manual. Also if the standard output is a teletype and
14 * the option - is not given we pipe through "ssp and cr3" to eliminate piled
15 * up blank lines.
16 */
17int nocr3;
18char *strcpy();
19char *strcat();
20int section;
21int subsec;
22int troffit;
23
24#define eq(a,b) (strcmp(a,b) == 0)
25
26main(argc, argv)
27 int argc;
28 char *argv[];
29{
30
31 if (argc <= 1) {
32 fprintf(stderr, "Usage: man [ section ] name ...\n");
33 exit(1);
34 }
35 if (chdir("/usr/man") < 0) {
36 fprintf(stderr, "Can't chdir to /usr/man.\n");
37 exit(1);
38 }
39 argc--, argv++;
40 while (argc > 0 && argv[0][0] == '-') {
41 switch(argv[0][1]) {
42
43 case 0:
44 nocr3++;
45 break;
46
47 case 't':
48 troffit++;
49 break;
50 }
51 argc--, argv++;
52 }
53 if (troffit == 0 && nocr3 == 0 && !isatty(1))
54 nocr3++;
55 section = 0;
56 do {
57 if (eq(argv[0], "new") || eq(argv[0], "ucb")) {
58 section = 'u';
59 goto sectin;
60 } else if (eq(argv[0], "public")) {
61 section = 'p';
62 goto sectin;
63 } else if (eq(argv[0], "sccs")) {
64 section = 's';
65 goto sectin;
66 } else if (argv[0][0] >= '0' && argv[0][0] <= '9' && (argv[0][1] == 0 || argv[0][2] == 0)) {
67 section = argv[0][0];
68 subsec = argv[0][1];
69sectin:
70 argc--, argv++;
71 if (argc == 0) {
72 fprintf(stderr, "But what do you want from section %s?\n", argv[-1]);
73 exit(1);
74 }
75 continue;
76 }
77 manual(section, argv[0]);
78 argc--, argv++;
79 } while (argc > 0);
80 exit(0);
81}
82
83manual(sec, name)
84 char sec;
85 char *name;
86{
87 char section = sec;
88 char work[100];
89 int ss;
90 struct stat stbuf;
91 int last;
92 char *sp = "u16823457ps";
93
94 strcpy(work, "manx/");
95 strcat(work, name);
96 strcat(work, ".x");
97 last = strlen(work) - 1;
98 if (section == '1') {
99 sp = "u16p";
100 section = 0;
101 }
102 if (section == 0) {
103 ss = 0;
104 for (section = *sp++; section; section = *sp++) {
105 work[3] = section;
106 work[last] = section;
107 work[last+1] = 0;
108 if (stat(work, &stbuf) >= 0)
109 break;
110 if (work[last] == '1' || work[last] == '3') {
111 char *cp;
112search:
113 cp = work[last] == '1' ? "mcg" : "xmsf";
114 while (*cp) {
115 work[last+1] = *cp++;
116 if (stat(work, &stbuf) >= 0) {
117 ss = work[last+1];
118 goto found;
119 }
120 }
121 if (ss = 0)
122 work[last+1] = 0;
123 }
124 }
125 if (section == 0) {
126 if (sec == 0)
127 printf("No manual entry for %s.\n", name);
128 else
129 printf("No entry for %s in section %c of the manual.\n", name, sec);
130 return;
131 }
132 } else {
133 work[3] = section;
134 work[last] = section;
135 work[last+1] = subsec;
136 if (stat(work, &stbuf) < 0) {
137 if ((section == '1' || section == '3') && subsec == 0) {
138 sp = "\0";
139 goto search;
140 }
141 printf("No entry for %s in section %c", name, section);
142 if (subsec)
143 putchar(subsec);
144 printf(" of the manual.\n");
145 return;
146 }
147 }
148found:
149 if (troffit)
150 troff(work);
151 else
152 nroff(work);
153}
154
155nroff(cp)
156 char *cp;
157{
158 char cmdbuf[BUFSIZ];
159
160 if (nocr3)
161 sprintf(cmdbuf, "nroff -h -man %s", cp);
162 else
163 sprintf(cmdbuf, "nroff -h -man %s | /usr/ucb/ssp | /usr/ucb/cr3", cp);
164 system(cmdbuf);
165}
166
167troff(cp)
168 char *cp;
169{
170 char cmdbuf[BUFSIZ];
171
172 sprintf(cmdbuf, "/usr/ucb/troff -t -man /usr/lib/tmac/tmac.vcat %s | /usr/ucb/vsort | /usr/ucb/vpr -t", cp);
173 system(cmdbuf);
174}
175
176any(c, sp)
177 register int c;
178 register char *sp;
179{
180 register int d;
181
182 while (d = *sp++)
183 if (c == d)
184 return (1);
185 return (0);
186}