#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
#include <roken.h>
#include <ui.h>
#ifdef HAVE_CONIO_H
#include <conio.h>
#endif
static sig_atomic_t intr_flag;
static void
intr(int sig)
{
intr_flag++;
}
#ifdef HAVE_CONIO_H
static int
read_string(const char *preprompt, const char *prompt,
char *buf, size_t len, int echo)
{
int of = 0;
int c;
char *p;
void (*oldsigintr)(int);
_cprintf("%s%s", preprompt, prompt);
oldsigintr = signal(SIGINT, intr);
p = buf;
while(intr_flag == 0){
c = ((echo)? _getche(): _getch());
if(c == '\n' || c == '\r')
break;
if(of == 0)
*p++ = c;
of = (p == buf + len);
}
if(of)
p--;
*p = 0;
if(echo == 0){
printf("\n");
}
signal(SIGINT, oldsigintr);
if(intr_flag)
return -2;
if(of)
return -1;
return 0;
}
#else
#ifndef NSIG
#define NSIG 47
#endif
#define FLAG_ECHO 1
#define FLAG_USE_STDIO 2
struct {
int signo;
enum { IGNORE, SKIP } action;
} specialSignals[] = {
{
.signo = SIGALRM,
.action = SKIP,
}, {
.signo = SIGWINCH,
.action = IGNORE,
}, {
.signo = SIGCHLD,
.action = IGNORE,
}, {
.signo = SIGINFO,
.action = IGNORE,
}
};
static int
read_string(const char *preprompt, const char *prompt,
char *buf, size_t len, int flags)
{
struct sigaction sigs[NSIG];
int oksigs[NSIG];
struct sigaction sa;
FILE *tty;
int ret = 0;
int of = 0;
size_t i, j;
int skip;
int c;
char *p;
struct termios t_new, t_old;
memset(&oksigs, 0, sizeof(oksigs));
memset(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask);
for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++) {
sa.sa_flags = 0;
sa.sa_handler = intr;
skip = false;
for (j = 0; j < sizeof(specialSignals)/sizeof(specialSignals[0]); j++) {
if (specialSignals[j].signo == (int)i) {
switch (specialSignals[j].action) {
case IGNORE:
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_RESTART;
break;
case SKIP:
skip = true;
break;
}
}
}
if (!skip && sigaction(i, &sa, &sigs[i]) == 0) {
oksigs[i] = 1;
}
}
if((flags & FLAG_USE_STDIO) != 0)
tty = stdin;
else if ((tty = fopen("/dev/tty", "r")) != NULL)
rk_cloexec_file(tty);
else
tty = stdin;
fprintf(stderr, "%s%s", preprompt, prompt);
fflush(stderr);
if((flags & FLAG_ECHO) == 0){
tcgetattr(fileno(tty), &t_old);
memcpy(&t_new, &t_old, sizeof(t_new));
t_new.c_lflag &= ~ECHO;
tcsetattr(fileno(tty), TCSANOW, &t_new);
}
intr_flag = 0;
p = buf;
while(intr_flag == 0){
c = getc(tty);
if(c == EOF){
if(!ferror(tty))
ret = 1;
break;
}
if(c == '\n')
break;
if(of == 0)
*p++ = c;
of = (p == buf + len);
}
if(of)
p--;
*p = 0;
if((flags & FLAG_ECHO) == 0){
fprintf(stderr, "\n");
tcsetattr(fileno(tty), TCSANOW, &t_old);
}
if(tty != stdin)
fclose(tty);
for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
if (oksigs[i])
sigaction(i, &sigs[i], NULL);
if(ret)
return -3;
if(intr_flag)
return -2;
if(of)
return -1;
return 0;
}
#endif
int
UI_UTIL_read_pw_string(char *buf, int length, const char *prompt, int verify)
{
int ret;
ret = read_string("", prompt, buf, length, 0);
if (ret)
return ret;
if (verify) {
char *buf2;
buf2 = malloc(length);
if (buf2 == NULL)
return 1;
ret = read_string("Verify password - ", prompt, buf2, length, 0);
if (ret) {
free(buf2);
return ret;
}
if (strcmp(buf2, buf) != 0)
ret = 1;
free(buf2);
}
return ret;
}
int
UI_UTIL_read_pw_string_stdio(char *buf, int length, const char *prompt, int verify)
{
int ret;
ret = read_string("", prompt, buf, length, FLAG_USE_STDIO);
if (ret)
return ret;
if (verify) {
char *buf2;
buf2 = malloc(length);
if (buf2 == NULL)
return 1;
ret = read_string("Verify password - ", prompt, buf2, length, FLAG_USE_STDIO);
if (ret) {
free(buf2);
return ret;
}
if (strcmp(buf2, buf) != 0)
ret = 1;
free(buf2);
}
return ret;
}