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.
67 #ifdef UCG_CMD_HAVE_TERMIOS
71 #include "ucg_cmd_parse.h"
72 #include "ucg_cmd_rdline.h"
75 * A command line structure.
78 ucg_cmd_ctx_t *ctx; /**< The list of commands for this context. */
79 struct ucg_rdline rdl; /**< The associated rdline structure. */
80 char prompt[UCG_RDLINE_PROMPT_SIZE]; /**< The command line prompt. */
81 #ifdef UCG_CMD_HAVE_TERMIOS
82 struct termios oldterm; /**< The old termios info */
84 void *opaque; /**< A user opaque pointer. */
88 * Allocate and initialize a new command line structure
90 * Allocate and initialize a new command line structure, using the
91 * specified context, prompt and input/output streams.
93 * Once unused, the command line structure should be freed using
97 * The command line context.
99 * The command line prompt. The string is copied in the ucg_cmd
100 * structure. The prompt must be smaller than UCG_RDLINE_PROMPT_SIZE.
106 * The newly allocated command line structure.
108 struct ucg_cmd *ucg_cmd_new(ucg_cmd_ctx_t *ctx,
109 const char *prompt, FILE *f_in, FILE *f_out);
112 * Initialize a new command line structure
114 * Initialize a command line structure, using the specified prompt and
115 * specified input/output streams.
118 * The uninitialize command line structure
120 * The command line context
122 * The command line prompt. The string is copied in the ucg_cmd
123 * structure. The prompt must be smaller than UCG_RDLINE_PROMPT_SIZE.
129 void ucg_cmd_init(struct ucg_cmd *cl, ucg_cmd_ctx_t *ctx,
130 const char *prompt, FILE *f_in, FILE *f_out);
133 * Set the prompt of the given command line.
135 void ucg_cmd_set_prompt(struct ucg_cmd *cl, const char *prompt);
138 * Free a previously allocated command line
140 * The structure pointed by cl is freed. The streams f_in and f_out are
141 * closed, except if it's stdin, stdout, or stderr. Note: the user
142 * should call ucg_cmd_quit() before calling this function.
145 * The command line pointer.
147 void ucg_cmd_free(struct ucg_cmd *cl);
150 * Parse a file use the given cmd context
152 * The output file descriptor (used for instance by cmd_printf) is
153 * given as a parameter. It can be -1 if no output is required.
155 struct ucg_cmd *ucg_cmd_file_new(ucg_cmd_ctx_t *ctx,
156 const char *prompt, const char *path, FILE *f_out);
159 * Print data on the output of the command line
161 * This function is a wrapper to rdline_printf().
164 * The command line pointer.
166 * The format string, followed by variable arguments.
168 * On success, return the number of characters printed (not including
169 * the trailing '\0'). On error, a negative value is returned.
171 int ucg_cmd_printf(struct ucg_cmd *cl, const char *fmt, ...);
174 * Push an input buffer to the command line.
176 * Typically, this function is called by ucg_cmd_interact() to send the
177 * input characters to the command line process. It can also be called
178 * by a user, for instance when new data is received from an input
182 * The command line pointer.
184 * The address of the input buffer.
186 * The len of the input buffer.
188 * The function returns the number of processed characters, or a
189 * negative value on error (ex: EOF reached or command line exited).
191 int ucg_cmd_in(struct ucg_cmd *cl, const char *buf, int size);
193 /* flags for ucg_cmd_interact */
194 #define UCG_CMD_F_IGNORE_EOF 0x0001 /**< ignore eof when polling */
197 * Start command line on configured file descriptor. This function
198 * loops until the user explicitelly call ucg_cmd_quit(), or if the
199 * input fd reaches EOF.
202 * The command line pointer
204 * Any flags from UCG_CMD_F_*
205 * - UCG_CMD_F_IGNORE_EOF: on eof, clear error and continue polling
207 void ucg_cmd_interact(struct ucg_cmd *cl, unsigned flags);
210 * Stop a running command line.
212 * Actually it will call ucg_rdline_quit() on the associated rdline.
215 * The command line pointer.
217 void ucg_cmd_quit(struct ucg_cmd *cl);
219 #endif /* UCG_CMD_H_ */