ptype.c 14 KB

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