Add repeat functionality

pull/4/head
Moritz Luedecke 7 years ago
parent e5b85da8d3
commit 22bc570658

@ -47,6 +47,8 @@ static int lrpad;
static size_t cursor; static size_t cursor;
static int mon = -1, screen; static int mon = -1, screen;
static char* pin;
static Atom clip, utf8; static Atom clip, utf8;
static Display *dpy; static Display *dpy;
static Window root, parentwin, win; static Window root, parentwin, win;
@ -114,7 +116,7 @@ nextrune(int cursor, int inc) {
/* Return location of next utf8 rune in the given direction (+1 or -1) */ /* Return location of next utf8 rune in the given direction (+1 or -1) */
for (n = cursor + inc; for (n = cursor + inc;
n + inc >= 0 && (pinentry->pin[n] & 0xc0) == 0x80; n + inc >= 0 && (pin[n] & 0xc0) == 0x80;
n += inc); n += inc);
return n; return n;
@ -122,19 +124,25 @@ nextrune(int cursor, int inc) {
static void static void
insert(const char *str, ssize_t n) { insert(const char *str, ssize_t n) {
if (strlen(pinentry->pin) + n > pinentry->pin_len - 1) { int repeat;
if (strlen(pin) + n > pinentry->pin_len - 1) {
repeat = (pin == pinentry->pin) ? 0 : 1;
if (!pinentry_setbufferlen(pinentry, 2 * pinentry->pin_len)) { if (!pinentry_setbufferlen(pinentry, 2 * pinentry->pin_len)) {
printf("Error: Couldn't allocate secure memory\n"); printf("Error: Couldn't allocate secure memory\n");
return; return;
} }
pin = (repeat) ? pinentry->pin : pinentry->repeat_passphrase;
} }
if (n > 0) { if (n > 0) {
memcpy(&pinentry->pin[cursor], str, n); memcpy(&pin[cursor], str, n);
} }
cursor += n; cursor += n;
pinentry->pin[cursor] = '\0'; pin[cursor] = '\0';
} }
static void static void
@ -196,7 +204,7 @@ drawwin(void) {
drw_setscheme(drw, scheme[SchemeNormal]); drw_setscheme(drw, scheme[SchemeNormal]);
if (winmode == WinPin) { if (winmode == WinPin) {
for (i = 0; i < asterlen * strlen(pinentry->pin); i += asterlen) { for (i = 0; i < asterlen * strlen(pin); i += asterlen) {
memcpy(&censort[i], asterisk, asterlen); memcpy(&censort[i], asterisk, asterlen);
} }
@ -219,6 +227,13 @@ drawwin(void) {
drw_map(drw, win, 0, 0, mw, mh); drw_map(drw, win, 0, 0, mw, mh);
} }
static void
setup_pin(char* pinlink) {
pin = pinlink;
pin[0] = '\0';
cursor = 0;
}
static void static void
setup(void) { setup(void) {
int x, y, i = 0; int x, y, i = 0;
@ -239,7 +254,6 @@ setup(void) {
scheme[SchemeSelect] = drw_scm_create(drw, colors[SchemeSelect], 2); scheme[SchemeSelect] = drw_scm_create(drw, colors[SchemeSelect], 2);
scheme[SchemeDesc] = drw_scm_create(drw, colors[SchemeDesc], 2); scheme[SchemeDesc] = drw_scm_create(drw, colors[SchemeDesc], 2);
pinentry->pin[0] = '\0';
clip = XInternAtom(dpy, "CLIPBOARD", False); clip = XInternAtom(dpy, "CLIPBOARD", False);
utf8 = XInternAtom(dpy, "UTF8_STRING", False); utf8 = XInternAtom(dpy, "UTF8_STRING", False);
@ -328,7 +342,6 @@ setup(void) {
} }
drw_resize(drw, mw, mh); drw_resize(drw, mw, mh);
drawwin();
} }
static void static void
@ -386,7 +399,7 @@ keypress(XKeyEvent *ev) {
} else { } else {
switch(ksym) { switch(ksym) {
case XK_Delete: case XK_Delete:
if (pinentry->pin[cursor] == '\0') { if (pin[cursor] == '\0') {
return 0; return 0;
} }
cursor = nextrune(cursor, +1); cursor = nextrune(cursor, +1);
@ -409,7 +422,7 @@ keypress(XKeyEvent *ev) {
} }
break; break;
case XK_Right: case XK_Right:
if (pinentry->pin[cursor] != '\0') { if (pin[cursor] != '\0') {
cursor = nextrune(cursor, +1); cursor = nextrune(cursor, +1);
} }
break; break;
@ -437,7 +450,7 @@ paste(void) {
Atom da; Atom da;
/* We have been given the current selection, now insert it into input */ /* We have been given the current selection, now insert it into input */
XGetWindowProperty(dpy, win, utf8, 0, (sizeof pinentry->pin / 4) + 1, False, XGetWindowProperty(dpy, win, utf8, 0, (sizeof pin / 4) + 1, False,
utf8, &da, &di, &dl, &dl, (unsigned char **)&p); utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
insert(p, (q = strchr(p, '\n')) ? q - p : (ssize_t) strlen(p)); insert(p, (q = strchr(p, '\n')) ? q - p : (ssize_t) strlen(p));
XFree(p); XFree(p);
@ -447,6 +460,9 @@ paste(void) {
void void
run(void) { run(void) {
XEvent ev; XEvent ev;
drawwin();
while (!XNextEvent(dpy, &ev)) { while (!XNextEvent(dpy, &ev)) {
if (XFilterEvent(&ev, win)) { if (XFilterEvent(&ev, win)) {
/* What is this I don't even */ /* What is this I don't even */
@ -477,14 +493,6 @@ run(void) {
} }
} }
void
promptwin(void) {
grabkeyboard();
setup();
run();
cleanup();
}
static void static void
catchsig(int sig) { catchsig(int sig) {
if (sig == SIGALRM) { if (sig == SIGALRM) {
@ -495,12 +503,22 @@ catchsig(int sig) {
static int static int
password(void) { password(void) {
winmode = WinPin; winmode = WinPin;
promptwin();
// TODO: Add repeat function setup_pin(pinentry->pin);
//if (pe->repeat_passphrase) { run();
// pe->repeat_okay = 1;
//} if (pinentry->repeat_passphrase) {
setup_pin(pinentry->repeat_passphrase);
// TODO: Add repeat text
run();
if (strcmp(pinentry->pin, pinentry->repeat_passphrase)) {
pinentry->repeat_okay = 0;
return 0;
}
pinentry->repeat_okay = 1;
}
return (pinentry->canceled) ? -1 : 1; return (pinentry->canceled) ? -1 : 1;
} }
@ -509,7 +527,8 @@ static int
confirm(void) { confirm(void) {
winmode = WinConfirm; winmode = WinConfirm;
sel = Nothing; sel = Nothing;
promptwin();
run();
return sel != No; return sel != No;
} }
@ -518,8 +537,8 @@ static int
cmdhandler(pinentry_t received_pinentry) { cmdhandler(pinentry_t received_pinentry) {
struct sigaction sa; struct sigaction sa;
XWindowAttributes wa; XWindowAttributes wa;
int ret;
cursor = 0;
pinentry = received_pinentry; pinentry = received_pinentry;
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) { if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) {
@ -549,11 +568,18 @@ cmdhandler(pinentry_t received_pinentry) {
alarm(pinentry->timeout); alarm(pinentry->timeout);
} }
grabkeyboard();
setup();
if (pinentry->pin) { if (pinentry->pin) {
return password(); ret = password();
} else { } else {
return confirm(); ret = confirm();
} }
cleanup();
return ret;
} }
pinentry_cmd_handler_t pinentry_cmd_handler = cmdhandler; pinentry_cmd_handler_t pinentry_cmd_handler = cmdhandler;

Loading…
Cancel
Save