ptype.c 15 KB

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