.TH io 3erl "stdlib 4.3.1.3" "Ericsson AB" "Erlang Module Definition" .SH NAME io \- Standard I/O server interface functions. .SH DESCRIPTION .LP This module provides an interface to standard Erlang I/O servers\&. The output functions all return \fIok\fR\& if they are successful, or exit if they are not\&. .LP All functions in this module have an optional parameter \fIIoDevice\fR\&\&. If included, it must be the pid of a process that handles the I/O protocols\&. Normally, it is a \fIIoDevice\fR\& returned by \fIfile:open/2\fR\&\&. If no \fIIoDevice\fR\& is given, \fIstandard_io\fR\& is used\&. .LP For a description of the I/O protocols, see section The Erlang I/O Protocol in the User\&'s Guide\&. .LP .RS -4 .B Warning: .RE As from Erlang/OTP R13A, data supplied to function \fIput_chars/2\fR\& is to be in the \fIunicode:chardata()\fR\& format\&. This means that programs supplying binaries to this function must convert them to UTF-8 before trying to output the data on an I/O device\&. .LP If an I/O device is set in binary mode, functions \fIget_chars/2,3\fR\& and \fIget_line/1,2\fR\& can return binaries instead of lists\&. The binaries are, as from Erlang/OTP R13A, encoded in UTF-8\&. .LP To work with binaries in ISO Latin-1 encoding, use the \fIfile\fR\& module instead\&. .LP For conversion functions between character encodings, see the \fIunicode\fR\& module\&. .SH DATA TYPES .nf \fBdevice()\fR\& = atom() | pid() .br .fi .RS .LP An I/O device, either \fIstandard_io\fR\&, \fIstandard_error\fR\&, a registered name, or a pid handling I/O protocols (returned from \fIfile:open/2\fR\&)\&. .LP For more information about the built-in devices see Standard Input/Output and Standard Error\&. .RE .nf \fBopt_pair()\fR\& = .br {binary, boolean()} | .br {echo, boolean()} | .br {expand_fun, expand_fun()} | .br {encoding, encoding()} .br .fi .nf \fBexpand_fun()\fR\& = .br fun((term()) -> {yes | no, string(), [string(), \&.\&.\&.]}) .br .fi .nf \fBencoding()\fR\& = .br latin1 | unicode | utf8 | utf16 | utf32 | .br {utf16, big | little} | .br {utf32, big | little} .br .fi .nf \fBsetopt()\fR\& = binary | list | opt_pair() .br .fi .nf \fBformat()\fR\& = atom() | string() | binary() .br .fi .nf \fBlocation()\fR\& = erl_anno:location() .br .fi .nf \fBprompt()\fR\& = atom() | unicode:chardata() .br .fi .nf \fBserver_no_data()\fR\& = {error, ErrorDescription :: term()} | eof .br .fi .RS .LP What the I/O server sends when there is no data\&. .RE .SH EXPORTS .LP .nf .B columns() -> {ok, integer() >= 1} | {error, enotsup} .br .fi .br .nf .B columns(IoDevice) -> {ok, integer() >= 1} | {error, enotsup} .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br .RE .RE .RS .LP Retrieves the number of columns of the \fIIoDevice\fR\& (that is, the width of a terminal)\&. The function succeeds for terminal devices and returns \fI{error, enotsup}\fR\& for all other I/O devices\&. .RE .LP .nf .B format(Format) -> ok .br .fi .br .nf .B format(Format, Data) -> ok .br .fi .br .nf .B format(IoDevice, Format, Data) -> ok .br .fi .br .nf .B fwrite(Format) -> ok .br .fi .br .nf .B fwrite(Format, Data) -> ok .br .fi .br .nf .B fwrite(IoDevice, Format, Data) -> ok .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br Format = format() .br Data = [term()] .br .RE .RE .RS .LP Writes the items in \fIData\fR\& (\fI[]\fR\&) on the standard output (\fIIoDevice\fR\&) in accordance with \fIFormat\fR\&\&. \fIFormat\fR\& contains plain characters that are copied to the output device, and control sequences for formatting, see below\&. If \fIFormat\fR\& is an atom or a binary, it is first converted to a list with the aid of \fIatom_to_list/1\fR\& or \fIbinary_to_list/1\fR\&\&. Example: .LP .nf 1> io:fwrite("Hello world!~n", [])\&. Hello world! ok .fi .LP The general format of a control sequence is \fI~F\&.P\&.PadModC\fR\&\&. .LP The character \fIC\fR\& determines the type of control sequence to be used\&. It is the only required field\&. All of \fIF\fR\&, \fIP\fR\&, \fIPad\fR\&, and \fIMod\fR\& are optional\&. For example, to use a \fI#\fR\& for \fIPad\fR\& but use the default values for \fIF\fR\& and \fIP\fR\&, you can write \fI~\&.\&.#C\fR\&\&. .RS 2 .TP 2 * \fIF\fR\& is the \fIfield width\fR\& of the printed argument\&. A negative value means that the argument is left-justified within the field, otherwise right-justified\&. If no field width is specified, the required print width is used\&. If the field width specified is too small, the whole field is filled with \fI*\fR\& characters\&. .LP .TP 2 * \fIP\fR\& is the \fIprecision\fR\& of the printed argument\&. A default value is used if no precision is specified\&. The interpretation of precision depends on the control sequences\&. Unless otherwise specified, argument \fIwithin\fR\& is used to determine print width\&. .LP .TP 2 * \fIPad\fR\& is the padding character\&. This is the character used to pad the printed representation of the argument so that it conforms to the specified field width and precision\&. Only one padding character can be specified and, whenever applicable, it is used for both the field width and precision\&. The default padding character is \fI\&' \&'\fR\& (space)\&. .LP .TP 2 * \fIMod\fR\& is the control sequence modifier\&. This is one or more characters that change the interpretation of \fIData\fR\&\&. The current modifiers are \fIt\fR\&, for Unicode translation, and \fIl\fR\&, for stopping \fIp\fR\& and \fIP\fR\& from detecting printable characters\&. .LP .RE .LP If \fIF\fR\&, \fIP\fR\&, or \fIPad\fR\& is a \fI*\fR\& character, the next argument in \fIData\fR\& is used as the value\&. For example: .LP .nf 1> io:fwrite("~*\&.*\&.0f~n",[9, 5, 3\&.14159265])\&. 003.14159 ok .fi .LP To use a literal \fI*\fR\& character as \fIPad\fR\&, it must be passed as an argument: .LP .nf 2> io:fwrite("~*\&.*\&.*f~n",[9, 5, $*, 3\&.14159265])\&. **3.14159 ok .fi .LP \fIAvailable control sequences:\fR\& .RS 2 .TP 2 .B \fI~\fR\&: Character \fI~\fR\& is written\&. .TP 2 .B \fIc\fR\&: The argument is a number that is interpreted as an ASCII code\&. The precision is the number of times the character is printed and defaults to the field width, which in turn defaults to 1\&. Example: .LP .nf 1> io:fwrite("|~10\&.5c|~-10\&.5c|~5c|~n", [$a, $b, $c])\&. | aaaaa|bbbbb |ccccc| ok .fi .RS 2 .LP If the Unicode translation modifier (\fIt\fR\&) is in effect, the integer argument can be any number representing a valid Unicode codepoint, otherwise it is to be an integer less than or equal to 255, otherwise it is masked with 16#FF: .RE .LP .nf 2> io:fwrite("~tc~n",[1024])\&. \\x{400} ok 3> io:fwrite("~c~n",[1024])\&. ^@ ok .fi .TP 2 .B \fIf\fR\&: The argument is a float that is written as \fI[-]ddd\&.ddd\fR\&, where the precision is the number of digits after the decimal point\&. The default precision is 6 and it cannot be < 1\&. .TP 2 .B \fIe\fR\&: The argument is a float that is written as \fI[-]d\&.ddde+-ddd\fR\&, where the precision is the number of digits written\&. The default precision is 6 and it cannot be < 2\&. .TP 2 .B \fIg\fR\&: The argument is a float that is written as \fIf\fR\&, if it is >= 0\&.1 and < 10000\&.0\&. Otherwise, it is written in the \fIe\fR\& format\&. The precision is the number of significant digits\&. It defaults to 6 and is not to be < 2\&. If the absolute value of the float does not allow it to be written in the \fIf\fR\& format with the desired number of significant digits, it is also written in the \fIe\fR\& format\&. .TP 2 .B \fIs\fR\&: Prints the argument with the string syntax\&. The argument is, if no Unicode translation modifier is present, an \fIiolist()\fR\&, a \fIbinary()\fR\&, or an \fIatom()\fR\&\&. If the Unicode translation modifier (\fIt\fR\&) is in effect, the argument is \fIunicode:chardata()\fR\&, meaning that binaries are in UTF-8\&. The characters are printed without quotes\&. The string is first truncated by the specified precision and then padded and justified to the specified field width\&. The default precision is the field width\&. .RS 2 .LP This format can be used for printing any object and truncating the output so it fits a specified field: .RE .LP .nf 1> io:fwrite("|~10w|~n", [{hey, hey, hey}])\&. |**********| ok 2> io:fwrite("|~10s|~n", [io_lib:write({hey, hey, hey})])\&. |{hey,hey,h| 3> io:fwrite("|~-10\&.8s|~n", [io_lib:write({hey, hey, hey})])\&. |{hey,hey | ok .fi .RS 2 .LP A list with integers > 255 is considered an error if the Unicode translation modifier is not specified: .RE .LP .nf 4> io:fwrite("~ts~n",[[1024]])\&. \\x{400} ok 5> io:fwrite("~s~n",[[1024]])\&. ** exception error: bad argument in function io:format/3 called as io:format(<0.53.0>,"~s~n",[[1024]]) .fi .TP 2 .B \fIw\fR\&: Writes data with the standard syntax\&. This is used to output Erlang terms\&. Atoms are printed within quotes if they contain embedded non-printable characters\&. Atom characters > 255 are escaped unless the Unicode translation modifier (\fIt\fR\&) is used\&. Floats are printed accurately as the shortest, correctly rounded string\&. .TP 2 .B \fIp\fR\&: Writes the data with standard syntax in the same way as \fI~w\fR\&, but breaks terms whose printed representation is longer than one line into many lines and indents each line sensibly\&. Left-justification is not supported\&. It also tries to detect flat lists of printable characters and output these as strings\&. For example: .LP .nf 1> T = [{attributes,[[{id,age,1\&.50000},{mode,explicit}, {typename,"INTEGER"}], [{id,cho},{mode,explicit},{typename,\&'Cho\&'}]]}, {typename,\&'Person\&'},{tag,{\&'PRIVATE\&',3}},{mode,implicit}]\&. \&... 2> io:fwrite("~w~n", [T])\&. [{attributes,[[{id,age,1.5},{mode,explicit},{typename, [73,78,84,69,71,69,82]}],[{id,cho},{mode,explicit},{typena me,'Cho'}]]},{typename,'Person'},{tag,{'PRIVATE',3}},{mode ,implicit}] ok 3> io:fwrite("~62p~n", [T])\&. [{attributes,[[{id,age,1.5}, {mode,explicit}, {typename,"INTEGER"}], [{id,cho},{mode,explicit},{typename,'Cho'}]]}, {typename,'Person'}, {tag,{'PRIVATE',3}}, {mode,implicit}] ok .fi .RS 2 .LP The field width specifies the maximum line length\&. It defaults to 80\&. The precision specifies the initial indentation of the term\&. It defaults to the number of characters printed on this line in the \fIsame\fR\& call to \fIwrite/1\fR\& or \fIformat/1,2,3\fR\&\&. For example, using \fIT\fR\& above: .RE .LP .nf 4> io:fwrite("Here T = ~62p~n", [T])\&. Here T = [{attributes,[[{id,age,1.5}, {mode,explicit}, {typename,"INTEGER"}], [{id,cho}, {mode,explicit}, {typename,'Cho'}]]}, {typename,'Person'}, {tag,{'PRIVATE',3}}, {mode,implicit}] ok .fi .RS 2 .LP As from Erlang/OTP 21\&.0, a field width of value \fI0\fR\& can be used for specifying that a line is infinitely long, which means that no line breaks are inserted\&. For example: .RE .LP .nf 5> io:fwrite("~0p~n", [lists:seq(1, 30)])\&. [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] ok .fi .RS 2 .LP When the modifier \fIl\fR\& is specified, no detection of printable character lists takes place, for example: .RE .LP .nf 6> S = [{a,"a"}, {b, "b"}], io:fwrite("~15p~n", [S])\&. [{a,"a"}, {b,"b"}] ok 7> io:fwrite("~15lp~n", [S])\&. [{a,[97]}, {b,[98]}] ok .fi .RS 2 .LP The Unicode translation modifier \fIt\fR\& specifies how to treat characters outside the Latin-1 range of codepoints, in atoms, strings, and binaries\&. For example, printing an atom containing a character > 255: .RE .LP .nf 8> io:fwrite("~p~n",[list_to_atom([1024])])\&. '\\x{400}' ok 9> io:fwrite("~tp~n",[list_to_atom([1024])])\&. 'Ѐ' ok .fi .RS 2 .LP By default, Erlang only detects lists of characters in the Latin-1 range as strings, but the \fI+pc unicode\fR\& flag can be used to change this (see \fIprintable_range/0\fR\& for details)\&. For example: .RE .LP .nf 10> io:fwrite("~p~n",[[214]])\&. "Ö" ok 11> io:fwrite("~p~n",[[1024]])\&. [1024] ok 12> io:fwrite("~tp~n",[[1024]])\&. [1024] ok .fi .RS 2 .LP but if Erlang was started with \fI+pc unicode\fR\&: .RE .LP .nf 13> io:fwrite("~p~n",[[1024]])\&. [1024] ok 14> io:fwrite("~tp~n",[[1024]])\&. "Ѐ" ok .fi .RS 2 .LP Similarly, binaries that look like UTF-8 encoded strings are output with the binary string syntax if the \fIt\fR\& modifier is specified: .RE .LP .nf 15> io:fwrite("~p~n", [<<208,128>>])\&. <<208,128>> ok 16> io:fwrite("~tp~n", [<<208,128>>])\&. <<"Ѐ"/utf8>> ok 17> io:fwrite("~tp~n", [<<128,128>>])\&. <<128,128>> ok .fi .TP 2 .B \fIW\fR\&: Writes data in the same way as \fI~w\fR\&, but takes an extra argument that is the maximum depth to which terms are printed\&. Anything below this depth is replaced with \fI\&.\&.\&.\fR\&\&. For example, using \fIT\fR\& above: .LP .nf 8> io:fwrite("~W~n", [T,9])\&. [{attributes,[[{id,age,1.5},{mode,explicit},{typename,...}], [{id,cho},{mode,...},{...}]]},{typename,'Person'}, {tag,{'PRIVATE',3}},{mode,implicit}] ok .fi .RS 2 .LP If the maximum depth is reached, it cannot be read in the resultant output\&. Also, the \fI,\&.\&.\&.\fR\& form in a tuple denotes that there are more elements in the tuple but these are below the print depth\&. .RE .TP 2 .B \fIP\fR\&: Writes data in the same way as \fI~p\fR\&, but takes an extra argument that is the maximum depth to which terms are printed\&. Anything below this depth is replaced with \fI\&.\&.\&.\fR\&, for example: .LP .nf 9> io:fwrite("~62P~n", [T,9])\&. [{attributes,[[{id,age,1.5},{mode,explicit},{typename,...}], [{id,cho},{mode,...},{...}]]}, {typename,'Person'}, {tag,{'PRIVATE',3}}, {mode,implicit}] ok .fi .TP 2 .B \fIB\fR\&: Writes an integer in base 2-36, the default base is 10\&. A leading dash is printed for negative integers\&. .RS 2 .LP The precision field selects base, for example: .RE .LP .nf 1> io:fwrite("~\&.16B~n", [31])\&. 1F ok 2> io:fwrite("~\&.2B~n", [-19])\&. -10011 ok 3> io:fwrite("~\&.36B~n", [5*36+35])\&. 5Z ok .fi .TP 2 .B \fIX\fR\&: Like \fIB\fR\&, but takes an extra argument that is a prefix to insert before the number, but after the leading dash, if any\&. .RS 2 .LP The prefix can be a possibly deep list of characters or an atom\&. Example: .RE .LP .nf 1> io:fwrite("~X~n", [31,"10#"])\&. 10#31 ok 2> io:fwrite("~\&.16X~n", [-31,"0x"])\&. -0x1F ok .fi .TP 2 .B \fI#\fR\&: Like \fIB\fR\&, but prints the number with an Erlang style \fI#\fR\&-separated base prefix\&. Example: .LP .nf 1> io:fwrite("~\&.10#~n", [31])\&. 10#31 ok 2> io:fwrite("~\&.16#~n", [-31])\&. -16#1F ok .fi .TP 2 .B \fIb\fR\&: Like \fIB\fR\&, but prints lowercase letters\&. .TP 2 .B \fIx\fR\&: Like \fIX\fR\&, but prints lowercase letters\&. .TP 2 .B \fI+\fR\&: Like \fI#\fR\&, but prints lowercase letters\&. .TP 2 .B \fIn\fR\&: Writes a new line\&. .TP 2 .B \fIi\fR\&: Ignores the next term\&. .RE .LP The function returns: .RS 2 .TP 2 .B \fIok\fR\&: The formatting succeeded\&. .RE .LP If an error occurs, there is no output\&. Example: .LP .nf 1> io:fwrite("~s ~w ~i ~w ~c ~n",[\&'abc def\&', \&'abc def\&', {foo, 1},{foo, 1}, 65])\&. abc def 'abc def' {foo,1} A ok 2> io:fwrite("~s", [65])\&. ** exception error: bad argument in function io:format/3 called as io:format(<0.53.0>,"~s","A") .fi .LP In this example, an attempt was made to output the single character 65 with the aid of the string formatting directive \fI"~s"\fR\&\&. .RE .LP .nf .B fread(Prompt, Format) -> Result .br .fi .br .nf .B fread(IoDevice, Prompt, Format) -> Result .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br Prompt = prompt() .br Format = format() .br Result = .br {ok, Terms :: [term()]} | .br {error, {fread, FreadError :: io_lib:fread_error()}} | .br server_no_data() .br .nf \fBserver_no_data()\fR\& = {error, ErrorDescription :: term()} | eof .fi .br .RE .RE .RS .LP Reads characters from the standard input (\fIIoDevice\fR\&), prompting it with \fIPrompt\fR\&\&. Interprets the characters in accordance with \fIFormat\fR\&\&. \fIFormat\fR\& contains control sequences that directs the interpretation of the input\&. .LP \fIFormat\fR\& can contain the following: .RS 2 .TP 2 * Whitespace characters (\fISpace\fR\&, \fITab\fR\&, and \fINewline\fR\&) that cause input to be read to the next non-whitespace character\&. .LP .TP 2 * Ordinary characters that must match the next input character\&. .LP .TP 2 * Control sequences, which have the general format \fI~*FMC\fR\&, where: .RS 2 .TP 2 * Character \fI*\fR\& is an optional return suppression character\&. It provides a method to specify a field that is to be omitted\&. .LP .TP 2 * \fIF\fR\& is the \fIfield width\fR\& of the input field\&. .LP .TP 2 * \fIM\fR\& is an optional translation modifier (of which \fIt\fR\& is the only supported, meaning Unicode translation)\&. .LP .TP 2 * \fIC\fR\& determines the type of control sequence\&. .LP .RE .RS 2 .LP Unless otherwise specified, leading whitespace is ignored for all control sequences\&. An input field cannot be more than one line wide\&. .RE .RS 2 .LP \fIAvailable control sequences:\fR\& .RE .RS 2 .TP 2 .B \fI~\fR\&: A single \fI~\fR\& is expected in the input\&. .TP 2 .B \fId\fR\&: A decimal integer is expected\&. .TP 2 .B \fIu\fR\&: An unsigned integer in base 2-36 is expected\&. The field width parameter is used to specify base\&. Leading whitespace characters are not skipped\&. .TP 2 .B \fI-\fR\&: An optional sign character is expected\&. A sign character \fI-\fR\& gives return value \fI-1\fR\&\&. Sign character \fI+\fR\& or none gives \fI1\fR\&\&. The field width parameter is ignored\&. Leading whitespace characters are not skipped\&. .TP 2 .B \fI#\fR\&: An integer in base 2-36 with Erlang-style base prefix (for example, \fI"16#ffff"\fR\&) is expected\&. .TP 2 .B \fIf\fR\&: A floating point number is expected\&. It must follow the Erlang floating point number syntax\&. .TP 2 .B \fIs\fR\&: A string of non-whitespace characters is read\&. If a field width has been specified, this number of characters are read and all trailing whitespace characters are stripped\&. An Erlang string (list of characters) is returned\&. .RS 2 .LP If Unicode translation is in effect (\fI~ts\fR\&), characters > 255 are accepted, otherwise not\&. With the translation modifier, the returned list can as a consequence also contain integers > 255: .RE .LP .nf 1> io:fread("Prompt> ","~s")\&. Prompt> {error,{fread,string}} 2> io:fread("Prompt> ","~ts")\&. Prompt> {ok,[[1091,1085,1080,1094,1086,1076,1077]]} .fi .TP 2 .B \fIa\fR\&: Similar to \fIs\fR\&, but the resulting string is converted into an atom\&. .TP 2 .B \fIc\fR\&: The number of characters equal to the field width are read (default is 1) and returned as an Erlang string\&. However, leading and trailing whitespace characters are not omitted as they are with \fIs\fR\&\&. All characters are returned\&. .RS 2 .LP The Unicode translation modifier works as with \fIs\fR\&: .RE .LP .nf 1> io:fread("Prompt> ","~c")\&. Prompt> {error,{fread,string}} 2> io:fread("Prompt> ","~tc")\&. Prompt> {ok,[[1091]]} .fi .TP 2 .B \fIl\fR\&: Returns the number of characters that have been scanned up to that point, including whitespace characters\&. .RE .RS 2 .LP The function returns: .RE .RS 2 .TP 2 .B \fI{ok, Terms}\fR\&: The read was successful and \fITerms\fR\& is the list of successfully matched and read items\&. .TP 2 .B \fIeof\fR\&: End of file was encountered\&. .TP 2 .B \fI{error, FreadError}\fR\&: The reading failed and \fIFreadError\fR\& gives a hint about the error\&. .TP 2 .B \fI{error, ErrorDescription}\fR\&: The read operation failed and parameter \fIErrorDescription\fR\& gives a hint about the error\&. .RE .LP .RE .LP \fIExamples:\fR\& .LP .nf 20> io:fread(\&'enter>\&', "~f~f~f")\&. enter>1\&.9 35\&.5e3 15\&.0 {ok,[1.9,3.55e4,15.0]} 21> io:fread(\&'enter>\&', "~10f~d")\&. enter> 5\&.67899 {ok,[5.678,99]} 22> io:fread(\&'enter>\&', ":~10s:~10c:")\&. enter>: alan : joe : {ok, ["alan", " joe "]} .fi .RE .LP .nf .B get_chars(Prompt, Count) -> Data | server_no_data() .br .fi .br .nf .B get_chars(IoDevice, Prompt, Count) -> Data | server_no_data() .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br Prompt = prompt() .br Count = integer() >= 0 .br Data = string() | unicode:unicode_binary() .br .nf \fBserver_no_data()\fR\& = {error, ErrorDescription :: term()} | eof .fi .br .RE .RE .RS .LP Reads \fICount\fR\& characters from standard input (\fIIoDevice\fR\&), prompting it with \fIPrompt\fR\&\&. .LP The function returns: .RS 2 .TP 2 .B \fIData\fR\&: The input characters\&. If the I/O device supports Unicode, the data can represent codepoints > 255 (the \fIlatin1\fR\& range)\&. If the I/O server is set to deliver binaries, they are encoded in UTF-8 (regardless of whether the I/O device supports Unicode)\&. .TP 2 .B \fIeof\fR\&: End of file was encountered\&. .TP 2 .B \fI{error, ErrorDescription}\fR\&: Other (rare) error condition, such as \fI{error, estale}\fR\& if reading from an NFS file system\&. .RE .RE .LP .nf .B get_line(Prompt) -> Data | server_no_data() .br .fi .br .nf .B get_line(IoDevice, Prompt) -> Data | server_no_data() .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br Prompt = prompt() .br Data = string() | unicode:unicode_binary() .br .nf \fBserver_no_data()\fR\& = {error, ErrorDescription :: term()} | eof .fi .br .RE .RE .RS .LP Reads a line from the standard input (\fIIoDevice\fR\&), prompting it with \fIPrompt\fR\&\&. .LP The function returns: .RS 2 .TP 2 .B \fIData\fR\&: The characters in the line terminated by a line feed (or end of file)\&. If the I/O device supports Unicode, the data can represent codepoints > 255 (the \fIlatin1\fR\& range)\&. If the I/O server is set to deliver binaries, they are encoded in UTF-8 (regardless of if the I/O device supports Unicode)\&. .TP 2 .B \fIeof\fR\&: End of file was encountered\&. .TP 2 .B \fI{error, ErrorDescription}\fR\&: Other (rare) error condition, such as \fI{error, estale}\fR\& if reading from an NFS file system\&. .RE .RE .LP .nf .B getopts() -> [opt_pair()] | {error, Reason} .br .fi .br .nf .B getopts(IoDevice) -> [opt_pair()] | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br Reason = term() .br .RE .RE .RS .LP Requests all available options and their current values for a specific I/O device, for example: .LP .nf 1> {ok,F} = file:open("/dev/null",[read])\&. {ok,<0.42.0>} 2> io:getopts(F)\&. [{binary,false},{encoding,latin1}] .fi .LP Here the file I/O server returns all available options for a file, which are the expected ones, \fIencoding\fR\& and \fIbinary\fR\&\&. However, the standard shell has some more options: .LP .nf 3> io:getopts(). [{expand_fun,#Fun}, {echo,true}, {binary,false}, {encoding,unicode}] .fi .LP This example is, as can be seen, run in an environment where the terminal supports Unicode input and output\&. .RE .LP .nf .B nl() -> ok .br .fi .br .nf .B nl(IoDevice) -> ok .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br .RE .RE .RS .LP Writes new line to the standard output (\fIIoDevice\fR\&)\&. .RE .LP .nf .B parse_erl_exprs(Prompt) -> Result .br .fi .br .nf .B parse_erl_exprs(IoDevice, Prompt) -> Result .br .fi .br .nf .B parse_erl_exprs(IoDevice, Prompt, StartLocation) -> Result .br .fi .br .nf .B parse_erl_exprs(IoDevice, Prompt, StartLocation, Options) -> .B Result .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br Prompt = prompt() .br StartLocation = location() .br Options = erl_scan:options() .br Result = parse_ret() .br .nf \fBparse_ret()\fR\& = .br {ok, .br ExprList :: [erl_parse:abstract_expr()], .br EndLocation :: location()} | .br {eof, EndLocation :: location()} | .br {error, .br ErrorInfo :: erl_scan:error_info() | erl_parse:error_info(), .br ErrorLocation :: location()} | .br server_no_data() .fi .br .nf \fBserver_no_data()\fR\& = {error, ErrorDescription :: term()} | eof .fi .br .RE .RE .RS .LP Reads data from the standard input (\fIIoDevice\fR\&), prompting it with \fIPrompt\fR\&\&. Starts reading at location \fIStartLocation\fR\& (\fI1\fR\&)\&. Argument \fIOptions\fR\& is passed on as argument \fIOptions\fR\& of function \fIerl_scan:tokens/4\fR\&\&. The data is tokenized and parsed as if it was a sequence of Erlang expressions until a final dot (\fI\&.\fR\&) is reached\&. .LP The function returns: .RS 2 .TP 2 .B \fI{ok, ExprList, EndLocation}\fR\&: The parsing was successful\&. .TP 2 .B \fI{eof, EndLocation}\fR\&: End of file was encountered by the tokenizer\&. .TP 2 .B \fIeof\fR\&: End of file was encountered by the I/O server\&. .TP 2 .B \fI{error, ErrorInfo, ErrorLocation}\fR\&: An error occurred while tokenizing or parsing\&. .TP 2 .B \fI{error, ErrorDescription}\fR\&: Other (rare) error condition, such as \fI{error, estale}\fR\& if reading from an NFS file system\&. .RE .LP Example: .LP .nf 25> io:parse_erl_exprs(\&'enter>\&')\&. enter>abc(), "hey"\&. {ok, [{call,1,{atom,1,abc},[]},{string,1,"hey"}],2} 26> io:parse_erl_exprs (\&'enter>\&')\&. enter>abc("hey"\&. {error,{1,erl_parse,["syntax error before: ",["'.'"]]},2} .fi .RE .LP .nf .B parse_erl_form(Prompt) -> Result .br .fi .br .nf .B parse_erl_form(IoDevice, Prompt) -> Result .br .fi .br .nf .B parse_erl_form(IoDevice, Prompt, StartLocation) -> Result .br .fi .br .nf .B parse_erl_form(IoDevice, Prompt, StartLocation, Options) -> Result .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br Prompt = prompt() .br StartLocation = location() .br Options = erl_scan:options() .br Result = parse_form_ret() .br .nf \fBparse_form_ret()\fR\& = .br {ok, .br AbsForm :: erl_parse:abstract_form(), .br EndLocation :: location()} | .br {eof, EndLocation :: location()} | .br {error, .br ErrorInfo :: erl_scan:error_info() | erl_parse:error_info(), .br ErrorLocation :: location()} | .br server_no_data() .fi .br .nf \fBserver_no_data()\fR\& = {error, ErrorDescription :: term()} | eof .fi .br .RE .RE .RS .LP Reads data from the standard input (\fIIoDevice\fR\&), prompting it with \fIPrompt\fR\&\&. Starts reading at location \fIStartLocation\fR\& (\fI1\fR\&)\&. Argument \fIOptions\fR\& is passed on as argument \fIOptions\fR\& of function \fIerl_scan:tokens/4\fR\&\&. The data is tokenized and parsed as if it was an Erlang form (one of the valid Erlang expressions in an Erlang source file) until a final dot (\fI\&.\fR\&) is reached\&. .LP The function returns: .RS 2 .TP 2 .B \fI{ok, AbsForm, EndLocation}\fR\&: The parsing was successful\&. .TP 2 .B \fI{eof, EndLocation}\fR\&: End of file was encountered by the tokenizer\&. .TP 2 .B \fIeof\fR\&: End of file was encountered by the I/O server\&. .TP 2 .B \fI{error, ErrorInfo, ErrorLocation}\fR\&: An error occurred while tokenizing or parsing\&. .TP 2 .B \fI{error, ErrorDescription}\fR\&: Other (rare) error condition, such as \fI{error, estale}\fR\& if reading from an NFS file system\&. .RE .RE .LP .nf .B printable_range() -> unicode | latin1 .br .fi .br .RS .LP Returns the user-requested range of printable Unicode characters\&. .LP The user can request a range of characters that are to be considered printable in heuristic detection of strings by the shell and by the formatting functions\&. This is done by supplying \fI+pc \fR\& when starting Erlang\&. .LP The only valid values for \fI\fR\& are \fIlatin1\fR\& and \fIunicode\fR\&\&. \fIlatin1\fR\& means that only code points < 256 (except control characters, and so on) are considered printable\&. \fIunicode\fR\& means that all printable characters in all Unicode character ranges are considered printable by the I/O functions\&. .LP By default, Erlang is started so that only the \fIlatin1\fR\& range of characters indicate that a list of integers is a string\&. .LP The simplest way to use the setting is to call \fIio_lib:printable_list/1\fR\&, which uses the return value of this function to decide if a list is a string of printable characters\&. .LP .RS -4 .B Note: .RE In a future release, this function may return more values and ranges\&. To avoid compatibility problems, it is recommended to use function \fIio_lib:printable_list/1\fR\&\&. .RE .LP .nf .B put_chars(CharData) -> ok .br .fi .br .nf .B put_chars(IoDevice, CharData) -> ok .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br CharData = unicode:chardata() .br .RE .RE .RS .LP Writes the characters of \fICharData\fR\& to the I/O server (\fIIoDevice\fR\&)\&. .RE .LP .nf .B read(Prompt) -> Result .br .fi .br .nf .B read(IoDevice, Prompt) -> Result .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br Prompt = prompt() .br Result = .br {ok, Term :: term()} | server_no_data() | {error, ErrorInfo} .br ErrorInfo = erl_scan:error_info() | erl_parse:error_info() .br .nf \fBserver_no_data()\fR\& = {error, ErrorDescription :: term()} | eof .fi .br .RE .RE .RS .LP Reads a term \fITerm\fR\& from the standard input (\fIIoDevice\fR\&), prompting it with \fIPrompt\fR\&\&. .LP The function returns: .RS 2 .TP 2 .B \fI{ok, Term}\fR\&: The parsing was successful\&. .TP 2 .B \fIeof\fR\&: End of file was encountered\&. .TP 2 .B \fI{error, ErrorInfo}\fR\&: The parsing failed\&. .TP 2 .B \fI{error, ErrorDescription}\fR\&: Other (rare) error condition, such as \fI{error, estale}\fR\& if reading from an NFS file system\&. .RE .RE .LP .nf .B read(IoDevice, Prompt, StartLocation) -> Result .br .fi .br .nf .B read(IoDevice, Prompt, StartLocation, Options) -> Result .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br Prompt = prompt() .br StartLocation = location() .br Options = erl_scan:options() .br Result = .br {ok, Term :: term(), EndLocation :: location()} | .br {eof, EndLocation :: location()} | .br server_no_data() | .br {error, ErrorInfo, ErrorLocation :: location()} .br ErrorInfo = erl_scan:error_info() | erl_parse:error_info() .br .nf \fBserver_no_data()\fR\& = {error, ErrorDescription :: term()} | eof .fi .br .RE .RE .RS .LP Reads a term \fITerm\fR\& from \fIIoDevice\fR\&, prompting it with \fIPrompt\fR\&\&. Reading starts at location \fIStartLocation\fR\&\&. Argument \fIOptions\fR\& is passed on as argument \fIOptions\fR\& of function \fIerl_scan:tokens/4\fR\&\&. .LP The function returns: .RS 2 .TP 2 .B \fI{ok, Term, EndLocation}\fR\&: The parsing was successful\&. .TP 2 .B \fI{eof, EndLocation}\fR\&: End of file was encountered\&. .TP 2 .B \fI{error, ErrorInfo, ErrorLocation}\fR\&: The parsing failed\&. .TP 2 .B \fI{error, ErrorDescription}\fR\&: Other (rare) error condition, such as \fI{error, estale}\fR\& if reading from an NFS file system\&. .RE .RE .LP .nf .B rows() -> {ok, integer() >= 1} | {error, enotsup} .br .fi .br .nf .B rows(IoDevice) -> {ok, integer() >= 1} | {error, enotsup} .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br .RE .RE .RS .LP Retrieves the number of rows of \fIIoDevice\fR\& (that is, the height of a terminal)\&. The function only succeeds for terminal devices, for all other I/O devices the function returns \fI{error, enotsup}\fR\&\&. .RE .LP .nf .B scan_erl_exprs(Prompt) -> Result .br .fi .br .nf .B scan_erl_exprs(Device, Prompt) -> Result .br .fi .br .nf .B scan_erl_exprs(Device, Prompt, StartLocation) -> Result .br .fi .br .nf .B scan_erl_exprs(Device, Prompt, StartLocation, Options) -> Result .br .fi .br .RS .LP Types: .RS 3 Device = device() .br Prompt = prompt() .br StartLocation = location() .br Options = erl_scan:options() .br Result = erl_scan:tokens_result() | server_no_data() .br .nf \fBserver_no_data()\fR\& = {error, ErrorDescription :: term()} | eof .fi .br .RE .RE .RS .LP Reads data from the standard input (\fIIoDevice\fR\&), prompting it with \fIPrompt\fR\&\&. Reading starts at location \fIStartLocation\fR\& (\fI1\fR\&)\&. Argument \fIOptions\fR\& is passed on as argument \fIOptions\fR\& of function \fIerl_scan:tokens/4\fR\&\&. The data is tokenized as if it were a sequence of Erlang expressions until a final dot (\fI\&.\fR\&) is reached\&. This token is also returned\&. .LP The function returns: .RS 2 .TP 2 .B \fI{ok, Tokens, EndLocation}\fR\&: The tokenization succeeded\&. .TP 2 .B \fI{eof, EndLocation}\fR\&: End of file was encountered by the tokenizer\&. .TP 2 .B \fIeof\fR\&: End of file was encountered by the I/O server\&. .TP 2 .B \fI{error, ErrorInfo, ErrorLocation}\fR\&: An error occurred while tokenizing\&. .TP 2 .B \fI{error, ErrorDescription}\fR\&: Other (rare) error condition, such as \fI{error, estale}\fR\& if reading from an NFS file system\&. .RE .LP \fIExample:\fR\& .LP .nf 23> io:scan_erl_exprs(\&'enter>\&')\&. enter>abc(), "hey"\&. {ok,[{atom,1,abc},{'(',1},{')',1},{',',1},{string,1,"hey"},{dot,1}],2} 24> io:scan_erl_exprs(\&'enter>\&')\&. enter>1\&.0er\&. {error,{1,erl_scan,{illegal,float}},2} .fi .RE .LP .nf .B scan_erl_form(Prompt) -> Result .br .fi .br .nf .B scan_erl_form(IoDevice, Prompt) -> Result .br .fi .br .nf .B scan_erl_form(IoDevice, Prompt, StartLocation) -> Result .br .fi .br .nf .B scan_erl_form(IoDevice, Prompt, StartLocation, Options) -> Result .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br Prompt = prompt() .br StartLocation = location() .br Options = erl_scan:options() .br Result = erl_scan:tokens_result() | server_no_data() .br .nf \fBserver_no_data()\fR\& = {error, ErrorDescription :: term()} | eof .fi .br .RE .RE .RS .LP Reads data from the standard input (\fIIoDevice\fR\&), prompting it with \fIPrompt\fR\&\&. Starts reading at location \fIStartLocation\fR\& (\fI1\fR\&)\&. Argument \fIOptions\fR\& is passed on as argument \fIOptions\fR\& of function \fIerl_scan:tokens/4\fR\&\&. The data is tokenized as if it was an Erlang form (one of the valid Erlang expressions in an Erlang source file) until a final dot (\fI\&.\fR\&) is reached\&. This last token is also returned\&. .LP The return values are the same as for \fIscan_erl_exprs/1,2,3,4\fR\&\&. .RE .LP .nf .B setopts(Opts) -> ok | {error, Reason} .br .fi .br .nf .B setopts(IoDevice, Opts) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br Opts = [setopt()] .br Reason = term() .br .RE .RE .RS .LP Set options for the standard I/O device (\fIIoDevice\fR\&)\&. .LP Possible options and values vary depending on the I/O device\&. For a list of supported options and their current values on a specific I/O device, use function \fIgetopts/1\fR\&\&. .LP The options and values supported by the OTP I/O devices are as follows: .RS 2 .TP 2 .B \fIbinary\fR\&, \fIlist\fR\&, or \fI{binary, boolean()}\fR\&: If set in binary mode (\fIbinary\fR\& or \fI{binary, true}\fR\&), the I/O server sends binary data (encoded in UTF-8) as answers to the \fIget_line\fR\&, \fIget_chars\fR\&, and, if possible, \fIget_until\fR\& requests (for details, see section The Erlang I/O Protocol) in the User\&'s Guide)\&. The immediate effect is that \fIget_chars/2,3\fR\& and \fIget_line/1,2\fR\& return UTF-8 binaries instead of lists of characters for the affected I/O device\&. .RS 2 .LP By default, all I/O devices in OTP are set in \fIlist\fR\& mode\&. However, the I/O functions can handle any of these modes and so should other, user-written, modules behaving as clients to I/O servers\&. .RE .RS 2 .LP This option is supported by the standard shell (\fIgroup\&.erl\fR\&), the \&'oldshell\&' (\fIuser\&.erl\fR\&), and the file I/O servers\&. .RE .TP 2 .B \fI{echo, boolean()}\fR\&: Denotes if the terminal is to echo input\&. Only supported for the standard shell I/O server (\fIgroup\&.erl\fR\&) .TP 2 .B \fI{expand_fun, expand_fun()}\fR\&: Provides a function for tab-completion (expansion) like the Erlang shell\&. This function is called when the user presses the \fITab\fR\& key\&. The expansion is active when calling line-reading functions, such as \fIget_line/1,2\fR\&\&. .RS 2 .LP The function is called with the current line, up to the cursor, as a reversed string\&. It is to return a three-tuple: \fI{yes|no, string(), [string(), \&.\&.\&.]}\fR\&\&. The first element gives a beep if \fIno\fR\&, otherwise the expansion is silent; the second is a string that will be entered at the cursor position; the third is a list of possible expansions\&. If this list is not empty, it is printed and the current input line is written once again\&. .RE .RS 2 .LP Trivial example (beep on anything except empty line, which is expanded to \fI"quit"\fR\&): .RE .LP .nf fun("") -> {yes, "quit", []}; (_) -> {no, "", ["quit"]} end .fi .RS 2 .LP This option is only supported by the standard shell (\fIgroup\&.erl\fR\&)\&. .RE .TP 2 .B \fI{encoding, latin1 | unicode}\fR\&: Specifies how characters are input or output from or to the I/O device, implying that, for example, a terminal is set to handle Unicode input and output or a file is set to handle UTF-8 data encoding\&. .RS 2 .LP The option \fIdoes not\fR\& affect how data is returned from the I/O functions or how it is sent in the I/O protocol, it only affects how the I/O device is to handle Unicode characters to the "physical" device\&. .RE .RS 2 .LP The standard shell is set for \fIunicode\fR\& or \fIlatin1\fR\& encoding when the system is started\&. The encoding is set with the help of the \fILANG\fR\& or \fILC_CTYPE\fR\& environment variables on Unix-like system or by other means on other systems\&. So, the user can input Unicode characters and the I/O device is in \fI{encoding, unicode}\fR\& mode if the I/O device supports it\&. The mode can be changed, if the assumption of the runtime system is wrong, by setting this option\&. .RE .RS 2 .LP The I/O device used when Erlang is started with the "-oldshell" or "-noshell" flags is by default set to \fIlatin1\fR\& encoding, meaning that any characters > codepoint 255 are escaped and that input is expected to be plain 8-bit ISO Latin-1\&. If the encoding is changed to Unicode, input and output from the standard file descriptors are in UTF-8 (regardless of operating system)\&. .RE .RS 2 .LP Files can also be set in \fI{encoding, unicode}\fR\&, meaning that data is written and read as UTF-8\&. More encodings are possible for files, see below\&. .RE .RS 2 .LP \fI{encoding, unicode | latin1}\fR\& is supported by both the standard shell (\fIgroup\&.erl\fR\& including \fIwerl\fR\& on Windows), the \&'oldshell\&' (\fIuser\&.erl\fR\&), and the file I/O servers\&. .RE .TP 2 .B \fI{encoding, utf8 | utf16 | utf32 | {utf16,big} | {utf16,little} | {utf32,big} | {utf32,little}}\fR\&: For disk files, the encoding can be set to various UTF variants\&. This has the effect that data is expected to be read as the specified encoding from the file, and the data is written in the specified encoding to the disk file\&. .RS 2 .LP \fI{encoding, utf8}\fR\& has the same effect as \fI{encoding, unicode}\fR\& on files\&. .RE .RS 2 .LP The extended encodings are only supported on disk files (opened by function \fIfile:open/2\fR\&)\&. .RE .RE .RE .LP .nf .B write(Term) -> ok .br .fi .br .nf .B write(IoDevice, Term) -> ok .br .fi .br .RS .LP Types: .RS 3 IoDevice = device() .br Term = term() .br .RE .RE .RS .LP Writes term \fITerm\fR\& to the standard output (\fIIoDevice\fR\&)\&. .RE .SH "STANDARD INPUT/OUTPUT" .LP All Erlang processes have a default standard I/O device\&. This device is used when no \fIIoDevice\fR\& argument is specified in the function calls in this module\&. However, it is sometimes desirable to use an explicit \fIIoDevice\fR\& argument that refers to the default I/O device\&. This is the case with functions that can access either a file or the default I/O device\&. The atom \fIstandard_io\fR\& has this special meaning\&. The following example illustrates this: .LP .nf 27> io:read(\&'enter>\&')\&. enter>foo\&. {ok,foo} 28> io:read(standard_io, \&'enter>\&')\&. enter>bar\&. {ok,bar} .fi .LP \fIstandard_io\fR\& is an alias for \fI group_leader/0\fR\&, so in order to change where the default input/output requests are sent you can change the group leader for the current process using \fI group_leader(NewGroupLeader, self())\&.\fR\& .LP There is always a process registered under the name of \fIuser\fR\&\&. This can be used for sending output to the user\&. .SH "STANDARD ERROR" .LP In certain situations, especially when the standard output is redirected, access to an I/O server specific for error messages can be convenient\&. The I/O device \fIstandard_error\fR\& can be used to direct output to whatever the current operating system considers a suitable I/O device for error output\&. Example on a Unix-like operating system: .LP .nf $ erl -noshell -noinput -eval \&'io:format(standard_error,"Error: ~s~n",["error 11"]),\&'\\ \&'init:stop()\&.\&' > /dev/null Error: error 11 .fi .SH "ERROR INFORMATION" .LP The \fIErrorInfo\fR\& mentioned in this module is the standard \fIErrorInfo\fR\& structure that is returned from all I/O modules\&. It has the following format: .LP .nf {ErrorLocation, Module, ErrorDescriptor} .fi .LP A string that describes the error is obtained with the following call: .LP .nf Module:format_error(ErrorDescriptor) .fi