File: error.c

package info (click to toggle)
librnd 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,812 kB
  • sloc: ansic: 126,990; sh: 2,602; makefile: 2,145; awk: 7
file content (188 lines) | stat: -rw-r--r-- 4,376 bytes parent folder | download
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
/*
    puplug - portable micro plugin framework
    Copyright (C) 2017 Tibor 'Igor2' Palinkas

    libgpmi - General Package/Module Interface
    Copyright (C) 2005-2007 Patric 'TrueLight' Stout & Tibor 'Igor2' Palinkas

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 31 Milk Street, # 960789 Boston, MA 02196 USA.
*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "error.h"
#include "os_dep.h"

const char *pup_error_sources_txt[] = {
	"Unknown",
	"load_dep",
	"find_lib",
	"load_pup",
	"load_lib",
	NULL
};

const char *pup_error_txt[] = {
	NULL,
	"Failed to load library",
	"Failed to load plugin",
	"Version mismatch",
	"Initialization failed",
	"Already loading library",
	"Found cyclic dependency",
	"File not found",
	"Plugin-plugin conflict",
	"Error parsing the pup file"
};

#define array_len(array) (sizeof(array) / sizeof(char *))

const char *pup_strerror(pup_error_t errnum)
{
	/* Check if it is a puplug error */
	if ((errnum > -34000) || (errnum < -34000 - array_len(pup_error_txt)))
		return NULL;

	return pup_error_txt[(-errnum) - 34000];
}

const char *pup_strerror_src(pup_error_source_t srcnum)
{
	/* Check if it is a puplug error */
	if ((srcnum < 0) || (srcnum >= array_len(pup_error_sources_txt)))
		return NULL;

	return pup_error_sources_txt[srcnum];
}

pup_error_t pup_err_stack_push(pup_context_t *pup, pup_error_t error, pup_error_source_t from, const char *details)
{
	pup_err_stack_t *entry;

	/* Don't push anything if the stack is not enabled */
	if (!pup->error_stack_enable)
		return error;

	/* Create the entry */
	entry = malloc(sizeof(pup_err_stack_t));
	entry->error = error;
	entry->from  = from;
	if (details != NULL)
		entry->details = pup_strdup(details);
	else
		entry->details = NULL;

	/* Insert at the beginning of the linked list */
	entry->next     = pup->err_stack;
	pup->err_stack = entry;
	return error;
}

pup_err_stack_t *pup_err_stack_pop(pup_context_t *pup)
{
	pup_err_stack_t *entry;

	/* Get the first record */
	entry = pup->err_stack;

	/* Set next record so we popped the first */
	if (entry != NULL)
		pup->err_stack = entry->next;

	return entry;
}

void pup_err_stack_destroy(pup_context_t *pup, pup_err_stack_t *entry)
{
	pup_err_stack_t *e, *next;

	if (entry == NULL) {
		/* Destroy the whole stack */
		for (e = pup->err_stack; e != NULL; e = next) {
			next = e->next;
			pup_err_stack_destroy(pup, e);
		}
		pup->err_stack = NULL;
	}
	else {
		/* Destroy one entry */
		if (entry->details != NULL)
			free(entry->details);

		free(entry);
	}
}

char *pup_str_err_stack_entry(pup_err_stack_t *entry)
{
	char *tmp, *det;
	const char *from, *msg;

	msg = pup_strerror(entry->error);
	if (msg == NULL)
		msg = "(unknown)";

	from = pup_strerror_src(entry->from);
	if (from == NULL)
		from = "(unknown)";

	if (entry->details != NULL)
		det = entry->details;
	else
		det = "(empty)";

	tmp = malloc(strlen(msg) + strlen(from) + strlen(det) + 32);
	sprintf(tmp, "%s(): '%s': '%s'", from, msg, det);
	return tmp;
}

void pup_err_stack_process_str(pup_context_t *pup, void (*callback)(pup_err_stack_t *entry, char *string))
{
	pup_err_stack_t *entry;
	char *str;

	assert(callback != NULL);
	do {
		entry = pup_err_stack_pop(pup);
		if (entry != NULL) {
			str   = pup_str_err_stack_entry(entry);
			callback(entry, str);
			free(str);
			pup_err_stack_destroy(pup, entry);
		}
	} while(entry != NULL);
}

int pup_err_stack_enable(pup_context_t *pup)
{
	int previous;

	previous = pup->error_stack_enable;
	pup->error_stack_enable = 1;
	return previous;
}

int pup_err_stack_disable(pup_context_t *pup)
{
	int previous;

	previous = pup->error_stack_enable;
	pup->error_stack_enable = 0;
	return previous;
}