File: error.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (275 lines) | stat: -rw-r--r-- 3,756 bytes parent folder | download | duplicates (2)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <string.h>
#include <stdlib.h>
#include <grass/dbmi.h>

#include <errno.h>

static int err_flag = 0;
static int err_code = DB_OK;
static char *err_msg = 0;
static int auto_print_errors = 1;
static int auto_print_protocol_errors = 1;
static void (*user_print_function) (const char *);

static char *who = NULL;

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_on_error(void (*f) (const char *))
{
    user_print_function = f;
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_set_error_who(const char *me)
{
    if (who)
	db_free(who);
    who = db_store(me);
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
const char *db_get_error_who(void)
{
    return who ? who : "";
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_error(const char *s)
{
    if (s == NULL)
	s = "<NULL error message>";
    if (err_msg)
	db_free(err_msg);
    err_msg = db_store(s);
    err_flag = 1;
    if (auto_print_errors)
	db_print_error();
    err_code = DB_FAILED;
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_protocol_error(void)
{
    int flag;

    flag = auto_print_errors;
    auto_print_errors = auto_print_protocol_errors;
    db_error("dbmi: Protocol error");
    auto_print_errors = flag;
    err_code = DB_PROTOCOL_ERR;
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_syserror(const char *s)
{
    char lead[1024];
    char msg[1024];


    err_flag = 0;
    if (errno <= 0)
	return;

    *lead = 0;
    if (who)
	sprintf(lead, "%s: ", who);

    if (errno > 0)
	sprintf(msg, "%s%s: %s", lead, strerror(errno), s);

    db_error(msg);
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
int db_get_error_code(void)
{
    return err_flag ? err_code : DB_OK;
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_memory_error(void)
{
    db_error("dbmi: Out of Memory");
    err_code = DB_MEMORY_ERR;
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_procedure_not_implemented(const char *name)
{
    char msg[128];

    sprintf(msg, "dbmi: %s() not implemented", name);
    db_error(msg);
    err_code = DB_NOPROC;
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_noproc_error(procnum)
{
    char msg[128];

    sprintf(msg, "dbmi: Invalid procedure %d", procnum);
    db_error(msg);
    err_code = DB_NOPROC;
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_clear_error(void)
{
    err_flag = 0;
    err_code = DB_OK;
    errno = 0;			/* clearn system errno as well */
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_print_error(void)
{
    char lead[1024];

    if (!err_flag)
	return;

    *lead = 0;
    if (who)
	sprintf(lead, "%s: ", who);

    if (user_print_function) {
	char buf[1024];

	sprintf(buf, "%s%s\n", lead, err_msg);
	user_print_function(buf);
    }
    else
	fprintf(stderr, "%s%s\n", lead, err_msg);
}


static int debug_on = 0;

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_debug_on(void)
{
    debug_on = 1;
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_debug_off(void)
{
    debug_on = 0;
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_debug(const char *s)
{
    if (debug_on)
	fprintf(stderr, "debug(%s): %s\n", who ? who : "", s ? s : "<NULL>");
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
const char *db_get_error_msg(void)
{
    return err_flag ? err_msg : (const char *)NULL;
}

/*!
   \fn void db_auto_print_errors (flag)
   \brief toggles printing of DBMI error messages
   \return void
   \param flag
 */
void db_auto_print_errors(int flag)
{
    auto_print_errors = flag;
    auto_print_protocol_errors = flag;
}

/*!
   \fn 
   \brief 
   \return 
   \param 
 */
void db_auto_print_protocol_errors(int flag)
{
    auto_print_protocol_errors = flag;
}