display.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <stdbool.h>
  18. #include <ctype.h>
  19. #include <readline/readline.h>
  20. #include "display.h"
  21. static char *saved_prompt = NULL;
  22. static int saved_point = 0;
  23. static rl_prompt_input_func saved_func = NULL;
  24. static void *saved_user_data = NULL;
  25. void rl_printf(const char *fmt, ...)
  26. {
  27. va_list args;
  28. bool save_input;
  29. char *saved_line;
  30. int saved_point;
  31. save_input = !RL_ISSTATE(RL_STATE_DONE);
  32. if (save_input) {
  33. saved_point = rl_point;
  34. saved_line = rl_copy_text(0, rl_end);
  35. rl_save_prompt();
  36. rl_replace_line("", 0);
  37. rl_redisplay();
  38. }
  39. va_start(args, fmt);
  40. vprintf(fmt, args);
  41. va_end(args);
  42. if (save_input) {
  43. rl_restore_prompt();
  44. rl_replace_line(saved_line, 0);
  45. rl_point = saved_point;
  46. rl_forced_update_display();
  47. free(saved_line);
  48. }
  49. }
  50. void rl_hexdump(const unsigned char *buf, size_t len)
  51. {
  52. static const char hexdigits[] = "0123456789abcdef";
  53. char str[68];
  54. size_t i;
  55. if (!len)
  56. return;
  57. str[0] = ' ';
  58. for (i = 0; i < len; i++) {
  59. str[((i % 16) * 3) + 1] = ' ';
  60. str[((i % 16) * 3) + 2] = hexdigits[buf[i] >> 4];
  61. str[((i % 16) * 3) + 3] = hexdigits[buf[i] & 0xf];
  62. str[(i % 16) + 51] = isprint(buf[i]) ? buf[i] : '.';
  63. if ((i + 1) % 16 == 0) {
  64. str[49] = ' ';
  65. str[50] = ' ';
  66. str[67] = '\0';
  67. rl_printf("%s\n", str);
  68. str[0] = ' ';
  69. }
  70. }
  71. if (i % 16 > 0) {
  72. size_t j;
  73. for (j = (i % 16); j < 16; j++) {
  74. str[(j * 3) + 1] = ' ';
  75. str[(j * 3) + 2] = ' ';
  76. str[(j * 3) + 3] = ' ';
  77. str[j + 51] = ' ';
  78. }
  79. str[49] = ' ';
  80. str[50] = ' ';
  81. str[67] = '\0';
  82. rl_printf("%s\n", str);
  83. }
  84. }
  85. void rl_prompt_input(const char *label, const char *msg,
  86. rl_prompt_input_func func, void *user_data)
  87. {
  88. char prompt[256];
  89. /* Normal use should not prompt for user input to the value a second
  90. * time before it releases the prompt, but we take a safe action. */
  91. if (saved_prompt)
  92. return;
  93. saved_point = rl_point;
  94. saved_prompt = strdup(rl_prompt);
  95. saved_func = func;
  96. saved_user_data = user_data;
  97. rl_set_prompt("");
  98. rl_redisplay();
  99. memset(prompt, 0, sizeof(prompt));
  100. snprintf(prompt, sizeof(prompt), COLOR_RED "[%s]" COLOR_OFF " %s ",
  101. label, msg);
  102. rl_set_prompt(prompt);
  103. rl_replace_line("", 0);
  104. rl_redisplay();
  105. }
  106. int rl_release_prompt(const char *input)
  107. {
  108. rl_prompt_input_func func;
  109. void *user_data;
  110. if (!saved_prompt)
  111. return -1;
  112. /* This will cause rl_expand_prompt to re-run over the last prompt, but
  113. * our prompt doesn't expand anyway. */
  114. rl_set_prompt(saved_prompt);
  115. rl_replace_line("", 0);
  116. rl_point = saved_point;
  117. rl_redisplay();
  118. free(saved_prompt);
  119. saved_prompt = NULL;
  120. func = saved_func;
  121. user_data = saved_user_data;
  122. saved_func = NULL;
  123. saved_user_data = NULL;
  124. func(input, user_data);
  125. return 0;
  126. }