icommand.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/str.h>
  6. #include <faux/conv.h>
  7. #include <klish/khelper.h>
  8. #include <klish/kcommand.h>
  9. #include <klish/kaction.h>
  10. char *icommand_to_text(const icommand_t *icommand, int level)
  11. {
  12. char *str = NULL;
  13. char *tmp = NULL;
  14. tmp = faux_str_sprintf("%*cCOMMAND {\n", level, ' ');
  15. faux_str_cat(&str, tmp);
  16. faux_str_free(tmp);
  17. attr2ctext(&str, "name", icommand->name, level + 1);
  18. attr2ctext(&str, "help", icommand->help, level + 1);
  19. // PARAM list
  20. if (icommand->params) {
  21. iparam_t **p_iparam = NULL;
  22. tmp = faux_str_sprintf("\n%*cPARAM_LIST\n\n", level + 1, ' ');
  23. faux_str_cat(&str, tmp);
  24. faux_str_free(tmp);
  25. for (p_iparam = *icommand->params; *p_iparam; p_iparam++) {
  26. iparam_t *iparam = *p_iparam;
  27. tmp = iparam_to_text(iparam, level + 2);
  28. faux_str_cat(&str, tmp);
  29. faux_str_free(tmp);
  30. }
  31. tmp = faux_str_sprintf("%*cEND_PARAM_LIST,\n", level + 1, ' ');
  32. faux_str_cat(&str, tmp);
  33. faux_str_free(tmp);
  34. }
  35. // ACTION list
  36. if (icommand->actions) {
  37. iaction_t **p_iaction = NULL;
  38. tmp = faux_str_sprintf("\n%*cACTION_LIST\n\n", level + 1, ' ');
  39. faux_str_cat(&str, tmp);
  40. faux_str_free(tmp);
  41. for (p_iaction = *icommand->actions; *p_iaction; p_iaction++) {
  42. iaction_t *iaction = *p_iaction;
  43. tmp = iaction_to_text(iaction, level + 2);
  44. faux_str_cat(&str, tmp);
  45. faux_str_free(tmp);
  46. }
  47. tmp = faux_str_sprintf("%*cEND_ACTION_LIST,\n", level + 1, ' ');
  48. faux_str_cat(&str, tmp);
  49. faux_str_free(tmp);
  50. }
  51. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  52. faux_str_cat(&str, tmp);
  53. faux_str_free(tmp);
  54. return str;
  55. }