ptype.c 13 KB

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