helpline.c 1.3 KB

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