1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
16 #include <rte_string_fns.h>
18 #include "cmdline_private.h"
21 cmdline_valid_buffer(struct rdline *rdl, const char *buf,
22 __rte_unused unsigned int size)
24 struct cmdline *cl = rdl->opaque;
26 ret = cmdline_parse(cl, buf);
27 if (ret == CMDLINE_PARSE_AMBIGUOUS)
28 cmdline_printf(cl, "Ambiguous command\n");
29 else if (ret == CMDLINE_PARSE_NOMATCH)
30 cmdline_printf(cl, "Command not found\n");
31 else if (ret == CMDLINE_PARSE_BAD_ARGS)
32 cmdline_printf(cl, "Bad arguments\n");
36 cmdline_complete_buffer(struct rdline *rdl, const char *buf,
37 char *dstbuf, unsigned int dstsize,
40 struct cmdline *cl = rdl->opaque;
41 return cmdline_complete(cl, buf, state, dstbuf, dstsize);
45 cmdline_write_char(struct rdline *rdl, char c)
56 ret = write(cl->s_out, &c, 1);
63 cmdline_set_prompt(struct cmdline *cl, const char *prompt)
67 strlcpy(cl->prompt, prompt, sizeof(cl->prompt));
71 cmdline_new(cmdline_parse_ctx_t *ctx, const char *prompt, int s_in, int s_out)
79 cl = malloc(sizeof(struct cmdline));
82 memset(cl, 0, sizeof(struct cmdline));
87 ret = rdline_init(&cl->rdl, cmdline_write_char, cmdline_valid_buffer,
88 cmdline_complete_buffer);
95 cmdline_set_prompt(cl, prompt);
96 rdline_newline(&cl->rdl, cl->prompt);
102 cmdline_get_rdline(struct cmdline *cl)
108 cmdline_free(struct cmdline *cl)
117 if (cl->s_out != cl->s_in && cl->s_out > 2)
123 cmdline_printf(const struct cmdline *cl, const char *fmt, ...)
133 cmdline_vdprintf(cl->s_out, fmt, ap);
138 cmdline_in(struct cmdline *cl, const char *buf, int size)
140 const char *history, *buffer;
141 size_t histlen, buflen;
148 for (i=0; i<size; i++) {
149 ret = rdline_char_in(&cl->rdl, buf[i]);
151 if (ret == RDLINE_RES_VALIDATED) {
152 buffer = rdline_get_buffer(&cl->rdl);
153 history = rdline_get_history_item(&cl->rdl, 0);
155 histlen = strnlen(history, RDLINE_BUF_SIZE);
156 same = !memcmp(buffer, history, histlen) &&
157 buffer[histlen] == '\n';
161 buflen = strnlen(buffer, RDLINE_BUF_SIZE);
162 if (buflen > 1 && !same)
163 rdline_add_history(&cl->rdl, buffer);
164 rdline_newline(&cl->rdl, cl->prompt);
166 else if (ret == RDLINE_RES_EOF)
168 else if (ret == RDLINE_RES_EXITED)
175 cmdline_quit(struct cmdline *cl)
179 rdline_quit(&cl->rdl);
183 cmdline_poll(struct cmdline *cl)
191 else if (cl->rdl.status == RDLINE_EXITED)
192 return RDLINE_EXITED;
194 status = cmdline_poll_char(cl);
197 else if (status > 0) {
199 read_status = cmdline_read_char(cl, &c);
203 status = cmdline_in(cl, &c, 1);
204 if (status < 0 && cl->rdl.status != RDLINE_EXITED)
208 return cl->rdl.status;
212 cmdline_interact(struct cmdline *cl)
221 if (cmdline_read_char(cl, &c) <= 0)
223 if (cmdline_in(cl, &c, 1) < 0)