helpline.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "../debug.h"
  6. #include "helpline.h"
  7. #include "ui.h"
  8. #include "../util.h"
  9. char ui_helpline__current[512];
  10. static void nop_helpline__pop(void)
  11. {
  12. }
  13. static void nop_helpline__push(const char *msg __maybe_unused)
  14. {
  15. }
  16. static int nop_helpline__show(const char *fmt __maybe_unused,
  17. va_list ap __maybe_unused)
  18. {
  19. return 0;
  20. }
  21. static struct ui_helpline default_helpline_fns = {
  22. .pop = nop_helpline__pop,
  23. .push = nop_helpline__push,
  24. .show = nop_helpline__show,
  25. };
  26. struct ui_helpline *helpline_fns = &default_helpline_fns;
  27. void ui_helpline__pop(void)
  28. {
  29. helpline_fns->pop();
  30. }
  31. void ui_helpline__push(const char *msg)
  32. {
  33. helpline_fns->push(msg);
  34. }
  35. void ui_helpline__vpush(const char *fmt, va_list ap)
  36. {
  37. char *s;
  38. if (vasprintf(&s, fmt, ap) < 0)
  39. vfprintf(stderr, fmt, ap);
  40. else {
  41. ui_helpline__push(s);
  42. free(s);
  43. }
  44. }
  45. void ui_helpline__fpush(const char *fmt, ...)
  46. {
  47. va_list ap;
  48. va_start(ap, fmt);
  49. ui_helpline__vpush(fmt, ap);
  50. va_end(ap);
  51. }
  52. void ui_helpline__puts(const char *msg)
  53. {
  54. ui_helpline__pop();
  55. ui_helpline__push(msg);
  56. }
  57. int ui_helpline__vshow(const char *fmt, va_list ap)
  58. {
  59. return helpline_fns->show(fmt, ap);
  60. }
  61. void ui_helpline__printf(const char *fmt, ...)
  62. {
  63. va_list ap;
  64. ui_helpline__pop();
  65. va_start(ap, fmt);
  66. ui_helpline__vpush(fmt, ap);
  67. va_end(ap);
  68. }