ptype.c 13 KB

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