'\" '\" Copyright (c) 1993 The Regents of the University of California. '\" Copyright (c) 1994-1996 Sun Microsystems, Inc. '\" '\" See the file "license.terms" for information on usage and redistribution '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. '\" '\" RCS: @(#) $Id: open.n,v 1.2 2003/11/24 05:09:59 bbbush Exp $ '\" '\" The definitions below are for supplemental macros used in Tcl/Tk '\" manual entries. '\" '\" .AP type name in/out ?indent? '\" Start paragraph describing an argument to a library procedure. '\" type is type of argument (int, etc.), in/out is either "in", "out", '\" or "in/out" to describe whether procedure reads or modifies arg, '\" and indent is equivalent to second arg of .IP (shouldn't ever be '\" needed; use .AS below instead) '\" '\" .AS ?type? ?name? '\" Give maximum sizes of arguments for setting tab stops. Type and '\" name are examples of largest possible arguments that will be passed '\" to .AP later. If args are omitted, default tab stops are used. '\" '\" .BS '\" Start box enclosure. From here until next .BE, everything will be '\" enclosed in one large box. '\" '\" .BE '\" End of box enclosure. '\" '\" .CS '\" Begin code excerpt. '\" '\" .CE '\" End code excerpt. '\" '\" .VS ?version? ?br? '\" Begin vertical sidebar, for use in marking newly-changed parts '\" of man pages. The first argument is ignored and used for recording '\" the version when the .VS was added, so that the sidebars can be '\" found and removed when they reach a certain age. If another argument '\" is present, then a line break is forced before starting the sidebar. '\" '\" .VE '\" End of vertical sidebar. '\" '\" .DS '\" Begin an indented unfilled display. '\" '\" .DE '\" End of indented unfilled display. '\" '\" .SO '\" Start of list of standard options for a Tk widget. The '\" options follow on successive lines, in four columns separated '\" by tabs. '\" '\" .SE '\" End of list of standard options for a Tk widget. '\" '\" .OP cmdName dbName dbClass '\" Start of description of a specific option. cmdName gives the '\" option's name as specified in the class command, dbName gives '\" the option's name in the option database, and dbClass gives '\" the option's class in the option database. '\" '\" .UL arg1 arg2 '\" Print arg1 underlined, then print arg2 normally. '\" '\" RCS: @(#) $Id: open.n,v 1.2 2003/11/24 05:09:59 bbbush Exp $ '\" '\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages. .if t .wh -1.3i ^B .nr ^l \n(.l .ad b '\" # Start an argument description .de AP .ie !"\\$4"" .TP \\$4 .el \{\ . ie !"\\$2"" .TP \\n()Cu . el .TP 15 .\} .ta \\n()Au \\n()Bu .ie !"\\$3"" \{\ \&\\$1 \\fI\\$2\\fP (\\$3) .\".b .\} .el \{\ .br .ie !"\\$2"" \{\ \&\\$1 \\fI\\$2\\fP .\} .el \{\ \&\\fI\\$1\\fP .\} .\} .. '\" # define tabbing values for .AP .de AS .nr )A 10n .if !"\\$1"" .nr )A \\w'\\$1'u+3n .nr )B \\n()Au+15n .\" .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n .nr )C \\n()Bu+\\w'(in/out)'u+2n .. .AS Tcl_Interp Tcl_CreateInterp in/out '\" # BS - start boxed text '\" # ^y = starting y location '\" # ^b = 1 .de BS .br .mk ^y .nr ^b 1u .if n .nf .if n .ti 0 .if n \l'\\n(.lu\(ul' .if n .fi .. '\" # BE - end boxed text (draw box now) .de BE .nf .ti 0 .mk ^t .ie n \l'\\n(^lu\(ul' .el \{\ .\" Draw four-sided box normally, but don't draw top of .\" box if the box started on an earlier page. .ie !\\n(^b-1 \{\ \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul' .\} .el \}\ \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul' .\} .\} .fi .br .nr ^b 0 .. '\" # VS - start vertical sidebar '\" # ^Y = starting y location '\" # ^v = 1 (for troff; for nroff this doesn't matter) .de VS .if !"\\$2"" .br .mk ^Y .ie n 'mc \s12\(br\s0 .el .nr ^v 1u .. '\" # VE - end of vertical sidebar .de VE .ie n 'mc .el \{\ .ev 2 .nf .ti 0 .mk ^t \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n' .sp -1 .fi .ev .\} .nr ^v 0 .. '\" # Special macro to handle page bottom: finish off current '\" # box/sidebar if in box/sidebar mode, then invoked standard '\" # page bottom macro. .de ^B .ev 2 'ti 0 'nf .mk ^t .if \\n(^b \{\ .\" Draw three-sided box if this is the box's first page, .\" draw two sides but no top otherwise. .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c .\} .if \\n(^v \{\ .nr ^x \\n(^tu+1v-\\n(^Yu \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c .\} .bp 'fi .ev .if \\n(^b \{\ .mk ^y .nr ^b 2 .\} .if \\n(^v \{\ .mk ^Y .\} .. '\" # DS - begin display .de DS .RS .nf .sp .. '\" # DE - end display .de DE .fi .RE .sp .. '\" # SO - start of list of standard options .de SO .SH "STANDARD OPTIONS" .LP .nf .ta 5.5c 11c .ft B .. '\" # SE - end of list of standard options .de SE .fi .ft R .LP See the \\fBoptions\\fR manual entry for details on the standard options. .. '\" # OP - start of full description for a single option .de OP .LP .nf .ta 4c Command-Line Name: \\fB\\$1\\fR Database Name: \\fB\\$2\\fR Database Class: \\fB\\$3\\fR .fi .IP .. '\" # CS - begin code excerpt .de CS .RS .nf .ta .25i .5i .75i 1i .. '\" # CE - end code excerpt .de CE .fi .RE .. .de UL \\$1\l'|0\(ul'\\$2 .. .TH open 3tcl 7.6 Tcl "Tcl Built-In Commands" .BS '\" Note: do not modify the .SH NAME line immediately below! .SH NAME open \- 打開基於文件或命令管道的一個通道 .SH "總覽 SYNOPSIS" .sp \fBopen \fIfileName\fR .br \fBopen \fIfileName access\fR .br \fBopen \fIfileName access permissions\fR .BE .SH "描述 DESCRIPTION" .PP .VS 這個命令打開一個文件、串行端口、或命令管道並返回一個 .VE 通道標識符用於將來被命令如 \fBread\fR、\fBputs\fR、和\fBclose \fR調用。如果 \fIfileName\fR 的第一個字符不是“|”則命令打開一個文件: \fIfileName\fR 給出要打開的文件的名字,並且它必須符合在 \fBfilename\fR 手冊條目中描述的規範。 .PP 如果存在 \fIaccess\fR 參數,則它指示以何種方式訪問文件(或命令管道)。在第一種形式下 \fIaccess\fR 可以是下列值: .TP 15 \fBr\fR 爲只讀而打開文件;文件必須已經存在。如果未指定 \fIaccess\fR 則這個值是缺省值。 .TP 15 \fBr+\fR 爲讀寫而打開文件;文件必須已經存在。 .TP 15 \fBw\fR 爲只寫而打開文件。如果文件已經存在則截斷(truncate)它,如果文件不存在則建立一個新文件。 .TP 15 \fBw+\fR 爲讀寫而打開文件。如果文件已經存在則截斷(truncate)它,如果文件不存在則建立一個新文件。 .TP 15 \fBa\fR 爲只讀而打開文件。如果文件不存在,則建立一個新的空文件。設置初始訪問位置爲文件的結束處。 .TP 15 \fBa+\fR 爲讀寫而打開文件。如果文件不存在,則建立一個新的空文件。設置初始訪問位置爲文件的結束處。 .PP 在第二種形式下,\fIaccess\fR 由某些下列標誌的一個列表構成,所有這些都有標準的 POSIX 意義。 必須有一個標誌是 \fBRDONLY\fR、\fBWRONLY\fR 或 \fBRDWR\fR 中的一個。 .TP 15 \fBRDONLY\fR 爲只讀而打開文件。 .TP 15 \fBWRONLY\fR 爲只寫而打開文件。 .TP 15 \fBRDWR\fR 爲讀寫而打開文件。 .TP 15 \fBAPPEND\fR 在每次寫之前把指針設置到文件的結束處。 .TP 15 \fBCREAT\fR 如果文件不存在則建立這個文件(如果沒有這個標誌,在文件不存在時返回一個錯誤)。 .TP 15 \fBEXCL\fR 如果還指定了 \fBCREAT\fR,如果文件已經存在則返回一個錯誤。 .TP 15 \fBNOCTTY\fR 如果文件是一個終端設備,這個標誌防止這個文件成爲這個進程的控制終端。 .TP 15 \fBNONBLOCK\fR 在打開文件和可能的後續 I/O 操作期間防止過程阻塞。這個標誌的真實行爲是依賴於系統的和依賴於設備的;不鼓勵使用它(更好是使用 \fBfconfigure\fR 命令來使一個文件進入非阻塞模式)。詳情參見你的系統文檔中的 \fBopen\fR 系統調用的 \fBO_NONBLOCK\fR 標誌。 .TP 15 \fBTRUNC\fR 如果文件存在則截斷其爲零長度。 .PP 如果作爲打開過程的一部分而建立了一個新文件,使用 \fIpermissions\fR (一個整數)並聯合進程的文件模式建立掩碼爲新文件設置權限。\fIPermissions\fR 缺省爲 0666。 .PP '\" Not versioned as advice applies to all recent versions of Tcl. '\" Prior to that, Tcl didn't really support binary files anyway... .VS Note that if you are going to be reading or writing binary data from the channel created by this command, you should use the \fBfconfigure\fR command to change the \fB-translation\fR option of the channel to \fBbinary\fR before transferring any binary data. This is in contrast to the ``b'' character passed as part of the equivalent of the \fIaccess\fR parameter to some versions of the C library \fIfopen()\fR function. .VE .SH "命令管道 COMMAND PIPELINES" .PP 如果 \fIfileName\fR is 的第一個字符是“|”,則把 \fIfileName\fR 的餘下的字符被作爲描述要調用的一個命令管道的參數的一個列表來對待,與 \fBexec \fR對待參數的方式相同。在這種情況下,依賴於 access 的值,使用由 \fBopen\fR 返回的通道標識符來向這個命令的輸入管道寫或從它的輸出管道讀。如果使用了只寫訪問(例如, \fIaccess\fR 是 \fBw\fR),則除非被這個命令所屏棄(override),這個(命令)管道的標準輸出被定向到當前的標準輸出上。如果使用了只讀訪問(例如,\fIaccess\fR 是 \fBr\fR),則除非被這個命令所屏棄(override),這個(命令)管道的標準輸入接受當前的標準輸入。 .PP 譯註 .CS 打開用來寫的命令管道 Tcl ==> Pipeline ==> command +--------+ +--------+ +--------+ +--------+ | stdin |<------+ stdin | +-->| pipein | +--+ stdin | +--------+ +--------+ | +--------+ | +--------+ | stdout |<--+---+ stdout | | | pipeout|<--+ | stdout +--+ +--------+ | +--------+ | +--------+ +--------+ | | | stderr | | | stderr | | | +--------+ | +--------+ | | | channel+--+ | | +--------+ | +-----------------------------------------------+ 打開一個用來讀的命令管道 +-----------------------------------------------+ | | +--------+ | +--------+ +--------+ +--------+ | | stdin |<--+---+ stdin | | pipein |<--+ | stdin +--+ +--------+ +--------+ +--------+ | +--------+ | stdout |<------+ stdout | +-->| pipeout| +--+ stdout | +--------+ +--------+ | +--------+ +--------+ | stderr | | | stderr | +--------+ | +--------+ | channel+--+ +--------+ Tcl <== Pipeline <== command .CE .SH "串行通信 SERIAL COMMUNICATIONS" .VS .PP 如果 \fIfileName\fR 參照一個串行端口,則打開特定的串行端口並以依賴於平臺的方式初始化。給 \fIfileName\fR 用以打開一個串行端口的可接受的值在移植要點中描述。 .SH "配置選項 CONFIGURATION OPTIONS" 使用 \fBfconfigure\fR 命令來爲打開的串行端口查詢和設置下列選項: .TP \fB\-mode \fIbaud\fB,\fIparity\fB,\fIdata\fB,\fIstop\fR . 這個選項是由逗號分隔的四個值的一個集合: 這個串行端口的波特(baud)率、奇偶校驗(parity)、數據位數、和停止位。\fIbaud\fR 率是指定連接速度的一個簡單整數。\fIParity\fR 是下列字母之一: \fBn\fR、\fBo\fR、\fBe\fR、\fBm\fR、\fBs\fR;分別表示奇偶校驗選項“none”、“odd”、“even”、“mark”、或“space”。\fIData\fR 是數據位數,應當是從 5 到 8 中的一個整數,而 \fIstop\fR 是停止位的數目,應當是整數 1 或 2。 .TP \fB\-pollinterval \fImsec\fR . 只在 Windows 上串行端口能獲得這個選項,它被用來設置文件事件輪詢(poll)之間的最大時間。這影響整個 Tcl 解釋器檢查事件中間的時間間隔(總是採用最小的值)。只有在你想要使輪詢這個串行端口的時間間隔小於 10 msec (缺省值)時才使用這個選項。 .TP \fB\-lasterror\fR . 只在 Windows 上串行端口能獲得這個選項,並只能查詢(只在直接請求時報告)。在串行通信出錯的情況下,\fBread\fR 或 \fBputs\fR 返回一個一般 Tcl 文件 I/O 錯誤。可以調用 \fBfconfigure -lasterror\fR 來得到錯誤詳情的一個列表(例如,FRAME RXOVER)。 .VE .VS .SH "移植要點 PORTABILITY ISSUES" .sp .TP \fBWindows \fR(所有版本) . 給 \fIfileName\fR 來打開一個串行端口的有效值的形式是 \fBcom\fIX\fB:\fR,這裏 \fIX\fR 是一個數,一般是從 1 到 4。如果系統有多於四個串行端口,則這個符號(notation)可以表示串行端口從 1 到 9。嘗試打開一個不存在或序號大於 9 的串行端口將導致錯誤。打開串行端口的可替代的方法是使用文件名 \fB\e\e.\ecomX\fR,這裏 X 是對應於一個串行端口的任何(整)數;請注意,這個方法在 Windows 95 和 Windows 98 上相當的慢。 .TP \fBWindows NT\fR . 在交互運行 Tcl 的時候,如果存在一個控制檯,則在真實的控制檯和使用標準輸入或輸出的一個命令管道之間可能有一些奇怪的相互作用。如果爲讀而打開一個命令管道,在這個控制檯鍵入的一些行將被髮送到命令管道而一些行將別發送到 Tcl 求值器。如果爲寫而打開一個命令管道,在管道關閉之前,在這個控制檯中鍵入的擊鍵(keystroke)將一直是不可見的。在執行 16-bit 或 32-bit 應用程序時都可能發生這種事情。這些問題只發生在 Tcl 和子應用程序同時競爭這個控制檯的時候。如果命令管道是從一個腳本中啓動的,所以 Tcl 不訪問控制檯,或者命令管道不使用標準輸出或輸出,而是重定向(從或)到一個文件,那麼上述問題不發生。 .TP \fBWindows 95\fR . 不能同時爲讀寫而打開一個命令管道來執行一個 16-bit DOS 應用程序,因爲從一個管道接收標準輸入和向一個管道發送標準輸出的(兩個) 16-bit DOS 應用程序同步運行。不執行 16-bit DOS 應用程序的命令管道異步運行並且可以同時爲讀寫而打開。 .sp 在交互運行 Tcl 的時候,如果存在一個控制檯,則在真實的控制檯和使用標準輸入或輸出的一個命令管道之間可能有一些奇怪的相互作用。如果爲從一個 32-bit 應用程序讀而打開一個命令管道,在這個控制檯鍵入的一些擊鍵將被髮送到命令管道而一些擊鍵將別發送到 Tcl 求值器。如果爲向一個 32-bit 應用程序寫而打開一個命令管道,在管道關閉之前,在控制檯中將一直沒有輸出是可見的。這些問題只發生在 Tcl 和子應用程序同時競爭這個控制檯的時候。如果命令管道是從一個腳本中啓動的,所以 Tcl 不訪問控制檯,或者命令管道不使用標準輸出或輸出,而是重定向(從或)到一個文件,那麼上述問題不發生。 .sp 不論 Tcl 是否在交互的運行,如果爲從一個 16-bit DOS 應用程序讀而打開一個命令管道,在從命令管道的標準輸出收到文件結束之前,對 \fBopen\fR 的調用一直不返回。如果爲向一個 16-bit DOS 應用程序寫而打開一個命令管道,在管道實際上被關閉之前,沒有數據被髮送到命令管道的標準輸出。象上面描述的那樣,這個問題發生的原因是 16-bit DOS 應用程序同步運行。 .TP \fBMacintosh\fR . 在 Macintosh 下目前未實現打開串行端口。 .sp 在 Macintosh 不支持打開命令管道,原因是應用程序不支持標準輸入或輸出的概念。 .TP \fBUnix\fR\0\0\0\0\0\0\0 . 給 \fIfileName\fR 來打開一個串行端口的有效值的形式是 \fB/dev/tty\fIX\fR,這裏的 \fIX\fR 是 \fBa\fR 或 \fBb\fR,但是可以使用映射到一個串行端口的任何僞文件(pseudo-file)的名字。 .sp 在交互運行 Tcl 的時候,如果存在一個控制檯,則在真實的控制檯和使用標準輸入或輸出的一個命令管道之間可能有一些奇怪的相互作用。如果爲讀而打開一個命令管道,在這個控制檯鍵入的一些行將被髮送到命令管道而一些行將別發送到 Tcl 求值器。這些問題只發生在 Tcl 和子應用程序同時競爭這個控制檯的時候。如果命令管道是從一個腳本中啓動的,所以 Tcl 不訪問控制檯,或者命令管道不使用標準輸出或輸出,而是重定向(從或)到一個文件,那麼上述問題不發生。 .LP 關於在不同平臺上執行應用程序請參見 \fBexec\fR 命令的\fB移植要點\fR章節來得到不特定於命令管道的額外的信息。 .SH "參見 SEE ALSO" file(n), close(n), filename(n), fconfigure(n), gets(n), read(n), puts(n), exec(n), fopen(1) .SH "關鍵字 KEYWORDS" access mode, append, create, file, non-blocking, open, permissions, pipeline, process, serial .SH "[中文版維護人]" .B 寒蟬退士 .SH "[中文版最新更新]" .B 2001/10/16 .SH "《中國 Linux 論壇 man 手冊頁翻譯計劃》:" .BI http://cmpp.linuxforum.net .SH "跋" .br 本頁面中文版由中文 man 手冊頁計劃提供。 .br 中文 man 手冊頁計劃:\fBhttps://github.com/man-pages-zh/manpages-zh\fR