summaryrefslogtreecommitdiff
path: root/src/DrawResources/tdoc
blob: 969fa85494202e870b90bb9018ec0c34ce49997e (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/sh
# The next line is executed by /bin/sh, but not Tcl \
  exec tclsh $0 ${1+"$@"}

source $env(DRAWHOME)/Documentation.tcl

#
# format a documentation for troff
#

# proc ot manage keeping of lines together
proc keep {} {
    global theFile keeping
    if {! $keeping} {
	puts $theFile ".KS"
    }
    set keeping 1
}

proc endkeep {} {
    global theFile keeping
    if {$keeping} {
	puts $theFile ".KE"
    }
    set keeping 0
}

proc troffTitle {title level} {
    global theFile

    endkeep

    # set the header
    puts $theFile ".ds LH $title"

    # try to keep command titles with their synopsis
    if {$level == 3} keep

    puts $theFile ".NH $level"
    puts $theFile $title
    puts $theFile ".XS"
    for {set i 1} {$i <= $level} {incr i} {puts -nonewline $theFile "  "}
    puts $theFile $title
    puts $theFile ".XE"
}

proc putText {aText} {
    global theFile
    foreach line $aText {
	regsub -all {\\} $line "\\e" l
	puts $theFile $l
    }
}

proc troffSection {aSection aText} {
    global theFile ExNumber fill

    # check if text is empty
    set empty 1
    foreach line $aText {
	if {![regexp {^[ \t]*$} $line]} {
	    set empty 0
	    break
	}
    }
    if $empty return

    if {$aSection == ""} {
	if {$fill} {puts $theFile ".PP"} else {puts $theFile ".LD\n.R"}
	putText $aText
	if {! $fill} {puts $theFile ".DE"}
	return
    }

    switch $aSection {
		
	.Synopsis  {
	    puts $theFile ".LD\n.R"
	    putText $aText
	    puts $theFile ".DE"
	}
	
	.Purpose   {
	    puts $theFile ".B Purpose\n.PP"
	    putText $aText
	    # Synopsis and purpose are kept with command title
	    endkeep
	}
	
	.Example    {
	    endkeep
	    incr ExNumber
	    puts $theFile ".sp\n.B1\n.DS"         
	    for {set i 1} {$i <= 80} {incr i} {puts -nonewline $theFile " "}
	    puts $theFile " "
	    puts $theFile ".B \"Example $ExNumber\""
	    putText $aText
	    puts $theFile ".DE"
	    puts $theFile ".B2"
	}
	
	".See also" {
	    puts $theFile ".sp\n.B \"See also\"\n.PP"
	    putText $aText
	}
	
	.Warning    {
	    puts $theFile ".sp\n.B Warnings\n.PP"
	    putText $aText
	}
	
	.Text   {
	    if {$fill} {puts $theFile ".PP"} else {puts $theFile ".LD\n.R"}
	    putText $aText
	    if {! $fill} {puts $theFile ".DE"}
	}
	
	.Index      {
	    foreach word $aText {
		if {$word != ""} {puts $theFile ".IX $word"}
	    }
	}
    }
}

proc troffText {aText} {
	sectionText $aText troffSection
}

proc troff {} {
    global theFile texts ExNumber title
    set ExNumber 0

    puts $theFile ".RP"
    puts $theFile ".TL\n$title"
    puts $theFile ".AU\nThe CAS.CADE Software Factory"
    puts $theFile ".AI\nMatra-Datavision"

    if [info exists texts(Top)] {
	# Abstract
	puts $theFile ".AB"
	putText $texts(Top)
	puts $theFile ".AE"
	set text(Top) {}
    }

    # header and footer
    puts $theFile ".ds CH "
    puts $theFile ".ds RH $title"
    puts $theFile ".ds CF -%-"
    puts $theFile ".ds LF Copyright Matra-Datavision"

    dump troffTitle troffText

    puts $theFile ".ds RH Contents"
    puts $theFile ".bp\n.TL\nTable of contents\n.PX no"
}

# compare without case, used for sorting the index
proc cmp {s1 s2} {
    return [string compare [string tolower $s1] [string tolower $s2]]
}

proc processIndex {fileindex filetr} {
    # read the index file and create the index
    # Try to keep together the letter an the first line

    set f [open $fileindex r]
    set sindex {}
    while {[gets $f line] >= 0} {
	if [regexp {^(.*[^ 	])[ 	]*\.\.\. ([0-9]*)$} $line dummy word page] {
	    lappend sindex "$word $page"
	}
    }
    close $f
    set f [open $filetr w]
    puts $f ".TL\nIndex"
    puts $f ".2C\n.LD"
    set letter ""
    set count 0
    foreach line [lsort -command cmp $sindex] {
	set l [string toupper [string index $line 0]]
	if {$l != $letter} {
	    set letter $l
	    puts $f "\n.KS\n.LG 2\n.B $letter\n.NL\n"
	    set count 0
	}
	puts $f $line
	incr count
	if {$count == 1} {puts $f ".KE"}
    }
    close $f
}

#
# process arguments
#

if {$argc < 1} {
    puts "tdoc docfile title [nofill]"
    puts "format a doc file for printing in postscript"
    puts "if nofill text are not justified"
    exit
}

set file   [lindex $argv 0]
set title  "CAS.CADE Documentation"
if {$argc > 1} {set title [lindex $argv 1]}
set fill 1
if {$argc > 2} {set fill 0}


if [file readable $file] {
    readFile $file
    set file [file rootname $file]
    set filetr    F[pid]
    set fileout   O[pid]
    set fileindex I[pid]
    set theFile [open $filetr w]
    set keeping 0
    troff
    close $theFile
    exec troff -ms $filetr > $fileout 2> $fileindex
    set fileindextr IT[pid]
    processIndex $fileindex $fileindextr
    exec troff -ms $fileindextr >> $fileout
    exec /usr/lib/lp/postscript/dpost $fileout > $file.ps
    exec rm -f $filetr $fileout $fileindex $fileindextr
    puts "$file.ps created"
} else {
	puts "Cannot open $file for reading"
}