ptype.c 13 KB

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