2009-02-05 21:08:46 +00:00
|
|
|
/* radare - LGPL - Copyright 2009 pancake<nopcode.org> */
|
|
|
|
|
|
|
|
#include <r_cons.h>
|
|
|
|
#include <string.h>
|
2009-04-07 11:28:22 +00:00
|
|
|
#if HAVE_DIETLINE
|
|
|
|
#include <r_line.h>
|
|
|
|
#endif
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-07-16 09:44:56 +00:00
|
|
|
#if 0
|
2009-02-05 21:08:46 +00:00
|
|
|
#define CMDS 54
|
|
|
|
static const char *radare_argv[CMDS] ={
|
|
|
|
NULL, // padding
|
2009-03-14 11:39:37 +00:00
|
|
|
"? ", "!step ", "!stepo ", "!cont ", "!signal ", "!fd ", "!maps ", ".!maps*",
|
|
|
|
"!bp ", "!!", "#md5", "#sha1", "#crc32", "#entropy", "Visual", "ad", "ac",
|
|
|
|
"ag", "emenu ", "eval ", "seek ", "info ", "help ", "move ", "quit ", "flag ",
|
|
|
|
"Po ", "Ps ", "Pi ", "H ", "H no ", "H nj ", "H fj ", "H lua ", "x ", "b ",
|
|
|
|
"y ", "yy ", "y? ", "wx ", "ww ", "wf ", "w?", "pD ", "pG ", "pb ", "px ",
|
|
|
|
"pX ", "po ", "pm ", "pz ", "pr > ", "p? "
|
2009-02-05 21:08:46 +00:00
|
|
|
};
|
2009-07-16 09:44:56 +00:00
|
|
|
#endif
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-01-09 01:05:04 +00:00
|
|
|
int (*r_cons_user_fgets)(char *buf, int len) = NULL;
|
2009-04-07 11:28:22 +00:00
|
|
|
|
2009-02-05 21:08:46 +00:00
|
|
|
char *dl_readline(int argc, const char **argv);
|
2010-01-09 01:05:04 +00:00
|
|
|
|
2009-04-07 11:28:22 +00:00
|
|
|
// XXX no control for max length here?!?!
|
2010-01-09 01:05:04 +00:00
|
|
|
R_API int r_cons_fgets(char *buf, int len, int argc, const char **argv)
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
2009-04-07 11:28:22 +00:00
|
|
|
if (r_cons_user_fgets)
|
2010-01-09 01:05:04 +00:00
|
|
|
return r_cons_user_fgets (buf, 512);
|
|
|
|
|
2009-02-05 21:08:46 +00:00
|
|
|
#if HAVE_DIETLINE
|
|
|
|
/* TODO: link against dietline if possible for autocompletion */
|
|
|
|
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);
|
|
|
|
#else
|
|
|
|
buf[0]='\0';
|
2009-07-16 09:44:56 +00:00
|
|
|
if (fgets(buf, len, r_cons_stdin_fd) == NULL)
|
2009-02-05 21:08:46 +00:00
|
|
|
return -1;
|
2010-01-09 01:05:04 +00:00
|
|
|
if (feof (r_cons_stdin_fd))
|
|
|
|
return -1;
|
2009-02-05 21:08:46 +00:00
|
|
|
buf[strlen(buf)-1]='\0';
|
|
|
|
#endif
|
|
|
|
return strlen(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-09 01:05:04 +00:00
|
|
|
R_API void r_cons_any_key()
|
2009-02-05 21:08:46 +00:00
|
|
|
{
|
|
|
|
r_cons_strcat("\n--press any key--\n");
|
|
|
|
r_cons_flush();
|
|
|
|
r_cons_readchar();
|
|
|
|
r_cons_strcat("\x1b[2J\x1b[0;0H");
|
|
|
|
}
|
|
|
|
|
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__
|
|
|
|
DWORD out;
|
|
|
|
BOOL ret;
|
|
|
|
LPDWORD mode;
|
|
|
|
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
GetConsoleMode(h, &mode);
|
|
|
|
SetConsoleMode(h, 0); // RAW
|
|
|
|
ret = ReadConsole(h, buf,1, &out, NULL);
|
|
|
|
if (!ret)
|
|
|
|
return -1;
|
|
|
|
SetConsoleMode(h, mode);
|
|
|
|
#else
|
2009-02-09 00:54:09 +00:00
|
|
|
r_cons_set_raw(1);
|
2009-02-05 21:08:46 +00:00
|
|
|
if (read(0, buf, 1)==-1)
|
|
|
|
return -1;
|
2009-02-09 00:54:09 +00:00
|
|
|
r_cons_set_raw(0);
|
2009-02-05 21:08:46 +00:00
|
|
|
#endif
|
|
|
|
return buf[0];
|
|
|
|
}
|