'\" '\" 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