xref: /illumos-gate/usr/src/contrib/ast/src/lib/libast/include/error.h (revision b30d193948be5a7794d7ae3ba0ed9c2f72c88e0f)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2012 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * Glenn Fowler
25  * AT&T Research
26  *
27  * option, error and message formatter external definitions
28  */
29 
30 #ifndef _ERROR_H
31 #define _ERROR_H
32 
33 #include <ast.h>
34 #include <option.h>
35 #include <errno.h>
36 
37 #define ERROR_VERSION	20070319L
38 
39 #if !defined(errno) && defined(__DYNAMIC__)
40 #define errno		__DYNAMIC__(errno)
41 #endif
42 
43 #define ERROR_debug(n)	(-(n))
44 #define ERROR_exit(n)	((n)+ERROR_ERROR)
45 #define ERROR_system(n)	(((n)+ERROR_ERROR)|ERROR_SYSTEM)
46 #define ERROR_usage(n)	((((n)?2:0)+ERROR_ERROR)|ERROR_USAGE)
47 #define ERROR_warn(n)	(ERROR_WARNING)
48 
49 #ifndef ERROR_catalog
50 #define ERROR_catalog(t)		t
51 #endif
52 #ifndef ERROR_dictionary
53 #define ERROR_dictionary(t)		t
54 #endif
55 
56 #ifndef ERROR_translate
57 #define ERROR_translating()		(error_info.translate&&(ast.locale.set&(1<<AST_LC_MESSAGES)))
58 #define ERROR_translate(l,i,d,m)	(ERROR_translating()?errorx((const char*)(l),(const char*)(i),(const char*)(d),(const char*)(m)):(char*)(m))
59 #endif
60 
61 #define ERROR_INFO	0		/* info message -- no err_id	*/
62 #define ERROR_WARNING	1		/* warning message		*/
63 #define ERROR_ERROR	2		/* error message -- no err_exit	*/
64 #define ERROR_FATAL	3		/* error message with err_exit	*/
65 #define ERROR_NOEXEC	EXIT_NOEXEC	/* shell convention		*/
66 #define ERROR_NOENT	EXIT_NOTFOUND	/* shell convention		*/
67 #define ERROR_PANIC	ERROR_LEVEL	/* panic message with err_exit	*/
68 
69 #define ERROR_LEVEL	0x00ff		/* level portion of status	*/
70 #define ERROR_SYSTEM	0x0100		/* report system errno message	*/
71 #define ERROR_OUTPUT	0x0200		/* next arg is error fd		*/
72 #define ERROR_SOURCE	0x0400		/* next 2 args are FILE,LINE	*/
73 #define ERROR_USAGE	0x0800		/* usage message		*/
74 #define ERROR_PROMPT	0x1000		/* omit trailing newline	*/
75 #define ERROR_NOID	0x2000		/* omit err_id			*/
76 #define ERROR_LIBRARY	0x4000		/* library routine error	*/
77 
78 #define ERROR_INTERACTIVE	0x0001	/* context is interactive	*/
79 #define ERROR_SILENT		0x0002	/* context is silent		*/
80 #define ERROR_NOTIFY		0x0004	/* main(-sig,0,ctx) on signal	*/
81 
82 #define ERROR_FREE		0x0010	/* free context on pop		*/
83 #define ERROR_POP		0x0020	/* pop context			*/
84 #define ERROR_PUSH		0x0040	/* push context			*/
85 #define ERROR_SET		0x0080	/* set context			*/
86 
87 #ifdef ECONNRESET
88 #define ERROR_PIPE(e)		((e)==EPIPE||(e)==ECONNRESET||(e)==EIO)
89 #else
90 #define ERROR_PIPE(e)		((e)==EPIPE||(e)==EIO)
91 #endif
92 
93 /*
94  * errorpush()/errorpop() are obsolete -- use errorctx() instead
95  */
96 
97 #ifndef ERROR_CONTEXT_T
98 #define ERROR_CONTEXT_T		Error_info_t
99 #endif
100 
101 #define ERROR_CONTEXT_BASE	((Error_context_t*)&error_info.context)
102 
103 #define errorpush(p,f)	(*(p)=*ERROR_CONTEXT_BASE,*ERROR_CONTEXT_BASE=error_info.empty,error_info.context=(Error_context_t*)(p),error_info.flags=(f))
104 #define errorpop(p)	(*ERROR_CONTEXT_BASE=*(p))
105 
106 typedef struct Error_info_s Error_info_t;
107 typedef struct Error_context_s Error_context_t;
108 
109 #define ERROR_CONTEXT \
110 	ERROR_CONTEXT_T* context;	/* prev context stack element	*/ \
111 	int	errors;			/* >= ERROR_ERROR count		*/ \
112 	int	flags;			/* context flags		*/ \
113 	int	line;			/* input|output line number	*/ \
114 	int	warnings;		/* ERROR_WARNING count		*/ \
115 	char*	file;			/* input|output file name	*/ \
116 	char*	id;			/* command id			*/
117 
118 struct Error_context_s			/* context stack element	*/
119 {
120 	ERROR_CONTEXT
121 };
122 
123 struct Error_info_s			/* error state			*/
124 {
125 	int	fd;			/* write(2) fd			*/
126 
127 	void	(*exit)(int);		/* error exit			*/
128 	ssize_t	(*write)(int, const void*, size_t); /* error output	*/
129 
130 	/* the rest are implicitly initialized				*/
131 
132 	int	clear;			/* default clear ERROR_* flags	*/
133 	int	core;			/* level>=core -> core dump	*/
134 	int	indent;			/* debug trace indent level	*/
135 	int	init;			/* initialized			*/
136 	int	last_errno;		/* last reported errno		*/
137 	int	mask;			/* multi level debug trace mask	*/
138 	int	set;			/* default set ERROR_* flags	*/
139 	int	trace;			/* debug trace level		*/
140 
141 	char*	version;		/* ERROR_SOURCE command version	*/
142 
143 	int	(*auxilliary)(Sfio_t*, int, int); /* aux info to append	*/
144 
145 	ERROR_CONTEXT			/* top of context stack		*/
146 
147 	Error_context_t	empty;		/* empty context stack element	*/
148 
149 	unsigned long	time;		/* debug time trace		*/
150 
151 	char*	(*translate)(const char*, const char*, const char*, const char*);	/* format translator */
152 
153 	const char*	catalog;	/* message catalog		*/
154 };
155 
156 #ifndef errno
157 extern int	errno;			/* system call error status	*/
158 #endif
159 
160 #if _BLD_ast && defined(__EXPORT__)
161 #define extern		extern __EXPORT__
162 #endif
163 #if !_BLD_ast && defined(__IMPORT__)
164 #define extern		extern __IMPORT__
165 #endif
166 
167 extern Error_info_t*	_error_infop_;
168 
169 #define error_info	(*_error_infop_)
170 
171 #undef	extern
172 
173 #if _BLD_ast && defined(__EXPORT__)
174 #define extern		__EXPORT__
175 #endif
176 
177 extern void		error(int, ...);
178 extern int		errormsg(const char*, int, ...);
179 extern int		errorf(void*, void*, int, ...);
180 extern void		errorv(const char*, int, va_list);
181 #ifndef errorx
182 extern char*		errorx(const char*, const char*, const char*, const char*);
183 #endif
184 extern Error_info_t*	errorctx(Error_info_t*, int, int);
185 
186 #undef	extern
187 
188 #endif
189