del: 将go版本的内容删除,只保留处理日志功能
This commit is contained in:
77
src/interactive.c
Normal file
77
src/interactive.c
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "interactive.h"
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
static void interactive_skip_spaces(const char **cursor) {
|
||||
while (**cursor != '\0' && isspace((unsigned char) **cursor)) {
|
||||
(*cursor)++;
|
||||
}
|
||||
}
|
||||
|
||||
int interactive_parse_command(const char *line, interactive_command_t *command, char *err, size_t err_len) {
|
||||
const char *cursor = line;
|
||||
char action[16];
|
||||
size_t action_len = 0;
|
||||
size_t to_len = 0;
|
||||
size_t value_len;
|
||||
|
||||
if (line == NULL || command == NULL) {
|
||||
snprintf(err, err_len, "interactive: invalid command");
|
||||
return -1;
|
||||
}
|
||||
memset(command, 0, sizeof(*command));
|
||||
interactive_skip_spaces(&cursor);
|
||||
while (*cursor != '\0' && !isspace((unsigned char) *cursor) && action_len + 1 < sizeof(action)) {
|
||||
action[action_len++] = *cursor++;
|
||||
}
|
||||
action[action_len] = '\0';
|
||||
if (action_len == 0) {
|
||||
snprintf(err, err_len, "interactive: empty command");
|
||||
return -1;
|
||||
}
|
||||
if (strcmp(action, "help") == 0) {
|
||||
command->type = INTERACTIVE_CMD_HELP;
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(action, "quit") == 0) {
|
||||
command->type = INTERACTIVE_CMD_QUIT;
|
||||
return 0;
|
||||
}
|
||||
|
||||
interactive_skip_spaces(&cursor);
|
||||
while (*cursor != '\0' && !isspace((unsigned char) *cursor) && to_len + 1 < sizeof(command->to)) {
|
||||
command->to[to_len++] = *cursor++;
|
||||
}
|
||||
command->to[to_len] = '\0';
|
||||
interactive_skip_spaces(&cursor);
|
||||
if (command->to[0] == '\0' || *cursor == '\0') {
|
||||
snprintf(err, err_len, "interactive: missing target or value");
|
||||
return -1;
|
||||
}
|
||||
|
||||
value_len = strlen(cursor);
|
||||
if (value_len >= sizeof(command->value)) {
|
||||
snprintf(err, err_len, "interactive: value too long");
|
||||
return -1;
|
||||
}
|
||||
snprintf(command->value, sizeof(command->value), "%s", cursor);
|
||||
|
||||
if (strcmp(action, "text") == 0) {
|
||||
command->type = INTERACTIVE_CMD_TEXT;
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(action, "file") == 0) {
|
||||
command->type = INTERACTIVE_CMD_FILE;
|
||||
return 0;
|
||||
}
|
||||
snprintf(err, err_len, "interactive: unknown command %s", action);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void interactive_print_help(FILE *out, const char *transport_name) {
|
||||
fprintf(out, "interactive mode commands (%s):\n", transport_name);
|
||||
fprintf(out, " help show this help\n");
|
||||
fprintf(out, " text <peer> <message> send one text message\n");
|
||||
fprintf(out, " file <peer> <path> send one file\n");
|
||||
fprintf(out, " quit exit this process\n");
|
||||
}
|
||||
Reference in New Issue
Block a user