OSDN Git Service

* Makefile.in ($(docdir)/gccint.info, gccint.dvi): Add additional
[pf3gnuchains/gcc-fork.git] / libchill / chillstdio.c
1 /* Implement runtime actions for CHILL.
2    Copyright (C) 1992,1993 Free Software Foundation, Inc.
3    Author: Wilfried Moser
4
5 This file is part of GNU CC.
6
7 GNU CC 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 2, or (at your option)
10 any later version.
11
12 GNU CC 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 GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* As a special exception, if you link this library with other files,
23    some of which are compiled with GCC, to produce an executable,
24    this library does not by itself cause the resulting executable
25    to be covered by the GNU General Public License.
26    This exception does not however invalidate any other reasons why
27    the executable file might be covered by the GNU General Public License.  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <string.h>
33
34 #include "iomodes.h"
35
36 /* predefined associations, accesses, and text for stdin, stdout, stderr */
37 /* stdin */
38 #define STDIO_TEXT_LENGTH 1024
39 #define STDIN_TEXT_LENGTH STDIO_TEXT_LENGTH
40
41 static Access_Mode stdin_access;
42
43 #ifndef STDIN_FILENO
44 #define STDIN_FILENO 0
45 #endif
46
47 static
48 Association_Mode stdin_association =
49 {
50   IO_EXISTING | IO_READABLE | IO_SEQUENCIBLE | IO_ISASSOCIATED,
51   NULL,
52   &stdin_access,
53   STDIN_FILENO,
54   NULL,
55   0,
56   ReadOnly
57 };
58
59 static Access_Mode stdin_access = 
60 {
61   IO_TEXTIO,
62   STDIN_TEXT_LENGTH + 2,
63   0,
64   0,
65   &stdin_association,
66   0,
67   NULL,
68   VaryingChars
69 };
70
71 static
72 VARYING_STRING(STDIN_TEXT_LENGTH) stdin_text_record;
73
74 Text_Mode chill_stdin =
75 {
76   IO_TEXTLOCATION,
77   (VarString *)&stdin_text_record,
78   &stdin_access,
79   0
80 };
81
82 /* stdout */
83 #define STDOUT_TEXT_LENGTH STDIO_TEXT_LENGTH
84 #ifndef STDOUT_FILENO
85 #define STDOUT_FILENO 1
86 #endif
87
88 static Access_Mode stdout_access;
89
90 static
91 Association_Mode stdout_association =
92 {
93   IO_EXISTING | IO_WRITEABLE | IO_SEQUENCIBLE | IO_ISASSOCIATED,
94   NULL,
95   &stdout_access,
96   STDOUT_FILENO,
97   NULL,
98   0,
99   WriteOnly
100 };
101
102 static Access_Mode stdout_access = 
103 {
104   IO_TEXTIO,
105   STDOUT_TEXT_LENGTH + 2,
106   0,
107   0,
108   &stdout_association,
109   0,
110   NULL,
111   VaryingChars
112 };
113
114 static
115 VARYING_STRING(STDOUT_TEXT_LENGTH) stdout_text_record;
116
117 Text_Mode chill_stdout =
118 {
119   IO_TEXTLOCATION,
120   (VarString *)&stdout_text_record,
121   &stdout_access,
122   0
123 };
124
125 /* stderr */
126 #define STDERR_TEXT_LENGTH STDIO_TEXT_LENGTH
127 #ifndef STDERR_FILENO
128 #define STDERR_FILENO 2
129 #endif
130
131 static Access_Mode stderr_access;
132
133 static
134 Association_Mode stderr_association =
135 {
136   IO_EXISTING | IO_WRITEABLE | IO_SEQUENCIBLE | IO_ISASSOCIATED,
137   NULL,
138   &stderr_access,
139   STDERR_FILENO,
140   NULL,
141   0,
142   WriteOnly
143 };
144
145 static Access_Mode stderr_access = 
146 {
147   IO_TEXTIO,
148   STDERR_TEXT_LENGTH + 2,
149   0,
150   0,
151   &stderr_association,
152   0,
153   NULL,
154   VaryingChars
155 };
156
157 static
158 VARYING_STRING(STDIN_TEXT_LENGTH) stderr_text_record;
159
160 Text_Mode chill_stderr =
161 {
162   IO_TEXTLOCATION,
163   (VarString *)&stderr_text_record,
164   &stderr_access,
165   0
166 };
167
168 /*
169  * function __xmalloc_
170  *
171  * parameter:
172  *   size               number of bytes to allocate
173  *
174  * returns:
175  *  void*
176  *
177  * abstract:
178  *  This is the general allocation routine for libchill
179  *
180  */
181
182 void *
183 __xmalloc_ (size)
184 int size;
185 {
186   void  *tmp = malloc (size);
187   
188   if (!tmp)
189     {
190       fprintf (stderr, "ChillLib: Out of heap space.\n");
191       fflush (stderr);
192       exit (ENOMEM);
193     }
194   return (tmp);
195 } /* __xmalloc_ */
196
197 static char *
198 newstring (char *str)
199 {
200   char *tmp = __xmalloc_ (strlen (str) + 1);
201   strcpy (tmp, str);
202   return tmp;
203 }
204
205 static void setup_stdinout (void) __attribute__((constructor));
206
207 static void
208 setup_stdinout ()
209 {
210   /* allocate the names */
211   stdin_association.pathname = newstring ("stdin");
212   stdout_association.pathname = newstring ("stdout");
213   stderr_association.pathname = newstring ("stderr");
214
215   /* stdin needs a readbuffer */
216   stdin_association.bufptr = __xmalloc_ (sizeof (readbuf_t));
217   memset (stdin_association.bufptr, 0, sizeof (readbuf_t));
218 }