stack.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * \file stack.c
  3. *
  4. * This is a special option processing routine that will save the
  5. * argument to an option in a FIFO queue.
  6. *
  7. * @addtogroup autoopts
  8. * @{
  9. */
  10. /*
  11. * This file is part of AutoOpts, a companion to AutoGen.
  12. * AutoOpts is free software.
  13. * AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
  14. *
  15. * AutoOpts is available under any one of two licenses. The license
  16. * in use must be one of these two and the choice is under the control
  17. * of the user of the license.
  18. *
  19. * The GNU Lesser General Public License, version 3 or later
  20. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  21. *
  22. * The Modified Berkeley Software Distribution License
  23. * See the file "COPYING.mbsd"
  24. *
  25. * These files have the following sha256 sums:
  26. *
  27. * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
  28. * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
  29. * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
  30. */
  31. #ifdef WITH_LIBREGEX
  32. # include REGEX_HEADER
  33. #endif
  34. /*=export_func optionUnstackArg
  35. * private:
  36. *
  37. * what: Remove option args from a stack
  38. * arg: + tOptions * + opts + program options descriptor +
  39. * arg: + tOptDesc * + od + the descriptor for this arg +
  40. *
  41. * doc:
  42. * Invoked for options that are equivalenced to stacked options.
  43. =*/
  44. void
  45. optionUnstackArg(tOptions * opts, tOptDesc * od)
  46. {
  47. tArgList * arg_list;
  48. if (INQUERY_CALL(opts, od))
  49. return;
  50. arg_list = (tArgList *)od->optCookie;
  51. /*
  52. * IF we don't have any stacked options,
  53. * THEN indicate that we don't have any of these options
  54. */
  55. if (arg_list == NULL) {
  56. od->fOptState &= OPTST_PERSISTENT_MASK;
  57. if ((od->fOptState & OPTST_INITENABLED) == 0)
  58. od->fOptState |= OPTST_DISABLED;
  59. return;
  60. }
  61. #ifdef WITH_LIBREGEX
  62. {
  63. regex_t re;
  64. int i, ct, dIdx;
  65. if (regcomp(&re, od->optArg.argString, REG_NOSUB) != 0)
  66. return;
  67. /*
  68. * search the list for the entry(s) to remove. Entries that
  69. * are removed are *not* copied into the result. The source
  70. * index is incremented every time. The destination only when
  71. * we are keeping a define.
  72. */
  73. for (i = 0, dIdx = 0, ct = arg_list->useCt; --ct >= 0; i++) {
  74. char const * pzSrc = arg_list->apzArgs[ i ];
  75. char * pzEq = strchr(pzSrc, '=');
  76. int res;
  77. if (pzEq != NULL)
  78. *pzEq = NUL;
  79. res = regexec(&re, pzSrc, (size_t)0, NULL, 0);
  80. switch (res) {
  81. case 0:
  82. /*
  83. * Remove this entry by reducing the in-use count
  84. * and *not* putting the string pointer back into
  85. * the list.
  86. */
  87. AGFREE(pzSrc);
  88. arg_list->useCt--;
  89. break;
  90. default:
  91. case REG_NOMATCH:
  92. if (pzEq != NULL)
  93. *pzEq = '=';
  94. /*
  95. * IF we have dropped an entry
  96. * THEN we have to move the current one.
  97. */
  98. if (dIdx != i)
  99. arg_list->apzArgs[ dIdx ] = pzSrc;
  100. dIdx++;
  101. }
  102. }
  103. regfree(&re);
  104. }
  105. #else /* not WITH_LIBREGEX */
  106. {
  107. int i, ct, dIdx;
  108. /*
  109. * search the list for the entry(s) to remove. Entries that
  110. * are removed are *not* copied into the result. The source
  111. * index is incremented every time. The destination only when
  112. * we are keeping a define.
  113. */
  114. for (i = 0, dIdx = 0, ct = arg_list->useCt; --ct >= 0; i++) {
  115. const char * pzSrc = arg_list->apzArgs[ i ];
  116. char * pzEq = strchr(pzSrc, '=');
  117. if (pzEq != NULL)
  118. *pzEq = NUL;
  119. if (strcmp(pzSrc, od->optArg.argString) == 0) {
  120. /*
  121. * Remove this entry by reducing the in-use count
  122. * and *not* putting the string pointer back into
  123. * the list.
  124. */
  125. AGFREE(pzSrc);
  126. arg_list->useCt--;
  127. } else {
  128. if (pzEq != NULL)
  129. *pzEq = '=';
  130. /*
  131. * IF we have dropped an entry
  132. * THEN we have to move the current one.
  133. */
  134. if (dIdx != i)
  135. arg_list->apzArgs[ dIdx ] = pzSrc;
  136. dIdx++;
  137. }
  138. }
  139. }
  140. #endif /* WITH_LIBREGEX */
  141. /*
  142. * IF we have unstacked everything,
  143. * THEN indicate that we don't have any of these options
  144. */
  145. if (arg_list->useCt == 0) {
  146. od->fOptState &= OPTST_PERSISTENT_MASK;
  147. if ((od->fOptState & OPTST_INITENABLED) == 0)
  148. od->fOptState |= OPTST_DISABLED;
  149. AGFREE(arg_list);
  150. od->optCookie = NULL;
  151. }
  152. }
  153. /*
  154. * Put an entry into an argument list. The first argument points to
  155. * a pointer to the argument list structure. It gets passed around
  156. * as an opaque address.
  157. */
  158. static void
  159. addArgListEntry(void ** ppAL, void * entry)
  160. {
  161. tArgList * pAL = *(void **)ppAL;
  162. /*
  163. * IF we have never allocated one of these,
  164. * THEN allocate one now
  165. */
  166. if (pAL == NULL) {
  167. pAL = (tArgList *)AGALOC(sizeof(*pAL), "new option arg stack");
  168. if (pAL == NULL)
  169. return;
  170. pAL->useCt = 0;
  171. pAL->allocCt = MIN_ARG_ALLOC_CT;
  172. *ppAL = VOIDP(pAL);
  173. }
  174. /*
  175. * ELSE if we are out of room
  176. * THEN make it bigger
  177. */
  178. else if (pAL->useCt >= pAL->allocCt) {
  179. size_t sz = sizeof(*pAL);
  180. pAL->allocCt += INCR_ARG_ALLOC_CT;
  181. /*
  182. * The base structure contains space for MIN_ARG_ALLOC_CT
  183. * pointers. We subtract it off to find our augment size.
  184. */
  185. sz += sizeof(char *) * ((size_t)pAL->allocCt - MIN_ARG_ALLOC_CT);
  186. pAL = (tArgList *)AGREALOC(VOIDP(pAL), sz, "expanded opt arg stack");
  187. if (pAL == NULL)
  188. return;
  189. *ppAL = VOIDP(pAL);
  190. }
  191. /*
  192. * Insert the new argument into the list
  193. */
  194. pAL->apzArgs[ (pAL->useCt)++ ] = entry;
  195. }
  196. /*=export_func optionStackArg
  197. * private:
  198. *
  199. * what: put option args on a stack
  200. * arg: + tOptions * + opts + program options descriptor +
  201. * arg: + tOptDesc * + od + the descriptor for this arg +
  202. *
  203. * doc:
  204. * Keep an entry-ordered list of option arguments.
  205. =*/
  206. void
  207. optionStackArg(tOptions * opts, tOptDesc * od)
  208. {
  209. char * pz;
  210. if (INQUERY_CALL(opts, od))
  211. return;
  212. if ((od->fOptState & OPTST_RESET) != 0) {
  213. tArgList * arg_list = od->optCookie;
  214. int ix;
  215. if (arg_list == NULL)
  216. return;
  217. ix = arg_list->useCt;
  218. while (--ix >= 0)
  219. AGFREE(arg_list->apzArgs[ix]);
  220. AGFREE(arg_list);
  221. } else {
  222. if (od->optArg.argString == NULL)
  223. return;
  224. AGDUPSTR(pz, od->optArg.argString, "stack arg");
  225. addArgListEntry(&(od->optCookie), VOIDP(pz));
  226. }
  227. }
  228. /** @}
  229. *
  230. * Local Variables:
  231. * mode: C
  232. * c-file-style: "stroustrup"
  233. * indent-tabs-mode: nil
  234. * End:
  235. * end of autoopts/stack.c */