ptype.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. return method_names[method];
  149. }
  150. /*--------------------------------------------------------- */
  151. clish_ptype_method_e clish_ptype_method_resolve(const char *name)
  152. {
  153. clish_ptype_method_e result = CLISH_PTYPE_REGEXP;
  154. if (NULL != name) {
  155. unsigned i;
  156. for (i = 0; i < CLISH_PTYPE_SELECT + 1; i++) {
  157. if (0 == strcmp(name, method_names[i])) {
  158. result = (clish_ptype_method_e) i;
  159. break;
  160. }
  161. }
  162. /* error for incorrect type spec */
  163. assert(i <= CLISH_PTYPE_SELECT);
  164. }
  165. return result;
  166. }
  167. /*--------------------------------------------------------- */
  168. static const char *preprocess_names[] = {
  169. "none",
  170. "toupper",
  171. "tolower"
  172. };
  173. /*--------------------------------------------------------- */
  174. const char *clish_ptype_preprocess__get_name(clish_ptype_preprocess_e
  175. preprocess)
  176. {
  177. return preprocess_names[preprocess];
  178. }
  179. /*--------------------------------------------------------- */
  180. clish_ptype_preprocess_e clish_ptype_preprocess_resolve(const char *name)
  181. {
  182. clish_ptype_preprocess_e result = CLISH_PTYPE_NONE;
  183. if (NULL != name) {
  184. unsigned i;
  185. for (i = 0; i < CLISH_PTYPE_TOLOWER + 1; i++) {
  186. if (0 == strcmp(name, preprocess_names[i])) {
  187. result = (clish_ptype_preprocess_e) i;
  188. break;
  189. }
  190. }
  191. /* error for incorrect type spec */
  192. assert((clish_ptype_preprocess_e) i <= CLISH_PTYPE_TOLOWER);
  193. }
  194. return result;
  195. }
  196. /*---------------------------------------------------------
  197. * PUBLIC METHODS
  198. *--------------------------------------------------------- */
  199. /*--------------------------------------------------------- */
  200. char *clish_ptype_word_generator(clish_ptype_t * this,
  201. const char *text, unsigned state)
  202. {
  203. char *result = NULL;
  204. if (0 == state) {
  205. /* first of all simply try to validate the result */
  206. result = clish_ptype_validate(this, text);
  207. }
  208. if (NULL == result) {
  209. switch (this->method) {
  210. /*--------------------------------------------- */
  211. case CLISH_PTYPE_SELECT:
  212. {
  213. if (0 == state) {
  214. this->last_name = 0;
  215. }
  216. while ((result =
  217. clish_ptype_select__get_name(this,
  218. this->
  219. last_name++)))
  220. {
  221. /* get the next item and check if it is a completion */
  222. if (result ==
  223. lub_string_nocasestr(result,
  224. text)) {
  225. /* found the next completion */
  226. break;
  227. }
  228. lub_string_free(result);
  229. }
  230. break;
  231. }
  232. /*--------------------------------------------- */
  233. case CLISH_PTYPE_INTEGER:
  234. case CLISH_PTYPE_UNSIGNEDINTEGER:
  235. case CLISH_PTYPE_REGEXP:
  236. {
  237. /* do nothing */
  238. break;
  239. }
  240. /*--------------------------------------------- */
  241. }
  242. }
  243. return result;
  244. }
  245. /*--------------------------------------------------------- */
  246. static char *clish_ptype_validate_or_translate(const clish_ptype_t * this,
  247. const char *text,
  248. bool_t translate)
  249. {
  250. char *result = lub_string_dup(text);
  251. assert(this->pattern);
  252. switch (this->preprocess) {
  253. /*----------------------------------------- */
  254. case CLISH_PTYPE_NONE:
  255. {
  256. break;
  257. }
  258. /*----------------------------------------- */
  259. case CLISH_PTYPE_TOUPPER:
  260. {
  261. unsigned char *p = result;
  262. while (*p) {
  263. /*lint -e155 Ignoring { }'ed sequence within an expression, 0 assumed
  264. * MACRO implementation uses braces to prevent multiple increments
  265. * when called.
  266. */
  267. *p = lub_ctype_toupper(*p);
  268. p++;
  269. }
  270. break;
  271. }
  272. /*----------------------------------------- */
  273. case CLISH_PTYPE_TOLOWER:
  274. {
  275. unsigned char *p = result;
  276. while (*p) {
  277. *p = lub_ctype_tolower(*p);
  278. p++;
  279. }
  280. break;
  281. }
  282. /*----------------------------------------- */
  283. }
  284. /*
  285. * now validate according the specified method
  286. */
  287. switch (this->method) {
  288. /*------------------------------------------------- */
  289. case CLISH_PTYPE_REGEXP:
  290. {
  291. /* test the regular expression against the string */
  292. /*lint -e64 Type mismatch (arg. no. 4) */
  293. /*
  294. * lint seems to equate regmatch_t[] as being of type regmatch_t !!!
  295. */
  296. if (0 != regexec(&this->u.regexp, result, 0, NULL, 0)) {
  297. lub_string_free(result);
  298. result = NULL;
  299. }
  300. /*lint +e64 */
  301. break;
  302. }
  303. /*------------------------------------------------- */
  304. case CLISH_PTYPE_INTEGER:
  305. {
  306. /* first of all check that this is a number */
  307. bool_t ok = BOOL_TRUE;
  308. const char *p = result;
  309. if (*p == '-') {
  310. p++;
  311. }
  312. while (*p) {
  313. if (!lub_ctype_isdigit(*p++)) {
  314. ok = BOOL_FALSE;
  315. break;
  316. }
  317. }
  318. if (BOOL_TRUE == ok) {
  319. /* convert and check the range */
  320. int value = atoi(result);
  321. if ((value < this->u.integer.min)
  322. || (value > this->u.integer.max)) {
  323. lub_string_free(result);
  324. result = NULL;
  325. }
  326. } else {
  327. lub_string_free(result);
  328. result = NULL;
  329. }
  330. break;
  331. }
  332. /*------------------------------------------------- */
  333. case CLISH_PTYPE_UNSIGNEDINTEGER:
  334. {
  335. /* first of all check that this is a number */
  336. bool_t ok = BOOL_TRUE;
  337. const char *p = result;
  338. if (*p == '-') {
  339. p++;
  340. }
  341. while (*p) {
  342. if (!lub_ctype_isdigit(*p++)) {
  343. ok = BOOL_FALSE;
  344. break;
  345. }
  346. }
  347. if (BOOL_TRUE == ok) {
  348. /* convert and check the range */
  349. unsigned int value = (unsigned int)atoi(result);
  350. if ((value < (unsigned)this->u.integer.min)
  351. || (value > (unsigned)this->u.integer.max)) {
  352. lub_string_free(result);
  353. result = NULL;
  354. }
  355. } else {
  356. lub_string_free(result);
  357. result = NULL;
  358. }
  359. break;
  360. }
  361. /*------------------------------------------------- */
  362. case CLISH_PTYPE_SELECT:
  363. {
  364. unsigned i;
  365. for (i = 0;
  366. i < lub_argv__get_count(this->u.select.items);
  367. i++) {
  368. unsigned char *name =
  369. clish_ptype_select__get_name(this, i);
  370. unsigned char *value =
  371. clish_ptype_select__get_value(this, i);
  372. int tmp = lub_string_nocasecmp(result, name);
  373. lub_string_free((BOOL_TRUE ==
  374. translate) ? name : value);
  375. if (0 == tmp) {
  376. lub_string_free(result);
  377. result =
  378. ((BOOL_TRUE ==
  379. translate) ? value : name);
  380. break;
  381. } else {
  382. lub_string_free((BOOL_TRUE ==
  383. translate) ? value :
  384. name);
  385. }
  386. }
  387. if (i == lub_argv__get_count(this->u.select.items)) {
  388. /* failed to find a match */
  389. lub_string_free(result);
  390. result = NULL;
  391. }
  392. break;
  393. }
  394. /*------------------------------------------------- */
  395. }
  396. return (char *)result;
  397. }
  398. /*--------------------------------------------------------- */
  399. static void
  400. clish_ptype_init(clish_ptype_t * this,
  401. const char *name,
  402. const char *text,
  403. const char *pattern,
  404. clish_ptype_method_e method,
  405. clish_ptype_preprocess_e preprocess)
  406. {
  407. assert(name);
  408. this->name = lub_string_dup(name);
  409. this->text = NULL;
  410. this->pattern = NULL;
  411. this->preprocess = preprocess;
  412. this->range = NULL;
  413. /* Be a good binary tree citizen */
  414. lub_bintree_node_init(&this->bt_node);
  415. if (NULL != pattern) {
  416. /* set the pattern for this type */
  417. clish_ptype__set_pattern(this, pattern, method);
  418. }
  419. if (NULL != text) {
  420. /* set the help text for this type */
  421. clish_ptype__set_text(this, text);
  422. }
  423. }
  424. /*--------------------------------------------------------- */
  425. char *clish_ptype_validate(const clish_ptype_t * this, const char *text)
  426. {
  427. return clish_ptype_validate_or_translate(this, text, BOOL_FALSE);
  428. }
  429. /*--------------------------------------------------------- */
  430. char *clish_ptype_translate(const clish_ptype_t * this, const char *text)
  431. {
  432. return clish_ptype_validate_or_translate(this, text, BOOL_TRUE);
  433. }
  434. /*--------------------------------------------------------- */
  435. clish_ptype_t *clish_ptype_new(const char *name,
  436. const char *help,
  437. const char *pattern,
  438. clish_ptype_method_e method,
  439. clish_ptype_preprocess_e preprocess)
  440. {
  441. clish_ptype_t *this = malloc(sizeof(clish_ptype_t));
  442. if (NULL != this) {
  443. clish_ptype_init(this, name, help, pattern, method, preprocess);
  444. }
  445. return this;
  446. }
  447. /*--------------------------------------------------------- */
  448. static void clish_ptype_fini(clish_ptype_t * this)
  449. {
  450. lub_string_free(this->name);
  451. this->name = NULL;
  452. lub_string_free(this->text);
  453. this->text = NULL;
  454. lub_string_free(this->pattern);
  455. this->pattern = NULL;
  456. lub_string_free(this->range);
  457. this->range = NULL;
  458. switch (this->method) {
  459. /*------------------------------------------------- */
  460. case CLISH_PTYPE_REGEXP:
  461. {
  462. regfree(&this->u.regexp);
  463. break;
  464. }
  465. /*------------------------------------------------- */
  466. case CLISH_PTYPE_INTEGER:
  467. case CLISH_PTYPE_UNSIGNEDINTEGER:
  468. {
  469. break;
  470. }
  471. /*------------------------------------------------- */
  472. case CLISH_PTYPE_SELECT:
  473. {
  474. lub_argv_delete(this->u.select.items);
  475. break;
  476. }
  477. /*------------------------------------------------- */
  478. }
  479. }
  480. /*--------------------------------------------------------- */
  481. void clish_ptype_delete(clish_ptype_t * this)
  482. {
  483. clish_ptype_fini(this);
  484. free(this);
  485. }
  486. /*--------------------------------------------------------- */
  487. const char *clish_ptype__get_name(const clish_ptype_t * this)
  488. {
  489. return (const char *)this->name;
  490. }
  491. /*--------------------------------------------------------- */
  492. const char *clish_ptype__get_text(const clish_ptype_t * this)
  493. {
  494. return (const char *)this->text;
  495. }
  496. /*--------------------------------------------------------- */
  497. void
  498. clish_ptype__set_pattern(clish_ptype_t * this,
  499. const char *pattern, clish_ptype_method_e method)
  500. {
  501. assert(NULL == this->pattern);
  502. this->method = method;
  503. switch (this->method) {
  504. /*------------------------------------------------- */
  505. case CLISH_PTYPE_REGEXP:
  506. {
  507. int result;
  508. /* only the expression is allowed */
  509. lub_string_cat(&this->pattern, "^");
  510. lub_string_cat(&this->pattern, pattern);
  511. lub_string_cat(&this->pattern, "$");
  512. /* compile the regular expression for later use */
  513. result =
  514. regcomp(&this->u.regexp, this->pattern,
  515. REG_NOSUB | REG_EXTENDED);
  516. assert(0 == result);
  517. break;
  518. }
  519. /*------------------------------------------------- */
  520. case CLISH_PTYPE_INTEGER:
  521. {
  522. /* default the range to that of an integer */
  523. this->u.integer.min = INT_MIN;
  524. this->u.integer.max = INT_MAX;
  525. this->pattern = lub_string_dup(pattern);
  526. /* now try and read the specified range */
  527. sscanf(this->pattern,
  528. "%d..%d",
  529. &this->u.integer.min, &this->u.integer.max);
  530. break;
  531. }
  532. /*------------------------------------------------- */
  533. case CLISH_PTYPE_UNSIGNEDINTEGER:
  534. {
  535. /* default the range to that of an unsigned integer */
  536. this->u.integer.min = 0;
  537. this->u.integer.max = (int)UINT_MAX;
  538. this->pattern = lub_string_dup(pattern);
  539. /* now try and read the specified range */
  540. sscanf(this->pattern,
  541. "%u..%u",
  542. (unsigned int *)&this->u.integer.min,
  543. (unsigned int *)&this->u.integer.max);
  544. break;
  545. }
  546. /*------------------------------------------------- */
  547. case CLISH_PTYPE_SELECT:
  548. {
  549. this->pattern = lub_string_dup(pattern);
  550. /* store a vector of item descriptors */
  551. this->u.select.items = lub_argv_new(this->pattern, 0);
  552. break;
  553. }
  554. /*------------------------------------------------- */
  555. }
  556. /* now set up the range details */
  557. clish_ptype__set_range(this);
  558. }
  559. /*--------------------------------------------------------- */
  560. void clish_ptype__set_text(clish_ptype_t * this, const char *text)
  561. {
  562. assert(NULL == this->text);
  563. this->text = lub_string_dup(text);
  564. }
  565. /*--------------------------------------------------------- */
  566. void
  567. clish_ptype__set_preprocess(clish_ptype_t * this,
  568. clish_ptype_preprocess_e preprocess)
  569. {
  570. this->preprocess = preprocess;
  571. }
  572. /*--------------------------------------------------------- */
  573. const char *clish_ptype__get_range(const clish_ptype_t * this)
  574. {
  575. return (const char *)this->range;
  576. }
  577. /*--------------------------------------------------------- */