1#!/usr/bin/awk -f
2#
3#  Generate an index into a manual section by using find.
4#  Michael Hamilton <michael@actrix.gen.nz>
5#  Small changes - aeb, 980109
6#
7BEGIN {
8
9  OFS="";
10
11  if (ARGC != 3) {
12    print "Content-type: text/html\n\n";  
13    print "<head>";
14    print "<title>manwhatis - bad call</title>";
15    print "</head>\n<body>";
16    print "manwhatis: wrong number of arguments";
17    print "</body>";
18    exit;
19  }
20  cgipath = ARGV[1];
21  section = ARGV[2];
22
23  if (section !~ /^[1-8ln]$/ && section != "all") {
24    print "Content-type: text/html\n\n";  
25    print "<head>";
26    print "<title>Manual - Illegal section</title>";
27    print "<body>";
28    print "Illegal section number '" section "'." ;
29    print "Must be one of 1,2,3,4,5,6,7,8,l,n or all";
30    print "</body>";
31    exit;
32  }
33    
34  "echo $PPID" | getline pid;
35
36  if (cgipath ~ /lynxcgi/) {
37    cache_suffix = "l";
38  }
39  else {
40    cache_suffix = "h";
41  }
42
43  cache_dir  = "/var/man2html";
44  cache_file = "manindex" cache_suffix "-" section ".html";
45  cache = cache_dir "/" cache_file;
46  cache_tmp = cache "_" pid;
47  buffer_tmp = cache "_items_" pid;
48
49				# Find out the man path
50  "man -w" | getline man_path
51  man_path = man_path ":";
52  gsub(":", " ", man_path);
53				# See if anything is out of date.
54				# Check all man[1-8] dir dates vs cache date
55  if (section == "all") {
56    if (system("test -f " cache) == 0) {
57      cmd = "find " man_path " -maxdepth 1 -name 'man[1-8]' -newer " cache;
58      cmd | getline need_update;
59    }
60    else {
61      need_update = 1;
62    }
63  }
64  else {
65    if (system("test -f " cache) == 0) {
66      cmd = "find " man_path " -maxdepth 1 -name man" section " -newer " cache;
67      cmd | getline need_update;
68    }
69    else {
70      need_update = 1;
71    }
72  }
73
74  if (need_update != "") {
75    if (system("test -w " cache_dir "/.") != 0) {
76      print "Content-type: text/html\n\n";  
77      print "<head>";
78      print "<title>mansec - no cache</title>";
79      print "</head>\n<body>";
80      print "Sorry - cannot create index.";
81      print "No writable cache directory " cache_dir " exists.";
82      print "</body>";
83      exit;
84    }
85
86    sec_name[1] = "User Commands";
87    sec_name[2] = "System Calls";
88    sec_name[3] = "Library Functions";
89    sec_name[4] = "Special Files";
90    sec_name[5] = "File Formats";
91    sec_name[6] = "Games";
92    sec_name[7] = "Miscellany";
93    sec_name[8] = "Administration and Privileged Commands";
94    sec_name["all"] = "All available manual pages";
95    num_sections = 8;
96
97				# Print heading
98    print "Content-type: text/html\n\n" > cache_tmp;  
99    print "<html>\n<head>" > cache_tmp;
100    print "<title>Manual Pages - Names: " section ". " sec_name[section] "</title>"> cache_tmp;
101    print "</head>\n<body>" > cache_tmp;
102    print "<h1>Manual Pages - Page Names</h1>" > cache_tmp;
103    print "<h2>Section " section ": " sec_name[section] "</h2>" > cache_tmp;
104    
105    "hostname" | getline hostname;
106    "date" | getline date;
107    print hostname " (" date ")" > cache_tmp;
108    
109    if (section != "all") {
110      sec_sub_dir = "/man" section;
111    }
112    else {
113      sec_sub_dir = "/man*";    
114    }
115    gsub(" ", sec_sub_dir " ", man_path); 
116    
117    print "<p>Manual pages found under " man_path "." > cache_tmp;
118
119				# Find any man[1-8]/filenames
120    while ((("find " man_path " -follow -type f -printf '%f\n' | sort -f ") | getline manpage) > 0) {
121				# Check for new letter of alphabet
122      letter = tolower(substr(manpage,1,1));
123      if (letter != last_letter) {
124	last_letter = letter;
125	letter_index[++num_letters] = letter;
126				# Start a new alphabetic heading
127	print "<h2> <a name=\"", letter, "\">", toupper(letter), "</a></h2>" > buffer_tmp;
128				# Print out alphabetic quick index and other links
129      }
130				# Split page.n into "page" and "n" and generate an entry
131      sub(/[.]([zZ]|(gz))$/, "", manpage);
132      match(manpage, /[.][^.]+$/);      
133      title = substr(manpage, 1, RSTART - 1);
134      if (section != "all") {
135        print "<a href=\"" cgipath "/man2html?", section, "+", title, "\">", title, "(", substr(manpage, RSTART + 1), ")</a>" > buffer_tmp;
136      }
137      else {
138	sec = substr(manpage, RSTART + 1)
139	print "<a href=\"" cgipath "/man2html?", sec, "+", title, "\">", title, "(", sec, ")</a>" > buffer_tmp;
140      }
141    }
142
143    close(buffer_tmp);
144
145    print "<p>" > cache_tmp;
146
147				# Print out alphabetic quick index and other links
148    for (i = 1; i <= num_letters; i++) {
149      print "<a href=\"#" letter_index[i] "\">" toupper(letter_index[i]) "</a>" > cache_tmp;
150    }
151
152    print "<p><hr>" > cache_tmp;
153    print "<a href=\"" cgipath "/man2html\">Return to Main Contents</a>" > cache_tmp;
154
155    print "<p>Other sections:" > cache_tmp;
156    for (i=1; i<=num_sections; i++) {
157      if (i != section) {		# Dont print an entry for the section we are in
158	print "<a href=\"" cgipath "/mansec?" cgipath "+" i "\">" i ". " sec_name[i] "</a> " > cache_tmp;
159      }
160    }
161    print "<hr><p>" > cache_tmp;
162				# Print out the accumulated index entries
163    while ((getline < buffer_tmp) > 0) print > cache_tmp;
164    print "<hr><p>" > cache_tmp;
165				# Print out alphabetic quick index and other links
166    for (i = 1; i <= num_letters; i++) {
167      print "<a href=\"#" letter_index[i] "\">" toupper(letter_index[i]) "</a>" > cache_tmp;
168    }
169    print "<hr>" > cache_tmp;
170    print "<p><a href=\"" cgipath "/man2html\">Return to Main Contents</a>" > cache_tmp;
171    print "<p>Other sections:" > cache_tmp;
172    for (i=1; i<=num_sections; i++) {
173      if (i != section) {		# Dont print an entry for the section we are in
174	print "<a href=\"" cgipath "/mansec?" cgipath "+" i "\">" i ". " sec_name[i] "</a> " > cache_tmp;
175      }
176    }
177    print "</body>\n</html>" > cache_tmp;
178    system("/bin/mv " cache_tmp " " cache);
179    system("/bin/rm -f " buffer_tmp);
180  }
181  system("/bin/cat " cache);
182  exit;
183}
184