ksym.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/str.h>
  6. #include <faux/list.h>
  7. #include <faux/conv.h>
  8. #include <faux/error.h>
  9. #include <klish/khelper.h>
  10. #include <klish/ksym.h>
  11. struct ksym_s {
  12. char *name;
  13. ksym_fn function;
  14. tri_t permanent; // Dry-run option has no effect for permanent sym
  15. tri_t sync; // Don't fork before sync sym execution
  16. bool_t silent; // Silent syn doesn't have stdin, stdout, stderr
  17. };
  18. // Simple methods
  19. // Name
  20. KGET_STR(sym, name);
  21. KSET_STR_ONCE(sym, name);
  22. // Function
  23. KGET(sym, ksym_fn, function);
  24. KSET(sym, ksym_fn, function);
  25. // Permanent
  26. KGET(sym, tri_t, permanent);
  27. KSET(sym, tri_t, permanent);
  28. // Sync
  29. KGET(sym, tri_t, sync);
  30. KSET(sym, tri_t, sync);
  31. // Silent
  32. KGET(sym, bool_t, silent);
  33. KSET(sym, bool_t, silent);
  34. ksym_t *ksym_new(const char *name, ksym_fn function)
  35. {
  36. ksym_t *sym = NULL;
  37. if (faux_str_is_empty(name))
  38. return NULL;
  39. sym = faux_zmalloc(sizeof(*sym));
  40. assert(sym);
  41. if (!sym)
  42. return NULL;
  43. // Initialize
  44. sym->name = faux_str_dup(name);
  45. sym->function = function;
  46. sym->permanent = TRI_UNDEFINED;
  47. sym->sync = TRI_UNDEFINED;
  48. sym->silent = BOOL_FALSE;
  49. return sym;
  50. }
  51. ksym_t *ksym_new_ext(const char *name, ksym_fn function,
  52. tri_t permanent, tri_t sync, bool_t silent)
  53. {
  54. ksym_t *sym = NULL;
  55. sym = ksym_new(name, function);
  56. if (!sym)
  57. return NULL;
  58. ksym_set_permanent(sym, permanent);
  59. ksym_set_sync(sym, sync);
  60. ksym_set_silent(sym, silent);
  61. return sym;
  62. }
  63. void ksym_free(ksym_t *sym)
  64. {
  65. if (!sym)
  66. return;
  67. faux_str_free(sym->name);
  68. faux_free(sym);
  69. }