-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib-askpass.c
167 lines (141 loc) · 3.54 KB
/
lib-askpass.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright Neil Brown ©2023 <neil@brown.name>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* askpass - ask for a password.
* This doesn't yet have any focus on protecting the password
* from being swapped out.
*
* We place a popup in mid-display with a message and
* "HEAVY BALLOT X" for each character typed.
*/
#include <unistd.h>
#include <memory.h>
#define PANE_DATA_TYPE struct apinfo
#include "core.h"
struct apinfo {
char *msg safe;
struct buf b;
struct command *c;
};
#include "core-pane.h"
static struct map *askpass_map;
DEF_LOOKUP_CMD(askpass_handle, askpass_map);
DEF_CMD(askpass_refresh_view)
{
struct buf b;
struct apinfo *ai = ci->home->data;
int shift = 0;
int i;
buf_init(&b);
buf_concat(&b, ai->msg);
for (i = 0; i < utf8_strlen(buf_final(&ai->b)); i++)
buf_append(&b, 0x2718); /* HEAVY BALLOT X */
call("render-line:set", ci->focus, b.len, NULL, buf_final(&b));
for (i = 0; i < 10; i++) {
int cw;
attr_set_int(&ci->focus->attrs, "render-wrap", shift);
call("render-line:measure", ci->focus, b.len);
cw = pane_attr_get_int(ci->focus, "curs_width", 1);
if (ci->home->parent->cx < ci->home->parent->w - cw)
break;
shift += 8 * cw;
}
free(buf_final(&b));
return 1;
}
DEF_CMD(askpass_key)
{
const char *k = ksuffix(ci, "K-");
struct apinfo *ai = ci->home->data;
buf_concat(&ai->b, k);
pane_damaged(ci->home, DAMAGED_VIEW);
return 1;
}
DEF_CMD(askpass_bs)
{
struct apinfo *ai = ci->home->data;
if (ai->b.len > 0)
ai->b.len = utf8_round_len(ai->b.b, ai->b.len-1);
pane_damaged(ci->home, DAMAGED_VIEW);
return 1;
}
DEF_CMD(askpass_ignore)
{
return 1;
}
DEF_CMD(askpass_done)
{
struct apinfo *ai = ci->home->data;
comm_call(ai->c, "cb", ci->focus, ai->b.len, NULL,
buf_final(&ai->b));
memset(ai->b.b, 0, ai->b.size);
call("popup:close", ci->focus);
return 1;
}
DEF_CMD(askpass_abort)
{
struct apinfo *ai = ci->home->data;
memset(ai->b.b, 0, ai->b.size);
comm_call(ai->c, "cb", ci->focus, -1);
call("popup:close", ci->focus);
return 1;
}
DEF_CMD(askpass_attach)
{
struct pane *p, *p2;
if (!ci->str || !ci->comm2)
return Enoarg;
p = call_ret(pane, "PopupTile", ci->focus, 0, NULL, "D2");
if (!p)
return Efail;
p2 = call_ret(pane, "attach-view", p);
if (!p2)
goto fail;
p = p2;
p2 = call_ret(pane, "attach-renderline", p);
if (!p2)
goto fail;
p = p2;
p2 = pane_register(p, 0, &askpass_handle.c);
if (!p2)
goto fail;
p = p2;
attr_set_str(&p->attrs, "pane-title", "Ask Password");
p->data->msg = strdup(ci->str);
p->data->c = command_get(ci->comm2);
buf_init(&p->data->b);
pane_damaged(p, DAMAGED_VIEW);
return 1;
fail:
if (ci->focus->focus)
pane_close(ci->focus->focus);
return Efail;
}
DEF_CMD_CLOSED(askpass_close)
{
struct apinfo *ai = ci->home->data;
free(ai->msg);
ai->msg = safe_cast NULL;
free(buf_final(&ai->b));
ai->b.b = safe_cast NULL;
command_put(ai->c);
ai->c = NULL;
return 1;
}
void edlib_init(struct pane *ed safe)
{
call_comm("global-set-command", ed, &askpass_attach,
0, NULL, "AskPass");
askpass_map = key_alloc();
key_add(askpass_map, "Close", &askpass_close);
key_add_prefix(askpass_map, "K-", &askpass_key);
key_add_prefix(askpass_map, "K:", &askpass_ignore);
key_add_prefix(askpass_map, "M:", &askpass_ignore);
key_add(askpass_map, "K:Enter", &askpass_done);
key_add(askpass_map, "K:Backspace", &askpass_bs);
key_add(askpass_map, "K:ESC", &askpass_abort);
key_add(askpass_map, "K:C-C", &askpass_abort);
key_add(askpass_map, "K:C-G", &askpass_abort);
key_add_prefix(askpass_map, "Refresh:view", &askpass_refresh_view);
}