ptype.c 14 KB

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