BSD 3 development
[unix-history] / usr / src / cmd / man / man.c
CommitLineData
1c486cb1
BJ
1#include <stdio.h>
2#include <sgtty.h>
3#include <sys/types.h>
4#include <stat.h>
5#include <local/uparm.h>
6/*
7 * man - intelligent man command
8 *
9 * Author: Bill Joy UCB August 25, 1977
10 *
11 * Man is an intelligent man command which obviates the need to know
12 * section numbers in the manual. Also if the standard output is a teletype and
13 * the option - is not given we pipe through "ssp, ul, and more" to eliminate piled
14 * up blank lines.
15 */
16int nocr3;
17int cflag;
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 'c':
48 cflag++;
49 break;
50
51 case 't':
52 troffit++;
53 break;
54 }
55 argc--, argv++;
56 }
57 if (troffit == 0 && nocr3 == 0 && !isatty(1))
58 nocr3++;
59 section = 0;
60 do {
61 if (eq(argv[0], "ucb")) {
62 section = 'u';
63 goto sectin;
64 } else if (eq(argv[0], "local")) {
65 section = 'l';
66 goto sectin;
67 } else if (eq(argv[0], "new")) {
68 section = 'n';
69 goto sectin;
70 } else if (eq(argv[0], "public")) {
71 section = 'p';
72 goto sectin;
73 } else if (eq(argv[0], "sccs")) {
74 section = 's';
75 goto sectin;
76 } else if (eq(argv[0], "junk")) {
77 section = 'j';
78 goto sectin;
79 } else if (argv[0][0] >= '0' && argv[0][0] <= '9' && (argv[0][1] == 0 || argv[0][2] == 0)) {
80 section = argv[0][0];
81 subsec = argv[0][1];
82sectin:
83 argc--, argv++;
84 if (argc == 0) {
85 fprintf(stderr, "But what do you want from section %s?\n", argv[-1]);
86 exit(1);
87 }
88 continue;
89 }
90 manual(section, argv[0]);
91 argc--, argv++;
92 } while (argc > 0);
93 exit(0);
94}
95
96manual(sec, name)
97 char sec;
98 char *name;
99{
100 char section = sec;
101 char work[100];
102 int ss;
103 struct stat stbuf;
104 int last;
105 char *sp = "lun16823457psj";
106
107 strcpy(work, "manx/");
108 strcat(work, name);
109 strcat(work, ".x");
110 last = strlen(work) - 1;
111 if (section == '1') {
112 sp = "lun16pj";
113 section = 0;
114 }
115 if (section == 0) {
116 ss = 0;
117 for (section = *sp++; section; section = *sp++) {
118 work[3] = section;
119 work[last] = section;
120 work[last+1] = 0;
121 if (stat(work, &stbuf) >= 0)
122 break;
123 if (work[last] == '1' || work[last] == '3') {
124 char *cp;
125search:
126 cp = work[last] == '1' ? "mcg" : "xmsf";
127 while (*cp) {
128 work[last+1] = *cp++;
129 if (stat(work, &stbuf) >= 0) {
130 ss = work[last+1];
131 goto found;
132 }
133 }
134 if (ss = 0)
135 work[last+1] = 0;
136 }
137 }
138 if (section == 0) {
139 if (sec == 0)
140 printf("No manual entry for %s.\n", name);
141 else
142 printf("No entry for %s in section %c of the manual.\n", name, sec);
143 return;
144 }
145 } else {
146 work[3] = section;
147 work[last] = section;
148 work[last+1] = subsec;
149 if (stat(work, &stbuf) < 0) {
150 if ((section == '1' || section == '3') && subsec == 0) {
151 sp = "\0";
152 goto search;
153 }
154 printf("No entry for %s in section %c", name, section);
155 if (subsec)
156 putchar(subsec);
157 printf(" of the manual.\n");
158 return;
159 }
160 }
161found:
162 if (troffit)
163 troff(work);
164 else
165 nroff(work);
166}
167
168nroff(cp)
169 char *cp;
170{
171 char cmdbuf[BUFSIZ];
172
173 if (nocr3)
174 sprintf(cmdbuf, "nroff -h -man %s", cp);
175 else
176 sprintf(cmdbuf, "nroff -h -man %s | %s | %s | %s",
177 cp, binpath(ssp), binpath(ul),
178 cflag ? binpath(cr3) : binpath(more));
179 system(cmdbuf);
180}
181
182troff(cp)
183 char *cp;
184{
185 char cmdbuf[BUFSIZ];
186
187 sprintf(cmdbuf, "%s -t -man %s %s | %s | %s -t", "/usr/bin/troff",
188 libpath(tmac/tmac.vcat), cp, libpath(vsort), binpath(vpr));
189 system(cmdbuf);
190}
191
192any(c, sp)
193 register int c;
194 register char *sp;
195{
196 register int d;
197
198 while (d = *sp++)
199 if (c == d)
200 return (1);
201 return (0);
202}