home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / ver_cont / cvs-1.8 / cvs-1 / cvs-1.8.1 / src / error.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-06  |  6.1 KB  |  257 lines

  1. /* error.c -- error handler for noninteractive utilities
  2.    Copyright (C) 1990-1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* David MacKenzie */
  19. /* Brian Berliner added support for CVS */
  20.  
  21. #include "cvs.h"
  22.  
  23. #include <stdio.h>
  24.  
  25. /* If non-zero, error will use the CVS protocol to stdout to report error
  26.    messages.  This will only be set in the CVS server parent process;
  27.    most other code is run via do_cvs_command, which forks off a child
  28.    process and packages up its stderr in the protocol.  */
  29. int error_use_protocol; 
  30.  
  31. #ifdef HAVE_VPRINTF
  32.  
  33. #if __STDC__
  34. #include <stdarg.h>
  35. #define VA_START(args, lastarg) va_start(args, lastarg)
  36. #else /* ! __STDC__ */
  37. #include <varargs.h>
  38. #define VA_START(args, lastarg) va_start(args)
  39. #endif /* __STDC__ */
  40.  
  41. #else /* ! HAVE_VPRINTF */ 
  42.  
  43. #ifdef HAVE_DOPRNT
  44. #define va_alist args
  45. #define va_dcl int args;
  46. #else /* ! HAVE_DOPRNT */
  47. #define va_alist a1, a2, a3, a4, a5, a6, a7, a8
  48. #define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
  49. #endif /* HAVE_DOPRNT */
  50.  
  51. #endif /* HAVE_VPRINTF */ 
  52.  
  53. #if STDC_HEADERS
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #else /* ! STDC_HEADERS */
  57. #if __STDC__
  58. void exit(int status);
  59. #else /* ! __STDC__ */
  60. void exit ();
  61. #endif /* __STDC__ */
  62. #endif /* STDC_HEADERS */
  63.  
  64. extern char *strerror ();
  65.  
  66. extern int vasprintf ();
  67.  
  68. typedef void (*fn_returning_void) PROTO((void));
  69.  
  70. /* Function to call before exiting.  */
  71. static fn_returning_void cleanup_fn;
  72.  
  73. fn_returning_void
  74. error_set_cleanup (arg)
  75.      fn_returning_void arg;
  76. {
  77.     fn_returning_void retval = cleanup_fn;
  78.     cleanup_fn = arg;
  79.     return retval;
  80. }
  81.  
  82. /* Print the program name and error message MESSAGE, which is a printf-style
  83.    format string with optional args.
  84.    If ERRNUM is nonzero, print its corresponding system error message.
  85.    Exit with status EXIT_FAILURE if STATUS is nonzero.  */
  86. /* VARARGS */
  87. void
  88. #if defined (HAVE_VPRINTF) && __STDC__
  89. error (int status, int errnum, const char *message, ...)
  90. #else
  91. error (status, errnum, message, va_alist)
  92.     int status;
  93.     int errnum;
  94.     const char *message;
  95.     va_dcl
  96. #endif
  97. {
  98.     FILE *out = stderr;
  99. #ifdef HAVE_VPRINTF
  100.     va_list args;
  101. #endif
  102.  
  103.     if (error_use_protocol)
  104.     {
  105.     out = stdout;
  106.     printf ("E ");
  107.     }
  108.  
  109. #ifdef HAVE_VPRINTF
  110.     {
  111.     char *mess = NULL;
  112.     char *entire;
  113.     size_t len;
  114.  
  115.     VA_START (args, message);
  116.     vasprintf (&mess, message, args);
  117.     va_end (args);
  118.  
  119.     if (mess == NULL)
  120.     {
  121.         entire = NULL;
  122.         status = 1;
  123.     }
  124.     else
  125.     {
  126.         len = strlen (mess) + strlen (program_name) + 80;
  127.         if (command_name != NULL)
  128.         len += strlen (command_name);
  129.         if (errnum != 0)
  130.         len += strlen (strerror (errnum));
  131.         entire = malloc (len);
  132.         if (entire == NULL)
  133.         {
  134.         free (mess);
  135.         status = 1;
  136.         }
  137.         else
  138.         {
  139.         strcpy (entire, program_name);
  140.         if (command_name != NULL && command_name[0] != '\0')
  141.         {
  142.             strcat (entire, " ");
  143.             if (status != 0)
  144.             strcat (entire, "[");
  145.             strcat (entire, command_name);
  146.             if (status != 0)
  147.             strcat (entire, " aborted]");
  148.         }
  149.         strcat (entire, ": ");
  150.         strcat (entire, mess);
  151.         if (errnum != 0)
  152.         {
  153.             strcat (entire, ": ");
  154.             strcat (entire, strerror (errnum));
  155.         }
  156.         strcat (entire, "\n");
  157.         free (mess);
  158.         }
  159.     }
  160.     if (error_use_protocol)
  161.         fputs (entire ? entire : "out of memory", out);
  162.     else
  163.         cvs_outerr (entire ? entire : "out of memory", 0);
  164.     if (entire != NULL)
  165.         free (entire);
  166.     }
  167.  
  168. #else /* No HAVE_VPRINTF */
  169.     /* I think that all relevant systems have vprintf these days.  But
  170.        just in case, I'm leaving this code here.  */
  171.  
  172.     if (command_name && *command_name)
  173.     {
  174.     if (status)
  175.         fprintf (out, "%s [%s aborted]: ", program_name, command_name);
  176.     else
  177.         fprintf (out, "%s %s: ", program_name, command_name);
  178.     }
  179.     else
  180.     fprintf (out, "%s: ", program_name);
  181.  
  182. #ifdef HAVE_VPRINTF
  183.     VA_START (args, message);
  184.     vfprintf (out, message, args);
  185.     va_end (args);
  186. #else
  187. #ifdef HAVE_DOPRNT
  188.     _doprnt (message, &args, out);
  189. #else
  190.     fprintf (out, message, a1, a2, a3, a4, a5, a6, a7, a8);
  191. #endif
  192. #endif
  193.     if (errnum)
  194.     fprintf (out, ": %s", strerror (errnum));
  195.     putc ('\n', out);
  196.  
  197. #endif /* No HAVE_VPRINTF */
  198.  
  199.     /* In the error_use_protocol case, this probably does something useful.
  200.        In most other cases, I suspect it is a noop (either stderr is line
  201.        buffered or we haven't written anything to stderr) or unnecessary
  202.        (if stderr is not line buffered, maybe there is a reason....).  */
  203.     fflush (out);
  204.  
  205.     if (status)
  206.     {
  207.     if (cleanup_fn)
  208.         (*cleanup_fn) ();
  209.     exit (EXIT_FAILURE);
  210.     }
  211. }
  212.  
  213. /* Print the program name and error message MESSAGE, which is a printf-style
  214.    format string with optional args to the file specified by FP.
  215.    If ERRNUM is nonzero, print its corresponding system error message.
  216.    Exit with status EXIT_FAILURE if STATUS is nonzero.  */
  217. /* VARARGS */
  218. void
  219. #if defined (HAVE_VPRINTF) && __STDC__
  220. fperror (FILE *fp, int status, int errnum, char *message, ...)
  221. #else
  222. fperror (fp, status, errnum, message, va_alist)
  223.     FILE *fp;
  224.     int status;
  225.     int errnum;
  226.     char *message;
  227.     va_dcl
  228. #endif
  229. {
  230. #ifdef HAVE_VPRINTF
  231.     va_list args;
  232. #endif
  233.  
  234.     fprintf (fp, "%s: ", program_name);
  235. #ifdef HAVE_VPRINTF
  236.     VA_START (args, message);
  237.     vfprintf (fp, message, args);
  238.     va_end (args);
  239. #else
  240. #ifdef HAVE_DOPRNT
  241.     _doprnt (message, &args, fp);
  242. #else
  243.     fprintf (fp, message, a1, a2, a3, a4, a5, a6, a7, a8);
  244. #endif
  245. #endif
  246.     if (errnum)
  247.     fprintf (fp, ": %s", strerror (errnum));
  248.     putc ('\n', fp);
  249.     fflush (fp);
  250.     if (status)
  251.     {
  252.     if (cleanup_fn)
  253.         (*cleanup_fn) ();
  254.     exit (EXIT_FAILURE);
  255.     }
  256. }
  257.