ptype.c 13 KB

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