ptype.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. 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 && *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 && *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. if (*p == '-')
  303. p++;
  304. while (*p) {
  305. if (!lub_ctype_isdigit(*p++)) {
  306. ok = BOOL_FALSE;
  307. break;
  308. }
  309. }
  310. if (BOOL_TRUE == ok) {
  311. /* convert and check the range */
  312. unsigned int value = (unsigned int)atoi(result);
  313. if ((value < (unsigned)this->u.integer.min)
  314. || (value > (unsigned)this->u.integer.max)) {
  315. lub_string_free(result);
  316. result = NULL;
  317. }
  318. } else {
  319. lub_string_free(result);
  320. result = NULL;
  321. }
  322. break;
  323. }
  324. /*------------------------------------------------- */
  325. case CLISH_PTYPE_SELECT:
  326. {
  327. unsigned i;
  328. for (i = 0; i < lub_argv__get_count(this->u.select.items);
  329. i++) {
  330. char *name = clish_ptype_select__get_name(this, i);
  331. char *value = clish_ptype_select__get_value(this, i);
  332. int tmp = lub_string_nocasecmp(result, name);
  333. lub_string_free((BOOL_TRUE ==
  334. translate) ? name : value);
  335. if (0 == tmp) {
  336. lub_string_free(result);
  337. result = ((BOOL_TRUE ==
  338. translate) ? value : name);
  339. break;
  340. } else {
  341. lub_string_free((BOOL_TRUE ==
  342. translate) ? value : name);
  343. }
  344. }
  345. if (i == lub_argv__get_count(this->u.select.items)) {
  346. /* failed to find a match */
  347. lub_string_free(result);
  348. result = NULL;
  349. }
  350. break;
  351. }
  352. /*------------------------------------------------- */
  353. }
  354. return (char *)result;
  355. }
  356. /*--------------------------------------------------------- */
  357. static void clish_ptype_init(clish_ptype_t * this,
  358. const char *name, const char *text, const char *pattern,
  359. clish_ptype_method_e method, clish_ptype_preprocess_e preprocess)
  360. {
  361. assert(name);
  362. this->name = lub_string_dup(name);
  363. this->text = NULL;
  364. this->pattern = NULL;
  365. this->preprocess = preprocess;
  366. this->range = NULL;
  367. /* Be a good binary tree citizen */
  368. lub_bintree_node_init(&this->bt_node);
  369. if (pattern) {
  370. /* set the pattern for this type */
  371. clish_ptype__set_pattern(this, pattern, method);
  372. } else {
  373. /* The method is regexp by default */
  374. this->method = CLISH_PTYPE_REGEXP;
  375. }
  376. /* set the help text for this type */
  377. if (text)
  378. clish_ptype__set_text(this, text);
  379. }
  380. /*--------------------------------------------------------- */
  381. char *clish_ptype_validate(const clish_ptype_t * this, const char *text)
  382. {
  383. return clish_ptype_validate_or_translate(this, text, BOOL_FALSE);
  384. }
  385. /*--------------------------------------------------------- */
  386. char *clish_ptype_translate(const clish_ptype_t * this, const char *text)
  387. {
  388. return clish_ptype_validate_or_translate(this, text, BOOL_TRUE);
  389. }
  390. /*--------------------------------------------------------- */
  391. clish_ptype_t *clish_ptype_new(const char *name,
  392. const char *help, const char *pattern,
  393. clish_ptype_method_e method, clish_ptype_preprocess_e preprocess)
  394. {
  395. clish_ptype_t *this = malloc(sizeof(clish_ptype_t));
  396. if (this)
  397. clish_ptype_init(this, name, help, pattern, method, preprocess);
  398. return this;
  399. }
  400. /*--------------------------------------------------------- */
  401. static void clish_ptype_fini(clish_ptype_t * this)
  402. {
  403. if (this->pattern) {
  404. switch (this->method) {
  405. case CLISH_PTYPE_REGEXP:
  406. regfree(&this->u.regexp);
  407. break;
  408. case CLISH_PTYPE_INTEGER:
  409. case CLISH_PTYPE_UNSIGNEDINTEGER:
  410. break;
  411. case CLISH_PTYPE_SELECT:
  412. lub_argv_delete(this->u.select.items);
  413. break;
  414. }
  415. }
  416. lub_string_free(this->name);
  417. this->name = NULL;
  418. lub_string_free(this->text);
  419. this->text = NULL;
  420. lub_string_free(this->pattern);
  421. this->pattern = NULL;
  422. lub_string_free(this->range);
  423. this->range = NULL;
  424. }
  425. /*--------------------------------------------------------- */
  426. void clish_ptype_delete(clish_ptype_t * this)
  427. {
  428. clish_ptype_fini(this);
  429. free(this);
  430. }
  431. /*--------------------------------------------------------- */
  432. const char *clish_ptype__get_name(const clish_ptype_t * this)
  433. {
  434. return (const char *)this->name;
  435. }
  436. /*--------------------------------------------------------- */
  437. const char *clish_ptype__get_text(const clish_ptype_t * this)
  438. {
  439. return (const char *)this->text;
  440. }
  441. /*--------------------------------------------------------- */
  442. void
  443. clish_ptype__set_pattern(clish_ptype_t * this,
  444. const char *pattern, clish_ptype_method_e method)
  445. {
  446. assert(NULL == this->pattern);
  447. this->method = method;
  448. switch (this->method) {
  449. /*------------------------------------------------- */
  450. case CLISH_PTYPE_REGEXP:
  451. {
  452. int result;
  453. /* only the expression is allowed */
  454. lub_string_cat(&this->pattern, "^");
  455. lub_string_cat(&this->pattern, pattern);
  456. lub_string_cat(&this->pattern, "$");
  457. /* compile the regular expression for later use */
  458. result = regcomp(&this->u.regexp, this->pattern,
  459. REG_NOSUB | REG_EXTENDED);
  460. assert(0 == result);
  461. break;
  462. }
  463. /*------------------------------------------------- */
  464. case CLISH_PTYPE_INTEGER:
  465. /* default the range to that of an integer */
  466. this->u.integer.min = INT_MIN;
  467. this->u.integer.max = INT_MAX;
  468. this->pattern = lub_string_dup(pattern);
  469. /* now try and read the specified range */
  470. sscanf(this->pattern, "%d..%d",
  471. &this->u.integer.min, &this->u.integer.max);
  472. break;
  473. /*------------------------------------------------- */
  474. case CLISH_PTYPE_UNSIGNEDINTEGER:
  475. /* default the range to that of an unsigned integer */
  476. this->u.integer.min = 0;
  477. this->u.integer.max = (int)UINT_MAX;
  478. this->pattern = lub_string_dup(pattern);
  479. /* now try and read the specified range */
  480. sscanf(this->pattern, "%u..%u",
  481. (unsigned int *)&this->u.integer.min,
  482. (unsigned int *)&this->u.integer.max);
  483. break;
  484. /*------------------------------------------------- */
  485. case CLISH_PTYPE_SELECT:
  486. this->pattern = lub_string_dup(pattern);
  487. /* store a vector of item descriptors */
  488. this->u.select.items = lub_argv_new(this->pattern, 0);
  489. break;
  490. /*------------------------------------------------- */
  491. }
  492. /* now set up the range details */
  493. clish_ptype__set_range(this);
  494. }
  495. /*--------------------------------------------------------- */
  496. void clish_ptype__set_text(clish_ptype_t * this, const char *text)
  497. {
  498. assert(!this->text);
  499. this->text = lub_string_dup(text);
  500. }
  501. /*--------------------------------------------------------- */
  502. void
  503. clish_ptype__set_preprocess(clish_ptype_t * this,
  504. clish_ptype_preprocess_e preprocess)
  505. {
  506. assert(!this->preprocess);
  507. this->preprocess = preprocess;
  508. }
  509. /*--------------------------------------------------------- */
  510. const char *clish_ptype__get_range(const clish_ptype_t * this)
  511. {
  512. return (const char *)this->range;
  513. }
  514. /*--------------------------------------------------------- */