1
0

ptype.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. unsigned 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. unsigned 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. unsigned char *name =
  372. clish_ptype_select__get_name(this, i);
  373. unsigned 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. lub_string_free(this->name);
  457. this->name = NULL;
  458. lub_string_free(this->text);
  459. this->text = NULL;
  460. lub_string_free(this->pattern);
  461. this->pattern = NULL;
  462. lub_string_free(this->range);
  463. this->range = NULL;
  464. switch (this->method) {
  465. /*------------------------------------------------- */
  466. case CLISH_PTYPE_REGEXP:
  467. {
  468. regfree(&this->u.regexp);
  469. break;
  470. }
  471. /*------------------------------------------------- */
  472. case CLISH_PTYPE_INTEGER:
  473. case CLISH_PTYPE_UNSIGNEDINTEGER:
  474. {
  475. break;
  476. }
  477. /*------------------------------------------------- */
  478. case CLISH_PTYPE_SELECT:
  479. {
  480. lub_argv_delete(this->u.select.items);
  481. break;
  482. }
  483. /*------------------------------------------------- */
  484. }
  485. }
  486. /*--------------------------------------------------------- */
  487. void clish_ptype_delete(clish_ptype_t * this)
  488. {
  489. clish_ptype_fini(this);
  490. free(this);
  491. }
  492. /*--------------------------------------------------------- */
  493. const char *clish_ptype__get_name(const clish_ptype_t * this)
  494. {
  495. return (const char *)this->name;
  496. }
  497. /*--------------------------------------------------------- */
  498. const char *clish_ptype__get_text(const clish_ptype_t * this)
  499. {
  500. return (const char *)this->text;
  501. }
  502. /*--------------------------------------------------------- */
  503. void
  504. clish_ptype__set_pattern(clish_ptype_t * this,
  505. const char *pattern, clish_ptype_method_e method)
  506. {
  507. assert(NULL == this->pattern);
  508. this->method = method;
  509. switch (this->method) {
  510. /*------------------------------------------------- */
  511. case CLISH_PTYPE_REGEXP:
  512. {
  513. int result;
  514. /* only the expression is allowed */
  515. lub_string_cat(&this->pattern, "^");
  516. lub_string_cat(&this->pattern, pattern);
  517. lub_string_cat(&this->pattern, "$");
  518. /* compile the regular expression for later use */
  519. result =
  520. regcomp(&this->u.regexp, this->pattern,
  521. REG_NOSUB | REG_EXTENDED);
  522. assert(0 == result);
  523. break;
  524. }
  525. /*------------------------------------------------- */
  526. case CLISH_PTYPE_INTEGER:
  527. {
  528. /* default the range to that of an integer */
  529. this->u.integer.min = INT_MIN;
  530. this->u.integer.max = INT_MAX;
  531. this->pattern = lub_string_dup(pattern);
  532. /* now try and read the specified range */
  533. sscanf(this->pattern,
  534. "%d..%d",
  535. &this->u.integer.min, &this->u.integer.max);
  536. break;
  537. }
  538. /*------------------------------------------------- */
  539. case CLISH_PTYPE_UNSIGNEDINTEGER:
  540. {
  541. /* default the range to that of an unsigned integer */
  542. this->u.integer.min = 0;
  543. this->u.integer.max = (int)UINT_MAX;
  544. this->pattern = lub_string_dup(pattern);
  545. /* now try and read the specified range */
  546. sscanf(this->pattern,
  547. "%u..%u",
  548. (unsigned int *)&this->u.integer.min,
  549. (unsigned int *)&this->u.integer.max);
  550. break;
  551. }
  552. /*------------------------------------------------- */
  553. case CLISH_PTYPE_SELECT:
  554. {
  555. this->pattern = lub_string_dup(pattern);
  556. /* store a vector of item descriptors */
  557. this->u.select.items = lub_argv_new(this->pattern, 0);
  558. break;
  559. }
  560. /*------------------------------------------------- */
  561. }
  562. /* now set up the range details */
  563. clish_ptype__set_range(this);
  564. }
  565. /*--------------------------------------------------------- */
  566. void clish_ptype__set_text(clish_ptype_t * this, const char *text)
  567. {
  568. assert(NULL == this->text);
  569. this->text = lub_string_dup(text);
  570. }
  571. /*--------------------------------------------------------- */
  572. void
  573. clish_ptype__set_preprocess(clish_ptype_t * this,
  574. clish_ptype_preprocess_e preprocess)
  575. {
  576. this->preprocess = preprocess;
  577. }
  578. /*--------------------------------------------------------- */
  579. const char *clish_ptype__get_range(const clish_ptype_t * this)
  580. {
  581. return (const char *)this->range;
  582. }
  583. /*--------------------------------------------------------- */