| 1 |
#ifndef RBIMPL_INTERN_IO_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_IO_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Public APIs related to ::rb_cIO. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/dllexport.h" |
| 24 |
#include "ruby/internal/value.h" |
| 25 |
|
| 26 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 27 |
|
| 28 |
/* io.c */ |
| 29 |
|
| 30 |
/** |
| 31 |
* @private |
| 32 |
* |
| 33 |
* @deprecated This macro once was a thing in the old days, but makes no sense |
| 34 |
* any longer today. Exists here for backwards compatibility |
| 35 |
* only. You can safely forget about it. |
| 36 |
*/ |
| 37 |
#define rb_defout rb_stdout |
| 38 |
|
| 39 |
/* string.c */ /* ...why? moved in commit de7161526014b781468cea5d84411e23be */ |
| 40 |
|
| 41 |
/** |
| 42 |
* The field separator character for inputs, or the `$;`. This affects how |
| 43 |
* `String#split` works. You can set this via the `-F` command line option. |
| 44 |
* You can also assign arbitrary ruby objects programmatically, but it makes |
| 45 |
* best sense for you to assign a regular expression here. |
| 46 |
* |
| 47 |
* @internal |
| 48 |
* |
| 49 |
* Tidbit: "fs" comes from AWK's `FS` variable. |
| 50 |
*/ |
| 51 |
RUBY_EXTERN VALUE rb_fs; |
| 52 |
|
| 53 |
/* io.c */ /* ...why? given rb_fs is in string.c? */ |
| 54 |
|
| 55 |
/** |
| 56 |
* The field separator character for outputs, or the `$,`. This affects how |
| 57 |
* `Array#join` works. |
| 58 |
* |
| 59 |
* @deprecated Assigning anything other than ::RUBY_Qnil to this variable is |
| 60 |
* deprecated. |
| 61 |
*/ |
| 62 |
RUBY_EXTERN VALUE rb_output_fs; |
| 63 |
|
| 64 |
/** |
| 65 |
* The record separator character for inputs, or the `$/`. This affects how |
| 66 |
* `IO#gets` works. You can set this via the `-0` command line option. |
| 67 |
* |
| 68 |
* @deprecated Assigning anything other than ::RUBY_Qnil to this variable is |
| 69 |
* deprecated. |
| 70 |
* |
| 71 |
* @internal |
| 72 |
* |
| 73 |
* Tidbit: "rs" comes from AWK's `RS` variable. |
| 74 |
*/ |
| 75 |
RUBY_EXTERN VALUE rb_rs; |
| 76 |
|
| 77 |
/** |
| 78 |
* This is the default value of ::rb_rs, i.e. `"\n"`. It seems it has always |
| 79 |
* been just a newline string since the beginning. Not sure why C codes has to |
| 80 |
* use this, given there is no way for ruby programs to interface. |
| 81 |
* |
| 82 |
* Also it has not been deprecated for unknown reasons. |
| 83 |
*/ |
| 84 |
RUBY_EXTERN VALUE rb_default_rs; |
| 85 |
|
| 86 |
/** |
| 87 |
* The record separator character for outputs, or the `$\`. This affects how |
| 88 |
* `IO#print` works. |
| 89 |
* |
| 90 |
* @deprecated Assigning anything other than ::RUBY_Qnil to this variable is |
| 91 |
* deprecated. |
| 92 |
*/ |
| 93 |
RUBY_EXTERN VALUE rb_output_rs; |
| 94 |
|
| 95 |
/** |
| 96 |
* Writes the given string to the given IO. |
| 97 |
* |
| 98 |
* @param[out] io An IO, opened for writing. |
| 99 |
* @param[in] str A String-like object to write to `io`. |
| 100 |
* @exception rb_eIOError `io` isn't opened for writing. |
| 101 |
* @exception rb_eFrozenError `io` is frozen. |
| 102 |
* @exception rb_eTypeError No conversion from `str` to String. |
| 103 |
* @exception rb_eSystemCallError `write(2)` failed for some reason. |
| 104 |
* @return The number of bytes written to the `io`. |
| 105 |
* @post `str` (up to the length of return value) is written to `io`. |
| 106 |
* @note This function blocks. |
| 107 |
* @note Partial write is a thing. It must be at least questionable not |
| 108 |
* to check the return value. |
| 109 |
* |
| 110 |
* @internal |
| 111 |
* |
| 112 |
* Above description is in fact inaccurate. This function can take arbitrary |
| 113 |
* objects, and calls their `write` method. What is written above in fact |
| 114 |
* describes how `IO#write` works. You can pass StringIO etc. here, and would |
| 115 |
* work completely differently. |
| 116 |
*/ |
| 117 |
VALUE rb_io_write(VALUE io, VALUE str); |
| 118 |
|
| 119 |
/** |
| 120 |
* Reads a "line" from the given IO. A line here means a chunk of characters |
| 121 |
* which is terminated by either `"\n"` or an EOF. |
| 122 |
* |
| 123 |
* @param[in,out] io An IO, opened for reading. |
| 124 |
* @exception rb_eIOError `io` isn't opened for reading. |
| 125 |
* @exception rb_eFrozenError `io` is frozen. |
| 126 |
* @retval RUBY_Qnil `io` is at EOF. |
| 127 |
* @retval otherwise An instance of ::rb_cString. |
| 128 |
* @post `io` is read. |
| 129 |
* @note Unlike `IO#gets` it doesn't set `$_`. |
| 130 |
* @note Unlike `IO#gets` it doesn't consider `$/`. |
| 131 |
*/ |
| 132 |
VALUE rb_io_gets(VALUE io); |
| 133 |
|
| 134 |
/** |
| 135 |
* Reads a byte from the given IO. |
| 136 |
* |
| 137 |
* @note In Ruby a "byte" always means an 8 bit integer ranging from |
| 138 |
* 0 to 255 inclusive. |
| 139 |
* @param[in,out] io An IO, opened for reading. |
| 140 |
* @exception rb_eIOError `io` is not opened for reading. |
| 141 |
* @exception rb_eFrozenError `io` is frozen. |
| 142 |
* @retval RUBY_Qnil `io` is at EOF. |
| 143 |
* @retval otherwise An instance of ::rb_cInteger. |
| 144 |
* @post `io` is read. |
| 145 |
* |
| 146 |
* @internal |
| 147 |
* |
| 148 |
* Of course there was a function called `rb_io_getc()`. It was removed in |
| 149 |
* commit a25fbe3b3e531bbe479f344af24eaf9d2eeae6ea. |
| 150 |
*/ |
| 151 |
VALUE rb_io_getbyte(VALUE io); |
| 152 |
|
| 153 |
/** |
| 154 |
* "Unget"s a string. This function pushes back the passed string onto the |
| 155 |
* passed IO, such that a subsequent buffered read will return it. If the |
| 156 |
* passed content is in fact an integer, a single character string of that |
| 157 |
* codepoint of the encoding of the IO will be pushed back instead. |
| 158 |
* |
| 159 |
* It might be counter-intuitive but this function can push back multiple |
| 160 |
* characters at once. Also this function can be called multiple times on a |
| 161 |
* same IO. Also a "character" can be wider than a byte, depending on the |
| 162 |
* encoding of the IO. |
| 163 |
* |
| 164 |
* @param[out] io An IO, opened for reading. |
| 165 |
* @param[in] c Either a String, or an Integer. |
| 166 |
* @exception rb_eIOError `io` is not opened for reading. |
| 167 |
* @exception rb_eFrozenError `io` is frozen. |
| 168 |
* @exception rb_eTypeError No conversion from `c` to ::rb_cString. |
| 169 |
* @return Always returns ::RUBY_Qnil. |
| 170 |
* |
| 171 |
* @internal |
| 172 |
* |
| 173 |
* Why there is ungetc, given there is no getc? |
| 174 |
*/ |
| 175 |
VALUE rb_io_ungetc(VALUE io, VALUE c); |
| 176 |
|
| 177 |
/** |
| 178 |
* Identical to rb_io_ungetc(), except it doesn't take the encoding of the |
| 179 |
* passed IO into account. When an integer is passed, it just casts that value |
| 180 |
* to C's `unsigned char`, and pushes that back. |
| 181 |
* |
| 182 |
* @param[out] io An IO, opened for reading. |
| 183 |
* @param[in] b Either a String, or an Integer. |
| 184 |
* @exception rb_eIOError `io` is not opened for reading. |
| 185 |
* @exception rb_eFrozenError `io` is frozen. |
| 186 |
* @exception rb_eTypeError No conversion from `b` to ::rb_cString. |
| 187 |
* @return Always returns ::RUBY_Qnil. |
| 188 |
*/ |
| 189 |
VALUE rb_io_ungetbyte(VALUE io, VALUE b); |
| 190 |
|
| 191 |
/** |
| 192 |
* Closes the IO. Any buffered contents are flushed to the operating system. |
| 193 |
* Any future operations against the IO would raise ::rb_eIOError. In case the |
| 194 |
* io was created using `IO.popen`, it also sets the `$?`. |
| 195 |
* |
| 196 |
* @param[out] io Target IO to close. |
| 197 |
* @return Always returns ::RUBY_Qnil. |
| 198 |
* @post `$?` is set in case IO is a pipe. |
| 199 |
* @post No operations are possible against `io` any further. |
| 200 |
* @note This can block to flush the contents. |
| 201 |
* @note This can wake other threads up, especially those who are |
| 202 |
* `select()`-ing the passed IO. |
| 203 |
* @note Multiple invocations of this function over the same IO again |
| 204 |
* and again is not an error, since Ruby 2.3. |
| 205 |
* |
| 206 |
* @internal |
| 207 |
* |
| 208 |
* You can close a frozen IO... Is this intentional? |
| 209 |
*/ |
| 210 |
VALUE rb_io_close(VALUE io); |
| 211 |
|
| 212 |
/** |
| 213 |
* Flushes any buffered data within the passed IO to the underlying operating |
| 214 |
* system. |
| 215 |
* |
| 216 |
* @param[out] io Target IO to flush. |
| 217 |
* @exception rb_eIOError `io` is closed. |
| 218 |
* @exception rb_eFrozenError `io` is frozen. |
| 219 |
* @exception rb_eSystemCallError `write(2)` failed for some reason. |
| 220 |
* @return The passed `io`. |
| 221 |
* @post `io`'s buffers are empty. |
| 222 |
* @note This operation also discards the read buffer. Should basically |
| 223 |
* be harmless, but in an esoteric situation like when user pushed |
| 224 |
* something different from what was read using `ungetc`, this |
| 225 |
* operation in fact changes the behaviour of the `io`. |
| 226 |
* @note Buffering is difficult. This operation flushes the data from |
| 227 |
* our userspace to the kernel, but that doesn't always mean you |
| 228 |
* can expect them stored persistently onto your hard drive. |
| 229 |
*/ |
| 230 |
VALUE rb_io_flush(VALUE io); |
| 231 |
|
| 232 |
/** |
| 233 |
* Queries if the passed IO is at the end of file. "The end of file" here mans |
| 234 |
* that there are no more data to read. This function blocks until the read |
| 235 |
* buffer is filled in, and if that operation reached the end of file, it still |
| 236 |
* returns ::RUBY_Qfalse (because there are data yet in that buffer). It |
| 237 |
* returns ::RUBY_Qtrue once after the buffer is cleared. |
| 238 |
* |
| 239 |
* @param[in,out] io Target io to query. |
| 240 |
* @exception rb_eIOError `io` is not opened for reading. |
| 241 |
* @exception rb_eFrozenError `io` is frozen. |
| 242 |
* @retval RUBY_Qfalse There are things yet to be read. |
| 243 |
* @retval RUBY_Qtrue "The end of file" situation. |
| 244 |
*/ |
| 245 |
VALUE rb_io_eof(VALUE io); |
| 246 |
|
| 247 |
/** |
| 248 |
* Sets the binmode. This operation nullifies the effect of textmode (newline |
| 249 |
* conversion from `"\r\n"` to `"\n"` or vice versa). Note that it doesn't |
| 250 |
* stop character encodings conversions. For instance an IO created using: |
| 251 |
* |
| 252 |
* ```ruby |
| 253 |
* File.open( |
| 254 |
* "/dev/urandom", |
| 255 |
* textmode: true, |
| 256 |
* external_encoding: Encoding::GB18030, |
| 257 |
* internal_encoding: Encoding::Windows_31J) |
| 258 |
* ``` |
| 259 |
* |
| 260 |
* has both newline and character conversions. If you pass such IO to this |
| 261 |
* function, only the `textmode:true` part is cancelled. Texts read through |
| 262 |
* the IO would still be encoded in Windows-31J; texts written to the IO will |
| 263 |
* be encoded in GB18030. |
| 264 |
* |
| 265 |
* @param[out] io Target IO to modify. |
| 266 |
* @exception rb_eFrozenError `io` is frozen. |
| 267 |
* @return The passed `io`. |
| 268 |
* @post `io` is in binmode. |
| 269 |
* @note There is no equivalent operation in Ruby. You can do this only |
| 270 |
* in C. |
| 271 |
*/ |
| 272 |
VALUE rb_io_binmode(VALUE io); |
| 273 |
|
| 274 |
/** |
| 275 |
* Forces no conversions be applied to the passed IO. Unlike rb_io_binmode(), |
| 276 |
* this cancels any newline conversions as well as encoding conversions. Any |
| 277 |
* texts read/written through the IO will be the verbatim binary contents. |
| 278 |
* |
| 279 |
* @param[out] io Target IO to modify. |
| 280 |
* @exception rb_eFrozenError `io` is frozen. |
| 281 |
* @return The passed `io`. |
| 282 |
* @post `io` is in binmode. Both external/internal encoding are set to |
| 283 |
* rb_ascii8bit_encoding(). |
| 284 |
* @note This is the implementation of `IO#binmode`. |
| 285 |
*/ |
| 286 |
VALUE rb_io_ascii8bit_binmode(VALUE io); |
| 287 |
|
| 288 |
/** |
| 289 |
* Identical to rb_io_write(), except it always returns the passed IO. |
| 290 |
* |
| 291 |
* @param[out] io An IO, opened for writing. |
| 292 |
* @param[in] str A String-like object to write to `io`. |
| 293 |
* @exception rb_eIOError `io` isn't opened for writing. |
| 294 |
* @exception rb_eFrozenError `io` is frozen. |
| 295 |
* @exception rb_eTypeError No conversion from `str` to String. |
| 296 |
* @exception rb_eSystemCallError `write(2)` failed. |
| 297 |
* @return The passed `io`. |
| 298 |
* @post `str` is written to `io`. |
| 299 |
* @note This function blocks. |
| 300 |
* |
| 301 |
* @internal |
| 302 |
* |
| 303 |
* As rb_io_write(), above description is a fake. |
| 304 |
*/ |
| 305 |
VALUE rb_io_addstr(VALUE io, VALUE str); |
| 306 |
|
| 307 |
/** |
| 308 |
* This is a rb_f_sprintf() + rb_io_write() combo. |
| 309 |
* |
| 310 |
* @param[in] argc Number of objects of `argv`. |
| 311 |
* @param[in] argv A format string followed by its arguments. |
| 312 |
* @param[out] io An IO, opened for writing. |
| 313 |
* @exception rb_eIOError `io` isn't opened for writing. |
| 314 |
* @exception rb_eFrozenError `io` is frozen. |
| 315 |
* @exception rb_eTypeError No conversion from `str` to String. |
| 316 |
* @exception rb_eSystemCallError `write(2)` failed. |
| 317 |
* @return Always returns ::RUBY_Qnil. |
| 318 |
* @post `argv` is formatted, then written to `io`. |
| 319 |
* @note This function blocks. |
| 320 |
* |
| 321 |
* @internal |
| 322 |
* |
| 323 |
* As rb_io_write(), above descriptions include fakes. |
| 324 |
*/ |
| 325 |
VALUE rb_io_printf(int argc, const VALUE *argv, VALUE io); |
| 326 |
|
| 327 |
/** |
| 328 |
* Iterates over the passed array to apply rb_io_write() individually. If |
| 329 |
* there is `$,`, this function inserts the string in middle of each |
| 330 |
* iterations. If there is `$\`, this function appends the string at the end. |
| 331 |
* If the array is empty, this function outputs `$_`. |
| 332 |
* |
| 333 |
* @param[in] argc Number of objects of `argv`. |
| 334 |
* @param[in] argv An array of strings to display. |
| 335 |
* @param[out] io An IO, opened for writing. |
| 336 |
* @exception rb_eIOError `io` isn't opened for writing. |
| 337 |
* @exception rb_eFrozenError `io` is frozen. |
| 338 |
* @exception rb_eTypeError No conversion from `str` to String. |
| 339 |
* @exception rb_eSystemCallError `write(2)` failed. |
| 340 |
* @return Always returns ::RUBY_Qnil. |
| 341 |
* @post `argv` is written to `io`. |
| 342 |
* @note This function blocks. |
| 343 |
* @note This function calls rb_io_write() multiple times. Which means, |
| 344 |
* it is not an atomic operation. Outputs from multiple threads |
| 345 |
* can interleave. |
| 346 |
* |
| 347 |
* @internal |
| 348 |
* |
| 349 |
* As rb_io_write(), above descriptions include fakes. |
| 350 |
*/ |
| 351 |
VALUE rb_io_print(int argc, const VALUE *argv, VALUE io); |
| 352 |
|
| 353 |
/** |
| 354 |
* Iterates over the passed array to apply rb_io_write() individually. Unlike |
| 355 |
* rb_io_print(), this function prints a newline per each element. It also |
| 356 |
* flattens the passed array (OTOH rb_io_print() just resorts to |
| 357 |
* rb_ary_to_s()). |
| 358 |
* |
| 359 |
* @param[in] argc Number of objects of `argv`. |
| 360 |
* @param[in] argv An array of strings to display. |
| 361 |
* @param[out] io An IO, opened for writing. |
| 362 |
* @exception rb_eIOError `io` isn't opened for writing. |
| 363 |
* @exception rb_eFrozenError `io` is frozen. |
| 364 |
* @exception rb_eTypeError No conversion from `str` to String. |
| 365 |
* @exception rb_eSystemCallError `write(2)` failed. |
| 366 |
* @return Always returns ::RUBY_Qnil. |
| 367 |
* @post `argv` is written to `io`. |
| 368 |
* @note This function blocks. |
| 369 |
* @note This function calls rb_io_write() multiple times. Which means, |
| 370 |
* it is not an atomic operation. Outputs from multiple threads |
| 371 |
* can interleave. |
| 372 |
* |
| 373 |
* @internal |
| 374 |
* |
| 375 |
* As rb_io_write(), above descriptions include fakes. |
| 376 |
*/ |
| 377 |
VALUE rb_io_puts(int argc, const VALUE *argv, VALUE io); |
| 378 |
|
| 379 |
/** |
| 380 |
* Creates an IO instance whose backend is the given file descriptor. C |
| 381 |
* extension libraries sometimes have file descriptors created elsewhere (maybe |
| 382 |
* deep inside of another shared library), which they want ruby programs to |
| 383 |
* handle. This function is handy for such situations. |
| 384 |
* |
| 385 |
* @param[in] fd Target file descriptor. |
| 386 |
* @param[in] flags Flags, e.g. `O_CREAT|O_EXCL` |
| 387 |
* @param[in] path The path of the file that backs `fd`, for diagnostics. |
| 388 |
* @return An allocated instance of ::rb_cIO. |
| 389 |
* @note Leave `path` NULL if you don't know. |
| 390 |
*/ |
| 391 |
VALUE rb_io_fdopen(int fd, int flags, const char *path); |
| 392 |
|
| 393 |
RBIMPL_ATTR_NONNULL(()) |
| 394 |
/** |
| 395 |
* Opens a file located at the given path. |
| 396 |
* |
| 397 |
* `fmode` is a C string that represents the open mode. It can be one of: |
| 398 |
* |
| 399 |
* - `r` (means `O_RDONLY`), |
| 400 |
* - `w` (means `O_WRONLY | O_TRUNC | O_CREAT`), |
| 401 |
* - `a` (means `O_WRONLY | O_APPEND | O_CREAT`), |
| 402 |
* |
| 403 |
* Followed by zero or more combinations of: |
| 404 |
* |
| 405 |
* - `b` (means `_O_BINARY`), |
| 406 |
* - `t` (means `_O_TEXT`), |
| 407 |
* - `+` (means `O_RDWR`), |
| 408 |
* - `x` (means `O_TRUNC`), or |
| 409 |
* - `:[BOM|]enc[:enc]` (see below). |
| 410 |
* |
| 411 |
* This last one specifies external (and internal if any) encodings, |
| 412 |
* respectively. If optional `BOM|` is specified and the specified external |
| 413 |
* encoding is capable of expressing BOMs, opening file's contents' byte order |
| 414 |
* is auto-detected using the mechanism. |
| 415 |
* |
| 416 |
* So for instance, fmode of `"rt|BOM:utf-16le:utf-8"` specifies that... |
| 417 |
* |
| 418 |
* - the physical representation of the contents of the file is in UTF-16; |
| 419 |
* - honours its BOM but assumes little endian if absent; |
| 420 |
* - opens the file for reading; |
| 421 |
* - what is read is converted into UTF-8; |
| 422 |
* - with newlines cannibalised to `\n`. |
| 423 |
* |
| 424 |
* @param[in] fname Path to open. |
| 425 |
* @param[in] fmode Mode specifier much like `fopen(3)`. |
| 426 |
* @exception rb_eArgError `fmode` contradicted (e.g. `"bt"`). |
| 427 |
* @exception rb_eSystemCallError `open(2)` failed for some reason. |
| 428 |
* @return An instance of ::rb_cIO. |
| 429 |
*/ |
| 430 |
VALUE rb_file_open(const char *fname, const char *fmode); |
| 431 |
|
| 432 |
RBIMPL_ATTR_NONNULL(()) |
| 433 |
/** |
| 434 |
* Identical to rb_file_open(), except it takes the pathname as a Ruby's string |
| 435 |
* instead of C's. In case the passed Ruby object is a non-String it tries to |
| 436 |
* call `#to_path`. |
| 437 |
* |
| 438 |
* @param[in] fname Path to open. |
| 439 |
* @param[in] fmode Mode specifier much like `fopen(3)`. |
| 440 |
* @exception rb_eTypeError `fname` is not a String. |
| 441 |
* @exception rb_eEncCompatError `fname` is not ASCII-compatible. |
| 442 |
* @exception rb_eArgError `fmode` contradicted (e.g. `"bt"`). |
| 443 |
* @exception rb_eSystemCallError `open(2)` failed for some reason. |
| 444 |
* @return An instance of ::rb_cIO. |
| 445 |
*/ |
| 446 |
VALUE rb_file_open_str(VALUE fname, const char *fmode); |
| 447 |
|
| 448 |
/** |
| 449 |
* Much like rb_io_gets(), but it reads from the mysterious ARGF object. ARGF |
| 450 |
* in this context can be seen as a virtual IO which concatenates contents of |
| 451 |
* the files passed to the process via the ARGV, or just STDIN if there are no |
| 452 |
* such files. |
| 453 |
* |
| 454 |
* Unlike rb_io_gets() this function sets `$_`. |
| 455 |
* |
| 456 |
* @exception rb_eFrozenError ARGF resorts to STDIN but it is frozen. |
| 457 |
* @retval RUBY_Qnil ARGF is at EOF. |
| 458 |
* @retval otherwise An instance of ::rb_cString. |
| 459 |
* @post ARGF is read. |
| 460 |
* @post `$_` is set. |
| 461 |
* |
| 462 |
* @internal |
| 463 |
* |
| 464 |
* In reality, this function can call `ARGF.gets`. Its redefinition can affect |
| 465 |
* the behaviour. |
| 466 |
* |
| 467 |
* Also, you can tamper ARGV on-the-fly in middle of ARGF usages: |
| 468 |
* |
| 469 |
* ``` |
| 470 |
* gets # Reads the first file. |
| 471 |
* ARGV << '/proc/self/limits' # Adds a file. |
| 472 |
* gets # Can read from /proc/self/limits. |
| 473 |
* ``` |
| 474 |
*/ |
| 475 |
VALUE rb_gets(void); |
| 476 |
|
| 477 |
RBIMPL_ATTR_NONNULL(()) |
| 478 |
/** |
| 479 |
* Writes the given error message to somewhere applicable. On Windows it goes |
| 480 |
* to the console. On POSIX environments it goes to the standard error. |
| 481 |
* |
| 482 |
* @warning IT IS A BAD IDEA to use this function form your C extensions. |
| 483 |
* It is often annoying when GUI applications write to consoles; |
| 484 |
* users don't want to look at there. Programmers also want to |
| 485 |
* control the cause of the message itself, like by rescuing an |
| 486 |
* exception. Just let ruby handle errors. That must be better than |
| 487 |
* going your own way. |
| 488 |
* |
| 489 |
* @param[in] str Error message to display. |
| 490 |
* @post `str` is written to somewhere. |
| 491 |
* |
| 492 |
* @internal |
| 493 |
* |
| 494 |
* AFAIK this function is listed here without marked deprecated because there |
| 495 |
* are usages of this function in the wild. |
| 496 |
*/ |
| 497 |
void rb_write_error(const char *str); |
| 498 |
|
| 499 |
/** |
| 500 |
* Identical to rb_write_error(), except it additionally takes the message's |
| 501 |
* length. Necessary when you want to handle wide characters. |
| 502 |
* |
| 503 |
* @param[in] str Error message to display. |
| 504 |
* @param[in] len Length of `str`, in bytes. |
| 505 |
* @post `str` is written to somewhere. |
| 506 |
*/ |
| 507 |
void rb_write_error2(const char *str, long len); |
| 508 |
|
| 509 |
/** |
| 510 |
* Closes everything. In case of POSIX environments, a child process inherits |
| 511 |
* its parent's opened file descriptors. Which is nowadays considered as one |
| 512 |
* of the UNIX mistakes. This function closes such inherited file descriptors. |
| 513 |
* When your C extension needs to have a child process, don't forget to call |
| 514 |
* this from your child process right before exec. |
| 515 |
* |
| 516 |
* @param[in] lowfd Lower bound of FDs (you want STDIN to remain, no?). |
| 517 |
* @param[in] maxhint Hint of max FDs. |
| 518 |
* @param[in] noclose_fds A hash, whose keys are an allowlist. |
| 519 |
* |
| 520 |
* @internal |
| 521 |
* |
| 522 |
* As of writing, in spite of the name, this function does not actually close |
| 523 |
* anything. It just sets `FD_CLOEXEC` for everything and let `execve(2)` to |
| 524 |
* atomically close them at once. This is because as far as we know there are |
| 525 |
* no such platform that has `fork(2)` but lacks `FD_CLOEXEC`. |
| 526 |
* |
| 527 |
* Because this function is expected to run on a forked process it is entirely |
| 528 |
* async-signal-safe. |
| 529 |
*/ |
| 530 |
void rb_close_before_exec(int lowfd, int maxhint, VALUE noclose_fds); |
| 531 |
|
| 532 |
RBIMPL_ATTR_NONNULL(()) |
| 533 |
/** |
| 534 |
* This is an rb_cloexec_pipe() + rb_update_max_fd() combo. |
| 535 |
* |
| 536 |
* @param[out] pipes Return buffer. Must at least hold 2 elements. |
| 537 |
* @retval 0 Successful creation of a pipe. |
| 538 |
* @retval -1 Failure in underlying system call(s). |
| 539 |
* @post `pipes` is filled with file descriptors. |
| 540 |
* @post `errno` is set on failure. |
| 541 |
*/ |
| 542 |
int rb_pipe(int *pipes); |
| 543 |
|
| 544 |
/** |
| 545 |
* Queries if the given FD is reserved or not. Occasionally Ruby interpreter |
| 546 |
* opens files for its own purposes. Use this function to prevent touching |
| 547 |
* such behind-the-scene descriptors. |
| 548 |
* |
| 549 |
* @param[in] fd Target file descriptor. |
| 550 |
* @retval 1 `fd` is reserved. |
| 551 |
* @retval 0 Otherwise. |
| 552 |
*/ |
| 553 |
int rb_reserved_fd_p(int fd); |
| 554 |
|
| 555 |
/** @alias{rb_reserved_fd_p} */ |
| 556 |
#define RB_RESERVED_FD_P(fd) rb_reserved_fd_p(fd) |
| 557 |
|
| 558 |
/** |
| 559 |
* Opens a file that closes on exec. In case of POSIX environments, a child |
| 560 |
* process inherits its parent's opened file descriptors. Which is nowadays |
| 561 |
* considered as one of the UNIX mistakes. This function opens a file |
| 562 |
* descriptor as `open(2)` does, but additionally instructs the operating |
| 563 |
* system that we don't want it be seen from child processes. |
| 564 |
* |
| 565 |
* @param[in] pathname File path to open. |
| 566 |
* @param[in] flags Open mode, as in `open(2)`. |
| 567 |
* @param[in] mode File mode, in case of `O_CREAT`. |
| 568 |
* @retval -1 `open(2)` failed for some reason. |
| 569 |
* @retval otherwise An allocated new file descriptor. |
| 570 |
* @note This function does not raise. |
| 571 |
* |
| 572 |
* @internal |
| 573 |
* |
| 574 |
* Whether this function can take NULL or not depends on the underlying open(2) |
| 575 |
* system call implementation but @shyouhei doesn't think it's worth trying. |
| 576 |
*/ |
| 577 |
int rb_cloexec_open(const char *pathname, int flags, mode_t mode); |
| 578 |
|
| 579 |
/** |
| 580 |
* Identical to rb_cloexec_fcntl_dupfd(), except it implies minfd is 3. |
| 581 |
* |
| 582 |
* @param[in] oldfd File descriptor to duplicate. |
| 583 |
* @retval -1 `dup2(2)` failed for some reason. |
| 584 |
* @retval otherwise An allocated new file descriptor. |
| 585 |
* @note This function does not raise. |
| 586 |
*/ |
| 587 |
int rb_cloexec_dup(int oldfd); |
| 588 |
|
| 589 |
/** |
| 590 |
* Identical to rb_cloexec_dup(), except you can specify the destination file |
| 591 |
* descriptor. If the destination is already squatted by another file |
| 592 |
* descriptor that gets silently closed without any warnings. (This is a spec |
| 593 |
* requested by POSIX.) |
| 594 |
* |
| 595 |
* @param[in] oldfd File descriptor to duplicate. |
| 596 |
* @param[in] newfd Return value destination. |
| 597 |
* @retval -1 `dup2(2)` failed for some reason. |
| 598 |
* @retval newfd An allocated new file descriptor. |
| 599 |
* @post Whatever sat at `newfd` gets closed with no notifications. |
| 600 |
* @post In case return value is -1 `newfd` is untouched. |
| 601 |
* @note This function does not raise. |
| 602 |
*/ |
| 603 |
int rb_cloexec_dup2(int oldfd, int newfd); |
| 604 |
|
| 605 |
RBIMPL_ATTR_NONNULL(()) |
| 606 |
/** |
| 607 |
* Opens a pipe with closing on exec. In case of POSIX environments, a child |
| 608 |
* process inherits its parent's opened file descriptors. Which is nowadays |
| 609 |
* considered as one of the UNIX mistakes. This function opens a pipe as |
| 610 |
* `pipe(2)` does, but additionally instructs the operating system that we |
| 611 |
* don't want the duplicated FDs be seen from child processes. |
| 612 |
* |
| 613 |
* @param[out] fildes Return buffer. Must at least hold 2 elements. |
| 614 |
* @retval 0 Successful creation of a pipe. |
| 615 |
* @retval -1 Failure in underlying system call(s). |
| 616 |
* @post `pipes` is filled with file descriptors. |
| 617 |
* @post `errno` is set on failure. |
| 618 |
*/ |
| 619 |
int rb_cloexec_pipe(int fildes[2]); |
| 620 |
|
| 621 |
/** |
| 622 |
* Duplicates a file descriptor with closing on exec. In case of POSIX |
| 623 |
* environments, a child process inherits its parent's opened file descriptors. |
| 624 |
* Which is nowadays considered as one of the UNIX mistakes. This function |
| 625 |
* duplicates a file descriptor as `dup(2)` does, but additionally instructs |
| 626 |
* the operating system that we don't want the duplicated FD be seen from child |
| 627 |
* processes. |
| 628 |
* |
| 629 |
* @param[in] fd File descriptor to duplicate. |
| 630 |
* @param[in] minfd Minimum allowed FD to return. |
| 631 |
* @retval -1 `dup(2)` failed for some reason. |
| 632 |
* @retval otherwise An allocated new file descriptor. |
| 633 |
* @note This function does not raise. |
| 634 |
* |
| 635 |
* `minfd` is handy when for instance STDERR is closed but you don't want to |
| 636 |
* use fd 2. |
| 637 |
*/ |
| 638 |
int rb_cloexec_fcntl_dupfd(int fd, int minfd); |
| 639 |
|
| 640 |
/** |
| 641 |
* Informs the interpreter that the passed fd can be the max. This information |
| 642 |
* is used from rb_close_before_exec(). |
| 643 |
* |
| 644 |
* @param[in] fd An open FD, which can be large. |
| 645 |
*/ |
| 646 |
void rb_update_max_fd(int fd); |
| 647 |
|
| 648 |
/** |
| 649 |
* Sets or clears the close-on-exec flag of the passed file descriptor to the |
| 650 |
* desired state. STDIN, STDOUT, STDERR are the exceptional file descriptors |
| 651 |
* that shall remain open. All others are to be closed on exec. When a C |
| 652 |
* extension library opens a file descriptor using anything other than |
| 653 |
* rb_cloexec_open() etc., that file descriptor shall experience this function. |
| 654 |
* |
| 655 |
* @param[in] fd An open file descriptor. |
| 656 |
*/ |
| 657 |
void rb_fd_fix_cloexec(int fd); |
| 658 |
|
| 659 |
RBIMPL_SYMBOL_EXPORT_END() |
| 660 |
|
| 661 |
#endif /* RBIMPL_INTERN_IO_H */ |