├── .gitignore ├── COPYING ├── Makefile ├── README ├── demo ├── commands.c ├── commands.h └── demo.c ├── tokenline.c └── tokenline.h /.gitignore: -------------------------------------------------------------------------------- 1 | tokenline-demo 2 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | ## Copyright (C) 2014 Bert Vermeulen 3 | ## 4 | ## This program is free software: you can redistribute it and/or modify 5 | ## it under the terms of the GNU General Public License as published by 6 | ## the Free Software Foundation, either version 2 of the License, or 7 | ## (at your option) any later version. 8 | ## 9 | ## This program is distributed in the hope that it will be useful, 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | ## GNU General Public License for more details. 13 | ## 14 | ## You should have received a copy of the GNU General Public License 15 | ## along with this program. If not, see . 16 | ## 17 | 18 | FLAGS = -g -O0 -Wall -Wextra -Wno-missing-field-initializers 19 | 20 | TOKENLINE_SRC = tokenline.c 21 | TOKENLINE_DEP = $(TOKENLINE_SRC) tokenline.h 22 | 23 | DEMO_SRC = demo/commands.c demo/demo.c 24 | DEMO_DEP = $(DEMO_SRC) 25 | 26 | tokenline-demo: $(TOKENLINE_DEP) $(DEMO_DEP) 27 | $(CC) $(FLAGS) -I. -Idemo $(TOKENLINE_SRC) $(DEMO_SRC) -o tokenline-demo 28 | 29 | clean: 30 | rm -f tokenline-demo 31 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Tokenline is a simple command-line shell interface, intended for systems 2 | with few resources. It is available under the terms of the GNU GPL, version 3 | 2 or later. 4 | 5 | 6 | FEATURES: 7 | 8 | - Fully tokenizes input according to defined command hierarchy 9 | - Supports integer, floating-point and string variables 10 | - Uses no dynamic memory allocation 11 | - Automatic context-sensitive help 12 | - Automatic command completion, and partial command parsing 13 | - Bash-like key bindings 14 | - History 15 | 16 | To integrate tokenline into your application, only the following source 17 | files are needed: 18 | tokenline.c 19 | tokenline.h 20 | 21 | The demo/ directory has a sample application that shows how to use 22 | tokenline. 23 | 24 | 25 | DEFINING A COMMAND HIERARCHY 26 | 27 | All commands are defined as tokens (integers). To define a token, your 28 | application should do something like this: 29 | 30 | #define T_SHOW 1 31 | 32 | The token integers must be between 1 and 9999. Defining the command keyword 33 | associated with that token is done in a token dictionary: 34 | 35 | t_token_dict dict[] = { 36 | { /* Dummy entry */ } 37 | { T_SHOW, "show" } 38 | { } 39 | }; 40 | 41 | This table must be kept in the same order as the token integers, hence 42 | the dummy entry: that skips entry 0. 43 | 44 | Also note the { } entry in the table. Every table must be terminated with an 45 | all-zero entry. 46 | 47 | The following struct defines a single token: 48 | 49 | typedef struct token { 50 | int token; 51 | uint16_t arg_type; 52 | uint16_t flags; 53 | struct token *subtokens; 54 | char *help; 55 | char *help_full; 56 | } t_token; 57 | 58 | Only the token field is required; all others are optional: 59 | - arg_type 60 | If the token takes an argument, The type of that argument should 61 | be filled in here. The following are available: 62 | T_ARG_UINT unsigned integer 63 | T_ARG_FLOAT floating-point value 64 | T_ARG_FREQ frequency, as float followed by khz, mhz or ghz 65 | T_ARG_STRING freeform string, should be quoted if it contains spaces 66 | T_ARG_TOKEN one of a set of tokens in the subtokens field 67 | T_ARG_HELP special type for the "help" command, see below 68 | - flags 69 | Flags to change behavior on this token or its arguments. The currently 70 | defined flags are: 71 | T_FLAG_SUFFIX_TOKEN_DELIM_INT 72 | This allows the token to be suffixed with TL_TOKEN_DELIMITER 73 | (set to : by default), followed by an integer. If this syntax is 74 | used, the result will show up on the parsed token list like this: 75 | (integer) 76 | - subtokens 77 | A t_token table contains a set of tokens to be used if this token is 78 | used. For example, The T_SHOW token above might have a set of subtokens 79 | selecting the thing to show (see example). When not used with T_ARG_TOKEN, 80 | this table defines a new context. 81 | - help 82 | A freeform string which will be shown next to this token on help or 83 | completion for the table in which this entry is found. It should not be 84 | longer than 60 characters. 85 | - help_full 86 | A freeform string which will be shown when help for this specific token is 87 | requested. This can be more than one line; it is intended to be a more 88 | detailed help text than the summary provided by the "help" field. 89 | 90 | The T_ARG_UINT type may be specified as a token in the table: this allows 91 | free-standing numbers to be entered on the command line. See the demo 92 | application's 'calc' command for an example. 93 | 94 | Here's a complete example implementing a "show" command: 95 | 96 | enum { 97 | T_SHOW = 1, 98 | T_HARDWARE, 99 | T_VERSION, 100 | T_HELP, 101 | }; 102 | t_token_dict dict[] = { 103 | { /* Dummy entry */ }, 104 | { T_SHOW, "show" }, 105 | { T_HARDWARE, "hardware" }, 106 | { T_VERSION, "version" }, 107 | { T_HELP, "help" }, 108 | { } 109 | }; 110 | t_token tokens_show[] = { 111 | { T_HARDWARE, 0, NULL, "Show hardware information" }, 112 | { T_VERSION, 0, NULL, "Show version" }, 113 | { } 114 | }; 115 | t_token tokens[] = { 116 | { T_SHOW, 0, tokens_show, "Show information" }, 117 | { T_HELP, T_ARG_HELP, NULL, "Available commands" }, 118 | { } 119 | }; 120 | 121 | If you define tokens with the command string "history" or "help", these 122 | will be handled internally, not passed on to your callback. The "history" 123 | command simply prints the command history for this session, and the "help" 124 | command provides help for the current context. Define the help command with 125 | the T_ARG_HELP type: this lets you do tab completion on keywords after the 126 | help command. 127 | 128 | The above definitions are enough to make this work: 129 | 130 | > help 131 | Available commands 132 | show Show information 133 | help Available commands 134 | > help show 135 | Show information 136 | hardware Show hardware information 137 | version Show version 138 | > help show hardware 139 | Show hardware information 140 | 141 | This also gets you tab completion of all keywords, and lets you abbreviate 142 | keywords. The following are equivalent, and get your callback the same 143 | tokens: 144 | 145 | > show hardware 146 | > sh hard 147 | 148 | 149 | API 150 | 151 | Interact with tokenline via the following functions: 152 | 153 | typedef void (*tl_printfunc)(void *user, const char *str); 154 | void tl_init(t_tokenline *tl, t_token *tokens, t_token_dict *dict, 155 | tl_printfunc printfunc, void *user) 156 | 157 | Initializes a previously-allocated t_tokenline struct "tl". This pointer 158 | represents a single command-line instance, and will be passed to all 159 | other API functions. "tokens" is the initial token tree for this instance, 160 | and "printfunc" is the function called when tokenline produces output. 161 | "user" is a pointer which will be passed along to all your callback 162 | functions. 163 | 164 | void tl_set_prompt(t_tokenline *tl, char *prompt) 165 | 166 | Change the prompt tokenline uses. The pointer is used from then on, 167 | so dynamic prompt changes can be done on that pointer. 168 | 169 | typedef void (*tl_callback)(void *user, t_tokenline_parsed *p); 170 | void tl_set_callback(t_tokenline *tl, tl_callback callback) 171 | 172 | Set the function that will be called when a command is entered. The 173 | t_tokenline_parsed struct is defined as follows: 174 | 175 | typedef struct tokenline_parsed { 176 | int tokens[TL_MAX_WORDS]; 177 | char buf[TL_MAX_LINE_LEN]; 178 | } t_tokenline_parsed; 179 | 180 | The list of entered tokens is in the tokens field, terminated by 0 (this 181 | is why token integers must not be 0). An argument is represented by one 182 | of the T_ARG_* types (see above), followed by an integer representing an 183 | offset into the "buf" field where the value lives. This value is simply 184 | sizeof(int), sizeof(float) or a NULL-terminated string. For example, 185 | the following command line: 186 | 187 | > show device 1 file "foo bar" 188 | 189 | Would be represented like this in the tokens field: 190 | 191 | 0 4 0 192 | ^ ^ 193 | argument offset in buffer --| --| 194 | 195 | With the buf field containing the following: 196 | 197 | 00 00 00 01 66 6f 6f 20 62 61 72 00 198 | ^ ^ 199 | 0 (int) 4 (string) 200 | 201 | int tl_mode_push(t_tokenline *tl, t_token *tokens) 202 | 203 | Switch to a new mode with its own token tree. The token tree is pushed 204 | on to a stack. 205 | 206 | int tl_mode_pop(t_tokenline *tl) 207 | 208 | Exit the current mode, and return to the previous token tree. 209 | 210 | int tl_input(t_tokenline *tl, uint8_t c) 211 | 212 | Feed input to the command-line. 213 | 214 | -------------------------------------------------------------------------------- /demo/commands.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Bert Vermeulen 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | #include "tokenline.h" 19 | #include "commands.h" 20 | 21 | t_token_dict dict[] = { 22 | { /* Dummy entry */ }, 23 | { T_SHOW, "show" }, 24 | { T_SET, "set" }, 25 | { T_HARDWARE, "hardware" }, 26 | { T_VERSION, "version" }, 27 | { T_DEVICE, "device" }, 28 | { T_DIRECTORY, "directory" }, 29 | { T_HELP, "help" }, 30 | { T_CPU, "cpu" }, 31 | { T_MEMORY, "memory" }, 32 | { T_FREQUENCY, "frequency" }, 33 | { T_NUMBER, "number" }, 34 | { T_MKDIR, "mkdir" }, 35 | { T_LS, "ls" }, 36 | { T_TAP, "tap" }, 37 | { T_CALC, "calc" }, 38 | { T_PLUS, "+" }, 39 | { T_EXIT, "exit" }, 40 | { T_LED, "led" }, 41 | { T_ON, "on" }, 42 | { T_OFF, "off" }, 43 | { } 44 | }; 45 | 46 | t_token tokens_mode_calc[] = { 47 | { T_ARG_UINT, 48 | .help = "Operand" }, 49 | { T_ARG_STRING, 50 | .help = "Comment" }, 51 | { T_PLUS }, 52 | { T_EXIT, 53 | .help = "Exit calc mode" }, 54 | { } 55 | }; 56 | 57 | t_token tokens_on_off[] = { 58 | { T_ON }, 59 | { T_OFF }, 60 | { } 61 | }; 62 | 63 | t_token tokens_mode_device[] = { 64 | { T_SHOW, 65 | .help = "Show device information" }, 66 | { T_MKDIR, 67 | .help = "Create directory" }, 68 | { T_LS, 69 | .help = "List files and directories" }, 70 | { T_LED, 71 | .arg_type = T_ARG_TOKEN, 72 | .subtokens = tokens_on_off, 73 | .help = "LED control" }, 74 | { T_EXIT, 75 | .help = "Exit device mode" }, 76 | { } 77 | }; 78 | 79 | t_token tokens_set[] = { 80 | { T_FREQUENCY, 81 | .arg_type = T_ARG_FLOAT, 82 | .help = "Frequency" }, 83 | { T_NUMBER, 84 | .arg_type = T_ARG_UINT, 85 | .help = "Number" }, 86 | { } 87 | }; 88 | 89 | t_token tokens_hardware[] = { 90 | { T_CPU, 91 | .help = "CPU" }, 92 | { T_MEMORY, 93 | .help = "Memory" }, 94 | { } 95 | }; 96 | t_token tokens_show[] = { 97 | { T_HARDWARE, 98 | .subtokens = tokens_hardware, 99 | .help = "Hardware information" }, 100 | { T_VERSION, 101 | .help = "Version" }, 102 | { T_DEVICE, 103 | .arg_type = T_ARG_UINT, 104 | .help = "Device" }, 105 | { T_DIRECTORY, 106 | .arg_type = T_ARG_STRING, 107 | .help = "Directory" }, 108 | { } 109 | }; 110 | t_token tokens[] = { 111 | { T_SHOW, 112 | .subtokens = tokens_show, 113 | .help = "Show information" }, 114 | { T_SET, 115 | .subtokens = tokens_set, 116 | .help = "Set things" }, 117 | { T_DEVICE, 118 | .help = "Device mode" }, 119 | { T_TAP, 120 | .flags = T_FLAG_SUFFIX_TOKEN_DELIM_INT, 121 | .help = "Tap" }, 122 | { T_CALC, 123 | .subtokens = tokens_mode_calc, 124 | .help = "Calculator", 125 | .help_full = "This wants to become a calculator some day." }, 126 | { T_HELP, 127 | .arg_type = T_ARG_HELP, 128 | .help = "Show help on available commands", 129 | .help_full = "The following commands are available:" }, 130 | { } 131 | }; 132 | 133 | -------------------------------------------------------------------------------- /demo/commands.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Bert Vermeulen 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | enum { 19 | T_SHOW = 1, 20 | T_SET, 21 | T_HARDWARE, 22 | T_VERSION, 23 | T_DEVICE, 24 | T_DIRECTORY, 25 | T_HELP, 26 | T_CPU, 27 | T_MEMORY, 28 | T_FREQUENCY, 29 | T_NUMBER, 30 | T_MKDIR, 31 | T_LS, 32 | T_TAP, 33 | T_CALC, 34 | T_PLUS, 35 | T_EXIT, 36 | T_LED, 37 | T_ON, 38 | T_OFF, 39 | }; 40 | 41 | -------------------------------------------------------------------------------- /demo/demo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Bert Vermeulen 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include "commands.h" 23 | 24 | #include "tokenline.h" 25 | 26 | extern t_token tokens[]; 27 | extern t_token_dict dict[]; 28 | extern t_token tokens_mode_device[]; 29 | extern t_token tokens_mode_calc[]; 30 | 31 | enum { 32 | MODE_TOP, 33 | MODE_DEVICE, 34 | MODE_CALC, 35 | }; 36 | 37 | struct demo_context { 38 | t_tokenline *tl; 39 | int mode; 40 | } ctx = { }; 41 | 42 | void print(void *user, const char *str) 43 | { 44 | (void)user; 45 | 46 | printf("%s", str); 47 | } 48 | 49 | static void dump_parsed(void *user, t_tokenline_parsed *p) 50 | { 51 | struct demo_context *ctx; 52 | float arg_float; 53 | uint32_t arg_uint; 54 | int arg_int, i; 55 | 56 | ctx = user; 57 | for (i = 0; p->tokens[i]; i++) { 58 | printf("%d: ", i); 59 | switch (p->tokens[i]) { 60 | case T_ARG_UINT: 61 | memcpy(&arg_int, p->buf + p->tokens[++i], sizeof(int)); 62 | printf("integer %d\n", arg_int); 63 | break; 64 | case T_ARG_FLOAT: 65 | memcpy(&arg_float, p->buf + p->tokens[++i], sizeof(float)); 66 | printf("float %f\n", arg_float); 67 | break; 68 | case T_ARG_STRING: 69 | printf("string '%s'\n", p->buf + p->tokens[++i]); 70 | break; 71 | case T_ARG_TOKEN_SUFFIX_INT: 72 | memcpy(&arg_uint, p->buf + p->tokens[++i], sizeof(uint32_t)); 73 | printf("token-suffixed integer %d\n", arg_uint); 74 | break; 75 | default: 76 | printf("token %d (%s)\n", p->tokens[i], 77 | dict[p->tokens[i]].tokenstr); 78 | } 79 | } 80 | 81 | switch (p->tokens[0]) { 82 | case T_DEVICE: 83 | ctx->mode = MODE_DEVICE; 84 | tl_set_prompt(ctx->tl, "device> "); 85 | tl_mode_push(ctx->tl, tokens_mode_device); 86 | break; 87 | case T_CALC: 88 | ctx->mode = MODE_CALC; 89 | tl_set_prompt(ctx->tl, "calc> "); 90 | tl_mode_push(ctx->tl, tokens_mode_calc); 91 | break; 92 | case T_EXIT: 93 | ctx->mode = MODE_TOP; 94 | tl_set_prompt(ctx->tl, "> "); 95 | tl_mode_pop(ctx->tl); 96 | break; 97 | } 98 | 99 | } 100 | 101 | int main(int argc, char **argv) 102 | { 103 | t_tokenline tl; 104 | struct termios old_termios, new_termios; 105 | int c; 106 | 107 | (void)argc; 108 | (void)argv; 109 | 110 | tcgetattr(0, &old_termios); 111 | tcgetattr(0, &new_termios); 112 | new_termios.c_lflag = ~(ICANON|ECHO); 113 | new_termios.c_cc[VMIN] = 1; 114 | new_termios.c_cc[VTIME] = 0; 115 | tcsetattr(0, TCSAFLUSH, &new_termios); 116 | ctx.tl = &tl; 117 | tl_init(&tl, tokens, dict, print, &ctx); 118 | tl_set_prompt(&tl, "> "); 119 | tl_set_callback(&tl, dump_parsed); 120 | while (TRUE) { 121 | c = getchar(); 122 | if (!tl_input(&tl, c)) 123 | break; 124 | } 125 | tcsetattr(0, TCSANOW, &old_termios); 126 | printf("\n"); 127 | 128 | return 0; 129 | } 130 | -------------------------------------------------------------------------------- /tokenline.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Bert Vermeulen 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include "tokenline.h" 22 | 23 | #define INDENT " " 24 | #define NO_HELP "No help available."NL 25 | #define NL "\r\n" 26 | 27 | static void line_clear(t_tokenline *tl); 28 | static void line_backspace(t_tokenline *tl); 29 | static void set_line(t_tokenline *tl, char *line); 30 | 31 | static char space[] = " "; 32 | 33 | static void unsplit_line(t_tokenline *tl) 34 | { 35 | int quoted, i; 36 | 37 | quoted = FALSE; 38 | for (i = 0; i < tl->buf_len; i++) { 39 | if (tl->buf[i] == '"') { 40 | quoted = TRUE; 41 | continue; 42 | } 43 | if (!tl->buf[i]) { 44 | if (quoted) { 45 | tl->buf[i] = '"'; 46 | quoted = FALSE; 47 | } else { 48 | tl->buf[i] = ' '; 49 | } 50 | } 51 | } 52 | tl->buf[tl->buf_len] = 0; 53 | } 54 | 55 | static int split_line(t_tokenline *tl, int *words, int *num_words, int silent) 56 | { 57 | int state, quoted, i; 58 | 59 | state = 1; 60 | quoted = FALSE; 61 | *num_words = 0; 62 | for (i = 0; i < tl->buf_len && *num_words < TL_MAX_WORDS; i++) { 63 | switch (state) { 64 | case 1: 65 | /* Looking for a new word. */ 66 | if (tl->buf[i] == ' ') 67 | continue; 68 | if (tl->buf[i] == '"') 69 | quoted = TRUE; 70 | words[(*num_words)++] = i + (quoted ? 1 : 0); 71 | state = 2; 72 | break; 73 | case 2: 74 | /* In a word. */ 75 | if (quoted && tl->buf[i] == '"') { 76 | quoted = FALSE; 77 | tl->buf[i] = 0; 78 | state = 1; 79 | } else if (!quoted && tl->buf[i] == ' ') { 80 | tl->buf[i] = 0; 81 | state = 1; 82 | } 83 | break; 84 | } 85 | } 86 | if (quoted) { 87 | if (!silent) 88 | tl->print(tl->user, "Unmatched quote."NL); 89 | unsplit_line(tl); 90 | return FALSE; 91 | } 92 | if (*num_words == TL_MAX_WORDS) { 93 | if (!silent) 94 | tl->print(tl->user, "Too many words."NL); 95 | unsplit_line(tl); 96 | return FALSE; 97 | } 98 | 99 | return TRUE; 100 | } 101 | 102 | static int history_previous(t_tokenline *tl, int entry) 103 | { 104 | int prev; 105 | 106 | if (entry == tl->hist_begin) 107 | return -1; 108 | 109 | /* Onto the terminating zero of the previous entry. */ 110 | if (--entry < 0) 111 | entry = TL_MAX_HISTORY_SIZE - 1; 112 | 113 | /* Onto the last byte of the previous entry. */ 114 | prev = entry; 115 | if (--entry < 0) 116 | entry = TL_MAX_HISTORY_SIZE - 1; 117 | while (tl->hist_buf[entry]) { 118 | prev = entry; 119 | if (--entry < 0) 120 | entry = TL_MAX_HISTORY_SIZE - 1; 121 | } 122 | 123 | return prev; 124 | } 125 | 126 | static void history_up(t_tokenline *tl) 127 | { 128 | int entry; 129 | 130 | if (tl->hist_step == -1) 131 | entry = history_previous(tl, tl->hist_end); 132 | else 133 | entry = history_previous(tl, tl->hist_step); 134 | if (entry == -1) 135 | return; 136 | line_clear(tl); 137 | set_line(tl, tl->hist_buf + entry); 138 | tl->hist_step = entry; 139 | } 140 | 141 | static void history_down(t_tokenline *tl) 142 | { 143 | int i; 144 | 145 | if (tl->hist_step == -1) 146 | return; 147 | 148 | line_clear(tl); 149 | if (tl->hist_step == tl->hist_end) { 150 | tl->hist_step = -1; 151 | return; 152 | } 153 | 154 | i = tl->hist_step; 155 | while (tl->hist_buf[i]) { 156 | if (++i == TL_MAX_HISTORY_SIZE) 157 | i = 0; 158 | } 159 | if (++i == TL_MAX_HISTORY_SIZE) 160 | i = 0; 161 | 162 | set_line(tl, tl->hist_buf + i); 163 | tl->hist_step = i; 164 | } 165 | 166 | static void history_show(t_tokenline *tl) 167 | { 168 | int entry, i; 169 | 170 | /* Skip the 'history' command itself. */ 171 | entry = history_previous(tl, tl->hist_end); 172 | for (entry = history_previous(tl, entry); entry != -1; 173 | entry = history_previous(tl, entry)) { 174 | tl->print(tl->user, tl->hist_buf + entry); 175 | 176 | /* Did we drop off the end of the buffer? */ 177 | for (i = entry; i < TL_MAX_HISTORY_SIZE; i++) { 178 | if (!tl->hist_buf[i]) 179 | break; 180 | } 181 | if (i == TL_MAX_HISTORY_SIZE) 182 | /* Yes we did */ 183 | tl->print(tl->user, tl->hist_buf); 184 | 185 | tl->print(tl->user, NL); 186 | } 187 | 188 | } 189 | 190 | /* Free at least size, but also fully zero out any deleted entry. */ 191 | static int history_delete(t_tokenline *tl, int entry, int size) 192 | { 193 | int freed, i; 194 | 195 | freed = 0; 196 | i = entry; 197 | while (freed < size || tl->hist_buf[i]) { 198 | tl->hist_buf[i] = 0; 199 | freed++; 200 | if (++i == TL_MAX_HISTORY_SIZE) 201 | i = 0; 202 | } 203 | tl->hist_buf[i] = 0; 204 | /* 205 | * Skip at least over the terminating zero, and any after that, 206 | * to the next entry. 207 | */ 208 | do { 209 | if (++i == TL_MAX_HISTORY_SIZE) 210 | i = 0; 211 | } while (!tl->hist_buf[i] && i < entry); 212 | 213 | return i; 214 | } 215 | 216 | static void history_add(t_tokenline *tl) 217 | { 218 | int size, entry, part1; 219 | 220 | size = strlen(tl->buf) + 1; 221 | if (tl->hist_begin == tl->hist_end) { 222 | /* First entry, both are 0. */ 223 | memcpy(tl->hist_buf, tl->buf, size); 224 | } else if (tl->hist_begin < tl->hist_end) { 225 | if (TL_MAX_HISTORY_SIZE - tl->hist_end >= size) { 226 | memcpy(tl->hist_buf + tl->hist_end, tl->buf, size); 227 | } else { 228 | part1 = TL_MAX_HISTORY_SIZE - tl->hist_end; 229 | entry = tl->hist_begin; 230 | tl->hist_begin = history_delete(tl, entry, size - part1); 231 | memcpy(tl->hist_buf + tl->hist_end, tl->buf, part1); 232 | memcpy(tl->hist_buf, tl->buf + part1, size - part1); 233 | } 234 | } else { 235 | part1 = tl->hist_begin - tl->hist_end; 236 | if (part1 <= size) { 237 | /* Not enough room between end and begin. */ 238 | tl->hist_begin = history_delete(tl, tl->hist_begin, size - part1); 239 | part1 = TL_MAX_HISTORY_SIZE - tl->hist_end; 240 | if (part1 < size) { 241 | memcpy(tl->hist_buf + tl->hist_end, tl->buf, part1); 242 | memcpy(tl->hist_buf, tl->buf + part1, size - part1); 243 | } else { 244 | memcpy(tl->hist_buf + tl->hist_end, tl->buf, size); 245 | } 246 | } else { 247 | /* Enough room between end and begin. */ 248 | memcpy(tl->hist_buf + tl->hist_end, tl->buf, size); 249 | } 250 | } 251 | tl->hist_end += size; 252 | if (tl->hist_end >= TL_MAX_HISTORY_SIZE) 253 | tl->hist_end -= TL_MAX_HISTORY_SIZE; 254 | if (tl->hist_begin == tl->hist_end) 255 | tl->hist_begin = history_delete(tl, tl->hist_begin, 256 | strlen(tl->hist_buf + tl->hist_begin)); 257 | } 258 | 259 | /* 260 | * Converts string to uint32_t. Takes decimal, hex prefixed with 0x, 261 | * binary prefixed with 0b and octal prefixed with 0. Returns FALSE 262 | * if conversion in any of these fails. 263 | */ 264 | static int str_to_uint(char *s, uint32_t *out) 265 | { 266 | int i; 267 | char *suffix; 268 | 269 | if (strncmp(s, "0b", 2)) { 270 | *out = strtoul(s, &suffix, 0); 271 | if (*suffix != '\0') 272 | return FALSE; 273 | } else { 274 | *out = 0; 275 | for (i = 2; s[i]; i++) { 276 | if (s[i] != '0' && s[i] != '1') 277 | return FALSE; 278 | *out <<= 1; 279 | *out |= s[i] - 0x30; 280 | } 281 | } 282 | 283 | return TRUE; 284 | } 285 | 286 | static char *arg_type_to_string(int arg_type) 287 | { 288 | if (arg_type == T_ARG_UINT) 289 | return ""; 290 | else if (arg_type == T_ARG_FLOAT) 291 | return ""; 292 | else if (arg_type == T_ARG_STRING) 293 | return ""; 294 | 295 | return NULL; 296 | } 297 | 298 | static int find_token(t_token *tokens, t_token_dict *token_dict, char *word) 299 | { 300 | uint32_t arg_uint; 301 | int token, partial, i; 302 | 303 | /* Find exact match. */ 304 | for (i = 0; tokens[i].token; i++) { 305 | token = tokens[i].token; 306 | if (token == T_ARG_UINT) { 307 | if (str_to_uint(word, &arg_uint)) 308 | return i; 309 | } else if (token > T_ARG_UINT) { 310 | continue; 311 | } else { 312 | if (!strcmp(word, token_dict[token].tokenstr)) 313 | return i; 314 | } 315 | } 316 | 317 | /* Find partial match. */ 318 | partial = -1; 319 | for (i = 0; tokens[i].token; i++) { 320 | token = tokens[i].token; 321 | if (token >= T_ARG_UINT) 322 | continue; 323 | if (strlen(word) >= strlen(token_dict[token].tokenstr)) 324 | continue; 325 | if (!strncmp(word, token_dict[token].tokenstr, strlen(word))) { 326 | if (partial != -1) 327 | /* Not unique. */ 328 | return -1; 329 | else 330 | partial = i; 331 | } 332 | } 333 | 334 | return partial; 335 | } 336 | 337 | /* 338 | * Tokenize the current set of NULL-terminated words, allowing for 339 | * one token sublevel starting from the current token level. 340 | */ 341 | static int tokenize(t_tokenline *tl, int *words, int num_words, 342 | t_token **complete_tokens, int *complete_arg) 343 | { 344 | t_token *token_stack[8], *arg_tokens; 345 | t_tokenline_parsed *p; 346 | float arg_float; 347 | uint32_t arg_uint, suffix_uint; 348 | int done, arg_needed, w, t, t_idx, size; 349 | int cur_tsp, cur_tp, cur_bufsize, i; 350 | char *word, *suffix; 351 | 352 | done = FALSE; 353 | p = &tl->parsed; 354 | token_stack[0] = tl->token_levels[tl->token_level]; 355 | cur_tsp = 0; 356 | cur_tp = 0; 357 | cur_bufsize = 0; 358 | arg_needed = 0; 359 | arg_tokens = NULL; 360 | for (w = 0; w < num_words; w++) { 361 | word = tl->buf + words[w]; 362 | if (done) { 363 | if (!complete_tokens) 364 | tl->print(tl->user, "Too many arguments."NL); 365 | return FALSE; 366 | } else if (!arg_needed) { 367 | /* Token needed. */ 368 | if ((suffix = strchr(word, TL_TOKEN_DELIMITER))) { 369 | *suffix++ = 0; 370 | if (!str_to_uint(suffix, &suffix_uint)) { 371 | tl->print(tl->user, "Invalid number."NL); 372 | return FALSE; 373 | } 374 | } else { 375 | suffix_uint = 0; 376 | } 377 | 378 | if ((t_idx = find_token(token_stack[cur_tsp], tl->token_dict, word)) > -1) { 379 | t = token_stack[cur_tsp][t_idx].token; 380 | p->tokens[cur_tp++] = t; 381 | if (t == T_ARG_UINT) { 382 | /* Integer token. */ 383 | str_to_uint(word, &arg_uint); 384 | p->tokens[cur_tp++] = cur_bufsize; 385 | memcpy(p->buf + cur_bufsize, &arg_uint, sizeof(uint32_t)); 386 | cur_bufsize += sizeof(uint32_t); 387 | } 388 | if (suffix) { 389 | if (!(token_stack[cur_tsp][t_idx].flags & 390 | T_FLAG_SUFFIX_TOKEN_DELIM_INT)) { 391 | if (!complete_tokens) 392 | tl->print(tl->user, "Token suffix not allowed."NL); 393 | return FALSE; 394 | } 395 | if (suffix_uint > 1) { 396 | p->tokens[cur_tp++] = T_ARG_TOKEN_SUFFIX_INT; 397 | p->tokens[cur_tp++] = cur_bufsize; 398 | memcpy(p->buf + cur_bufsize, &suffix_uint, sizeof(uint32_t)); 399 | cur_bufsize += sizeof(uint32_t); 400 | } 401 | } 402 | p->last_token_entry = &token_stack[cur_tsp][t_idx]; 403 | 404 | if (token_stack[cur_tsp][t_idx].arg_type == T_ARG_HELP) { 405 | /* Nothing to do, just keep cur_tsp from increasing. */ 406 | } else if (token_stack[cur_tsp][t_idx].arg_type) { 407 | /* Token needs an argument */ 408 | arg_needed = token_stack[cur_tsp][t_idx].arg_type; 409 | if (arg_needed == T_ARG_TOKEN) 410 | /* Argument is one of these subtokens. */ 411 | arg_tokens = token_stack[cur_tsp][t_idx].subtokens; 412 | } else if (token_stack[cur_tsp][t_idx].subtokens) { 413 | /* Switch to a new token set. */ 414 | token_stack[cur_tsp + 1] = token_stack[cur_tsp][t_idx].subtokens; 415 | cur_tsp++; 416 | } else { 417 | /* Not expecting any more arguments or tokens. */ 418 | done = tl->one_command_per_line; 419 | } 420 | } else { 421 | /* 422 | * No matching token found, but maybe the token 423 | * set allows freeform strings? 424 | */ 425 | for (i = 0; token_stack[cur_tsp][i].token; i++) { 426 | if (token_stack[cur_tsp][i].token == T_ARG_STRING) 427 | break; 428 | } 429 | if (token_stack[cur_tsp][i].token) { 430 | /* Add it in as a token. */ 431 | p->tokens[cur_tp++] = T_ARG_STRING; 432 | p->tokens[cur_tp++] = cur_bufsize; 433 | size = strlen(word) + 1; 434 | memcpy(p->buf + cur_bufsize, word, size); 435 | cur_bufsize += size; 436 | p->buf[cur_bufsize] = 0; 437 | } else { 438 | if (!complete_tokens) 439 | tl->print(tl->user, "Invalid command."NL); 440 | return FALSE; 441 | } 442 | } 443 | } else { 444 | /* Parse word as the type in arg_needed */ 445 | switch (arg_needed) { 446 | case T_ARG_UINT: 447 | str_to_uint(word, &arg_uint); 448 | if (*suffix) { 449 | switch(*suffix) 450 | { 451 | case 'k': 452 | arg_uint *= 1000; 453 | break; 454 | case 'm': 455 | arg_uint *= 1000000; 456 | break; 457 | case 'g': 458 | arg_uint *= 1000000000L; 459 | break; 460 | default: 461 | if (!complete_tokens) 462 | tl->print(tl->user, "Invalid value."NL); 463 | return FALSE; 464 | } 465 | } 466 | p->tokens[cur_tp++] = T_ARG_UINT; 467 | p->tokens[cur_tp++] = cur_bufsize; 468 | memcpy(p->buf + cur_bufsize, &arg_uint, sizeof(uint32_t)); 469 | cur_bufsize += sizeof(uint32_t); 470 | break; 471 | case T_ARG_FLOAT: 472 | arg_float = strtof(word, &suffix); 473 | if (*suffix) { 474 | switch(*suffix) 475 | { 476 | case 'k': 477 | arg_float *= 1000; 478 | break; 479 | case 'm': 480 | arg_float *= 1000000; 481 | break; 482 | case 'g': 483 | arg_float *= 1000000000L; 484 | break; 485 | default: 486 | if (!complete_tokens) 487 | tl->print(tl->user, "Invalid value."NL); 488 | return FALSE; 489 | } 490 | } 491 | p->tokens[cur_tp++] = T_ARG_FLOAT; 492 | p->tokens[cur_tp++] = cur_bufsize; 493 | memcpy(p->buf + cur_bufsize, &arg_float, sizeof(float)); 494 | cur_bufsize += sizeof(float); 495 | break; 496 | case T_ARG_STRING: 497 | p->tokens[cur_tp++] = T_ARG_STRING; 498 | p->tokens[cur_tp++] = cur_bufsize; 499 | size = strlen(word) + 1; 500 | memcpy(p->buf + cur_bufsize, word, size); 501 | cur_bufsize += size; 502 | p->buf[cur_bufsize] = 0; 503 | break; 504 | case T_ARG_TOKEN: 505 | if ((t_idx = find_token(arg_tokens, tl->token_dict, word)) > -1) { 506 | p->tokens[cur_tp++] = arg_tokens[t_idx].token; 507 | p->last_token_entry = &arg_tokens[t_idx]; 508 | } else { 509 | if (!complete_tokens) 510 | tl->print(tl->user, "Invalid value."NL); 511 | return FALSE; 512 | } 513 | break; 514 | } 515 | arg_needed = 0; 516 | done = tl->one_command_per_line; 517 | } 518 | } 519 | if (arg_needed && !complete_tokens) { 520 | tl->print(tl->user, "Missing argument."NL); 521 | return FALSE; 522 | } 523 | 524 | p->tokens[cur_tp] = 0; 525 | 526 | if (complete_tokens) { 527 | if (done) { 528 | /* Nothing to add. */ 529 | *complete_tokens = NULL; 530 | } else { 531 | /* Fill in the completion token list. */ 532 | if (arg_needed == T_ARG_TOKEN) 533 | *complete_tokens = arg_tokens; 534 | else 535 | *complete_tokens = token_stack[cur_tsp]; 536 | } 537 | } 538 | if (complete_arg) 539 | *complete_arg = arg_needed; 540 | 541 | return TRUE; 542 | } 543 | 544 | static void show_help(t_tokenline *tl, int *words, int num_words) 545 | { 546 | t_token *tokens; 547 | int i; 548 | char *s; 549 | 550 | (void)words; 551 | 552 | if (tl->parsed.last_token_entry) { 553 | if (tl->parsed.last_token_entry->help_full) { 554 | tl->print(tl->user, tl->parsed.last_token_entry->help_full); 555 | tl->print(tl->user, NL); 556 | } else if (tl->parsed.last_token_entry->help) { 557 | tl->print(tl->user, tl->parsed.last_token_entry->help); 558 | tl->print(tl->user, NL); 559 | } 560 | } 561 | 562 | if (num_words == 1) 563 | /* Just "help" -- global command overview. */ 564 | tokens = tl->token_levels[tl->token_level]; 565 | else 566 | tokens = tl->parsed.last_token_entry->subtokens; 567 | if (tokens) { 568 | for (i = 0; tokens[i].token; i++) { 569 | tl->print(tl->user, INDENT); 570 | if (tokens[i].token < T_ARG_UINT) 571 | s = tl->token_dict[tokens[i].token].tokenstr; 572 | else 573 | s = arg_type_to_string(tokens[i].token); 574 | tl->print(tl->user, s); 575 | if (tokens[i].help) { 576 | tl->print(tl->user, space + strlen(s)); 577 | tl->print(tl->user, tokens[i].help); 578 | } 579 | tl->print(tl->user, NL); 580 | } 581 | } 582 | if ((!tl->parsed.last_token_entry || !tl->parsed.last_token_entry->help) 583 | && !tokens) 584 | tl->print(tl->user, NO_HELP); 585 | } 586 | 587 | static void process_line(t_tokenline *tl) 588 | { 589 | t_token *tokens; 590 | int words[TL_MAX_WORDS], num_words, i; 591 | 592 | tl->print(tl->user, NL); 593 | do { 594 | if (!tl->buf_len) 595 | break; 596 | history_add(tl); 597 | if (!split_line(tl, words, &num_words, FALSE)) 598 | break; 599 | if (!num_words) 600 | break; 601 | if (!strcmp(tl->buf + words[0], "help")) { 602 | if (num_words == 1) { 603 | /* 604 | * Nothing to tokenize: find the help entry 605 | * if any so its help text can be shown. 606 | */ 607 | tokens = tl->token_levels[tl->token_level]; 608 | for (i = 0; tokens[i].token; i++) { 609 | if (tokens[i].arg_type == T_ARG_HELP) { 610 | tl->parsed.last_token_entry = &tokens[i]; 611 | break; 612 | } 613 | } 614 | } else { 615 | /* Tokenize with errors turned off. */ 616 | tokenize(tl, words + 1, num_words - 1, &tokens, NULL); 617 | } 618 | show_help(tl, words, num_words); 619 | } else if (!strcmp(tl->buf + words[0], "history")) { 620 | history_show(tl); 621 | } else { 622 | if (!tokenize(tl, words, num_words, NULL, NULL)) 623 | break; 624 | if (tl->callback) 625 | tl->callback(tl->user, &tl->parsed); 626 | } 627 | } while (FALSE); 628 | 629 | tl->buf[0] = 0; 630 | tl->buf_len = 0; 631 | tl->escape_len = 0; 632 | tl->pos = 0; 633 | tl->hist_step = -1; 634 | tl->print(tl->user, tl->prompt); 635 | } 636 | 637 | static void add_char(t_tokenline *tl, int c) 638 | { 639 | int i; 640 | 641 | if (tl->pos == tl->buf_len) { 642 | tl->buf[tl->buf_len++] = c; 643 | tl->buf[tl->buf_len] = 0; 644 | tl->print(tl->user, tl->buf + tl->buf_len - 1); 645 | tl->pos++; 646 | } else { 647 | memmove(tl->buf + tl->pos + 1, tl->buf + tl->pos, 648 | tl->buf_len - tl->pos + 1); 649 | tl->buf[tl->pos] = c; 650 | tl->print(tl->user, tl->buf + tl->pos); 651 | for (i = 0; i < tl->buf_len - tl->pos; i++) 652 | tl->print(tl->user, "\x1b\x5b\x44"); 653 | tl->buf_len++; 654 | tl->pos++; 655 | } 656 | } 657 | 658 | static void set_line(t_tokenline *tl, char *line) 659 | { 660 | int size; 661 | 662 | size = strlen(line); 663 | if (size > TL_MAX_LINE_LEN - 1) { 664 | add_char(tl, '!'); 665 | return; 666 | } 667 | 668 | if (tl->pos == tl->buf_len) { 669 | tl->print(tl->user, line); 670 | tl->pos += size; 671 | memcpy(tl->buf + tl->buf_len, line, size); 672 | tl->buf_len += size; 673 | tl->buf[tl->buf_len] = 0; 674 | } 675 | } 676 | 677 | static void print_token_and_help(t_tokenline *tl, t_token *token) 678 | { 679 | char *s; 680 | 681 | tl->print(tl->user, INDENT); 682 | if (token->token < T_ARG_UINT) 683 | s = tl->token_dict[token->token].tokenstr; 684 | else 685 | s = arg_type_to_string(token->token); 686 | tl->print(tl->user, s); 687 | if (token->help) { 688 | tl->print(tl->user, space + strlen(s)); 689 | tl->print(tl->user, token->help); 690 | } 691 | } 692 | 693 | static void complete(t_tokenline *tl) 694 | { 695 | t_token *tokens, *partial; 696 | unsigned int i; 697 | int words[TL_MAX_WORDS], num_words, arg_needed, multiple; 698 | int reprompt, t; 699 | char *word; 700 | 701 | reprompt = FALSE; 702 | if (!tl->pos) { 703 | /* Tab on an empty line: show all commmands. */ 704 | tl->print(tl->user, NL); 705 | tokens = tl->token_levels[tl->token_level]; 706 | for (i = 0; tokens[i].token; i++) { 707 | print_token_and_help(tl, &tokens[i]); 708 | tl->print(tl->user, NL); 709 | } 710 | reprompt = TRUE; 711 | } else if (tl->buf[tl->pos - 1] != ' ') { 712 | /* Try to complete the current word. */ 713 | if (!split_line(tl, words, &num_words, TRUE) || !num_words) 714 | return; 715 | if (tokenize(tl, words, num_words - 1, &tokens, NULL)) { 716 | if (tokens) { 717 | word = tl->buf + words[num_words - 1]; 718 | partial = NULL; 719 | multiple = FALSE; 720 | for (t = 0; tokens[t].token; t++) { 721 | if (tokens[t].token >= T_ARG_UINT) 722 | continue; 723 | if (!strncmp(word, tl->token_dict[tokens[t].token].tokenstr, strlen(word))) { 724 | if (partial) { 725 | /* Not the first match, print previous one. */ 726 | multiple = TRUE; 727 | tl->print(tl->user, NL); 728 | print_token_and_help(tl, partial); 729 | } 730 | partial = &tokens[t]; 731 | } 732 | } 733 | if (partial) { 734 | if (multiple) { 735 | /* Last partial match. */ 736 | tl->print(tl->user, NL); 737 | print_token_and_help(tl, partial); 738 | tl->print(tl->user, NL); 739 | reprompt = TRUE; 740 | } else { 741 | for (i = strlen(word); i < strlen(tl->token_dict[partial->token].tokenstr); i++) 742 | add_char(tl, tl->token_dict[partial->token].tokenstr[i]); 743 | add_char(tl, ' '); 744 | } 745 | } 746 | } 747 | } 748 | } else { 749 | /* List all possible tokens from this point. */ 750 | if (!split_line(tl, words, &num_words, TRUE) || !num_words) 751 | return; 752 | if (tokenize(tl, words, num_words, &tokens, &arg_needed)) { 753 | if (arg_needed && arg_needed != T_ARG_TOKEN) { 754 | tl->print(tl->user, INDENT NL); 755 | tl->print(tl->user, arg_type_to_string(arg_needed)); 756 | tl->print(tl->user, NL); 757 | reprompt = TRUE; 758 | } else if (tokens) { 759 | tl->print(tl->user, NL); 760 | for (t = 0; tokens[t].token; t++) { 761 | print_token_and_help(tl, &tokens[t]); 762 | tl->print(tl->user, NL); 763 | reprompt = TRUE; 764 | } 765 | } 766 | } 767 | } 768 | unsplit_line(tl); 769 | if (reprompt) { 770 | tl->print(tl->user, tl->prompt); 771 | tl->print(tl->user, tl->buf); 772 | } 773 | } 774 | 775 | static void line_clear(t_tokenline *tl) 776 | { 777 | while (tl->pos < tl->buf_len) { 778 | tl->pos++; 779 | tl->print(tl->user, "\x1b\x5b\x43"); 780 | } 781 | while (tl->pos) 782 | line_backspace(tl); 783 | } 784 | 785 | static void line_backspace(t_tokenline *tl) 786 | { 787 | int i; 788 | 789 | if (tl->pos == tl->buf_len) { 790 | tl->buf[tl->buf_len - 1] = 0; 791 | tl->print(tl->user, "\x1b\x5b\x44 \x1b\x5b\x44"); 792 | } else { 793 | memmove(tl->buf + tl->pos - 1, tl->buf + tl->pos, 794 | tl->buf_len - tl->pos + 1); 795 | tl->print(tl->user, "\x1b\x5b\x44"); 796 | tl->print(tl->user, tl->buf + tl->pos - 1); 797 | tl->print(tl->user, " "); 798 | for (i = 0; i < tl->buf_len - tl->pos + 1; i++) 799 | tl->print(tl->user, "\x1b\x5b\x44"); 800 | } 801 | tl->buf_len--; 802 | tl->pos--; 803 | } 804 | 805 | static void line_home(t_tokenline *tl) 806 | { 807 | while (tl->pos) { 808 | tl->pos--; 809 | tl->print(tl->user, "\x1b\x5b\x44"); 810 | } 811 | } 812 | 813 | static void line_end(t_tokenline *tl) 814 | { 815 | while (tl->pos < tl->buf_len) { 816 | tl->pos++; 817 | tl->print(tl->user, "\x1b\x5b\x43"); 818 | } 819 | } 820 | 821 | static void line_delete_char(t_tokenline *tl) 822 | { 823 | int i; 824 | 825 | if (tl->pos < tl->buf_len) { 826 | memmove(tl->buf + tl->pos, tl->buf + tl->pos + 1, 827 | tl->buf_len - tl->pos); 828 | tl->print(tl->user, tl->buf + tl->pos); 829 | tl->print(tl->user, " "); 830 | for (i = 0; i < tl->buf_len - tl->pos; i++) 831 | tl->print(tl->user, "\x1b\x5b\x44"); 832 | tl->buf_len--; 833 | } 834 | } 835 | 836 | static int process_escape(t_tokenline *tl) 837 | { 838 | if (tl->escape_len == 4) { 839 | if (!strncmp(tl->escape, "\x1b\x5b\x33\x7e", 4)) { 840 | /* Delete */ 841 | line_delete_char(tl); 842 | } else if (!strncmp(tl->escape, "\x1b\x5b\x31\x7e", 4)) { 843 | /* Home */ 844 | line_home(tl); 845 | } else if (!strncmp(tl->escape, "\x1b\x5b\x34\x7e", 4)) { 846 | /* End */ 847 | line_end(tl); 848 | } 849 | } else if (tl->escape_len == 3) { 850 | if (!strncmp(tl->escape, "\x1b\x5b\x41", 3)) { 851 | /* Up arrow */ 852 | history_up(tl); 853 | } else if (!strncmp(tl->escape, "\x1b\x5b\x42", 3)) { 854 | /* Down arrow */ 855 | history_down(tl); 856 | } else if (!strncmp(tl->escape, "\x1b\x5b\x44", 3)) { 857 | /* Left arrow */ 858 | if (tl->pos > 0) { 859 | tl->pos--; 860 | tl->print(tl->user, "\x1b\x5b\x31\x44"); 861 | } 862 | } else if (!strncmp(tl->escape, "\x1b\x5b\x43", 3)) { 863 | /* Right arrow */ 864 | if (tl->pos < tl->buf_len) { 865 | tl->pos++; 866 | tl->print(tl->user, "\x1b\x5b\x31\x43"); 867 | } 868 | } else if (!strncmp(tl->escape, "\x1b\x4f\x48", 3)) { 869 | /* Home */ 870 | line_home(tl); 871 | } else if (!strncmp(tl->escape, "\x1b\x4f\x46", 3)) { 872 | /* End */ 873 | line_end(tl); 874 | } else 875 | return FALSE; 876 | } else 877 | return FALSE; 878 | 879 | return TRUE; 880 | } 881 | 882 | void tl_init(t_tokenline *tl, t_token *tokens_top, t_token_dict *token_dict, 883 | tl_printfunc printfunc, void *user) 884 | { 885 | memset(tl, 0, sizeof(t_tokenline)); 886 | tl->token_levels[0] = tokens_top; 887 | tl->token_dict = token_dict; 888 | tl->print = printfunc; 889 | tl->user = user; 890 | tl->one_command_per_line = TL_ONE_COMMAND_PER_LINE; 891 | tl->hist_step = -1; 892 | } 893 | 894 | void tl_set_prompt(t_tokenline *tl, char *prompt) 895 | { 896 | if (!tl->prompt) 897 | tl->print(tl->user, prompt); 898 | tl->prompt = prompt; 899 | } 900 | 901 | void tl_set_callback(t_tokenline *tl, tl_callback callback) 902 | { 903 | tl->callback = callback; 904 | } 905 | 906 | int tl_mode_push(t_tokenline *tl, t_token *tokens) 907 | { 908 | if (tl->token_level == TL_MAX_TOKEN_LEVELS - 1) 909 | return FALSE; 910 | 911 | tl->token_levels[++tl->token_level] = tokens; 912 | 913 | return TRUE; 914 | } 915 | 916 | int tl_mode_pop(t_tokenline *tl) 917 | { 918 | if (!tl->token_level) 919 | return FALSE; 920 | 921 | tl->token_level--; 922 | 923 | return TRUE; 924 | } 925 | 926 | int tl_input(t_tokenline *tl, uint8_t c) 927 | { 928 | int ret, i; 929 | 930 | if (tl->escape_len) { 931 | tl->escape[tl->escape_len++] = c; 932 | if (process_escape(tl)) 933 | tl->escape_len = 0; 934 | else if (tl->escape_len == TL_MAX_ESCAPE_LEN) 935 | /* Not a valid escape sequence, and buffer full. */ 936 | tl->escape_len = 0; 937 | return TRUE; 938 | } 939 | 940 | ret = TRUE; 941 | switch (c) { 942 | case 0x1b: 943 | /* Start of escape sequence. */ 944 | tl->escape[tl->escape_len++] = c; 945 | break; 946 | case '\r': 947 | case '\n': 948 | process_line(tl); 949 | break; 950 | case '\t': 951 | if (tl->buf_len == tl->pos) 952 | complete(tl); 953 | break; 954 | case 0x7f: 955 | /* Backspace */ 956 | if (tl->pos) 957 | line_backspace(tl); 958 | break; 959 | case 0x01: 960 | /* Ctrl-a */ 961 | while (tl->pos) { 962 | tl->pos--; 963 | tl->print(tl->user, "\x1b\x5b\x44"); 964 | } 965 | break; 966 | case 0x03: 967 | /* Ctrl-c */ 968 | tl->print(tl->user, "^C"); 969 | tl->buf_len = 0; 970 | process_line(tl); 971 | break; 972 | case 0x04: 973 | /* Ctrl-d */ 974 | if (tl->buf_len) 975 | line_delete_char(tl); 976 | else 977 | /* Ctrl-d on empty line exits. */ 978 | ret = FALSE; 979 | break; 980 | case 0x05: 981 | /* Ctrl-e */ 982 | while (tl->pos < tl->buf_len) { 983 | tl->pos++; 984 | tl->print(tl->user, "\x1b\x5b\x43"); 985 | } 986 | break; 987 | case 0x0b: 988 | /* Ctrl-k */ 989 | if (tl->buf_len > tl->pos) { 990 | for (i = 0; i < tl->buf_len - tl->pos; i++) 991 | tl->print(tl->user, " "); 992 | for (i = 0; i < tl->buf_len - tl->pos; i++) 993 | tl->print(tl->user, "\x1b\x5b\x44"); 994 | tl->buf_len = tl->pos; 995 | tl->buf[tl->buf_len] = 0; 996 | } 997 | break; 998 | case 0x0c: 999 | /* Ctrl-l */ 1000 | tl->print(tl->user, "\x1b\x5b\x32\x4a\x1b\x5b\x48"); 1001 | tl->print(tl->user, tl->prompt); 1002 | tl->print(tl->user, tl->buf); 1003 | break; 1004 | case 0x10: 1005 | /* Ctrl-p */ 1006 | history_up(tl); 1007 | break; 1008 | case 0x0e: 1009 | /* Ctrl-n */ 1010 | history_down(tl); 1011 | break; 1012 | case 0x17: 1013 | /* Ctrl-w */ 1014 | while (tl->pos && tl->buf[tl->pos - 1] == ' ') 1015 | line_backspace(tl); 1016 | while (tl->pos && tl->buf[tl->pos - 1] != ' ') 1017 | line_backspace(tl); 1018 | break; 1019 | default: 1020 | if (c >= 0x20 && c <= 0x7e) { 1021 | if (tl->buf_len < TL_MAX_LINE_LEN - 1) 1022 | add_char(tl, c); 1023 | tl->hist_step = -1; 1024 | } 1025 | break; 1026 | } 1027 | 1028 | return ret; 1029 | } 1030 | 1031 | -------------------------------------------------------------------------------- /tokenline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Bert Vermeulen 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | #ifndef TOKENLINE_H 19 | #define TOKENLINE_H 20 | 21 | #include 22 | 23 | #define TL_MAX_LINE_LEN 128 24 | #define TL_MAX_ESCAPE_LEN 8 25 | #define TL_MAX_WORDS 64 26 | #define TL_MAX_TOKEN_LEVELS 8 27 | #define TL_MAX_HISTORY_SIZE 512 28 | #define TL_TOKEN_DELIMITER ':' 29 | #define TL_ONE_COMMAND_PER_LINE FALSE 30 | 31 | enum { 32 | /* Token can be optionally suffixed by delimiter and integer. */ 33 | T_FLAG_SUFFIX_TOKEN_DELIM_INT = (1 << 0), 34 | }; 35 | 36 | typedef struct token_dict { 37 | int token; 38 | char *tokenstr; 39 | } t_token_dict; 40 | 41 | typedef struct token { 42 | int token; 43 | uint16_t arg_type; 44 | uint16_t flags; 45 | struct token *subtokens; 46 | char *help; 47 | char *help_full; 48 | } t_token; 49 | 50 | typedef struct tokenline_parsed { 51 | int tokens[TL_MAX_WORDS]; 52 | char buf[TL_MAX_LINE_LEN]; 53 | t_token *last_token_entry; 54 | } t_tokenline_parsed; 55 | 56 | typedef void (*tl_printfunc)(void *user, const char *str); 57 | typedef void (*tl_callback)(void *user, t_tokenline_parsed *p); 58 | typedef struct tokenline { 59 | t_token *token_levels[TL_MAX_TOKEN_LEVELS]; 60 | int token_level; 61 | t_token_dict *token_dict; 62 | tl_printfunc print; 63 | void *user; 64 | char buf[TL_MAX_LINE_LEN]; 65 | int buf_len; 66 | char escape[TL_MAX_ESCAPE_LEN]; 67 | int escape_len; 68 | char *prompt; 69 | tl_callback callback; 70 | int pos; 71 | int one_command_per_line; 72 | t_tokenline_parsed parsed; 73 | int hist_step; 74 | int hist_begin; 75 | int hist_end; 76 | /* Need a terminating zero to print last chunk in ring buffer. */ 77 | char hist_buf[TL_MAX_HISTORY_SIZE + 1]; 78 | } t_tokenline; 79 | 80 | /* These share a number space with the tokens. */ 81 | enum token_argtypes { 82 | /* Optionally followed by k (kilo), m (mega) or g (giga). */ 83 | T_ARG_UINT = 10000, 84 | /* Optionally followed by k (kilo), m (mega) or g (giga). */ 85 | T_ARG_FLOAT, 86 | T_ARG_STRING, 87 | /* Argument is one of the tokens in subtokens. */ 88 | T_ARG_TOKEN, 89 | /* Integer argument suffixed to token with delimiter and integer. */ 90 | T_ARG_TOKEN_SUFFIX_INT, 91 | T_ARG_HELP, 92 | }; 93 | 94 | void tl_init(t_tokenline *tl, t_token *tokens_top, t_token_dict *token_dict, 95 | tl_printfunc printfunc, void *user); 96 | void tl_set_prompt(t_tokenline *tl, char *prompt); 97 | void tl_set_callback(t_tokenline *tl, tl_callback callback); 98 | int tl_mode_push(t_tokenline *tl, t_token *tokens_mode); 99 | int tl_mode_pop(t_tokenline *tl); 100 | int tl_input(t_tokenline *tl, uint8_t c); 101 | 102 | #ifndef NULL 103 | #define NULL 0 104 | #endif 105 | 106 | #ifndef TRUE 107 | #define TRUE 1 108 | #endif 109 | 110 | #ifndef FALSE 111 | #define FALSE 0 112 | #endif 113 | 114 | #endif 115 | --------------------------------------------------------------------------------