File: errors.c

package info (click to toggle)
inform 6.21-1
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 3,620 kB
  • ctags: 3,527
  • sloc: ansic: 27,278; makefile: 76; sh: 9; lisp: 2
file content (426 lines) | stat: -rw-r--r-- 14,498 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* ------------------------------------------------------------------------- */
/*   "errors" : Warnings, errors and fatal errors                            */
/*              (with error throwback code for RISC OS machines)             */
/*                                                                           */
/*   Part of Inform 6.21                                                     */
/*   copyright (c) Graham Nelson 1993, 1994, 1995, 1996, 1997, 1998, 1999    */
/*                                                                           */
/* ------------------------------------------------------------------------- */

#include "header.h"

static char error_message_buff[256];

/* ------------------------------------------------------------------------- */
/*   Error preamble printing.                                                */
/* ------------------------------------------------------------------------- */

ErrorPosition ErrorReport;             /*  Maintained by "lexer.c"           */

static void print_preamble(void)
{
    /*  Only really prints the preamble to an error or warning message:

        e.g.  "jigsaw.apollo", line 24:

        The format is controllable (from an ICL switch) since this assists
        the working of some development environments.                        */

    int j, with_extension_flag = FALSE; char *p;

    j = ErrorReport.file_number;
    if (j <= 0) p = ErrorReport.source;
    else p = InputFiles[j-1].filename;

    switch(error_format)
    {
        case 0:  /* RISC OS error message format */

            if (!(ErrorReport.main_flag)) printf("\"%s\", ", p);
            printf("line %d: ", ErrorReport.line_number);
            break;

        case 1:  /* Microsoft error message format */

            for (j=0; p[j]!=0; j++)
            {   if (p[j] == FN_SEP) with_extension_flag = TRUE;
                if (p[j] == '.') with_extension_flag = FALSE;
            }
            printf("%s", p);
            if (with_extension_flag) printf("%s", Source_Extension);
            printf("(%d): ", ErrorReport.line_number);
            break;

        case 2:  /* Macintosh Programmer's Workshop error message format */

            printf("File \"%s\"; Line %d\t# ", p, ErrorReport.line_number);
            break;
    }
}

static char trimmed_text[128];

static void trim_text(char *s)
{   int i;
    if (strlen(s) < 128) { strcpy(trimmed_text, s); return; }
    for (i=0; i<120; i++) trimmed_text[i] = s[i];
    trimmed_text[i++] = '.';
    trimmed_text[i++] = '.';
    trimmed_text[i++] = '.';
    trimmed_text[i++] = 0;
    return;
}

/* ------------------------------------------------------------------------- */
/*   Fatal errors (which have style 0)                                       */
/* ------------------------------------------------------------------------- */

extern void fatalerror(char *s)
{   print_preamble();

    printf("Fatal error: %s\n",s);
    if (no_compiler_errors > 0) print_sorry_message();

#ifdef ARC_THROWBACK
    throwback(0, s);
    throwback_end();
#endif
#ifdef MAC_FACE
    close_all_source();
    if (temporary_files_switch) remove_temp_files();
    abort_transcript_file();
    free_arrays();
    if (store_the_text)
        my_free(&all_text,"transcription text");
    longjmp(g_fallback, 1);
#endif
    exit(1);
}

extern void fatalerror_named(char *m, char *fn)
{   trim_text(fn);
    sprintf(error_message_buff, "%s \"%s\"", m, trimmed_text);
    fatalerror(error_message_buff);
}

extern void memory_out_error(int32 size, int32 howmany, char *name)
{   if (howmany == 1)
        sprintf(error_message_buff,
            "Run out of memory allocating %d bytes for %s", size, name);
    else
        sprintf(error_message_buff,
            "Run out of memory allocating array of %dx%d bytes for %s",
                howmany, size, name);
    fatalerror(error_message_buff);
}

extern void memoryerror(char *s, int32 size)
{
    sprintf(error_message_buff,
        "The memory setting %s (which is %ld at present) has been \
exceeded.  Try running Inform again with $%s=<some-larger-number> on the \
command line.",s,(long int) size,s);
    fatalerror(error_message_buff);
}

/* ------------------------------------------------------------------------- */
/*   Survivable diagnostics:                                                 */
/*      compilation errors   style 1                                         */
/*      warnings             style 2                                         */
/*      linkage errors       style 3                                         */
/*      compiler errors      style 4 (these should never happen and          */
/*                                    indicate a bug in Inform)              */
/* ------------------------------------------------------------------------- */

static int errors[MAX_ERRORS];

int no_errors, no_warnings, no_suppressed_warnings, no_link_errors,
    no_compiler_errors;

char *forerrors_buff;
int  forerrors_pointer;

