khelper.c 678 B

123456789101112131415161718192021222324252627282930313233
  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/kptype.h>
  9. #include <klish/kaction.h>
  10. bool_t attr2ctext(char **dst, const char *field, const char *value, int level)
  11. {
  12. char *tmp = NULL;
  13. char *esc = NULL;
  14. if (!field) // Error
  15. return BOOL_FALSE;
  16. if (faux_str_is_empty(value)) // Not error. Just empty field.
  17. return BOOL_TRUE;
  18. esc = faux_str_c_esc(value);
  19. if (!esc)
  20. return BOOL_FALSE;
  21. tmp = faux_str_sprintf("%*c.%s = \"%s\",\n",
  22. level, ' ', field, esc);
  23. faux_str_free(esc);
  24. faux_str_cat(dst, tmp);
  25. faux_str_free(tmp);
  26. return BOOL_TRUE;
  27. }