Initial import from FreeBSD RELENG_4:
[dragonfly.git] / gnu / usr.bin / as / messages.c
1 /* messages.c - error reporter -
2    Copyright (C) 1987, 1991, 1992 Free Software Foundation, Inc.
3
4    This file is part of GAS, the GNU Assembler.
5
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GAS is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GAS; see the file COPYING.  If not, write to
18    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #ifndef lint
21 static char rcsid[] = "$FreeBSD: src/gnu/usr.bin/as/messages.c,v 1.8 1999/08/27 23:34:19 peter Exp $";
22 #endif
23
24 #include <stdio.h>
25 #include <errno.h>
26
27 #include "as.h"
28
29 #ifndef __STDC__
30 #ifndef NO_STDARG
31 #define NO_STDARG
32 #endif
33 #endif
34
35 #ifndef NO_STDARG
36 #include <stdarg.h>
37 #else
38 #ifndef NO_VARARGS
39 #include <varargs.h>
40 #endif /* NO_VARARGS */
41 #endif /* NO_STDARG */
42
43 extern char *strerror ();
44
45 static void as_show_where PARAMS ((void));
46 static void as_warn_internal PARAMS ((char *, unsigned int, char *));
47 static void as_bad_internal PARAMS ((char *, unsigned int, char *));
48
49 /*
50  * Despite the rest of the comments in this file, (FIXME-SOON),
51  * here is the current scheme for error messages etc:
52  *
53  * as_fatal() is used when gas is quite confused and
54  * continuing the assembly is pointless.  In this case we
55  * exit immediately with error status.
56  *
57  * as_bad() is used to mark errors that result in what we
58  * presume to be a useless object file.  Say, we ignored
59  * something that might have been vital.  If we see any of
60  * these, assembly will continue to the end of the source,
61  * no object file will be produced, and we will terminate
62  * with error status.  The new option, -Z, tells us to
63  * produce an object file anyway but we still exit with
64  * error status.  The assumption here is that you don't want
65  * this object file but we could be wrong.
66  *
67  * as_warn() is used when we have an error from which we
68  * have a plausible error recovery.  eg, masking the top
69  * bits of a constant that is longer than will fit in the
70  * destination.  In this case we will continue to assemble
71  * the source, although we may have made a bad assumption,
72  * and we will produce an object file and return normal exit
73  * status (ie, no error).  The new option -X tells us to
74  * treat all as_warn() errors as as_bad() errors.  That is,
75  * no object file will be produced and we will exit with
76  * error status.  The idea here is that we don't kill an
77  * entire make because of an error that we knew how to
78  * correct.  On the other hand, sometimes you might want to
79  * stop the make at these points.
80  *
81  * as_tsktsk() is used when we see a minor error for which
82  * our error recovery action is almost certainly correct.
83  * In this case, we print a message and then assembly
84  * continues as though no error occurred.
85  */
86
87 static void
88 identify (file)
89      char *file;
90 {
91   static int identified;
92   if (identified)
93     return;
94   identified++;
95
96   if (!file)
97     {
98       unsigned int x;
99       as_where (&file, &x);
100     }
101
102   fprintf (stderr, "%s: Assembler messages:\n", file);
103 }
104
105 static int warning_count;       /* Count of number of warnings issued */
106
107 int
108 had_warnings ()
109 {
110   return (warning_count);
111 }                               /* had_err() */
112
113 /* Nonzero if we've hit a 'bad error', and should not write an obj file,
114    and exit with a nonzero error code */
115
116 static int error_count;
117
118 int
119 had_errors ()
120 {
121   return (error_count);
122 }                               /* had_errors() */
123
124
125 /* Print the current location to stderr.  */
126
127 static void
128 as_show_where ()
129 {
130   char *file;
131   unsigned int line;
132
133   as_where (&file, &line);
134   identify (file);
135   fprintf (stderr, "%s:%u: ", file, line);
136 }
137
138 /*
139  *                      a s _ p e r r o r
140  *
141  * Like perror(3), but with more info.
142  */
143
144 void
145 as_perror (gripe, filename)
146      const char *gripe;         /* Unpunctuated error theme. */
147      const char *filename;
148 {
149   const char *errtxt;
150
151   as_show_where ();
152   fprintf (stderr, gripe, filename);
153 #ifdef BFD_ASSEMBLER
154   errtxt = bfd_errmsg (bfd_get_error ());
155 #else
156   errtxt = strerror (errno);
157 #endif
158   fprintf (stderr, ": %s\n", errtxt);
159   errno = 0;
160 #ifdef BFD_ASSEMBLER
161   bfd_set_error (bfd_error_no_error);
162 #endif
163 }
164
165 /*
166  *                      a s _ t s k t s k ()
167  *
168  * Send to stderr a string as a warning, and locate warning
169  * in input file(s).
170  * Please only use this for when we have some recovery action.
171  * Please explain in string (which may have '\n's) what recovery was done.
172  */
173
174 #ifndef NO_STDARG
175 void
176 as_tsktsk (const char *format,...)
177 {
178   va_list args;
179
180   as_show_where ();
181   va_start (args, format);
182   vfprintf (stderr, format, args);
183   va_end (args);
184   (void) putc ('\n', stderr);
185 }                               /* as_tsktsk() */
186
187 #else
188 #ifndef NO_VARARGS
189 void
190 as_tsktsk (format, va_alist)
191      char *format;
192      va_dcl
193 {
194   va_list args;
195
196   as_show_where ();
197   va_start (args);
198   vfprintf (stderr, format, args);
199   va_end (args);
200   (void) putc ('\n', stderr);
201 }                               /* as_tsktsk() */
202
203 #else
204 /*VARARGS1 */
205 as_tsktsk (format, args)
206      char *format;
207 {
208   as_show_where ();
209   _doprnt (format, &args, stderr);
210   (void) putc ('\n', stderr);
211 }                               /* as_tsktsk */
212
213 #endif /* not NO_VARARGS */
214 #endif /* not NO_STDARG */
215
216 /* The common portion of as_warn and as_warn_where.  */
217
218 static void
219 as_warn_internal (file, line, buffer)
220      char *file;
221      unsigned int line;
222      char *buffer;
223 {
224   ++warning_count;
225
226   if (file == NULL)
227     as_where (&file, &line);
228
229   identify (file);
230   fprintf (stderr, "%s:%u: Warning: ", file, line);
231   fputs (buffer, stderr);
232   (void) putc ('\n', stderr);
233 #ifndef NO_LISTING
234   listing_warning (buffer);
235 #endif
236 }
237
238 /*
239  *                      a s _ w a r n ()
240  *
241  * Send to stderr a string as a warning, and locate warning
242  * in input file(s).
243  * Please only use this for when we have some recovery action.
244  * Please explain in string (which may have '\n's) what recovery was done.
245  */
246
247 #if 1
248 #define flag_no_warnings        (flagseen['W'])
249 #endif
250
251 #ifndef NO_STDARG
252 void
253 as_warn (const char *format,...)
254 {
255   va_list args;
256   char buffer[200];
257
258   if (!flag_no_warnings)
259     {
260       va_start (args, format);
261       vsprintf (buffer, format, args);
262       va_end (args);
263       as_warn_internal ((char *) NULL, 0, buffer);
264     }
265 }                               /* as_warn() */
266
267 #else
268 #ifndef NO_VARARGS
269 void
270 as_warn (format, va_alist)
271      char *format;
272      va_dcl
273 {
274   va_list args;
275   char buffer[200];
276
277   if (!flag_no_warnings)
278     {
279       va_start (args);
280       vsprintf (buffer, format, args);
281       va_end (args);
282       as_warn_internal ((char *) NULL, 0, buffer);
283     }
284 }                               /* as_warn() */
285
286 #else
287 /*VARARGS1 */
288 as_warn (format, args)
289      char *format;
290 {
291   if (!flag_no_warnings)
292     {
293       ++warning_count;
294       as_show_where ();
295       fprintf (stderr, "Warning: ");
296       _doprnt (format, &args, stderr);
297       (void) putc ('\n', stderr);
298     }
299 }                               /* as_warn() */
300
301 #endif /* not NO_VARARGS */
302 #endif /* not NO_STDARG */
303
304 /* as_warn_where, like as_bad but the file name and line number are
305    passed in.  Unfortunately, we have to repeat the function in order
306    to handle the varargs correctly and portably.  */
307
308 #ifndef NO_STDARG
309 void
310 as_warn_where (char *file, unsigned int line, const char *format,...)
311 {
312   va_list args;
313   char buffer[200];
314
315   if (!flag_no_warnings)
316     {
317       va_start (args, format);
318       vsprintf (buffer, format, args);
319       va_end (args);
320       as_warn_internal (file, line, buffer);
321     }
322 }                               /* as_warn() */
323
324 #else
325 #ifndef NO_VARARGS
326 void
327 as_warn_where (file, line, format, va_alist)
328      char *file;
329      unsigned int line;
330      char *format;
331      va_dcl
332 {
333   va_list args;
334   char buffer[200];
335
336   if (!flag_no_warnings)
337     {
338       va_start (args);
339       vsprintf (buffer, format, args);
340       va_end (args);
341       as_warn_internal (file, line, buffer);
342     }
343 }                               /* as_warn() */
344
345 #else
346 /*VARARGS1 */
347 as_warn_where (file, line, format, args)
348      char *file;
349      unsigned int line;
350      char *format;
351 {
352   if (!flag_no_warnings)
353     {
354       ++warning_count;
355       identify (file);
356       fprintf (stderr, "%s:%u: Warning: ", file, line);
357       _doprnt (format, &args, stderr);
358       (void) putc ('\n', stderr);
359     }
360 }                               /* as_warn() */
361
362 #endif /* not NO_VARARGS */
363 #endif /* not NO_STDARG */
364
365 /* The common portion of as_bad and as_bad_where.  */
366
367 static void
368 as_bad_internal (file, line, buffer)
369      char *file;
370      unsigned int line;
371      char *buffer;
372 {
373   ++error_count;
374
375   if (file == NULL)
376     as_where (&file, &line);
377
378   identify (file);
379   fprintf (stderr, "%s:%u: Error: ", file, line);
380   fputs (buffer, stderr);
381   (void) putc ('\n', stderr);
382 #ifndef NO_LISTING
383   listing_error (buffer);
384 #endif
385 }
386
387 /*
388  *                      a s _ b a d ()
389  *
390  * Send to stderr a string as a warning, and locate warning in input file(s).
391  * Please us when there is no recovery, but we want to continue processing
392  * but not produce an object file.
393  * Please explain in string (which may have '\n's) what recovery was done.
394  */
395
396 #ifndef NO_STDARG
397 void
398 as_bad (const char *format,...)
399 {
400   va_list args;
401   char buffer[200];
402
403   va_start (args, format);
404   vsprintf (buffer, format, args);
405   va_end (args);
406
407   as_bad_internal ((char *) NULL, 0, buffer);
408 }
409
410 #else
411 #ifndef NO_VARARGS
412 void
413 as_bad (format, va_alist)
414      char *format;
415      va_dcl
416 {
417   va_list args;
418   char buffer[200];
419
420   va_start (args);
421   vsprintf (buffer, format, args);
422   va_end (args);
423
424   as_bad_internal ((char *) NULL, 0, buffer);
425 }
426
427 #else
428 /*VARARGS1 */
429 as_bad (format, args)
430      char *format;
431 {
432   ++error_count;
433
434   as_show_where ();
435   fprintf (stderr, "Error: ");
436   _doprnt (format, &args, stderr);
437   (void) putc ('\n', stderr);
438 }                               /* as_bad() */
439
440 #endif /* not NO_VARARGS */
441 #endif /* not NO_STDARG */
442
443 /* as_bad_where, like as_bad but the file name and line number are
444    passed in.  Unfortunately, we have to repeat the function in order
445    to handle the varargs correctly and portably.  */
446
447 #ifndef NO_STDARG
448 void
449 as_bad_where (char *file, unsigned int line, const char *format,...)
450 {
451   va_list args;
452   char buffer[200];
453
454   va_start (args, format);
455   vsprintf (buffer, format, args);
456   va_end (args);
457
458   as_bad_internal (file, line, buffer);
459 }
460
461 #else
462 #ifndef NO_VARARGS
463 void
464 as_bad_where (file, line, format, va_alist)
465      char *file;
466      unsigned int line;
467      char *format;
468      va_dcl
469 {
470   va_list args;
471   char buffer[200];
472
473   va_start (args);
474   vsprintf (buffer, format, args);
475   va_end (args);
476
477   as_bad_internal (file, line, buffer);
478 }
479
480 #else
481 /*VARARGS1 */
482 as_bad_where (file, line, format, args)
483      char *file;
484      unsigned int line;
485      char *format;
486 {
487   ++error_count;
488
489   identify (file);
490   fprintf (stderr, "%s:%u: Error: ", file, line);
491   _doprnt (format, &args, stderr);
492   (void) putc ('\n', stderr);
493 }                               /* as_bad() */
494
495 #endif /* not NO_VARARGS */
496 #endif /* not NO_STDARG */
497
498 /*
499  *                      a s _ f a t a l ()
500  *
501  * Send to stderr a string as a fatal message, and print location of error in
502  * input file(s).
503  * Please only use this for when we DON'T have some recovery action.
504  * It exit()s with a warning status.
505  */
506
507 #ifndef NO_STDARG
508 void
509 as_fatal (const char *format,...)
510 {
511   va_list args;
512
513   as_show_where ();
514   va_start (args, format);
515   fprintf (stderr, "Fatal error:");
516   vfprintf (stderr, format, args);
517   (void) putc ('\n', stderr);
518   va_end (args);
519   exit (33);
520 }                               /* as_fatal() */
521
522 #else
523 #ifndef NO_VARARGS
524 void
525 as_fatal (format, va_alist)
526      char *format;
527      va_dcl
528 {
529   va_list args;
530
531   as_show_where ();
532   va_start (args);
533   fprintf (stderr, "Fatal error:");
534   vfprintf (stderr, format, args);
535   (void) putc ('\n', stderr);
536   va_end (args);
537   exit (33);
538 }                               /* as_fatal() */
539
540 #else
541 /*VARARGS1 */
542 as_fatal (format, args)
543      char *format;
544 {
545   as_show_where ();
546   fprintf (stderr, "Fatal error:");
547   _doprnt (format, &args, stderr);
548   (void) putc ('\n', stderr);
549   exit (33);                    /* What is a good exit status? */
550 }                               /* as_fatal() */
551
552 #endif /* not NO_VARARGS */
553 #endif /* not NO_STDARG */
554
555 void
556 fprint_value (file, val)
557      FILE *file;
558      valueT val;
559 {
560   if (sizeof (val) <= sizeof (long))
561     {
562       fprintf (file, "%ld", (long) val);
563       return;
564     }
565 #ifdef BFD_ASSEMBLER
566   if (sizeof (val) <= sizeof (bfd_vma))
567     {
568       fprintf_vma (file, val);
569       return;
570     }
571 #endif
572   abort ();
573 }
574
575 void
576 sprint_value (buf, val)
577      char *buf;
578      valueT val;
579 {
580   if (sizeof (val) <= sizeof (long))
581     {
582       sprintf (buf, "%ld", (long) val);
583       return;
584     }
585 #ifdef BFD_ASSEMBLER
586   if (sizeof (val) <= sizeof (bfd_vma))
587     {
588       sprintf_vma (buf, val);
589       return;
590     }
591 #endif
592   abort ();
593 }
594
595 /* end of messages.c */