xref: /titanic_50/usr/src/lib/libast/common/include/error.h (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
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 /*
88  * errorpush()/errorpop() are obsolete -- use errorctx() instead
89  */
90 
91 #ifndef ERROR_CONTEXT_T
92 #define ERROR_CONTEXT_T		Error_info_t
93 #endif
94 
95 #define ERROR_CONTEXT_BASE	((Error_context_t*)&error_info.context)
96 
97 #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))
98 #define errorpop(p)	(*ERROR_CONTEXT_BASE=*(p))
99 
100 typedef struct Error_info_s Error_info_t;
101 typedef struct Error_context_s Error_context_t;
102 
103 #define ERROR_CONTEXT \
104 	ERROR_CONTEXT_T* context;	/* prev context stack element	*/ \
105 	int	errors;			/* >= ERROR_ERROR count		*/ \
106 	int	flags;			/* context flags		*/ \
107 	int	line;			/* input|output line number	*/ \
108 	int	warnings;		/* ERROR_WARNING count		*/ \
109 	char*	file;			/* input|output file name	*/ \
110 	char*	id;			/* command id			*/
111 
112 struct Error_context_s			/* context stack element	*/
113 {
114 	ERROR_CONTEXT
115 };
116 
117 struct Error_info_s			/* error state			*/
118 {
119 	int	fd;			/* write(2) fd			*/
120 
121 	void	(*exit)(int);		/* error exit			*/
122 	ssize_t	(*write)(int, const void*, size_t); /* error output	*/
123 
124 	/* the rest are implicitly initialized				*/
125 
126 	int	clear;			/* default clear ERROR_* flags	*/
127 	int	core;			/* level>=core -> core dump	*/
128 	int	indent;			/* debug trace indent level	*/
129 	int	init;			/* initialized			*/
130 	int	last_errno;		/* last reported errno		*/
131 	int	mask;			/* multi level debug trace mask	*/
132 	int	set;			/* default set ERROR_* flags	*/
133 	int	trace;			/* debug trace level		*/
134 
135 	char*	version;		/* ERROR_SOURCE command version	*/
136 
137 	int	(*auxilliary)(Sfio_t*, int, int); /* aux info to append	*/
138 
139 	ERROR_CONTEXT			/* top of context stack		*/
140 
141 	Error_context_t	empty;		/* empty context stack element	*/
142 
143 	unsigned long	time;		/* debug time trace		*/
144 
145 	char*	(*translate)(const char*, const char*, const char*, const char*);	/* format translator */
146 
147 	const char*	catalog;	/* message catalog		*/
148 };
149 
150 #ifndef errno
151 extern int	errno;			/* system call error status	*/
152 #endif
153 
154 #if _BLD_ast && defined(__EXPORT__)
155 #define extern		extern __EXPORT__
156 #endif
157 #if !_BLD_ast && defined(__IMPORT__)
158 #define extern		extern __IMPORT__
159 #endif
160 
161 extern Error_info_t*	_error_infop_;
162 
163 #define error_info	(*_error_infop_)
164 
165 #undef	extern
166 
167 #if _BLD_ast && defined(__EXPORT__)
168 #define extern		__EXPORT__
169 #endif
170 
171 extern void		error(int, ...);
172 extern int		errormsg(const char*, int, ...);
173 extern int		errorf(void*, void*, int, ...);
174 extern void		errorv(const char*, int, va_list);
175 #ifndef errorx
176 extern char*		errorx(const char*, const char*, const char*, const char*);
177 #endif
178 extern Error_info_t*	errorctx(Error_info_t*, int, int);
179 
180 #undef	extern
181 
182 #endif
183