| 1 | /* UI_FILE - a generic STDIO like output stream. |
| 2 | |
| 3 | Copyright (C) 1999-2015 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | /* Implement the ``struct ui_file'' object. */ |
| 21 | |
| 22 | #include "defs.h" |
| 23 | #include "ui-file.h" |
| 24 | #include "gdb_obstack.h" |
| 25 | #include "gdb_select.h" |
| 26 | #include "filestuff.h" |
| 27 | |
| 28 | static ui_file_isatty_ftype null_file_isatty; |
| 29 | static ui_file_write_ftype null_file_write; |
| 30 | static ui_file_write_ftype null_file_write_async_safe; |
| 31 | static ui_file_fputs_ftype null_file_fputs; |
| 32 | static ui_file_read_ftype null_file_read; |
| 33 | static ui_file_flush_ftype null_file_flush; |
| 34 | static ui_file_delete_ftype null_file_delete; |
| 35 | static ui_file_rewind_ftype null_file_rewind; |
| 36 | static ui_file_put_ftype null_file_put; |
| 37 | static ui_file_fseek_ftype null_file_fseek; |
| 38 | |
| 39 | struct ui_file |
| 40 | { |
| 41 | int *magic; |
| 42 | ui_file_flush_ftype *to_flush; |
| 43 | ui_file_write_ftype *to_write; |
| 44 | ui_file_write_async_safe_ftype *to_write_async_safe; |
| 45 | ui_file_fputs_ftype *to_fputs; |
| 46 | ui_file_read_ftype *to_read; |
| 47 | ui_file_delete_ftype *to_delete; |
| 48 | ui_file_isatty_ftype *to_isatty; |
| 49 | ui_file_rewind_ftype *to_rewind; |
| 50 | ui_file_put_ftype *to_put; |
| 51 | ui_file_fseek_ftype *to_fseek; |
| 52 | void *to_data; |
| 53 | }; |
| 54 | int ui_file_magic; |
| 55 | |
| 56 | struct ui_file * |
| 57 | ui_file_new (void) |
| 58 | { |
| 59 | struct ui_file *file = XNEW (struct ui_file); |
| 60 | |
| 61 | file->magic = &ui_file_magic; |
| 62 | set_ui_file_data (file, NULL, null_file_delete); |
| 63 | set_ui_file_flush (file, null_file_flush); |
| 64 | set_ui_file_write (file, null_file_write); |
| 65 | set_ui_file_write_async_safe (file, null_file_write_async_safe); |
| 66 | set_ui_file_fputs (file, null_file_fputs); |
| 67 | set_ui_file_read (file, null_file_read); |
| 68 | set_ui_file_isatty (file, null_file_isatty); |
| 69 | set_ui_file_rewind (file, null_file_rewind); |
| 70 | set_ui_file_put (file, null_file_put); |
| 71 | set_ui_file_fseek (file, null_file_fseek); |
| 72 | return file; |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | ui_file_delete (struct ui_file *file) |
| 77 | { |
| 78 | file->to_delete (file); |
| 79 | xfree (file); |
| 80 | } |
| 81 | |
| 82 | static int |
| 83 | null_file_isatty (struct ui_file *file) |
| 84 | { |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static void |
| 89 | null_file_rewind (struct ui_file *file) |
| 90 | { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | static void |
| 95 | null_file_put (struct ui_file *file, |
| 96 | ui_file_put_method_ftype *write, |
| 97 | void *dest) |
| 98 | { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | static void |
| 103 | null_file_flush (struct ui_file *file) |
| 104 | { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | static void |
| 109 | null_file_write (struct ui_file *file, |
| 110 | const char *buf, |
| 111 | long sizeof_buf) |
| 112 | { |
| 113 | if (file->to_fputs == null_file_fputs) |
| 114 | /* Both the write and fputs methods are null. Discard the |
| 115 | request. */ |
| 116 | return; |
| 117 | else |
| 118 | { |
| 119 | /* The fputs method isn't null, slowly pass the write request |
| 120 | onto that. FYI, this isn't as bad as it may look - the |
| 121 | current (as of 1999-11-07) printf_* function calls fputc and |
| 122 | fputc does exactly the below. By having a write function it |
| 123 | is possible to clean up that code. */ |
| 124 | int i; |
| 125 | char b[2]; |
| 126 | |
| 127 | b[1] = '\0'; |
| 128 | for (i = 0; i < sizeof_buf; i++) |
| 129 | { |
| 130 | b[0] = buf[i]; |
| 131 | file->to_fputs (b, file); |
| 132 | } |
| 133 | return; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static long |
| 138 | null_file_read (struct ui_file *file, |
| 139 | char *buf, |
| 140 | long sizeof_buf) |
| 141 | { |
| 142 | errno = EBADF; |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static void |
| 147 | null_file_fputs (const char *buf, struct ui_file *file) |
| 148 | { |
| 149 | if (file->to_write == null_file_write) |
| 150 | /* Both the write and fputs methods are null. Discard the |
| 151 | request. */ |
| 152 | return; |
| 153 | else |
| 154 | { |
| 155 | /* The write method was implemented, use that. */ |
| 156 | file->to_write (file, buf, strlen (buf)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static void |
| 161 | null_file_write_async_safe (struct ui_file *file, |
| 162 | const char *buf, |
| 163 | long sizeof_buf) |
| 164 | { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | static void |
| 169 | null_file_delete (struct ui_file *file) |
| 170 | { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | static int |
| 175 | null_file_fseek (struct ui_file *stream, long offset, int whence) |
| 176 | { |
| 177 | errno = EBADF; |
| 178 | |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | void * |
| 183 | ui_file_data (struct ui_file *file) |
| 184 | { |
| 185 | if (file->magic != &ui_file_magic) |
| 186 | internal_error (__FILE__, __LINE__, |
| 187 | _("ui_file_data: bad magic number")); |
| 188 | return file->to_data; |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | gdb_flush (struct ui_file *file) |
| 193 | { |
| 194 | file->to_flush (file); |
| 195 | } |
| 196 | |
| 197 | int |
| 198 | ui_file_isatty (struct ui_file *file) |
| 199 | { |
| 200 | return file->to_isatty (file); |
| 201 | } |
| 202 | |
| 203 | void |
| 204 | ui_file_rewind (struct ui_file *file) |
| 205 | { |
| 206 | file->to_rewind (file); |
| 207 | } |
| 208 | |
| 209 | void |
| 210 | ui_file_put (struct ui_file *file, |
| 211 | ui_file_put_method_ftype *write, |
| 212 | void *dest) |
| 213 | { |
| 214 | file->to_put (file, write, dest); |
| 215 | } |
| 216 | |
| 217 | void |
| 218 | ui_file_write (struct ui_file *file, |
| 219 | const char *buf, |
| 220 | long length_buf) |
| 221 | { |
| 222 | file->to_write (file, buf, length_buf); |
| 223 | } |
| 224 | |
| 225 | void |
| 226 | ui_file_write_for_put (void *data, const char *buffer, long length_buffer) |
| 227 | { |
| 228 | ui_file_write ((struct ui_file *) data, buffer, length_buffer); |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | ui_file_write_async_safe (struct ui_file *file, |
| 233 | const char *buf, |
| 234 | long length_buf) |
| 235 | { |
| 236 | file->to_write_async_safe (file, buf, length_buf); |
| 237 | } |
| 238 | |
| 239 | long |
| 240 | ui_file_read (struct ui_file *file, char *buf, long length_buf) |
| 241 | { |
| 242 | return file->to_read (file, buf, length_buf); |
| 243 | } |
| 244 | |
| 245 | int |
| 246 | ui_file_fseek (struct ui_file *file, long offset, int whence) |
| 247 | { |
| 248 | return file->to_fseek (file, offset, whence); |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | fputs_unfiltered (const char *buf, struct ui_file *file) |
| 253 | { |
| 254 | file->to_fputs (buf, file); |
| 255 | } |
| 256 | |
| 257 | void |
| 258 | set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush_ptr) |
| 259 | { |
| 260 | file->to_flush = flush_ptr; |
| 261 | } |
| 262 | |
| 263 | void |
| 264 | set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty_ptr) |
| 265 | { |
| 266 | file->to_isatty = isatty_ptr; |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind_ptr) |
| 271 | { |
| 272 | file->to_rewind = rewind_ptr; |
| 273 | } |
| 274 | |
| 275 | void |
| 276 | set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put_ptr) |
| 277 | { |
| 278 | file->to_put = put_ptr; |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | set_ui_file_write (struct ui_file *file, |
| 283 | ui_file_write_ftype *write_ptr) |
| 284 | { |
| 285 | file->to_write = write_ptr; |
| 286 | } |
| 287 | |
| 288 | void |
| 289 | set_ui_file_write_async_safe (struct ui_file *file, |
| 290 | ui_file_write_async_safe_ftype *write_async_safe_ptr) |
| 291 | { |
| 292 | file->to_write_async_safe = write_async_safe_ptr; |
| 293 | } |
| 294 | |
| 295 | void |
| 296 | set_ui_file_read (struct ui_file *file, ui_file_read_ftype *read_ptr) |
| 297 | { |
| 298 | file->to_read = read_ptr; |
| 299 | } |
| 300 | |
| 301 | void |
| 302 | set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs_ptr) |
| 303 | { |
| 304 | file->to_fputs = fputs_ptr; |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | set_ui_file_fseek (struct ui_file *file, ui_file_fseek_ftype *fseek_ptr) |
| 309 | { |
| 310 | file->to_fseek = fseek_ptr; |
| 311 | } |
| 312 | |
| 313 | void |
| 314 | set_ui_file_data (struct ui_file *file, void *data, |
| 315 | ui_file_delete_ftype *delete_ptr) |
| 316 | { |
| 317 | file->to_data = data; |
| 318 | file->to_delete = delete_ptr; |
| 319 | } |
| 320 | |
| 321 | /* ui_file utility function for converting a ``struct ui_file'' into |
| 322 | a memory buffer. */ |
| 323 | |
| 324 | struct accumulated_ui_file |
| 325 | { |
| 326 | char *buffer; |
| 327 | long length; |
| 328 | }; |
| 329 | |
| 330 | static void |
| 331 | do_ui_file_xstrdup (void *context, const char *buffer, long length) |
| 332 | { |
| 333 | struct accumulated_ui_file *acc = (struct accumulated_ui_file *) context; |
| 334 | |
| 335 | if (acc->buffer == NULL) |
| 336 | acc->buffer = (char *) xmalloc (length + 1); |
| 337 | else |
| 338 | acc->buffer = (char *) xrealloc (acc->buffer, acc->length + length + 1); |
| 339 | memcpy (acc->buffer + acc->length, buffer, length); |
| 340 | acc->length += length; |
| 341 | acc->buffer[acc->length] = '\0'; |
| 342 | } |
| 343 | |
| 344 | char * |
| 345 | ui_file_xstrdup (struct ui_file *file, long *length) |
| 346 | { |
| 347 | struct accumulated_ui_file acc; |
| 348 | |
| 349 | acc.buffer = NULL; |
| 350 | acc.length = 0; |
| 351 | ui_file_put (file, do_ui_file_xstrdup, &acc); |
| 352 | if (acc.buffer == NULL) |
| 353 | acc.buffer = xstrdup (""); |
| 354 | if (length != NULL) |
| 355 | *length = acc.length; |
| 356 | return acc.buffer; |
| 357 | } |
| 358 | |
| 359 | static void |
| 360 | do_ui_file_obsavestring (void *context, const char *buffer, long length) |
| 361 | { |
| 362 | struct obstack *obstack = (struct obstack *) context; |
| 363 | |
| 364 | obstack_grow (obstack, buffer, length); |
| 365 | } |
| 366 | |
| 367 | char * |
| 368 | ui_file_obsavestring (struct ui_file *file, struct obstack *obstack, |
| 369 | long *length) |
| 370 | { |
| 371 | ui_file_put (file, do_ui_file_obsavestring, obstack); |
| 372 | *length = obstack_object_size (obstack); |
| 373 | obstack_1grow (obstack, '\0'); |
| 374 | return (char *) obstack_finish (obstack); |
| 375 | } |
| 376 | \f |
| 377 | /* A pure memory based ``struct ui_file'' that can be used an output |
| 378 | buffer. The buffers accumulated contents are available via |
| 379 | ui_file_put(). */ |
| 380 | |
| 381 | struct mem_file |
| 382 | { |
| 383 | int *magic; |
| 384 | char *buffer; |
| 385 | int sizeof_buffer; |
| 386 | int length_buffer; |
| 387 | }; |
| 388 | |
| 389 | static ui_file_rewind_ftype mem_file_rewind; |
| 390 | static ui_file_put_ftype mem_file_put; |
| 391 | static ui_file_write_ftype mem_file_write; |
| 392 | static ui_file_delete_ftype mem_file_delete; |
| 393 | static struct ui_file *mem_file_new (void); |
| 394 | static int mem_file_magic; |
| 395 | |
| 396 | static struct ui_file * |
| 397 | mem_file_new (void) |
| 398 | { |
| 399 | struct mem_file *stream = XNEW (struct mem_file); |
| 400 | struct ui_file *file = ui_file_new (); |
| 401 | |
| 402 | set_ui_file_data (file, stream, mem_file_delete); |
| 403 | set_ui_file_rewind (file, mem_file_rewind); |
| 404 | set_ui_file_put (file, mem_file_put); |
| 405 | set_ui_file_write (file, mem_file_write); |
| 406 | stream->magic = &mem_file_magic; |
| 407 | stream->buffer = NULL; |
| 408 | stream->sizeof_buffer = 0; |
| 409 | stream->length_buffer = 0; |
| 410 | return file; |
| 411 | } |
| 412 | |
| 413 | static void |
| 414 | mem_file_delete (struct ui_file *file) |
| 415 | { |
| 416 | struct mem_file *stream = (struct mem_file *) ui_file_data (file); |
| 417 | |
| 418 | if (stream->magic != &mem_file_magic) |
| 419 | internal_error (__FILE__, __LINE__, |
| 420 | _("mem_file_delete: bad magic number")); |
| 421 | if (stream->buffer != NULL) |
| 422 | xfree (stream->buffer); |
| 423 | xfree (stream); |
| 424 | } |
| 425 | |
| 426 | struct ui_file * |
| 427 | mem_fileopen (void) |
| 428 | { |
| 429 | return mem_file_new (); |
| 430 | } |
| 431 | |
| 432 | static void |
| 433 | mem_file_rewind (struct ui_file *file) |
| 434 | { |
| 435 | struct mem_file *stream = (struct mem_file *) ui_file_data (file); |
| 436 | |
| 437 | if (stream->magic != &mem_file_magic) |
| 438 | internal_error (__FILE__, __LINE__, |
| 439 | _("mem_file_rewind: bad magic number")); |
| 440 | stream->length_buffer = 0; |
| 441 | } |
| 442 | |
| 443 | static void |
| 444 | mem_file_put (struct ui_file *file, |
| 445 | ui_file_put_method_ftype *write, |
| 446 | void *dest) |
| 447 | { |
| 448 | struct mem_file *stream = (struct mem_file *) ui_file_data (file); |
| 449 | |
| 450 | if (stream->magic != &mem_file_magic) |
| 451 | internal_error (__FILE__, __LINE__, |
| 452 | _("mem_file_put: bad magic number")); |
| 453 | if (stream->length_buffer > 0) |
| 454 | write (dest, stream->buffer, stream->length_buffer); |
| 455 | } |
| 456 | |
| 457 | void |
| 458 | mem_file_write (struct ui_file *file, |
| 459 | const char *buffer, |
| 460 | long length_buffer) |
| 461 | { |
| 462 | struct mem_file *stream = (struct mem_file *) ui_file_data (file); |
| 463 | |
| 464 | if (stream->magic != &mem_file_magic) |
| 465 | internal_error (__FILE__, __LINE__, |
| 466 | _("mem_file_write: bad magic number")); |
| 467 | if (stream->buffer == NULL) |
| 468 | { |
| 469 | stream->length_buffer = length_buffer; |
| 470 | stream->sizeof_buffer = length_buffer; |
| 471 | stream->buffer = (char *) xmalloc (stream->sizeof_buffer); |
| 472 | memcpy (stream->buffer, buffer, length_buffer); |
| 473 | } |
| 474 | else |
| 475 | { |
| 476 | int new_length = stream->length_buffer + length_buffer; |
| 477 | |
| 478 | if (new_length >= stream->sizeof_buffer) |
| 479 | { |
| 480 | stream->sizeof_buffer = new_length; |
| 481 | stream->buffer |
| 482 | = (char *) xrealloc (stream->buffer, stream->sizeof_buffer); |
| 483 | } |
| 484 | memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer); |
| 485 | stream->length_buffer = new_length; |
| 486 | } |
| 487 | } |
| 488 | \f |
| 489 | /* ``struct ui_file'' implementation that maps directly onto |
| 490 | <stdio.h>'s FILE. */ |
| 491 | |
| 492 | static ui_file_write_ftype stdio_file_write; |
| 493 | static ui_file_write_async_safe_ftype stdio_file_write_async_safe; |
| 494 | static ui_file_fputs_ftype stdio_file_fputs; |
| 495 | static ui_file_read_ftype stdio_file_read; |
| 496 | static ui_file_isatty_ftype stdio_file_isatty; |
| 497 | static ui_file_delete_ftype stdio_file_delete; |
| 498 | static struct ui_file *stdio_file_new (FILE *file, int close_p); |
| 499 | static ui_file_flush_ftype stdio_file_flush; |
| 500 | static ui_file_fseek_ftype stdio_file_fseek; |
| 501 | |
| 502 | static int stdio_file_magic; |
| 503 | |
| 504 | struct stdio_file |
| 505 | { |
| 506 | int *magic; |
| 507 | FILE *file; |
| 508 | /* The associated file descriptor is extracted ahead of time for |
| 509 | stdio_file_write_async_safe's benefit, in case fileno isn't async-safe. */ |
| 510 | int fd; |
| 511 | int close_p; |
| 512 | }; |
| 513 | |
| 514 | static struct ui_file * |
| 515 | stdio_file_new (FILE *file, int close_p) |
| 516 | { |
| 517 | struct ui_file *ui_file = ui_file_new (); |
| 518 | struct stdio_file *stdio = XNEW (struct stdio_file); |
| 519 | |
| 520 | stdio->magic = &stdio_file_magic; |
| 521 | stdio->file = file; |
| 522 | stdio->fd = fileno (file); |
| 523 | stdio->close_p = close_p; |
| 524 | set_ui_file_data (ui_file, stdio, stdio_file_delete); |
| 525 | set_ui_file_flush (ui_file, stdio_file_flush); |
| 526 | set_ui_file_write (ui_file, stdio_file_write); |
| 527 | set_ui_file_write_async_safe (ui_file, stdio_file_write_async_safe); |
| 528 | set_ui_file_fputs (ui_file, stdio_file_fputs); |
| 529 | set_ui_file_read (ui_file, stdio_file_read); |
| 530 | set_ui_file_isatty (ui_file, stdio_file_isatty); |
| 531 | set_ui_file_fseek (ui_file, stdio_file_fseek); |
| 532 | return ui_file; |
| 533 | } |
| 534 | |
| 535 | static void |
| 536 | stdio_file_delete (struct ui_file *file) |
| 537 | { |
| 538 | struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file); |
| 539 | |
| 540 | if (stdio->magic != &stdio_file_magic) |
| 541 | internal_error (__FILE__, __LINE__, |
| 542 | _("stdio_file_delete: bad magic number")); |
| 543 | if (stdio->close_p) |
| 544 | { |
| 545 | fclose (stdio->file); |
| 546 | } |
| 547 | xfree (stdio); |
| 548 | } |
| 549 | |
| 550 | static void |
| 551 | stdio_file_flush (struct ui_file *file) |
| 552 | { |
| 553 | struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file); |
| 554 | |
| 555 | if (stdio->magic != &stdio_file_magic) |
| 556 | internal_error (__FILE__, __LINE__, |
| 557 | _("stdio_file_flush: bad magic number")); |
| 558 | fflush (stdio->file); |
| 559 | } |
| 560 | |
| 561 | static long |
| 562 | stdio_file_read (struct ui_file *file, char *buf, long length_buf) |
| 563 | { |
| 564 | struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file); |
| 565 | |
| 566 | if (stdio->magic != &stdio_file_magic) |
| 567 | internal_error (__FILE__, __LINE__, |
| 568 | _("stdio_file_read: bad magic number")); |
| 569 | |
| 570 | /* For the benefit of Windows, call gdb_select before reading from |
| 571 | the file. Wait until at least one byte of data is available. |
| 572 | Control-C can interrupt gdb_select, but not read. */ |
| 573 | { |
| 574 | fd_set readfds; |
| 575 | FD_ZERO (&readfds); |
| 576 | FD_SET (stdio->fd, &readfds); |
| 577 | if (gdb_select (stdio->fd + 1, &readfds, NULL, NULL, NULL) == -1) |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | return read (stdio->fd, buf, length_buf); |
| 582 | } |
| 583 | |
| 584 | static void |
| 585 | stdio_file_write (struct ui_file *file, const char *buf, long length_buf) |
| 586 | { |
| 587 | struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file); |
| 588 | |
| 589 | if (stdio->magic != &stdio_file_magic) |
| 590 | internal_error (__FILE__, __LINE__, |
| 591 | _("stdio_file_write: bad magic number")); |
| 592 | /* Calling error crashes when we are called from the exception framework. */ |
| 593 | if (fwrite (buf, length_buf, 1, stdio->file)) |
| 594 | { |
| 595 | /* Nothing. */ |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | static void |
| 600 | stdio_file_write_async_safe (struct ui_file *file, |
| 601 | const char *buf, long length_buf) |
| 602 | { |
| 603 | struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file); |
| 604 | |
| 605 | if (stdio->magic != &stdio_file_magic) |
| 606 | { |
| 607 | /* gettext isn't necessarily async safe, so we can't use _("error message") here. |
| 608 | We could extract the correct translation ahead of time, but this is an extremely |
| 609 | rare event, and one of the other stdio_file_* routines will presumably catch |
| 610 | the problem anyway. For now keep it simple and ignore the error here. */ |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | /* This is written the way it is to avoid a warning from gcc about not using the |
| 615 | result of write (since it can be declared with attribute warn_unused_result). |
| 616 | Alas casting to void doesn't work for this. */ |
| 617 | if (write (stdio->fd, buf, length_buf)) |
| 618 | { |
| 619 | /* Nothing. */ |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | static void |
| 624 | stdio_file_fputs (const char *linebuffer, struct ui_file *file) |
| 625 | { |
| 626 | struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file); |
| 627 | |
| 628 | if (stdio->magic != &stdio_file_magic) |
| 629 | internal_error (__FILE__, __LINE__, |
| 630 | _("stdio_file_fputs: bad magic number")); |
| 631 | /* Calling error crashes when we are called from the exception framework. */ |
| 632 | if (fputs (linebuffer, stdio->file)) |
| 633 | { |
| 634 | /* Nothing. */ |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | static int |
| 639 | stdio_file_isatty (struct ui_file *file) |
| 640 | { |
| 641 | struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file); |
| 642 | |
| 643 | if (stdio->magic != &stdio_file_magic) |
| 644 | internal_error (__FILE__, __LINE__, |
| 645 | _("stdio_file_isatty: bad magic number")); |
| 646 | return (isatty (stdio->fd)); |
| 647 | } |
| 648 | |
| 649 | static int |
| 650 | stdio_file_fseek (struct ui_file *file, long offset, int whence) |
| 651 | { |
| 652 | struct stdio_file *stdio = (struct stdio_file *) ui_file_data (file); |
| 653 | |
| 654 | if (stdio->magic != &stdio_file_magic) |
| 655 | internal_error (__FILE__, __LINE__, |
| 656 | _("stdio_file_fseek: bad magic number")); |
| 657 | |
| 658 | return fseek (stdio->file, offset, whence); |
| 659 | } |
| 660 | |
| 661 | #ifdef __MINGW32__ |
| 662 | /* This is the implementation of ui_file method to_write for stderr. |
| 663 | gdb_stdout is flushed before writing to gdb_stderr. */ |
| 664 | |
| 665 | static void |
| 666 | stderr_file_write (struct ui_file *file, const char *buf, long length_buf) |
| 667 | { |
| 668 | gdb_flush (gdb_stdout); |
| 669 | stdio_file_write (file, buf, length_buf); |
| 670 | } |
| 671 | |
| 672 | /* This is the implementation of ui_file method to_fputs for stderr. |
| 673 | gdb_stdout is flushed before writing to gdb_stderr. */ |
| 674 | |
| 675 | static void |
| 676 | stderr_file_fputs (const char *linebuffer, struct ui_file *file) |
| 677 | { |
| 678 | gdb_flush (gdb_stdout); |
| 679 | stdio_file_fputs (linebuffer, file); |
| 680 | } |
| 681 | #endif |
| 682 | |
| 683 | struct ui_file * |
| 684 | stderr_fileopen (void) |
| 685 | { |
| 686 | struct ui_file *ui_file = stdio_fileopen (stderr); |
| 687 | |
| 688 | #ifdef __MINGW32__ |
| 689 | /* There is no real line-buffering on Windows, see |
| 690 | http://msdn.microsoft.com/en-us/library/86cebhfs%28v=vs.71%29.aspx |
| 691 | so the stdout is either fully-buffered or non-buffered. We can't |
| 692 | make stdout non-buffered, because of two concerns, |
| 693 | 1. non-buffering hurts performance, |
| 694 | 2. non-buffering may change GDB's behavior when it is interacting |
| 695 | with front-end, such as Emacs. |
| 696 | |
| 697 | We decided to leave stdout as fully buffered, but flush it first |
| 698 | when something is written to stderr. */ |
| 699 | |
| 700 | /* Method 'to_write_async_safe' is not overwritten, because there's |
| 701 | no way to flush a stream in an async-safe manner. Fortunately, |
| 702 | it doesn't really matter, because: |
| 703 | - that method is only used for printing internal debug output |
| 704 | from signal handlers. |
| 705 | - Windows hosts don't have a concept of async-safeness. Signal |
| 706 | handlers run in a separate thread, so they can call |
| 707 | the regular non-async-safe output routines freely. */ |
| 708 | set_ui_file_write (ui_file, stderr_file_write); |
| 709 | set_ui_file_fputs (ui_file, stderr_file_fputs); |
| 710 | #endif |
| 711 | |
| 712 | return ui_file; |
| 713 | } |
| 714 | |
| 715 | /* Like fdopen(). Create a ui_file from a previously opened FILE. */ |
| 716 | |
| 717 | struct ui_file * |
| 718 | stdio_fileopen (FILE *file) |
| 719 | { |
| 720 | return stdio_file_new (file, 0); |
| 721 | } |
| 722 | |
| 723 | struct ui_file * |
| 724 | gdb_fopen (const char *name, const char *mode) |
| 725 | { |
| 726 | FILE *f = gdb_fopen_cloexec (name, mode); |
| 727 | |
| 728 | if (f == NULL) |
| 729 | return NULL; |
| 730 | return stdio_file_new (f, 1); |
| 731 | } |
| 732 | |
| 733 | /* ``struct ui_file'' implementation that maps onto two ui-file objects. */ |
| 734 | |
| 735 | static ui_file_write_ftype tee_file_write; |
| 736 | static ui_file_fputs_ftype tee_file_fputs; |
| 737 | static ui_file_isatty_ftype tee_file_isatty; |
| 738 | static ui_file_delete_ftype tee_file_delete; |
| 739 | static ui_file_flush_ftype tee_file_flush; |
| 740 | |
| 741 | static int tee_file_magic; |
| 742 | |
| 743 | struct tee_file |
| 744 | { |
| 745 | int *magic; |
| 746 | struct ui_file *one, *two; |
| 747 | int close_one, close_two; |
| 748 | }; |
| 749 | |
| 750 | struct ui_file * |
| 751 | tee_file_new (struct ui_file *one, int close_one, |
| 752 | struct ui_file *two, int close_two) |
| 753 | { |
| 754 | struct ui_file *ui_file = ui_file_new (); |
| 755 | struct tee_file *tee = XNEW (struct tee_file); |
| 756 | |
| 757 | tee->magic = &tee_file_magic; |
| 758 | tee->one = one; |
| 759 | tee->two = two; |
| 760 | tee->close_one = close_one; |
| 761 | tee->close_two = close_two; |
| 762 | set_ui_file_data (ui_file, tee, tee_file_delete); |
| 763 | set_ui_file_flush (ui_file, tee_file_flush); |
| 764 | set_ui_file_write (ui_file, tee_file_write); |
| 765 | set_ui_file_fputs (ui_file, tee_file_fputs); |
| 766 | set_ui_file_isatty (ui_file, tee_file_isatty); |
| 767 | return ui_file; |
| 768 | } |
| 769 | |
| 770 | static void |
| 771 | tee_file_delete (struct ui_file *file) |
| 772 | { |
| 773 | struct tee_file *tee = (struct tee_file *) ui_file_data (file); |
| 774 | |
| 775 | if (tee->magic != &tee_file_magic) |
| 776 | internal_error (__FILE__, __LINE__, |
| 777 | _("tee_file_delete: bad magic number")); |
| 778 | if (tee->close_one) |
| 779 | ui_file_delete (tee->one); |
| 780 | if (tee->close_two) |
| 781 | ui_file_delete (tee->two); |
| 782 | |
| 783 | xfree (tee); |
| 784 | } |
| 785 | |
| 786 | static void |
| 787 | tee_file_flush (struct ui_file *file) |
| 788 | { |
| 789 | struct tee_file *tee = (struct tee_file *) ui_file_data (file); |
| 790 | |
| 791 | if (tee->magic != &tee_file_magic) |
| 792 | internal_error (__FILE__, __LINE__, |
| 793 | _("tee_file_flush: bad magic number")); |
| 794 | tee->one->to_flush (tee->one); |
| 795 | tee->two->to_flush (tee->two); |
| 796 | } |
| 797 | |
| 798 | static void |
| 799 | tee_file_write (struct ui_file *file, const char *buf, long length_buf) |
| 800 | { |
| 801 | struct tee_file *tee = (struct tee_file *) ui_file_data (file); |
| 802 | |
| 803 | if (tee->magic != &tee_file_magic) |
| 804 | internal_error (__FILE__, __LINE__, |
| 805 | _("tee_file_write: bad magic number")); |
| 806 | ui_file_write (tee->one, buf, length_buf); |
| 807 | ui_file_write (tee->two, buf, length_buf); |
| 808 | } |
| 809 | |
| 810 | static void |
| 811 | tee_file_fputs (const char *linebuffer, struct ui_file *file) |
| 812 | { |
| 813 | struct tee_file *tee = (struct tee_file *) ui_file_data (file); |
| 814 | |
| 815 | if (tee->magic != &tee_file_magic) |
| 816 | internal_error (__FILE__, __LINE__, |
| 817 | _("tee_file_fputs: bad magic number")); |
| 818 | tee->one->to_fputs (linebuffer, tee->one); |
| 819 | tee->two->to_fputs (linebuffer, tee->two); |
| 820 | } |
| 821 | |
| 822 | static int |
| 823 | tee_file_isatty (struct ui_file *file) |
| 824 | { |
| 825 | struct tee_file *tee = (struct tee_file *) ui_file_data (file); |
| 826 | |
| 827 | if (tee->magic != &tee_file_magic) |
| 828 | internal_error (__FILE__, __LINE__, |
| 829 | _("tee_file_isatty: bad magic number")); |
| 830 | |
| 831 | return ui_file_isatty (tee->one); |
| 832 | } |