File: mansearch

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 (172 lines) | stat: -rw-r--r-- 4,386 bytes parent folder | download | duplicates (2)
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
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/sh
#
#  Interface to a glimpse search of the man pages.
#  Michael Hamilton <michael@actrix.gen.nz>
#
QUOTE="'"
export QUOTE

type glimpse > /dev/null 2> /dev/null || {
	cat <<-EOF
	Content-type: text/html

	<title>Glimpse not installed</title>
	<body>
	<h1>Glimpse not installed</h1>
	Search is only enabled if the <b>Glimpse</b> package is installed.
	You may <a
href="http://www.debian.org/Packages/unstable/utils/glimpse.html">download</a> it from the
	<a href="http://www.debian.org">Debian</a> site.
	</body>
	EOF
	exit 0
}

exec awk '
function removeopts(string) {
  gsub(/^[ \t]/, "", string);	# Remove leading spaces
  gsub(/[ \t]$/, "", string);	# Remove trailing spaces
  gsub(/[ \t\\];/, ";", string);	# Remove spaces before ;
  gsub(/[ \t];/, ",", string);  # Remove spaces before ,
  # print "---" string "---" > "/dev/stderr"
  while (match(string, /^-[FLBwk1-8]/)) {
    if (match(string, /^-[FL]( |.)[^ \t]+[ \t]+/)) { # Option with arg
      options = options " " substr(string, RSTART, RLENGTH);
      string = substr(string, RSTART + RLENGTH);
    } 
    else if (match(string, /^-[Bwk1-8][ \t]+/)) { # Option without arg
      options = options " " substr(string, RSTART, RLENGTH);
      string = substr(string, RSTART + RLENGTH);
    }
    else if (match(string, /^-[^ \t]/)) { # Remove it
      string = substr(string, RSTART + RLENGTH);   
    }
  }
  return string;
}

BEGIN {

  quote = ENVIRON["QUOTE"];
  truncate_at = 11;		# Single page display match limit.

  glimpse_cmd = "glimpse -z -H /var/cache/man2html -y -W -i "

  for (i = 1; i < ARGC; i++) {
    # print "---" i "---" ARGV[i] "---" > "/dev/stderr"
    string = string " " ARGV[i];
  }
				# Have to be carefull to single quote this
				# string later.
  string = removeopts(string);

  gsub(/[^a-zA-Z0-9-_+ \t\/@%;,$*|]/, " ", string);

  print "Content-type: text/html";
  print "";

  searchdocument = "/usr/doc/man2html/html/mansearch.html";
  if (!string) {
    while (getline line < searchdocument) {
      print line;
    }
    exit;
  }

  print "<HTML>";
  print "<HEAD>";
  print "<TITLE>Manual Pages - Search Results: " string "</TITLE>";
  print "</HEAD>";
  print "<BODY>";
  
  print "<H1>Manual Pages - Search Results</H1>";
  print "<H2>Target text: " options " " string "</H2>";

  print "<A HREF=\"http:/cgi-bin/mansearch\">";
  print "Perform another search";
  print "</A><BR>";
  print "<A HREF=\"http:/cgi-bin/man2html\">";
  print "Return to Main Contents";
  print "</A>";

  print "<HR>";  

  print "<DL>";
				# Unless you like being hacked, the single
				# forward quotes are most important.
  cmd = glimpse_cmd " " options " " quote string quote " 2>&1" ;
  #print cmd > "/dev/stderr"

  while (cmd | getline matchline) {
    if (split(matchline, part, ":") == 1) {
      continue;
    }
    else {
      fullname = part[1];
    }

    if (fullname == "glimpse") {
      print "<DT><B>"fullname"</B>:";
    }
    else if (fullname != last_fullname) {
      mcount++;
      tcount = 0;
      last_fullname = fullname ;
      last_text = "";

      if (match(fullname, ".*/")) {
	dirname = substr(fullname, 1, RLENGTH);
	filename = substr(fullname, RLENGTH + 1);
	if (dirname != last_dirname) {
	  last_dirname = dirname;
          print "</DL>";
	  print "<H3>Location: " dirname "</H3>";
	  print "<DL>";
	}
      }
      else {
	filename = fullname;
      }

      if (match(filename, /\.[^.]+$/)) {
	ref = substr(filename, 1, RSTART - 1) "+" substr(filename, RSTART + 1);
      }
      else {
	ref = filename;
      }
      print "<DT> <a href=\"http:/cgi-bin/man2html" fullname "\">";
      textname = filename;
      sub(/\.(gz)|Z|z$/, "", textname);
      sub(/\./, "(", textname);
      textname = textname ")";
      print textname;
      print "</A>";	
    }

    text = substr(matchline, length(fullname) + 2);
    tcount++;
    if (tcount < truncate_at) {
      sub(/^ *.[^ ]+ /, "", text);
      sub(/ +$/, "", text);
      gsub(/\\f./,    "", text);
      gsub(/\\&/,     "", text);
      gsub(/\\/,      "", text);
      print "<DD>" text;
    }
    else if (tcount == truncate_at) {
      print "<DD> <I>...additional matches not shown.</I>";
    }
  }

  print "</DL>";
  if (mcount == 0) {
    print "No matches found.";
  }
  else {
    print "<HR>\n<P>" mcount " matches found."
  }
  print "</BODY>";
  print "</HTML>";
  exit;    
}' "$@"