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# drh@hwaci.com 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 25if {![info exists argv] || [llength $argv] != 1} { 26 error "Usage: \"[info nameofexecutable]\" \"[info script]\" <fileName>" 27} 28 29############################################################################### 30 31proc makeBinaryChannel { channel } { 32 fconfigure $channel -encoding binary -translation binary 33} 34 35proc readFile { fileName } { 36 set channel [open $fileName RDONLY] 37 makeBinaryChannel $channel 38 set result [read $channel] 39 close $channel 40 return $result 41} 42 43proc 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 53set fileName [lindex $argv 0] 54 55if {[file exists $fileName]} { 56 set data [readFile $fileName] 57} else { 58 set data "" 59} 60 61############################################################################### 62 63if {[info exists env(FAKE_EDITOR_SCRIPT)]} { 64 # 65 # NOTE: If an error is caught while evaluating this script, catch 66 # it and return, which will also skip writing the (possibly 67 # modified) content back to the original file. 68 # 69 set script $env(FAKE_EDITOR_SCRIPT) 70 set code [catch $script error] 71 72 if {$code != 0} { 73 if {[info exists env(FAKE_EDITOR_VERBOSE)]} { 74 if {[info exists errorInfo]} { 75 puts stdout "ERROR ($code): $errorInfo" 76 } else { 77 puts stdout "ERROR ($code): $error" 78 } 79 } 80 81 return 82 } 83} else { 84 # 85 # NOTE: The default behavior is to append the process identifier 86 # and the current time, in seconds, to the file data. 87 # 88 append data " " [pid] " " [clock seconds] 89} 90 91############################################################################### 92 93writeFile $fileName $data 94