Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
6 7u83 2
 * Copyright (c) 2002-2006 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
6 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:-
6 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
6 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;
6 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;
6 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
/**** rename-file.c --- Routines for parsing rename file.
62
 *
63
 ** Author: Steve Folkes <smf@hermes.mod.uk>
64
 *
65
 **** Commentary:
66
 *
67
 * This file provides the routines that parse the rename file.  The rename
68
 * file has the following format.
69
 *
70
 *	'shape'
71
 *		"name1" "name2";
72
 *		"name3" [unique.4];
73
 *
74
 * Where shape names are enclosed in single quotes, string names are enclosed
75
 * in double quotes, and uniques are written using the normal unique syntax.
76
 * A single shape name may be followed by any number of name pairs, each of
77
 * which is terminated by a semi-colon.
78
 *
79
 **** Change Log:
80
 * $Log: rename-file.c,v $
81
 * Revision 1.1.1.1  1998/01/17  15:57:16  release
82
 * First version to be checked into rolling release.
83
 *
84
 * Revision 1.3  1995/09/22  08:37:15  smf
85
 * Fixed problems with incomplete structures (to shut "tcc" up).
86
 *
87
 * Revision 1.2  1994/12/12  11:44:10  smf
88
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
89
 * OSSG C Coding Standards.
90
 *
91
 * Revision 1.1.1.1  1994/07/25  16:03:28  smf
92
 * Initial import of TDF linker 3.5 non shared files.
93
 *
94
**/
95
 
96
/****************************************************************************/
97
 
98
#include "rename-file.h"
99
#include "dstring.h"
100
#include "gen-errors.h"
101
#include "istream.h"
102
#include "nstring-list.h"
103
#include "syntax.h"
104
 
105
#include "solve-cycles.h"
106
 
107
/*--------------------------------------------------------------------------*/
108
 
109
#ifdef FS_NO_ENUM
110
typedef int RenameTagT, *RenameTagP;
111
#define RTOK_SHAPE		(0)
112
#define RTOK_NAME		(1)
113
#define RTOK_SEMI		(2)
114
#define RTOK_EOF		(3)
115
#else
116
typedef enum {
117
    RTOK_SHAPE,
118
    RTOK_NAME,
119
    RTOK_SEMI,
120
    RTOK_EOF
121
} RenameTagT, *RenameTagP;
122
#endif /* defined (FS_NO_ENUM) */
123
 
124
typedef struct RenameTokenT {
125
    RenameTagT			tag;
126
    union {
127
	NStringT		shape;
128
	NameKeyT		name;
129
    } u;
130
} RenameTokenT, *RenameTokenP;
131
 
132
/*--------------------------------------------------------------------------*/
133
 
134
static BoolT
6 7u83 135
rename_file_skip_white_space(IStreamP istream,				      char    *c_ref)
2 7u83 136
{
137
    BoolT comment = FALSE;
138
 
139
    for (;;) {
140
	char c;
141
 
142
      redo:
6 7u83 143
	switch (c = ISTREAM_READ_CHAR(istream)) {
2 7u83 144
	  case '\0':
6 7u83 145
	    ISTREAM_HANDLE_NULL(istream, redo, eof);
2 7u83 146
	    break;
147
	  case '\n':
6 7u83 148
	    istream_inc_line(istream);
2 7u83 149
	    comment = FALSE;
150
	    break;
151
	  case '#':
152
	    comment = TRUE;
153
	    break;
154
	  default:
6 7u83 155
	    if ((!comment) && (!syntax_is_white_space(c))) {
2 7u83 156
		*c_ref = c;
6 7u83 157
		return(TRUE);
2 7u83 158
	    }
159
	    break;
160
	}
161
    }
162
  eof:
6 7u83 163
    return(FALSE);
2 7u83 164
}
165
 
