ptype.c 13 KB

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