This file is indexed.

/usr/share/tcltk/tcllib1.17/doctools2toc/container.tcl is in tcllib 1.17-dfsg-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# doctoc.tcl --
#
#	Implementation of doctoc objects for Tcl. v2.
#
# Copyright (c) 2009 Andreas Kupries <andreas_kupries@sourceforge.net>
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# 
# RCS: @(#) $Id: container.tcl,v 1.2 2009/11/15 05:50:03 andreas_kupries Exp $

# Each object manages one table of contents, with methods to add and
# remove entries and divisions, singly, or in bulk. The bulk methods
# accept various forms of textual serializations, among them text
# using the doctoc markup language.

# ### ### ### ######### ######### #########
## Requisites

package require Tcl 8.4
package require doctools::toc::structure
package require snit
package require struct::tree

# ### ### ### ######### ######### #########
## API

snit::type ::doctools::toc {

    # Concepts:
    # - A table of contents consists of an ordered set of elements,
    #   references and divisions.
    # - Both type of elements within the table are identified by their
    #   label.
    # - A reference has two additional pieces of information,
    #   the id of the document it references, and a textual description.
    # - A division may have the id of a document.
    # - The main data of a division is an ordered set of elements,
    #   references and divisions.
    # - Both type of elements within the division are identified by
    #   their label.
    # - The definitions above define a tree of elements, with
    #   references as leafs, and divisions as the inner nodes.
    # - Regarding identification, the full label of each element is
    #   the list of per-node labels on the path from the root of the
    #   tree to the element itself.

    # ### ### ### ######### ######### #########
    ## Options

    ## None

    # ### ### ### ######### ######### #########
    ## Methods

    constructor {} {
	install mytree using struct::tree ${selfns}::T
	# Root is a fake division
	set myroot [$mytree rootname]
	$mytree set $myroot type division
	$mytree set $myroot label {}
	$mytree set $myroot labelindex {}
	return
    }

    # Default destructor.

    # ### ### ### ######### ######### #########

    method invalidate {} {
	array unset mytoc *
	return
    }

    # ### ### ### ######### ######### #########

    method title {{text {}}} {
	if {[llength [info level 0]] == 6} {
	    set mytitle $text
	}
	return $mytitle
    }

    method label {{text {}}} {
	if {[llength [info level 0]] == 6} {
	    set mylabel $text
	    $mytree set $myroot label $text
	}
	return $mylabel
    }

    method exporter {{object {}}} {
	# TODO :: unlink/link change notification callbacks on the
	# config/include components so that we can invalidate our
	# cache when the settings change.

	if {[llength [info level 0]] == 6} {
	    set myexporter $object
	}
	return $myexporter
    }

    method importer {{object {}}} {
	if {[llength [info level 0]] == 6} {
	    set myimporter $object
	}
	return $myimporter
    }

    # ### ### ### ######### ######### #########
    ## Direct manipulation of the table of contents.

    method {+ reference} {pid label docid desc} {
	CheckDiv $pid
	if {$docid eq {}} {
	    return -code error "Illegal empty document reference for reference entry"
	}

	array set l [$mytree get $pid labelindex]
	if {[info exists l($label)]} {
	    return -code error "Redefinition of label '$label' in '[$self full-label $pid]'"
	}

	set new [$mytree insert $pid end]
	set l($label) $new
	$mytree set $pid labelindex [array get l]

	$mytree set $new type        reference
	$mytree set $new label       $label
	$mytree set $new document    $docid
	$mytree set $new description $desc

	array unset mytoc *
	return $new
    }

    method {+ division} {pid label {docid {}}} {
	CheckDiv $pid

	array set l [$mytree get $pid labelindex]
	if {[info exists l($label)]} {
	    return -code error "Redefinition of label '$label' in '[$self full-label $pid]'"
	}

	set new [$mytree insert $pid end]
	set l($label) $new
	$mytree set $pid labelindex [array get l]

	$mytree set $new type  division
	$mytree set $new label $label
	if {$docid ne {}} {
	    $mytree set $new document $docid
	}
	$mytree set $new labelindex {}

	array unset mytoc *
	return $new
    }

    method remove {id} {
	Check $id
	if {$id eq $myroot} {
	    return -code error {Unable to remove root}
	}
	set pid   [$mytree parent $id]
	set label [$mytree get $id label]

	array set l [$mytree get $pid labelindex]
	unset l($label)
	$mytree set $pid labelindex [array get l]
	$mytree delete $id

	array unset mytoc *
	return
    }

    # ### ### ### ######### ######### #########

    method up {id} {
	Check $id
	return [$mytree parent $id]
    }

    method next {id} {
	Check $id
	set n [$mytree next $id]
	if {$n eq {}} { set n [$mytree parent $id] }
	return $n
    }

    method prev {id} {
	Check $id
	set n [$mytree previous $id]
	if {$n eq {}} { set n [$mytree parent $id] }
	return $n
    }

    method child {id label args} {
	CheckDiv $id
	# Find the id of the element with the given labels, in the
	# parent element id.
	foreach label [linsert $args 0 $label] {
	    array set l [$mytree get $id labelindex]
	    if {![info exists l($label)]} {
		return -code error "Bad label '$label' in '[$self full-label $id]'"
	    }
	    set id $l($label)
	    unset l
	}
	return $id
    }

    method element {args} {
	if {![llength $args]} { return $myroot }
	# 8.5: $self child $myroot {*}$args
	return [eval [linsert $args 0 $self child $myroot]]
    }

    method children {id} {
	CheckDiv $id
	return [$mytree children $id]
    }

    # ### ### ### ######### ######### #########

    method type {id} {
	Check $id
	return [$mytree get $id type]
    }

    method full-label {id} {
	Check $id
	set result {}
	foreach node [struct::list reverse [lrange [$mytree ancestors $id] 0 end-1]] {
	    lappend result [$mytree get $node label]
	}
	lappend result [$mytree get $id label]

	return $result
    }

    method elabel {id {newlabel {}}} {
	Check $id
	set thelabel [$mytree get $id label]
	if {
	    ([llength [info level 0]] == 7) &&
	    ($newlabel ne $thelabel)
	} {
	    # Handle only calls which change the label

	    set parent [$mytree parent $id]
	    array set l [$mytree get $parent labelindex]

	    if {[info exists l($newlabel)]} {
		return -code error "Redefinition of label '$newlabel' in '[$self full-label $parent]'"
	    }

	    # Copy node information and re-label.
	    set   l($newlabel) $l($thelabel)
	    unset l($thelabel)
	    $mytree set $id label $newlabel
	    $mytree set $parent labelindex [array get l]

	    if {$id eq $myroot} {
		set mylabel $newlabel
	    }

	    set thelabel $newlabel
	}
	return $thelabel
    }

    method description {id {newdesc {}}} {
	Check $id
	if {[$mytree get $id type] eq "division"} {
	    return -code error "Divisions have no description"
	}
	set thedescription [$mytree get $id description]
	if {
	    ([llength [info level 0]] == 7) &&
	    ($newdesc ne $thedescription)
	} {
	    # Handle only calls which change the description
	    $mytree set $id description $newdesc

	    set thedescription $newdesc
	}
	return $thedescription
    }

    method document {id {newdocid {}}} {
	Check $id
	set thedocid {}
	catch {
	    set thedocid [$mytree get $id document]
	}
	if {
	    ([llength [info level 0]] == 7) &&
	    ($newdocid ne $thedocid)
	} {
	    # Handle only calls which change the document
	    if {$newdocid eq {}} {
		if {[$mytree get $id type] eq "division"} {
		    $mytree unset $id document
		} else {
		    return -code error "Illegal to unset document reference in reference entry"
		}
	    } else {
		$mytree set $id document $newdocid
	    }
	    set thedocid $newdocid
	}
	return $thedocid
    }

    # ### ### ### ######### ######### #########
    ## Public methods. Bulk loading and merging.

    method {deserialize =} {data {format {}}} {
	# Default format is the regular toc serialization
	if {$format eq {}} {
	    set format serial
	}

	if {$format ne "serial"} {
	    set data [$self Import $format $data]
	    # doctools::toc::structure verify-as-canonical $data
	    # ImportSerial verifies.
	}

	$self ImportSerial $data
	return
    }

    method {deserialize +=} {data {format {}}} {
	# Default format is the regular toc serialization
	if {$format eq {}} {
	    set format serial
	}

	if {$format ne "serial"} {
	    set data [$self Import $format $data]
	    # doctools::toc::structure verify-as-canonical $data
	    # merge or ImportSerial verify the structure.
	}

	set data [doctools::toc::structure merge [$self serialize] $data]
	# doctools::toc::structure verify-as-canonical $data
	# ImportSerial verifies.

	$self ImportSerial $data
	return
    }

    # ### ### ### ######### ######### #########

    method serialize {{format {}}} {
	# Default format is the regular toc serialization
	if {$format eq {}} {
	    set format serial
	}

	# First check the cache for a remebered representation of the
	# toc for the chosen format, and return it, if such is known.

	if {[info exists mytoc($format)]} {
	    return $mytoc($format)
	}

	# If there is no cached representation we have to generate it
	# from it from our internal representation.

	if {$format eq "serial"} {
	    return [$self GenerateSerial]
	} else {
	    return [$self Generate $format]
	}

	return -code error "Internal error, reached unreachable location"
    }

    # ### ### ### ######### ######### #########
    ## Internal methods

    proc Check {id} {
	upvar 1 mytree mytree
	if {![$mytree exists $id]} {
	    return -code error "Bad toc element handle '$id'"
	}
	return
    }

    proc CheckDiv {id} {
	upvar 1 mytree mytree
	Check $id
	if {[$mytree get $id type] ne "division"} {
	    return -code error "toc element handle '$id' does not refer to a division"
	}
    }

    method GenerateSerial {} {
	# We can generate the list serialization easily from the
	# internal representation.

	# Construct result
	set serial [list doctools::toc \
			[list \
			     items [$self GenerateDivision $myroot] \
			     label $mylabel \
			     title $mytitle]]

	# This is just present to assert that the code above creates
	# correct serializations.
	doctools::toc::structure verify-as-canonical $serial

	set mytoc(serial) $serial
	return $serial
    }

    method GenerateDivision {root} {
	upvar 1 mytree mytree
	set div {}
	foreach id [$mytree children $root] {
	    set etype [$mytree get $id type]
	    set edata {}
	    switch -exact -- $etype {
		reference {
		    lappend edata \
			desc  [$mytree get $id description] \
			id    [$mytree get $id document] \
			label [$mytree get $id label]
		}
		division {
		    if {[$mytree keyexists $id document]} {
			lappend edata id [$mytree get $id document]
		    }
		    lappend edata \
			items [$self GenerateDivision $id] \
			label [$mytree get $id label]
		}
	    }
	    lappend div [list $etype $edata]
	}
	return $div
    }

    method Generate {format} {
	if {$myexporter eq {}} {
	    return -code error "Unable to export from \"$format\", no exporter configured"
	}
	set res [$myexporter export object $self $format]
	set mytoc($format) $res
	return $res
    }

    method ImportSerial {serial} {
	doctools::toc::structure verify $serial iscanonical

	# Kill existing content
	foreach id [$mytree children $myroot] {
	    $mytree delete $id
	}

	# Unpack the serialization.
	array set toc $serial
	array set toc $toc(doctools::toc)
	unset     toc(doctools::toc)

	# We are setting the relevant variables directly instead of
	# going through the accessor methods.

	set mytitle $toc(title)
	set mylabel $toc(label)

	$self ImportDivision $toc(items) $myroot

	# Extend cache (only if canonical, as we return only canonical
	# data).
	if {$iscanonical} {
	    set mytoc(serial) $serial
	}
	return
    }

    method ImportDivision {items root} {
	foreach element $items {
	    foreach {etype edata} $element break
	    #struct::list assign $element etype edata
	    array set toc $edata
	    switch -exact -- $etype {
		reference {
		    $self + reference $root \
			$toc(label) $toc(id) $toc(desc)
		}
		division {
		    if {[info exists toc(id)]} {
			set div [$self + division $root $toc(label) $toc(id)]
		    } else {
			set div [$self + division $root $toc(label)]
		    }
		    $self ImportDivision $toc(items) $div
		}
	    }
	    unset toc
	}
	return
    }

    method Import {format data} {
	if {$myimporter eq {}} {
	    return -code error "Unable to import from \"$format\", no importer configured"
	}

	return [$myimporter import text $data $format]
    }

    # ### ### ### ######### ######### #########
    ## State

    # References to export/import managers extending the
    # (de)serialization abilities of the table of contents.
    variable myexporter {}
    variable myimporter {}

    # Internal representation of the table of contents.

    variable mytitle           {} ; # 
    variable mylabel           {} ; # 
    variable mytree            {} ; # Tree object holding the toc.
    variable myroot            {} ; # Name of the tree root node.

    # Array serving as cache holding alternative representations of
    # the toc generated via 'serialize', i.e. data export.

    variable mytoc -array {}

    ##
    # ### ### ### ######### ######### #########
}

# ### ### ### ######### ######### #########
## Ready

package provide doctools::toc 2
return