opt.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* LIBDGL -- a Directed Graph Library implementation
  2. * Copyright (C) 2002 Roberto Micarelli
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /* best view tabstop=4
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "opt.h"
  24. static int _ParseLongOption(GnoOption_s * pOpt, char *pszArg)
  25. {
  26. char *pszLong;
  27. char *pszPar;
  28. char *pszMatch = NULL;
  29. int nret;
  30. if (pOpt->pszLong == NULL) {
  31. return 0;
  32. }
  33. pszLong = strdup(pOpt->pszLong);
  34. if ((pszPar = strchr(pszArg, '=')) != NULL) {
  35. *pszPar = 0;
  36. }
  37. pszMatch = strdup(pszArg);
  38. if (pszPar)
  39. *pszPar++ = '=';
  40. if (strcmp(pszLong, pszMatch + 2) == 0) {
  41. /* * mandatory parameter not found
  42. * */
  43. if (pszPar == NULL) {
  44. nret = -1;
  45. goto free_and_exit;
  46. }
  47. if (pOpt->ppszValue) {
  48. if (pOpt->ppszValue[0])
  49. free(pOpt->ppszValue[0]);
  50. pOpt->ppszValue[0] = strdup(pszPar);
  51. }
  52. nret = 1;
  53. goto free_and_exit;
  54. }
  55. nret = 0;
  56. free_and_exit:
  57. free(pszLong);
  58. free(pszMatch);
  59. return nret;
  60. }
  61. static int _ParseLongSwitch(GnoOption_s * pOpt, char *pszArg)
  62. {
  63. if (pOpt->pszLong == NULL) {
  64. return 0;
  65. }
  66. if (strcmp(pOpt->pszLong, pszArg + 2) == 0) {
  67. if (pOpt->pfValue)
  68. *pOpt->pfValue = True;
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. static int _ParseShortOption(GnoOption_s * pOpt, char *pszArg, char *pszPar)
  74. {
  75. char *pszShort;
  76. int ich;
  77. if (pOpt->pszShort == NULL)
  78. return 0;
  79. pszShort = strdup(pOpt->pszShort);
  80. for (ich = 1; pszArg[ich]; ich++) {
  81. if (pszShort[0] == pszArg[ich]) {
  82. if (pszPar == NULL || pszPar[0] == 0) {
  83. free(pszShort);
  84. return -1;
  85. }
  86. if (pszPar[0] == '-' && pszPar[1] != 0) {
  87. free(pszShort);
  88. return -1;
  89. }
  90. if (pOpt->ppszValue) {
  91. if (pOpt->ppszValue[0])
  92. free(pOpt->ppszValue[0]);
  93. pOpt->ppszValue[0] = strdup(pszPar);
  94. }
  95. free(pszShort);
  96. return 2;
  97. }
  98. }
  99. free(pszShort);
  100. return 0;
  101. }
  102. static int _ParseShortSwitch(GnoOption_s * pOpt, char *pszArg)
  103. {
  104. int ich;
  105. if (pOpt->pszShort == NULL)
  106. return 0;
  107. for (ich = 1; pszArg[ich]; ich++) {
  108. if (pOpt->pszShort[0] == pszArg[ich]) {
  109. if (pOpt->pfValue)
  110. *pOpt->pfValue = True;
  111. return 1;
  112. }
  113. }
  114. return 0;
  115. }
  116. /***********************************************************************
  117. * CALLBACKS
  118. **********************************************************************/
  119. /***********************************************************************
  120. * PUBLIC FUNCTIONS
  121. **********************************************************************/
  122. /*@*--------------------------------------------------------------------
  123. * @func: GnoParse()
  124. * @descr: Parse argc, argv against the option array and setup option
  125. * values in the array.
  126. *
  127. * @args: I: argc = count of argv entries
  128. * I: argv -> array of pointer to string
  129. * I: pOpt -> option array pointer
  130. *
  131. * @ret: The number of 'orphan' entries found in the argv.
  132. * @see: GnoOption_s
  133. *
  134. * @notes: The argv array will be modified: each argv entry that contains a
  135. * recognized option ( '-.' or '--...' ) or each entry recognized as
  136. * a parametric option parameter, will be set to NULL.
  137. * Thus, at the function return the argv entries not set to NULL are
  138. * those of orphan entries (those not related to any option).
  139. * The user can then scan argv to find out orphans.
  140. * However the number of argv entries will not be altered.
  141. *
  142. *--------------------------------------------------------------------*/
  143. int GnoParse(int argc, char **argv, GnoOption_s * pOpt)
  144. {
  145. char *pszArgv;
  146. char *pszArgvNxt;
  147. int iArg, iOpt, cOrphan = 0;
  148. int nret, cret;
  149. Boolean fParseError = False;
  150. /* * this first loop setup default values
  151. * * strdup is used for non-switch options
  152. * * to make life easier when freeing the field
  153. * */
  154. for (iOpt = 0; pOpt[iOpt].pszShort || pOpt[iOpt].pszLong; iOpt++) {
  155. if (pOpt[iOpt].nFlg & GNO_FLG_SWITCH) {
  156. if (pOpt[iOpt].pfValue) {
  157. pOpt[iOpt].pfValue[0] = pOpt[iOpt].fDef;
  158. }
  159. }
  160. else {
  161. if (pOpt[iOpt].pszDef) {
  162. if (pOpt[iOpt].ppszValue) {
  163. pOpt[iOpt].ppszValue[0] = strdup(pOpt[iOpt].pszDef);
  164. }
  165. }
  166. else {
  167. if (pOpt[iOpt].ppszValue) {
  168. pOpt[iOpt].ppszValue[0] = NULL;
  169. }
  170. }
  171. }
  172. }
  173. /* * for each arg in argv lookup the matching options
  174. * */
  175. for (iArg = 0, pszArgv = NULL;
  176. iArg < argc && (pszArgv = strdup(argv[iArg])) != NULL;
  177. iArg++, free(pszArgv), pszArgv = NULL) {
  178. if (pszArgv[0] == '-' && pszArgv[1] == '-' && pszArgv[2]) { /* long style */
  179. for (iOpt = 0;
  180. (pOpt[iOpt].pszShort || pOpt[iOpt].pszLong) && argv[iArg];
  181. iOpt++) {
  182. if (pOpt[iOpt].pszLong) {
  183. if (pOpt[iOpt].nFlg & GNO_FLG_SWITCH) {
  184. nret = _ParseLongSwitch(&pOpt[iOpt], pszArgv);
  185. }
  186. else {
  187. nret = _ParseLongOption(&pOpt[iOpt], pszArgv);
  188. }
  189. if (nret < 0) {
  190. fprintf(stderr,
  191. "parse option: syntax error at <%s>\n",
  192. pszArgv);
  193. fParseError = True;
  194. }
  195. if (nret == 1) {
  196. argv[iArg] = NULL;
  197. }
  198. }
  199. }
  200. if (argv[iArg]) {
  201. fprintf(stderr, "parse option: <%s> is out of scope\n",
  202. pszArgv);
  203. fParseError = True;
  204. }
  205. }
  206. else if (argv[iArg][0] == '-' && argv[iArg][1]) { /* short style */
  207. if (iArg + 1 < argc) {
  208. pszArgvNxt = strdup(argv[iArg + 1]);
  209. }
  210. else {
  211. pszArgvNxt = NULL;
  212. }
  213. for (cret = iOpt = 0;
  214. pOpt[iOpt].pszShort || pOpt[iOpt].pszLong; iOpt++) {
  215. if (pOpt[iOpt].pszShort) {
  216. if (pOpt[iOpt].nFlg & GNO_FLG_SWITCH) {
  217. nret = _ParseShortSwitch(&pOpt[iOpt], pszArgv);
  218. }
  219. else {
  220. nret =
  221. _ParseShortOption(&pOpt[iOpt], pszArgv,
  222. pszArgvNxt);
  223. }
  224. if (nret < 0) {
  225. fprintf(stderr,
  226. "parse option: syntax error at <%s>\n",
  227. pszArgv);
  228. fParseError = True;
  229. }
  230. else {
  231. cret = (nret > cret) ? nret : cret;
  232. }
  233. }
  234. }
  235. if (pszArgvNxt) {
  236. free(pszArgvNxt);
  237. }
  238. if (cret == 1) {
  239. argv[iArg] = NULL;
  240. }
  241. else if (cret == 2) {
  242. argv[iArg++] = NULL;
  243. argv[iArg] = NULL;
  244. }
  245. }
  246. else {
  247. cOrphan++;
  248. }
  249. }
  250. if (pszArgv)
  251. free(pszArgv);
  252. return (fParseError == True) ? -1 : cOrphan;
  253. }
  254. /*@*--------------------------------------------------------------------
  255. * @func: GnoFree()
  256. * @descr: Free resource previously created with a call to GnoParse()
  257. *
  258. * @args: I: pOpt -> option array pointer
  259. *
  260. * @see: GnoOption_s, GnoParse()
  261. *
  262. *--------------------------------------------------------------------*/
  263. void GnoFree(GnoOption_s * pOpt)
  264. {
  265. int iOpt;
  266. for (iOpt = 0; pOpt[iOpt].pszShort || pOpt[iOpt].pszLong; iOpt++) {
  267. if (pOpt[iOpt].ppszValue) {
  268. if (pOpt[iOpt].ppszValue[0]) {
  269. free(pOpt[iOpt].ppszValue[0]);
  270. pOpt[iOpt].ppszValue[0] = NULL;
  271. }
  272. }
  273. }
  274. }
  275. /*@*--------------------------------------------------------------------
  276. * @func: GnoHelp()
  277. * @descr: Print a brief option's help on the standard error
  278. *
  279. * @args: I: pszHead -> help header string
  280. *
  281. * @args: I: pOpt -> option array pointer
  282. *
  283. * @see: GnoOption_s
  284. *
  285. *--------------------------------------------------------------------*/
  286. void GnoHelp(char *pszHead, GnoOption_s * pOpt)
  287. {
  288. int iOpt;
  289. fprintf(stderr, "%s\n", (pszHead) ? pszHead : "options");
  290. for (iOpt = 0; pOpt[iOpt].pszShort || pOpt[iOpt].pszLong; iOpt++) {
  291. if (pOpt[iOpt].nFlg & GNO_FLG_SWITCH) {
  292. if (pOpt[iOpt].pszShort) {
  293. fprintf(stderr, "-%s ", pOpt[iOpt].pszShort);
  294. }
  295. if (pOpt[iOpt].pszLong) {
  296. fprintf(stderr, "--%s", pOpt[iOpt].pszLong);
  297. }
  298. fprintf(stderr, "\n\t%s\n", (pOpt[iOpt].pszDescr)
  299. ? pOpt[iOpt].pszDescr : "No description available.");
  300. }
  301. else {
  302. if (pOpt[iOpt].pszShort) {
  303. fprintf(stderr, "-%s ", pOpt[iOpt].pszShort);
  304. fprintf(stderr, "<value> ");
  305. }
  306. if (pOpt[iOpt].pszLong) {
  307. fprintf(stderr, "--%s", pOpt[iOpt].pszLong);
  308. fprintf(stderr, "=<value>");
  309. }
  310. fprintf(stderr, "\n\t%s\n", (pOpt[iOpt].pszDescr)
  311. ? pOpt[iOpt].pszDescr : "No description available.");
  312. }
  313. }
  314. }
  315. /******************************* END OF FILE **************************/