aboutsummaryrefslogtreecommitdiff
path: root/stdio.tcl
blob: a6e9f97f4afddc66ec7a2089962e2fc7a1f9f874 (plain)
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
# (c) 2008 Steve Bennett <steveb@workware.net.au>
#
# Implements Tcl-compatible IO commands based on the aio package
#
# Provides puts, gets, open, close, eof, flush, seek, tell

package provide stdio 1.0
catch {package require aio 1.0}

# Remove the builtin puts
rename puts ""

set stdio::stdin  [aio.open standard input]
set stdio::stdout [aio.open standard output]
set stdio::stderr [aio.open standard error]
set stdio::std_channel_map [list stdin ${stdio::stdin} stdout ${stdio::stdout} stderr ${stdio::stderr}]

proc stdio::std_channel {channel} {
	global _stdmap
	return [string map ${::stdio::std_channel_map} $channel]
}

proc puts {channel args} {
	set nonewline 0
	if {$channel eq "-nonewline"} {
		set nonewline 1
		set channel [lindex $args 0]
		set args [lrange $args 1 end]
	}
	if {[llength $args] == 0} {
		set args [list $channel]
		set channel stdout
	}

	set channel [stdio::std_channel $channel]

	if {$nonewline} {
		$channel puts -nonewline {expand}$args
	} else {
		$channel puts {expand}$args
	}
}

proc gets {channel args} {
	set channel [stdio::std_channel $channel]
	return [uplevel 1 [list $channel gets {expand}$args]]
}

proc open {file args} {
	return [aio.open $file {expand}$args]
}

proc close {channel} {
	[stdio::std_channel $channel] close
}

proc eof {channel} {
	[stdio::std_channel $channel] eof
}

proc flush {channel} {
	[stdio::std_channel $channel] flush
}

proc seek {channel args} {
	[stdio::std_channel $channel] seek {expand}$args
}

proc tell {channel} {
	[stdio::std_channel $channel] tell
}