166
static void
6 7u83 167
rename_file_read_unique(IStreamP     istream,				 RenameTokenP token)
2 7u83 168
{
6 7u83 169
    NameKeyP          name   = & (token->u.name);
2 7u83 170
    unsigned          length = 1;
171
    DStringT          dstring;
172
    NStringT          nstring;
173
    NStringListT      list;
174
    NStringListEntryP entry;
175
    unsigned          i;
176
 
6 7u83 177
    dstring_init(&dstring);
178
    nstring_list_init(&list);
2 7u83 179
    for (;;) {
180
	char c;
181
 
182
      redo:
6 7u83 183
	switch (c = ISTREAM_READ_CHAR(istream)) {
2 7u83 184
	  case '\0':
6 7u83 185
	    ISTREAM_HANDLE_NULL(istream, redo, eof);
186
	    dstring_append_char(&dstring, '\0');
2 7u83 187
	    break;
188
	  case '\n':
6 7u83 189
	    istream_inc_line(istream);
190
	    E_rename_unexpected_newline(istream);
2 7u83 191
	    break;
192
	  case ']':
6 7u83 193
	    dstring_to_nstring(&dstring, &nstring);
194
	    nstring_list_append(&list, &nstring);
195
	    dstring_destroy(&dstring);
196
	    name_key_init_unique(name, length);
197
 	    for (i = 0, entry = nstring_list_head(&list); entry;
198
		 i++, entry = nstring_list_entry_deallocate(entry)) {
199
		NStringP component = nstring_list_entry_string(entry);
2 7u83 200
 
6 7u83 201
		name_key_set_component(name, i, component);
2 7u83 202
	    }
203
	    token->tag = RTOK_NAME;
204
	    return;
205
	  case '[':
6 7u83 206
	    E_rename_illegal_char(istream, c);
2 7u83 207
	    break;
208
	  case '.':
6 7u83 209
	    dstring_to_nstring(&dstring, &nstring);
210
	    nstring_list_append(&list, &nstring);
211
	    dstring_destroy(&dstring);
212
	    dstring_init(&dstring);
213
	    length++;
2 7u83 214
	    break;
215
	  case '\\':
6 7u83 216
	    switch (istream_read_escaped_char(istream, &c))EXHAUSTIVE {
2 7u83 217
	      case ISTREAM_STAT_READ_CHAR:
6 7u83 218
		dstring_append_char(&dstring, c);
2 7u83 219
		break;
220
	      case ISTREAM_STAT_SYNTAX_ERROR:
6 7u83 221
		E_rename_illegal_escape(istream);
2 7u83 222
		break;
223
	      case ISTREAM_STAT_NO_CHAR:
224
		/*NOTHING*/
225
		break;
226
	    }
227
	    break;
228
	  default:
6 7u83 229
	    dstring_append_char(&dstring, c);
2 7u83 230
	    break;
231
	}
232
    }
233
  eof:
6 7u83 234
    E_rename_unexpected_eof(istream);
235
    dstring_destroy(&dstring);
236
    for (entry = nstring_list_head(&list); entry;
237
	 entry = nstring_list_entry_deallocate(entry)) {
238
	nstring_destroy(nstring_list_entry_string(entry));
2 7u83 239
    }
240
    token->tag = RTOK_EOF;
241
}
242
 
