Subversion Repositories tendra.SVN

Rev

Rev 2 | Go to most recent revision | Details | Compare with Previous | 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:45  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
    "<stdout>",
68
    0,
69
    1
70
};
71
static OStreamT			ostream_error_1 = {
72
    NIL (FILE *),
73
    "<stderr>",
74
    "<stderr>",
75
    0,
76
    1
77
};
78
 
79
OStreamT		 *const ostream_output = &ostream_output_1;
80
OStreamT		 *const ostream_error  = &ostream_error_1;
81
 
82
/*--------------------------------------------------------------------------*/
83
 
84
#define OSTREAM_WRITE_ERROR_CHECK(ostream) \
85
if (ferror ((ostream)->file)) { \
86
    CStringP X___name = cstring_duplicate (ostream_name (ostream)); \
87
    THROW_VALUE (XX_ostream_write_error, X___name); \
88
}
89
 
90
/*--------------------------------------------------------------------------*/
91
 
92
void
93
ostream_setup PROTO_Z ()
94
{
95
    ostream_output_1.file = stdout;
96
    ostream_error_1.file  = stderr;
97
}
98
 
99
#ifdef FS_FAST
100
#undef ostream_init
101
#endif /* defined (FS_FAST) */
102
void
103
ostream_init PROTO_N ((ostream))
104
	     PROTO_T (OStreamP ostream)
105
{
106
    ostream->name = NIL (CStringP);
107
}
108
#ifdef FS_FAST
109
#define ostream_init(os) ((os)->name = NIL (CStringP))
110
#endif /* defined (FS_FAST) */
111
 
112
BoolT
113
ostream_open PROTO_N ((ostream, name))
114
	     PROTO_T (OStreamP ostream X
115
		      CStringP name)
116
{
117
    CStringP oname = name;
118
    CStringP pname = cstring_find_reverse (name, '@');
119
    if (pname != NIL (CStringP)) {
120
	oname = ALLOCATE_VECTOR (char, cstring_length (name) + 10);
121
	(void) sprintf (oname, "%.*s%d%s", (int) (pname - name), name,
122
			++ostream->no, pname + 1);
123
    }
124
    if ((ostream->file = fopen (oname, "w")) == NIL (FILE *)) {
125
	return (FALSE);
126
    }
127
    ostream->name = oname;
128
    ostream->gen_name = name;
129
    ostream->line = 1;
130
    (void) setvbuf (ostream->file, NIL (CStringP), _IOFBF, (SizeT) BUFSIZ);
131
    return (TRUE);
132
}
133
 
134
#ifdef FS_FAST
135
#undef ostream_is_open
136
#endif /* defined (FS_FAST) */
137
BoolT
138
ostream_is_open PROTO_N ((ostream))
139
		PROTO_T (OStreamP ostream)
140
{
141
    return (ostream->name != NIL (CStringP));
142
}
143
#ifdef FS_FAST
144
#define ostream_is_open(os) ((os)->name != NIL (CStringP))
145
#endif /* defined (FS_FAST) */
146
 
147
void
148
ostream_buffer PROTO_N ((ostream))
149
	       PROTO_T (OStreamP ostream)
150
{
151
    (void) setvbuf (ostream->file, NIL (CStringP), _IOFBF, (SizeT) BUFSIZ);
152
}
153
 
154
void
155
ostream_unbuffer PROTO_N ((ostream))
156
		 PROTO_T (OStreamP ostream)
157
{
158
    (void) setvbuf (ostream->file, NIL (CStringP), _IONBF, (SizeT) 0);
159
}
160
 
161
void
162
ostream_close PROTO_N ((ostream))
163
	      PROTO_T (OStreamP ostream)
164
{
165
    if (fclose (ostream->file)) {
166
	CStringP name = cstring_duplicate (ostream_name (ostream));
167
 
168
	THROW_VALUE (XX_ostream_write_error, name);
169
    }
170
    ostream_init (ostream);
171
}
172
 
