misc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. const char *user = NULL;
  102. user = ksession_user(kcontext_session(context));
  103. if (user)
  104. faux_str_cat(&prompt, user);
  105. break;
  106. }
  107. }
  108. is_macro = BOOL_FALSE;
  109. start = pos + 1;
  110. } else if (*pos == '%') {
  111. is_macro = BOOL_TRUE;
  112. if (pos > start)
  113. faux_str_catn(&prompt, start, pos - start);
  114. }
  115. pos++;
  116. }
  117. if (pos > start)
  118. faux_str_catn(&prompt, start, pos - start);
  119. printf("%s", prompt);
  120. faux_str_free(prompt);
  121. fflush(stdout);
  122. return 0;
  123. }