1f05cddf9SRui Paulo /*
2f05cddf9SRui Paulo * Minimal command line editing
3f05cddf9SRui Paulo * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
4f05cddf9SRui Paulo *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
7f05cddf9SRui Paulo */
8f05cddf9SRui Paulo
9f05cddf9SRui Paulo #include "includes.h"
10f05cddf9SRui Paulo
11f05cddf9SRui Paulo #include "common.h"
12f05cddf9SRui Paulo #include "eloop.h"
13f05cddf9SRui Paulo #include "edit.h"
14f05cddf9SRui Paulo
15f05cddf9SRui Paulo
165b9c547cSRui Paulo #define CMD_BUF_LEN 4096
17f05cddf9SRui Paulo static char cmdbuf[CMD_BUF_LEN];
18f05cddf9SRui Paulo static int cmdbuf_pos = 0;
19f05cddf9SRui Paulo static const char *ps2 = NULL;
20f05cddf9SRui Paulo
21f05cddf9SRui Paulo static void *edit_cb_ctx;
22f05cddf9SRui Paulo static void (*edit_cmd_cb)(void *ctx, char *cmd);
23f05cddf9SRui Paulo static void (*edit_eof_cb)(void *ctx);
24f05cddf9SRui Paulo
25f05cddf9SRui Paulo
edit_read_char(int sock,void * eloop_ctx,void * sock_ctx)26f05cddf9SRui Paulo static void edit_read_char(int sock, void *eloop_ctx, void *sock_ctx)
27f05cddf9SRui Paulo {
28f05cddf9SRui Paulo int c;
29f05cddf9SRui Paulo unsigned char buf[1];
30f05cddf9SRui Paulo int res;
31f05cddf9SRui Paulo
32f05cddf9SRui Paulo res = read(sock, buf, 1);
33f05cddf9SRui Paulo if (res < 0)
34f05cddf9SRui Paulo perror("read");
35f05cddf9SRui Paulo if (res <= 0) {
36f05cddf9SRui Paulo edit_eof_cb(edit_cb_ctx);
37f05cddf9SRui Paulo return;
38f05cddf9SRui Paulo }
39f05cddf9SRui Paulo c = buf[0];
40f05cddf9SRui Paulo
41f05cddf9SRui Paulo if (c == '\r' || c == '\n') {
42f05cddf9SRui Paulo cmdbuf[cmdbuf_pos] = '\0';
43f05cddf9SRui Paulo cmdbuf_pos = 0;
44f05cddf9SRui Paulo edit_cmd_cb(edit_cb_ctx, cmdbuf);
45f05cddf9SRui Paulo printf("%s> ", ps2 ? ps2 : "");
46f05cddf9SRui Paulo fflush(stdout);
47f05cddf9SRui Paulo return;
48f05cddf9SRui Paulo }
49f05cddf9SRui Paulo
50*780fb4a2SCy Schubert if (c == '\b') {
51*780fb4a2SCy Schubert if (cmdbuf_pos > 0)
52*780fb4a2SCy Schubert cmdbuf_pos--;
53*780fb4a2SCy Schubert return;
54*780fb4a2SCy Schubert }
55*780fb4a2SCy Schubert
56f05cddf9SRui Paulo if (c >= 32 && c <= 255) {
57f05cddf9SRui Paulo if (cmdbuf_pos < (int) sizeof(cmdbuf) - 1) {
58f05cddf9SRui Paulo cmdbuf[cmdbuf_pos++] = c;
59f05cddf9SRui Paulo }
60f05cddf9SRui Paulo }
61f05cddf9SRui Paulo }
62f05cddf9SRui Paulo
63f05cddf9SRui Paulo
edit_init(void (* cmd_cb)(void * ctx,char * cmd),void (* eof_cb)(void * ctx),char ** (* completion_cb)(void * ctx,const char * cmd,int pos),void * ctx,const char * history_file,const char * ps)64f05cddf9SRui Paulo int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
65f05cddf9SRui Paulo void (*eof_cb)(void *ctx),
66f05cddf9SRui Paulo char ** (*completion_cb)(void *ctx, const char *cmd, int pos),
67f05cddf9SRui Paulo void *ctx, const char *history_file, const char *ps)
68f05cddf9SRui Paulo {
69f05cddf9SRui Paulo edit_cb_ctx = ctx;
70f05cddf9SRui Paulo edit_cmd_cb = cmd_cb;
71f05cddf9SRui Paulo edit_eof_cb = eof_cb;
72f05cddf9SRui Paulo eloop_register_read_sock(STDIN_FILENO, edit_read_char, NULL, NULL);
73f05cddf9SRui Paulo ps2 = ps;
74f05cddf9SRui Paulo
75f05cddf9SRui Paulo printf("%s> ", ps2 ? ps2 : "");
76f05cddf9SRui Paulo fflush(stdout);
77f05cddf9SRui Paulo
78f05cddf9SRui Paulo return 0;
79f05cddf9SRui Paulo }
80f05cddf9SRui Paulo
81f05cddf9SRui Paulo
edit_deinit(const char * history_file,int (* filter_cb)(void * ctx,const char * cmd))82f05cddf9SRui Paulo void edit_deinit(const char *history_file,
83f05cddf9SRui Paulo int (*filter_cb)(void *ctx, const char *cmd))
84f05cddf9SRui Paulo {
85f05cddf9SRui Paulo eloop_unregister_read_sock(STDIN_FILENO);
86f05cddf9SRui Paulo }
87f05cddf9SRui Paulo
88f05cddf9SRui Paulo
edit_clear_line(void)89f05cddf9SRui Paulo void edit_clear_line(void)
90f05cddf9SRui Paulo {
91f05cddf9SRui Paulo }
92f05cddf9SRui Paulo
93f05cddf9SRui Paulo
edit_redraw(void)94f05cddf9SRui Paulo void edit_redraw(void)
95f05cddf9SRui Paulo {
96f05cddf9SRui Paulo cmdbuf[cmdbuf_pos] = '\0';
97f05cddf9SRui Paulo printf("\r> %s", cmdbuf);
98f05cddf9SRui Paulo }
99