600243d766c027517e401ee2ac57a1505b66d544
[linux-flexiantxendom0.git] / tools / perf / util / ui / helpline.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "../debug.h"
6 #include "helpline.h"
7 #include "ui.h"
8 #include "libslang.h"
9
10 void ui_helpline__pop(void)
11 {
12 }
13
14 void ui_helpline__push(const char *msg)
15 {
16         SLsmg_gotorc(SLtt_Screen_Rows - 1, 0);
17         SLsmg_set_color(0);
18         SLsmg_write_nstring((char *)msg, SLtt_Screen_Cols);
19         SLsmg_refresh();
20 }
21
22 void ui_helpline__vpush(const char *fmt, va_list ap)
23 {
24         char *s;
25
26         if (vasprintf(&s, fmt, ap) < 0)
27                 vfprintf(stderr, fmt, ap);
28         else {
29                 ui_helpline__push(s);
30                 free(s);
31         }
32 }
33
34 void ui_helpline__fpush(const char *fmt, ...)
35 {
36         va_list ap;
37
38         va_start(ap, fmt);
39         ui_helpline__vpush(fmt, ap);
40         va_end(ap);
41 }
42
43 void ui_helpline__puts(const char *msg)
44 {
45         ui_helpline__pop();
46         ui_helpline__push(msg);
47 }
48
49 void ui_helpline__init(void)
50 {
51         ui_helpline__puts(" ");
52 }
53
54 char ui_helpline__last_msg[1024];
55
56 int ui_helpline__show_help(const char *format, va_list ap)
57 {
58         int ret;
59         static int backlog;
60
61         pthread_mutex_lock(&ui__lock);
62         ret = vsnprintf(ui_helpline__last_msg + backlog,
63                         sizeof(ui_helpline__last_msg) - backlog, format, ap);
64         backlog += ret;
65
66         if (ui_helpline__last_msg[backlog - 1] == '\n') {
67                 ui_helpline__puts(ui_helpline__last_msg);
68                 SLsmg_refresh();
69                 backlog = 0;
70         }
71         pthread_mutex_unlock(&ui__lock);
72
73         return ret;
74 }