2 * Copyright (c) 2009-2015, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Copyright (c) <2010>, Intel Corporation
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
36 * - Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
39 * - Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in
41 * the documentation and/or other materials provided with the
44 * - Neither the name of Intel Corporation nor the names of its
45 * contributors may be used to endorse or promote products derived
46 * from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
57 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59 * OF THE POSSIBILITY OF SUCH DAMAGE.
70 #include "ucg_cmd_parse.h"
71 #include "ucg_cmd_rdline.h"
75 default_valid_buffer(struct ucg_rdline *rdl, const char *line)
77 struct ucg_cmd *cl = rdl->opaque;
80 ret = ucg_cmd_parse(cl, line, cl);
81 if (ret == UCG_CMD_PARSE_AMBIGUOUS)
82 ucg_cmd_printf(cl, "Ambiguous command\n");
83 else if (ret == UCG_CMD_PARSE_NOMATCH)
84 ucg_cmd_printf(cl, "Bad arguments\n");
85 else if (ret == UCG_CMD_PARSE_UNTERMINATED_QUOTE)
86 ucg_cmd_printf(cl, "Unterminated quote\n");
90 default_complete_buffer(struct ucg_rdline *rdl, const char *line,
91 char *dstbuf, unsigned int dstsize)
94 struct ucg_cmd *cl = rdl->opaque;
95 ret = ucg_cmd_complete(cl, line, dstbuf, dstsize);
96 if (ret == UCG_CMD_COMPLETE_APPEND)
103 default_help(struct ucg_rdline *rdl, const char *line)
105 struct ucg_cmd *cl = rdl->opaque;
107 ucg_cmd_help(cl, line);
110 /* ---- Some rdline wrappers ---- */
113 ucg_cmd_set_prompt(struct ucg_cmd *cl, const char *prompt)
115 snprintf(cl->prompt, sizeof(cl->prompt), "%s", prompt);
119 ucg_cmd_init(struct ucg_cmd *cl, ucg_cmd_ctx_t *ctx,
120 const char *prompt, FILE *f_in, FILE *f_out)
122 /* init cmd structure */
123 memset(cl, 0, sizeof(struct ucg_cmd));
126 /* init embedded rdline */
127 ucg_rdline_init(&cl->rdl, f_in, f_out,
128 default_valid_buffer,
129 default_complete_buffer,
133 ucg_cmd_set_prompt(cl, prompt);
134 ucg_rdline_newline(&cl->rdl, cl->prompt);
138 ucg_cmd_new(ucg_cmd_ctx_t *ctx, const char *prompt,
139 FILE *f_in, FILE *f_out)
143 cl = malloc(sizeof(struct ucg_cmd));
147 ucg_cmd_init(cl, ctx, prompt, f_in, f_out);
152 ucg_cmd_file_new(ucg_cmd_ctx_t *ctx, const char *prompt,
153 const char *path, FILE *f_out)
155 #if UCG_CMD_HAVE_FILE
158 f_in = fopen(path, "r");
161 return (ucg_cmd_new(ctx, prompt, f_in, f_out));
172 ucg_cmd_free(struct ucg_cmd *cl)
174 struct ucg_rdline *rdl = &cl->rdl;
176 if (rdl->f_in != stdin)
178 if (rdl->f_out != rdl->f_in && rdl->f_out != stdout &&
179 rdl->f_out != stderr)
185 ucg_cmd_printf(struct ucg_cmd *cl, const char *fmt, ...)
191 ret = ucg_rdline_vprintf(&cl->rdl, fmt, ap);
197 /* Push an input buffer in the command line. Typically, this function
198 * is called by ucg_cmd_interact() to send the input characters to the
199 * cmd process. It can also be called by a user callback function,
200 * when a buffer is received from the input socket.
202 * The function returns the number of processed characters, or a
203 * negative value on error (EOF reached or command line exited. */
205 ucg_cmd_in(struct ucg_cmd *cl, const char *buf, int size)
210 for (i = 0; i < size; i++) {
211 ret = ucg_rdline_char_in(&cl->rdl, buf[i]);
213 if (ret == UCG_RDLINE_RES_VALIDATED &&
214 cl->rdl.status == UCG_RDLINE_STOPPED)
217 if (ret == UCG_RDLINE_RES_VALIDATED)
218 ucg_rdline_newline(&cl->rdl, cl->prompt);
219 else if (ret == UCG_RDLINE_RES_EOF)
221 else if (ret == UCG_RDLINE_RES_EXITED)
227 /* Interrupt a running command line (exits from ucg_cmd_interact) */
229 ucg_cmd_quit(struct ucg_cmd *cl)
231 ucg_rdline_quit(&cl->rdl);
234 /* loop until the user explicitelly call ucg_cmd_quit(), or if the input
237 ucg_cmd_interact(struct ucg_cmd *cl, unsigned flags)
244 ret = fread(&c, 1, 1, cl->rdl.f_in);
247 if (flags & UCG_CMD_F_IGNORE_EOF) {
248 clearerr(cl->rdl.f_in);
254 if (ucg_cmd_in(cl, &c, 1) < 0)