xref: /netbsd-src/external/gpl3/binutils/dist/libsframe/sframe-error.c (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1*cb63e24eSchristos /* sframe-error.c - Error messages.
2*cb63e24eSchristos 
3*cb63e24eSchristos    Copyright (C) 2022-2024 Free Software Foundation, Inc.
4*cb63e24eSchristos 
5*cb63e24eSchristos    This file is part of libsframe.
6*cb63e24eSchristos 
7*cb63e24eSchristos    This program is free software; you can redistribute it and/or modify
8*cb63e24eSchristos    it under the terms of the GNU General Public License as published by
9*cb63e24eSchristos    the Free Software Foundation; either version 3 of the License, or
10*cb63e24eSchristos    (at your option) any later version.
11*cb63e24eSchristos 
12*cb63e24eSchristos    This program is distributed in the hope that it will be useful,
13*cb63e24eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*cb63e24eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*cb63e24eSchristos    GNU General Public License for more details.
16*cb63e24eSchristos 
17*cb63e24eSchristos    You should have received a copy of the GNU General Public License
18*cb63e24eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19*cb63e24eSchristos 
20*cb63e24eSchristos #include "sframe-api.h"
21*cb63e24eSchristos #include <stddef.h>
22*cb63e24eSchristos #include <string.h>
23*cb63e24eSchristos 
24*cb63e24eSchristos /* In this file, we want to treat the first item of the SFrame error
25*cb63e24eSchristos    macro like subsequent items.  */
26*cb63e24eSchristos #define _SFRAME_FIRST(NAME, VALUE) _SFRAME_ITEM(NAME, VALUE)
27*cb63e24eSchristos 
28*cb63e24eSchristos /* The error message strings, each in a unique structure member precisely big
29*cb63e24eSchristos    enough for that error, plus a str member to access them all as a string
30*cb63e24eSchristos    table.  */
31*cb63e24eSchristos 
32*cb63e24eSchristos static const char *const _sframe_errlist[] = {
33*cb63e24eSchristos #define _SFRAME_ITEM(n, s) s,
34*cb63e24eSchristos _SFRAME_ERRORS
35*cb63e24eSchristos #undef _SFRAME_ITEM
36*cb63e24eSchristos };
37*cb63e24eSchristos 
38*cb63e24eSchristos const char *
sframe_errmsg(int error)39*cb63e24eSchristos sframe_errmsg (int error)
40*cb63e24eSchristos {
41*cb63e24eSchristos   const char *str;
42*cb63e24eSchristos 
43*cb63e24eSchristos   if (error >= SFRAME_ERR_BASE && (error - SFRAME_ERR_BASE) < SFRAME_ERR_NERR)
44*cb63e24eSchristos     str = _sframe_errlist[error - SFRAME_ERR_BASE];
45*cb63e24eSchristos   else
46*cb63e24eSchristos     str = (const char *) strerror (error);
47*cb63e24eSchristos 
48*cb63e24eSchristos   return (str ? str : "Unknown error");
49*cb63e24eSchristos }
50