ptype.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. this->action = clish_action_new();
  352. /* Be a good binary tree citizen */
  353. lub_bintree_node_init(&this->bt_node);
  354. if (pattern) {
  355. /* set the pattern for this type */
  356. clish_ptype__set_pattern(this, pattern, method);
  357. } else {
  358. /* The method is regexp by default */
  359. this->method = CLISH_PTYPE_REGEXP;
  360. }
  361. /* set the help text for this type */
  362. if (text)
  363. clish_ptype__set_text(this, text);
  364. }
  365. /*--------------------------------------------------------- */
  366. char *clish_ptype_validate(const clish_ptype_t * this, const char *text)
  367. {
  368. return clish_ptype_validate_or_translate(this, text, BOOL_FALSE);
  369. }
  370. /*--------------------------------------------------------- */
  371. char *clish_ptype_translate(const clish_ptype_t * this, const char *text)
  372. {
  373. return clish_ptype_validate_or_translate(this, text, BOOL_TRUE);
  374. }
  375. /*--------------------------------------------------------- */
  376. clish_ptype_t *clish_ptype_new(const char *name,
  377. const char *help, const char *pattern,
  378. clish_ptype_method_e method, clish_ptype_preprocess_e preprocess)
  379. {
  380. clish_ptype_t *this = malloc(sizeof(clish_ptype_t));
  381. if (this)
  382. clish_ptype_init(this, name, help, pattern, method, preprocess);
  383. return this;
  384. }
  385. /*--------------------------------------------------------- */
  386. static void clish_ptype_fini(clish_ptype_t * this)
  387. {
  388. if (this->pattern) {
  389. switch (this->method) {
  390. case CLISH_PTYPE_REGEXP:
  391. regfree(&this->u.regexp);
  392. break;
  393. case CLISH_PTYPE_INTEGER:
  394. case CLISH_PTYPE_UNSIGNEDINTEGER:
  395. break;
  396. case CLISH_PTYPE_SELECT:
  397. lub_argv_delete(this->u.select.items);
  398. break;
  399. }
  400. }
  401. lub_string_free(this->name);
  402. this->name = NULL;
  403. lub_string_free(this->text);
  404. this->text = NULL;
  405. lub_string_free(this->pattern);
  406. this->pattern = NULL;
  407. lub_string_free(this->range);
  408. this->range = NULL;
  409. clish_action_delete(this->action);
  410. }
  411. /*--------------------------------------------------------- */
  412. void clish_ptype_delete(clish_ptype_t * this)
  413. {
  414. clish_ptype_fini(this);
  415. free(this);
  416. }
  417. CLISH_GET_STR(ptype, name);
  418. CLISH_SET_STR_ONCE(ptype, text);
  419. CLISH_GET_STR(ptype, text);
  420. CLISH_SET_ONCE(ptype, clish_ptype_preprocess_e, preprocess);
  421. CLISH_GET_STR(ptype, range);
  422. CLISH_GET(ptype, clish_action_t *, action);
  423. /*--------------------------------------------------------- */
  424. void clish_ptype__set_pattern(clish_ptype_t * this,
  425. const char *pattern, clish_ptype_method_e method)
  426. {
  427. assert(NULL == this->pattern);
  428. this->method = method;
  429. switch (this->method) {
  430. /*------------------------------------------------- */
  431. case CLISH_PTYPE_REGEXP:
  432. {
  433. int result;
  434. /* only the expression is allowed */
  435. lub_string_cat(&this->pattern, "^");
  436. lub_string_cat(&this->pattern, pattern);
  437. lub_string_cat(&this->pattern, "$");
  438. /* compile the regular expression for later use */
  439. result = regcomp(&this->u.regexp, this->pattern,
  440. REG_NOSUB | REG_EXTENDED);
  441. assert(0 == result);
  442. break;
  443. }
  444. /*------------------------------------------------- */
  445. case CLISH_PTYPE_INTEGER:
  446. /* default the range to that of an integer */
  447. this->u.integer.min = INT_MIN;
  448. this->u.integer.max = INT_MAX;
  449. this->pattern = lub_string_dup(pattern);
  450. /* now try and read the specified range */
  451. sscanf(this->pattern, "%d..%d",
  452. &this->u.integer.min, &this->u.integer.max);
  453. break;
  454. /*------------------------------------------------- */
  455. case CLISH_PTYPE_UNSIGNEDINTEGER:
  456. /* default the range to that of an unsigned integer */
  457. this->u.integer.min = 0;
  458. this->u.integer.max = (int)UINT_MAX;
  459. this->pattern = lub_string_dup(pattern);
  460. /* now try and read the specified range */
  461. sscanf(this->pattern, "%u..%u",
  462. (unsigned int *)&this->u.integer.min,
  463. (unsigned int *)&this->u.integer.max);
  464. break;
  465. /*------------------------------------------------- */
  466. case CLISH_PTYPE_SELECT:
  467. this->pattern = lub_string_dup(pattern);
  468. /* store a vector of item descriptors */
  469. this->u.select.items = lub_argv_new(this->pattern, 0);
  470. break;
  471. /*------------------------------------------------- */
  472. }
  473. /* now set up the range details */
  474. clish_ptype__set_range(this);
  475. }