ptype.c 13 KB

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