2010-01-30 13:02:53 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2010 pancake<nopcode.org> */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
#include <r_cons.h>
|
|
|
|
#include <string.h>
|
2009-04-07 11:28:22 +00:00
|
|
|
#if HAVE_DIETLINE
|
|
|
|
#include <r_line.h>
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-01-30 13:02:53 +00:00
|
|
|
extern char *dl_readline(int argc, const char **argv);
|
2009-07-16 09:44:56 +00:00
|
|
|
#endif
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-02-21 19:21:36 +00:00
|
|
|
R_API int r_cons_arrow_to_hjkl(int ch) {
|
2010-01-30 13:02:53 +00:00
|
|
|
if (ch==0x1b) {
|
|
|
|
ch = r_cons_readchar();
|
|
|
|
if (ch==0x5b) {
|
|
|
|
// TODO: must also work in interactive visual write ascii mode
|
2010-02-28 13:49:26 +00:00
|
|
|
ch = r_cons_readchar ();
|
2010-01-30 13:02:53 +00:00
|
|
|
switch(ch) {
|
|
|
|
case 0x35: ch='K'; break; // re.pag
|
|
|
|
case 0x36: ch='J'; break; // av.pag
|
|
|
|
case 0x41: ch='k'; break; // up
|
|
|
|
case 0x42: ch='j'; break; // down
|
|
|
|
case 0x43: ch='l'; break; // right
|
|
|
|
case 0x44: ch='h'; break; // left
|
|
|
|
case 0x3b: break;
|
|
|
|
default: ch = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ch;
|
|
|
|
}
|
2010-01-09 01:05:04 +00:00
|
|
|
|
2009-04-07 11:28:22 +00:00
|
|
|
// XXX no control for max length here?!?!
|
2010-02-21 19:21:36 +00:00
|
|
|
R_API int r_cons_fgets(char *buf, int len, int argc, const char **argv) {
|
|
|
|
RCons *cons = r_cons_singleton ();
|
|
|
|
if (cons->user_fgets)
|
|
|
|
return cons->user_fgets (buf, 512);
|
2010-01-30 13:02:53 +00:00
|
|
|
else {
|
2009-02-05 21:08:46 +00:00
|
|
|
#if HAVE_DIETLINE
|
2010-01-30 13:02:53 +00:00
|
|
|
char *ptr;
|
|
|
|
buf[0]='\0';
|
|
|
|
ptr = r_line_readline ((argv)?argc:CMDS, (argv)?argv:radare_argv);
|
|
|
|
if (ptr == NULL)
|
|
|
|
return -1;
|
|
|
|
strncpy (buf, ptr, len);
|
2009-02-05 21:08:46 +00:00
|
|
|
#else
|
2010-01-30 13:02:53 +00:00
|
|
|
buf[0]='\0';
|
2010-02-21 19:21:36 +00:00
|
|
|
if (fgets (buf, len, cons->fdin) == NULL)
|
2010-01-30 13:02:53 +00:00
|
|
|
return -1;
|
2010-02-21 19:21:36 +00:00
|
|
|
if (feof (cons->fdin))
|
2010-01-30 13:02:53 +00:00
|
|
|
return -1;
|
|
|
|
buf[strlen(buf)-1]='\0';
|
2009-02-05 21:08:46 +00:00
|
|
|
#endif
|
2010-01-30 13:02:53 +00:00
|
|
|
}
|
|
|
|
return strlen (buf);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-01-09 01:05:04 +00:00
|
|
|
R_API void r_cons_any_key()
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2010-01-30 13:02:53 +00:00
|
|
|
r_cons_strcat ("\n--press any key--\n");
|
|
|
|
r_cons_flush ();
|
|
|
|
r_cons_readchar ();
|
|
|
|
//r_cons_strcat ("\x1b[2J\x1b[0;0H"); // wtf?
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-01-09 01:05:04 +00:00
|
|
|
R_API int r_cons_readchar()
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
|
|
|
char buf[2];
|
|
|
|
#if __WINDOWS__
|
|
|
|
BOOL ret;
|
2010-01-30 13:02:53 +00:00
|
|
|
DWORD out;
|
2009-02-05 21:08:46 +00:00
|
|
|
LPDWORD mode;
|
2010-01-30 13:02:53 +00:00
|
|
|
HANDLE h = GetStdHandle (STD_INPUT_HANDLE);
|
|
|
|
GetConsoleMode (h, &mode);
|
|
|
|
SetConsoleMode (h, 0); // RAW
|
|
|
|
ret = ReadConsole (h, buf,1, &out, NULL);
|
2009-02-05 21:08:46 +00:00
|
|
|
if (!ret)
|
|
|
|
return -1;
|
2010-01-30 13:02:53 +00:00
|
|
|
SetConsoleMode (h, mode);
|
2009-02-05 21:08:46 +00:00
|
|
|
#else
|
2010-01-30 13:02:53 +00:00
|
|
|
r_cons_set_raw (1);
|
|
|
|
if (read (0, buf, 1)==-1)
|
2009-02-05 21:08:46 +00:00
|
|
|
return -1;
|
2010-01-30 13:02:53 +00:00
|
|
|
r_cons_set_raw (0);
|
2009-02-05 21:08:46 +00:00
|
|
|
#endif
|
|
|
|
return buf[0];
|
|
|
|
}
|
2010-02-28 13:49:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
R_API int r_cons_yesno(int def, const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
int key = def;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
vfprintf (stderr, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
fflush (stderr);
|
|
|
|
r_cons_set_raw (1);
|
|
|
|
read (0, &key, 1);
|
|
|
|
write (2, "\n", 1);
|
|
|
|
if (key == 'Y')
|
|
|
|
key = 'y';
|
|
|
|
r_cons_set_raw (0);
|
|
|
|
if (key=='\n'||key=='\r')
|
|
|
|
key = def;
|
|
|
|
return key=='y';
|
|
|
|
}
|
|
|
|
|