summaryrefslogtreecommitdiffstats
path: root/lib/libaccess/nseframe.cpp
blob: 96b71dbc766e219f217a146db3f735626d57fb11 (plain)
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
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

/*
 * Description (nseframe.c)
 *
 *	This module is part of the NSACL_RES_ERROR facility.  It contains functions
 *	for allocating, freeing, and managing error frame structures.  It
 *	does not contain routines for generating error messages through
 *	the use of a message file.
 */

#include "base/systems.h"
#include "netsite.h"
#include "libaccess/nserror.h"

/*
 * Description (nserrDispose)
 *
 *	This function is used to dispose of an entire list of error
 *	frames when the error information is no longer needed.  It
 *	does not free the list head, since it is usually not dynamically
 *	allocated.
 *
 * Arguments:
 *
 *	errp			- error frame list head pointer
 */

void nserrDispose(NSErr_t * errp)
{
    /* Ignore null list head */
    if (errp == 0) return;

    while (errp->err_first) {

	nserrFFree(errp, errp->err_first);
    }
}

/*
 * Description (nserrFAlloc)
 *
 *	This is the default allocator for error frame structures.  It
 *	calls an allocator function indicated by err_falloc, if any,
 *	or else uses MALLOC().
 *
 * Arguments:
 *
 *	errp			- error frame list head pointer
 *				  (may be null)
 *
 * Returns:
 *
 *	The return value is a pointer to a cleared error frame.  The
 *	frame will not have been added to the list referenced by errp.
 *	An allocation error is indicated by a null return value.
 */

NSEFrame_t * nserrFAlloc(NSErr_t * errp)
{
    NSEFrame_t * efp;			/* return error frame pointer */

    /* Allocate the error frame */
    efp = (errp && errp->err_falloc)
			? (*errp->err_falloc)(errp)
			: (NSEFrame_t *)MALLOC(sizeof(NSEFrame_t));

    if (efp) {
	/* Clear the error frame */
	memset((void *)efp, 0, sizeof(NSEFrame_t));
    }

    return efp;
}

/*
 * Description (nserrFFree)
 *
 *	This function frees an error frame structure.  If an error list
 *	head is specified, it first checks whether the indicated error
 *	frame is on the list, and removes it if so.  If the ef_dispose
 *	field is non-null, the indicated function is called.  The error
 *	frame is deallocated using either a function indicated by
 *	err_free in the list head, or FREE() otherwise.
 *
 * Arguments:
 *
 *	errp			- error frame list head pointer
 *				  (may be null)
 *	efp			- error frame pointer
 */

void nserrFFree(NSErr_t * errp, NSEFrame_t * efp)
{
    NSEFrame_t **lefp;		/* pointer to error frame pointer */
    NSEFrame_t * pefp;		/* previous error frame on list */
    int i;

    /* Ignore null error frame pointer */
    if (efp == 0) return;

    /* Got a list head? */
    if (errp) {

	/* Yes, see if this frame is on the list */
	pefp = 0;
	for (lefp = &errp->err_first; *lefp != 0; lefp = &pefp->ef_next) {
	    if (*lefp == efp) {

		/* Yes, remove it */
		*lefp = efp->ef_next;
		if (errp->err_last == efp) errp->err_last = pefp;
		break;
	    }
	    pefp = *lefp;
	}
    }

    /* Free strings referenced by the frame */
    for (i = 0; i < efp->ef_errc; ++i) {
	if (efp->ef_errv[i]) {
	    FREE(efp->ef_errv[i]);
	}
    }

    /* Free the frame */
    if (errp && errp->err_ffree) {
	(*errp->err_ffree)(errp, efp);
    }
    else {
	FREE(efp);
    }
}

/*
 * Description (nserrGenerate)
 *
 *	This function is called to generate an error frame and add it
 *	to a specified error list.
 *
 * Arguments:
 *
 *	errp			- error frame list head pointer
 *				  (may be null)
 *	retcode			- return code (ef_retcode)
 *	errorid			- error id (ef_errorid)
 *	program			- program string pointer (ef_program)
 *	errc			- count of error arguments (ef_errc)
 *	...			- values for ef_errv[]
 *
 * Returns:
 *
 *	The return value is a pointer to the generated error frame, filled
 *	in with the provided information.  An allocation error is indicated
 *	by a null return value.
 */

NSEFrame_t * nserrGenerate(NSErr_t * errp, long retcode, long errorid,
			   char * program, int errc, ...)
{
    NSEFrame_t * efp;			/* error frame pointer */
    char * esp;				/* error string pointer */
    int i;
    va_list ap;

    /* Null frame list head pointer means do nothing */
    if (errp == 0) {
	return 0;
    }

    /* Limit the number of values in ef_errv[] */
    if (errc > NSERRMAXARG) errc = NSERRMAXARG;

    /* Allocate the error frame */
    efp = nserrFAlloc(errp);

    /* Did we get it? */
    if (efp) {

	/* Yes, copy information into it */
	efp->ef_retcode = retcode;
	efp->ef_errorid = errorid;
	efp->ef_program = program;
	efp->ef_errc = errc;

	/* Get the string arguments and copy them */
	va_start(ap, errc);
	for (i = 0; i < errc; ++i) {
	    esp = va_arg(ap, char *);
	    efp->ef_errv[i] = STRDUP(esp);
	}

	/* Add the frame to the list (if any) */
	if (errp) {
	    efp->ef_next = errp->err_first;
	    errp->err_first = efp;
	    if (efp->ef_next == 0) errp->err_last = efp;
	}
    }

    /* Return the error frame pointer */
    return efp;
}