1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
15 #include <netinet/in.h>
17 #include <rte_string_fns.h>
19 #include "cmdline_private.h"
21 #ifdef RTE_EXEC_ENV_WINDOWS
26 cmdline_valid_buffer(struct rdline *rdl, const char *buf,
27 __rte_unused unsigned int size)
29 struct cmdline *cl = rdl->opaque;
31 ret = cmdline_parse(cl, buf);
32 if (ret == CMDLINE_PARSE_AMBIGUOUS)
33 cmdline_printf(cl, "Ambiguous command\n");
34 else if (ret == CMDLINE_PARSE_NOMATCH)
35 cmdline_printf(cl, "Command not found\n");
36 else if (ret == CMDLINE_PARSE_BAD_ARGS)
37 cmdline_printf(cl, "Bad arguments\n");
41 cmdline_complete_buffer(struct rdline *rdl, const char *buf,
42 char *dstbuf, unsigned int dstsize,
45 struct cmdline *cl = rdl->opaque;
46 return cmdline_complete(cl, buf, state, dstbuf, dstsize);
50 cmdline_write_char(struct rdline *rdl, char c)
61 ret = write(cl->s_out, &c, 1);
68 cmdline_set_prompt(struct cmdline *cl, const char *prompt)
72 strlcpy(cl->prompt, prompt, sizeof(cl->prompt));
76 cmdline_new(cmdline_parse_ctx_t *ctx, const char *prompt, int s_in, int s_out)
84 cl = malloc(sizeof(struct cmdline));
87 memset(cl, 0, sizeof(struct cmdline));
92 ret = rdline_init(&cl->rdl, cmdline_write_char, cmdline_valid_buffer,
93 cmdline_complete_buffer);
100 cmdline_set_prompt(cl, prompt);
101 rdline_newline(&cl->rdl, cl->prompt);
107 cmdline_get_rdline(struct cmdline *cl)
113 cmdline_free(struct cmdline *cl)
122 if (cl->s_out != cl->s_in && cl->s_out > 2)
128 cmdline_printf(const struct cmdline *cl, const char *fmt, ...)
138 cmdline_vdprintf(cl->s_out, fmt, ap);
143 cmdline_in(struct cmdline *cl, const char *buf, int size)
145 const char *history, *buffer;
146 size_t histlen, buflen;
153 for (i=0; i<size; i++) {
154 ret = rdline_char_in(&cl->rdl, buf[i]);
156 if (ret == RDLINE_RES_VALIDATED) {
157 buffer = rdline_get_buffer(&cl->rdl);
158 history = rdline_get_history_item(&cl->rdl, 0);
160 histlen = strnlen(history, RDLINE_BUF_SIZE);
161 same = !memcmp(buffer, history, histlen) &&
162 buffer[histlen] == '\n';
166 buflen = strnlen(buffer, RDLINE_BUF_SIZE);
167 if (buflen > 1 && !same)
168 rdline_add_history(&cl->rdl, buffer);
169 rdline_newline(&cl->rdl, cl->prompt);
171 else if (ret == RDLINE_RES_EOF)
173 else if (ret == RDLINE_RES_EXITED)
180 cmdline_quit(struct cmdline *cl)
184 rdline_quit(&cl->rdl);
188 cmdline_poll(struct cmdline *cl)
196 else if (cl->rdl.status == RDLINE_EXITED)
197 return RDLINE_EXITED;
199 status = cmdline_poll_char(cl);
202 else if (status > 0) {
204 read_status = cmdline_read_char(cl, &c);
208 status = cmdline_in(cl, &c, 1);
209 if (status < 0 && cl->rdl.status != RDLINE_EXITED)
213 return cl->rdl.status;
217 cmdline_interact(struct cmdline *cl)
226 if (cmdline_read_char(cl, &c) <= 0)
228 if (cmdline_in(cl, &c, 1) < 0)