243
static void
6 7u83 244
rename_file_read_shape(IStreamP     istream,				RenameTokenP token)
2 7u83 245
{
246
    DStringT dstring;
247
 
6 7u83 248
    dstring_init(&dstring);
2 7u83 249
    for (;;) {
250
	char c;
251
 
252
      redo:
6 7u83 253
	switch (c = ISTREAM_READ_CHAR(istream)) {
2 7u83 254
	  case '\0':
6 7u83 255
	    ISTREAM_HANDLE_NULL(istream, redo, eof);
256
	    dstring_append_char(&dstring, '\0');
2 7u83 257
	    break;
258
	  case '\n':
6 7u83 259
	    istream_inc_line(istream);
260
	    E_rename_unexpected_newline(istream);
2 7u83 261
	    break;
262
	  case '\'':
6 7u83 263
	    dstring_to_nstring(&dstring, & (token->u.shape));
264
	    dstring_destroy(&dstring);
2 7u83 265
	    token->tag = RTOK_SHAPE;
266
	    return;
267
	  case '\\':
6 7u83 268
	    switch (istream_read_escaped_char(istream, &c))EXHAUSTIVE {
2 7u83 269
	      case ISTREAM_STAT_READ_CHAR:
6 7u83 270
		dstring_append_char(&dstring, c);
2 7u83 271
		break;
272
	      case ISTREAM_STAT_SYNTAX_ERROR:
6 7u83 273
		E_rename_illegal_escape(istream);
2 7u83 274
		break;
275
	      case ISTREAM_STAT_NO_CHAR:
276
		/*NOTHING*/
277
		break;
278
	    }
279
	    break;
280
	  default:
6 7u83 281
	    dstring_append_char(&dstring, c);
2 7u83 282
	    break;
283
	}
284
    }
285
  eof:
6 7u83 286
    E_rename_unexpected_eof(istream);
287
    dstring_destroy(&dstring);
2 7u83 288
    token->tag = RTOK_EOF;
289
}
290
 
291
static void
6 7u83 292
rename_file_read_string(IStreamP     istream,				 RenameTokenP token)
2 7u83 293
{
294
    DStringT dstring;
295
    NStringT nstring;
296
 
6 7u83 297
    dstring_init(&dstring);
2 7u83 298
    for (;;) {
299
	char c;
300
 
301
      redo:
6 7u83 302
	switch (c = ISTREAM_READ_CHAR(istream)) {
2 7u83 303
	  case '\0':
6 7u83 304
	    ISTREAM_HANDLE_NULL(istream, redo, eof);
305
	    dstring_append_char(&dstring, '\0');
2 7u83 306
	    break;
307
	  case '\n':
6 7u83 308
	    istream_inc_line(istream);
309
	    E_rename_unexpected_newline(istream);
2 7u83 310
	    break;
311
	  case '"':
6 7u83 312
	    dstring_to_nstring(&dstring, &nstring);
313
	    dstring_destroy(&dstring);
314
	    name_key_init_string(& (token->u.name), &nstring);
2 7u83 315
	    token->tag = RTOK_NAME;
316
	    return;
317
	  case '[': case ']': case '.':
6 7u83 318
	    E_rename_illegal_char(istream, c);
2 7u83 319
	    break;
320
	  case '\\':
6 7u83 321
	    switch (istream_read_escaped_char(istream, &c))EXHAUSTIVE {
2 7u83 322
	      case ISTREAM_STAT_READ_CHAR:
6 7u83 323
		dstring_append_char(&dstring, c);
2 7u83 324
		break;
325
	      case ISTREAM_STAT_SYNTAX_ERROR:
6 7u83 326
		E_rename_illegal_escape(istream);
2 7u83 327
		break;
328
	      case ISTREAM_STAT_NO_CHAR:
329
		/*NOTHING*/
330
		break;
331
	    }
332
	    break;
333
	  default:
6 7u83 334
	    dstring_append_char(&dstring, c);
2 7u83 335
	    break;
336
	}
337
    }
338
  eof:
6 7u83 339
    E_rename_unexpected_eof(istream);
340
    dstring_destroy(&dstring);
2 7u83 341
    token->tag = RTOK_EOF;
342
}
343
 
344
static void
6 7u83 345
rename_file_next_token(IStreamP     istream,				RenameTokenP token)
2 7u83 346
{
347
    char c;
348
 
349
  again:
6 7u83 350
    if (rename_file_skip_white_space(istream, &c)) {
2 7u83 351
	switch (c) {
352
	  case '[':
6 7u83 353
	    rename_file_read_unique(istream, token);
2 7u83 354
	    break;
355
	  case '\'':
6 7u83 356
	     rename_file_read_shape(istream, token);
2 7u83 357
	    break;
358
	  case '"':
6 7u83 359
	    rename_file_read_string(istream, token);
2 7u83 360
	    break;
361
	  case ';':
362
	    token->tag = RTOK_SEMI;
363
	    break;
364
	  default:
6 7u83 365
	    E_rename_illegal_char(istream, c);
2 7u83 366
	    goto again;
367
	}
368
    } else {
369
	token->tag = RTOK_EOF;
370
    }
371
}
372
 
