libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmPgmOpts.c 34KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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. // link onto the end of the master arg list
  393. _cmPoArg_t* ap = p->args;
  394. while( ap!=NULL && ap->link != NULL )
  395. ap=ap->link;
  396. if( ap == NULL )
  397. p->args = a;
  398. else
  399. ap->link = a;
  400. // link onto the end of the opt recd list
  401. ap = r->inst;
  402. while( ap!=NULL && ap->inst!=NULL)
  403. ap=ap->inst;
  404. if( ap==NULL)
  405. r->inst = a;
  406. else
  407. ap->inst = a;
  408. // if no parm. type flag was given then the arg is implicitely a bool and the value is true.
  409. //if( (r->flags & kTypeMaskPoFl) == 0 )
  410. // a->u.b = true;
  411. // if this is an enumerated type
  412. if( cmIsFlag(r->sflags,kEnumPoFl) )
  413. {
  414. a->u.u = enumId;
  415. if( r->retVal.u != NULL )
  416. *r->retVal.u = enumId;
  417. if( e->retVal.u != NULL )
  418. *e->retVal.u = enumId;
  419. }
  420. // if this is a flag type
  421. if( cmIsFlag(r->sflags,kFlagPoFl) )
  422. {
  423. a->u.u = r->dfltVal.u;
  424. if( r->retVal.u != NULL )
  425. *r->retVal.u |= a->u.u;
  426. }
  427. return a;
  428. }
  429. cmPoRC_t _cmPgmOptParseBool(_cmPo_t* p, const cmChar_t* valStr, bool* retValPtr )
  430. {
  431. cmPoRC_t rc = kOkPoRC;
  432. unsigned n = strlen("false");
  433. char val[n+1];
  434. int i;
  435. for(i=0; i<n && valStr[i]; ++i)
  436. val[i] = tolower(valStr[i]);
  437. val[i] = 0;
  438. if( !strcmp(val,"0") || !strcmp(val,"false") || !strcmp(val,"f") )
  439. *retValPtr = false;
  440. else
  441. if( !strcmp(val,"1") || !strcmp(val,"true") || !strcmp(val,"t") )
  442. *retValPtr = true;
  443. else
  444. 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);
  445. return rc;
  446. }
  447. cmPoRC_t _cmPgmOptParseValue( _cmPo_t* p, _cmPoOpt_t* r, _cmPoArg_t* a, const cmChar_t* valStr )
  448. {
  449. cmPoRC_t rc = kOkPoRC;
  450. assert(a != NULL && valStr != NULL);
  451. a->valStr = valStr;
  452. errno = 0;
  453. switch( r->sflags & kTypeMaskPoFl )
  454. {
  455. // case kFlagPoFl:
  456. // enum values get set in _cmPgmOptInsertArg()
  457. case kBoolPoFl:
  458. //rc = _cmPgmOptParseBool(p,valStr,&a->u.b);
  459. a->u.b = true;
  460. if( r->retVal.b != NULL )
  461. *r->retVal.b = a->u.b;
  462. break;
  463. case kCharPoFl:
  464. a->u.c = valStr[0];
  465. if( r->retVal.c != NULL )
  466. *r->retVal.c = a->u.c;
  467. break;
  468. case kIntPoFl:
  469. a->u.i = strtol(valStr,NULL, cmIsFlag(r->cflags,kHexPoFl) ? 16 : 10);
  470. if( r->retVal.i != NULL )
  471. *r->retVal.i = a->u.i;
  472. break;
  473. case kUIntPoFl:
  474. a->u.u = strtol(valStr,NULL, cmIsFlag(r->cflags,kHexPoFl) ? 16 : 10);
  475. if( r->retVal.u != NULL )
  476. *r->retVal.u = a->u.u;
  477. break;
  478. // case kEnumPoFl:
  479. // enum values get set in _cmPgmOptInsertArg()
  480. case kDblPoFl:
  481. a->u.d = strtod(valStr,NULL);
  482. if( r->retVal.d != NULL )
  483. *r->retVal.d = a->u.d;
  484. break;
  485. case kStrPoFl:
  486. a->u.s = valStr;
  487. if( r->retVal.s != NULL )
  488. *r->retVal.s = a->u.s;
  489. break;
  490. default:
  491. { assert(0); }
  492. }
  493. if( errno != 0 )
  494. rc = cmErrMsg(&p->err,kParseFailPoRC,"The value '%s' for option '%s' could not be parsed.",valStr,r->wordId);
  495. return rc;
  496. }
  497. unsigned _cmPgmOptInstCount( const _cmPoOpt_t* r )
  498. {
  499. const _cmPoArg_t* a = r->inst;
  500. unsigned n = 0;
  501. while( a != NULL )
  502. {
  503. ++n;
  504. a = a->inst;
  505. }
  506. return n;
  507. }
  508. // check if any non-built-in options were provided
  509. // If only built in options were selected then no execution is assumed
  510. // and required arg's are not checked.
  511. bool _cmPgmOptCheckNoExec( _cmPo_t* p )
  512. {
  513. _cmPoArg_t* a = p->args;
  514. while( a != NULL )
  515. {
  516. if( a->opt->numId >= kBasePoId )
  517. return true;
  518. a = a->link;
  519. }
  520. return false;
  521. }
  522. cmPoRC_t _cmPgmOptMissingEnumError( _cmPo_t* p, _cmPoOpt_t* r )
  523. {
  524. _cmPoOpt_t* mstr = r->enumPtr==NULL ? r : r->enumPtr;
  525. _cmPoOpt_t* rp = p->list;
  526. cmChar_t* msg = cmTsPrintfP(NULL,"No argument was supplied for the required enumerated parameter containing the following set of possible flags: { ");
  527. for(; rp!=NULL; rp=rp->link)
  528. if( cmIsFlag(rp->sflags,kEnumPoFl) && (rp==mstr || rp->enumPtr==mstr) )
  529. {
  530. msg = cmTextAppendSS(msg,rp->wordId);
  531. msg = cmTextAppendSS(msg," ");
  532. }
  533. msg = cmTextAppendSS(msg,"}");
  534. cmPoRC_t rc = cmErrMsg(&p->err,kNoReqArgPoRC,msg);
  535. cmMemFree(msg);
  536. return rc;
  537. }
  538. // check that all required arg.s were actually given and that the actual
  539. // number of instances does not exceed the defined limit
  540. cmPoRC_t _cmPgmOptCheckReqArgs( _cmPo_t* p )
  541. {
  542. cmPoRC_t rc = kOkPoRC;
  543. _cmPoOpt_t* r = p->list;
  544. while( r != NULL )
  545. {
  546. if( cmIsFlag(r->cflags, kReqPoFl ) )
  547. {
  548. _cmPoArg_t* a = p->args;
  549. while( a != NULL )
  550. {
  551. if( a->opt == r )
  552. break;
  553. a = a->link;
  554. }
  555. if( a == NULL )
  556. {
  557. if( cmIsFlag(r->sflags, kEnumPoFl ) )
  558. rc = _cmPgmOptMissingEnumError(p,r);
  559. else
  560. rc = cmErrMsg(&p->err,kNoReqArgPoRC,"No argument was supplied for the required parameter '%s'.",r->wordId);
  561. }
  562. }
  563. unsigned cnt;
  564. if( r->maxInstCnt > 0 && r->maxInstCnt < (cnt=_cmPgmOptInstCount(r)) )
  565. rc = cmErrMsg(&p->err,kArgCntErrPoRC,"The parameter '%s' has %i instances which exceeds the defined limit of %i instances.",r->wordId,cnt,r->maxInstCnt);
  566. r = r->link;
  567. }
  568. return rc;
  569. }
  570. cmPoRC_t cmPgmOptParse( cmPgmOptH_t h, unsigned argCnt, char* argArray[] )
  571. {
  572. enum { kDash, kCharId, kWordId, kArgVal };
  573. cmPoRC_t rc = kOkPoRC;
  574. _cmPo_t* p = _cmPoHandleToPtr(h);
  575. unsigned state = kDash;
  576. _cmPoOpt_t* r = NULL;
  577. _cmPoArg_t* a = NULL;
  578. int i = 0; // arg index
  579. int j = 0; // arg label character index
  580. if( _cmPgmOptNumIdToOptRecd(p,kPrintHelpPoId) == NULL )
  581. cmPgmOptInstallBool(h, kPrintHelpPoId, 'h', "help", 0, false, NULL,0,"Print this usage information." );
  582. if( _cmPgmOptNumIdToOptRecd(p,kVersionPoId) == NULL )
  583. cmPgmOptInstallBool(h, kVersionPoId, 'v', "version", 0, false, NULL,0,"Print version information." );
  584. if( _cmPgmOptNumIdToOptRecd(p,kPrintParmsPoId) == NULL )
  585. cmPgmOptInstallBool(h, kPrintParmsPoId,'p', "parms", 0, false, NULL,0,"Print the arguments.");
  586. while(i<argCnt)
  587. {
  588. const cmChar_t* valStr = NULL;
  589. switch(state)
  590. {
  591. case kDash:
  592. // this token must begin with a '-' character
  593. if( argArray[i][0] != '-')
  594. return cmErrMsg(&p->err,kSyntaxErrPoRC,"Syntax error. Expecting a '-'.");
  595. // if the second char. is also a '-' then this is a wordId
  596. if( argArray[i][1] == '-' )
  597. {
  598. state = kWordId;
  599. j = 2; // word id's always begin on the third token char
  600. }
  601. else // otherwise it is a charId
  602. {
  603. state = kCharId;
  604. j = 1; // char id's always begin on the second token char
  605. }
  606. r = NULL;
  607. break;
  608. case kCharId:
  609. // we complete our parsing of charId's only when we encounter a '\0'
  610. if( argArray[i][j] == '\0' )
  611. {
  612. ++i;
  613. state = kDash;
  614. r = NULL;
  615. a = NULL;
  616. }
  617. else
  618. {
  619. // get the opt recd assoc'd with the jth character in this charId
  620. if((r = _cmPgmOptCharIdToOptRecd(p,argArray[i][j])) == NULL )
  621. return cmErrMsg(&p->err,kSyntaxErrPoRC,"The program option selector char '%c' is not valid.",argArray[i][j]);
  622. // if this charId is not a flag,bool or enum then it must be followed by a value.
  623. if( _cmPoOptUsesValue(r) )
  624. ++i;
  625. else // otherwise process the next char id in this charId token
  626. ++j;
  627. }
  628. break;
  629. case kWordId:
  630. // get the wordId
  631. if((r = _cmPgmOptWordIdToOptRecd(p,argArray[i]+j)) == NULL )
  632. return cmErrMsg(&p->err,kSyntaxErrPoRC,"The program option selector word '%s' is not valid.",cmStringNullGuard(argArray[i]+j));
  633. ++i; // advance to the next token
  634. break;
  635. case kArgVal:
  636. assert(r != NULL );
  637. valStr = argArray[i];
  638. ++i;
  639. break;
  640. }
  641. // if a valid opt recd has been selected
  642. if( r != NULL )
  643. {
  644. // if the cur token reprsents a value ...
  645. if( state == kArgVal )
  646. {
  647. // ... then parse the value string
  648. if((rc = _cmPgmOptParseValue(p,r,a,valStr)) != kOkPoRC )
  649. goto errLabel;
  650. r = NULL;
  651. a = NULL;
  652. state = kDash; // value strings are always followed by char or word id's
  653. }
  654. else
  655. {
  656. // create an arg record for the cur char or word id.
  657. a = _cmPgmOptInsertArg(p, r );
  658. // and the value type is not 'flag','bool' or 'enum'
  659. if( _cmPoOptUsesValue(r) )
  660. state = kArgVal;
  661. switch(state)
  662. {
  663. case kDash: assert(i==argCnt); break;
  664. case kCharId: break;
  665. case kWordId: state = kDash; break;
  666. case kArgVal: break;
  667. default: assert(0); break;
  668. }
  669. }
  670. }
  671. }
  672. errLabel:
  673. // if no errors have occurred then check for missing parameters.
  674. if( rc == kOkPoRC )
  675. {
  676. if((p->execFl = _cmPgmOptCheckNoExec(p)) == true )
  677. rc = _cmPgmOptCheckReqArgs(p);
  678. }
  679. return rc;
  680. }
  681. unsigned cmPgmOptArgCount( cmPgmOptH_t h)
  682. {
  683. _cmPo_t* p = _cmPoHandleToPtr(h);
  684. const _cmPoArg_t* a = p->args;
  685. unsigned n = 0;
  686. while( a != NULL )
  687. {
  688. ++n;
  689. a = a->link;
  690. }
  691. return n;
  692. }
  693. cmPoRC_t _cmPgmOptIndexToPtr( _cmPo_t* p, unsigned idx, const _cmPoArg_t** app )
  694. {
  695. unsigned n = 0;
  696. const _cmPoArg_t* a = p->args;
  697. while( a != NULL && n < idx )
  698. {
  699. ++n;
  700. a = a->link;
  701. }
  702. if( n!=idx || a==NULL)
  703. return cmErrMsg(&p->err,kInvalidIdxPoRC,"The option index '%i' is not valid.",idx);
  704. *app = a;
  705. return kOkPoRC;
  706. }
  707. unsigned cmPgmOptNumId( cmPgmOptH_t h, unsigned argIdx )
  708. {
  709. const _cmPoArg_t* a;
  710. cmPoRC_t rc;
  711. if((rc = _cmPgmOptIndexToPtr(_cmPoHandleToPtr(h),argIdx,&a)) != kOkPoRC )
  712. return cmInvalidId;
  713. return a->opt->numId;
  714. }
  715. unsigned cmPgmOptCharId( cmPgmOptH_t h, unsigned argIdx )
  716. {
  717. const _cmPoArg_t* a;
  718. cmPoRC_t rc;
  719. if((rc = _cmPgmOptIndexToPtr(_cmPoHandleToPtr(h),argIdx,&a)) != kOkPoRC )
  720. return cmInvalidId;
  721. return a->opt->charId;
  722. }
  723. const cmChar_t* cmPgmOptWordId( cmPgmOptH_t h, unsigned argIdx )
  724. {
  725. const _cmPoArg_t* a;
  726. cmPoRC_t rc;
  727. if((rc = _cmPgmOptIndexToPtr(_cmPoHandleToPtr(h),argIdx,&a)) != kOkPoRC )
  728. return NULL;
  729. return a->opt->wordId;
  730. }
  731. cmPoRC_t _cmPgmOptArgPtr( _cmPo_t* p, unsigned argIdx, const _cmPoArg_t** app )
  732. {
  733. cmPoRC_t rc;
  734. if((rc = _cmPgmOptIndexToPtr(p,argIdx,app)) != kOkPoRC )
  735. {
  736. if( (*app)->valStr == NULL )
  737. return cmErrMsg(&p->err,kParseFailPoRC,"The parameter at index '%i' is not a value.",argIdx);
  738. }
  739. return rc;
  740. }
  741. char cmPgmOptParseArgChar( cmPgmOptH_t h, unsigned argIdx )
  742. {
  743. cmPoRC_t rc;
  744. const _cmPoArg_t* a;
  745. _cmPo_t* p = _cmPoHandleToPtr(h);
  746. if((rc = _cmPgmOptArgPtr(p,argIdx,&a)) != kOkPoRC )
  747. return 0;
  748. return a->valStr==NULL ? 0 : a->valStr[0];
  749. }
  750. bool cmPgmOptParseArgBool( cmPgmOptH_t h, unsigned argIdx )
  751. {
  752. cmPoRC_t rc;
  753. const _cmPoArg_t* a;
  754. _cmPo_t* p = _cmPoHandleToPtr(h);
  755. bool val = false;
  756. if((rc = _cmPgmOptArgPtr(p,argIdx,&a)) != kOkPoRC )
  757. return false;
  758. if( a->valStr != NULL )
  759. _cmPgmOptParseBool(p,a->valStr,&val);
  760. return val;
  761. }
  762. int cmPgmOptParseArgInt( cmPgmOptH_t h, unsigned argIdx )
  763. {
  764. cmPoRC_t rc;
  765. const _cmPoArg_t* a;
  766. _cmPo_t* p = _cmPoHandleToPtr(h);
  767. if((rc = _cmPgmOptArgPtr(p,argIdx,&a)) != kOkPoRC )
  768. return rc;
  769. errno = 0;
  770. int v = strtol(a->valStr,NULL,10);
  771. if( errno != 0 )
  772. cmErrMsg(&p->err,kParseFailPoRC,"The parameter '%s' could not be converted to an 'int'.",a->valStr);
  773. return v;
  774. }
  775. unsigned cmPgmOptParseArgUInt( cmPgmOptH_t h, unsigned argIdx )
  776. {
  777. cmPoRC_t rc;
  778. const _cmPoArg_t* a;
  779. _cmPo_t* p = _cmPoHandleToPtr(h);
  780. if((rc = _cmPgmOptArgPtr(p,argIdx,&a)) != kOkPoRC )
  781. return rc;
  782. errno = 0;
  783. unsigned v = strtol(a->valStr,NULL,10);
  784. if( errno != 0 )
  785. cmErrMsg(&p->err,kParseFailPoRC,"The parameter '%s' could not be converted to an 'unsigned int'.",a->valStr);
  786. return v;
  787. }
  788. double cmPgmOptParseArgDbl( cmPgmOptH_t h, unsigned argIdx )
  789. {
  790. cmPoRC_t rc;
  791. const _cmPoArg_t* a;
  792. _cmPo_t* p = _cmPoHandleToPtr(h);
  793. if((rc = _cmPgmOptArgPtr(p,argIdx,&a)) != kOkPoRC )
  794. return rc;
  795. errno = 0;
  796. double v = strtod(a->valStr,NULL);
  797. if( errno != 0 )
  798. cmErrMsg(&p->err,kParseFailPoRC,"The parameter '%s' could not be converted to a 'double'.",a->valStr);
  799. return v;
  800. }
  801. const char* cmPgmOptParseArgStr( cmPgmOptH_t h, unsigned argIdx )
  802. {
  803. cmPoRC_t rc;
  804. const _cmPoArg_t* a;
  805. _cmPo_t* p = _cmPoHandleToPtr(h);
  806. if((rc = _cmPgmOptArgPtr(p,argIdx,&a)) != kOkPoRC )
  807. return NULL;
  808. return a->valStr;
  809. }
  810. unsigned cmPgmOptParmArgCount( cmPgmOptH_t h, unsigned numId )
  811. {
  812. const _cmPoOpt_t* r;
  813. _cmPo_t* p = _cmPoHandleToPtr(h);
  814. unsigned n = 0;
  815. if((r = _cmPgmOptNumIdToOptRecd(p,numId)) == NULL )
  816. cmErrMsg(&p->err,kParmNotFoundPoRC,"No parameter definition was found for numeric id %i.",numId);
  817. else
  818. n = _cmPgmOptInstCount(r);
  819. return n;
  820. }
  821. cmPoRC_t _cmPgmOptInstPtr( _cmPo_t* p, unsigned numId, unsigned instIdx, const _cmPoOpt_t** rpp, const _cmPoValue_t** vpp )
  822. {
  823. *rpp = NULL;
  824. *vpp = NULL;
  825. // locate the opt recd
  826. if((*rpp = _cmPgmOptNumIdToOptRecd(p,numId)) == NULL )
  827. return cmErrMsg(&p->err,kParmNotFoundPoRC,"No parameter definition was found for the numeric id %i.",numId);
  828. const _cmPoArg_t* a = (*rpp)->inst;
  829. unsigned i;
  830. // locate the instance recd
  831. for(i=0; i<instIdx && a != NULL; ++i)
  832. a = a->inst;
  833. // if the instance recd was not found
  834. if( i != instIdx || a == NULL )
  835. {
  836. // if the instance index is 0 then and no instance recd exists then
  837. // we will return the default value.
  838. if( instIdx == 0 )
  839. {
  840. *vpp = &((*rpp)->dfltVal);
  841. return kOkPoRC;
  842. }
  843. // otherwise signal an error
  844. return cmErrMsg(&p->err,kInstNotFoundPoRC,"The instance index %i is not valid for parameter %s instance count %i.",instIdx,(*rpp)->wordId,i);
  845. }
  846. *vpp = &a->u;
  847. return kOkPoRC;
  848. }
  849. char cmPgmOptArgChar( cmPgmOptH_t h, unsigned numId, unsigned instIdx )
  850. {
  851. const _cmPoOpt_t* rp = NULL;
  852. const _cmPoValue_t* vp = NULL;
  853. _cmPo_t* p = _cmPoHandleToPtr(h);
  854. if(_cmPgmOptInstPtr(p,numId,instIdx,&rp,&vp) != kOkPoRC )
  855. return 0;
  856. if( cmIsFlag(rp->sflags,kCharPoFl) )
  857. {
  858. cmErrMsg(&p->err,kTypeErrPoRC,"The parameter '%s' is not a 'char'.",rp->wordId);
  859. return 0;
  860. }
  861. return vp->c;
  862. }
  863. bool cmPgmOptArgBool( cmPgmOptH_t h, unsigned numId, unsigned instIdx )
  864. {
  865. const _cmPoOpt_t* rp = NULL;
  866. const _cmPoValue_t* vp = NULL;
  867. _cmPo_t* p = _cmPoHandleToPtr(h);
  868. if(_cmPgmOptInstPtr(p,numId,instIdx,&rp,&vp) != kOkPoRC )
  869. return 0;
  870. // all types have a meaning in terms of bool
  871. return vp->b;
  872. }
  873. int cmPgmOptArgInt( cmPgmOptH_t h, unsigned numId, unsigned instIdx )
  874. {
  875. cmPoRC_t rc;
  876. const _cmPoOpt_t* rp = NULL;
  877. const _cmPoValue_t* vp = NULL;
  878. _cmPo_t* p = _cmPoHandleToPtr(h);
  879. int v = 0;
  880. if((rc = _cmPgmOptInstPtr(p,numId,instIdx,&rp,&vp)) != kOkPoRC )
  881. return rc;
  882. switch( rp->sflags & kTypeMaskPoFl )
  883. {
  884. case kFlagPoFl: v = vp->u; break;
  885. case kBoolPoFl: v = vp->b; break;
  886. case kCharPoFl: v = vp->c; break;
  887. case kIntPoFl:
  888. case kEnumPoFl: v = vp->i; break;
  889. case kUIntPoFl:
  890. case kDblPoFl: v = roundl(vp->d); break;
  891. case kStrPoFl:
  892. cmErrMsg(&p->err,kTypeErrPoRC,"The string parameter '%s' cannot be converted to an integer.",rp->wordId);
  893. break;
  894. default:
  895. assert(0);
  896. break;
  897. }
  898. return v;
  899. }
  900. unsigned cmPgmOptArgUInt( cmPgmOptH_t h, unsigned numId, unsigned instIdx )
  901. { return cmPgmOptArgInt(h,numId,instIdx); }
  902. double cmPgmOptArgDbl( cmPgmOptH_t h, unsigned numId, unsigned instIdx )
  903. {
  904. cmPoRC_t rc;
  905. const _cmPoOpt_t* rp = NULL;
  906. const _cmPoValue_t* vp = NULL;
  907. _cmPo_t* p = _cmPoHandleToPtr(h);
  908. double v = 0;
  909. if((rc = _cmPgmOptInstPtr(p,numId,instIdx,&rp,&vp)) != kOkPoRC )
  910. return rc;
  911. switch( rp->sflags & kTypeMaskPoFl )
  912. {
  913. case kFlagPoFl: v = vp->u; break;
  914. case kBoolPoFl: v = vp->b; break;
  915. case kCharPoFl: v = vp->c; break;
  916. case kEnumPoFl:
  917. case kIntPoFl: v = vp->i; break;
  918. case kUIntPoFl: v = vp->u; break;
  919. case kDblPoFl: v = vp->d; break;
  920. case kStrPoFl:
  921. cmErrMsg(&p->err,kTypeErrPoRC,"The string parameter '%s' cannot be converted to a double.",rp->wordId);
  922. break;
  923. default:
  924. assert(0);
  925. break;
  926. }
  927. return v;
  928. }
  929. const char* cmPgmOptArgStr( cmPgmOptH_t h, unsigned numId, unsigned instIdx )
  930. {
  931. cmPoRC_t rc;
  932. const _cmPoOpt_t* rp = NULL;
  933. const _cmPoValue_t* vp = NULL;
  934. _cmPo_t* p = _cmPoHandleToPtr(h);
  935. if((rc = _cmPgmOptInstPtr(p,numId,instIdx,&rp,&vp)) != kOkPoRC )
  936. return NULL;
  937. // if the requested param is a defined as a string
  938. if( cmIsFlag(rp->sflags,kStrPoFl) )
  939. return vp->s;
  940. // otherwise the requested param is not defined as a string - so try to return the instance string value
  941. const _cmPoArg_t* a = rp->inst;
  942. unsigned i;
  943. for(i=0; i<instIdx && a != NULL; ++i)
  944. a = a->inst;
  945. // if the index is not valid
  946. if( i != instIdx )
  947. {
  948. cmErrMsg(&p->err,kInstNotFoundPoRC,"The instance index %i is not valid for parameter %s instance count %i.",instIdx,rp->wordId,i);
  949. return NULL;
  950. }
  951. // otherwise return the instance string
  952. return a->valStr;
  953. }
  954. cmPoRC_t cmPgmOptRC( cmPgmOptH_t h, cmPoRC_t rc )
  955. {
  956. _cmPo_t* p = _cmPoHandleToPtr(h);
  957. return cmErrSetRC(&p->err,rc);
  958. }
  959. bool cmPgmOptHandleBuiltInActions( cmPgmOptH_t h, cmRpt_t* rpt )
  960. {
  961. _cmPo_t* p = _cmPoHandleToPtr(h);
  962. _cmPoArg_t* a = p->args;
  963. while( a != NULL )
  964. {
  965. switch( a->opt->numId )
  966. {
  967. case kPrintHelpPoId:
  968. cmPgmOptPrintHelp(h,rpt);
  969. break;
  970. case kVersionPoId:
  971. cmPgmOptPrintVersion(h,rpt);
  972. break;
  973. case kPrintParmsPoId:
  974. cmPgmOptPrintParms(h,rpt);
  975. break;
  976. }
  977. a = a->link;
  978. }
  979. return p->execFl;
  980. }
  981. void cmPgmOptPrintHelp( cmPgmOptH_t h, cmRpt_t* rpt )
  982. {
  983. _cmPo_t* p = _cmPoHandleToPtr(h);
  984. const _cmPoOpt_t* r = p->list;
  985. if( p->helpBegStr != NULL )
  986. cmRptPrintf(rpt,"%s\n",p->helpBegStr);
  987. for(; r != NULL; r=r->link )
  988. {
  989. const cmChar_t* reqStr = "(required)";
  990. const cmChar_t* valueTypeLabel = "";
  991. const cmChar_t* reqLabel = "";
  992. const cmChar_t* indentStr = "";
  993. const _cmPoOpt_t* mstr = NULL;
  994. if( cmIsFlag(r->sflags,kEnumPoFl) )
  995. {
  996. if( r->enumPtr == NULL )
  997. mstr = r;
  998. indentStr = " ";
  999. }
  1000. if( _cmPoOptUsesValue(r) )
  1001. valueTypeLabel = _cmPoTypeFlagToLabel(r->sflags & kTypeMaskPoFl,r->cflags);
  1002. if( cmIsNotFlag(r->sflags,kEnumPoFl) && cmIsFlag(r->sflags,kReqPoFl) )
  1003. reqLabel = reqStr;
  1004. if( mstr != NULL )
  1005. cmRptPrintf(rpt,"Enumerated group: %s %s",mstr->mstrStr==NULL ? "" : mstr->mstrStr, cmIsFlag(mstr->cflags,kReqPoFl) ? reqStr : "" );
  1006. cmRptPrintf(rpt,"%s-%c --%s %s %s",indentStr,r->charId,r->wordId,valueTypeLabel,reqLabel);
  1007. if( r->helpStr != NULL )
  1008. cmRptPrintf(rpt," %s\n",r->helpStr);
  1009. }
  1010. if( p->helpEndStr != NULL )
  1011. cmRptPrintf(rpt,"\n%s\n",p->helpEndStr);
  1012. }
  1013. void cmPgmOptPrintVersion( cmPgmOptH_t h, cmRpt_t* rpt )
  1014. {
  1015. cmRptPrintf(rpt,"%s\n",PACKAGE_STRING); // PACKAGE_STRING is defined in config.h
  1016. }
  1017. bool _cmPgmOptPrint( _cmPo_t* p, cmRpt_t* rpt, const _cmPoOpt_t* r, const _cmPoValue_t* v, const cmChar_t* valStr )
  1018. {
  1019. const _cmPoOpt_t* e = r;
  1020. if( cmIsFlag(r->sflags,kEnumPoFl) )
  1021. {
  1022. if( r->enumPtr != NULL )
  1023. return false;
  1024. if((e = _cmPgmOptEnumIdToOptRecd(p,r->numId,v->u)) == NULL )
  1025. {
  1026. cmErrMsg(&p->err,kParmNotFoundPoRC,"The parm. defn. could not be found for numId=%i enumId=%i.",r->numId,v->u);
  1027. return false;
  1028. }
  1029. }
  1030. cmRptPrintf(rpt,"-%c --%-20s %i ",e->charId,e->wordId, _cmPgmOptInstCount(r));
  1031. switch(r->sflags & kTypeMaskPoFl)
  1032. {
  1033. case kFlagPoFl: cmRptPrintf(rpt,r->cflags & kHexPoFl ? "0x%x ": "%u ",v->u ); break;
  1034. case kBoolPoFl: cmRptPrintf(rpt,"%c ", v->b ? 'T' : 'F'); break;
  1035. case kCharPoFl: cmRptPrintf(rpt,"%c ", v->c); break;
  1036. case kIntPoFl: cmRptPrintf(rpt,r->cflags & kHexPoFl ? "0x%x ": "%i ", v->i); break;
  1037. case kUIntPoFl: cmRptPrintf(rpt,r->cflags & kHexPoFl ? "0x%x ": "%u ", v->u); break;
  1038. case kDblPoFl: cmRptPrintf(rpt,"%f ", v->d); break;
  1039. case kStrPoFl: cmRptPrintf(rpt,"%s ", v->s); break;
  1040. case kEnumPoFl: cmRptPrintf(rpt,"%i ", v->u); break;
  1041. default:
  1042. cmRptPrintf(rpt,"%s ",cmStringNullGuard(valStr)); break;
  1043. }
  1044. return true;
  1045. }
  1046. void cmPgmOptPrintParms( cmPgmOptH_t h, cmRpt_t* rpt )
  1047. {
  1048. _cmPo_t* p = _cmPoHandleToPtr(h);
  1049. _cmPoArg_t* a = p->args;
  1050. // print the given arguments
  1051. while( a != NULL )
  1052. {
  1053. if(_cmPgmOptPrint(p, rpt, a->opt,&a->u,a->valStr))
  1054. cmRptPrintf(rpt,"\n");
  1055. a = a->link;
  1056. }
  1057. // print the default values
  1058. _cmPoOpt_t* r = p->list;
  1059. while( r != NULL )
  1060. {
  1061. if( r->inst == NULL )
  1062. {
  1063. if(_cmPgmOptPrint(p,rpt,r,&r->dfltVal,NULL))
  1064. cmRptPrintf(rpt,"\n");
  1065. }
  1066. r = r->link;
  1067. }
  1068. }