ptype.c 14 KB

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