aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_interface/src/legacy/erl_error.c
blob: d3309d28636acb5319d80ca345ce372d668dbadc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 1996-2009. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * %CopyrightEnd%
 */
/*
 * Function: Some nice error routines taken from:
 *    "Advanced Programming in the UNIX Environment",
 *    by W.Richard Stevens
 *   
 *   void erl_err_sys(const char *fmt, ... ) fatal, sys-error 
 *   void erl_err_ret(const char *fmt, ... ) non-fatal, sys-error
 *   void erl_err_quit(const char *fmt, ...) fatal, non-sys-error
 *   void erl_err_msg(const char *fmt, ... ) non-fatal, non-sys-error
 */

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#ifdef VRTX			/* What's VRIX? [sverkerw] */
#define __READY_EXTENSIONS__
#endif
#include <errno.h>

#if defined(VXWORKS)
#include <taskLib.h>
#include <taskVarLib.h>
#endif

#include "eidef.h"
#include "erl_interface.h"
#include "erl_error.h"

/* Forward */
static void err_doit(int, const char*, va_list); 
/*    __attribute__ ((format (printf, 2, 0)))*/

/*
 * Some thoughts on flushing stdout/stderr:
 *
 * The defaults are reasonable (linebuffered stdout, unbuffered
 * stderr). If they are in effect (the user neither knows nor cares),
 * there's no need to flush.
 *
 * If the user changes these defaults (and knows what he's doing, so
 * he knows and cares) we shouldn't surprise him by
 * second-guessing. So there's a need to not flush.
 *
 * If the user doesn't know what he's doing, he's hosed anyway.
 */

/* Fatal error related to a system call.
 * Print a message and terminate.
 */
void erl_err_sys(const char *fmt, ... )
{
  va_list ap;

  va_start(ap, fmt);
  err_doit(1, fmt, ap);
  va_end(ap);
  exit(1);
} /* erl_err_sys */

/* Nonfatal error related to a system call.
 * Print a message and return
 */
void erl_err_ret(const char *fmt, ... )
{
  va_list ap;

  va_start(ap, fmt);
  err_doit(1, fmt, ap);
  va_end(ap);
  return;
} /* erl_err_ret */

/* Nonfatal error unrelated to a system call.
 * Print a message and return
 */
void erl_err_msg(const char *fmt, ... )
{
  va_list ap;

  va_start(ap, fmt);
  err_doit(0, fmt, ap);
  va_end(ap);
  return;
} /* erl_err_msg */

/* Fatal error unrelated to a system call.
 * Print a message and terminate
 */
void erl_err_quit(const char *fmt, ... )
{
  va_list ap;

  va_start(ap, fmt);
  err_doit(0, fmt, ap);
  va_end(ap);
  exit(1);
} /* erl_err_quit */



/* 
 * For example on SunOS we don't have the ANSI C strerror.
 * 
 * maybe move to a convenince lib     [sverkerw]
 */
#ifndef HAVE_STRERROR

/* FIXME: move to configure */
/* CONFIG: probe for sys_nerr/_sys_nerr */
extern int sys_nerr;

/* CONFIG: probe for sys_errlist/_sys_errlist and maybe for const-ness */
#ifdef FREEBSD
extern const char * const sys_errlist[];
#else
extern char * sys_errlist[];
#endif

/* Should be in string.h */
/* Is supposed to return 'char *' (no const-ness in ANSI's prototype),
   but if you rewrite the returned string in place you deserve to
   lose. */
static const char *strerror(int errnum)
{
    if (errnum >= 0 && errnum < sys_nerr) {
	return sys_errlist[errnum];
    } else {
	/* Enough buffer for 64 bits of error. It should last a while. */
        /* FIXME problem for threaded ? */
	static char b[] = "(error -9223372036854775808)";
	sprintf(b, "(error %d)", errnum);
	buf[sizeof(b)-1] = '\0';
	return b;
    }
}
#endif /* !HAVE_STRERROR */


/* Print a message and return to caller.
 * Caller specifies "errnoflag".
 */
static void err_doit(int errnoflag, const char *fmt, va_list ap)
{
#ifndef NO_ERR_MSG
  int errno_save;

  errno_save = errno;

  vfprintf(stderr, fmt, ap);
  if (errnoflag)
  {
      fputs(": ", stderr);
      fputs(strerror(errno_save), stderr);
  }
  fputs("\n", stderr);
#endif

  return;
} /* err_doit */