Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – tendra.SVN – Blame – /branches/tendra4/src/tools/tld/ostream.c – Rev 2

Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
/**** ostream.c --- Output stream handling.
32
 *
33
 ** Author: Steve Folkes <smf@hermes.mod.uk>
34
 *
35
 **** Commentary:
36
 *
37
 * This file implements the output stream facility specified in the file
38
 * "ostream.h".  See that file for more details.
39
 *
40
 **** Change Log:
41
 * $Log: ostream.c,v $
42
 * Revision 1.1.1.1  1998/01/17  15:57:18  release
43
 * First version to be checked into rolling release.
44
 *
45
 * Revision 1.2  1994/12/12  11:45:47  smf
46
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
47
 * OSSG C Coding Standards.
48
 *
49
 * Revision 1.1.1.1  1994/07/25  16:06:11  smf
50
 * Initial import of os-interface shared files.
51
 *
52
**/
53
 
54
/****************************************************************************/
55
 
56
#include "ostream.h"
57
#include "cstring.h"
58
#include "syntax.h"
59
 
60
/*--------------------------------------------------------------------------*/
61
 
62
ExceptionP XX_ostream_write_error = EXCEPTION ("error writing to stream");
63
 
64
static OStreamT			ostream_output_1 = {
65
    NIL (FILE *),
66
    "<stdout>",
67
    1
68
};
69
static OStreamT			ostream_error_1 = {
70
    NIL (FILE *),
71
    "<stderr>",
72
    1
73
};
74
 
75
OStreamT		 *const ostream_output = &ostream_output_1;
76
OStreamT		 *const ostream_error  = &ostream_error_1;
77
 
78
/*--------------------------------------------------------------------------*/
79
 
80
#define OSTREAM_WRITE_ERROR_CHECK(ostream) \
81
if (ferror ((ostream)->file)) { \
82
    CStringP X___name = cstring_duplicate (ostream_name (ostream)); \
83
    THROW_VALUE (XX_ostream_write_error, X___name); \
84
}
85
 
86
/*--------------------------------------------------------------------------*/
87
 
88
void
89
ostream_setup PROTO_Z ()
90
{
91
    ostream_output_1.file = stdout;
92
    ostream_error_1.file  = stderr;
93
}
94
 
95
#ifdef FS_FAST
96
#undef ostream_init
97
#endif /* defined (FS_FAST) */
98
void
99
ostream_init PROTO_N ((ostream))
100
	     PROTO_T (OStreamP ostream)
101
{
102
    ostream->name = NIL (CStringP);
103
}
104
#ifdef FS_FAST
105
#define ostream_init(os) ((os)->name = NIL (CStringP))
106
#endif /* defined (FS_FAST) */
107
 
108
BoolT
109
ostream_open PROTO_N ((ostream, name))
110
	     PROTO_T (OStreamP ostream X
111
		      CStringP name)
112
{
113
    if ((ostream->file = fopen (name, "w")) == NIL (FILE *)) {
114
	return (FALSE);
115
    }
116
    ostream->name = name;
117
    ostream->line = 1;
118
    (void) setvbuf (ostream->file, NIL (CStringP), _IOFBF, (SizeT) BUFSIZ);
119
    return (TRUE);
120
}
121
 
122
#ifdef FS_FAST
123
#undef ostream_is_open
124
#endif /* defined (FS_FAST) */
125
BoolT
126
ostream_is_open PROTO_N ((ostream))
127
		PROTO_T (OStreamP ostream)
128
{
129
    return (ostream->name != NIL (CStringP));
130
}
131
#ifdef FS_FAST
132
#define ostream_is_open(os) ((os)->name != NIL (CStringP))
133
#endif /* defined (FS_FAST) */
134
 
135
void
136
ostream_buffer PROTO_N ((ostream))
137
	       PROTO_T (OStreamP ostream)
138
{
139
    (void) setvbuf (ostream->file, NIL (CStringP), _IOFBF, (SizeT) BUFSIZ);
140
}
141
 
