home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume29 / parseargs / part03 / syserr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-19  |  6.0 KB  |  263 lines

  1. /*************************************************************************
  2. ** ^FILE: syserr.c - error-message printing routines
  3. **
  4. ** ^DESCRIPTION:
  5. **    This fill implements various routines for printing diagnostic
  6. **    messages on standard diagnostic output (stderr). The routines are:
  7. **
  8. **       usrerr()  --  print message and any system message(s) and return
  9. **       syserr()  --  print message and any system message(s) and exit
  10. **       eprintf() --  print to stderr and return
  11. **
  12. ** ^HISTORY:
  13. **    27/08/91     Earl Chew     <cechew@bruce.cs.monash.edu.au>
  14. **    - Use ProgNameLen when accessing ProgName
  15. **
  16. **    01/02/91    Brad Appleton    <brad@ssd.csd.harris.com>
  17. **       - Changed to use varargs/stdargs
  18. **       - Added structured comment blocks
  19. **       - Added eprintf()
  20. **
  21. **    --/--/--    Peter da Silva    <peter@ferranti.com>
  22. **
  23. **    --/--/--    Eric P. Allman    <eric@Berkeley.EDU>     Created
  24. ***^^**********************************************************************/
  25.  
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <useful.h>
  29. /* #include <funclist.h> */
  30.  
  31. #ifdef vms
  32. # include <ssdef.h>
  33. # define  e_FATAL  SS$_CANCEL
  34. #else
  35. # define  e_FATAL  127
  36. #endif
  37.  
  38. VERSIONID("$Header: syserr.c,v 2.0 89/12/24 00:56:31 eric Exp $");
  39.  
  40. extern  char *ProgName;
  41. extern  int ProgNameLen;
  42. EXTERN  int   vfprintf  ARGS((FILE *, const char *, va_list));
  43.  
  44.  
  45. /***************************************************************************
  46. ** ^FUNCTION: _error_message - generic message printing routine.
  47. **
  48. ** ^SYNOPSIS:
  49. */
  50. #ifndef __ANSI_C__
  51.    static VOID _error_message( format, ap )
  52. /*
  53. ** ^PARAMETERS:
  54. */
  55.    char *format;
  56. /*    -- the formatted message-string to print.
  57. */
  58.    va_list ap;
  59. /*    -- the list of variable arguments for vfprintf().
  60. */
  61. #endif  /* !__ANSI_C__ */
  62.  
  63. /* ^DESCRIPTION:
  64. **    _error_message will print the program name followed by the
  65. **    formatted message. If errno is non-zero, the corresponding
  66. **    system message will also be printed.
  67. **
  68. ** ^REQUIREMENTS:
  69. **    None.
  70. **
  71. ** ^SIDE-EFFECTS:
  72. **    Writes to stderr.
  73. **
  74. ** ^RETURN-VALUE:
  75. **    None.
  76. **
  77. ** ^ALGORITHM:
  78. **    - print the program name
  79. **    - print the message
  80. **    - if errno is non-zero, call perror()
  81. ***^^**********************************************************************/
  82. #ifdef __ANSI_C__
  83.    static void _error_message( const char *format, va_list ap )
  84. #endif
  85. {
  86.    int save_err;
  87.  
  88.    save_err = errno;
  89.    if (ProgName  &&  *ProgName)
  90.       fprintf(stderr, "%.*s: ", ProgNameLen, ProgName);
  91.  
  92.    (VOID) vfprintf(stderr, format, ap);
  93.  
  94.    fputc('\n', stderr);
  95.    if ( save_err ) {
  96.       errno = save_err;
  97.       perror("System error");
  98.    }
  99.    fflush(stderr);
  100. }
  101.  
  102.  
  103. /***************************************************************************
  104. ** ^FUNCTION: syserr - print a formatted message and exit
  105. **
  106. ** ^SYNOPSIS:
  107. */
  108. #ifndef __ANSI_C__
  109.    VOID syserr( format, va_alist )
  110. /*
  111. ** ^PARAMETERS:
  112. */
  113.    char *format;
  114. /*    -- the format string to pass to vfprintf()
  115. */
  116.    va_dcl  
  117. /*    -- the arguments to be formatted
  118. */
  119. #endif  /* !__ANSI_C__ */
  120.  
  121. /* ^DESCRIPTION:
  122. **    Syserr will print the current program name followed by the
  123. **    formatted message. If errno is non-zero, it will use perror
  124. **    to print the corresponding system error message. Lastly, Syserr
  125. **    will terminate execution with an exit code of 1.
  126. **
  127. ** ^REQUIREMENTS:
  128. **    No special requirements.
  129. **
  130. ** ^SIDE-EFFECTS:
  131. **    All output is written to stderr. Program execution is terminated.
  132. **
  133. ** ^RETURN-VALUE:
  134. **    None (Does not return).
  135. **
  136. ** ^ALGORITHM:
  137. **    - print the error message(s)
  138. **    - take care of recursive calls to syserr()
  139. **    - exit
  140. ***^^**********************************************************************/
  141. #ifdef __ANSI_C__
  142.    void syserr( const char *format, ... )
  143. #endif
  144. {
  145.    static BOOL exiting = FALSE;
  146.    va_list ap;
  147.  
  148.    /* print the error message */
  149.    VA_START(ap, format);
  150.    _error_message(format, ap);
  151.    VA_END(ap);
  152.  
  153.    /* if we recursively syserr during exit, drop out now! */
  154.    if (exiting)  exit(e_FATAL);
  155.  
  156.    /* try a clean exit */
  157.    exiting = TRUE;
  158.    exit(e_FATAL);
  159.    /*NOTREACHED*/
  160. }
  161.  
  162.  
  163. /***************************************************************************
  164. ** ^FUNCTION: eprintf - print a formatted message on stderr.
  165. **
  166. ** ^SYNOPSIS:
  167. */
  168. #ifndef __ANSI_C__
  169.    int eprintf( format, va_alist )
  170. /*
  171. ** ^PARAMETERS:
  172. */
  173.    char *format;
  174. /*    -- the printf() message to print.
  175. */
  176.    va_dcl
  177. /*    -- the arguments to be formatted
  178. */
  179. #endif  /* !__ANSI_C__ */
  180.  
  181. /* ^DESCRIPTION:
  182. **    Eprintf() will behaves exactly like printf with the sole
  183. **    exception being that it writes to stderr instead of stdout.
  184. **
  185. ** ^REQUIREMENTS:
  186. **    None.
  187. **
  188. ** ^SIDE-EFFECTS:
  189. **    Writes to stderr.
  190. **
  191. ** ^RETURN-VALUE:
  192. **    Same as printf(3).
  193. **
  194. ** ^ALGORITHM:
  195. **    Trivial.
  196. ***^^**********************************************************************/
  197. #ifdef __ANSI_C__
  198.    int eprintf( const char *format, ... )
  199. #endif
  200. {
  201.    int rc;
  202.    va_list ap;
  203.  
  204.    VA_START(ap, format);
  205.    rc = vfprintf( stderr, format, ap );
  206.    VA_END(ap);
  207.  
  208.    fflush(stderr);
  209.    return rc;
  210. }
  211.  
  212.  
  213. /***************************************************************************
  214. ** ^FUNCTION: usrerr - print a user error message
  215. **
  216. ** ^SYNOPSIS:
  217. */
  218. #ifndef __ANSI_C__
  219.    VOID usrerr( format, va_alist )
  220. /*
  221. ** ^PARAMETERS:
  222. */
  223.    char *format;
  224. /*    -- the format string to pass to vfprintf()
  225. */
  226.    va_dcl
  227. /*    -- the arguments to be formatted
  228. */
  229. #endif  /* !__ANSI_C__ */
  230.  
  231. /* ^DESCRIPTION:
  232. **    Usrerr will print the current program name followed by the
  233. **    formatted message. If errno is non-zero, it will use perror
  234. **    to print the corresponding system error message.
  235. **
  236. ** ^REQUIREMENTS:
  237. **    No special requirements.
  238. **
  239. ** ^SIDE-EFFECTS:
  240. **    All output is written to stderr.  Errno is cleared.
  241. **
  242. ** ^RETURN-VALUE:
  243. **    None.
  244. **
  245. ** ^ALGORITHM:
  246. **    - print the error message(s)
  247. **    - set errno to zero
  248. ***^^**********************************************************************/
  249. #ifdef __ANSI_C__
  250.    void usrerr( const char *format, ... )
  251. #endif
  252. {
  253.    va_list ap;
  254.  
  255.    /* print the error message */
  256.    VA_START(ap, format);
  257.    _error_message(format, ap);
  258.    VA_END(ap);
  259.  
  260.    /* give us a clean slate */
  261.    errno = 0;
  262. }
  263.