|
1
|
# |
|
2
|
# Copyright (c) 2016 D. Richard Hipp |
|
3
|
# |
|
4
|
# This program is free software; you can redistribute it and/or |
|
5
|
# modify it under the terms of the Simplified BSD License (also |
|
6
|
# known as the "2-Clause License" or "FreeBSD License".) |
|
7
|
# |
|
8
|
# This program is distributed in the hope that it will be useful, |
|
9
|
# but without any warranty; without even the implied warranty of |
|
10
|
# merchantability or fitness for a particular purpose. |
|
11
|
# |
|
12
|
# Author contact information: |
|
13
|
# [email protected] |
|
14
|
# http://www.hwaci.com/drh/ |
|
15
|
# |
|
16
|
############################################################################ |
|
17
|
# |
|
18
|
# This is a fake text editor for use by tests. To customize its behavior, |
|
19
|
# set the FAKE_EDITOR_SCRIPT environment variable prior to evaluating this |
|
20
|
# script file. If FAKE_EDITOR_SCRIPT environment variable is not set, the |
|
21
|
# default behavior will be used. The default behavior is to append the |
|
22
|
# process identifier and the current time, in seconds, to the file data. |
|
23
|
# |
|
24
|
|
|
25
|
if {![info exists argv] || [llength $argv] != 1} { |
|
26
|
error "Usage: \"[info nameofexecutable]\" \"[info script]\" <fileName>" |
|
27
|
} |
|
28
|
|
|
29
|
############################################################################### |
|
30
|
|
|
31
|
proc makeBinaryChannel { channel } { |
|
32
|
fconfigure $channel -encoding binary -translation binary |
|
33
|
} |
|
34
|
|
|
35
|
proc readFile { fileName } { |
|
36
|
set channel [open $fileName RDONLY] |
|
37
|
makeBinaryChannel $channel |
|
38
|
set result [read $channel] |
|
39
|
close $channel |
|
40
|
return $result |
|
41
|
} |
|
42
|
|
|
43
|
proc writeFile { fileName data } { |
|
44
|
set channel [open $fileName {WRONLY CREAT TRUNC}] |
|
45
|
makeBinaryChannel $channel |
|
46
|
puts -nonewline $channel $data |
|
47
|
close $channel |
|
48
|
return "" |
|
49
|
} |
|
50
|
|
|
51
|
############################################################################### |
|
52
|
|
|
53
|
set fileName [lindex $argv 0] |
|
54
|
|
|
55
|
if {[regexp {^CYGWIN} $::tcl_platform(os)]} { |
|
56
|
# Under Cygwin, we get a Windows path but must access using the unix path. |
|
57
|
set fileName [exec cygpath --unix $fileName] |
|
58
|
} |
|
59
|
|
|
60
|
if {[file exists $fileName]} { |
|
61
|
set data [readFile $fileName] |
|
62
|
} else { |
|
63
|
set data "" |
|
64
|
} |
|
65
|
|
|
66
|
############################################################################### |
|
67
|
|
|
68
|
if {[info exists env(FAKE_EDITOR_SCRIPT)]} { |
|
69
|
# |
|
70
|
# NOTE: If an error is caught while evaluating this script, catch |
|
71
|
# it and return, which will also skip writing the (possibly |
|
72
|
# modified) content back to the original file. |
|
73
|
# |
|
74
|
set script $env(FAKE_EDITOR_SCRIPT) |
|
75
|
set code [catch $script error] |
|
76
|
|
|
77
|
if {$code != 0} { |
|
78
|
if {[info exists env(FAKE_EDITOR_VERBOSE)]} { |
|
79
|
if {[info exists errorInfo]} { |
|
80
|
puts stdout "ERROR ($code): $errorInfo" |
|
81
|
} else { |
|
82
|
puts stdout "ERROR ($code): $error" |
|
83
|
} |
|
84
|
} |
|
85
|
|
|
86
|
return |
|
87
|
} |
|
88
|
} else { |
|
89
|
# |
|
90
|
# NOTE: The default behavior is to append the process identifier |
|
91
|
# and the current time, in seconds, to the file data. |
|
92
|
# |
|
93
|
append data " " [pid] " " [clock seconds] |
|
94
|
} |
|
95
|
|
|
96
|
############################################################################### |
|
97
|
|
|
98
|
writeFile $fileName $data |
|
99
|
|