nseframe.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * Description (nseframe.c)
  14. *
  15. * This module is part of the NSACL_RES_ERROR facility. It contains functions
  16. * for allocating, freeing, and managing error frame structures. It
  17. * does not contain routines for generating error messages through
  18. * the use of a message file.
  19. */
  20. #include "base/systems.h"
  21. #include "netsite.h"
  22. #include "libaccess/nserror.h"
  23. /*
  24. * Description (nserrDispose)
  25. *
  26. * This function is used to dispose of an entire list of error
  27. * frames when the error information is no longer needed. It
  28. * does not free the list head, since it is usually not dynamically
  29. * allocated.
  30. *
  31. * Arguments:
  32. *
  33. * errp - error frame list head pointer
  34. */
  35. void nserrDispose(NSErr_t * errp)
  36. {
  37. /* Ignore null list head */
  38. if (errp == 0) return;
  39. while (errp->err_first) {
  40. nserrFFree(errp, errp->err_first);
  41. }
  42. }
  43. /*
  44. * Description (nserrFAlloc)
  45. *
  46. * This is the default allocator for error frame structures. It
  47. * calls an allocator function indicated by err_falloc, if any,
  48. * or else uses MALLOC().
  49. *
  50. * Arguments:
  51. *
  52. * errp - error frame list head pointer
  53. * (may be null)
  54. *
  55. * Returns:
  56. *
  57. * The return value is a pointer to a cleared error frame. The
  58. * frame will not have been added to the list referenced by errp.
  59. * An allocation error is indicated by a null return value.
  60. */
  61. NSEFrame_t * nserrFAlloc(NSErr_t * errp)
  62. {
  63. NSEFrame_t * efp; /* return error frame pointer */
  64. /* Allocate the error frame */
  65. efp = (errp && errp->err_falloc)
  66. ? (*errp->err_falloc)(errp)
  67. : (NSEFrame_t *)MALLOC(sizeof(NSEFrame_t));
  68. if (efp) {
  69. /* Clear the error frame */
  70. memset((void *)efp, 0, sizeof(NSEFrame_t));
  71. }
  72. return efp;
  73. }
  74. /*
  75. * Description (nserrFFree)
  76. *
  77. * This function frees an error frame structure. If an error list
  78. * head is specified, it first checks whether the indicated error
  79. * frame is on the list, and removes it if so. If the ef_dispose
  80. * field is non-null, the indicated function is called. The error
  81. * frame is deallocated using either a function indicated by
  82. * err_free in the list head, or FREE() otherwise.
  83. *
  84. * Arguments:
  85. *
  86. * errp - error frame list head pointer
  87. * (may be null)
  88. * efp - error frame pointer
  89. */
  90. void nserrFFree(NSErr_t * errp, NSEFrame_t * efp)
  91. {
  92. NSEFrame_t **lefp; /* pointer to error frame pointer */
  93. NSEFrame_t * pefp; /* previous error frame on list */
  94. int i;
  95. /* Ignore null error frame pointer */
  96. if (efp == 0) return;
  97. /* Got a list head? */
  98. if (errp) {
  99. /* Yes, see if this frame is on the list */
  100. pefp = 0;
  101. for (lefp = &errp->err_first; *lefp != 0; lefp = &pefp->ef_next) {
  102. if (*lefp == efp) {
  103. /* Yes, remove it */
  104. *lefp = efp->ef_next;
  105. if (errp->err_last == efp) errp->err_last = pefp;
  106. break;
  107. }
  108. pefp = *lefp;
  109. }
  110. }
  111. /* Free strings referenced by the frame */
  112. for (i = 0; i < efp->ef_errc; ++i) {
  113. if (efp->ef_errv[i]) {
  114. FREE(efp->ef_errv[i]);
  115. }
  116. }
  117. /* Free the frame */
  118. if (errp && errp->err_ffree) {
  119. (*errp->err_ffree)(errp, efp);
  120. }
  121. else {
  122. FREE(efp);
  123. }
  124. }
  125. /*
  126. * Description (nserrGenerate)
  127. *
  128. * This function is called to generate an error frame and add it
  129. * to a specified error list.
  130. *
  131. * Arguments:
  132. *
  133. * errp - error frame list head pointer
  134. * (may be null)
  135. * retcode - return code (ef_retcode)
  136. * errorid - error id (ef_errorid)
  137. * program - program string pointer (ef_program)
  138. * errc - count of error arguments (ef_errc)
  139. * ... - values for ef_errv[]
  140. *
  141. * Returns:
  142. *
  143. * The return value is a pointer to the generated error frame, filled
  144. * in with the provided information. An allocation error is indicated
  145. * by a null return value.
  146. */
  147. NSEFrame_t * nserrGenerate(NSErr_t * errp, long retcode, long errorid,
  148. const char * program, int errc, ...)
  149. {
  150. NSEFrame_t * efp; /* error frame pointer */
  151. char * esp; /* error string pointer */
  152. int i;
  153. va_list ap;
  154. /* Null frame list head pointer means do nothing */
  155. if (errp == 0) {
  156. return 0;
  157. }
  158. /* Limit the number of values in ef_errv[] */
  159. if (errc > NSERRMAXARG) errc = NSERRMAXARG;
  160. /* Allocate the error frame */
  161. efp = nserrFAlloc(errp);
  162. /* Did we get it? */
  163. if (efp) {
  164. /* Yes, copy information into it */
  165. efp->ef_retcode = retcode;
  166. efp->ef_errorid = errorid;
  167. efp->ef_program = program;
  168. efp->ef_errc = errc;
  169. /* Get the string arguments and copy them */
  170. va_start(ap, errc);
  171. for (i = 0; i < errc; ++i) {
  172. esp = va_arg(ap, char *);
  173. efp->ef_errv[i] = STRDUP(esp);
  174. }
  175. va_end(ap);
  176. /* Add the frame to the list (if any) */
  177. if (errp) {
  178. efp->ef_next = errp->err_first;
  179. errp->err_first = efp;
  180. if (efp->ef_next == 0) errp->err_last = efp;
  181. }
  182. }
  183. /* Return the error frame pointer */
  184. return efp;
  185. }