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