Mercurial > hg > xemacs-beta
diff src/print.c @ 442:abe6d1db359e r21-2-36
Import from CVS: tag r21-2-36
author | cvs |
---|---|
date | Mon, 13 Aug 2007 11:35:02 +0200 |
parents | 8de8e3f6228a |
children | 576fb035e263 |
line wrap: on
line diff
--- a/src/print.c Mon Aug 13 11:33:40 2007 +0200 +++ b/src/print.c Mon Aug 13 11:35:02 2007 +0200 @@ -1,6 +1,6 @@ /* Lisp object printing and output streams. Copyright (C) 1985, 1986, 1988, 1992-1995 Free Software Foundation, Inc. - Copyright (C) 1995, 1996 Ben Wing. + Copyright (C) 1995, 1996, 2000 Ben Wing. This file is part of XEmacs. @@ -38,8 +38,10 @@ #include "insdel.h" #include "lstream.h" #include "sysfile.h" +#ifdef WIN32_NATIVE +#include "console-msw.h" +#endif -#include <limits.h> #include <float.h> /* Define if not in float.h */ #ifndef DBL_DIG @@ -50,7 +52,11 @@ /* The subroutine object for external-debugging-output is kept here for the convenience of the debugger. */ -Lisp_Object Qexternal_debugging_output; +Lisp_Object Qexternal_debugging_output, Qalternate_debugging_output; + +#ifdef HAVE_MS_WINDOWS +Lisp_Object Qmswindows_debugging_output; +#endif /* Avoid actual stack overflow in print. */ static int print_depth; @@ -103,40 +109,45 @@ int stdout_needs_newline; -/* Write a string (in internal format) to stdio stream STREAM. */ - -void -write_string_to_stdio_stream (FILE *stream, struct console *con, - CONST Bufbyte *str, - Bytecount offset, Bytecount len, - Lisp_Object coding_system) +static void +std_handle_out_external (FILE *stream, Lisp_Object lstream, + const Extbyte *extptr, Extcount extlen, + /* is this really stdout/stderr? + (controls termscript writing) */ + int output_is_std_handle, + int must_flush) { - Extcount extlen; - CONST Extbyte *extptr; - - TO_EXTERNAL_FORMAT (DATA, (str + offset, len), - ALLOCA, (extptr, extlen), - coding_system); if (stream) { - fwrite (extptr, 1, extlen, stream); -#ifdef WINDOWSNT - /* Q122442 says that pipes are "treated as files, not as - devices", and that this is a feature. Before I found that - article, I thought it was a bug. Thanks MS, I feel much - better now. - kkm */ - if (stream == stdout || stream == stderr) - fflush (stream); +#ifdef WIN32_NATIVE + HANDLE errhand = GetStdHandle (STD_INPUT_HANDLE); + int no_useful_stderr = errhand == 0 || errhand == INVALID_HANDLE_VALUE; + + if (!no_useful_stderr) + no_useful_stderr = !PeekNamedPipe (errhand, 0, 0, 0, 0, 0); + /* we typically have no useful stdout/stderr under windows if we're + being invoked graphically. */ + if (no_useful_stderr) + mswindows_output_console_string (extptr, extlen); + else #endif + { + fwrite (extptr, 1, extlen, stream); +#ifdef WIN32_NATIVE + /* Q122442 says that pipes are "treated as files, not as + devices", and that this is a feature. Before I found that + article, I thought it was a bug. Thanks MS, I feel much + better now. - kkm */ + must_flush = 1; +#endif + if (must_flush) + fflush (stream); + } } else - { - assert (CONSOLE_TTY_P (con)); - Lstream_write (XLSTREAM (CONSOLE_TTY_DATA (con)->outstream), - extptr, extlen); - } - if (stream == stdout || stream == stderr || - (!stream && CONSOLE_TTY_DATA (con)->is_stdio)) + Lstream_write (XLSTREAM (lstream), extptr, extlen); + + if (output_is_std_handle) { if (termscript) { @@ -147,12 +158,132 @@ } } +/* #### The following function should be replaced a call to the + emacs_doprnt_*() functions. This is the only way to ensure that + I18N3 works properly (many implementations of the *printf() + functions, including the ones included in glibc, do not implement + the %###$ argument-positioning syntax). + + Note, however, that to do this, we'd have to + + 1) pre-allocate all the lstreams and do whatever else was necessary + to make sure that no allocation occurs, since these functions may be + called from fatal_error_signal(). + + 2) (to be really correct) make a new lstream that outputs using + mswindows_output_console_string(). */ + +static int +std_handle_out_va (FILE *stream, const char *fmt, va_list args) +{ + Bufbyte kludge[8192]; + Extbyte *extptr; + Extcount extlen; + int retval; + + retval = vsprintf ((char *) kludge, fmt, args); + if (initialized && !fatal_error_in_progress) + TO_EXTERNAL_FORMAT (DATA, (kludge, strlen ((char *) kludge)), + ALLOCA, (extptr, extlen), + Qnative); + else + { + extptr = (Extbyte *) kludge; + extlen = (Extcount) strlen ((char *) kludge); + } + + std_handle_out_external (stream, Qnil, extptr, extlen, 1, 1); + return retval; +} + +/* Output portably to stderr or its equivalent; call GETTEXT on the + format string. Automatically flush when done. */ + +int +stderr_out (const char *fmt, ...) +{ + int retval; + va_list args; + va_start (args, fmt); + retval = + std_handle_out_va + (stderr, initialized && !fatal_error_in_progress ? GETTEXT (fmt) : fmt, + args); + va_end (args); + return retval; +} + +/* Output portably to stdout or its equivalent; call GETTEXT on the + format string. Automatically flush when done. */ + +int +stdout_out (const char *fmt, ...) +{ + int retval; + va_list args; + va_start (args, fmt); + retval = + std_handle_out_va + (stdout, initialized && !fatal_error_in_progress ? GETTEXT (fmt) : fmt, + args); + va_end (args); + return retval; +} + +DOESNT_RETURN +fatal (const char *fmt, ...) +{ + va_list args; + va_start (args, fmt); + + stderr_out ("\nXEmacs: "); + std_handle_out_va (stderr, GETTEXT (fmt), args); + stderr_out ("\n"); + + va_end (args); + exit (1); +} + +/* Write a string (in internal format) to stdio stream STREAM. */ + +void +write_string_to_stdio_stream (FILE *stream, struct console *con, + const Bufbyte *str, + Bytecount offset, Bytecount len, + Lisp_Object coding_system, + int must_flush) +{ + Extcount extlen; + const Extbyte *extptr; + + /* #### yuck! sometimes this function is called with string data, + and the following call may gc. */ + { + Bufbyte *puta = (Bufbyte *) alloca (len); + memcpy (puta, str + offset, len); + TO_EXTERNAL_FORMAT (DATA, (puta, len), + ALLOCA, (extptr, extlen), + coding_system); + } + + if (stream) + std_handle_out_external (stream, Qnil, extptr, extlen, + stream == stdout || stream == stderr, must_flush); + else + { + assert (CONSOLE_TTY_P (con)); + std_handle_out_external (0, CONSOLE_TTY_DATA (con)->outstream, + extptr, extlen, + CONSOLE_TTY_DATA (con)->is_stdio, must_flush); + } +} + /* Write a string to the output location specified in FUNCTION. Arguments NONRELOC, RELOC, OFFSET, and LEN are as in buffer_insert_string_1() in insdel.c. */ static void -output_string (Lisp_Object function, CONST Bufbyte *nonreloc, +output_string (Lisp_Object function, const Bufbyte *nonreloc, Lisp_Object reloc, Bytecount offset, Bytecount len) { /* This function can GC */ @@ -162,7 +293,7 @@ other functions that take both a nonreloc and a reloc, or things may get confused and an assertion failure in fixup_internal_substring() may get triggered. */ - CONST Bufbyte *newnonreloc = nonreloc; + const Bufbyte *newnonreloc = nonreloc; struct gcpro gcpro1, gcpro2; /* Emacs won't print while GCing, but an external debugger might */ @@ -238,7 +369,7 @@ else if (EQ (function, Qt) || EQ (function, Qnil)) { write_string_to_stdio_stream (stdout, 0, newnonreloc, offset, len, - Qterminal); + Qterminal, print_unbuffered); } else { @@ -347,7 +478,7 @@ /* Used for printing a single-byte character (*not* any Emchar). */ #define write_char_internal(string_of_length_1, stream) \ - output_string (stream, (CONST Bufbyte *) (string_of_length_1), \ + output_string (stream, (const Bufbyte *) (string_of_length_1), \ Qnil, 0, 1) /* NOTE: Do not call this with the data of a Lisp_String, as @@ -360,7 +491,7 @@ canonicalize_printcharfun() (i.e. Qnil means stdout, not Vstandard_output, etc.) */ void -write_string_1 (CONST Bufbyte *str, Bytecount size, Lisp_Object stream) +write_string_1 (const Bufbyte *str, Bytecount size, Lisp_Object stream) { /* This function can GC */ #ifdef ERROR_CHECK_BUFPOS @@ -370,10 +501,10 @@ } void -write_c_string (CONST char *str, Lisp_Object stream) +write_c_string (const char *str, Lisp_Object stream) { /* This function can GC */ - write_string_1 ((CONST Bufbyte *) str, strlen (str), stream); + write_string_1 ((const Bufbyte *) str, strlen (str), stream); } @@ -804,10 +935,8 @@ } #endif /* LISP_FLOAT_TYPE */ -/* Print NUMBER to BUFFER. The digits are first written in reverse - order (the least significant digit first), and are then reversed. - This is equivalent to sprintf(buffer, "%ld", number), only much - faster. +/* Print NUMBER to BUFFER. This is equivalent to sprintf(buffer, + "%ld", number), only much faster. BUFFER should accept 24 bytes. This should suffice for the longest numbers on 64-bit machines, including the `-' sign and the trailing @@ -859,7 +988,7 @@ } static void -print_vector_internal (CONST char *start, CONST char *end, +print_vector_internal (const char *start, const char *end, Lisp_Object obj, Lisp_Object printcharfun, int escapeflag) { @@ -1277,7 +1406,7 @@ the flag print-gensym is non-nil, prefix it with #n= to read the object back with the #n# reader syntax later if needed. */ if (!NILP (Vprint_gensym) - /* #### Test whether this produces a noticable slow-down for + /* #### Test whether this produces a noticeable slow-down for printing when print-gensym is non-nil. */ && !EQ (obj, oblookup (Vobarray, string_data (symbol_name (XSYMBOL (obj))), @@ -1385,13 +1514,9 @@ UNGCPRO; } -/* #ifdef DEBUG_XEMACS */ -/* I don't like seeing `Note: Strange doc (not fboundp) for function - alternate-debugging-output @ 429542' -slb */ -/* #### Eek! Any clue how to get rid of it? In fact, how about - getting rid of this function altogether? Does anything actually - *use* it? --hniksic */ +/* Useful on systems or in places where writing to stdout is unavailable or + not working. */ static int alternate_do_pointer; static char alternate_do_string[5000]; @@ -1407,7 +1532,7 @@ Bufbyte str[MAX_EMCHAR_LEN]; Bytecount len; int extlen; - CONST Extbyte *extptr; + const Extbyte *extptr; CHECK_CHAR_COERCE_INT (character); len = set_charptr_emchar (str, XCHAR (character)); @@ -1419,7 +1544,6 @@ alternate_do_string[alternate_do_pointer] = 0; return character; } -/* #endif / * DEBUG_XEMACS */ DEFUN ("external-debugging-output", Fexternal_debugging_output, 1, 3, 0, /* Write CHAR-OR-STRING to stderr or stdout. @@ -1427,6 +1551,10 @@ to stderr. You can use this function to write directly to the terminal. This function can be used as the STREAM argument of Fprint() or the like. +Under MS Windows, this writes output to the console window (which is +created, if necessary), unless XEmacs is being run noninteractively +\(i.e. using the `-batch' argument). + If you have opened a termscript file (using `open-termscript'), then the output also will be logged to this file. */ @@ -1461,7 +1589,7 @@ write_string_to_stdio_stream (file, con, XSTRING_DATA (char_or_string), 0, XSTRING_LENGTH (char_or_string), - Qterminal); + Qterminal, 1); else { Bufbyte str[MAX_EMCHAR_LEN]; @@ -1469,7 +1597,7 @@ CHECK_CHAR_COERCE_INT (char_or_string); len = set_charptr_emchar (str, XCHAR (char_or_string)); - write_string_to_stdio_stream (file, con, str, 0, len, Qterminal); + write_string_to_stdio_stream (file, con, str, 0, len, Qterminal, 1); } return char_or_string; @@ -1527,6 +1655,12 @@ Vprint_level = make_int (debug_print_level); print_internal (debug_print_obj, Qexternal_debugging_output, 1); + alternate_do_pointer = 0; + print_internal (debug_print_obj, Qalternate_debugging_output, 1); +#ifdef WIN32_NATIVE + /* Write out to the debugger, as well */ + print_internal (debug_print_obj, Qmswindows_debugging_output, 1); +#endif Vinhibit_quit = save_Vinhibit_quit; Vprint_level = save_Vprint_level; @@ -1542,7 +1676,6 @@ { debug_print_no_newline (debug_print_obj); stderr_out ("\n"); - fflush (stderr); } /* Debugging kludge -- unbuffered */ @@ -1575,7 +1708,6 @@ Fbacktrace (Qexternal_debugging_output, Qt); stderr_out ("\n"); - fflush (stderr); Vinhibit_quit = old_inhibit_quit; Vprint_level = old_print_level; @@ -1593,13 +1725,11 @@ int first = 1; struct backtrace *bt = backtrace_list; stderr_out (" ["); - fflush (stderr); while (length > 0 && bt) { if (!first) { stderr_out (", "); - fflush (stderr); } if (COMPILED_FUNCTIONP (*bt->function)) { @@ -1612,15 +1742,12 @@ if (!NILP (ann)) { stderr_out ("<compiled-function from "); - fflush (stderr); debug_print_no_newline (ann); stderr_out (">"); - fflush (stderr); } else { stderr_out ("<compiled-function of unknown origin>"); - fflush (stderr); } } else @@ -1630,7 +1757,6 @@ bt = bt->next; } stderr_out ("]\n"); - fflush (stderr); } #endif /* debugging kludge */ @@ -1660,6 +1786,10 @@ DEFSUBR (Fexternal_debugging_output); DEFSUBR (Fopen_termscript); defsymbol (&Qexternal_debugging_output, "external-debugging-output"); + defsymbol (&Qalternate_debugging_output, "alternate-debugging-output"); +#ifdef HAVE_MS_WINDOWS + defsymbol (&Qmswindows_debugging_output, "mswindows-debugging-output"); +#endif DEFSUBR (Fwith_output_to_temp_buffer); }