misc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. *
  3. */
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <sys/types.h>
  11. #include <sys/utsname.h>
  12. #include <faux/str.h>
  13. #include <faux/list.h>
  14. #include <faux/sysdb.h>
  15. #include <klish/kcontext.h>
  16. #include <klish/ksession.h>
  17. #include <klish/kpath.h>
  18. int klish_nop(kcontext_t *context)
  19. {
  20. context = context; // Happy compiler
  21. return 0;
  22. }
  23. // Symbol for testing purposes
  24. int klish_tsym(kcontext_t *context)
  25. {
  26. const char *script = NULL;
  27. script = kcontext_script(context);
  28. if (faux_str_is_empty(script)) {
  29. printf("[<empty>]\n");
  30. fprintf(stderr, "Empty item\n");
  31. return -1;
  32. }
  33. printf("[%s]\n", script);
  34. return 0;
  35. }
  36. // Print content of ACTION script
  37. int klish_printl(kcontext_t *context)
  38. {
  39. const char *script = NULL;
  40. script = kcontext_script(context);
  41. if (faux_str_is_empty(script))
  42. script = "";
  43. printf("%s\n", script);
  44. return 0;
  45. }
  46. // Print content of ACTION script. Without additional '/n'
  47. int klish_print(kcontext_t *context)
  48. {
  49. const char *script = NULL;
  50. script = kcontext_script(context);
  51. if (faux_str_is_empty(script))
  52. script = "";
  53. printf("%s", script);
  54. fflush(stdout);
  55. return 0;
  56. }
  57. // Symbol to show current path
  58. int klish_pwd(kcontext_t *context)
  59. {
  60. kpath_t *path = NULL;
  61. kpath_levels_node_t *iter = NULL;
  62. klevel_t *level = NULL;
  63. path = ksession_path(kcontext_session(context));
  64. iter = kpath_iter(path);
  65. while ((level = kpath_each(&iter))) {
  66. printf("/%s", kentry_name(klevel_entry(level)));
  67. }
  68. printf("\n");
  69. return 0;
  70. }
  71. // Template for easy prompt string generation
  72. int klish_prompt(kcontext_t *context)
  73. {
  74. const char *script = NULL;
  75. const char *start = NULL;
  76. const char *pos = NULL;
  77. char *prompt = NULL;
  78. bool_t is_macro = BOOL_FALSE;
  79. script = kcontext_script(context);
  80. if (faux_str_is_empty(script))
  81. return 0;
  82. pos = script;
  83. start = script;
  84. while (*pos != '\0') {
  85. if (is_macro) {
  86. switch (*pos) {
  87. // Percent symbol itself
  88. case '%': {
  89. faux_str_cat(&prompt, "%");
  90. break;
  91. }
  92. // Hostname
  93. case 'h': {
  94. struct utsname buf;
  95. if (uname(&buf) == 0)
  96. faux_str_cat(&prompt, buf.nodename);
  97. break;
  98. }
  99. // Username
  100. case 'u': {
  101. char *user = getenv("USER");
  102. if (user) {
  103. faux_str_cat(&prompt, user);
  104. break;
  105. }
  106. user = faux_sysdb_name_by_uid(getuid());
  107. if (user)
  108. faux_str_cat(&prompt, user);
  109. faux_str_free(user);
  110. break;
  111. }
  112. }
  113. is_macro = BOOL_FALSE;
  114. start = pos + 1;
  115. } else if (*pos == '%') {
  116. is_macro = BOOL_TRUE;
  117. if (pos > start)
  118. faux_str_catn(&prompt, start, pos - start);
  119. }
  120. pos++;
  121. }
  122. if (pos > start)
  123. faux_str_catn(&prompt, start, pos - start);
  124. printf("%s", prompt);
  125. faux_str_free(prompt);
  126. fflush(stdout);
  127. return 0;
  128. }