1/* This is part of libio/iostream, providing -*- C++ -*- input/output.
2Copyright (C) 1993 Free Software Foundation
3
4This file is part of the GNU IO Library.  This library is free
5software; you can redistribute it and/or modify it under the
6terms of the GNU General Public License as published by the
7Free Software Foundation; either version 2, or (at your option)
8any later version.
9
10This library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this library; see the file COPYING.  If not, write to the Free
17Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19As a special exception, if you link this library with files
20compiled with a GNU compiler to produce an executable, this does not cause
21the resulting executable to be covered by the GNU General Public License.
22This exception does not however invalidate any other reasons why
23the executable file might be covered by the GNU General Public License. */
24
25/* Written by Per Bothner (bothner@cygnus.com). */
26
27#include "libioP.h"
28#include "streambuf.h"
29#include <stdio.h>
30
31// The ANSI draft requires that operations on cin/cout/cerr can be
32// mixed with operations on stdin/stdout/stderr on a character by
33// character basis.  This normally requires that the streambuf's
34// used by cin/cout/cerr be stdiostreams.  However, if the stdio
35// implementation is the one that is built using this library,
36// then we don't need to, since in that case stdin/stdout/stderr
37// are identical to _IO_stdin/_IO_stdout/_IO_stderr.
38
39#include <libio.h>
40
41#ifdef _STDIO_USES_IOSTREAM
42#define CIN_SBUF _IO_stdin_
43#define COUT_SBUF _IO_stdout_
44#define CERR_SBUF _IO_stderr_
45#else
46#define CIN_SBUF _IO_stdin_buf
47#define COUT_SBUF _IO_stdout_buf
48#define CERR_SBUF _IO_stderr_buf
49static int use_stdiobuf = 1;
50#endif
51
52#define cin CIN
53#define cout COUT
54#define cerr CERR
55#define clog CLOG
56#include "iostream.h"
57#undef cin
58#undef cout
59#undef cerr
60#undef clog
61
62#ifdef __GNUG__
63#define PAD 0 /* g++ allows 0-length arrays. */
64#else
65#define PAD 1
66#endif
67struct _fake_istream {
68    struct myfields {
69#ifdef __GNUC__
70	_ios_fields *vb; /* pointer to virtual base class ios */
71	_IO_ssize_t _gcount;
72#else
73	/* This is supposedly correct for cfront. */
74	_IO_ssize_t _gcount;
75	void *vptr;
76	_ios_fields *vb; /* pointer to virtual base class ios */
77#endif
78    } mine;
79    _ios_fields base;
80    char filler[sizeof(struct istream)-sizeof(struct _ios_fields)+PAD];
81};
82struct _fake_ostream {
83    struct myfields {
84#ifndef __GNUC__
85	void *vptr;
86#endif
87	_ios_fields *vb; /* pointer to virtual base class ios */
88    } mine;
89    _ios_fields base;
90    char filler[sizeof(struct ostream)-sizeof(struct _ios_fields)+PAD];
91};
92
93
94#ifdef _IO_NEW_STREAMS
95#define STD_STR(SBUF, TIE, EXTRA_FLAGS) \
96 (streambuf*)&SBUF, TIE, 0, ios::skipws|ios::dec|EXTRA_FLAGS, ' ',0,0,6
97#else
98#define STD_STR(SBUF, TIE, EXTRA_FLAGS) \
99 (streambuf*)&SBUF, TIE, 0, ios::dont_close|ios::dec|ios::skipws|EXTRA_FLAGS, ' ',0,0,6
100#endif
101
102#ifdef __GNUC__
103#define OSTREAM_DEF(NAME, SBUF, TIE, EXTRA_FLAGS, ASM) \
104  _fake_ostream NAME ASM = { {&NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
105#define ISTREAM_DEF(NAME, SBUF, TIE, EXTRA_FLAGS) \
106  _fake_istream NAME = { {&NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
107#else
108#define OSTREAM_DEF(NAME, SBUF, TIE, EXTRA_FLAGS) \
109  _fake_ostream NAME = { {0, &NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
110#define ISTREAM_DEF(NAME, SBUF, TIE, EXTRA_FLAGS) \
111  _fake_istream NAME = {{0, 0, &NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS)}};
112#endif
113
114OSTREAM_DEF(cout, COUT_SBUF, NULL, 0, )
115OSTREAM_DEF(cerr, CERR_SBUF,(ostream*)&cout, ios::unitbuf, )
116ISTREAM_DEF(cin, CIN_SBUF,  (ostream*)&cout, 0)
117
118/* Only for (partial) compatibility with AT&T's library. */
119#if _G_CLOG_CONFLICT
120OSTREAM_DEF(clog, CERR_SBUF, (ostream*)&cout, 0, __asm__ ("__IO_clog"))
121#else
122OSTREAM_DEF(clog, CERR_SBUF, (ostream*)&cout, 0, )
123#endif
124
125// Switches between using _IO_std{in,out,err} and __std{in,out,err}_buf
126// for standard streams.  This does not normally need to be called
127// explicitly, but is provided for AT&T compatibility.
128
129int ios::sync_with_stdio(int new_state)
130{
131#ifdef _STDIO_USES_IOSTREAM
132    // It is always synced.
133    return 0;
134#else
135    if (new_state == use_stdiobuf) // The usual case now.
136	return use_stdiobuf;
137    if (new_state) {
138	cin.base._strbuf = (streambuf*)&_IO_stdin_buf;
139	cout.base._strbuf = (streambuf*)&_IO_stdout_buf;
140	cerr.base._strbuf = (streambuf*)&_IO_stderr_buf;
141	clog.base._strbuf = (streambuf*)&_IO_stderr_buf;
142    } else {
143	cin.base._strbuf = (streambuf*)_IO_stdin;
144	cout.base._strbuf = (streambuf*)_IO_stdout;
145	cerr.base._strbuf = (streambuf*)_IO_stderr;
146	clog.base._strbuf = (streambuf*)_IO_stderr;
147    }
148    int old_state = use_stdiobuf;
149    use_stdiobuf = new_state;
150    return old_state;
151#endif
152}
153