static void message(int style, char *s)
{   int throw_style = style;
    if (hash_printed_since_newline) printf("\n");
    hash_printed_since_newline = FALSE;
    print_preamble();
    switch(style)
    {   case 1: printf("Error: "); no_errors++; break;
        case 2: printf("Warning: "); no_warnings++; break;
        case 3: printf("Error:  [linking '%s']  ", current_module_filename);
                no_link_errors++; no_errors++; throw_style=1; break;
        case 4: printf("*** Compiler error: ");
                no_compiler_errors++; throw_style=1; break;
    }
    printf(" %s\n", s);
#ifdef ARC_THROWBACK
    throwback(throw_style, s);
#endif
#ifdef MAC_FACE
    ProcessEvents (&g_proc);
    if (g_proc != true)
    {   free_arrays();
        if (store_the_text)
            my_free(&all_text,"transcription text");
        close_all_source ();
        if (temporary_files_switch) remove_temp_files();
        abort_transcript_file();
        longjmp (g_fallback, 1);
    }
#endif
    if ((!concise_switch) && (forerrors_pointer > 0) && (style <= 2))
    {   forerrors_buff[forerrors_pointer] = 0;
        sprintf(forerrors_buff+68,"  ...etc");
        printf("> %s\n",forerrors_buff);
    }
}

/* ------------------------------------------------------------------------- */
/*   Style 1: Error message routines                                         */
/* ------------------------------------------------------------------------- */

extern void error(char *s)
{   if (no_errors == MAX_ERRORS)
        fatalerror("Too many errors: giving up");
    errors[no_errors] = no_syntax_lines;
    message(1,s);
}

extern void error_named(char *s1, char *s2)
{   trim_text(s2);
    sprintf(error_message_buff,"%s \"%s\"",s1,trimmed_text);
    error(error_message_buff);
}

extern void error_named_at(char *s1, char *s2, int32 report_line)
{   int i;

    ErrorPosition E = ErrorReport;
    if (report_line != -1)
    {   ErrorReport.file_number = report_line/0x10000;
        ErrorReport.line_number = report_line%0x10000;
        ErrorReport.main_flag = (ErrorReport.file_number == 1);
    }

    trim_text(s2);
    sprintf(error_message_buff,"%s \"%s\"",s1,trimmed_text);

    i = concise_switch; concise_switch = TRUE;
    error(error_message_buff);
    ErrorReport = E; concise_switch = i;
}

extern void no_such_label(char *lname)
{   error_named("No such label as",lname);
}

extern void ebf_error(char *s1, char *s2)
{   trim_text(s2);
    sprintf(error_message_buff, "Expected %s but found %s", s1, trimmed_text);
    error(error_message_buff);
}

extern void char_error(char *s, int ch)
{   int32 uni;

    uni = iso_to_unicode(ch);

    if (uni >= 0x100)
    {   sprintf(error_message_buff,
            "%s (unicode) $%04x = (ISO %s) $%02x", s, uni,
            name_of_iso_set(character_set_setting), ch);
    }
    else
        sprintf(error_message_buff, "%s (ISO Latin1) $%02x", s, uni);

    if (((uni>=32) && (uni<127))
        || (((uni >= 0xa1) && (uni <= 0xff)) && (character_set_setting==1)))
        sprintf(error_message_buff+strlen(error_message_buff),
            ", i.e., '%c'", uni);

    error(error_message_buff);
}

extern void unicode_char_error(char *s, int32 uni)
{
    if (uni >= 0x100)
        sprintf(error_message_buff, "%s (unicode) $%04x", s, uni);
    else
        sprintf(error_message_buff, "%s (ISO Latin1) $%02x", s, uni);

    if (((uni>=32) && (uni<127))
        || (((uni >= 0xa1) && (uni <= 0xff)) && (character_set_setting==1)))
        sprintf(error_message_buff+strlen(error_message_buff),
            ", i.e., '%c'", uni);

    error(error_message_buff);
}

/* ------------------------------------------------------------------------- */
/*   Style 2: Warning message routines                                       */
/* ------------------------------------------------------------------------- */

extern void warning(char *s1)
{   if (nowarnings_switch) { no_suppressed_warnings++; return; }
    message(2,s1);
}

extern void warning_named(char *s1, char *s2)
{
    trim_text(s2);
    if (nowarnings_switch) { no_suppressed_warnings++; return; }
    sprintf(error_message_buff,"%s \"%s\"", s1, trimmed_text);
    message(2,error_message_buff);
}

extern void dbnu_warning(char *type, char *name, int32 report_line)
{   int i;
    ErrorPosition E = ErrorReport;
    if (nowarnings_switch) { no_suppressed_warnings++; return; }
    if (report_line != -1)
    {   ErrorReport.file_number = report_line/0x10000;
        ErrorReport.line_number = report_line%0x10000;
        ErrorReport.main_flag = (ErrorReport.file_number == 1);
    }
    sprintf(error_message_buff, "%s \"%s\" declared but not used", type, name);
    i = concise_switch; concise_switch = TRUE;
    message(2,error_message_buff);
    concise_switch = i;
    ErrorReport = E;
}

