PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/reference/bash-1.14.7/error.c

http://github.com/cardmagic/lucash
C | 244 lines | 179 code | 43 blank | 22 comment | 11 complexity | fd28157bddd4df2a0afdfd36e0a68051 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /* error.c -- Functions for handling errors. */
  2. /* Copyright (C) 1993 Free Software Foundation, Inc.
  3. This file is part of GNU Bash, the Bourne Again SHell.
  4. Bash 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 2, or (at your option) any later
  7. version.
  8. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Bash; see the file COPYING. If not, write to the Free Software
  14. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include <stdio.h>
  16. #include <sys/types.h>
  17. #include <fcntl.h>
  18. #if defined (HAVE_VFPRINTF)
  19. #include <varargs.h>
  20. #endif
  21. #include <errno.h>
  22. #if !defined (errno)
  23. extern int errno;
  24. #endif /* !errno */
  25. #include "bashansi.h"
  26. #include "flags.h"
  27. #include "error.h"
  28. #include "command.h"
  29. #include "general.h"
  30. extern int interactive_shell;
  31. extern char *dollar_vars[];
  32. extern char *shell_name;
  33. extern char *the_current_maintainer;
  34. #if defined (JOB_CONTROL)
  35. extern pid_t shell_pgrp;
  36. #endif /* JOB_CONTROL */
  37. /* Return the name of the shell or the shell script for error reporting. */
  38. char *
  39. get_name_for_error ()
  40. {
  41. char *name = (char *) NULL;
  42. if (!interactive_shell)
  43. name = dollar_vars[0];
  44. if (!name && shell_name && *shell_name)
  45. name = base_pathname (shell_name);
  46. if (!name)
  47. name = "bash";
  48. return (name);
  49. }
  50. /* Report an error having to do with FILENAME. */
  51. void
  52. file_error (filename)
  53. char *filename;
  54. {
  55. report_error ("%s: %s", filename, strerror (errno));
  56. }
  57. #if !defined (HAVE_VFPRINTF)
  58. void
  59. programming_error (reason, arg1, arg2, arg3, arg4, arg5)
  60. char *reason;
  61. {
  62. #if defined (JOB_CONTROL)
  63. give_terminal_to (shell_pgrp);
  64. #endif /* JOB_CONTROL */
  65. report_error (reason, arg1, arg2);
  66. fprintf (stderr, "Report this to %s\n", the_current_maintainer);
  67. fprintf (stderr, "Stopping myself...");
  68. fflush (stderr);
  69. abort ();
  70. }
  71. void
  72. report_error (format, arg1, arg2, arg3, arg4, arg5)
  73. char *format;
  74. {
  75. fprintf (stderr, "%s: ", get_name_for_error ());
  76. fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  77. fprintf (stderr, "\n");
  78. if (exit_immediately_on_error)
  79. exit (1);
  80. }
  81. void
  82. fatal_error (format, arg1, arg2, arg3, arg4, arg5)
  83. char *format;
  84. {
  85. fprintf (stderr, "%s: ", get_name_for_error ());
  86. fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  87. fprintf (stderr, "\n");
  88. exit (2);
  89. }
  90. void
  91. internal_error (format, arg1, arg2, arg3, arg4, arg5)
  92. char *format;
  93. {
  94. fprintf (stderr, "%s: ", get_name_for_error ());
  95. fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  96. fprintf (stderr, "\n");
  97. }
  98. #else /* We have VARARGS support, so use it. */
  99. void
  100. programming_error (va_alist)
  101. va_dcl
  102. {
  103. va_list args;
  104. char *format;
  105. #if defined (JOB_CONTROL)
  106. give_terminal_to (shell_pgrp);
  107. #endif /* JOB_CONTROL */
  108. va_start (args);
  109. format = va_arg (args, char *);
  110. vfprintf (stderr, format, args);
  111. fprintf (stderr, "\n");
  112. va_end (args);
  113. fprintf (stderr, "Tell %s to fix this someday.\n", the_current_maintainer);
  114. fprintf (stderr, "Stopping myself...");
  115. fflush (stderr);
  116. abort ();
  117. }
  118. void
  119. report_error (va_alist)
  120. va_dcl
  121. {
  122. va_list args;
  123. char *format;
  124. fprintf (stderr, "%s: ", get_name_for_error ());
  125. va_start (args);
  126. format = va_arg (args, char *);
  127. vfprintf (stderr, format, args);
  128. fprintf (stderr, "\n");
  129. va_end (args);
  130. if (exit_immediately_on_error)
  131. exit (1);
  132. }
  133. void
  134. fatal_error (va_alist)
  135. va_dcl
  136. {
  137. va_list args;
  138. char *format;
  139. fprintf (stderr, "%s: ", get_name_for_error ());
  140. va_start (args);
  141. format = va_arg (args, char *);
  142. vfprintf (stderr, format, args);
  143. fprintf (stderr, "\n");
  144. va_end (args);
  145. exit (2);
  146. }
  147. void
  148. internal_error (va_alist)
  149. va_dcl
  150. {
  151. va_list args;
  152. char *format;
  153. fprintf (stderr, "%s: ", get_name_for_error ());
  154. va_start (args);
  155. format = va_arg (args, char *);
  156. vfprintf (stderr, format, args);
  157. fprintf (stderr, "\n");
  158. va_end (args);
  159. }
  160. itrace (va_alist)
  161. va_dcl
  162. {
  163. va_list args;
  164. char *format;
  165. fprintf(stderr, "TRACE: pid %d: ", getpid());
  166. va_start (args);
  167. format = va_arg (args, char *);
  168. vfprintf (stderr, format, args);
  169. fprintf (stderr, "\n");
  170. va_end (args);
  171. fflush(stderr);
  172. }
  173. #if 0
  174. /* A trace function for silent debugging -- doesn't require a control
  175. terminal. */
  176. trace (va_alist)
  177. va_dcl
  178. {
  179. va_list args;
  180. char *format;
  181. static FILE *tracefp = (FILE *)NULL;
  182. if (tracefp == NULL)
  183. tracefp = fopen("/usr/tmp/bash-trace.log", "a+");
  184. if (tracefp == NULL)
  185. tracefp = stderr;
  186. else
  187. fcntl (fileno (tracefp), F_SETFD, 1); /* close-on-exec */
  188. fprintf(tracefp, "TRACE: pid %d: ", getpid());
  189. va_start (args);
  190. format = va_arg (args, char *);
  191. vfprintf (tracefp, format, args);
  192. fprintf (tracefp, "\n");
  193. va_end (args);
  194. fflush(tracefp);
  195. }
  196. #endif
  197. #endif /* HAVE_VFPRINTF */