icommand.c 1.6 KB

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