Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

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