1
0

nseframe.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * Description (nseframe.c)
  43. *
  44. * This module is part of the NSACL_RES_ERROR facility. It contains functions
  45. * for allocating, freeing, and managing error frame structures. It
  46. * does not contain routines for generating error messages through
  47. * the use of a message file.
  48. */
  49. #include "base/systems.h"
  50. #include "netsite.h"
  51. #include "libaccess/nserror.h"
  52. /*
  53. * Description (nserrDispose)
  54. *
  55. * This function is used to dispose of an entire list of error
  56. * frames when the error information is no longer needed. It
  57. * does not free the list head, since it is usually not dynamically
  58. * allocated.
  59. *
  60. * Arguments:
  61. *
  62. * errp - error frame list head pointer
  63. */
  64. void nserrDispose(NSErr_t * errp)
  65. {
  66. /* Ignore null list head */
  67. if (errp == 0) return;
  68. while (errp->err_first) {
  69. nserrFFree(errp, errp->err_first);
  70. }
  71. }
  72. /*
  73. * Description (nserrFAlloc)
  74. *
  75. * This is the default allocator for error frame structures. It
  76. * calls an allocator function indicated by err_falloc, if any,
  77. * or else uses MALLOC().
  78. *
  79. * Arguments:
  80. *
  81. * errp - error frame list head pointer
  82. * (may be null)
  83. *
  84. * Returns:
  85. *
  86. * The return value is a pointer to a cleared error frame. The
  87. * frame will not have been added to the list referenced by errp.
  88. * An allocation error is indicated by a null return value.
  89. */
  90. NSEFrame_t * nserrFAlloc(NSErr_t * errp)
  91. {
  92. NSEFrame_t * efp; /* return error frame pointer */
  93. /* Allocate the error frame */
  94. efp = (errp && errp->err_falloc)
  95. ? (*errp->err_falloc)(errp)
  96. : (NSEFrame_t *)MALLOC(sizeof(NSEFrame_t));
  97. if (efp) {
  98. /* Clear the error frame */
  99. memset((void *)efp, 0, sizeof(NSEFrame_t));
  100. }
  101. return efp;
  102. }
  103. /*
  104. * Description (nserrFFree)
  105. *
  106. * This function frees an error frame structure. If an error list
  107. * head is specified, it first checks whether the indicated error
  108. * frame is on the list, and removes it if so. If the ef_dispose
  109. * field is non-null, the indicated function is called. The error
  110. * frame is deallocated using either a function indicated by
  111. * err_free in the list head, or FREE() otherwise.
  112. *
  113. * Arguments:
  114. *
  115. * errp - error frame list head pointer
  116. * (may be null)
  117. * efp - error frame pointer
  118. */
  119. void nserrFFree(NSErr_t * errp, NSEFrame_t * efp)
  120. {
  121. NSEFrame_t **lefp; /* pointer to error frame pointer */
  122. NSEFrame_t * pefp; /* previous error frame on list */
  123. int i;
  124. /* Ignore null error frame pointer */
  125. if (efp == 0) return;
  126. /* Got a list head? */
  127. if (errp) {
  128. /* Yes, see if this frame is on the list */
  129. pefp = 0;
  130. for (lefp = &errp->err_first; *lefp != 0; lefp = &pefp->ef_next) {
  131. if (*lefp == efp) {
  132. /* Yes, remove it */
  133. *lefp = efp->ef_next;
  134. if (errp->err_last == efp) errp->err_last = pefp;
  135. break;
  136. }
  137. pefp = *lefp;
  138. }
  139. }
  140. /* Free strings referenced by the frame */
  141. for (i = 0; i < efp->ef_errc; ++i) {
  142. if (efp->ef_errv[i]) {
  143. FREE(efp->ef_errv[i]);
  144. }
  145. }
  146. /* Free the frame */
  147. if (errp && errp->err_ffree) {
  148. (*errp->err_ffree)(errp, efp);
  149. }
  150. else {
  151. FREE(efp);
  152. }
  153. }
  154. /*
  155. * Description (nserrGenerate)
  156. *
  157. * This function is called to generate an error frame and add it
  158. * to a specified error list.
  159. *
  160. * Arguments:
  161. *
  162. * errp - error frame list head pointer
  163. * (may be null)
  164. * retcode - return code (ef_retcode)
  165. * errorid - error id (ef_errorid)
  166. * program - program string pointer (ef_program)
  167. * errc - count of error arguments (ef_errc)
  168. * ... - values for ef_errv[]
  169. *
  170. * Returns:
  171. *
  172. * The return value is a pointer to the generated error frame, filled
  173. * in with the provided information. An allocation error is indicated
  174. * by a null return value.
  175. */
  176. NSEFrame_t * nserrGenerate(NSErr_t * errp, long retcode, long errorid,
  177. const char * program, int errc, ...)
  178. {
  179. NSEFrame_t * efp; /* error frame pointer */
  180. char * esp; /* error string pointer */
  181. int i;
  182. va_list ap;
  183. /* Null frame list head pointer means do nothing */
  184. if (errp == 0) {
  185. return 0;
  186. }
  187. /* Limit the number of values in ef_errv[] */
  188. if (errc > NSERRMAXARG) errc = NSERRMAXARG;
  189. /* Allocate the error frame */
  190. efp = nserrFAlloc(errp);
  191. /* Did we get it? */
  192. if (efp) {
  193. /* Yes, copy information into it */
  194. efp->ef_retcode = retcode;
  195. efp->ef_errorid = errorid;
  196. efp->ef_program = program;
  197. efp->ef_errc = errc;
  198. /* Get the string arguments and copy them */
  199. va_start(ap, errc);
  200. for (i = 0; i < errc; ++i) {
  201. esp = va_arg(ap, char *);
  202. efp->ef_errv[i] = STRDUP(esp);
  203. }
  204. va_end(ap);
  205. /* Add the frame to the list (if any) */
  206. if (errp) {
  207. efp->ef_next = errp->err_first;
  208. errp->err_first = efp;
  209. if (efp->ef_next == 0) errp->err_last = efp;
  210. }
  211. }
  212. /* Return the error frame pointer */
  213. return efp;
  214. }