142
void
143
ostream_unbuffer PROTO_N ((ostream))
144
		 PROTO_T (OStreamP ostream)
145
{
146
    (void) setvbuf (ostream->file, NIL (CStringP), _IONBF, (SizeT) 0);
147
}
148
 
149
void
150
ostream_close PROTO_N ((ostream))
151
	      PROTO_T (OStreamP ostream)
152
{
153
    if (fclose (ostream->file)) {
154
	CStringP name = cstring_duplicate (ostream_name (ostream));
155
 
156
	THROW_VALUE (XX_ostream_write_error, name);
157
    }
158
    ostream_init (ostream);
159
}
160
 
161
void
162
ostream_flush PROTO_N ((ostream))
163
	      PROTO_T (OStreamP ostream)
164
{
165
    if (fflush (ostream->file)) {
166
	CStringP name = cstring_duplicate (ostream_name (ostream));
167
 
168
	THROW_VALUE (XX_ostream_write_error, name);
169
    }
170
}
171
 
172
#ifdef FS_FAST
173
#undef ostream_name
174
#endif /* defined (FS_FAST) */
175
CStringP
176
ostream_name PROTO_N ((ostream))
177
	     PROTO_T (OStreamP ostream)
178
{
179
    return (ostream->name);
180
}
181
#ifdef FS_FAST
182
#define ostream_name(os) ((os)->name)
183
#endif /* defined (FS_FAST) */
184
 
185
#ifdef FS_FAST
186
#undef ostream_line
187
#endif /* defined (FS_FAST) */
188
unsigned
189
ostream_line PROTO_N ((ostream))
190
	     PROTO_T (OStreamP ostream)
191
{
192
    return (ostream->line);
193
}
194
#ifdef FS_FAST
195
#define ostream_line(os) ((os)->line)
196
#endif /* defined (FS_FAST) */
197
 
198
void
199
write_newline PROTO_N ((ostream))
200
	      PROTO_T (OStreamP ostream)
201
{
202
    ostream->line ++;
203
    (void) putc ('\n', ostream->file);
204
    OSTREAM_WRITE_ERROR_CHECK (ostream);
205
}
206
 
207
void
208
write_tab PROTO_N ((ostream))
209
	  PROTO_T (OStreamP ostream)
210
{
211
    (void) putc ('\t', ostream->file);
212
    OSTREAM_WRITE_ERROR_CHECK (ostream);
213
}
214
 
215
void
216
write_byte PROTO_N ((ostream, c))
217
	   PROTO_T (OStreamP ostream X
218
		    ByteT    c)
219
{
220
    if (c == '\n') {
221
	ostream->line ++;
222
    }
223
    (void) putc ((int) c, ostream->file);
224
    OSTREAM_WRITE_ERROR_CHECK (ostream);
225
}
226
 
227
void
228
write_char PROTO_N ((ostream, c))
229
	   PROTO_T (OStreamP ostream X
230
		    char     c)
231
{
232
    if (c == '\n') {
233
	ostream->line ++;
234
    }
235
    (void) putc ((int) c, ostream->file);
236
    OSTREAM_WRITE_ERROR_CHECK (ostream);
237
}
238
 
239
void
240
write_escaped_char PROTO_N ((ostream, c))
241
		   PROTO_T (OStreamP ostream X
242
			    char     c)
243
{
244
    switch (c) {
245
      case '\0':
246
	(void) fputs ("\\0", ostream->file);
247
	break;
248
      case '\f':
249
	(void) fputs ("\\f", ostream->file);
250
	break;
251
      case '\n':
252
	ostream->line ++;
253
	(void) fputc ('\n', ostream->file);
254
	break;
255
      case '\r':
256
	(void) fputs ("\\r", ostream->file);
257
	break;
258
      case '\t':
259
	(void) fputc ('\t', ostream->file);
260
	break;
261
      case '\\':
262
	(void) fputs ("\\\\", ostream->file);
263
	break;
264
      default:
265
	if (syntax_is_printable (c)) {
266
	    (void) fputc ((int) c, ostream->file);
267
	} else {
268
	    (void) fprintf (ostream->file, "\\x%02x",
269
			    (unsigned) (unsigned char) c);
270
	}
271
    }
272
    OSTREAM_WRITE_ERROR_CHECK (ostream);
273
}
274
 
