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 4096
17 static char cmdbuf[CMD_BUF_LEN];
18 static int cmdbuf_pos = 0;
19 static const char *ps2 = NULL;
21 static void *edit_cb_ctx;
22 static void (*edit_cmd_cb)(void *ctx, char *cmd);
23 static void (*edit_eof_cb)(void *ctx);
26 static void edit_read_char(int sock, void *eloop_ctx, void *sock_ctx)
32 res = read(sock, buf, 1);
36 edit_eof_cb(edit_cb_ctx);
41 if (c == '\r' || c == '\n') {
42 cmdbuf[cmdbuf_pos] = '\0';
44 edit_cmd_cb(edit_cb_ctx, cmdbuf);
45 printf("%s> ", ps2 ? ps2 : "");
50 if (c >= 32 && c <= 255) {
51 if (cmdbuf_pos < (int) sizeof(cmdbuf) - 1) {
52 cmdbuf[cmdbuf_pos++] = c;
58 int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
59 void (*eof_cb)(void *ctx),
60 char ** (*completion_cb)(void *ctx, const char *cmd, int pos),
61 void *ctx, const char *history_file, const char *ps)
66 eloop_register_read_sock(STDIN_FILENO, edit_read_char, NULL, NULL);
69 printf("%s> ", ps2 ? ps2 : "");
76 void edit_deinit(const char *history_file,
77 int (*filter_cb)(void *ctx, const char *cmd))
79 eloop_unregister_read_sock(STDIN_FILENO);
83 void edit_clear_line(void)
88 void edit_redraw(void)
90 cmdbuf[cmdbuf_pos] = '\0';
91 printf("\r> %s", cmdbuf);