dclib-option.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /***************************************************************************
  2. * *
  3. * _____ ____ *
  4. * | __ \ / __ \ _ _ _____ *
  5. * | | \ \ / / \_\ | | | | _ \ *
  6. * | | \ \| | | | | | |_| | *
  7. * | | | || | | | | | ___/ *
  8. * | | / /| | __ | | | | _ \ *
  9. * | |__/ / \ \__/ / | |___| | |_| | *
  10. * |_____/ \____/ |_____|_|_____/ *
  11. * *
  12. * Wiimms source code library *
  13. * *
  14. ***************************************************************************
  15. * *
  16. * Copyright (c) 2012-2022 by Dirk Clemens <wiimm@wiimm.de> *
  17. * *
  18. ***************************************************************************
  19. * *
  20. * This library is free software; you can redistribute it and/or modify *
  21. * it under the terms of the GNU General Public License as published by *
  22. * the Free Software Foundation; either version 2 of the License, or *
  23. * (at your option) any later version. *
  24. * *
  25. * This library is distributed in the hope that it will be useful, *
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  28. * GNU General Public License for more details. *
  29. * *
  30. * See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
  31. * *
  32. ***************************************************************************/
  33. #define _GNU_SOURCE 1
  34. #include "dclib-option.h"
  35. #include <getopt.h>
  36. //
  37. ///////////////////////////////////////////////////////////////////////////////
  38. /////////////// GenericOpt_t ///////////////
  39. ///////////////////////////////////////////////////////////////////////////////
  40. void InitializeGenericOpt ( GenericOpt_t *go, char first_char )
  41. {
  42. DASSERT(go);
  43. memset(go,0,sizeof(*go));
  44. InitializeMemPool(&go->str_pool,2000);
  45. char *dest = go->sopt;
  46. if (first_char)
  47. *dest++ = first_char;
  48. *dest++ = ':';
  49. *dest = 0;
  50. }
  51. ///////////////////////////////////////////////////////////////////////////////
  52. void ResetGenericOpt ( GenericOpt_t *go )
  53. {
  54. DASSERT(go);
  55. ResetMemPool(&go->str_pool);
  56. FREE(go->lopt);
  57. InitializeGenericOpt(go,0);
  58. }
  59. ///////////////////////////////////////////////////////////////////////////////
  60. void AddGenericOpt
  61. (
  62. GenericOpt_t *go, // data structure
  63. const GenericOptParm_t *gp, // list to add, list end on name==NULL
  64. bool rm_minus // remove '-' on options for alternatives
  65. )
  66. {
  67. DASSERT(go);
  68. DASSERT(gp);
  69. char * sopt_ptr = go->sopt + strlen(go->sopt);
  70. char * sopt_end = go->sopt + sizeof(go->sopt) - 3;
  71. for ( ; gp->name; gp++ )
  72. {
  73. ccp ptr = gp->name;
  74. for(;;)
  75. {
  76. while ( *ptr == '|' )
  77. ptr++;
  78. uint have_minus = 0;
  79. char name_buf[50], *name_ptr = name_buf;
  80. while ( *ptr && *ptr != '|' )
  81. {
  82. char ch = *ptr++;
  83. if ( ch == '-' )
  84. have_minus++;
  85. if ( name_ptr < name_buf + sizeof(name_buf) - 1 )
  86. *name_ptr++ = ch;
  87. }
  88. *name_ptr = 0;
  89. uint len = name_ptr - name_buf;
  90. if (!len)
  91. break;
  92. //--- short option
  93. if ( len == 1 )
  94. {
  95. uchar ch = *name_buf;
  96. if ( ch > ' ' && ch < 0x80 && ch != ':'
  97. && !go->sopt_id[ch] && sopt_ptr < sopt_end )
  98. {
  99. go->sopt_id[ch] = gp->id;
  100. *sopt_ptr++ = ch;
  101. if ( gp->arg == 1 || gp->arg == 2 )
  102. {
  103. *sopt_ptr++ = ':';
  104. if ( gp->arg == 2 )
  105. *sopt_ptr++ = ':';
  106. }
  107. }
  108. }
  109. //--- long option
  110. for(;;)
  111. {
  112. #if 0 // avoid duplicates, needed?
  113. uint i;
  114. for ( i = 0; i < go->lopt_used; i++ )
  115. if (!strcmp(name_buf,go->lopt[i].name))
  116. goto skip;
  117. #endif
  118. if ( go->lopt_used == go->lopt_size )
  119. {
  120. go->lopt_size += 100;
  121. go->lopt = REALLOC( go->lopt, go->lopt_size * sizeof(*go->lopt) );
  122. }
  123. struct option *opt = go->lopt + go->lopt_used++;
  124. opt->name = StrDupMemPool(&go->str_pool,name_buf);
  125. opt->has_arg = gp->arg < 2 ? gp->arg : 2;
  126. opt->flag = 0;
  127. opt->val = gp->id;
  128. //skip:;
  129. if ( !have_minus || !rm_minus )
  130. break;
  131. have_minus = 0;
  132. char *src = name_buf, *dest = name_buf;
  133. while ( *src )
  134. {
  135. if ( *src != '-' )
  136. *dest++ = *src;
  137. src++;
  138. }
  139. *dest = 0;
  140. }
  141. }
  142. }
  143. *sopt_ptr = 0;
  144. if ( go->lopt_used == go->lopt_size )
  145. {
  146. go->lopt_size += 100;
  147. go->lopt = REALLOC( go->lopt, go->lopt_size * sizeof(*go->lopt) );
  148. }
  149. struct option *opt = go->lopt + go->lopt_used;
  150. memset(opt,0,sizeof(*opt));
  151. }
  152. //
  153. ///////////////////////////////////////////////////////////////////////////////
  154. /////////////// global options: parameters ///////////////
  155. ///////////////////////////////////////////////////////////////////////////////
  156. GParamValueType_t GetGParamValueType ( GOPT_t gt )
  157. {
  158. if ( gt & GOPT_RESERVED_BEG )
  159. gt = GetGOPType(gt);
  160. switch (gt)
  161. {
  162. case GOPT_IDX_INC0:
  163. case GOPT_IDX_INC:
  164. case GOPT_IDX_DEC0:
  165. case GOPT_IDX_DEC:
  166. case GOPT_IDX_SETP:
  167. case GOPT_IDX_SETM:
  168. case GOPT_IDX_SINT:
  169. return GPT_SN;
  170. case GOPT_IDX_UINT:
  171. case GOPT_IDX_OFF_ON:
  172. case GOPT_IDX_OR:
  173. case GOPT_IDX_SETBIT:
  174. return GPT_UN;
  175. case GOPT_IDX_SETP64:
  176. case GOPT_IDX_SETM64:
  177. case GOPT_IDX_S64:
  178. return GPT_SN64;
  179. case GOPT_IDX_U64:
  180. case GOPT_IDX_OR64:
  181. case GOPT_IDX_SETBIT64:
  182. case GOPT_IDX_HEX:
  183. case GOPT_IDX_SIZE:
  184. return GPT_UN64;
  185. case GOPT_IDX_DOUBLE:
  186. case GOPT_IDX_DURATION:
  187. return GPT_D;
  188. case GOPT_IDX_STRING:
  189. return GPT_STR;
  190. case GOPT_IDX_BIT:
  191. return GPT_BIT;
  192. default:
  193. return GPT_NONE;
  194. }
  195. }
  196. ///////////////////////////////////////////////////////////////////////////////
  197. ///////////////////////////////////////////////////////////////////////////////
  198. GetGParam_t GetGParamINT ( const GParam_t *par, int *return_val )
  199. {
  200. if (par)
  201. {
  202. DASSERT( par->vtype == GetGParamValueType(par->ptype) );
  203. switch (par->vtype)
  204. {
  205. case GPT_NONE:
  206. case GPT_STR:
  207. case GPT_BIT:
  208. return GGP_NONE;
  209. case GPT_SN:
  210. if (return_val)
  211. *return_val = par->sn;
  212. return GGP_OK;
  213. case GPT_UN:
  214. if (return_val)
  215. *return_val = par->un;
  216. return GGP_SIGN;
  217. case GPT_SN64:
  218. if (return_val)
  219. *return_val = par->sn64;
  220. return GGP_CONVERT;
  221. case GPT_UN64:
  222. if (return_val)
  223. *return_val = par->un64;
  224. return GGP_CONVERT;
  225. case GPT_D:
  226. if (return_val)
  227. *return_val = par->d;
  228. return GGP_CONVERT;
  229. }
  230. }
  231. return GGP_NONE;
  232. }
  233. ///////////////////////////////////////////////////////////////////////////////
  234. GetGParam_t GetGParamUINT ( const GParam_t *par, uint *return_val )
  235. {
  236. if (par)
  237. {
  238. DASSERT( par->vtype == GetGParamValueType(par->ptype) );
  239. switch (par->vtype)
  240. {
  241. case GPT_NONE:
  242. case GPT_STR:
  243. case GPT_BIT:
  244. return GGP_NONE;
  245. case GPT_SN:
  246. if (return_val)
  247. *return_val = par->sn;
  248. return GGP_SIGN;
  249. case GPT_UN:
  250. if (return_val)
  251. *return_val = par->un;
  252. return GGP_OK;
  253. case GPT_SN64:
  254. if (return_val)
  255. *return_val = par->sn64;
  256. return GGP_CONVERT;
  257. case GPT_UN64:
  258. if (return_val)
  259. *return_val = par->un64;
  260. return GGP_CONVERT;
  261. case GPT_D:
  262. if (return_val)
  263. *return_val = par->d;
  264. return GGP_CONVERT;
  265. }
  266. }
  267. return GGP_NONE;
  268. }
  269. ///////////////////////////////////////////////////////////////////////////////
  270. GetGParam_t GetGParamS64 ( const GParam_t *par, s64 *return_val )
  271. {
  272. if (par)
  273. {
  274. DASSERT( par->vtype == GetGParamValueType(par->ptype) );
  275. switch (par->vtype)
  276. {
  277. case GPT_NONE:
  278. case GPT_STR:
  279. case GPT_BIT:
  280. return GGP_NONE;
  281. case GPT_SN:
  282. if (return_val)
  283. *return_val = par->sn;
  284. return GGP_OK;
  285. case GPT_UN:
  286. if (return_val)
  287. *return_val = par->un;
  288. return GGP_SIGN;
  289. case GPT_SN64:
  290. if (return_val)
  291. *return_val = par->sn64;
  292. return GGP_OK;
  293. case GPT_UN64:
  294. if (return_val)
  295. *return_val = par->un64;
  296. return GGP_SIGN;
  297. case GPT_D:
  298. if (return_val)
  299. *return_val = par->d;
  300. return GGP_CONVERT;
  301. }
  302. }
  303. return GGP_NONE;
  304. }
  305. ///////////////////////////////////////////////////////////////////////////////
  306. GetGParam_t GetGParamU64 ( const GParam_t *par, u64 *return_val )
  307. {
  308. if (par)
  309. {
  310. DASSERT( par->vtype == GetGParamValueType(par->ptype) );
  311. switch (par->vtype)
  312. {
  313. case GPT_NONE:
  314. case GPT_STR:
  315. case GPT_BIT:
  316. return GGP_NONE;
  317. case GPT_SN:
  318. if (return_val)
  319. *return_val = par->sn;
  320. return GGP_SIGN;
  321. case GPT_UN:
  322. if (return_val)
  323. *return_val = par->un;
  324. return GGP_OK;
  325. case GPT_SN64:
  326. if (return_val)
  327. *return_val = par->sn64;
  328. return GGP_SIGN;
  329. case GPT_UN64:
  330. if (return_val)
  331. *return_val = par->un64;
  332. return GGP_OK;
  333. case GPT_D:
  334. if (return_val)
  335. *return_val = par->d;
  336. return GGP_CONVERT;
  337. }
  338. }
  339. return GGP_NONE;
  340. }
  341. ///////////////////////////////////////////////////////////////////////////////
  342. GetGParam_t GetGParamDBL ( const GParam_t *par, double *return_val )
  343. {
  344. if (par)
  345. {
  346. DASSERT( par->vtype == GetGParamValueType(par->ptype) );
  347. switch (par->vtype)
  348. {
  349. case GPT_NONE:
  350. case GPT_STR:
  351. case GPT_BIT:
  352. return GGP_NONE;
  353. case GPT_SN:
  354. if (return_val)
  355. *return_val = par->sn;
  356. return GGP_CONVERT;
  357. case GPT_UN:
  358. if (return_val)
  359. *return_val = par->un;
  360. return GGP_CONVERT;
  361. case GPT_SN64:
  362. if (return_val)
  363. *return_val = par->sn64;
  364. return GGP_CONVERT;
  365. case GPT_UN64:
  366. if (return_val)
  367. *return_val = par->un64;
  368. return GGP_CONVERT;
  369. case GPT_D:
  370. if (return_val)
  371. *return_val = par->d;
  372. return GGP_OK;
  373. }
  374. }
  375. return GGP_NONE;
  376. }
  377. ///////////////////////////////////////////////////////////////////////////////
  378. GetGParam_t GetGParamCCP ( const GParam_t *par, ccp *return_val )
  379. {
  380. DASSERT( !par || par->vtype == GetGParamValueType(par->ptype) );
  381. if ( par && par->vtype == GPT_STR )
  382. {
  383. if (return_val)
  384. *return_val = par->str;
  385. return GGP_OK;
  386. }
  387. return GGP_NONE;
  388. }
  389. ///////////////////////////////////////////////////////////////////////////////
  390. GetGParam_t GetGParamBIT ( const GParam_t *par, u32 *return_val, u32 *return_mask )
  391. {
  392. DASSERT( !par || par->vtype == GetGParamValueType(par->ptype) );
  393. if ( par && par->vtype == GPT_BIT )
  394. {
  395. if (return_val)
  396. *return_val = par->bit.val;
  397. if (return_mask)
  398. *return_mask = par->bit.mask;
  399. return GGP_OK;
  400. }
  401. return GGP_NONE;
  402. }
  403. ///////////////////////////////////////////////////////////////////////////////
  404. ///////////////////////////////////////////////////////////////////////////////
  405. ccp PrintGParam ( const GParam_t *par )
  406. {
  407. DASSERT(par);
  408. DASSERT( par->vtype == GetGParamValueType(par->ptype) );
  409. char buf[100];
  410. ccp extra = "";
  411. switch (par->ptype)
  412. {
  413. case GOPT_IDX_NULL:
  414. snprintf(buf,sizeof(buf),"[-]");
  415. break;
  416. case GOPT_IDX_INC0:
  417. case GOPT_IDX_DEC0:
  418. case GOPT_IDX_DEC:
  419. extra = "?";
  420. case GOPT_IDX_INC:
  421. snprintf(buf,sizeof(buf),"[%sINC] %d",extra,par->sn);
  422. break;
  423. case GOPT_IDX_SETP:
  424. case GOPT_IDX_SETM:
  425. extra = "?";
  426. case GOPT_IDX_SINT:
  427. snprintf(buf,sizeof(buf),"[%sSINT] %d",extra,par->sn);
  428. break;
  429. case GOPT_IDX_OR:
  430. case GOPT_IDX_SETBIT:
  431. case GOPT_IDX_OFF_ON:
  432. extra = "?";
  433. case GOPT_IDX_UINT:
  434. snprintf(buf,sizeof(buf),"[%sUINT] %u",extra,par->un);
  435. break;
  436. case GOPT_IDX_BIT:
  437. snprintf(buf,sizeof(buf),"[BIT] set=%x, clr=%x",
  438. par->bit.val, par->bit.mask & ~par->bit.val );
  439. break;
  440. case GOPT_IDX_SETP64:
  441. case GOPT_IDX_SETM64:
  442. extra = "?";
  443. case GOPT_IDX_S64:
  444. snprintf(buf,sizeof(buf),"[%sS64] %lld",extra,par->sn64);
  445. break;
  446. case GOPT_IDX_OR64:
  447. case GOPT_IDX_SETBIT64:
  448. extra = "?";
  449. case GOPT_IDX_U64:
  450. snprintf(buf,sizeof(buf),"[%sU64] %llu",extra,par->un64);
  451. break;
  452. case GOPT_IDX_HEX:
  453. snprintf(buf,sizeof(buf),"[HEX] 0x%llx",par->un64);
  454. break;
  455. case GOPT_IDX_SIZE:
  456. snprintf(buf,sizeof(buf),"[SIZE] %llu = %s",
  457. par->un64, PrintSize1024(0,0,par->un64,0) );
  458. break;
  459. case GOPT_IDX_DOUBLE:
  460. snprintf(buf,sizeof(buf),"[DBL] %12g",par->d);
  461. break;
  462. case GOPT_IDX_DURATION:
  463. snprintf(buf,sizeof(buf),"[DUR] %12g = %s",
  464. par->d, PrintTimerUSec6(0,0,double2usec(par->d),false) );
  465. break;
  466. case GOPT_IDX_STRING:
  467. snprintf(buf,sizeof(buf),"[STR] \"%s\"",par->str);
  468. break;
  469. default:
  470. snprintf(buf,sizeof(buf),"[?%u]",par->ptype);
  471. break;
  472. }
  473. const uint size = strlen(buf) + 1;
  474. char *res = GetCircBuf(size);
  475. memcpy(res,buf,size);
  476. return res;
  477. }
  478. //
  479. ///////////////////////////////////////////////////////////////////////////////
  480. /////////////// ScanGOptionsHelper() ///////////////
  481. ///////////////////////////////////////////////////////////////////////////////
  482. const GenericOptParm_t StandardGOptions[] =
  483. {
  484. { GOPT_O_TEST_OPT, 0, "@opt" },
  485. { GOPT_O_HELP, 0, "h|help" },
  486. { GOPT_O_CLEAR, 0, "c|clear" },
  487. { GOPT_O_PAGER, 0, "p|pager" },
  488. { GOPT_O_WIDTH, 1, "w|width" },
  489. { GOPT_O_QUIT, 0, "Q|quit" },
  490. { GOPT_O_QUIET, 0, "q|quiet" },
  491. { GOPT_O_VERBOSE, 3, "v|verbose" },
  492. { GOPT_O_DEBUG, 3, "D|d|debug" },
  493. {0,0,0}
  494. };
  495. ///////////////////////////////////////////////////////////////////////////////
  496. static int ScanGOffOn ( GenericOpt_t *go, int opt_stat, bool use_max, ccp optarg )
  497. {
  498. ccp object = "'off/on' value";
  499. char buf[100];
  500. if (go)
  501. {
  502. uint i;
  503. for ( i = 0; i < go->lopt_used; i++ )
  504. {
  505. struct option *lp = go->lopt + i;
  506. if ( lp->val == opt_stat && lp->has_arg && lp->name )
  507. {
  508. snprintf(buf,sizeof(buf),"'off/on' value of --%s",lp->name);
  509. object = buf;
  510. break;
  511. }
  512. }
  513. }
  514. const uint max_num = use_max ? ( opt_stat & GOPT_M_VALUE ) >> GOPT_S_VALUE : 0;
  515. return ScanKeywordOffOn(optarg,max_num,object);
  516. }
  517. ///////////////////////////////////////////////////////////////////////////////
  518. enumError ScanGOptionsHelper
  519. (
  520. GOptions_t *gopt, // valid pointer
  521. GenericOpt_t *go, // NULL or list with options
  522. // => ResetGenericOpt() is called at finish
  523. int argc, // argument counter
  524. char **argv, // list of arguments
  525. int max_param, // >=0: max allowed parameters
  526. ScanGOpt_t scan_opt, // scan options
  527. //--- call back functions
  528. ScanOptionFunc OnAddOptions, // not NULL: called once to add options
  529. ScanOptionFunc OnOption, // not NULL: called for each option
  530. ScanOptionFunc OnArgument, // not NULL: called for each argument
  531. void *any_ptr // NULL or user specific pointer for call back
  532. )
  533. {
  534. DASSERT(gopt);
  535. DASSERT(argc>1);
  536. DASSERT(argv);
  537. //--- setup options
  538. GenericOpt_t go0;
  539. if (!go)
  540. {
  541. go = &go0;
  542. InitializeGenericOpt( go, scan_opt & SGO_KNOWN ? '+' : 0 );
  543. }
  544. if (OnAddOptions)
  545. OnAddOptions(go,gopt,any_ptr,scan_opt,0);
  546. AddGenericOpt(go,StandardGOptions,true);
  547. if ( argc > 1 )
  548. {
  549. // skip command
  550. argc--;
  551. argv++;
  552. }
  553. optind = 0; // '0' is correct for GNU to reset scanning
  554. #if !defined(TEST) && !defined(DEBUG)
  555. opterr = 0; // suppress error messages to stderr
  556. #endif
  557. //--- check options
  558. InitializeGOptions(gopt,gopt->max_width);
  559. int err = 0, test_opt = 0;
  560. enumError stat = ERR_OK;
  561. while ( stat == ERR_OK )
  562. {
  563. int opt_stat = getopt_long(argc,argv,go->sopt,go->lopt,0);
  564. if ( opt_stat == -1 )
  565. break;
  566. // translate short option names
  567. if ( (uint)opt_stat < sizeof(go->sopt_id)/sizeof(*go->sopt_id) && go->sopt_id[opt_stat] )
  568. opt_stat = go->sopt_id[opt_stat];
  569. if ( opt_stat >= GOPT_RESERVED_BEG && opt_stat < GOPT_RESERVED_END )
  570. {
  571. const uint pidx = opt_stat & GOPT_M_INDEX;
  572. if ( pidx >= GOPT_N_PARAM )
  573. {
  574. printf("%s!!! %s: Parameter index %u must be <%u: %s%s\n",
  575. colout->warn, *argv,
  576. pidx, GOPT_N_PARAM, argv[optind-1],
  577. colout->reset );
  578. stat = ERR_SYNTAX;
  579. break;
  580. }
  581. GParam_t *par = gopt->param + pidx;
  582. const uint ptype = GetGOPType(opt_stat);
  583. switch(ptype)
  584. {
  585. //--- increment & decrement: indicator 'ptype' is always GOPT_IDX_INC
  586. case GOPT_IDX_INC0:
  587. if ( par->ptype != GOPT_IDX_INC )
  588. {
  589. par->ptype = GOPT_IDX_INC;
  590. par->sn = 1;
  591. }
  592. else
  593. par->sn = par->sn >= 0 ? par->sn+1 : 0;
  594. break;
  595. case GOPT_IDX_INC:
  596. if ( par->ptype != GOPT_IDX_INC )
  597. {
  598. par->ptype = GOPT_IDX_INC;
  599. par->sn = 1;
  600. }
  601. else
  602. par->sn = par->sn > 0 ? par->sn+1 : 1;
  603. break;
  604. case GOPT_IDX_DEC0:
  605. if ( par->ptype != GOPT_IDX_INC )
  606. {
  607. par->ptype = GOPT_IDX_INC;
  608. par->sn = -1;
  609. }
  610. else
  611. par->sn = par->sn <= 0 ? par->sn-1 : 0;
  612. break;
  613. case GOPT_IDX_DEC:
  614. if ( par->ptype != GOPT_IDX_INC )
  615. {
  616. par->ptype = GOPT_IDX_INC;
  617. par->sn = -1;
  618. }
  619. else
  620. par->sn = par->sn < 0 ? par->sn-1 : -1;
  621. break;
  622. //--- set values: indicator 'ptype' is GOPT_IDX_SINT or GOPT_IDX_S64
  623. case GOPT_IDX_SETP:
  624. par->ptype = GOPT_IDX_SINT;
  625. par->un = ( opt_stat & GOPT_M_VALUE ) >> GOPT_S_VALUE;
  626. break;
  627. case GOPT_IDX_SETM:
  628. par->ptype = GOPT_IDX_SINT;
  629. par->sn = - ( ( opt_stat & GOPT_M_VALUE ) >> GOPT_S_VALUE );
  630. break;
  631. case GOPT_IDX_SETP64:
  632. par->ptype = GOPT_IDX_S64;
  633. par->un64 = ( opt_stat & GOPT_M_VALUE ) >> GOPT_S_VALUE;
  634. break;
  635. case GOPT_IDX_SETM64:
  636. par->ptype = GOPT_IDX_S64;
  637. par->sn64 = - ( ( opt_stat & GOPT_M_VALUE ) >> GOPT_S_VALUE );
  638. break;
  639. //--- set bits: indicator 'ptype' is GOPT_IDX_UINT or GOPT_IDX_U64
  640. case GOPT_IDX_OR:
  641. if ( par->ptype != GOPT_IDX_UINT )
  642. {
  643. par->ptype = GOPT_IDX_UINT;
  644. par->un = 0;
  645. }
  646. par->un |= ( opt_stat & GOPT_M_VALUE ) >> GOPT_S_VALUE;
  647. break;
  648. case GOPT_IDX_SETBIT:
  649. {
  650. if ( par->ptype != GOPT_IDX_UINT )
  651. {
  652. par->ptype = GOPT_IDX_UINT;
  653. par->un = 0;
  654. }
  655. const uint bitnum = ( opt_stat & GOPT_M_VALUE ) >> GOPT_S_VALUE;
  656. if ( bitnum < 8*sizeof(par->un) )
  657. par->un |= 1u << bitnum;
  658. }
  659. break;
  660. case GOPT_IDX_OR64:
  661. if ( par->ptype != GOPT_IDX_U64 )
  662. {
  663. par->ptype = GOPT_IDX_U64;
  664. par->un64 = 0;
  665. }
  666. par->un64 |= ( opt_stat & GOPT_M_VALUE ) >> GOPT_S_VALUE;
  667. break;
  668. case GOPT_IDX_SETBIT64:
  669. if ( par->ptype != GOPT_IDX_U64 )
  670. {
  671. par->ptype = GOPT_IDX_U64;
  672. par->un64 = 0;
  673. }
  674. {
  675. const uint bitnum = ( opt_stat & GOPT_M_VALUE ) >> GOPT_S_VALUE;
  676. if ( bitnum < 64 )
  677. par->un64 |= 1llu << bitnum;
  678. }
  679. break;
  680. case GOPT_IDX_BIT:
  681. {
  682. if ( par->ptype != GOPT_IDX_BIT )
  683. {
  684. par->ptype = GOPT_IDX_BIT;
  685. par->bit.val = par->bit.mask = 0;
  686. }
  687. const int stat = ScanGOffOn(go,opt_stat,false,optarg);
  688. if ( stat >= 0 )
  689. {
  690. const uint bitnum = ( opt_stat & GOPT_M_VALUE ) >> GOPT_S_VALUE;
  691. if ( bitnum < 32 )
  692. {
  693. const u32 m = 1u << bitnum;
  694. par->bit.mask |= m;
  695. if ( stat > 0 )
  696. par->un |= m;
  697. else
  698. par->un &= ~m;
  699. }
  700. }
  701. }
  702. break;
  703. //--- scan parameters
  704. case GOPT_IDX_SINT:
  705. par->ptype = GOPT_IDX_SINT;
  706. par->sn = str2l(optarg,0,10);
  707. break;
  708. case GOPT_IDX_UINT:
  709. par->ptype = GOPT_IDX_UINT;
  710. par->un = str2ul(optarg,0,10);
  711. break;
  712. case GOPT_IDX_OFF_ON:
  713. {
  714. const int stat = ScanGOffOn(go,opt_stat,true,optarg);
  715. par->un = stat >= 0 ? stat : 0;
  716. par->ptype = GOPT_IDX_UINT;
  717. }
  718. break;
  719. case GOPT_IDX_S64:
  720. par->ptype = GOPT_IDX_S64;
  721. par->sn64 = str2ll(optarg,0,10);
  722. break;
  723. case GOPT_IDX_U64:
  724. par->ptype = GOPT_IDX_U64;
  725. par->un64 = str2ull(optarg,0,10);
  726. break;
  727. case GOPT_IDX_HEX:
  728. par->ptype = GOPT_IDX_HEX;
  729. par->un64 = str2ull(optarg,0,16);
  730. break;
  731. case GOPT_IDX_SIZE:
  732. par->ptype = GOPT_IDX_SIZE;
  733. ScanSizeU64(&par->un64,optarg,1,1,0); break;
  734. break;
  735. case GOPT_IDX_DOUBLE:
  736. par->ptype = GOPT_IDX_DOUBLE;
  737. par->d = strtod(optarg,0);
  738. break;
  739. case GOPT_IDX_DURATION:
  740. par->ptype = GOPT_IDX_DURATION;
  741. ScanDuration(&par->d,optarg,1,SDUMD_M_DEFAULT); break;
  742. break;
  743. case GOPT_IDX_STRING:
  744. par->ptype = GOPT_IDX_STRING;
  745. par->str = optarg;
  746. break;
  747. default:
  748. printf("!!! %s: Undefinef GOPT_* type 0x%02x [0x%02x]: %s\n",
  749. *argv, GetGOPType(opt_stat), opt_stat, argv[optind-1] );
  750. stat = ERR_ERROR;
  751. }
  752. par->vtype = GetGParamValueType(par->ptype);
  753. continue;
  754. }
  755. switch (opt_stat)
  756. {
  757. case '?':
  758. if ( optopt && (uint)optopt < 0x7f )
  759. printf("%s!!! %s: Invalid option: %s-%c [0x%02x]%s\n",
  760. colout->warn, *argv,
  761. colout->value, optopt, optopt,
  762. colout->reset );
  763. else
  764. printf("%s!!! %s: Invalid option: %s%s%s\n",
  765. colout->warn, *argv,
  766. colout->value, argv[optind-1],
  767. colout->reset );
  768. stat = ERR_SYNTAX;
  769. break;
  770. case ':':
  771. printf("%s!!! %s: Missing parameter for %s%s%s\n",
  772. colout->warn, *argv,
  773. colout->value, argv[optind-1],
  774. colout->reset );
  775. stat = ERR_SYNTAX;
  776. break;
  777. //--- standard options
  778. case GOPT_O_QUIET:
  779. gopt->verbose = gopt->verbose > -1 ? -1 : gopt->verbose - 1;
  780. break;
  781. case GOPT_O_VERBOSE:
  782. if (optarg)
  783. gopt->verbose = str2l(optarg,0,10);
  784. else
  785. gopt->verbose = gopt->verbose < 0 ? 0 : gopt->verbose + 1;
  786. break;
  787. case GOPT_O_DEBUG:
  788. if (optarg)
  789. gopt->debug = str2l(optarg,0,10);
  790. else
  791. gopt->debug++;
  792. break;
  793. case GOPT_O_WIDTH:
  794. {
  795. char *end;
  796. int num = str2l(optarg,&end,10);
  797. if ( num > 0 && !*end )
  798. gopt->width = num < 40 ? 40 : num <= gopt->max_width ? num : gopt->max_width;
  799. }
  800. break;
  801. case GOPT_O_HELP: gopt->help++; scan_opt |= SGO_PAGER; break;
  802. case GOPT_O_CLEAR: gopt->clear++; break;
  803. case GOPT_O_PAGER: gopt->pager++; break;
  804. case GOPT_O_QUIT: gopt->quit++; break;
  805. case GOPT_O_TEST_OPT: test_opt++; break;
  806. //--- misc
  807. default:
  808. if (OnOption)
  809. {
  810. const int ostat = OnOption(go,gopt,any_ptr,opt_stat,optarg);
  811. if ( ostat <= 0 )
  812. {
  813. if (!ostat)
  814. printf("!!! %s: Internal error while scanning options [0x%02x]!\n",
  815. *argv, opt_stat );
  816. stat = ERR_ERROR;
  817. }
  818. }
  819. break;
  820. }
  821. }
  822. if (test_opt)
  823. {
  824. printf("\n## %sShort options:%s %s\n## %sLong options (%u/%u):%s\n",
  825. colout->caption, colout->reset, go->sopt,
  826. colout->caption, go->lopt_used, go->lopt_size, colout->reset );
  827. for ( const struct option *opt = go->lopt; opt->name; opt++ )
  828. printf("## %6x %c %s\n", opt->val,
  829. opt->has_arg ? '0'+opt->has_arg : '-',
  830. opt->name );
  831. putchar('\n');
  832. }
  833. char **dest = argv + optind;
  834. gopt->argv = dest;
  835. uint i;
  836. for ( i = optind; i < argc; i++ )
  837. {
  838. char *arg = argv[i];
  839. if ( !OnArgument || !OnArgument(go,gopt,any_ptr,dest-gopt->argv,arg) )
  840. *dest++ = arg;
  841. }
  842. gopt->argc = dest - gopt->argv;
  843. if (err)
  844. {
  845. printf("%s!!! %s: Syntax error!%s\n",colout->warn,*argv,colout->reset);
  846. stat = ERR_SYNTAX;
  847. goto abort;
  848. }
  849. if ( max_param >= 0 && gopt->argc > max_param )
  850. {
  851. if (!max_param)
  852. printf(
  853. "%s!!! %s: No parameters allowed, but %u found!%s\n",
  854. colout->warn, *argv, gopt->argc, colout->reset );
  855. else
  856. printf(
  857. "%s!!! %s: Maximal %u parameter%s allowed, but %u found!%s\n",
  858. colout->warn, *argv,
  859. max_param, max_param == 1 ? "" : "s",
  860. gopt->argc, colout->reset );
  861. stat = ERR_SYNTAX;
  862. goto abort;
  863. }
  864. if ( scan_opt & SGO_PAGER && !gopt->pager )
  865. gopt->pager++;
  866. abort:
  867. ResetGenericOpt(go);
  868. if (stat)
  869. InitializeGOptions(gopt,gopt->max_width);
  870. return stat;
  871. }
  872. //
  873. ///////////////////////////////////////////////////////////////////////////////
  874. /////////////// END ///////////////
  875. ///////////////////////////////////////////////////////////////////////////////