shell_ptype.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * shell_find_create_ptype.c
  3. */
  4. #include <string.h>
  5. #include <assert.h>
  6. #include "private.h"
  7. /*--------------------------------------------------------- */
  8. clish_ptype_t *clish_shell_find_ptype(clish_shell_t *this, const char *name)
  9. {
  10. lub_list_node_t *iter;
  11. assert(this);
  12. if (!name || !name[0])
  13. return NULL;
  14. /* Iterate elements */
  15. for(iter = lub_list__get_head(this->ptype_tree);
  16. iter; iter = lub_list_node__get_next(iter)) {
  17. int r;
  18. clish_ptype_t *ptype = (clish_ptype_t *)lub_list_node__get_data(iter);
  19. r = strcmp(name, clish_ptype__get_name(ptype));
  20. if (!r)
  21. return ptype;
  22. if (r < 0)
  23. break;
  24. }
  25. return NULL;
  26. }
  27. /*--------------------------------------------------------- */
  28. clish_ptype_t *clish_shell_find_create_ptype(clish_shell_t * this,
  29. const char *name, const char *text, const char *pattern,
  30. clish_ptype_method_e method, clish_ptype_preprocess_e preprocess)
  31. {
  32. clish_ptype_t *ptype = clish_shell_find_ptype(this, name);
  33. if (!ptype) {
  34. /* Create a ptype */
  35. ptype = clish_ptype_new(name, text, pattern,
  36. method, preprocess);
  37. assert(ptype);
  38. lub_list_add(this->ptype_tree, ptype);
  39. }
  40. return ptype;
  41. }