1 /* $Id$ */
2 /* Copyright (c) 2007-2015 Pierre Pronchery <khorben@defora.org> */
3 /* This file is part of DeforaOS System libSystem */
4 /* This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, version 3 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
15
16
17
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include "System/error.h"
21
22
23 /* Error */
24 /* private */
25 /* prototypes */
26 static String const * _error_do(int * codeptr, String const * format,
27 va_list args);
28 static int _error_do_code(int * codeptr);
29
30
31 /* public */
32 /* accessors */
33 /* error_get */
34 static String const * _get_do(int * code, ...);
35
error_get(int * code)36 String const * error_get(int * code)
37 {
38 /* XXX workaround for portability */
39 return _get_do(code);
40 }
41
_get_do(int * code,...)42 static String const * _get_do(int * code, ...)
43 {
44 String const * ret;
45 va_list args;
46
47 va_start(args, code);
48 ret = _error_do(code, NULL, args);
49 va_end(args);
50 return ret;
51 }
52
53
54 /* error_get_code */
error_get_code(void)55 int error_get_code(void)
56 {
57 return _error_do_code(NULL);
58 }
59
60
61 /* error_set */
error_set(String const * format,...)62 void error_set(String const * format, ...)
63 {
64 va_list args;
65
66 va_start(args, format);
67 _error_do(NULL, format, args);
68 va_end(args);
69 }
70
71
72 /* error_set_code */
error_set_code(int code,String const * format,...)73 int error_set_code(int code, String const * format, ...)
74 {
75 va_list args;
76
77 va_start(args, format);
78 if(format == NULL)
79 format = "";
80 _error_do(&code, format, args);
81 va_end(args);
82 return code;
83 }
84
85
86 /* error_set_print */
error_set_print(String const * program,int code,String const * format,...)87 int error_set_print(String const * program, int code, String const * format, ...)
88 {
89 va_list args;
90
91 va_start(args, format);
92 _error_do(&code, format, args);
93 va_end(args);
94 return error_print(program);
95 }
96
97
98 /* useful */
99 /* error_print */
100 static int _print_do(String const * program, ...);
101
error_print(String const * program)102 int error_print(String const * program)
103 {
104 /* XXX workaround for portability */
105 return _print_do(program);
106 }
107
_print_do(String const * program,...)108 static int _print_do(String const * program, ...)
109 {
110 int ret = 0;
111 va_list args;
112 String const * error;
113
114 va_start(args, program);
115 if(program != NULL && string_length(program) > 0)
116 {
117 fputs(program, stderr);
118 fputs(": ", stderr);
119 }
120 if((error = _error_do(&ret, NULL, args)) == NULL
121 || string_length(error) == 0)
122 error = "Unknown error";
123 fputs(error, stderr);
124 fputc('\n', stderr);
125 va_end(args);
126 return ret;
127 }
128
129
130 /* private */
131 /* functions */
132 /* error_do */
_error_do(int * codeptr,String const * format,va_list args)133 static String const * _error_do(int * codeptr, String const * format,
134 va_list args)
135 {
136 static String buf[256] = "";
137
138 if(format != NULL) /* setting the error */
139 {
140 vsnprintf(buf, sizeof(buf), format, args);
141 if(codeptr != NULL)
142 _error_do_code(codeptr);
143 }
144 else if(codeptr != NULL)
145 *codeptr = _error_do_code(NULL);
146 return buf;
147 }
148
149
150 /* error_do_code */
_error_do_code(int * codeptr)151 static int _error_do_code(int * codeptr)
152 {
153 static int code = 0;
154
155 if(codeptr != NULL)
156 code = *codeptr;
157 return code;
158 }
159