173
void
174
ostream_flush PROTO_N ((ostream))
175
	      PROTO_T (OStreamP ostream)
176
{
177
    if (fflush (ostream->file)) {
178
	CStringP name = cstring_duplicate (ostream_name (ostream));
179
 
180
	THROW_VALUE (XX_ostream_write_error, name);
181
    }
182
}
183
 
184
#ifdef FS_FAST
185
#undef ostream_name
186
#endif /* defined (FS_FAST) */
187
CStringP
188
ostream_name PROTO_N ((ostream))
189
	     PROTO_T (OStreamP ostream)
190
{
191
    return (ostream->name);
192
}
193
#ifdef FS_FAST
194
#define ostream_name(os) ((os)->name)
195
#endif /* defined (FS_FAST) */
196
 
197
#ifdef FS_FAST
198
#undef ostream_gen_name
199
#endif /* defined (FS_FAST) */
200
CStringP
201
ostream_gen_name PROTO_N ((ostream))
202
		 PROTO_T (OStreamP ostream)
203
{
204
    return (ostream->gen_name);
205
}
206
#ifdef FS_FAST
207
#define ostream_gen_name(os) ((os)->gen_name)
208
#endif /* defined (FS_FAST) */
209
 
210
#ifdef FS_FAST
211
#undef ostream_line
212
#endif /* defined (FS_FAST) */
213
unsigned
214
ostream_line PROTO_N ((ostream))
215
	     PROTO_T (OStreamP ostream)
216
{
217
    return (ostream->line);
218
}
219
#ifdef FS_FAST
220
#define ostream_line(os) ((os)->line)
221
#endif /* defined (FS_FAST) */
222
 
223
void
224
write_newline PROTO_N ((ostream))
225
	      PROTO_T (OStreamP ostream)
226
{
227
    ostream->line ++;
228
    (void) putc ('\n', ostream->file);
229
    OSTREAM_WRITE_ERROR_CHECK (ostream);
230
}
231
 
232
void
233
write_tab PROTO_N ((ostream))
234
	  PROTO_T (OStreamP ostream)
235
{
236
    (void) putc ('\t', ostream->file);
237
    OSTREAM_WRITE_ERROR_CHECK (ostream);
238
}
239
 
240
void
241
write_byte PROTO_N ((ostream, c))
242
	   PROTO_T (OStreamP ostream X
243
		    ByteT    c)
244
{
245
    if (c == '\n') {
246
	ostream->line ++;
247
    }
248
    (void) putc ((int) c, ostream->file);
249
    OSTREAM_WRITE_ERROR_CHECK (ostream);
250
}
251
 
252
void
253
write_char PROTO_N ((ostream, c))
254
	   PROTO_T (OStreamP ostream X
255
		    char     c)
256
{
257
    if (c == '\n') {
258
	ostream->line ++;
259
    }
260
    (void) putc ((int) c, ostream->file);
261
    OSTREAM_WRITE_ERROR_CHECK (ostream);
262
}
263
 
264
void
265
write_escaped_char PROTO_N ((ostream, c))
266
		   PROTO_T (OStreamP ostream X
267
			    char     c)
268
{
269
    switch (c) {
270
      case '\0':
271
	(void) fputs ("\\0", ostream->file);
272
	break;
273
      case '\f':
274
	(void) fputs ("\\f", ostream->file);
275
	break;
276
      case '\n':
277
	ostream->line ++;
278
	(void) fputc ('\n', ostream->file);
279
	break;
280
      case '\r':
281
	(void) fputs ("\\r", ostream->file);
282
	break;
283
      case '\t':
284
	(void) fputc ('\t', ostream->file);
285
	break;
286
      case '\\':
287
	(void) fputs ("\\\\", ostream->file);
288
	break;
289
      default:
290
	if (syntax_is_printable (c)) {
291
	    (void) fputc ((int) c, ostream->file);
292
	} else {
293
	    (void) fprintf (ostream->file, "\\x%02x",
294
			    (unsigned) (unsigned char) c);
295
	}
296
    }
297
    OSTREAM_WRITE_ERROR_CHECK (ostream);
298
}
299
 
