1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 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  * standalone mini error implementation
25  */
26 
27 #include <ast.h>
28 #include <error.h>
29 
30 Error_info_t	error_info;
31 
32 void
errorv(const char * id,int level,va_list ap)33 errorv(const char* id, int level, va_list ap)
34 {
35 	char*	a;
36 	char*	s;
37 	int	flags;
38 
39 	if (level < 0)
40 		flags = 0;
41 	else
42 	{
43 		flags = level & ~ERROR_LEVEL;
44 		level &= ERROR_LEVEL;
45 	}
46 	a = va_arg(ap, char*);
47 	if (level && ((s = error_info.id) || (s = (char*)id)))
48 	{
49 		if (!(flags & ERROR_USAGE))
50 			sfprintf(sfstderr, "%s: ", s);
51 		else if (strcmp(a, "%s"))
52 			sfprintf(sfstderr, "Usage: %s ", s);
53 	}
54 	if (flags & ERROR_USAGE)
55 		/*nop*/;
56 	else if (level < 0)
57 		sfprintf(sfstderr, "debug%d: ", level);
58 	else if (level)
59 	{
60 		if (level == ERROR_WARNING)
61 		{
62 			sfprintf(sfstderr, "warning: ");
63 			error_info.warnings++;
64 		}
65 		else
66 		{
67 			error_info.errors++;
68 			if (level == ERROR_PANIC)
69 				sfprintf(sfstderr, "panic: ");
70 		}
71 		if (error_info.line)
72 		{
73 			if (error_info.file && *error_info.file)
74 				sfprintf(sfstderr, "\"%s\", ", error_info.file);
75 			sfprintf(sfstderr, "line %d: ", error_info.line);
76 		}
77 	}
78 	sfvprintf(sfstderr, a, ap);
79 	sfprintf(sfstderr, "\n");
80 	if (level >= ERROR_FATAL)
81 		exit(level - ERROR_FATAL + 1);
82 }
83 
84 void
error(int level,...)85 error(int level, ...)
86 {
87 	va_list	ap;
88 
89 	va_start(ap, level);
90 	errorv(NiL, level, ap);
91 	va_end(ap);
92 }
93 
94 int
errorf(void * handle,void * discipline,int level,...)95 errorf(void* handle, void* discipline, int level, ...)
96 {
97 	va_list	ap;
98 
99 	va_start(ap, level);
100 	errorv((discipline && handle) ? *((char**)handle) : (char*)handle, level, ap);
101 	va_end(ap);
102 	return 0;
103 }
104