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