Debian Codegraph @

tools/man2html1.tcl

raw
1 # man2html1.tcl --
2 #
3 # This file defines procedures that are used during the first pass of the
4 # man page to html conversion process. It is sourced by h.tcl.
5 #
6 # Copyright (c) 1996 Sun Microsystems, Inc.
7
8 # Global variables used by these scripts:
9 #
10 # state - state variable that controls action of text proc.
11 #
12 # curFile - tail of current man page.
13 #
14 # file - file pointer; for both xref.tcl and contents.html
15 #
16 # NAME_file - array indexed by NAME and containing file names used
17 # for hyperlinks.
18 #
19 # KEY_file - array indexed by KEYWORD and containing file names used
20 # for hyperlinks.
21 #
22 # lib - contains package name. Used to label section in contents.html
23 #
24 # inDT - in dictionary term.
25
26
27 # text --
28 #
29 # This procedure adds entries to the hypertext arrays NAME_file
30 # and KEY_file.
31 #
32 # DT: might do this: if first word of $dt matches $name and [llength $name==1]
33 # and [llength $dt > 1], then add to NAME_file.
34 #
35 # Arguments:
36 # string - Text to index.
37
38 proc text string {
39 global state curFile NAME_file KEY_file inDT
40
41 switch $state {
42 NAME {
43 foreach i [split $string ","] {
44 lappend NAME_file([string trim $i]) $curFile
45 }
46 }
47 KEY {
48 foreach i [split $string ","] {
49 lappend KEY_file([string trim $i]) $curFile
50 }
51 }
52 DT -
53 OFF -
54 DASH {}
55 default {
56 puts stderr "text: unknown state: $state"
57 }
58 }
59 }
60
61
62 # macro --
63 #
64 # This procedure is invoked to process macro invocations that start
65 # with "." (instead of ').
66 #
67 # Arguments:
68 # name - The name of the macro (without the ".").
69 # args - Any additional arguments to the macro.
70
71 proc macro {name args} {
72 switch $name {
73 SH - SS {
74 global state
75
76 switch $args {
77 NAME {
78 if {$state eq "INIT"} {
79 set state NAME
80 }
81 }
82 DESCRIPTION {set state DT}
83 INTRODUCTION {set state DT}
84 KEYWORDS {set state KEY}
85 default {set state OFF}
86 }
87
88 }
89 TP {
90 global inDT
91 set inDT 1
92 }
93 TH {
94 global lib state inDT
95 set inDT 0
96 set state INIT
97 if {[llength $args] != 5} {
98 set args [join $args " "]
99 puts stderr "Bad .TH macro: .$name $args"
100 }
101 set lib [lindex $args 3] ;# Tcl or Tk
102 }
103 }
104 }
105
106
107 # dash --
108 #
109 # This procedure is invoked to handle dash characters ("\-" in
110 # troff). It only function in pass1 is to terminate the NAME state.
111 #
112 # Arguments:
113 # None.
114
115 proc dash {} {
116 global state
117 if {$state eq "NAME"} {
118 set state DASH
119 }
120 }
121
122
123 # newline --
124 #
125 # This procedure is invoked to handle newlines in the troff input.
126 # It's only purpose is to terminate a DT (dictionary term).
127 #
128 # Arguments:
129 # None.
130
131 proc newline {} {
132 global inDT
133 set inDT 0
134 }
135
136
137 # initGlobals, tab, font, char, macro2 --
138 #
139 # These procedures do nothing during the first pass.
140 #
141 # Arguments:
142 # None.
143
144 proc initGlobals {} {}
145 proc tab {} {}
146 proc font type {}
147 proc char name {}
148 proc macro2 {name args} {}
149
150
151 # doListing --
152 #
153 # Writes an ls like list to a file. Searches NAME_file for entries
154 # that match the input pattern.
155 #
156 # Arguments:
157 # file - Output file pointer.
158 # pattern - glob style match pattern
159
160 proc doListing {file pattern} {
161 global NAME_file
162
163 set max_len 0
164 foreach name [lsort [array names NAME_file]] {
165 set ref $NAME_file($name)
166 if [string match $pattern $ref] {
167 lappend type $name
168 if {[string length $name] > $max_len} {
169 set max_len [string length $name]
170 }
171 }
172 }
173 if [catch {llength $type} ] {
174 puts stderr " doListing: no names matched pattern ($pattern)"
175 return
176 }
177 incr max_len
178 set ncols [expr {90/$max_len}]
179 set nrows [expr {int(ceil([llength $type] / double($ncols)))} ]
180
181 # ? max_len ncols nrows
182
183 set index 0
184 foreach f $type {
185 lappend row([expr {$index % $nrows}]) $f
186 incr index
187 }
188
189 puts -nonewline $file "<PRE>"
190 for {set i 0} {$i<$nrows} {incr i} {
191 foreach name $row($i) {
192 set str [format "%-*s" $max_len $name]
193 regsub $name $str "<A HREF=\"$NAME_file($name).html\">$name</A>" str
194 puts -nonewline $file $str
195 }
196 puts $file {}
197 }
198 puts $file "</PRE>"
199 }
200
201
202 # doContents --
203 #
204 # Generates a HTML contents file using the NAME_file array
205 # as its input database.
206 #
207 # Arguments:
208 # file - name of the contents file.
209 # packageName - string used in the title and sub-heads of the HTML
210 # page. Normally name of the package without version
211 # numbers.
212
213 proc doContents {file packageName} {
214 global footer
215
216 set file [open $file w]
217
218 puts $file "<HTML><HEAD><TITLE>$packageName Manual</TITLE></HEAD><BODY>"
219 puts $file "<H3>$packageName</H3>"
220 doListing $file "*.1"
221
222 puts $file "<HR><H3>$packageName Commands</H3>"
223 doListing $file "*.n"
224
225 puts $file "<HR><H3>$packageName Library</H3>"
226 doListing $file "*.3"
227
228 puts $file $footer
229 puts $file "</BODY></HTML>"
230 close $file
231 }
232
233
234 # do --
235 #
236 # This is the toplevel procedure that searches a man page
237 # for hypertext links. It builds a data base consisting of
238 # two arrays: NAME_file and KEY file. It runs the man2tcl
239 # program to turn the man page into a script, then it evals
240 # that script.
241 #
242 # Arguments:
243 # fileName - Name of the file to scan.
244
245 proc do fileName {
246 global curFile
247 set curFile [file tail $fileName]
248 set file stdout
249 puts " Pass 1 -- $fileName"
250 flush stdout
251 if [catch {eval [exec man2tcl [glob $fileName]]} msg] {
252 global errorInfo
253 puts stderr $msg
254 puts "in"
255 puts $errorInfo
256 exit 1
257 }
258 }