File: manwhatis

package info (click to toggle)
man2html 1.5-23.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 344 kB
  • ctags: 218
  • sloc: ansic: 3,889; awk: 241; sh: 224; makefile: 124
file content (160 lines) | stat: -rw-r--r-- 5,300 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/awk -f
#
#  Generate a whatis index into the manual pages by using find to 
#  locate all the whatis files.
#  Michael Hamilton <michael@actrix.gen.nz>
#
BEGIN {

  OFS="";
  section = ARGV[1];
  "echo $PPID" | getline pid;

  if (section !~ /^[0-9]$/) {
    print "Content-type: text/html";  
    print "Status: 400 Bad Request\n";  
    print "<head>";
    print "<title>Manual - Illegal section</title>";
    print "</head>\n<body>";
    print "Illegal section number '" section "'." ;
    print "Must be 0..9 or all";
    print "</body>";
    exit;
  }
    
  cache_dir  = "/var/cache/man2html";
  cache_file = "whatis-" section ".html";
  cache = cache_dir "/" cache_file;
				# Find out the man path
  "/usr/bin/manpath -cq" | getline man_path
  gsub(":", " ", man_path);
				# See if anything is out of date.
  if (system("test -f " cache) == 0) {
    cmd = "/usr/bin/find " man_path " -maxdepth 1 -name index.db -newer " cache;
    cmd | getline need_update;
  }
  else {
    need_update = 1;
  }

  if (need_update != "") {

    cache_tmp  = cache "_" pid;
    sort_tmp   = cache_dir "/manwhatis_tmp_" pid ;
    buffer_tmp = cache_dir "/manwhatis_tmp2_" pid;

    sec_name[1] = "User Commands";
    sec_name[2] = "System Calls";
    sec_name[3] = "Library Functions";
    sec_name[4] = "Special Files";
    sec_name[5] = "File Formats";
    sec_name[6] = "Games";
    sec_name[7] = "Miscellany";
    sec_name[8] = "Administration and Privileged Commands";
    
				# Print heading
    print "<html>\n<head>" > cache_tmp;
    print "<title>Manual Pages - Names and Descriptions: " section ". " sec_name[section] "</title>" > cache_tmp;

    print "</head>\n<body>" > cache_tmp;
    print "<h1>Manual Pages - Names and Descriptions</h1>" > cache_tmp;
    print "<h1>Section " section ": " sec_name[section] "</h1>" > cache_tmp;
				# Find out the man path 
				# Try to parse valid entries - those that contain ([0-9])
    extract_cmd =  "whatis -w '*'| egrep -h '\\(" section "[A-Za-z]*\\)' " ;

    print "Manual pages referenced<p>" > cache_tmp;

    sort_cmd = "/usr/bin/sort -f > " sort_tmp;
    while ( (extract_cmd | getline) > 0 ) { 
      if (bracket_pos = index($0, "(")) {
	sec_full_num = substr($0, bracket_pos + 1, index($0, ")") - bracket_pos - 1); 
	names = substr($0, 1, bracket_pos - 2);
				# Get rid of blanks and commas.
	n = split(names, name_list, " *, *");
	description = substr($0, bracket_pos + length(sec_full_num) + 2);
				# Generate a entry for each name
	for (i = 1; i <= n; i++) {
		j=0;
		do
		{
			j++;
			letter = tolower(substr($1,j,1))
		} while(s<length($1) && (match(letter,"_|,|\(|\)|:")==1));
	  print letter " " name_list[i] " " sec_full_num " " name_list[1] " / " description | sort_cmd;
	}
      }
    }
    close(extract_cmd);
    close(sort_cmd);
    while ((getline < sort_tmp) > 0) {
      letter = $1
      if (letter != last_letter) { 
	if (last_letter) {
	  print "</dl><p>" > buffer_tmp;
	}
	last_letter = letter;
	letter_index[++num_letters] = letter;
				# Terminate list, start a new one

	print "<h2> <a name=\"", letter, "\">", toupper(letter), "</a></h2>\n<dl>" > buffer_tmp ;
      }
				# Generate a <dt> for the name
      if ($4 != last_file || $2 != last_name) {	# Don't repeat the same entry link.
	print "<dt><a href=\"http:/cgi-bin/man2html?", $4, "+", $3, "\">", $2, "(", $3, ")", "</a>" > buffer_tmp;
	last_file = $4;
	last_name = $2;
      }
      print "<dd>", substr($0, match($0, "/") + 1) > buffer_tmp;
    }
				# Finish off last list
    print "\n</dl><p>" > buffer_tmp;
    close(buffer_tmp);

    system("rm " sort_tmp);

				# Print out alphabetic quick index and other links
    for (i = 1; i <= num_letters; i++) {
      print "<a href=\"#" letter_index[i] "\">" toupper(letter_index[i]) "</a>" > cache_tmp;
    }
    print "<hr>" > cache_tmp;
    print "<a href=\"http:/cgi-bin/man2html\">Return to Main Contents</a>" > cache_tmp;
    
    print "<p>Other sections:" > cache_tmp;
    for (i=1; i<=8; i++) { 
      if (i != section) {	# Dont print an entry for the section we are in
	print "<a href=\"http:/cgi-bin/manwhatis?" i "\">" i ". " sec_name[i] "</a> " > cache_tmp;
      }
    }
    print "<hr><p>" > cache_tmp;
				# Print out the accumulated contents entries
    while ((getline < buffer_tmp) > 0) print > cache_tmp;
    print "<hr><p>" > cache_tmp;

    for (i = 1; i <= num_letters; i++) {
      print "<a href=\"#" letter_index[i] "\">" toupper(letter_index[i]) "</a>" > cache_tmp;
    }
    print "<hr>" > cache_tmp;
    print "<p><a href=\"http:/cgi-bin/man2html\">Return to Main Contents</a>" > cache_tmp;
    
    print "<p>Other sections:" > cache_tmp;
    for (i=1; i<=8; i++) { 
      if (i != section) {	# Dont print an entry for the section we are in
	print "<a href=\"http:/cgi-bin/manwhatis?" i "\">" i ". " sec_name[i] "</a> " > cache_tmp;
      }
    }
    print "</body>" > cache_tmp;
    print "</html>" > cache_tmp;
    system("/bin/mv " cache_tmp " " cache);
    system("/bin/rm " buffer_tmp);
  }
  print "Content-type: text/html";
  cmd = "ls -la " cache
  cmd | getline;
  print "Content-length: " $5;
  cmd = "unset LANG ; unset LC_TIME ; date -d \"" $6 " " $7 " " $8 "\" -Ru"
  cmd | getline
  print "Last-modified: " $0 "\n";
  system("/bin/cat " cache);
  exit;
}