ptype.c 13 KB

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