ptype.c 14 KB

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