275
void
276
write_int PROTO_N ((ostream, i))
277
	  PROTO_T (OStreamP ostream X
278
		   int      i)
279
{
280
    (void) fprintf (ostream->file, "%d", i);
281
    OSTREAM_WRITE_ERROR_CHECK (ostream);
282
}
283
 
284
void
285
write_unsigned PROTO_N ((ostream, i))
286
	       PROTO_T (OStreamP ostream X
287
			unsigned i)
288
{
289
    (void) fprintf (ostream->file, "%u", i);
290
    OSTREAM_WRITE_ERROR_CHECK (ostream);
291
}
292
 
293
void
294
write_cstring PROTO_N ((ostream, cstring))
295
	      PROTO_T (OStreamP ostream X
296
		       CStringP cstring)
297
{
298
    CStringP tmp = cstring;
299
 
300
    while (*tmp) {
301
	if (*tmp ++ == '\n') {
302
	    ostream->line ++;
303
	}
304
    }
305
    (void) fputs (cstring, ostream->file);
306
    OSTREAM_WRITE_ERROR_CHECK (ostream);
307
}
308
 
309
void
310
write_bytes PROTO_N ((ostream, bytes, length))
311
	    PROTO_T (OStreamP ostream X
312
		     ByteP    bytes X
313
		     unsigned length)
314
{
315
    unsigned tmp_length = length;
316
    ByteP    tmp_bytes  = bytes;
317
 
318
    while (tmp_length --) {
319
	if (*tmp_bytes ++ == '\n') {
320
	    ostream->line ++;
321
	}
322
    }
323
    (void) fwrite ((GenericP) bytes, sizeof (ByteT), (SizeT) length,
324
		   ostream->file);
325
    OSTREAM_WRITE_ERROR_CHECK (ostream);
326
}
327
 
328
void
329
write_chars PROTO_N ((ostream, chars, length))
330
	    PROTO_T (OStreamP ostream X
331
		     CStringP chars X
332
		     unsigned length)
333
{
334
    while (length --) {
335
	write_char (ostream, *chars ++);
336
    }
337
}
338
 
339
void
340
write_escaped_chars PROTO_N ((ostream, chars, length))
341
		    PROTO_T (OStreamP ostream X
342
			     CStringP chars X
343
			     unsigned length)
344
{
345
    while (length --) {
346
	write_escaped_char (ostream, *chars ++);
347
    }
348
}
349
 
350
void
351
write_system_error PROTO_N ((ostream))
352
		   PROTO_T (OStreamP ostream)
353
{
354
#if (defined (FS_STRERROR) || defined (FS_SYS_ERRLIST))
355
# ifdef FS_STRERROR
356
    CStringP message = strerror (errno);
357
# else
358
    CStringP message;
359
 
360
    if ((errno >= 0) && (errno < sys_nerr)) {
361
	message = sys_errlist [errno];
362
    } else {
363
	message = "unknown error";
364
    }
365
# endif /* defined (FS_STRERROR) */
366
    write_cstring (ostream, message);
367
#else
368
    write_cstring (ostream, "error ");
369
    write_int (ostream, errno);
370
#endif /* (defined (FS_STRERROR) || defined (FS_SYS_ERRLIST)) */
371
}
372
 
373
void
374
write_pointer PROTO_N ((ostream, pointer))
375
	      PROTO_T (OStreamP ostream X
376
		       GenericP pointer)
377
{
378
    (void) fprintf (ostream->file, "%p", pointer);
379
    OSTREAM_WRITE_ERROR_CHECK (ostream);
380
}