1#!/usr/bin/awk -f
2#
3#  Generate a whatis index into the manual pages by using find to
4#  locate all the whatis files.
5#  Michael Hamilton <michael@actrix.gen.nz>
6#  Small changes - aeb, 980109
7#
8BEGIN {
9
10  OFS="";
11
12  if (ARGC != 3) {
13    print "Content-type: text/html\n\n";  
14    print "<head>";
15    print "<title>manwhatis - bad call</title>";
16    print "</head>\n<body>";
17    print "manwhatis: wrong number of arguments";
18    print "</body>";
19    exit;
20  }
21  cgipath = ARGV[1];
22  section = ARGV[2];
23
24  if (section !~ /^[1-8ln]$/) {
25    print "Content-type: text/html\n\n";  
26    print "<head>";
27    print "<title>Manual - Illegal section</title>";
28    print "</head>\n<body>";
29    print "Illegal section number '" section "'." ;
30    print "Must be one of 1,2,3,4,5,6,7,8,l,n";
31    print "</body>";
32    exit;
33  }
34    
35  if (cgipath ~ /lynxcgi/) {
36    cache_suffix = "l";
37  }
38  else {
39    cache_suffix = "h";
40  }
41
42  cache_dir  = "/var/man2html";
43  cache_file = "whatis" cache_suffix "-" section ".html";
44  cache = cache_dir "/" cache_file;
45
46
47				# Find out the man path
48  "man -w" | getline man_path
49  gsub(":", " ", man_path);
50				# See if anything is out of date.
51  if (system("test -f " cache) == 0) {
52    cmd = "find " man_path " -maxdepth 1 -name whatis -newer " cache;
53    cmd | getline need_update;
54  }
55  else {
56    need_update = 1;
57  }
58
59  if (need_update != "") {
60
61    if (system("test -w " cache_dir "/.") != 0) {
62      print "Content-type: text/html\n\n";  
63      print "<head>";
64      print "<title>manwhatis - no cache</title>";
65      print "</head>\n<body>";
66      print "Sorry - cannot create index.";
67      print "No writable cache directory " cache_dir " exists.";
68      print "</body>";
69      exit;
70    }
71
72    "echo $PPID" | getline pid;
73
74    cache_tmp  = cache "_" pid;
75    sort_tmp   = cache_dir "/manwhatis_tmp_" pid ;
76    buffer_tmp = cache_dir "/manwhatis_tmp2_" pid;
77
78    sec_name[1] = "User Commands";
79    sec_name[2] = "System Calls";
80    sec_name[3] = "Library Functions";
81    sec_name[4] = "Special Files";
82    sec_name[5] = "File Formats";
83    sec_name[6] = "Games";
84    sec_name[7] = "Miscellany";
85    sec_name[8] = "Administration and Privileged Commands";
86    num_sections = 8;
87				# Print heading
88    print "Content-type: text/html\n\n" > cache_tmp;  
89    print "<html>\n<head>" > cache_tmp;
90    print "<title>Manual Pages - Names and Descriptions: " section ". " sec_name[section] "</title>" > cache_tmp;
91
92    print "</head>\n<body>" > cache_tmp;
93    print "<h1>Manual Pages - Names and Descriptions</h1>" > cache_tmp;
94    print "<h1>Section " section ": " sec_name[section] "</h1>" > cache_tmp;
95    "hostname" | getline hostname;
96    "date" | getline date;
97    print hostname " (" date ")" > cache_tmp;
98				# Find out the man path 
99    "man -w" | getline;
100    $1 = $1 ":";
101    gsub(":", " ", $1); 
102
103    find_cmd = "find " man_path " -maxdepth 1 -name whatis -printf '%p '";
104    find_cmd | getline whatis_files;
105    close(find_cmd);
106
107    if (whatis_files == "") {
108      print "Content-type: text/html\n\n";
109      print "<head>";
110      print "<title>Manwhatis - Error updating index</title>";
111      print "</head>\n<body>";
112      print "Unable to find whatis files - Sorry."
113      print "</body>";
114      exit;
115    }
116	     # Try to parse valid entries - those that contain ([0-9])
117	     # Note that egrep is sometimes in /bin, sometimes in /usr/bin
118    extract_cmd = "egrep -h '\\(" section "[A-Za-z]*\\)' " whatis_files ;
119
120    print "<br>Manual pages referenced in " whatis_files "<p>" > cache_tmp;
121
122    # Note that sort sometimes lives in /bin and sometimes in /usr/bin
123    sort_cmd = "sort -f >> " sort_tmp;
124
125    while ( (extract_cmd | getline) > 0 ) { 
126      if (bracket_pos = index($0, "(")) {
127	sec_full_num = substr($0, bracket_pos + 1, index($0, ")") - bracket_pos - 1); 
128	names = substr($0, 1, bracket_pos - 2);
129				# Get rid of blanks and commas.
130	n = split(names, name_list, " *, *");
131	description = substr($0, bracket_pos + length(sec_full_num) + 2);
132				# Get rid of AT&T
133	gsub("&", "\&amp;", description);
134				# Generate a entry for each name
135	for (i = 1; i <= n; i++) {
136	  print name_list[i] " " sec_full_num " " name_list[1] " / " description | sort_cmd;
137	}
138      }
139    }
140    close(extract_cmd);
141    close(sort_cmd);
142
143    while ((getline < sort_tmp) > 0) { 
144
145      letter = tolower(substr($1,1,1));
146      if (letter != last_letter) { 
147	if (last_letter) {
148	  print "</dl><p>" > buffer_tmp;
149	}
150	last_letter = letter;
151	letter_index[++num_letters] = letter;
152				# Terminate list, start a new one
153
154	print "<h2> <a name=\"", letter, "\">", toupper(letter), "</a></h2>\n<dl>" > buffer_tmp ;
155      }
156				# Generate a <dt> for the name
157      if ($3 != last_file || $1 != last_name) {	# Don't repeat the same entry link.
158	print "<dt><a href=\"" cgipath "/man2html?", $2, "+", $3, "\">", $1, "(", $2, ")", "</a>" > buffer_tmp;
159	last_file = $3;
160	last_name = $1;
161      }
162      print "<dd>", substr($0, match($0, "/") + 1) > buffer_tmp;
163    }
164				# Finish off last list
165
166    print "\n</dl><p>" > buffer_tmp;
167    close(buffer_tmp);
168
169    system("/bin/rm " sort_tmp);
170
171				# Print out alphabetic quick index and other links
172    for (i = 1; i <= num_letters; i++) {
173      print "<a href=\"#" letter_index[i] "\">" toupper(letter_index[i]) "</a>" > cache_tmp;
174    }
175    print "<hr>" > cache_tmp;
176    print "<a href=\"" cgipath "/man2html\">Return to Main Contents</a>" > cache_tmp;
177    
178    print "<p>Other sections:" > cache_tmp;
179    for (i=1; i<=num_sections; i++) { 
180      if (i != section) {	# Dont print an entry for the section we are in
181	print "<a href=\"" cgipath "/manwhatis?" cgipath "+" i "\">" i ". " sec_name[i] "</a> " > cache_tmp;
182      }
183    }
184    print "<hr><p>" > cache_tmp;
185				# Print out the accumulated contents entries
186    while ((getline < buffer_tmp) > 0) print > cache_tmp;
187    print "<hr><p>" > cache_tmp;
188
189    for (i = 1; i <= num_letters; i++) {
190      print "<a href=\"#" letter_index[i] "\">" toupper(letter_index[i]) "</a>" > cache_tmp;
191    }
192    print "<hr>" > cache_tmp;
193    print "<p><a href=\"" cgipath "/man2html\">Return to Main Contents</a>" > cache_tmp;
194    
195    print "<p>Other sections:" > cache_tmp;
196    for (i=1; i<=num_sections; i++) { 
197      if (i != section) {	# Dont print an entry for the section we are in
198	print "<a href=\"" cgipath "/manwhatis?" cgipath "+" i "\">" i ". " sec_name[i] "</a> " > cache_tmp;
199      }
200    }
201    print "</body>" > cache_tmp;
202    print "</html>" > cache_tmp;
203    system("/bin/mv " cache_tmp " " cache);
204    system("/bin/rm " buffer_tmp);
205  }
206  system("/bin/cat " cache);
207  exit;
208}
209