2010-11-14 19:59:29 +01:00
|
|
|
/*
|
|
|
|
* Minimal command line editing
|
|
|
|
* Copyright (c) 2010, Jouni Malinen <j@w1.fi>
|
|
|
|
*
|
2012-02-11 15:46:35 +01:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2010-11-14 19:59:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "eloop.h"
|
|
|
|
#include "edit.h"
|
|
|
|
|
|
|
|
|
2014-03-31 11:29:22 +02:00
|
|
|
#define CMD_BUF_LEN 4096
|
2010-11-14 19:59:29 +01:00
|
|
|
static char cmdbuf[CMD_BUF_LEN];
|
|
|
|
static int cmdbuf_pos = 0;
|
2012-08-05 19:46:34 +02:00
|
|
|
static const char *ps2 = NULL;
|
2010-11-14 19:59:29 +01:00
|
|
|
|
|
|
|
static void *edit_cb_ctx;
|
|
|
|
static void (*edit_cmd_cb)(void *ctx, char *cmd);
|
|
|
|
static void (*edit_eof_cb)(void *ctx);
|
|
|
|
|
|
|
|
|
|
|
|
static void edit_read_char(int sock, void *eloop_ctx, void *sock_ctx)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
unsigned char buf[1];
|
|
|
|
int res;
|
|
|
|
|
|
|
|
res = read(sock, buf, 1);
|
|
|
|
if (res < 0)
|
|
|
|
perror("read");
|
|
|
|
if (res <= 0) {
|
|
|
|
edit_eof_cb(edit_cb_ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
c = buf[0];
|
|
|
|
|
|
|
|
if (c == '\r' || c == '\n') {
|
|
|
|
cmdbuf[cmdbuf_pos] = '\0';
|
|
|
|
cmdbuf_pos = 0;
|
|
|
|
edit_cmd_cb(edit_cb_ctx, cmdbuf);
|
2012-08-05 19:46:34 +02:00
|
|
|
printf("%s> ", ps2 ? ps2 : "");
|
2010-11-14 19:59:29 +01:00
|
|
|
fflush(stdout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-13 04:18:14 +02:00
|
|
|
if (c == '\b') {
|
|
|
|
if (cmdbuf_pos > 0)
|
|
|
|
cmdbuf_pos--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-14 19:59:29 +01:00
|
|
|
if (c >= 32 && c <= 255) {
|
|
|
|
if (cmdbuf_pos < (int) sizeof(cmdbuf) - 1) {
|
|
|
|
cmdbuf[cmdbuf_pos++] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
|
|
|
|
void (*eof_cb)(void *ctx),
|
2010-11-21 10:43:09 +01:00
|
|
|
char ** (*completion_cb)(void *ctx, const char *cmd, int pos),
|
2012-08-05 19:46:34 +02:00
|
|
|
void *ctx, const char *history_file, const char *ps)
|
2010-11-14 19:59:29 +01:00
|
|
|
{
|
|
|
|
edit_cb_ctx = ctx;
|
|
|
|
edit_cmd_cb = cmd_cb;
|
|
|
|
edit_eof_cb = eof_cb;
|
|
|
|
eloop_register_read_sock(STDIN_FILENO, edit_read_char, NULL, NULL);
|
2012-08-05 19:46:34 +02:00
|
|
|
ps2 = ps;
|
2010-11-14 20:19:35 +01:00
|
|
|
|
2012-08-05 19:46:34 +02:00
|
|
|
printf("%s> ", ps2 ? ps2 : "");
|
2010-11-14 20:19:35 +01:00
|
|
|
fflush(stdout);
|
|
|
|
|
2010-11-14 19:59:29 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 10:43:09 +01:00
|
|
|
void edit_deinit(const char *history_file,
|
|
|
|
int (*filter_cb)(void *ctx, const char *cmd))
|
2010-11-14 19:59:29 +01:00
|
|
|
{
|
|
|
|
eloop_unregister_read_sock(STDIN_FILENO);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void edit_clear_line(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void edit_redraw(void)
|
|
|
|
{
|
|
|
|
cmdbuf[cmdbuf_pos] = '\0';
|
|
|
|
printf("\r> %s", cmdbuf);
|
|
|
|
}
|