/usr/share/tcltk/tcllib1.16/rcs/rcs.tcl is in tcllib 1.16-dfsg-2.
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 | # rcs.tcl --
#
# Utilities for RCS related operations.
#
# Copyright (c) 2005 by Colin McCormack <coldstore@users.sourceforge.net>
# Copyright (c) 2005 by Andreas Kupries <andreas_kupries@users.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: rcs.tcl,v 1.4 2005/09/28 04:51:23 andreas_kupries Exp $
# ### ### ### ######### ######### #########
## Requisites.
package require Tcl 8.4
# ### ### ### ######### ######### #########
## API Implementation
namespace eval rcs {}
# ::rcs::text2dict --
#
# Convert a text into a dictionary. The dictionary is keyed by line
# numbers, and the value is the text of the corresponding line. The
# first line has index/number 1.
#
# Arguments
# - text The text to convert.
#
# Results
# A dictionary containing the text in split form.
#
# Side effects
# None
proc ::rcs::text2dict {text} {
array set lines {}
set lnum 0
foreach line [split $text \n] {
set lines([incr lnum]) $line
}
return [array get lines]
}
# ::rcs::file2dict --
#
# Convert a text stored in a file into a dictionary. The dictionary is
# keyed by line numbers, and the value is the text of the
# corresponding line. The first line has index/number 1.
#
# Arguments
# - file The path of the file containing the text to convert.
#
# Results
# A dictionary containing the text in split form.
#
# Side effects
# None
proc ::rcs::file2dict {filename} {
set chan [open $filename r]
set text [read $chan]
close $chan
return [text2dict $text]
}
# ::rcs::dict2text --
#
# Converts a dictionary as created by the 2dict commands back into a
# text. The dictionary is keyed by line numbers, and the value is the
# text of the corresponding line. The first line has index/number 1.
# The dictionary may have gaps in the line numbers.
#
# Arguments
# - dict The dictionary to convert.
#
# Results
# The text stored in the dictionary.
#
# Side effects
# None
proc ::rcs::dict2text {dict} {
array set lines $dict
set result {}
foreach lnum [lsort -integer [array names lines]] {
lappend result $lines($lnum)
}
return [join $result \n]
}
# ::rcs::dict2file --
#
# Converts a dictionary as created by the 2dict commands back into a
# text and stores it into the specified file. The dictionary is keyed
# by line numbers, and the value is the text of the corresponding
# line. The first line has index/number 1. The dictionary may have
# gaps in the line numbers.
#
# Arguments
# - filename The path to the file to store the reconstructed text into.
# - dict The dictionary to convert.
#
# Results
# None.
#
# Side effects
# None
proc ::rcs::dict2file {filename dict} {
set chan [open $filename w]
puts -nonewline $chan [dict2text $dict]
close $chan
}
# ::rcs::decodeRcsPatch --
#
# Converts a text containing a RCS patch (diff -n format) into a list
# of patch commands. Each element of the list is a list containing the
# patch command and its arguments, in this order.
#
# The valid patch commands are 'a' and 'd'. 'a' has two arguments, the
# index of the line where to add the text, and the text itself. The
# 'd' command has two arguments as well, the index of the first line
# to delete, and the number of lines to delete.
#
# Arguments
# - patch The text in diff -n format, the patch to parse.
#
# Results
# A list containing the patch as sequence of commands.
#
# Side effects
# None
proc ::rcs::decodeRcsPatch {patch} {
set patch [split $patch \n]
set plen [llength $patch]
set at 0
set res {}
while {$at < $plen} {
# I use an index into the list to avoid shifting the list
# elements down with each line processed. That is a lot of
# memcpy's.
set cmd [string trim [lindex $patch $at]]
incr at
switch -glob -- $cmd {
"" {}
a* {
foreach {start len} [split [string range $cmd 1 end]] break
set to [expr {$at + $len - 1}]
lappend res [list \
a \
$start \
[join [lrange $patch $at $to] \n]]
incr to
set at $to
}
d* {
foreach {start len} [split [string range $cmd 1 end]] break
lappend res [list d $start $len]
}
default {
return -code error "Unknown patch command: '$cmd'"
}
}
}
return $res
}
# ::rcs::encodeRcsPatch --
#
# Converts a list of patch commands into a text containing the same
# command as a RCS patch (i.e. in diff -n format). See decodePatch for
# a description of the input format.
#
# Arguments
# - patch The patch as list of patch commands.
#
# Results
# A text containing the patch in diff -n format.
#
# Side effects
# None
proc ::rcs::encodeRcsPatch {patch} {
set res {}
foreach cmd $patch {
foreach {op a b} $cmd break
switch -exact -- $op {
a {
# a = index of line where to add
# b = text to add
set lines [llength [split $b \n]]
lappend res "a$a $lines"
lappend res $b
}
d {
# a = index of first line to delete.
# b = #lines to delete.
lappend res "d$a $b"
}
default {
return -code error "Unknown patch command: '$op'"
}
}
}
return [join $res \n]\n
}
# ::rcs::applyRcsPatch --
#
# Apply a patch in the format returned by decodeRcsPatch to a text in
# the format returned by the xx2dict commands. The result is
# dictionary containing the modified text. Use the dict2xx commands to
# convert this back into a regular text.
#
# Arguments
# - text The text (as dict) to patch
# - patch The patch (as cmd list) to apply.
#
# Results
# The modified text (as dict)
#
# Side effects
# None
proc ::rcs::applyRcsPatch {text patch} {
array set lines $text
foreach cmd $patch {
foreach {op a b} $cmd break
switch -exact -- $op {
a {
# a = index of line where to add
# b = text to add
if {[info exists lines($a)]} {
append lines($a) \n $b
} else {
set lines($a) $b
}
}
d {
# a = index of first line to delete.
# b = #lines to delete.
while {$b > 0} {
unset lines($a)
incr a
incr b -1
}
}
default {
return -code error "Unknown patch command: '$op'"
}
}
}
return [array get lines]
}
# ### ### ### ######### ######### #########
## Ready for use.
package provide rcs 0.1
|