extern void obsolete_warning(char *s1)
{   if (is_systemfile()==1) return;
    if (obsolete_switch || nowarnings_switch)
    {   no_suppressed_warnings++; return; }
    sprintf(error_message_buff, "Obsolete usage: %s",s1);
    message(2,error_message_buff);
}

/* ------------------------------------------------------------------------- */
/*   Style 3: Link error message routines                                    */
/* ------------------------------------------------------------------------- */

extern void link_error(char *s)
{   if (no_errors==MAX_ERRORS) fatalerror("Too many errors: giving up");
    errors[no_errors] = no_syntax_lines;
    message(3,s);
}

extern void link_error_named(char *s1, char *s2)
{   trim_text(s2);
    sprintf(error_message_buff,"%s \"%s\"",s1,trimmed_text);
    link_error(error_message_buff);
}

/* ------------------------------------------------------------------------- */
/*   Style 4: Compiler error message routines                                */
/* ------------------------------------------------------------------------- */

extern void print_sorry_message(void)
{   printf(
"***********************************************************************\n\
* 'Compiler errors' should never occur if Inform is working properly. *\n\
* This is version %d.%02d of Inform, dated %20s: so      *\n\
* if that was more than six months ago, there may be a more recent    *\n\
* version available, from which the problem may have been removed.    *\n\
* If not, please report this fault to:   graham@gnelson.demon.co.uk   *\n\
* and if at all possible, please include your source code, as faults  *\n\
* such as these are rare and often difficult to reproduce.  Sorry.    *\n\
***********************************************************************\n",
    (RELEASE_NUMBER/100)%10, RELEASE_NUMBER%100, RELEASE_DATE);
}

extern int compiler_error(char *s)
{   if (no_link_errors > 0) return FALSE;
    if (no_errors > 0) return FALSE;
    if (no_compiler_errors==MAX_ERRORS)
        fatalerror("Too many compiler errors: giving up");
    message(4,s);
    return TRUE;
}

extern int compiler_error_named(char *s1, char *s2)
{   if (no_link_errors > 0) return FALSE;
    if (no_errors > 0) return FALSE;
    trim_text(s2);
    sprintf(error_message_buff,"%s \"%s\"",s1,trimmed_text);
    compiler_error(error_message_buff);
    return TRUE;
}

/* ------------------------------------------------------------------------- */
/*   Code for the Acorn RISC OS operating system, donated by Robin Watts,    */
/*   to provide error throwback under the DDE environment                    */
/* ------------------------------------------------------------------------- */

#ifdef ARC_THROWBACK

#define DDEUtils_ThrowbackStart 0x42587
#define DDEUtils_ThrowbackSend  0x42588
#define DDEUtils_ThrowbackEnd   0x42589

#include "kernel.h"

extern void throwback_start(void)
{    _kernel_swi_regs regs;
     if (throwback_switch)
         _kernel_swi(DDEUtils_ThrowbackStart, &regs, &regs);
}

extern void throwback_end(void)
{   _kernel_swi_regs regs;
    if (throwback_switch)
        _kernel_swi(DDEUtils_ThrowbackEnd, &regs, &regs);
}

int throwback_started = FALSE;

extern void throwback(int severity, char * error)
{   _kernel_swi_regs regs;
    if (!throwback_started)
    {   throwback_started = TRUE;
        throwback_start();
    }
    if (throwback_switch)
    {   regs.r[0] = 1;
        if ((ErrorReport.file_number == -1)
            || (ErrorReport.file_number == 0))
            regs.r[2] = (int) (InputFiles[0].filename);
        else regs.r[2] = (int) (InputFiles[ErrorReport.file_number-1].filename);
        regs.r[3] = ErrorReport.line_number;
        regs.r[4] = (2-severity);
        regs.r[5] = (int) error;
       _kernel_swi(DDEUtils_ThrowbackSend, &regs, &regs);
    }
}

#endif

/* ========================================================================= */
/*   Data structure management routines                                      */
/* ------------------------------------------------------------------------- */

extern void init_errors_vars(void)
{   forerrors_buff = NULL;
    no_errors = 0; no_warnings = 0; no_suppressed_warnings = 0;
    no_compiler_errors = 0;
}

extern void errors_begin_pass(void)
{   ErrorReport.line_number = 0;
    ErrorReport.file_number = -1;
    ErrorReport.source = "<no text read yet>";
    ErrorReport.main_flag = FALSE;
}

extern void errors_allocate_arrays(void)
{   forerrors_buff = my_malloc(512, "errors buffer");
}

extern void errors_free_arrays(void)
{   my_free(&forerrors_buff, "errors buffer");
}

/* ========================================================================= */