ctf-error.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* Error table.
  2. Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. This file is part of libctf.
  4. libctf is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <ctf-impl.h>
  16. #include <stddef.h>
  17. #include <string.h>
  18. /* This construct is due to Bruno Haible: much thanks. */
  19. /* Give each structure member a unique name. The name does not matter, so we
  20. use the enum constant to uniquify them. */
  21. #define ERRSTRFIELD(N) ctf_errstr##N
  22. /* In this file, we want to treat the first item of the ctf error
  23. macro like subsequent items. */
  24. #define _CTF_FIRST(NAME, VALUE) _CTF_ITEM(NAME, VALUE)
  25. /* The error message strings, each in a unique structure member precisely big
  26. enough for that error, plus a str member to access them all as a string
  27. table. */
  28. static const union _ctf_errlist_t
  29. {
  30. __extension__ struct
  31. {
  32. #define _CTF_ITEM(n, s) char ERRSTRFIELD (n) [sizeof (s)];
  33. _CTF_ERRORS
  34. #undef _CTF_ITEM
  35. };
  36. char str[1];
  37. } _ctf_errlist =
  38. {
  39. {
  40. #define _CTF_ITEM(n, s) N_(s),
  41. _CTF_ERRORS
  42. #undef _CTF_ITEM
  43. }
  44. };
  45. /* Offsets to each member in the string table, computed using offsetof. */
  46. static const unsigned int _ctf_erridx[] =
  47. {
  48. #define _CTF_ITEM(n, s) [n - ECTF_BASE] = offsetof (union _ctf_errlist_t, ERRSTRFIELD (n)),
  49. _CTF_ERRORS
  50. #undef _CTF_ITEM
  51. };
  52. const char *
  53. ctf_errmsg (int error)
  54. {
  55. const char *str;
  56. if (error >= ECTF_BASE && (error - ECTF_BASE) < ECTF_NERR)
  57. str = _ctf_errlist.str + _ctf_erridx[error - ECTF_BASE];
  58. else
  59. str = (const char *) strerror (error);
  60. return (str ? gettext (str) : _("Unknown error"));
  61. }
  62. int
  63. ctf_errno (ctf_dict_t * fp)
  64. {
  65. return fp->ctf_errno;
  66. }