Parse string tokens and pass remaining as parameter
I am working on a command line tool for an open source project. When you type a command it parses it into tokens using strtok_r(). This is working great except some commands have additional parameters that I want to pass to the command function.
The problem is that the buffer used by strtok_r() does not work because it removes the delimiter char " " and seems to replace them with "\0" to terminate the token. So when I pass that buffer I can only parse the first token of the original command and don't even see the parameters. So I need to pass the remaining un-used tokens as a string to the next function. My thought right now is to make another copy of the original string and maybe remove a sub-string from the beginning of it that is the command being executed. I can store the full command string excluding parameters in the command structure easy enough to be used for this. Is there another way I should consider for this though? |
How are you using strtok_r()? You should be able to get at all parts of the string - but you need to do it in the right way. For example, suppose you first call it like:
Code:
char *input_string; Code:
// NULL => use previous string, saveptr unchanged from before. For example: Code:
#include <string.h> |
May I suggest using a standard solution to commandline argument processing: getopt ? Doing this will let you use code which is well defined and debugged, behaves for the end-user like virtually all other commandline applications, is flexible enough to accommodate pretty well any requirement you can throw at it, and best of all, it's already done.
--- rod. |
I make a copy of my original string then parsing the copy with strtok_r(). I haven't had any problems accessing the tokens. At some point I have to stop and pass the remaining un-read tokens of the string to another function.
Never thought of passing the copied string and saved pointer so that just might work. It however wont work if I have multiple functions that need to execute using the same parameter. Not sure if I should keep that functionality in or not. I might keep it only for commands that don't accept parameters. As for using getopt() I like to think my cli is a bit more sophisticated that simply parsing a few static arguments. Its a modular cli with commands registered at run-time. The registered command are parsed. They are stored in a tree with a dynamic help menu and other neat-o goodness. The cli functions more like a shell environment for a RTOS think a router command line. When the user types a command it parses the first token tries to locate a match in the root tree nodes then follows them down until it finds one of them with a valid function pointer. It then executes that function pointer passing it any remaining tokens. Here is the full code. https://sourceforge.net/p/opennop/da...commands.c#l37 |
Quote:
|
Quote:
|
Quote:
--- rod. |
Quote:
So far I am having some luck with the dynamic array of char pointers. For each "parameter" token I re-allocate the array and point the next array index at that token. It seems to work but has a few issues right now. After 3-4 tokens its crashing. I will save that for another thread that I am off to make now. |
All times are GMT -5. The time now is 02:36 PM. |