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
#include "config.h"
62
#include "c_types.h"
63
#include "exp_ops.h"
64
#include "str_ops.h"
65
#include "error.h"
66
#include "extra.h"
67
#include "catalog.h"
68
#include "option.h"
69
#include "hash.h"
70
#include "lex.h"
71
#include "literal.h"
72
#include "syntax.h"
73
#include "ustring.h"
74
#include "xalloc.h"
75
 
76
 
77
/*
78
    ERROR CATALOGUE
79
 
80
    The error catalogue definition is automatically generated.  It consists
81
    of an array of ERR_DATA structures, ERR_CATALOG.
82
*/
83
 
84
#include "errors3.h"
85
 
86
 
87
/*
88
    NUMBER OF ERRORS
89
 
90
    This value gives the number of errors in the error catalogue.
91
*/
92
 
6 7u83 93
#define CATALOG_SIZE	array_size(ERR_CATALOG)
94
unsigned catalog_size = (unsigned)CATALOG_SIZE;
2 7u83 95
 
96
 
97
/*
98
    ERROR NAME HASH TABLE
99
 
100
    This hash table is used to hold the names of the various errors in
101
    the error catalogue.
102
*/
103
 
104
typedef struct err_hash_tag {
6 7u83 105
	int number;
106
	ERR_DATA *entry;
107
	struct err_hash_tag *next;
108
} ERR_HASH;
2 7u83 109
 
110
#define HASH_ERROR	128
6 7u83 111
static ERR_HASH *error_hash[HASH_ERROR + 1];
112
static ERR_HASH *all_error_hash = NULL;
2 7u83 113
 
114
 
115
/*
116
    INITIALISE THE ERROR NAME HASH TABLE
117
 
118
    This routine initialises the error hash table.
119
*/
120
 
6 7u83 121
static void
122
init_err_hash(void)
2 7u83 123
{
6 7u83 124
	int i;
125
	ERR_DATA *cat = ERR_CATALOG;
126
	ERR_HASH *err = xmalloc_nof(ERR_HASH, CATALOG_SIZE);
127
	all_error_hash = err;
128
	for (i = 0; i <= HASH_ERROR; i++) {
129
		error_hash[i] = NULL;
2 7u83 130
	}
6 7u83 131
	for (i = 0; i < CATALOG_SIZE; i++) {
132
		unsigned long h;
133
		CONST char *s = cat->name;
134
		if (s) {
135
			h = hash(ustrlit(s));
136
			h %= HASH_ERROR;
137
		} else {
138
			h = HASH_ERROR;
139
		}
140
		err->number = i;
141
		err->entry = cat;
142
		err->next = error_hash[h];
143
		error_hash[h] = err;
144
		cat++;
145
		err++;
146
	}
147
	return;
2 7u83 148
}
149
 
150
 
151
/*
152
    FIND AN ERROR NUMBER
153
 
154
    This routine finds the error number corresponding to the string literal
155
    expression s.  n gives a likely value to try first.  It returns -1 if
156
    s is not the name of a known error.
157
*/
158
 
6 7u83 159
int
160
find_error_no(STRING s, int n)
2 7u83 161
{
6 7u83 162
	unsigned kind = DEREF_unsigned(str_simple_kind(s));
163
	if (kind == STRING_NONE) {
164
		ERR_HASH *err;
165
		unsigned long h;
166
		string text = DEREF_string(str_simple_text(s));
167
		ulong len = DEREF_ulong(str_simple_len(s));
168
		if (n >= 0 && n < CATALOG_SIZE) {
169
			string nm = ustrlit(ERR_CATALOG[n].name);
170
			if (nm && ustreq(text, nm)) {
171
				if (len == (ulong)ustrlen(nm)) {
172
					return (n);
173
				}
174
			}
2 7u83 175
		}
6 7u83 176
		if (all_error_hash == NULL) {
177
			init_err_hash();
178
		}
179
		h = hash(text);
180
		h %= HASH_ERROR;
181
		for (err = error_hash[h]; err != NULL; err = err->next) {
182
			string nm = ustrlit(err->entry->name);
183
			if (nm && ustreq(text, nm)) {
184
				if (len == (ulong)ustrlen(nm)) {
185
					return (err->number);
186
				}
187
			}
188
		}
2 7u83 189
	}
6 7u83 190
	return (-1);
2 7u83 191
}
192
 
193
 
194
/*
195
    SET AN ERROR SEVERITY LEVEL
196
 
197
    This routine sets the severity level of error number n to the error
198
    severity corresponding to the option number opt.
199
*/
200
 
6 7u83 201
void
202
set_error_sev(int n, int opt)
2 7u83 203
{
6 7u83 204
	if (n >= 0 && n < CATALOG_SIZE) {
205
		ERR_CATALOG[n].usage = opt;
206
	}
207
	return;
2 7u83 208
}
209
 
210
 
211
/*
212
    LOOK UP A LANGUAGE DEPENDENT TERM
213
 
214
    Certain technical terms vary slightly between C and C++.  This routine
215
    looks up the term corresponding to the lexical token number t in the
216
    current language.  The default is the normal lexical token name.
217
*/
218
 
6 7u83 219
string
220
find_vocab(int t)
2 7u83 221
{
6 7u83 222
	CONST char *s = token_names[t];
2 7u83 223
#if LANGUAGE_C
6 7u83 224
	switch (t) {
225
	case lex_class:
226
		s = "struct/union"; break;
227
	}
2 7u83 228
#endif
6 7u83 229
	return (ustrlit(s));
2 7u83 230
}
231
 
232
 
233
/*
234
    INITIALISE THE ERROR CATALOGUE
235
 
236
    This routine initialises the error catalogue.
237
*/
238
 
6 7u83 239
void
240
init_catalog(void)
2 7u83 241
{
6 7u83 242
	ASSERT(OPT_error == 0);
243
	return;
2 7u83 244
}
245
 
246
 
247
/*
248
    TERMINATE THE ERROR CATALOGUE
249
 
250
    This routine terminates the error catalogue.
251
*/
252
 
6 7u83 253
void
254
term_catalog(void)
2 7u83 255
{
6 7u83 256
	xfree_nof(all_error_hash);
257
	all_error_hash = NULL;
258
	return;
2 7u83 259
}