15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
55129044dSC.J. Collier *   All rights reserved.
65129044dSC.J. Collier *
75129044dSC.J. Collier *   Redistribution and use in source and binary forms, with or without
85129044dSC.J. Collier *   modification, are permitted provided that the following conditions
95129044dSC.J. Collier *   are met:
105129044dSC.J. Collier *
115129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
125129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
135129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
145129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in
155129044dSC.J. Collier *       the documentation and/or other materials provided with the
165129044dSC.J. Collier *       distribution.
175129044dSC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
185129044dSC.J. Collier *       contributors may be used to endorse or promote products derived
195129044dSC.J. Collier *       from this software without specific prior written permission.
205129044dSC.J. Collier *
215129044dSC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225129044dSC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235129044dSC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245129044dSC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255129044dSC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265129044dSC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275129044dSC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285129044dSC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295129044dSC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305129044dSC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315129044dSC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325129044dSC.J. Collier */
335129044dSC.J. Collier
345129044dSC.J. Collier/*
355129044dSC.J. Collier * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
365129044dSC.J. Collier * All rights reserved.
375129044dSC.J. Collier * Redistribution and use in source and binary forms, with or without
385129044dSC.J. Collier * modification, are permitted provided that the following conditions are met:
395129044dSC.J. Collier *
405129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
415129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
425129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
435129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in the
445129044dSC.J. Collier *       documentation and/or other materials provided with the distribution.
455129044dSC.J. Collier *     * Neither the name of the University of California, Berkeley nor the
465129044dSC.J. Collier *       names of its contributors may be used to endorse or promote products
475129044dSC.J. Collier *       derived from this software without specific prior written permission.
485129044dSC.J. Collier *
495129044dSC.J. Collier * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
505129044dSC.J. Collier * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
515129044dSC.J. Collier * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
525129044dSC.J. Collier * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
535129044dSC.J. Collier * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
545129044dSC.J. Collier * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
555129044dSC.J. Collier * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
565129044dSC.J. Collier * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
575129044dSC.J. Collier * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
585129044dSC.J. Collier * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
595129044dSC.J. Collier */
605129044dSC.J. Collier
615129044dSC.J. Collier#ifndef _CMDLINE_PARSE_H_
625129044dSC.J. Collier#define _CMDLINE_PARSE_H_
635129044dSC.J. Collier
645129044dSC.J. Collier#ifdef __cplusplus
655129044dSC.J. Collierextern "C" {
665129044dSC.J. Collier#endif
675129044dSC.J. Collier
685129044dSC.J. Collier#ifndef offsetof
695129044dSC.J. Collier#define offsetof(type, field)  ((size_t) &( ((type *)0)->field) )
705129044dSC.J. Collier#endif
715129044dSC.J. Collier
725129044dSC.J. Collier/* return status for parsing */
735129044dSC.J. Collier#define CMDLINE_PARSE_SUCCESS        0
745129044dSC.J. Collier#define CMDLINE_PARSE_AMBIGUOUS     -1
755129044dSC.J. Collier#define CMDLINE_PARSE_NOMATCH       -2
765129044dSC.J. Collier#define CMDLINE_PARSE_BAD_ARGS      -3
775129044dSC.J. Collier
785129044dSC.J. Collier/* return status for completion */
795129044dSC.J. Collier#define CMDLINE_PARSE_COMPLETE_FINISHED 0
805129044dSC.J. Collier#define CMDLINE_PARSE_COMPLETE_AGAIN    1
815129044dSC.J. Collier#define CMDLINE_PARSE_COMPLETED_BUFFER  2
825129044dSC.J. Collier
835129044dSC.J. Collier/* maximum buffer size for parsed result */
845129044dSC.J. Collier#define CMDLINE_PARSE_RESULT_BUFSIZE 8192
855129044dSC.J. Collier
865129044dSC.J. Collier/**
875129044dSC.J. Collier * Stores a pointer to the ops struct, and the offset: the place to
885129044dSC.J. Collier * write the parsed result in the destination structure.
895129044dSC.J. Collier */
905129044dSC.J. Collierstruct cmdline_token_hdr {
915129044dSC.J. Collier	struct cmdline_token_ops *ops;
925129044dSC.J. Collier	unsigned int offset;
935129044dSC.J. Collier};
945129044dSC.J. Colliertypedef struct cmdline_token_hdr cmdline_parse_token_hdr_t;
955129044dSC.J. Collier
965129044dSC.J. Collier/**
975129044dSC.J. Collier * A token is defined by this structure.
985129044dSC.J. Collier *
995129044dSC.J. Collier * parse() takes the token as first argument, then the source buffer
1005129044dSC.J. Collier * starting at the token we want to parse. The 3rd arg is a pointer
1015129044dSC.J. Collier * where we store the parsed data (as binary). It returns the number of
1025129044dSC.J. Collier * parsed chars on success and a negative value on error.
1035129044dSC.J. Collier *
1045129044dSC.J. Collier * complete_get_nb() returns the number of possible values for this
1055129044dSC.J. Collier * token if completion is possible. If it is NULL or if it returns 0,
1065129044dSC.J. Collier * no completion is possible.
1075129044dSC.J. Collier *
1085129044dSC.J. Collier * complete_get_elt() copy in dstbuf (the size is specified in the
1095129044dSC.J. Collier * parameter) the i-th possible completion for this token.  returns 0
1105129044dSC.J. Collier * on success or and a negative value on error.
1115129044dSC.J. Collier *
1125129044dSC.J. Collier * get_help() fills the dstbuf with the help for the token. It returns
1135129044dSC.J. Collier * -1 on error and 0 on success.
1145129044dSC.J. Collier */
1155129044dSC.J. Collierstruct cmdline_token_ops {
1165129044dSC.J. Collier	/** parse(token ptr, buf, res pts, buf len) */
1175129044dSC.J. Collier	int (*parse)(cmdline_parse_token_hdr_t *, const char *, void *,
1185129044dSC.J. Collier		unsigned int);
1195129044dSC.J. Collier	/** return the num of possible choices for this token */
1205129044dSC.J. Collier	int (*complete_get_nb)(cmdline_parse_token_hdr_t *);
1215129044dSC.J. Collier	/** return the elt x for this token (token, idx, dstbuf, size) */
1225129044dSC.J. Collier	int (*complete_get_elt)(cmdline_parse_token_hdr_t *, int, char *,
1235129044dSC.J. Collier		unsigned int);
1245129044dSC.J. Collier	/** get help for this token (token, dstbuf, size) */
1255129044dSC.J. Collier	int (*get_help)(cmdline_parse_token_hdr_t *, char *, unsigned int);
1265129044dSC.J. Collier};
1275129044dSC.J. Collier
1285129044dSC.J. Collierstruct cmdline;
1295129044dSC.J. Collier/**
1305129044dSC.J. Collier * Store a instruction, which is a pointer to a callback function and
1315129044dSC.J. Collier * its parameter that is called when the instruction is parsed, a help
1325129044dSC.J. Collier * string, and a list of token composing this instruction.
1335129044dSC.J. Collier */
1345129044dSC.J. Collierstruct cmdline_inst {
1355129044dSC.J. Collier	/* f(parsed_struct, data) */
1365129044dSC.J. Collier	void (*f)(void *, struct cmdline *, void *);
1375129044dSC.J. Collier	void *data;
1385129044dSC.J. Collier	const char *help_str;
1395129044dSC.J. Collier	cmdline_parse_token_hdr_t *tokens[];
1405129044dSC.J. Collier};
1415129044dSC.J. Colliertypedef struct cmdline_inst cmdline_parse_inst_t;
1425129044dSC.J. Collier
1435129044dSC.J. Collier/**
1445129044dSC.J. Collier * A context is identified by its name, and contains a list of
1455129044dSC.J. Collier * instruction
1465129044dSC.J. Collier *
1475129044dSC.J. Collier */
1485129044dSC.J. Colliertypedef cmdline_parse_inst_t *cmdline_parse_ctx_t;
1495129044dSC.J. Collier
1505129044dSC.J. Collier/**
1515129044dSC.J. Collier * Try to parse a buffer according to the specified context. The
1525129044dSC.J. Collier * argument buf must ends with "\n\0". The function returns
1535129044dSC.J. Collier * CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
1545129044dSC.J. Collier * CMDLINE_PARSE_BAD_ARGS on error. Else it calls the associated
1555129044dSC.J. Collier * function (defined in the context) and returns 0
1565129044dSC.J. Collier * (CMDLINE_PARSE_SUCCESS).
1575129044dSC.J. Collier */
1585129044dSC.J. Collierint cmdline_parse(struct cmdline *cl, const char *buf);
1595129044dSC.J. Collier
1605129044dSC.J. Collier/**
1615129044dSC.J. Collier * complete() must be called with *state==0 (try to complete) or
1625129044dSC.J. Collier * with *state==-1 (just display choices), then called without
1635129044dSC.J. Collier * modifying *state until it returns CMDLINE_PARSE_COMPLETED_BUFFER or
1645129044dSC.J. Collier * CMDLINE_PARSE_COMPLETED_BUFFER.
1655129044dSC.J. Collier *
1665129044dSC.J. Collier * It returns < 0 on error.
1675129044dSC.J. Collier *
1685129044dSC.J. Collier * Else it returns:
1695129044dSC.J. Collier *   - CMDLINE_PARSE_COMPLETED_BUFFER on completion (one possible
1705129044dSC.J. Collier *     choice). In this case, the chars are appended in dst buffer.
1715129044dSC.J. Collier *   - CMDLINE_PARSE_COMPLETE_AGAIN if there is several possible
1725129044dSC.J. Collier *     choices. In this case, you must call the function again,
1735129044dSC.J. Collier *     keeping the value of state intact.
1745129044dSC.J. Collier *   - CMDLINE_PARSE_COMPLETED_BUFFER when the iteration is
1755129044dSC.J. Collier *     finished. The dst is not valid for this last call.
1765129044dSC.J. Collier *
1775129044dSC.J. Collier * The returned dst buf ends with \0.
1785129044dSC.J. Collier */
1795129044dSC.J. Collierint cmdline_complete(struct cmdline *cl, const char *buf, int *state,
1805129044dSC.J. Collier		     char *dst, unsigned int size);
1815129044dSC.J. Collier
1825129044dSC.J. Collier
1835129044dSC.J. Collier/* return true if(!c || iscomment(c) || isblank(c) ||
1845129044dSC.J. Collier * isendofline(c)) */
1855129044dSC.J. Collierint cmdline_isendoftoken(char c);
1865129044dSC.J. Collier
187809f0800SChristian Ehrhardt/* return true if(!c || iscomment(c) || isendofline(c)) */
188809f0800SChristian Ehrhardtint cmdline_isendofcommand(char c);
189809f0800SChristian Ehrhardt
1905129044dSC.J. Collier#ifdef __cplusplus
1915129044dSC.J. Collier}
1925129044dSC.J. Collier#endif
1935129044dSC.J. Collier
1945129044dSC.J. Collier#endif /* _CMDLINE_PARSE_H_ */
195