stack.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * stack.c
  3. * $Id: stack.c,v 4.4 2005/03/13 19:51:59 bkorb Exp $
  4. * Time-stamp: "2005-02-20 16:33:20 bkorb"
  5. *
  6. * This is a special option processing routine that will save the
  7. * argument to an option in a FIFO queue.
  8. */
  9. /*
  10. * Automated Options copyright 1992-2005 Bruce Korb
  11. *
  12. * Automated Options is free software.
  13. * You may redistribute it and/or modify it under the terms of the
  14. * GNU General Public License, as published by the Free Software
  15. * Foundation; either version 2, or (at your option) any later version.
  16. *
  17. * Automated Options is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with Automated Options. See the file "COPYING". If not,
  24. * write to: The Free Software Foundation, Inc.,
  25. * 59 Temple Place - Suite 330,
  26. * Boston, MA 02111-1307, USA.
  27. *
  28. * As a special exception, Bruce Korb gives permission for additional
  29. * uses of the text contained in his release of AutoOpts.
  30. *
  31. * The exception is that, if you link the AutoOpts library with other
  32. * files to produce an executable, this does not by itself cause the
  33. * resulting executable to be covered by the GNU General Public License.
  34. * Your use of that executable is in no way restricted on account of
  35. * linking the AutoOpts library code into it.
  36. *
  37. * This exception does not however invalidate any other reasons why
  38. * the executable file might be covered by the GNU General Public License.
  39. *
  40. * This exception applies only to the code released by Bruce Korb under
  41. * the name AutoOpts. If you copy code from other sources under the
  42. * General Public License into a copy of AutoOpts, as the General Public
  43. * License permits, the exception does not apply to the code that you add
  44. * in this way. To avoid misleading anyone as to the status of such
  45. * modified files, you must delete this exception notice from them.
  46. *
  47. * If you write modifications of your own for AutoOpts, it is your choice
  48. * whether to permit this exception to apply to your modifications.
  49. * If you do not wish that, delete this exception notice.
  50. */
  51. #include REGEX_HEADER
  52. /*=export_func optionUnstackArg
  53. * private:
  54. *
  55. * what: Remove option args from a stack
  56. * arg: + tOptions* + pOpts + program options descriptor +
  57. * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
  58. *
  59. * doc:
  60. * Invoked for options that are equivalenced to stacked options.
  61. =*/
  62. void
  63. optionUnstackArg(
  64. tOptions* pOpts,
  65. tOptDesc* pOptDesc )
  66. {
  67. int res;
  68. tArgList* pAL = (tArgList*)pOptDesc->optCookie;
  69. /*
  70. * IF we don't have any stacked options,
  71. * THEN indicate that we don't have any of these options
  72. */
  73. if (pAL == NULL) {
  74. pOptDesc->fOptState &= OPTST_PERSISTENT;
  75. if ( (pOptDesc->fOptState & OPTST_INITENABLED) == 0)
  76. pOptDesc->fOptState |= OPTST_DISABLED;
  77. return;
  78. }
  79. {
  80. regex_t re;
  81. int i, ct, dIdx;
  82. if (regcomp( &re, pOptDesc->pzLastArg, REG_NOSUB ) != 0)
  83. return;
  84. /*
  85. * search the list for the entry(s) to remove. Entries that
  86. * are removed are *not* copied into the result. The source
  87. * index is incremented every time. The destination only when
  88. * we are keeping a define.
  89. */
  90. for (i = 0, dIdx = 0, ct = pAL->useCt; --ct >= 0; i++) {
  91. tCC* pzSrc = pAL->apzArgs[ i ];
  92. char* pzEq = strchr( pzSrc, '=' );
  93. if (pzEq != NULL)
  94. *pzEq = NUL;
  95. res = regexec( &re, pzSrc, (size_t)0, NULL, 0 );
  96. switch (res) {
  97. case 0:
  98. /*
  99. * Remove this entry by reducing the in-use count
  100. * and *not* putting the string pointer back into
  101. * the list.
  102. */
  103. pAL->useCt--;
  104. break;
  105. default:
  106. case REG_NOMATCH:
  107. if (pzEq != NULL)
  108. *pzEq = '=';
  109. /*
  110. * IF we have dropped an entry
  111. * THEN we have to move the current one.
  112. */
  113. if (dIdx != i)
  114. pAL->apzArgs[ dIdx ] = pzSrc;
  115. dIdx++;
  116. }
  117. }
  118. regfree( &re );
  119. }
  120. /*
  121. * IF we have unstacked everything,
  122. * THEN indicate that we don't have any of these options
  123. */
  124. if (pAL->useCt == 0) {
  125. pOptDesc->fOptState &= OPTST_PERSISTENT;
  126. if ( (pOptDesc->fOptState & OPTST_INITENABLED) == 0)
  127. pOptDesc->fOptState |= OPTST_DISABLED;
  128. free( (void*)pAL );
  129. pOptDesc->optCookie = NULL;
  130. }
  131. }
  132. /*
  133. * Put an entry into an argument list. The first argument points to
  134. * a pointer to the argument list structure. It gets passed around
  135. * as an opaque address.
  136. */
  137. LOCAL void
  138. addArgListEntry( void** ppAL, void* entry )
  139. {
  140. tArgList* pAL = *(void**)ppAL;
  141. /*
  142. * IF we have never allocated one of these,
  143. * THEN allocate one now
  144. */
  145. if (pAL == NULL) {
  146. pAL = (tArgList*)AGALOC( sizeof( *pAL ), "new option arg stack" );
  147. if (pAL == NULL)
  148. return;
  149. pAL->useCt = 0;
  150. pAL->allocCt = MIN_ARG_ALLOC_CT;
  151. *ppAL = (void*)pAL;
  152. }
  153. /*
  154. * ELSE if we are out of room
  155. * THEN make it bigger
  156. */
  157. else if (pAL->useCt >= pAL->allocCt) {
  158. size_t sz = sizeof( *pAL );
  159. pAL->allocCt += INCR_ARG_ALLOC_CT;
  160. /*
  161. * The base structure contains space for MIN_ARG_ALLOC_CT
  162. * pointers. We subtract it off to find our augment size.
  163. */
  164. sz += sizeof(char*) * (pAL->allocCt - MIN_ARG_ALLOC_CT);
  165. pAL = (tArgList*)AGREALOC( (void*)pAL, sz, "expanded opt arg stack" );
  166. if (pAL == NULL)
  167. return;
  168. *ppAL = (void*)pAL;
  169. }
  170. /*
  171. * Insert the new argument into the list
  172. */
  173. pAL->apzArgs[ (pAL->useCt)++ ] = entry;
  174. }
  175. /*=export_func optionStackArg
  176. * private:
  177. *
  178. * what: put option args on a stack
  179. * arg: + tOptions* + pOpts + program options descriptor +
  180. * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
  181. *
  182. * doc:
  183. * Keep an entry-ordered list of option arguments.
  184. =*/
  185. void
  186. optionStackArg(
  187. tOptions* pOpts,
  188. tOptDesc* pOD )
  189. {
  190. if (pOD->pzLastArg == NULL)
  191. return;
  192. addArgListEntry( &(pOD->optCookie), (void*)pOD->pzLastArg );
  193. }
  194. /*
  195. * Local Variables:
  196. * mode: C
  197. * c-file-style: "stroustrup"
  198. * tab-width: 4
  199. * indent-tabs-mode: nil
  200. * End:
  201. * end of autoopts/stack.c */