libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmPgmOpts.c 34KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. #include "cmGlobal.h"
  2. #include "cmRpt.h"
  3. #include "cmErr.h"
  4. #include "cmCtx.h"
  5. #include "cmMem.h"
  6. #include "cmMallocDebug.h"
  7. #include "cmLinkedHeap.h"
  8. #include "cmPgmOpts.h"
  9. #include "cmText.h"
  10. #include "config.h"
  11. enum {
  12. kFlagPoFl = 0x001,
  13. kBoolPoFl = 0x002, // this parameter does not take a value
  14. kCharPoFl = 0x004, // parm. value is a character
  15. kIntPoFl = 0x008, // parm. value is a decimal int
  16. kUIntPoFl = 0x010, // parm. value is a decimal unsigned int
  17. kDblPoFl = 0x040, // parm. value is a decimal double
  18. kStrPoFl = 0x080, // parm. value is a string (default)
  19. kEnumPoFl = 0x100, // parm. valus is a enum type (automatically set by a non-zero enumId)
  20. kTypeMaskPoFl = 0x1ff
  21. };
  22. cmPgmOptH_t cmPgmOptNullHandle = cmSTATIC_NULL_HANDLE;
  23. struct _cmPoArg_str;
  24. typedef union
  25. {
  26. bool b;
  27. char c;
  28. int i;
  29. unsigned u;
  30. double d;
  31. const cmChar_t* s;
  32. } _cmPoValue_t;
  33. typedef union
  34. {
  35. bool* b;
  36. char* c;
  37. int* i;
  38. unsigned* u;
  39. double* d;
  40. const cmChar_t** s;
  41. } _cmPoValPtr_t;
  42. // opt records describe the a given parameters configuration
  43. typedef struct _cmPoOpt_str
  44. {
  45. unsigned numId; //
  46. cmChar_t charId; //
  47. cmChar_t* wordId; //
  48. unsigned sflags; // state flags (type flags)
  49. unsigned cflags; // cfg flags (user settable cfg flag)
  50. unsigned enumId; // enumerated set element id (numId is the group id)
  51. struct _cmPoOpt_str* enumPtr; // pointer to mast enum recd (the master is the first param. rcvd for a given enum set - so this field is NULL for the master record)
  52. unsigned maxInstCnt; //
  53. _cmPoValue_t dfltVal; // default value for this parm
  54. _cmPoValPtr_t retVal; // client supplied variable which recieves the value of the last arg. parsed for this parm.
  55. cmChar_t* helpStr; //
  56. cmChar_t* mstrStr;
  57. struct _cmPoOpt_str* link; // link used by the _cmPo_t.list linked list
  58. struct _cmPoArg_str* inst; // arg's belonging to this opt record formed by _cmPoArg_t.inst links
  59. } _cmPoOpt_t;
  60. // arg records describe an instance of a given parameter.
  61. // (there may be multiple instances of a given parameter)
  62. typedef struct _cmPoArg_str
  63. {
  64. const _cmPoOpt_t* opt; // the opt record associated with this arg. instance
  65. struct _cmPoArg_str* link; // link used by the _cmPo_t.args list
  66. struct _cmPoArg_str* inst; // link used by the _cmPoOpt_t* inst list
  67. const cmChar_t* valStr; // string value for this arg.
  68. _cmPoValue_t u; // parsed value for this arg.
  69. } _cmPoArg_t;
  70. typedef struct
  71. {
  72. cmErr_t err;
  73. cmLHeapH_t lH;
  74. cmChar_t* helpBegStr;
  75. cmChar_t* helpEndStr;
  76. _cmPoOpt_t* list; // list of control records formed by _cmPoOpt_t.link links
  77. _cmPoArg_t* args; // list of arg. records formed by _cmPoArg_t.link links
  78. bool execFl; // set to false in cmPgmOptParse() if only built-in options were selected
  79. } _cmPo_t;
  80. typedef struct
  81. {
  82. unsigned id;
  83. const cmChar_t* label;
  84. } _cmPoMap_t;
  85. _cmPoMap_t _cmPoTypeMap[] =
  86. {
  87. { kFlagPoFl, "" },
  88. { kBoolPoFl, "" },
  89. { kCharPoFl, "<char>" },
  90. { kIntPoFl, "<int>" },
  91. { kUIntPoFl, "<uint>" },
  92. { kDblPoFl, "<real>" },
  93. { kStrPoFl, "<text>" },
  94. { kEnumPoFl, "<enum>" },
  95. { 0, "<unknown>"}
  96. };
  97. const cmChar_t* _cmPoTypeFlagToLabel( unsigned flag, unsigned cflags )
  98. {
  99. if( cmIsFlag(cflags,kHexPoFl) )
  100. return "<hex>";
  101. const _cmPoMap_t* m = _cmPoTypeMap;
  102. for(; m->id != 0; ++m)
  103. if( m->id == flag )
  104. break;
  105. return m->label;
  106. }
  107. bool _cmPoOptUsesValue( const _cmPoOpt_t* r )
  108. {
  109. return
  110. cmIsNotFlag(r->sflags,kEnumPoFl) &&
  111. cmIsNotFlag(r->sflags,kBoolPoFl) &&
  112. cmIsNotFlag(r->sflags,kFlagPoFl);
  113. }
  114. _cmPo_t* _cmPoHandleToPtr( cmPgmOptH_t h )
  115. {
  116. _cmPo_t* p = (_cmPo_t*)h.h;
  117. assert(p != NULL );
  118. return p;
  119. }
  120. cmPoRC_t _cmPgmOptFinalize( _cmPo_t* p )
  121. {
  122. cmPoRC_t rc = kOkPoRC;
  123. cmLHeapDestroy(&p->lH);
  124. cmMemPtrFree(&p);
  125. return rc;
  126. }
  127. bool cmPgmOptIsValid( cmPgmOptH_t h )
  128. { return h.h != NULL; }
  129. cmPoRC_t cmPgmOptInitialize(cmCtx_t* c, cmPgmOptH_t* hp, const cmChar_t* helpBegStr, const cmChar_t* helpEndStr )
  130. {
  131. cmPoRC_t rc;
  132. if((rc = cmPgmOptFinalize(hp)) != kOkPoRC )
  133. return rc;
  134. _cmPo_t* p = cmMemAllocZ(_cmPo_t,1);
  135. cmErrSetup(&p->err,&c->rpt,"Program Options");
  136. p->lH = cmLHeapCreate(2048,c);
  137. p->helpBegStr = helpBegStr==NULL ? NULL : cmLhAllocStr( p->lH, helpBegStr );
  138. p->helpEndStr = helpEndStr==NULL ? NULL : cmLhAllocStr( p->lH, helpEndStr );
  139. hp->h = p;
  140. return cmErrLastRC(&p->err);
  141. }
  142. cmPoRC_t cmPgmOptFinalize( cmPgmOptH_t* hp )
  143. {
  144. cmPoRC_t rc = kOkPoRC;
  145. if( hp == NULL || cmPgmOptIsValid(*hp) == false )
  146. return kOkPoRC;
  147. _cmPo_t* p = _cmPoHandleToPtr(*hp);
  148. if((rc = _cmPgmOptFinalize(p)) != kOkPoRC )
  149. return rc;
  150. hp->h = NULL;
  151. return rc;
  152. }
  153. _cmPoOpt_t* _cmPgmOptNumIdToOptRecd( _cmPo_t* p, unsigned numId )
  154. {
  155. _cmPoOpt_t* r = p->list;
  156. while( r != NULL )
  157. {
  158. if( r->numId == numId )
  159. {
  160. if( cmIsFlag(r->sflags,kEnumPoFl) && r->enumPtr!=NULL )
  161. r = r->enumPtr;
  162. return r;
  163. }
  164. r = r->link;
  165. }
  166. return NULL;
  167. }
  168. _cmPoOpt_t* _cmPgmOptEnumIdToOptRecd( _cmPo_t* p, unsigned numId, unsigned enumId )
  169. {
  170. _cmPoOpt_t* r = p->list;
  171. while( r != NULL )
  172. {
  173. if( r->numId == numId && r->enumId == enumId )
  174. return r;
  175. r = r->link;
  176. }
  177. return NULL;
  178. }
  179. _cmPoOpt_t* _cmPgmOptCharIdToOptRecd( _cmPo_t* p, cmChar_t charId )
  180. {
  181. _cmPoOpt_t* r = p->list;
  182. while( r != NULL )
  183. {
  184. if( r->charId == charId )
  185. return r;
  186. r = r->link;
  187. }
  188. return NULL;
  189. }
  190. _cmPoOpt_t* _cmPgmOptWordIdToOptRecd( _cmPo_t* p, const cmChar_t* wordId )
  191. {
  192. _cmPoOpt_t* r = p->list;
  193. while( r != NULL )
  194. {
  195. if( strcmp(wordId,r->wordId) == 0 )
  196. return r;
  197. r = r->link;
  198. }
  199. return NULL;
  200. }
  201. cmPoRC_t _cmPgmOptInstall( _cmPo_t* p, unsigned numId, const cmChar_t charId, const cmChar_t* wordId, unsigned cflags, unsigned sflags, unsigned enumId, unsigned cnt, const cmChar_t* helpStr, _cmPoOpt_t** rpp )
  202. {
  203. // validate the num. id
  204. if( cmIsNotFlag(sflags,kEnumPoFl) && _cmPgmOptNumIdToOptRecd(p,numId) != NULL )
  205. return cmErrMsg(&p->err,kDuplicateIdPoRC,"The numeric id '%i' was already used by another parameter.",numId);
  206. // validate the char. id
  207. if( _cmPgmOptCharIdToOptRecd(p,charId) != NULL )
  208. return cmErrMsg(&p->err,kDuplicateIdPoRC,"The character id -'%c' was already used by another parameter.",charId);
  209. // validate the word. id
  210. if( _cmPgmOptWordIdToOptRecd(p,wordId) != NULL )
  211. return cmErrMsg(&p->err,kDuplicateIdPoRC,"The word id --'%s' was already used by another parameter.",wordId);
  212. // clear the kHexPoFl if this is not an int or uint.
  213. if( cmIsNotFlag(sflags,kFlagPoFl) && cmIsNotFlag(sflags,kIntPoFl) && cmIsNotFlag(sflags,kUIntPoFl) )
  214. cflags = cmClrFlag(cflags,kHexPoFl);
  215. // allocate the new parm recd
  216. _cmPoOpt_t* r = cmLhAllocZ( p->lH, _cmPoOpt_t, 1 );
  217. // if this is an enum type.
  218. if( cmIsFlag(sflags,kEnumPoFl) )
  219. {
  220. sflags = cmClrFlag(sflags,kTypeMaskPoFl) | kEnumPoFl;
  221. // set the master recd for this enum ptr
  222. _cmPoOpt_t* erp;
  223. if((erp = _cmPgmOptNumIdToOptRecd(p,numId)) != NULL )
  224. {
  225. r->enumPtr = erp->enumPtr==NULL ? erp : erp->enumPtr;
  226. // if this child enum has it's required flags set
  227. if( cmIsFlag(cflags,kReqPoFl) )
  228. {
  229. // then set the required flag in the parent and clear it in the child
  230. // (this way both the parent and child will not be required (which would be impossible for an enum))
  231. r->enumPtr->cflags = cmSetFlag(r->enumPtr->cflags,kReqPoFl);
  232. cflags = cmClrFlag(cflags,kReqPoFl);
  233. }
  234. }
  235. }
  236. r->cflags = cflags;
  237. r->sflags = sflags;
  238. r->numId = numId;
  239. r->charId = charId;
  240. r->wordId = cmLhAllocStr( p->lH, wordId );
  241. r->enumId = enumId;
  242. r->maxInstCnt = cnt;
  243. r->helpStr = helpStr==NULL ? NULL : cmLhAllocStr( p->lH, helpStr );
  244. _cmPoOpt_t* rp = p->list;
  245. while( rp != NULL && rp->link != NULL )
  246. rp = rp->link;
  247. if( rp == NULL )
  248. p->list = r;
  249. else
  250. rp->link = r;
  251. //r->link = p->list;
  252. //p->list = r;
  253. *rpp = r;
  254. return kOkPoRC;
  255. }
  256. cmPoRC_t cmPgmOptInstallChar(cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* wordId, unsigned flags, cmChar_t dfltVal, cmChar_t* retValPtr, unsigned cnt, const cmChar_t* helpStr )
  257. {
  258. cmPoRC_t rc;
  259. _cmPoOpt_t* r = NULL;
  260. _cmPo_t* p = _cmPoHandleToPtr(h);
  261. if((rc= _cmPgmOptInstall(p, numId, charId, wordId, flags, kCharPoFl, 0, cnt, helpStr, &r )) != kOkPoRC )
  262. return rc;
  263. r->dfltVal.c = dfltVal;
  264. if( retValPtr != NULL )
  265. {
  266. r->retVal.c = retValPtr;
  267. *r->retVal.c = dfltVal;
  268. }
  269. return rc;
  270. }
  271. cmPoRC_t cmPgmOptInstallBool(cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* wordId, unsigned flags, bool dfltVal, bool* retValPtr, unsigned cnt, const cmChar_t* helpStr )
  272. {
  273. cmPoRC_t rc;
  274. _cmPoOpt_t* r = NULL;
  275. _cmPo_t* p = _cmPoHandleToPtr(h);
  276. if((rc= _cmPgmOptInstall(p, numId, charId, wordId, flags, kBoolPoFl, 0, cnt, helpStr, &r )) != kOkPoRC )
  277. return rc;
  278. r->dfltVal.b = dfltVal;
  279. if( retValPtr != NULL )
  280. {
  281. r->retVal.b = retValPtr;
  282. *r->retVal.b = dfltVal;
  283. }
  284. return rc;
  285. }
  286. cmPoRC_t cmPgmOptInstallInt( cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* wordId, unsigned flags, int dfltVal, int* retValPtr, unsigned cnt, const cmChar_t* helpStr )
  287. {
  288. cmPoRC_t rc;
  289. _cmPoOpt_t* r = NULL;
  290. _cmPo_t* p = _cmPoHandleToPtr(h);
  291. if((rc= _cmPgmOptInstall(p, numId, charId, wordId, flags, kIntPoFl, 0, cnt, helpStr, &r )) != kOkPoRC )
  292. return rc;
  293. r->dfltVal.i = dfltVal;
  294. if( retValPtr != NULL )
  295. {
  296. r->retVal.i = retValPtr;
  297. *r->retVal.i = dfltVal;
  298. }
  299. return rc;
  300. }
  301. cmPoRC_t cmPgmOptInstallUInt(cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* wordId, unsigned flags, unsigned dfltVal, unsigned* retValPtr, unsigned cnt, const cmChar_t* helpStr )
  302. {
  303. cmPoRC_t rc;
  304. _cmPoOpt_t* r = NULL;
  305. _cmPo_t* p = _cmPoHandleToPtr(h);
  306. if((rc= _cmPgmOptInstall(p, numId, charId, wordId, flags, kUIntPoFl, 0, cnt, helpStr, &r )) != kOkPoRC )
  307. return rc;
  308. r->dfltVal.u = dfltVal;
  309. if( retValPtr != NULL )
  310. {
  311. r->retVal.u = retValPtr;
  312. *r->retVal.u = dfltVal;
  313. }
  314. return rc;
  315. }
  316. cmPoRC_t cmPgmOptInstallDbl( cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* wordId, unsigned flags, double dfltVal, double* retValPtr, unsigned cnt, const cmChar_t* helpStr )
  317. {
  318. cmPoRC_t rc;
  319. _cmPoOpt_t* r = NULL;
  320. _cmPo_t* p = _cmPoHandleToPtr(h);
  321. if((rc= _cmPgmOptInstall(p, numId, charId, wordId, flags, kDblPoFl, 0, cnt, helpStr, &r )) != kOkPoRC )
  322. return rc;
  323. r->dfltVal.d = dfltVal;
  324. if( retValPtr != NULL )
  325. {
  326. r->retVal.d = retValPtr;
  327. *r->retVal.d = dfltVal;
  328. }
  329. return rc;
  330. }
  331. cmPoRC_t cmPgmOptInstallStr( cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* wordId, unsigned flags, const cmChar_t* dfltVal, const cmChar_t** retValPtr, unsigned cnt, const cmChar_t* helpStr )
  332. {
  333. cmPoRC_t rc;
  334. _cmPoOpt_t* r = NULL;
  335. _cmPo_t* p = _cmPoHandleToPtr(h);
  336. if((rc= _cmPgmOptInstall(p, numId, charId, wordId, flags, kStrPoFl, 0, cnt, helpStr, &r )) != kOkPoRC )
  337. return rc;
  338. r->dfltVal.s = dfltVal;
  339. if( retValPtr != NULL )
  340. {
  341. r->retVal.s = retValPtr;
  342. *r->retVal.s = dfltVal;
  343. }
  344. return rc;
  345. }
  346. cmPoRC_t cmPgmOptInstallEnum(cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* wordId, unsigned flags, unsigned enumId, unsigned dfltVal, unsigned* retValPtr, unsigned cnt, const cmChar_t* helpStr, const cmChar_t* mstrHelpStr )
  347. {
  348. cmPoRC_t rc;
  349. _cmPoOpt_t* r = NULL;
  350. _cmPo_t* p = _cmPoHandleToPtr(h);
  351. if((rc= _cmPgmOptInstall(p, numId, charId, wordId, flags, kEnumPoFl, enumId, cnt, helpStr, &r )) != kOkPoRC )
  352. return rc;
  353. r->dfltVal.u = dfltVal;
  354. if( retValPtr != NULL )
  355. {
  356. r->retVal.u = retValPtr;
  357. *r->retVal.u = dfltVal;
  358. }
  359. if( mstrHelpStr != NULL )
  360. {
  361. _cmPoOpt_t* rp = r->enumPtr == NULL ? r : r->enumPtr;
  362. rp->mstrStr = cmLhAllocStr(p->lH,mstrHelpStr);
  363. }
  364. return rc;
  365. }
  366. cmPoRC_t cmPgmOptInstallFlag(cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* wordId, unsigned flags, unsigned dfltVal, unsigned* retValPtr, unsigned cnt, const cmChar_t* helpStr )
  367. {
  368. cmPoRC_t rc;
  369. _cmPoOpt_t* r = NULL;
  370. _cmPo_t* p = _cmPoHandleToPtr(h);
  371. if((rc= _cmPgmOptInstall(p, numId, charId, wordId, flags, kFlagPoFl, 0, cnt, helpStr, &r )) != kOkPoRC )
  372. return rc;
  373. r->dfltVal.u = dfltVal;
  374. if( retValPtr != NULL )
  375. {
  376. r->retVal.u = retValPtr;
  377. // Unlike other parameters we don't set the initial value of flag parameter
  378. // because we don't want to destroy any default flag that may be set there
  379. }
  380. return rc;
  381. }
  382. _cmPoArg_t* _cmPgmOptInsertArg( _cmPo_t* p, _cmPoOpt_t* r )
  383. {
  384. _cmPoArg_t* a = cmLhAllocZ(p->lH, _cmPoArg_t, 1);
  385. // if this is an enumerated type then switch to the master enum recd
  386. unsigned enumId = r->enumId;
  387. _cmPoOpt_t* e = r;
  388. if( r->enumPtr != NULL )
  389. r = r->enumPtr;
  390. a->opt = r;
  391. a->valStr = NULL;
  392. a->link = p->args; // link into master arg list
  393. p->args = a;
  394. a->inst = r->inst; // link into opt recd list
  395. r->inst = a;
  396. // if no parm. type flag was given then the arg is implicitely a bool and the value is true.
  397. //if( (r->flags & kTypeMaskPoFl) == 0 )
  398. // a->u.b = true;
  399. // if this is an enumerated type
  400. if( cmIsFlag(r->sflags,kEnumPoFl) )
  401. {
  402. a->u.u = enumId;
  403. if( r->retVal.u != NULL )
  404. *r->retVal.u = enumId;
  405. if( e->retVal.u != NULL )
  406. *e->retVal.u = enumId;
  407. }
  408. // if this is a flag type
  409. if( cmIsFlag(r->sflags,kFlagPoFl) )
  410. {
  411. a->u.u = r->dfltVal.u;
  412. if( r->retVal.u != NULL )
  413. *r->retVal.u |= a->u.u;
  414. }
  415. return a;
  416. }
  417. cmPoRC_t _cmPgmOptParseBool(_cmPo_t* p, const cmChar_t* valStr, bool* retValPtr )
  418. {
  419. cmPoRC_t rc = kOkPoRC;
  420. unsigned n = strlen("false");
  421. char val[n+1];
  422. int i;
  423. for(i=0; i<n && valStr[i]; ++i)
  424. val[i] = tolower(valStr[i]);
  425. val[i] = 0;
  426. if( !strcmp(val,"0") || !strcmp(val,"false") || !strcmp(val,"f") )
  427. *retValPtr = false;
  428. else
  429. if( !strcmp(val,"1") || !strcmp(val,"true") || !strcmp(val,"t") )
  430. *retValPtr = true;
  431. else
  432. rc = cmErrMsg(&p->err,kParseFailPoRC,"The argument '%s' could not be parsed into a bool. Legal bool values are 0,F,FALSE,1,T,TRUE (case insensitive).",valStr);
  433. return rc;
  434. }
  435. cmPoRC_t _cmPgmOptParseValue( _cmPo_t* p, _cmPoOpt_t* r, _cmPoArg_t* a, const cmChar_t* valStr )
  436. {
  437. cmPoRC_t rc = kOkPoRC;
  438. assert(a != NULL && valStr != NULL);
  439. a->valStr = valStr;
  440. errno = 0;
  441. switch( r->sflags & kTypeMaskPoFl )
  442. {
  443. // case kFlagPoFl:
  444. // enum values get set in _cmPgmOptInsertArg()
  445. case kBoolPoFl:
  446. //rc = _cmPgmOptParseBool(p,valStr,&a->u.b);
  447. a->u.b = true;
  448. if( r->retVal.b != NULL )
  449. *r->retVal.b = a->u.b;
  450. break;
  451. case kCharPoFl:
  452. a->u.c = valStr[0];
  453. if( r->retVal.c != NULL )
  454. *r->retVal.c = a->u.c;
  455. break;
  456. case kIntPoFl:
  457. a->u.i = strtol(valStr,NULL, cmIsFlag(r->cflags,kHexPoFl) ? 16 : 10);
  458. if( r->retVal.i != NULL )
  459. *r->retVal.i = a->u.i;
  460. break;
  461. case kUIntPoFl:
  462. a->u.u = strtol(valStr,NULL, cmIsFlag(r->cflags,kHexPoFl) ? 16 : 10);
  463. if( r->retVal.u != NULL )
  464. *r->retVal.u = a->u.u;
  465. break;
  466. // case kEnumPoFl:
  467. // enum values get set in _cmPgmOptInsertArg()
  468. case kDblPoFl:
  469. a->u.d = strtod(valStr,NULL);
  470. if( r->retVal.d != NULL )
  471. *r->retVal.d = a->u.d;
  472. break;
  473. case kStrPoFl:
  474. a->u.s = valStr;
  475. if( r->retVal.s != NULL )
  476. *r->retVal.s = a->u.s;
  477. break;
  478. default:
  479. { assert(0); }
  480. }
  481. if( errno != 0 )
  482. rc = cmErrMsg(&p->err,kParseFailPoRC,"The value '%s' for option '%s' could not be parsed.",valStr,r->wordId);
  483. return rc;
  484. }
  485. unsigned _cmPgmOptInstCount( const _cmPoOpt_t* r )
  486. {
  487. const _cmPoArg_t* a = r->inst;
  488. unsigned n = 0;
  489. while( a != NULL )
  490. {
  491. ++n;
  492. a = a->inst;
  493. }
  494. return n;
  495. }
  496. // check if any non-built-in options were provided
  497. // If only built in options were selected then no execution is assumed
  498. // and required arg's are not checked.
  499. bool _cmPgmOptCheckNoExec( _cmPo_t* p )
  500. {
  501. _cmPoArg_t* a = p->args;
  502. while( a != NULL )
  503. {
  504. if( a->opt->numId >= kBasePoId )
  505. return true;
  506. a = a->link;
  507. }
  508. return false;
  509. }
  510. cmPoRC_t _cmPgmOptMissingEnumError( _cmPo_t* p, _cmPoOpt_t* r )
  511. {
  512. _cmPoOpt_t* mstr = r->enumPtr==NULL ? r : r->enumPtr;
  513. _cmPoOpt_t* rp = p->list;
  514. cmChar_t* msg = cmTsPrintfP(NULL,"No argument was supplied for the required enumerated parameter containing the following set of possible flags: { ");
  515. for(; rp!=NULL; rp=rp->link)
  516. if( cmIsFlag(rp->sflags,kEnumPoFl) && (rp==mstr || rp->enumPtr==mstr) )
  517. {
  518. msg = cmTextAppendSS(msg,rp->wordId);
  519. msg = cmTextAppendSS(msg," ");
  520. }
  521. msg = cmTextAppendSS(msg,"}");
  522. cmPoRC_t rc = cmErrMsg(&p->err,kNoReqArgPoRC,msg);
  523. cmMemFree(msg);
  524. return rc;
  525. }
  526. // check that all required arg.s were actually given and that the actual
  527. // number of instances does not exceed the defined limit
  528. cmPoRC_t _cmPgmOptCheckReqArgs( _cmPo_t* p )
  529. {
  530. cmPoRC_t rc = kOkPoRC;
  531. _cmPoOpt_t* r = p->list;
  532. while( r != NULL )
  533. {
  534. if( cmIsFlag(r->cflags, kReqPoFl ) )
  535. {
  536. _cmPoArg_t* a = p->args;
  537. while( a != NULL )
  538. {
  539. if( a->opt == r )
  540. break;
  541. a = a->link;
  542. }
  543. if( a == NULL )
  544. {
  545. if( cmIsFlag(r->sflags, kEnumPoFl ) )
  546. rc = _cmPgmOptMissingEnumError(p,r);
  547. else
  548. rc = cmErrMsg(&p->err,kNoReqArgPoRC,"No argument was supplied for the required parameter '%s'.",r->wordId);
  549. }
  550. }
  551. unsigned cnt;
  552. if( r->maxInstCnt > 0 && r->maxInstCnt < (cnt=_cmPgmOptInstCount(r)) )
  553. rc = cmErrMsg(&p->err,kArgCntErrPoRC,"The parameter '%s' has %i instances which exceeds the defined limit of %i instances.",r->wordId,cnt,r->maxInstCnt);
  554. r = r->link;
  555. }
  556. return rc;
  557. }
  558. cmPoRC_t cmPgmOptParse( cmPgmOptH_t h, unsigned argCnt, char* argArray[] )
  559. {
  560. enum { kDash, kCharId, kWordId, kArgVal };
  561. cmPoRC_t rc = kOkPoRC;
  562. _cmPo_t* p = _cmPoHandleToPtr(h);
  563. unsigned state = kDash;
  564. _cmPoOpt_t* r = NULL;
  565. _cmPoArg_t* a = NULL;
  566. int i = 0; // arg index
  567. int j = 0; // arg label character index
  568. if( _cmPgmOptNumIdToOptRecd(p,kPrintHelpPoId) == NULL )
  569. cmPgmOptInstallBool(h, kPrintHelpPoId, 'h', "help", 0, false, NULL,0,"Print this usage information." );
  570. if( _cmPgmOptNumIdToOptRecd(p,kVersionPoId) == NULL )
  571. cmPgmOptInstallBool(h, kVersionPoId, 'v', "version", 0, false, NULL,0,"Print version information." );
  572. if( _cmPgmOptNumIdToOptRecd(p,kPrintParmsPoId) == NULL )
  573. cmPgmOptInstallBool(h, kPrintParmsPoId,'p', "parms", 0, false, NULL,0,"Print the arguments.");
  574. while(i<argCnt)
  575. {
  576. const cmChar_t* valStr = NULL;
  577. switch(state)
  578. {
  579. case kDash:
  580. // this token must begin with a '-' character
  581. if( argArray[i][0] != '-')
  582. return cmErrMsg(&p->err,kSyntaxErrPoRC,"Syntax error. Expecting a '-'.");
  583. // if the second char. is also a '-' then this is a wordId
  584. if( argArray[i][1] == '-' )
  585. {
  586. state = kWordId;
  587. j = 2; // word id's always begin on the third token char
  588. }
  589. else // otherwise it is a charId
  590. {
  591. state = kCharId;
  592. j = 1; // char id's always begin on the second token char
  593. }
  594. r = NULL;
  595. break;
  596. case kCharId:
  597. // we complete our parsing of charId's only when we encounter a '\0'
  598. if( argArray[i][j] == '\0' )
  599. {
  600. ++i;
  601. state = kDash;
  602. r = NULL;
  603. a = NULL;
  604. }
  605. else
  606. {
  607. // get the opt recd assoc'd with the jth character in this charId
  608. if((r = _cmPgmOptCharIdToOptRecd(p,argArray[i][j])) == NULL )
  609. return cmErrMsg(&p->err,kSyntaxErrPoRC,"The program option selector char '%c' is not valid.",argArray[i][j]);
  610. // if this charId is not a flag,bool or enum then it must be followed by a value.
  611. if( _cmPoOptUsesValue(r) )
  612. ++i;
  613. else // otherwise process the next char id in this charId token
  614. ++j;
  615. }
  616. break;
  617. case kWordId:
  618. // get the wordId
  619. if((r = _cmPgmOptWordIdToOptRecd(p,argArray[i]+j)) == NULL )
  620. return cmErrMsg(&p->err,kSyntaxErrPoRC,"The program option selector word '%s' is not valid.",cmStringNullGuard(argArray[i]+j));
  621. ++i; // advance to the next token
  622. break;
  623. case kArgVal:
  624. assert(r != NULL );
  625. valStr = argArray[i];
  626. ++i;
  627. break;
  628. }
  629. // if a valid opt recd has been selected
  630. if( r != NULL )
  631. {
  632. // if the cur token reprsents a value ...
  633. if( state == kArgVal )
  634. {
  635. // ... then parse the value string
  636. if((rc = _cmPgmOptParseValue(p,r,a,valStr)) != kOkPoRC )
  637. goto errLabel;
  638. r = NULL;
  639. a = NULL;
  640. state = kDash; // value strings are always followed by char or word id's
  641. }
  642. else
  643. {
  644. // create an arg record for the cur char or word id.
  645. a = _cmPgmOptInsertArg(p, r );
  646. // and the value type is not 'flag','bool' or 'enum'
  647. if( _cmPoOptUsesValue(r) )
  648. state = kArgVal;
  649. switch(state)
  650. {
  651. case kDash: assert(i==argCnt); break;
  652. case kCharId: break;
  653. case kWordId: state = kDash; break;
  654. case kArgVal: break;
  655. default: assert(0); break;
  656. }
  657. }
  658. }
  659. }
  660. errLabel:
  661. // if no errors have occurred then check for missing parameters.
  662. if( rc == kOkPoRC )
  663. {
  664. if((p->execFl = _cmPgmOptCheckNoExec(p)) == true )
  665. rc = _cmPgmOptCheckReqArgs(p);
  666. }
  667. return rc;
  668. }
  669. unsigned cmPgmOptArgCount( cmPgmOptH_t h)
  670. {
  671. _cmPo_t* p = _cmPoHandleToPtr(h);
  672. const _cmPoArg_t* a = p->args;
  673. unsigned n = 0;
  674. while( a != NULL )
  675. {
  676. ++n;
  677. a = a->link;
  678. }
  679. return n;
  680. }
  681. cmPoRC_t _cmPgmOptIndexToPtr( _cmPo_t* p, unsigned idx, const _cmPoArg_t** app )
  682. {
  683. unsigned n = 0;
  684. const _cmPoArg_t* a = p->args;
  685. while( a != NULL && n < idx )
  686. {
  687. ++n;
  688. a = a->link;
  689. }
  690. if( n!=idx || a==NULL)
  691. return cmErrMsg(&p->err,kInvalidIdxPoRC,"The option index '%i' is not valid.",idx);
  692. *app = a;
  693. return kOkPoRC;
  694. }
  695. unsigned cmPgmOptNumId( cmPgmOptH_t h, unsigned argIdx )
  696. {
  697. const _cmPoArg_t* a;
  698. cmPoRC_t rc;
  699. if((rc = _cmPgmOptIndexToPtr(_cmPoHandleToPtr(h),argIdx,&a)) != kOkPoRC )
  700. return cmInvalidId;
  701. return a->opt->numId;
  702. }
  703. unsigned cmPgmOptCharId( cmPgmOptH_t h, unsigned argIdx )
  704. {
  705. const _cmPoArg_t* a;
  706. cmPoRC_t rc;
  707. if((rc = _cmPgmOptIndexToPtr(_cmPoHandleToPtr(h),argIdx,&a)) != kOkPoRC )
  708. return cmInvalidId;
  709. return a->opt->charId;
  710. }
  711. const cmChar_t* cmPgmOptWordId( cmPgmOptH_t h, unsigned argIdx )
  712. {
  713. const _cmPoArg_t* a;
  714. cmPoRC_t rc;
  715. if((rc = _cmPgmOptIndexToPtr(_cmPoHandleToPtr(h),argIdx,&a)) != kOkPoRC )
  716. return NULL;
  717. return a->opt->wordId;
  718. }
  719. cmPoRC_t _cmPgmOptArgPtr( _cmPo_t* p, unsigned argIdx, const _cmPoArg_t** app )
  720. {
  721. cmPoRC_t rc;
  722. if((rc = _cmPgmOptIndexToPtr(p,argIdx,app)) != kOkPoRC )
  723. {
  724. if( (*app)->valStr == NULL )
  725. return cmErrMsg(&p->err,kParseFailPoRC,"The parameter at index '%i' is not a value.",argIdx);
  726. }
  727. return rc;
  728. }
  729. char cmPgmOptParseArgChar( cmPgmOptH_t h, unsigned argIdx )
  730. {
  731. cmPoRC_t rc;
  732. const _cmPoArg_t* a;
  733. _cmPo_t* p = _cmPoHandleToPtr(h);
  734. if((rc = _cmPgmOptArgPtr(p,argIdx,&a)) != kOkPoRC )
  735. return 0;
  736. return a->valStr==NULL ? 0 : a->valStr[0];
  737. }
  738. bool cmPgmOptParseArgBool( cmPgmOptH_t h, unsigned argIdx )
  739. {
  740. cmPoRC_t rc;
  741. const _cmPoArg_t* a;
  742. _cmPo_t* p = _cmPoHandleToPtr(h);
  743. bool val = false;
  744. if((rc = _cmPgmOptArgPtr(p,argIdx,&a)) != kOkPoRC )
  745. return false;
  746. if( a->valStr != NULL )
  747. _cmPgmOptParseBool(p,a->valStr,&val);
  748. return val;
  749. }
  750. int cmPgmOptParseArgInt( cmPgmOptH_t h, unsigned argIdx )
  751. {
  752. cmPoRC_t rc;
  753. const _cmPoArg_t* a;
  754. _cmPo_t* p = _cmPoHandleToPtr(h);
  755. if((rc = _cmPgmOptArgPtr(p,argIdx,&a)) != kOkPoRC )
  756. return rc;
  757. errno = 0;
  758. int v = strtol(a->valStr,NULL,10);
  759. if( errno != 0 )
  760. cmErrMsg(&p->err,kParseFailPoRC,"The parameter '%s' could not be converted to an 'int'.",a->valStr);
  761. return v;
  762. }
  763. unsigned cmPgmOptParseArgUInt( cmPgmOptH_t h, unsigned argIdx )
  764. {
  765. cmPoRC_t rc;
  766. const _cmPoArg_t* a;
  767. _cmPo_t* p = _cmPoHandleToPtr(h);
  768. if((rc = _cmPgmOptArgPtr(p,argIdx,&a)) != kOkPoRC )
  769. return rc;
  770. errno = 0;
  771. unsigned v = strtol(a->valStr,NULL,10);
  772. if( errno != 0 )
  773. cmErrMsg(&p->err,kParseFailPoRC,"The parameter '%s' could not be converted to an 'unsigned int'.",a->valStr);
  774. return v;
  775. }
  776. double cmPgmOptParseArgDbl( cmPgmOptH_t h, unsigned argIdx )
  777. {
  778. cmPoRC_t rc;
  779. const _cmPoArg_t* a;
  780. _cmPo_t* p = _cmPoHandleToPtr(h);
  781. if((rc = _cmPgmOptArgPtr(p,argIdx,&a)) != kOkPoRC )
  782. return rc;
  783. errno = 0;
  784. double v = strtod(a->valStr,NULL);
  785. if( errno != 0 )
  786. cmErrMsg(&p->err,kParseFailPoRC,"The parameter '%s' could not be converted to a 'double'.",a->valStr);
  787. return v;
  788. }
  789. const char* cmPgmOptParseArgStr( cmPgmOptH_t h, unsigned argIdx )
  790. {
  791. cmPoRC_t rc;
  792. const _cmPoArg_t* a;
  793. _cmPo_t* p = _cmPoHandleToPtr(h);
  794. if((rc = _cmPgmOptArgPtr(p,argIdx,&a)) != kOkPoRC )
  795. return NULL;
  796. return a->valStr;
  797. }
  798. unsigned cmPgmOptParmArgCount( cmPgmOptH_t h, unsigned numId )
  799. {
  800. const _cmPoOpt_t* r;
  801. _cmPo_t* p = _cmPoHandleToPtr(h);
  802. unsigned n = 0;
  803. if((r = _cmPgmOptNumIdToOptRecd(p,numId)) == NULL )
  804. cmErrMsg(&p->err,kParmNotFoundPoRC,"No parameter definition was found for numeric id %i.",numId);
  805. else
  806. n = _cmPgmOptInstCount(r);
  807. return n;
  808. }
  809. cmPoRC_t _cmPgmOptInstPtr( _cmPo_t* p, unsigned numId, unsigned instIdx, const _cmPoOpt_t** rpp, const _cmPoValue_t** vpp )
  810. {
  811. *rpp = NULL;
  812. *vpp = NULL;
  813. // locate the opt recd
  814. if((*rpp = _cmPgmOptNumIdToOptRecd(p,numId)) == NULL )
  815. return cmErrMsg(&p->err,kParmNotFoundPoRC,"No parameter definition was found for the numeric id %i.",numId);
  816. const _cmPoArg_t* a = (*rpp)->inst;
  817. unsigned i;
  818. // locate the instance recd
  819. for(i=0; i<instIdx && a != NULL; ++i)
  820. a = a->inst;
  821. // if the instance recd was not found
  822. if( i != instIdx || a == NULL )
  823. {
  824. // if the instance index is 0 then and no instance recd exists then
  825. // we will return the default value.
  826. if( instIdx == 0 )
  827. {
  828. *vpp = &((*rpp)->dfltVal);
  829. return kOkPoRC;
  830. }
  831. // otherwise signal an error
  832. return cmErrMsg(&p->err,kInstNotFoundPoRC,"The instance index %i is not valid for parameter %s instance count %i.",instIdx,(*rpp)->wordId,i);
  833. }
  834. *vpp = &a->u;
  835. return kOkPoRC;
  836. }
  837. char cmPgmOptArgChar( cmPgmOptH_t h, unsigned numId, unsigned instIdx )
  838. {
  839. const _cmPoOpt_t* rp = NULL;
  840. const _cmPoValue_t* vp = NULL;
  841. _cmPo_t* p = _cmPoHandleToPtr(h);
  842. if(_cmPgmOptInstPtr(p,numId,instIdx,&rp,&vp) != kOkPoRC )
  843. return 0;
  844. if( cmIsFlag(rp->sflags,kCharPoFl) )
  845. {
  846. cmErrMsg(&p->err,kTypeErrPoRC,"The parameter '%s' is not a 'char'.",rp->wordId);
  847. return 0;
  848. }
  849. return vp->c;
  850. }
  851. bool cmPgmOptArgBool( cmPgmOptH_t h, unsigned numId, unsigned instIdx )
  852. {
  853. const _cmPoOpt_t* rp = NULL;
  854. const _cmPoValue_t* vp = NULL;
  855. _cmPo_t* p = _cmPoHandleToPtr(h);
  856. if(_cmPgmOptInstPtr(p,numId,instIdx,&rp,&vp) != kOkPoRC )
  857. return 0;
  858. // all types have a meaning in terms of bool
  859. return vp->b;
  860. }
  861. int cmPgmOptArgInt( cmPgmOptH_t h, unsigned numId, unsigned instIdx )
  862. {
  863. cmPoRC_t rc;
  864. const _cmPoOpt_t* rp = NULL;
  865. const _cmPoValue_t* vp = NULL;
  866. _cmPo_t* p = _cmPoHandleToPtr(h);
  867. int v = 0;
  868. if((rc = _cmPgmOptInstPtr(p,numId,instIdx,&rp,&vp)) != kOkPoRC )
  869. return rc;
  870. switch( rp->sflags & kTypeMaskPoFl )
  871. {
  872. case kFlagPoFl: v = vp->u; break;
  873. case kBoolPoFl: v = vp->b; break;
  874. case kCharPoFl: v = vp->c; break;
  875. case kIntPoFl:
  876. case kEnumPoFl: v = vp->i; break;
  877. case kUIntPoFl:
  878. case kDblPoFl: v = roundl(vp->d); break;
  879. case kStrPoFl:
  880. cmErrMsg(&p->err,kTypeErrPoRC,"The string parameter '%s' cannot be converted to an integer.",rp->wordId);
  881. break;
  882. default:
  883. assert(0);
  884. break;
  885. }
  886. return v;
  887. }
  888. unsigned cmPgmOptArgUInt( cmPgmOptH_t h, unsigned numId, unsigned instIdx )
  889. { return cmPgmOptArgInt(h,numId,instIdx); }
  890. double cmPgmOptArgDbl( cmPgmOptH_t h, unsigned numId, unsigned instIdx )
  891. {
  892. cmPoRC_t rc;
  893. const _cmPoOpt_t* rp = NULL;
  894. const _cmPoValue_t* vp = NULL;
  895. _cmPo_t* p = _cmPoHandleToPtr(h);
  896. double v = 0;
  897. if((rc = _cmPgmOptInstPtr(p,numId,instIdx,&rp,&vp)) != kOkPoRC )
  898. return rc;
  899. switch( rp->sflags & kTypeMaskPoFl )
  900. {
  901. case kFlagPoFl: v = vp->u; break;
  902. case kBoolPoFl: v = vp->b; break;
  903. case kCharPoFl: v = vp->c; break;
  904. case kEnumPoFl:
  905. case kIntPoFl: v = vp->i; break;
  906. case kUIntPoFl: v = vp->u; break;
  907. case kDblPoFl: v = vp->d; break;
  908. case kStrPoFl:
  909. cmErrMsg(&p->err,kTypeErrPoRC,"The string parameter '%s' cannot be converted to a double.",rp->wordId);
  910. break;
  911. default:
  912. assert(0);
  913. break;
  914. }
  915. return v;
  916. }
  917. const char* cmPgmOptArgStr( cmPgmOptH_t h, unsigned numId, unsigned instIdx )
  918. {
  919. cmPoRC_t rc;
  920. const _cmPoOpt_t* rp = NULL;
  921. const _cmPoValue_t* vp = NULL;
  922. _cmPo_t* p = _cmPoHandleToPtr(h);
  923. if((rc = _cmPgmOptInstPtr(p,numId,instIdx,&rp,&vp)) != kOkPoRC )
  924. return NULL;
  925. // if the requested param is a defined as a string
  926. if( cmIsFlag(rp->sflags,kStrPoFl) )
  927. return vp->s;
  928. // otherwise the requested param is not defined as a string - so try to return the instance string value
  929. const _cmPoArg_t* a = rp->inst;
  930. unsigned i;
  931. for(i=0; i<instIdx && a != NULL; ++i)
  932. a = a->inst;
  933. // if the index is not valid
  934. if( i != instIdx )
  935. {
  936. cmErrMsg(&p->err,kInstNotFoundPoRC,"The instance index %i is not valid for parameter %s instance count %i.",instIdx,rp->wordId,i);
  937. return NULL;
  938. }
  939. // otherwise return the instance string
  940. return a->valStr;
  941. }
  942. cmPoRC_t cmPgmOptRC( cmPgmOptH_t h, cmPoRC_t rc )
  943. {
  944. _cmPo_t* p = _cmPoHandleToPtr(h);
  945. return cmErrSetRC(&p->err,rc);
  946. }
  947. bool cmPgmOptHandleBuiltInActions( cmPgmOptH_t h, cmRpt_t* rpt )
  948. {
  949. _cmPo_t* p = _cmPoHandleToPtr(h);
  950. _cmPoArg_t* a = p->args;
  951. while( a != NULL )
  952. {
  953. switch( a->opt->numId )
  954. {
  955. case kPrintHelpPoId:
  956. cmPgmOptPrintHelp(h,rpt);
  957. break;
  958. case kVersionPoId:
  959. cmPgmOptPrintVersion(h,rpt);
  960. break;
  961. case kPrintParmsPoId:
  962. cmPgmOptPrintParms(h,rpt);
  963. break;
  964. }
  965. a = a->link;
  966. }
  967. return p->execFl;
  968. }
  969. void cmPgmOptPrintHelp( cmPgmOptH_t h, cmRpt_t* rpt )
  970. {
  971. _cmPo_t* p = _cmPoHandleToPtr(h);
  972. const _cmPoOpt_t* r = p->list;
  973. if( p->helpBegStr != NULL )
  974. cmRptPrintf(rpt,"%s\n",p->helpBegStr);
  975. for(; r != NULL; r=r->link )
  976. {
  977. const cmChar_t* reqStr = "(required)";
  978. const cmChar_t* valueTypeLabel = "";
  979. const cmChar_t* reqLabel = "";
  980. const cmChar_t* indentStr = "";
  981. const _cmPoOpt_t* mstr = NULL;
  982. if( cmIsFlag(r->sflags,kEnumPoFl) )
  983. {
  984. if( r->enumPtr == NULL )
  985. mstr = r;
  986. indentStr = " ";
  987. }
  988. if( _cmPoOptUsesValue(r) )
  989. valueTypeLabel = _cmPoTypeFlagToLabel(r->sflags & kTypeMaskPoFl,r->cflags);
  990. if( cmIsNotFlag(r->sflags,kEnumPoFl) && cmIsFlag(r->sflags,kReqPoFl) )
  991. reqLabel = reqStr;
  992. if( mstr != NULL )
  993. cmRptPrintf(rpt,"Enumerated group: %s %s",mstr->mstrStr==NULL ? "" : mstr->mstrStr, cmIsFlag(mstr->cflags,kReqPoFl) ? reqStr : "" );
  994. cmRptPrintf(rpt,"%s-%c --%s %s %s",indentStr,r->charId,r->wordId,valueTypeLabel,reqLabel);
  995. if( r->helpStr != NULL )
  996. cmRptPrintf(rpt," %s\n",r->helpStr);
  997. }
  998. if( p->helpEndStr != NULL )
  999. cmRptPrintf(rpt,"\n%s\n",p->helpEndStr);
  1000. }
  1001. void cmPgmOptPrintVersion( cmPgmOptH_t h, cmRpt_t* rpt )
  1002. {
  1003. cmRptPrintf(rpt,"%s\n",PACKAGE_STRING); // PACKAGE_STRING is defined in config.h
  1004. }
  1005. bool _cmPgmOptPrint( _cmPo_t* p, cmRpt_t* rpt, const _cmPoOpt_t* r, const _cmPoValue_t* v, const cmChar_t* valStr )
  1006. {
  1007. const _cmPoOpt_t* e = r;
  1008. if( cmIsFlag(r->sflags,kEnumPoFl) )
  1009. {
  1010. if( r->enumPtr != NULL )
  1011. return false;
  1012. if((e = _cmPgmOptEnumIdToOptRecd(p,r->numId,v->u)) == NULL )
  1013. {
  1014. cmErrMsg(&p->err,kParmNotFoundPoRC,"The parm. defn. could not be found for numId=%i enumId=%i.",r->numId,v->u);
  1015. return false;
  1016. }
  1017. }
  1018. cmRptPrintf(rpt,"-%c --%-20s %i ",e->charId,e->wordId, _cmPgmOptInstCount(r));
  1019. switch(r->sflags & kTypeMaskPoFl)
  1020. {
  1021. case kFlagPoFl: cmRptPrintf(rpt,r->cflags & kHexPoFl ? "0x%x ": "%u ",v->u ); break;
  1022. case kBoolPoFl: cmRptPrintf(rpt,"%c ", v->b ? 'T' : 'F'); break;
  1023. case kCharPoFl: cmRptPrintf(rpt,"%c ", v->c); break;
  1024. case kIntPoFl: cmRptPrintf(rpt,r->cflags & kHexPoFl ? "0x%x ": "%i ", v->i); break;
  1025. case kUIntPoFl: cmRptPrintf(rpt,r->cflags & kHexPoFl ? "0x%x ": "%u ", v->u); break;
  1026. case kDblPoFl: cmRptPrintf(rpt,"%f ", v->d); break;
  1027. case kStrPoFl: cmRptPrintf(rpt,"%s ", v->s); break;
  1028. case kEnumPoFl: cmRptPrintf(rpt,"%i ", v->u); break;
  1029. default:
  1030. cmRptPrintf(rpt,"%s ",cmStringNullGuard(valStr)); break;
  1031. }
  1032. return true;
  1033. }
  1034. void cmPgmOptPrintParms( cmPgmOptH_t h, cmRpt_t* rpt )
  1035. {
  1036. _cmPo_t* p = _cmPoHandleToPtr(h);
  1037. _cmPoArg_t* a = p->args;
  1038. // print the given arguments
  1039. while( a != NULL )
  1040. {
  1041. if(_cmPgmOptPrint(p, rpt, a->opt,&a->u,a->valStr))
  1042. cmRptPrintf(rpt,"\n");
  1043. a = a->link;
  1044. }
  1045. // print the default values
  1046. _cmPoOpt_t* r = p->list;
  1047. while( r != NULL )
  1048. {
  1049. if( r->inst == NULL )
  1050. {
  1051. if(_cmPgmOptPrint(p,rpt,r,&r->dfltVal,NULL))
  1052. cmRptPrintf(rpt,"\n");
  1053. }
  1054. r = r->link;
  1055. }
  1056. }