373
static void
6 7u83 374
rename_file_parse_names(IStreamP     istream,				 NStringP     shape ,
375
				 ArgDataP     arg_data ,
2 7u83 376
				 RenameTokenP token)
377
{
6 7u83 378
    rename_file_next_token(istream, token);
2 7u83 379
    while (token->tag == RTOK_NAME) {
380
	NameKeyT name;
381
 
6 7u83 382
	name_key_assign(&name, & (token->u.name));
383
	rename_file_next_token(istream, token);
2 7u83 384
	if (token->tag != RTOK_NAME) {
6 7u83 385
	    E_rename_expected_name(istream);
386
	    name_key_destroy(&name);
2 7u83 387
	    if (token->tag != RTOK_SEMI) {
388
		return;
389
	    }
6 7u83 390
	    rename_file_next_token(istream, token);
2 7u83 391
	} else {
392
	    NameKeyT to_name;
393
 
6 7u83 394
	    name_key_assign(&to_name, & (token->u.name));
395
	    arg_data_add_rename(arg_data, shape, &name, &to_name);
396
	    rename_file_next_token(istream, token);
2 7u83 397
	    if (token->tag != RTOK_SEMI) {
6 7u83 398
		E_rename_expected_semi(istream);
2 7u83 399
	    } else {
6 7u83 400
		rename_file_next_token(istream, token);
2 7u83 401
	    }
402
	}
403
    }
404
}
405
 
406
static void
6 7u83 407
rename_file_parse_1(IStreamP istream,			     ArgDataP arg_data)
2 7u83 408
{
409
    BoolT        need_error = TRUE;
410
    RenameTokenT token;
411
    NStringT     shape;
412
 
6 7u83 413
    rename_file_next_token(istream, &token);
2 7u83 414
    while (token.tag != RTOK_EOF) {
415
	switch (token.tag) {
416
	  case RTOK_SHAPE:
6 7u83 417
	    nstring_assign(&shape, & (token.u.shape));
418
	    rename_file_parse_names(istream, &shape, arg_data, &token);
419
	    nstring_destroy(&shape);
2 7u83 420
	    need_error = TRUE;
421
	    break;
422
	  case RTOK_NAME:
6 7u83 423
	    name_key_destroy(& (token.u.name));
2 7u83 424
	    FALL_THROUGH;
425
	  default:
426
	    if (need_error) {
6 7u83 427
		E_rename_expected_shape(istream);
2 7u83 428
		need_error = FALSE;
429
	    }
6 7u83 430
	    rename_file_next_token(istream, &token);
2 7u83 431
	    break;
432
	}
433
    }
434
}
435
 
436
/*--------------------------------------------------------------------------*/
437
 
438
void
6 7u83 439
rename_file_parse(CStringP name,			   ArgDataP arg_data)
2 7u83 440
{
441
    IStreamT istream;
442
 
6 7u83 443
    if (istream_open(&istream, name)) {
444
	rename_file_parse_1(&istream, arg_data);
445
	istream_close(&istream);
2 7u83 446
    } else {
6 7u83 447
	E_cannot_open_rename_file(name);
2 7u83 448
    }
6 7u83 449
    if (error_max_reported_severity() >= ERROR_SEVERITY_ERROR) {
450
	exit(EXIT_FAILURE);
2 7u83 451
	UNREACHED;
452
    }
453
}
454
 
455
/*
456
 * Local variables(smf):
457
 * eval: (include::add-path-entry "../os-interface" "../library" "../tdf")
458
 * eval: (include::add-path-entry "../generated")
459
 * End:
460
**/