nseframe.cpp 4.9 KB

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