2 mtr -- a network diagnostic tool
3 Copyright (C) 2016 Matt Kimball
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
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.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 NUL terminate the whitespace separated tokens in the command string.
28 This modifies command_string in-place with NUL characters.
29 Fill the tokens array with pointers to the tokens, and return the
30 number of tokens found.
42 for (i
= 0; command_string
[i
]; i
++) {
44 if (!isspace((unsigned char) command_string
[i
])) {
45 /* Take care not to exceed the token array length */
46 if (token_count
>= max_tokens
) {
50 tokens
[token_count
++] = &command_string
[i
];
54 if (isspace((unsigned char) command_string
[i
])) {
55 command_string
[i
] = 0;
65 Parse a command string (or command reply string) into a command_t
66 structure for later semantic interpretation. Returns EINVAL if the
67 command string is unparseable or zero for success.
69 command_string will be modified in-place with NUL characters terminating
70 tokens, and the command_t will use pointers to the contents of
71 command_string without copying, so any interpretation of the
72 command_t structure requires that the command_string memory has not yet
73 been freed or otherwise reused.
76 struct command_t
*command
,
79 char *tokens
[MAX_COMMAND_TOKENS
];
83 memset(command
, 0, sizeof(struct command_t
));
85 /* Tokenize the string using whitespace */
87 tokenize_command(tokens
, MAX_COMMAND_TOKENS
, command_string
);
88 if (token_count
< 2) {
93 /* Expect the command token to be a numerical value */
95 command
->token
= strtol(tokens
[0], NULL
, 10);
100 command
->command_name
= tokens
[1];
103 The tokens beyond the command name are expected to be in
107 command
->argument_count
= 0;
108 while (i
< token_count
) {
109 /* It's an error if we get a name without a key */
110 if (i
+ 1 >= token_count
) {
115 /* It's an error if we get more arguments than we have space for */
116 if (command
->argument_count
>= MAX_COMMAND_ARGUMENTS
) {
121 command
->argument_name
[command
->argument_count
] = tokens
[i
];
122 command
->argument_value
[command
->argument_count
] = tokens
[i
+ 1];
123 command
->argument_count
++;