2 * Minimal command line editing
3 * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
16 #define CMD_BUF_LEN 256
17 static char cmdbuf[CMD_BUF_LEN];
18 static int cmdbuf_pos = 0;
20 static void *edit_cb_ctx;
21 static void (*edit_cmd_cb)(void *ctx, char *cmd);
22 static void (*edit_eof_cb)(void *ctx);
25 static void edit_read_char(int sock, void *eloop_ctx, void *sock_ctx)
31 res = read(sock, buf, 1);
35 edit_eof_cb(edit_cb_ctx);
40 if (c == '\r' || c == '\n') {
41 cmdbuf[cmdbuf_pos] = '\0';
43 edit_cmd_cb(edit_cb_ctx, cmdbuf);
49 if (c >= 32 && c <= 255) {
50 if (cmdbuf_pos < (int) sizeof(cmdbuf) - 1) {
51 cmdbuf[cmdbuf_pos++] = c;
57 int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
58 void (*eof_cb)(void *ctx),
59 char ** (*completion_cb)(void *ctx, const char *cmd, int pos),
60 void *ctx, const char *history_file)
65 eloop_register_read_sock(STDIN_FILENO, edit_read_char, NULL, NULL);
74 void edit_deinit(const char *history_file,
75 int (*filter_cb)(void *ctx, const char *cmd))
77 eloop_unregister_read_sock(STDIN_FILENO);
81 void edit_clear_line(void)
86 void edit_redraw(void)
88 cmdbuf[cmdbuf_pos] = '\0';
89 printf("\r> %s", cmdbuf);