300
void
301
write_int PROTO_N ((ostream, i))
302
	  PROTO_T (OStreamP ostream X
303
		   int      i)
304
{
305
    (void) fprintf (ostream->file, "%d", i);
306
    OSTREAM_WRITE_ERROR_CHECK (ostream);
307
}
308
 
309
void
310
write_unsigned PROTO_N ((ostream, i))
311
	       PROTO_T (OStreamP ostream X
312
			unsigned i)
313
{
314
    (void) fprintf (ostream->file, "%u", i);
315
    OSTREAM_WRITE_ERROR_CHECK (ostream);
316
}
317
 
318
void
319
write_cstring PROTO_N ((ostream, cstring))
320
	      PROTO_T (OStreamP ostream X
321
		       CStringP cstring)
322
{
323
    CStringP tmp = cstring;
324
 
325
    while (*tmp) {
326
	if (*tmp ++ == '\n') {
327
	    ostream->line ++;
328
	}
329
    }
330
    (void) fputs (cstring, ostream->file);
331
    OSTREAM_WRITE_ERROR_CHECK (ostream);
332
}
333
 
334
void
335
write_bytes PROTO_N ((ostream, bytes, length))
336
	    PROTO_T (OStreamP ostream X
337
		     ByteP    bytes X
338
		     unsigned length)
339
{
340
    unsigned tmp_length = length;
341
    ByteP    tmp_bytes  = bytes;
342
 
343
    while (tmp_length --) {
344
	if (*tmp_bytes ++ == '\n') {
345
	    ostream->line ++;
346
	}
347
    }
348
    (void) fwrite ((GenericP) bytes, sizeof (ByteT), (SizeT) length,
349
		   ostream->file);
350
    OSTREAM_WRITE_ERROR_CHECK (ostream);
351
}
352
 
353
void
354
write_chars PROTO_N ((ostream, chars, length))
355
	    PROTO_T (OStreamP ostream X
356
		     CStringP chars X
357
		     unsigned length)
358
{
359
    while (length --) {
360
	write_char (ostream, *chars ++);
361
    }
362
}
363
 
364
void
365
write_escaped_chars PROTO_N ((ostream, chars, length))
366
		    PROTO_T (OStreamP ostream X
367
			     CStringP chars X
368
			     unsigned length)
369
{
370
    while (length --) {
371
	write_escaped_char (ostream, *chars ++);
372
    }
373
}
374
 
375
void
376
write_system_error PROTO_N ((ostream))
377
		   PROTO_T (OStreamP ostream)
378
{
379
#if (defined (FS_STRERROR) || defined (FS_SYS_ERRLIST))
380
# ifdef FS_STRERROR
381
    CStringP message = strerror (errno);
382
# else
383
    CStringP message;
384
 
385
    if ((errno >= 0) && (errno < sys_nerr)) {
386
	message = sys_errlist [errno];
387
    } else {
388
	message = "unknown error";
389
    }
390
# endif /* defined (FS_STRERROR) */
391
    write_cstring (ostream, message);
392
#else
393
    write_cstring (ostream, "error ");
394
    write_int (ostream, errno);
395
#endif /* (defined (FS_STRERROR) || defined (FS_SYS_ERRLIST)) */
396
}
397
 
398
void
399
write_pointer PROTO_N ((ostream, pointer))
400
	      PROTO_T (OStreamP ostream X
401
		       GenericP pointer)
402
{
403
    (void) fprintf (ostream->file, "%p", pointer);
404
    OSTREAM_WRITE_ERROR_CHECK (ostream);
405
}