OSDN Git Service

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