nseframe.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /*
  39. * Description (nseframe.c)
  40. *
  41. * This module is part of the NSACL_RES_ERROR facility. It contains functions
  42. * for allocating, freeing, and managing error frame structures. It
  43. * does not contain routines for generating error messages through
  44. * the use of a message file.
  45. */
  46. #include "base/systems.h"
  47. #include "netsite.h"
  48. #include "libaccess/nserror.h"
  49. /*
  50. * Description (nserrDispose)
  51. *
  52. * This function is used to dispose of an entire list of error
  53. * frames when the error information is no longer needed. It
  54. * does not free the list head, since it is usually not dynamically
  55. * allocated.
  56. *
  57. * Arguments:
  58. *
  59. * errp - error frame list head pointer
  60. */
  61. void nserrDispose(NSErr_t * errp)
  62. {
  63. /* Ignore null list head */
  64. if (errp == 0) return;
  65. while (errp->err_first) {
  66. nserrFFree(errp, errp->err_first);
  67. }
  68. }
  69. /*
  70. * Description (nserrFAlloc)
  71. *
  72. * This is the default allocator for error frame structures. It
  73. * calls an allocator function indicated by err_falloc, if any,
  74. * or else uses MALLOC().
  75. *
  76. * Arguments:
  77. *
  78. * errp - error frame list head pointer
  79. * (may be null)
  80. *
  81. * Returns:
  82. *
  83. * The return value is a pointer to a cleared error frame. The
  84. * frame will not have been added to the list referenced by errp.
  85. * An allocation error is indicated by a null return value.
  86. */
  87. NSEFrame_t * nserrFAlloc(NSErr_t * errp)
  88. {
  89. NSEFrame_t * efp; /* return error frame pointer */
  90. /* Allocate the error frame */
  91. efp = (errp && errp->err_falloc)
  92. ? (*errp->err_falloc)(errp)
  93. : (NSEFrame_t *)MALLOC(sizeof(NSEFrame_t));
  94. if (efp) {
  95. /* Clear the error frame */
  96. memset((void *)efp, 0, sizeof(NSEFrame_t));
  97. }
  98. return efp;
  99. }
  100. /*
  101. * Description (nserrFFree)
  102. *
  103. * This function frees an error frame structure. If an error list
  104. * head is specified, it first checks whether the indicated error
  105. * frame is on the list, and removes it if so. If the ef_dispose
  106. * field is non-null, the indicated function is called. The error
  107. * frame is deallocated using either a function indicated by
  108. * err_free in the list head, or FREE() otherwise.
  109. *
  110. * Arguments:
  111. *
  112. * errp - error frame list head pointer
  113. * (may be null)
  114. * efp - error frame pointer
  115. */
  116. void nserrFFree(NSErr_t * errp, NSEFrame_t * efp)
  117. {
  118. NSEFrame_t **lefp; /* pointer to error frame pointer */
  119. NSEFrame_t * pefp; /* previous error frame on list */
  120. int i;
  121. /* Ignore null error frame pointer */
  122. if (efp == 0) return;
  123. /* Got a list head? */
  124. if (errp) {
  125. /* Yes, see if this frame is on the list */
  126. pefp = 0;
  127. for (lefp = &errp->err_first; *lefp != 0; lefp = &pefp->ef_next) {
  128. if (*lefp == efp) {
  129. /* Yes, remove it */
  130. *lefp = efp->ef_next;
  131. if (errp->err_last == efp) errp->err_last = pefp;
  132. break;
  133. }
  134. pefp = *lefp;
  135. }
  136. }
  137. /* Free strings referenced by the frame */
  138. for (i = 0; i < efp->ef_errc; ++i) {
  139. if (efp->ef_errv[i]) {
  140. FREE(efp->ef_errv[i]);
  141. }
  142. }
  143. /* Free the frame */
  144. if (errp && errp->err_ffree) {
  145. (*errp->err_ffree)(errp, efp);
  146. }
  147. else {
  148. FREE(efp);
  149. }
  150. }
  151. /*
  152. * Description (nserrGenerate)
  153. *
  154. * This function is called to generate an error frame and add it
  155. * to a specified error list.
  156. *
  157. * Arguments:
  158. *
  159. * errp - error frame list head pointer
  160. * (may be null)
  161. * retcode - return code (ef_retcode)
  162. * errorid - error id (ef_errorid)
  163. * program - program string pointer (ef_program)
  164. * errc - count of error arguments (ef_errc)
  165. * ... - values for ef_errv[]
  166. *
  167. * Returns:
  168. *
  169. * The return value is a pointer to the generated error frame, filled
  170. * in with the provided information. An allocation error is indicated
  171. * by a null return value.
  172. */
  173. NSEFrame_t * nserrGenerate(NSErr_t * errp, long retcode, long errorid,
  174. char * program, int errc, ...)
  175. {
  176. NSEFrame_t * efp; /* error frame pointer */
  177. char * esp; /* error string pointer */
  178. int i;
  179. va_list ap;
  180. /* Null frame list head pointer means do nothing */
  181. if (errp == 0) {
  182. return 0;
  183. }
  184. /* Limit the number of values in ef_errv[] */
  185. if (errc > NSERRMAXARG) errc = NSERRMAXARG;
  186. /* Allocate the error frame */
  187. efp = nserrFAlloc(errp);
  188. /* Did we get it? */
  189. if (efp) {
  190. /* Yes, copy information into it */
  191. efp->ef_retcode = retcode;
  192. efp->ef_errorid = errorid;
  193. efp->ef_program = program;
  194. efp->ef_errc = errc;
  195. /* Get the string arguments and copy them */
  196. va_start(ap, errc);
  197. for (i = 0; i < errc; ++i) {
  198. esp = va_arg(ap, char *);
  199. efp->ef_errv[i] = STRDUP(esp);
  200. }
  201. /* Add the frame to the list (if any) */
  202. if (errp) {
  203. efp->ef_next = errp->err_first;
  204. errp->err_first = efp;
  205. if (efp->ef_next == 0) errp->err_last = efp;
  206. }
  207. }
  208. /* Return the error frame pointer */
  209. return efp;
  210. }