ptype.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * ptype.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include "lub/ctype.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <limits.h>
  11. #include <stdio.h>
  12. /*---------------------------------------------------------
  13. * PRIVATE METHODS
  14. *--------------------------------------------------------- */
  15. static char *clish_ptype_select__get_name(const clish_ptype_t * this,
  16. unsigned index)
  17. {
  18. char *result = NULL;
  19. const char *arg = lub_argv__get_arg(this->u.select.items, index);
  20. if (NULL != arg) {
  21. size_t name_len = 0;
  22. const char *lbrk = strchr(arg, '(');
  23. if (NULL != lbrk) {
  24. name_len = (size_t) (lbrk - arg);
  25. }
  26. assert(name_len < strlen(arg)); /* check for syntax error */
  27. result = lub_string_dupn(arg, name_len);
  28. }
  29. return result;
  30. }
  31. /*--------------------------------------------------------- */
  32. static char *clish_ptype_select__get_value(const clish_ptype_t * this,
  33. unsigned index)
  34. {
  35. char *result = NULL;
  36. const char *arg = lub_argv__get_arg(this->u.select.items, index);
  37. if (NULL != arg) {
  38. const char *lbrk = strchr(arg, '(');
  39. const char *rbrk = strchr(arg, ')');
  40. const char *value = NULL;
  41. size_t value_len = 0;
  42. if (lbrk) {
  43. value = lbrk + 1;
  44. if (rbrk) {
  45. value_len = (size_t) (rbrk - value);
  46. }
  47. }
  48. assert(value_len < strlen(arg)); /* check for syntax error */
  49. result = lub_string_dupn(value, value_len);
  50. }
  51. return result;
  52. }
  53. /*--------------------------------------------------------- */
  54. static void clish_ptype__set_range(clish_ptype_t * this)
  55. {
  56. char tmp[80];
  57. /* now set up the range values */
  58. switch (this->method) {
  59. /*------------------------------------------------- */
  60. case CLISH_PTYPE_REGEXP:
  61. {
  62. /*
  63. * nothing more to do
  64. */
  65. break;
  66. }
  67. /*------------------------------------------------- */
  68. case CLISH_PTYPE_INTEGER:
  69. {
  70. /*
  71. * Setup the integer range
  72. */
  73. sprintf(tmp,
  74. "%d..%d",
  75. this->u.integer.min, this->u.integer.max);
  76. this->range = lub_string_dup(tmp);
  77. break;
  78. }
  79. /*------------------------------------------------- */
  80. case CLISH_PTYPE_UNSIGNEDINTEGER:
  81. {
  82. /*
  83. * Setup the unsigned integer range
  84. */
  85. sprintf(tmp,
  86. "%u..%u",
  87. (unsigned int)this->u.integer.min,
  88. (unsigned int)this->u.integer.max);
  89. this->range = lub_string_dup(tmp);
  90. break;
  91. }
  92. /*------------------------------------------------- */
  93. case CLISH_PTYPE_SELECT:
  94. {
  95. /*
  96. * Setup the selection values to the help text
  97. */
  98. unsigned i;
  99. for (i = 0;
  100. i < lub_argv__get_count(this->u.select.items);
  101. i++) {
  102. char *p = tmp;
  103. char *name =
  104. clish_ptype_select__get_name(this, i);
  105. if (i > 0) {
  106. p += sprintf(p, "/");
  107. }
  108. p += sprintf(p, "%s", name);
  109. lub_string_cat(&this->range, tmp);
  110. lub_string_free(name);
  111. }
  112. break;
  113. }
  114. /*------------------------------------------------- */
  115. }
  116. }
  117. /*---------------------------------------------------------
  118. * PUBLIC META FUNCTIONS
  119. *--------------------------------------------------------- */
  120. int clish_ptype_bt_compare(const void *clientnode, const void *clientkey)
  121. {
  122. const clish_ptype_t *this = clientnode;
  123. const char *key = clientkey;
  124. return strcmp(this->name, key);
  125. }
  126. /*-------------------------------------------------------- */
  127. void clish_ptype_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  128. {
  129. const clish_ptype_t *this = clientnode;
  130. /* fill out the opaque key */
  131. strcpy((char *)key, this->name);
  132. }
  133. /*--------------------------------------------------------- */
  134. size_t clish_ptype_bt_offset(void)
  135. {
  136. return offsetof(clish_ptype_t, bt_node);
  137. }
  138. /*--------------------------------------------------------- */
  139. static const char *method_names[] = {
  140. "regexp",
  141. "integer",
  142. "unsignedInteger",
  143. "select"
  144. };
  145. /*--------------------------------------------------------- */
  146. const char *clish_ptype_method__get_name(clish_ptype_method_e method)
  147. {
  148. int max_method = sizeof(method_names) / sizeof(char *);
  149. if (method >= max_method)
  150. return NULL;
  151. return method_names[method];
  152. }
  153. /*--------------------------------------------------------- */
  154. clish_ptype_method_e clish_ptype_method_resolve(const char *name)
  155. {
  156. clish_ptype_method_e result = CLISH_PTYPE_REGEXP;
  157. if (NULL != name) {
  158. unsigned i;
  159. for (i = 0; i < CLISH_PTYPE_SELECT + 1; i++) {
  160. if (0 == strcmp(name, method_names[i])) {
  161. result = (clish_ptype_method_e) i;
  162. break;
  163. }
  164. }
  165. /* error for incorrect type spec */
  166. assert(i <= CLISH_PTYPE_SELECT);
  167. }
  168. return result;
  169. }
  170. /*--------------------------------------------------------- */
  171. static const char *preprocess_names[] = {
  172. "none",
  173. "toupper",
  174. "tolower"
  175. };
  176. /*--------------------------------------------------------- */
  177. const char *clish_ptype_preprocess__get_name(clish_ptype_preprocess_e
  178. preprocess)
  179. {
  180. return preprocess_names[preprocess];
  181. }
  182. /*--------------------------------------------------------- */
  183. clish_ptype_preprocess_e clish_ptype_preprocess_resolve(const char *name)
  184. {
  185. clish_ptype_preprocess_e result = CLISH_PTYPE_NONE;
  186. if (NULL != name) {
  187. unsigned i;
  188. for (i = 0; i < CLISH_PTYPE_TOLOWER + 1; i++) {
  189. if (0 == strcmp(name, preprocess_names[i])) {
  190. result = (clish_ptype_preprocess_e) i;
  191. break;
  192. }
  193. }
  194. /* error for incorrect type spec */
  195. assert((clish_ptype_preprocess_e) i <= CLISH_PTYPE_TOLOWER);
  196. }
  197. return result;
  198. }
  199. /*---------------------------------------------------------
  200. * PUBLIC METHODS
  201. *--------------------------------------------------------- */
  202. /*--------------------------------------------------------- */
  203. char *clish_ptype_word_generator(clish_ptype_t * this,
  204. const char *text, unsigned state)
  205. {
  206. char *result = NULL;
  207. if (0 == state) {
  208. /* first of all simply try to validate the result */
  209. result = clish_ptype_validate(this, text);
  210. }
  211. if (NULL == result) {
  212. switch (this->method) {
  213. /*--------------------------------------------- */
  214. case CLISH_PTYPE_SELECT:
  215. {
  216. if (0 == state) {
  217. this->last_name = 0;
  218. }
  219. while ((result =
  220. clish_ptype_select__get_name(this,
  221. this->
  222. last_name++)))
  223. {
  224. /* get the next item and check if it is a completion */
  225. if (result ==
  226. lub_string_nocasestr(result,
  227. text)) {
  228. /* found the next completion */
  229. break;
  230. }
  231. lub_string_free(result);
  232. }
  233. break;
  234. }
  235. /*--------------------------------------------- */
  236. case CLISH_PTYPE_INTEGER:
  237. case CLISH_PTYPE_UNSIGNEDINTEGER:
  238. case CLISH_PTYPE_REGEXP:
  239. {
  240. /* do nothing */
  241. break;
  242. }
  243. /*--------------------------------------------- */
  244. }
  245. }
  246. return result;
  247. }
  248. /*--------------------------------------------------------- */
  249. static char *clish_ptype_validate_or_translate(const clish_ptype_t * this,
  250. const char *text,
  251. bool_t translate)
  252. {
  253. char *result = lub_string_dup(text);
  254. assert(this->pattern);
  255. switch (this->preprocess) {
  256. /*----------------------------------------- */
  257. case CLISH_PTYPE_NONE:
  258. {
  259. break;
  260. }
  261. /*----------------------------------------- */
  262. case CLISH_PTYPE_TOUPPER:
  263. {
  264. char *p = result;
  265. while (*p) {
  266. /*lint -e155 Ignoring { }'ed sequence within an expression, 0 assumed
  267. * MACRO implementation uses braces to prevent multiple increments
  268. * when called.
  269. */
  270. *p = lub_ctype_toupper(*p);
  271. p++;
  272. }
  273. break;
  274. }
  275. /*----------------------------------------- */
  276. case CLISH_PTYPE_TOLOWER:
  277. {
  278. char *p = result;
  279. while (*p) {
  280. *p = lub_ctype_tolower(*p);
  281. p++;
  282. }
  283. break;
  284. }
  285. /*----------------------------------------- */
  286. }
  287. /*
  288. * now validate according the specified method
  289. */
  290. switch (this->method) {
  291. /*------------------------------------------------- */
  292. case CLISH_PTYPE_REGEXP:
  293. {
  294. /* test the regular expression against the string */
  295. /*lint -e64 Type mismatch (arg. no. 4) */
  296. /*
  297. * lint seems to equate regmatch_t[] as being of type regmatch_t !!!
  298. */
  299. if (0 != regexec(&this->u.regexp, result, 0, NULL, 0)) {
  300. lub_string_free(result);
  301. result = NULL;
  302. }
  303. /*lint +e64 */
  304. break;
  305. }
  306. /*------------------------------------------------- */
  307. case CLISH_PTYPE_INTEGER:
  308. {
  309. /* first of all check that this is a number */
  310. bool_t ok = BOOL_TRUE;
  311. const char *p = result;
  312. if (*p == '-') {
  313. p++;
  314. }
  315. while (*p) {
  316. if (!lub_ctype_isdigit(*p++)) {
  317. ok = BOOL_FALSE;
  318. break;
  319. }
  320. }
  321. if (BOOL_TRUE == ok) {
  322. /* convert and check the range */
  323. int value = atoi(result);
  324. if ((value < this->u.integer.min)
  325. || (value > this->u.integer.max)) {
  326. lub_string_free(result);
  327. result = NULL;
  328. }
  329. } else {
  330. lub_string_free(result);
  331. result = NULL;
  332. }
  333. break;
  334. }
  335. /*------------------------------------------------- */
  336. case CLISH_PTYPE_UNSIGNEDINTEGER:
  337. {
  338. /* first of all check that this is a number */
  339. bool_t ok = BOOL_TRUE;
  340. const char *p = result;
  341. if (*p == '-') {
  342. p++;
  343. }
  344. while (*p) {
  345. if (!lub_ctype_isdigit(*p++)) {
  346. ok = BOOL_FALSE;
  347. break;
  348. }
  349. }
  350. if (BOOL_TRUE == ok) {
  351. /* convert and check the range */
  352. unsigned int value = (unsigned int)atoi(result);
  353. if ((value < (unsigned)this->u.integer.min)
  354. || (value > (unsigned)this->u.integer.max)) {
  355. lub_string_free(result);
  356. result = NULL;
  357. }
  358. } else {
  359. lub_string_free(result);
  360. result = NULL;
  361. }
  362. break;
  363. }
  364. /*------------------------------------------------- */
  365. case CLISH_PTYPE_SELECT:
  366. {
  367. unsigned i;
  368. for (i = 0;
  369. i < lub_argv__get_count(this->u.select.items);
  370. i++) {
  371. char *name =
  372. clish_ptype_select__get_name(this, i);
  373. char *value =
  374. clish_ptype_select__get_value(this, i);
  375. int tmp = lub_string_nocasecmp(result, name);
  376. lub_string_free((BOOL_TRUE ==
  377. translate) ? name : value);
  378. if (0 == tmp) {
  379. lub_string_free(result);
  380. result =
  381. ((BOOL_TRUE ==
  382. translate) ? value : name);
  383. break;
  384. } else {
  385. lub_string_free((BOOL_TRUE ==
  386. translate) ? value :
  387. name);
  388. }
  389. }
  390. if (i == lub_argv__get_count(this->u.select.items)) {
  391. /* failed to find a match */
  392. lub_string_free(result);
  393. result = NULL;
  394. }
  395. break;
  396. }
  397. /*------------------------------------------------- */
  398. }
  399. return (char *)result;
  400. }
  401. /*--------------------------------------------------------- */
  402. static void
  403. clish_ptype_init(clish_ptype_t * this,
  404. const char *name,
  405. const char *text,
  406. const char *pattern,
  407. clish_ptype_method_e method,
  408. clish_ptype_preprocess_e preprocess)
  409. {
  410. assert(name);
  411. this->name = lub_string_dup(name);
  412. this->text = NULL;
  413. this->pattern = NULL;
  414. this->preprocess = preprocess;
  415. this->range = NULL;
  416. /* Be a good binary tree citizen */
  417. lub_bintree_node_init(&this->bt_node);
  418. if (NULL != pattern) {
  419. /* set the pattern for this type */
  420. clish_ptype__set_pattern(this, pattern, method);
  421. } else {
  422. /* The method is regexp by default */
  423. this->method = CLISH_PTYPE_REGEXP;
  424. }
  425. if (NULL != text) {
  426. /* set the help text for this type */
  427. clish_ptype__set_text(this, text);
  428. }
  429. }
  430. /*--------------------------------------------------------- */
  431. char *clish_ptype_validate(const clish_ptype_t * this, const char *text)
  432. {
  433. return clish_ptype_validate_or_translate(this, text, BOOL_FALSE);
  434. }
  435. /*--------------------------------------------------------- */
  436. char *clish_ptype_translate(const clish_ptype_t * this, const char *text)
  437. {
  438. return clish_ptype_validate_or_translate(this, text, BOOL_TRUE);
  439. }
  440. /*--------------------------------------------------------- */
  441. clish_ptype_t *clish_ptype_new(const char *name,
  442. const char *help,
  443. const char *pattern,
  444. clish_ptype_method_e method,
  445. clish_ptype_preprocess_e preprocess)
  446. {
  447. clish_ptype_t *this = malloc(sizeof(clish_ptype_t));
  448. if (NULL != this) {
  449. clish_ptype_init(this, name, help, pattern, method, preprocess);
  450. }
  451. return this;
  452. }
  453. /*--------------------------------------------------------- */
  454. static void clish_ptype_fini(clish_ptype_t * this)
  455. {
  456. if (this->pattern) {
  457. switch (this->method) {
  458. /*------------------------------------------------- */
  459. case CLISH_PTYPE_REGEXP:
  460. regfree(&this->u.regexp);
  461. break;
  462. /*------------------------------------------------- */
  463. case CLISH_PTYPE_INTEGER:
  464. case CLISH_PTYPE_UNSIGNEDINTEGER:
  465. break;
  466. /*------------------------------------------------- */
  467. case CLISH_PTYPE_SELECT:
  468. lub_argv_delete(this->u.select.items);
  469. break;
  470. /*------------------------------------------------- */
  471. }
  472. }
  473. lub_string_free(this->name);
  474. this->name = NULL;
  475. lub_string_free(this->text);
  476. this->text = NULL;
  477. lub_string_free(this->pattern);
  478. this->pattern = NULL;
  479. lub_string_free(this->range);
  480. this->range = NULL;
  481. }
  482. /*--------------------------------------------------------- */
  483. void clish_ptype_delete(clish_ptype_t * this)
  484. {
  485. clish_ptype_fini(this);
  486. free(this);
  487. }
  488. /*--------------------------------------------------------- */
  489. const char *clish_ptype__get_name(const clish_ptype_t * this)
  490. {
  491. return (const char *)this->name;
  492. }
  493. /*--------------------------------------------------------- */
  494. const char *clish_ptype__get_text(const clish_ptype_t * this)
  495. {
  496. return (const char *)this->text;
  497. }
  498. /*--------------------------------------------------------- */
  499. void
  500. clish_ptype__set_pattern(clish_ptype_t * this,
  501. const char *pattern, clish_ptype_method_e method)
  502. {
  503. assert(NULL == this->pattern);
  504. this->method = method;
  505. switch (this->method) {
  506. /*------------------------------------------------- */
  507. case CLISH_PTYPE_REGEXP:
  508. {
  509. int result;
  510. /* only the expression is allowed */
  511. lub_string_cat(&this->pattern, "^");
  512. lub_string_cat(&this->pattern, pattern);
  513. lub_string_cat(&this->pattern, "$");
  514. /* compile the regular expression for later use */
  515. result =
  516. regcomp(&this->u.regexp, this->pattern,
  517. REG_NOSUB | REG_EXTENDED);
  518. assert(0 == result);
  519. break;
  520. }
  521. /*------------------------------------------------- */
  522. case CLISH_PTYPE_INTEGER:
  523. {
  524. /* default the range to that of an integer */
  525. this->u.integer.min = INT_MIN;
  526. this->u.integer.max = INT_MAX;
  527. this->pattern = lub_string_dup(pattern);
  528. /* now try and read the specified range */
  529. sscanf(this->pattern,
  530. "%d..%d",
  531. &this->u.integer.min, &this->u.integer.max);
  532. break;
  533. }
  534. /*------------------------------------------------- */
  535. case CLISH_PTYPE_UNSIGNEDINTEGER:
  536. {
  537. /* default the range to that of an unsigned integer */
  538. this->u.integer.min = 0;
  539. this->u.integer.max = (int)UINT_MAX;
  540. this->pattern = lub_string_dup(pattern);
  541. /* now try and read the specified range */
  542. sscanf(this->pattern,
  543. "%u..%u",
  544. (unsigned int *)&this->u.integer.min,
  545. (unsigned int *)&this->u.integer.max);
  546. break;
  547. }
  548. /*------------------------------------------------- */
  549. case CLISH_PTYPE_SELECT:
  550. {
  551. this->pattern = lub_string_dup(pattern);
  552. /* store a vector of item descriptors */
  553. this->u.select.items = lub_argv_new(this->pattern, 0);
  554. break;
  555. }
  556. /*------------------------------------------------- */
  557. }
  558. /* now set up the range details */
  559. clish_ptype__set_range(this);
  560. }
  561. /*--------------------------------------------------------- */
  562. void clish_ptype__set_text(clish_ptype_t * this, const char *text)
  563. {
  564. assert(NULL == this->text);
  565. this->text = lub_string_dup(text);
  566. }
  567. /*--------------------------------------------------------- */
  568. void
  569. clish_ptype__set_preprocess(clish_ptype_t * this,
  570. clish_ptype_preprocess_e preprocess)
  571. {
  572. this->preprocess = preprocess;
  573. }
  574. /*--------------------------------------------------------- */
  575. const char *clish_ptype__get_range(const clish_ptype_t * this)
  576. {
  577. return (const char *)this->range;
  578. }
  579. /*--------------------------------------------------------- */