X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=src%2Flib%2Fcmdline.c;h=7423cc09c81b87c70509f60fbd6850ae5409ff92;hb=07b56132836475e2231868771a53a7d2b1ee2fd7;hp=481507153379a9ad250715a4b6b967d9cdb5fc0e;hpb=369246d596ca24a9193b730c5aba2ec8837fb134;p=libcmdline.git diff --git a/src/lib/cmdline.c b/src/lib/cmdline.c index 4815071..7423cc0 100644 --- a/src/lib/cmdline.c +++ b/src/lib/cmdline.c @@ -1,3 +1,37 @@ +/*- + * Copyright (c) <2010>, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * - Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + /* * Copyright (c) 2009, Olivier MATZ * All rights reserved. @@ -32,76 +66,87 @@ #include #include #include - -#include +#include #include -#include -#include #include "cmdline_parse.h" #include "cmdline_rdline.h" #include "cmdline.h" - -#define PROMPT_SUFFIX "> " - - -/**********************/ - -void -cmdline_valid_buffer(struct rdline *rdl, const char *buf, - __attribute__((unused)) unsigned int size) +static void +cmdline_default_valid_buffer(struct rdline *rdl, const char *buf, + __attribute__((unused)) unsigned int size) { struct cmdline *cl = rdl->opaque; int ret; - ret = cmdline_parse(cl, buf); + ret = cmdline_parse(cl->ctx, buf, cl); if (ret == CMDLINE_PARSE_AMBIGUOUS) cmdline_printf(cl, "Ambiguous command\n"); else if (ret == CMDLINE_PARSE_NOMATCH) - cmdline_printf(cl, "Command not found\n"); - else if (ret == CMDLINE_PARSE_BAD_ARGS) cmdline_printf(cl, "Bad arguments\n"); + else if (ret == CMDLINE_PARSE_UNTERMINATED_QUOTE) + cmdline_printf(cl, "Unterminated quote\n"); } -int -cmdline_complete_buffer(struct rdline *rdl, const char *buf, - char *dstbuf, unsigned int dstsize, - int *state) +static int +cmdline_default_complete_buffer(struct rdline *rdl, const char *buf, + char *dstbuf, unsigned int dstsize) { struct cmdline *cl = rdl->opaque; - return cmdline_complete(cl, buf, state, dstbuf, dstsize); + // RDLINE_RES_COMPLETE ? + return cmdline_complete(cl->ctx, buf, dstbuf, dstsize); } -void -cmdline_write_char(struct rdline *rdl, char c) + +static int +cmdline_default_help(struct rdline *rdl, const char *buf, + rdline_write_t *write_cb, void *write_arg) { struct cmdline *cl = rdl->opaque; - if (cl->s_out >= 0) { - write(cl->s_out, &c, 1); - } + return cmdline_help(cl->ctx, buf, cmdline_write, write_arg); } +/* ---- Some rdline wrappers ---- */ + +/* write a buffer, just use rdline */ +ssize_t +cmdline_write(void *arg, void *buf, size_t count) +{ + struct rdline *rdl = arg; +#ifdef NO_PAGER + return rdline_write(rdl, buf, count); +#else + return rdline_asyncpager_write(rdl, buf, count); +#endif +} void cmdline_set_prompt(struct cmdline *cl, const char *prompt) { - snprintf(cl->prompt, sizeof(cl->prompt), prompt); + snprintf(cl->prompt, sizeof(cl->prompt), "%s", prompt); } struct cmdline * cmdline_new(cmdline_parse_ctx_t *ctx, const char *prompt, int s_in, int s_out) { struct cmdline *cl; + cl = malloc(sizeof(struct cmdline)); if (cl == NULL) return NULL; + + /* init cmdline structure */ memset(cl, 0, sizeof(struct cmdline)); cl->s_in = s_in; cl->s_out = s_out; cl->ctx = ctx; - rdline_init(&cl->rdl, cmdline_write_char, - cmdline_valid_buffer, cmdline_complete_buffer); + /* init embedded rdline */ + rdline_init(&cl->rdl, s_in, s_out, + cmdline_default_valid_buffer, + cmdline_default_complete_buffer, + cmdline_default_help); + cl->rdl.opaque = cl; cmdline_set_prompt(cl, prompt); rdline_newline(&cl->rdl, cl->prompt); @@ -120,46 +165,65 @@ cmdline_free(struct cmdline *cl) free(cl); } -void +int cmdline_printf(const struct cmdline *cl, const char *fmt, ...) { va_list ap; - char buffer[BUFSIZ]; + int ret; va_start(ap, fmt); - vsnprintf(buffer, 512, fmt, ap); + ret = rdline_vprintf(&cl->rdl, fmt, ap); va_end(ap); - if (cl->s_out >= 0) { - write(cl->s_out, buffer, strlen(buffer)); - } + + return ret; } +/* Push an input buffer in the command line. Typically, this function + * is called by cmdline_interact() to send the input characters to the + * cmdline process. It can also be called by a user callback function, + * when a buffer is received from the input socket. + * + * The function returns the number of processed characters, or a + * negative value on error (EOF reached or command line exited. */ int cmdline_in(struct cmdline *cl, const char *buf, int size) { - const char *history, *buffer; int ret = 0; - int i, same; + int i; - /* XXX use defines instead of hardcoded values */ - for (i=0; irdl, buf[i]); - if (ret == 1) { - buffer = rdline_get_buffer(&cl->rdl); - history = rdline_get_history_item(&cl->rdl, 0); - if (history) { - same = !memcmp(buffer, history, strlen(history)) && - buffer[strlen(history)] == '\n'; - } - else - same = 0; - if (strlen(buffer) > 1 && !same) - rdline_add_history(&cl->rdl, buffer); + if (ret == RDLINE_RES_VALIDATED) rdline_newline(&cl->rdl, cl->prompt); - } - else if (ret == -2) + else if (ret == RDLINE_RES_EOF) + return -1; + else if (ret == RDLINE_RES_EXITED) return -1; } return i; } + +/* Interrupt a running command line process */ +void +cmdline_quit(struct cmdline *cl) +{ + rdline_quit(&cl->rdl); +} + +/* Start command line on configured file descriptor. This function + * loops until the user explicitelly call cmdline_quit(), or if the + * input fd reaches EOF. */ +void +cmdline_interact(struct cmdline *cl) +{ + char c; + + c = -1; + while (1) { + if (read(cl->s_in, &c, 1) < 0) + break; + if (cmdline_in(cl, &c, 1) < 0) + break; + } +}