ptype.c 14 KB

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