ptype.c 14 KB

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