ptype.c 14 KB

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